pty.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * linux/drivers/char/pty.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * Added support for a Unix98-style ptmx device.
  7. * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
  8. *
  9. * When reading this code see also fs/devpts. In particular note that the
  10. * driver_data field is used by the devpts side as a binding to the devpts
  11. * inode.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/errno.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/tty.h>
  17. #include <linux/tty_flip.h>
  18. #include <linux/fcntl.h>
  19. #include <linux/string.h>
  20. #include <linux/major.h>
  21. #include <linux/mm.h>
  22. #include <linux/init.h>
  23. #include <linux/sysctl.h>
  24. #include <linux/device.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/bitops.h>
  27. #include <linux/devpts_fs.h>
  28. #include <asm/system.h>
  29. /* These are global because they are accessed in tty_io.c */
  30. #ifdef CONFIG_UNIX98_PTYS
  31. static struct tty_driver *ptm_driver;
  32. static struct tty_driver *pts_driver;
  33. #endif
  34. static void pty_close(struct tty_struct *tty, struct file *filp)
  35. {
  36. BUG_ON(!tty);
  37. if (tty->driver->subtype == PTY_TYPE_MASTER)
  38. WARN_ON(tty->count > 1);
  39. else {
  40. if (tty->count > 2)
  41. return;
  42. }
  43. wake_up_interruptible(&tty->read_wait);
  44. wake_up_interruptible(&tty->write_wait);
  45. tty->packet = 0;
  46. if (!tty->link)
  47. return;
  48. tty->link->packet = 0;
  49. set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  50. wake_up_interruptible(&tty->link->read_wait);
  51. wake_up_interruptible(&tty->link->write_wait);
  52. if (tty->driver->subtype == PTY_TYPE_MASTER) {
  53. set_bit(TTY_OTHER_CLOSED, &tty->flags);
  54. #ifdef CONFIG_UNIX98_PTYS
  55. if (tty->driver == ptm_driver)
  56. devpts_pty_kill(tty->link);
  57. #endif
  58. tty_vhangup(tty->link);
  59. }
  60. }
  61. /*
  62. * The unthrottle routine is called by the line discipline to signal
  63. * that it can receive more characters. For PTY's, the TTY_THROTTLED
  64. * flag is always set, to force the line discipline to always call the
  65. * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
  66. * characters in the queue. This is necessary since each time this
  67. * happens, we need to wake up any sleeping processes that could be
  68. * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
  69. * for the pty buffer to be drained.
  70. */
  71. static void pty_unthrottle(struct tty_struct *tty)
  72. {
  73. struct tty_struct *o_tty = tty->link;
  74. if (!o_tty)
  75. return;
  76. tty_wakeup(o_tty);
  77. set_bit(TTY_THROTTLED, &tty->flags);
  78. }
  79. /*
  80. * WSH 05/24/97: modified to
  81. * (1) use space in tty->flip instead of a shared temp buffer
  82. * The flip buffers aren't being used for a pty, so there's lots
  83. * of space available. The buffer is protected by a per-pty
  84. * semaphore that should almost never come under contention.
  85. * (2) avoid redundant copying for cases where count >> receive_room
  86. * N.B. Calls from user space may now return an error code instead of
  87. * a count.
  88. *
  89. * FIXME: Our pty_write method is called with our ldisc lock held but
  90. * not our partners. We can't just take the other one blindly without
  91. * risking deadlocks.
  92. */
  93. static int pty_write(struct tty_struct *tty, const unsigned char *buf,
  94. int count)
  95. {
  96. struct tty_struct *to = tty->link;
  97. int c;
  98. if (!to || tty->stopped)
  99. return 0;
  100. c = to->receive_room;
  101. if (c > count)
  102. c = count;
  103. to->ldisc.ops->receive_buf(to, buf, NULL, c);
  104. return c;
  105. }
  106. static int pty_write_room(struct tty_struct *tty)
  107. {
  108. struct tty_struct *to = tty->link;
  109. if (!to || tty->stopped)
  110. return 0;
  111. return to->receive_room;
  112. }
  113. /*
  114. * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior
  115. * The chars_in_buffer() value is used by the ldisc select() function
  116. * to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256).
  117. * The pty driver chars_in_buffer() Master/Slave must behave differently:
  118. *
  119. * The Master side needs to allow typed-ahead commands to accumulate
  120. * while being canonicalized, so we report "our buffer" as empty until
  121. * some threshold is reached, and then report the count. (Any count >
  122. * WAKEUP_CHARS is regarded by select() as "full".) To avoid deadlock
  123. * the count returned must be 0 if no canonical data is available to be
  124. * read. (The N_TTY ldisc.chars_in_buffer now knows this.)
  125. *
  126. * The Slave side passes all characters in raw mode to the Master side's
  127. * buffer where they can be read immediately, so in this case we can
  128. * return the true count in the buffer.
  129. */
  130. static int pty_chars_in_buffer(struct tty_struct *tty)
  131. {
  132. struct tty_struct *to = tty->link;
  133. int count;
  134. /* We should get the line discipline lock for "tty->link" */
  135. if (!to || !to->ldisc.ops->chars_in_buffer)
  136. return 0;
  137. /* The ldisc must report 0 if no characters available to be read */
  138. count = to->ldisc.ops->chars_in_buffer(to);
  139. if (tty->driver->subtype == PTY_TYPE_SLAVE)
  140. return count;
  141. /* Master side driver ... if the other side's read buffer is less than
  142. * half full, return 0 to allow writers to proceed; otherwise return
  143. * the count. This leaves a comfortable margin to avoid overflow,
  144. * and still allows half a buffer's worth of typed-ahead commands.
  145. */
  146. return (count < N_TTY_BUF_SIZE/2) ? 0 : count;
  147. }
  148. /* Set the lock flag on a pty */
  149. static int pty_set_lock(struct tty_struct *tty, int __user *arg)
  150. {
  151. int val;
  152. if (get_user(val, arg))
  153. return -EFAULT;
  154. if (val)
  155. set_bit(TTY_PTY_LOCK, &tty->flags);
  156. else
  157. clear_bit(TTY_PTY_LOCK, &tty->flags);
  158. return 0;
  159. }
  160. static void pty_flush_buffer(struct tty_struct *tty)
  161. {
  162. struct tty_struct *to = tty->link;
  163. unsigned long flags;
  164. if (!to)
  165. return;
  166. if (to->ldisc.ops->flush_buffer)
  167. to->ldisc.ops->flush_buffer(to);
  168. if (to->packet) {
  169. spin_lock_irqsave(&tty->ctrl_lock, flags);
  170. tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
  171. wake_up_interruptible(&to->read_wait);
  172. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  173. }
  174. }
  175. static int pty_open(struct tty_struct *tty, struct file *filp)
  176. {
  177. int retval = -ENODEV;
  178. if (!tty || !tty->link)
  179. goto out;
  180. retval = -EIO;
  181. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  182. goto out;
  183. if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
  184. goto out;
  185. if (tty->link->count != 1)
  186. goto out;
  187. clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  188. set_bit(TTY_THROTTLED, &tty->flags);
  189. retval = 0;
  190. out:
  191. return retval;
  192. }
  193. static void pty_set_termios(struct tty_struct *tty,
  194. struct ktermios *old_termios)
  195. {
  196. tty->termios->c_cflag &= ~(CSIZE | PARENB);
  197. tty->termios->c_cflag |= (CS8 | CREAD);
  198. }
  199. /**
  200. * pty_do_resize - resize event
  201. * @tty: tty being resized
  202. * @ws: window size being set.
  203. *
  204. * Update the termios variables and send the neccessary signals to
  205. * peform a terminal resize correctly
  206. */
  207. int pty_resize(struct tty_struct *tty, struct winsize *ws)
  208. {
  209. struct pid *pgrp, *rpgrp;
  210. unsigned long flags;
  211. struct tty_struct *pty = tty->link;
  212. /* For a PTY we need to lock the tty side */
  213. mutex_lock(&tty->termios_mutex);
  214. if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
  215. goto done;
  216. /* Get the PID values and reference them so we can
  217. avoid holding the tty ctrl lock while sending signals.
  218. We need to lock these individually however. */
  219. spin_lock_irqsave(&tty->ctrl_lock, flags);
  220. pgrp = get_pid(tty->pgrp);
  221. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  222. spin_lock_irqsave(&pty->ctrl_lock, flags);
  223. rpgrp = get_pid(pty->pgrp);
  224. spin_unlock_irqrestore(&pty->ctrl_lock, flags);
  225. if (pgrp)
  226. kill_pgrp(pgrp, SIGWINCH, 1);
  227. if (rpgrp != pgrp && rpgrp)
  228. kill_pgrp(rpgrp, SIGWINCH, 1);
  229. put_pid(pgrp);
  230. put_pid(rpgrp);
  231. tty->winsize = *ws;
  232. pty->winsize = *ws; /* Never used so will go away soon */
  233. done:
  234. mutex_unlock(&tty->termios_mutex);
  235. return 0;
  236. }
  237. static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
  238. {
  239. struct tty_struct *o_tty;
  240. int idx = tty->index;
  241. int retval;
  242. o_tty = alloc_tty_struct();
  243. if (!o_tty)
  244. return -ENOMEM;
  245. if (!try_module_get(driver->other->owner)) {
  246. /* This cannot in fact currently happen */
  247. free_tty_struct(o_tty);
  248. return -ENOMEM;
  249. }
  250. initialize_tty_struct(o_tty, driver->other, idx);
  251. /* We always use new tty termios data so we can do this
  252. the easy way .. */
  253. retval = tty_init_termios(tty);
  254. if (retval)
  255. goto free_mem_out;
  256. retval = tty_init_termios(o_tty);
  257. if (retval) {
  258. tty_free_termios(tty);
  259. goto free_mem_out;
  260. }
  261. /*
  262. * Everything allocated ... set up the o_tty structure.
  263. */
  264. driver->other->ttys[idx] = o_tty;
  265. tty_driver_kref_get(driver->other);
  266. if (driver->subtype == PTY_TYPE_MASTER)
  267. o_tty->count++;
  268. /* Establish the links in both directions */
  269. tty->link = o_tty;
  270. o_tty->link = tty;
  271. tty_driver_kref_get(driver);
  272. tty->count++;
  273. driver->ttys[idx] = tty;
  274. return 0;
  275. free_mem_out:
  276. module_put(o_tty->driver->owner);
  277. free_tty_struct(o_tty);
  278. return -ENOMEM;
  279. }
  280. static const struct tty_operations pty_ops = {
  281. .install = pty_install,
  282. .open = pty_open,
  283. .close = pty_close,
  284. .write = pty_write,
  285. .write_room = pty_write_room,
  286. .flush_buffer = pty_flush_buffer,
  287. .chars_in_buffer = pty_chars_in_buffer,
  288. .unthrottle = pty_unthrottle,
  289. .set_termios = pty_set_termios,
  290. .resize = pty_resize
  291. };
  292. /* Traditional BSD devices */
  293. #ifdef CONFIG_LEGACY_PTYS
  294. static struct tty_driver *pty_driver, *pty_slave_driver;
  295. static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file,
  296. unsigned int cmd, unsigned long arg)
  297. {
  298. switch (cmd) {
  299. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  300. return pty_set_lock(tty, (int __user *) arg);
  301. }
  302. return -ENOIOCTLCMD;
  303. }
  304. static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
  305. module_param(legacy_count, int, 0);
  306. static const struct tty_operations pty_ops_bsd = {
  307. .open = pty_open,
  308. .close = pty_close,
  309. .write = pty_write,
  310. .write_room = pty_write_room,
  311. .flush_buffer = pty_flush_buffer,
  312. .chars_in_buffer = pty_chars_in_buffer,
  313. .unthrottle = pty_unthrottle,
  314. .set_termios = pty_set_termios,
  315. .ioctl = pty_bsd_ioctl,
  316. .resize = pty_resize
  317. };
  318. static void __init legacy_pty_init(void)
  319. {
  320. if (legacy_count <= 0)
  321. return;
  322. pty_driver = alloc_tty_driver(legacy_count);
  323. if (!pty_driver)
  324. panic("Couldn't allocate pty driver");
  325. pty_slave_driver = alloc_tty_driver(legacy_count);
  326. if (!pty_slave_driver)
  327. panic("Couldn't allocate pty slave driver");
  328. pty_driver->owner = THIS_MODULE;
  329. pty_driver->driver_name = "pty_master";
  330. pty_driver->name = "pty";
  331. pty_driver->major = PTY_MASTER_MAJOR;
  332. pty_driver->minor_start = 0;
  333. pty_driver->type = TTY_DRIVER_TYPE_PTY;
  334. pty_driver->subtype = PTY_TYPE_MASTER;
  335. pty_driver->init_termios = tty_std_termios;
  336. pty_driver->init_termios.c_iflag = 0;
  337. pty_driver->init_termios.c_oflag = 0;
  338. pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  339. pty_driver->init_termios.c_lflag = 0;
  340. pty_driver->init_termios.c_ispeed = 38400;
  341. pty_driver->init_termios.c_ospeed = 38400;
  342. pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
  343. pty_driver->other = pty_slave_driver;
  344. tty_set_operations(pty_driver, &pty_ops);
  345. pty_slave_driver->owner = THIS_MODULE;
  346. pty_slave_driver->driver_name = "pty_slave";
  347. pty_slave_driver->name = "ttyp";
  348. pty_slave_driver->major = PTY_SLAVE_MAJOR;
  349. pty_slave_driver->minor_start = 0;
  350. pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
  351. pty_slave_driver->subtype = PTY_TYPE_SLAVE;
  352. pty_slave_driver->init_termios = tty_std_termios;
  353. pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  354. pty_slave_driver->init_termios.c_ispeed = 38400;
  355. pty_slave_driver->init_termios.c_ospeed = 38400;
  356. pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
  357. TTY_DRIVER_REAL_RAW;
  358. pty_slave_driver->other = pty_driver;
  359. tty_set_operations(pty_slave_driver, &pty_ops);
  360. if (tty_register_driver(pty_driver))
  361. panic("Couldn't register pty driver");
  362. if (tty_register_driver(pty_slave_driver))
  363. panic("Couldn't register pty slave driver");
  364. }
  365. #else
  366. static inline void legacy_pty_init(void) { }
  367. #endif
  368. /* Unix98 devices */
  369. #ifdef CONFIG_UNIX98_PTYS
  370. /*
  371. * sysctl support for setting limits on the number of Unix98 ptys allocated.
  372. * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
  373. */
  374. int pty_limit = NR_UNIX98_PTY_DEFAULT;
  375. static int pty_limit_min;
  376. static int pty_limit_max = NR_UNIX98_PTY_MAX;
  377. static int pty_count;
  378. static struct cdev ptmx_cdev;
  379. static struct ctl_table pty_table[] = {
  380. {
  381. .ctl_name = PTY_MAX,
  382. .procname = "max",
  383. .maxlen = sizeof(int),
  384. .mode = 0644,
  385. .data = &pty_limit,
  386. .proc_handler = &proc_dointvec_minmax,
  387. .strategy = &sysctl_intvec,
  388. .extra1 = &pty_limit_min,
  389. .extra2 = &pty_limit_max,
  390. }, {
  391. .ctl_name = PTY_NR,
  392. .procname = "nr",
  393. .maxlen = sizeof(int),
  394. .mode = 0444,
  395. .data = &pty_count,
  396. .proc_handler = &proc_dointvec,
  397. }, {
  398. .ctl_name = 0
  399. }
  400. };
  401. static struct ctl_table pty_kern_table[] = {
  402. {
  403. .ctl_name = KERN_PTY,
  404. .procname = "pty",
  405. .mode = 0555,
  406. .child = pty_table,
  407. },
  408. {}
  409. };
  410. static struct ctl_table pty_root_table[] = {
  411. {
  412. .ctl_name = CTL_KERN,
  413. .procname = "kernel",
  414. .mode = 0555,
  415. .child = pty_kern_table,
  416. },
  417. {}
  418. };
  419. static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file,
  420. unsigned int cmd, unsigned long arg)
  421. {
  422. switch (cmd) {
  423. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  424. return pty_set_lock(tty, (int __user *)arg);
  425. case TIOCGPTN: /* Get PT Number */
  426. return put_user(tty->index, (unsigned int __user *)arg);
  427. }
  428. return -ENOIOCTLCMD;
  429. }
  430. /**
  431. * ptm_unix98_lookup - find a pty master
  432. * @driver: ptm driver
  433. * @idx: tty index
  434. *
  435. * Look up a pty master device. Called under the tty_mutex for now.
  436. * This provides our locking.
  437. */
  438. static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
  439. struct inode *ptm_inode, int idx)
  440. {
  441. struct tty_struct *tty = devpts_get_tty(ptm_inode, idx);
  442. if (tty)
  443. tty = tty->link;
  444. return tty;
  445. }
  446. /**
  447. * pts_unix98_lookup - find a pty slave
  448. * @driver: pts driver
  449. * @idx: tty index
  450. *
  451. * Look up a pty master device. Called under the tty_mutex for now.
  452. * This provides our locking.
  453. */
  454. static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
  455. struct inode *pts_inode, int idx)
  456. {
  457. struct tty_struct *tty = devpts_get_tty(pts_inode, idx);
  458. /* Master must be open before slave */
  459. if (!tty)
  460. return ERR_PTR(-EIO);
  461. return tty;
  462. }
  463. static void pty_unix98_shutdown(struct tty_struct *tty)
  464. {
  465. /* We have our own method as we don't use the tty index */
  466. kfree(tty->termios);
  467. }
  468. /* We have no need to install and remove our tty objects as devpts does all
  469. the work for us */
  470. static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
  471. {
  472. struct tty_struct *o_tty;
  473. int idx = tty->index;
  474. o_tty = alloc_tty_struct();
  475. if (!o_tty)
  476. return -ENOMEM;
  477. if (!try_module_get(driver->other->owner)) {
  478. /* This cannot in fact currently happen */
  479. free_tty_struct(o_tty);
  480. return -ENOMEM;
  481. }
  482. initialize_tty_struct(o_tty, driver->other, idx);
  483. tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
  484. if (tty->termios == NULL)
  485. goto free_mem_out;
  486. *tty->termios = driver->init_termios;
  487. tty->termios_locked = tty->termios + 1;
  488. o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
  489. if (o_tty->termios == NULL)
  490. goto free_mem_out;
  491. *o_tty->termios = driver->other->init_termios;
  492. o_tty->termios_locked = o_tty->termios + 1;
  493. tty_driver_kref_get(driver->other);
  494. if (driver->subtype == PTY_TYPE_MASTER)
  495. o_tty->count++;
  496. /* Establish the links in both directions */
  497. tty->link = o_tty;
  498. o_tty->link = tty;
  499. /*
  500. * All structures have been allocated, so now we install them.
  501. * Failures after this point use release_tty to clean up, so
  502. * there's no need to null out the local pointers.
  503. */
  504. tty_driver_kref_get(driver);
  505. tty->count++;
  506. pty_count++;
  507. return 0;
  508. free_mem_out:
  509. kfree(o_tty->termios);
  510. module_put(o_tty->driver->owner);
  511. free_tty_struct(o_tty);
  512. kfree(tty->termios);
  513. return -ENOMEM;
  514. }
  515. static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
  516. {
  517. pty_count--;
  518. }
  519. static const struct tty_operations ptm_unix98_ops = {
  520. .lookup = ptm_unix98_lookup,
  521. .install = pty_unix98_install,
  522. .remove = pty_unix98_remove,
  523. .open = pty_open,
  524. .close = pty_close,
  525. .write = pty_write,
  526. .write_room = pty_write_room,
  527. .flush_buffer = pty_flush_buffer,
  528. .chars_in_buffer = pty_chars_in_buffer,
  529. .unthrottle = pty_unthrottle,
  530. .set_termios = pty_set_termios,
  531. .ioctl = pty_unix98_ioctl,
  532. .shutdown = pty_unix98_shutdown,
  533. .resize = pty_resize
  534. };
  535. static const struct tty_operations pty_unix98_ops = {
  536. .lookup = pts_unix98_lookup,
  537. .install = pty_unix98_install,
  538. .remove = pty_unix98_remove,
  539. .open = pty_open,
  540. .close = pty_close,
  541. .write = pty_write,
  542. .write_room = pty_write_room,
  543. .flush_buffer = pty_flush_buffer,
  544. .chars_in_buffer = pty_chars_in_buffer,
  545. .unthrottle = pty_unthrottle,
  546. .set_termios = pty_set_termios,
  547. .shutdown = pty_unix98_shutdown
  548. };
  549. /**
  550. * ptmx_open - open a unix 98 pty master
  551. * @inode: inode of device file
  552. * @filp: file pointer to tty
  553. *
  554. * Allocate a unix98 pty master device from the ptmx driver.
  555. *
  556. * Locking: tty_mutex protects the init_dev work. tty->count should
  557. * protect the rest.
  558. * allocated_ptys_lock handles the list of free pty numbers
  559. */
  560. static int __ptmx_open(struct inode *inode, struct file *filp)
  561. {
  562. struct tty_struct *tty;
  563. int retval;
  564. int index;
  565. nonseekable_open(inode, filp);
  566. /* find a device that is not in use. */
  567. index = devpts_new_index(inode);
  568. if (index < 0)
  569. return index;
  570. mutex_lock(&tty_mutex);
  571. tty = tty_init_dev(ptm_driver, index, 1);
  572. mutex_unlock(&tty_mutex);
  573. if (IS_ERR(tty)) {
  574. retval = PTR_ERR(tty);
  575. goto out;
  576. }
  577. set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
  578. filp->private_data = tty;
  579. file_move(filp, &tty->tty_files);
  580. retval = devpts_pty_new(inode, tty->link);
  581. if (retval)
  582. goto out1;
  583. retval = ptm_driver->ops->open(tty, filp);
  584. if (!retval)
  585. return 0;
  586. out1:
  587. tty_release_dev(filp);
  588. return retval;
  589. out:
  590. devpts_kill_index(inode, index);
  591. return retval;
  592. }
  593. static int ptmx_open(struct inode *inode, struct file *filp)
  594. {
  595. int ret;
  596. lock_kernel();
  597. ret = __ptmx_open(inode, filp);
  598. unlock_kernel();
  599. return ret;
  600. }
  601. static struct file_operations ptmx_fops;
  602. static void __init unix98_pty_init(void)
  603. {
  604. ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
  605. if (!ptm_driver)
  606. panic("Couldn't allocate Unix98 ptm driver");
  607. pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
  608. if (!pts_driver)
  609. panic("Couldn't allocate Unix98 pts driver");
  610. ptm_driver->owner = THIS_MODULE;
  611. ptm_driver->driver_name = "pty_master";
  612. ptm_driver->name = "ptm";
  613. ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
  614. ptm_driver->minor_start = 0;
  615. ptm_driver->type = TTY_DRIVER_TYPE_PTY;
  616. ptm_driver->subtype = PTY_TYPE_MASTER;
  617. ptm_driver->init_termios = tty_std_termios;
  618. ptm_driver->init_termios.c_iflag = 0;
  619. ptm_driver->init_termios.c_oflag = 0;
  620. ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  621. ptm_driver->init_termios.c_lflag = 0;
  622. ptm_driver->init_termios.c_ispeed = 38400;
  623. ptm_driver->init_termios.c_ospeed = 38400;
  624. ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  625. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
  626. ptm_driver->other = pts_driver;
  627. tty_set_operations(ptm_driver, &ptm_unix98_ops);
  628. pts_driver->owner = THIS_MODULE;
  629. pts_driver->driver_name = "pty_slave";
  630. pts_driver->name = "pts";
  631. pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
  632. pts_driver->minor_start = 0;
  633. pts_driver->type = TTY_DRIVER_TYPE_PTY;
  634. pts_driver->subtype = PTY_TYPE_SLAVE;
  635. pts_driver->init_termios = tty_std_termios;
  636. pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  637. pts_driver->init_termios.c_ispeed = 38400;
  638. pts_driver->init_termios.c_ospeed = 38400;
  639. pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  640. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
  641. pts_driver->other = ptm_driver;
  642. tty_set_operations(pts_driver, &pty_unix98_ops);
  643. if (tty_register_driver(ptm_driver))
  644. panic("Couldn't register Unix98 ptm driver");
  645. if (tty_register_driver(pts_driver))
  646. panic("Couldn't register Unix98 pts driver");
  647. register_sysctl_table(pty_root_table);
  648. /* Now create the /dev/ptmx special device */
  649. tty_default_fops(&ptmx_fops);
  650. ptmx_fops.open = ptmx_open;
  651. cdev_init(&ptmx_cdev, &ptmx_fops);
  652. if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
  653. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
  654. panic("Couldn't register /dev/ptmx driver\n");
  655. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
  656. }
  657. #else
  658. static inline void unix98_pty_init(void) { }
  659. #endif
  660. static int __init pty_init(void)
  661. {
  662. legacy_pty_init();
  663. unix98_pty_init();
  664. return 0;
  665. }
  666. module_init(pty_init);