pty.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. *
  4. * Added support for a Unix98-style ptmx device.
  5. * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
  6. *
  7. * When reading this code see also fs/devpts. In particular note that the
  8. * driver_data field is used by the devpts side as a binding to the devpts
  9. * inode.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/errno.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/tty.h>
  15. #include <linux/tty_flip.h>
  16. #include <linux/fcntl.h>
  17. #include <linux/sched.h>
  18. #include <linux/string.h>
  19. #include <linux/major.h>
  20. #include <linux/mm.h>
  21. #include <linux/init.h>
  22. #include <linux/device.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/bitops.h>
  25. #include <linux/devpts_fs.h>
  26. #include <linux/slab.h>
  27. #include <linux/mutex.h>
  28. #ifdef CONFIG_UNIX98_PTYS
  29. static struct tty_driver *ptm_driver;
  30. static struct tty_driver *pts_driver;
  31. static DEFINE_MUTEX(devpts_mutex);
  32. #endif
  33. static void pty_close(struct tty_struct *tty, struct file *filp)
  34. {
  35. BUG_ON(!tty);
  36. if (tty->driver->subtype == PTY_TYPE_MASTER)
  37. WARN_ON(tty->count > 1);
  38. else {
  39. if (tty->count > 2)
  40. return;
  41. }
  42. wake_up_interruptible(&tty->read_wait);
  43. wake_up_interruptible(&tty->write_wait);
  44. tty->packet = 0;
  45. /* Review - krefs on tty_link ?? */
  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. mutex_lock(&devpts_mutex);
  57. devpts_pty_kill(tty->link);
  58. mutex_unlock(&devpts_mutex);
  59. }
  60. #endif
  61. tty_unlock(tty);
  62. tty_vhangup(tty->link);
  63. tty_lock(tty);
  64. }
  65. }
  66. /*
  67. * The unthrottle routine is called by the line discipline to signal
  68. * that it can receive more characters. For PTY's, the TTY_THROTTLED
  69. * flag is always set, to force the line discipline to always call the
  70. * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
  71. * characters in the queue. This is necessary since each time this
  72. * happens, we need to wake up any sleeping processes that could be
  73. * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
  74. * for the pty buffer to be drained.
  75. */
  76. static void pty_unthrottle(struct tty_struct *tty)
  77. {
  78. tty_wakeup(tty->link);
  79. set_bit(TTY_THROTTLED, &tty->flags);
  80. }
  81. /**
  82. * pty_space - report space left for writing
  83. * @to: tty we are writing into
  84. *
  85. * The tty buffers allow 64K but we sneak a peak and clip at 8K this
  86. * allows a lot of overspill room for echo and other fun messes to
  87. * be handled properly
  88. */
  89. static int pty_space(struct tty_struct *to)
  90. {
  91. int n = 8192 - to->buf.memory_used;
  92. if (n < 0)
  93. return 0;
  94. return n;
  95. }
  96. /**
  97. * pty_write - write to a pty
  98. * @tty: the tty we write from
  99. * @buf: kernel buffer of data
  100. * @count: bytes to write
  101. *
  102. * Our "hardware" write method. Data is coming from the ldisc which
  103. * may be in a non sleeping state. We simply throw this at the other
  104. * end of the link as if we were an IRQ handler receiving stuff for
  105. * the other side of the pty/tty pair.
  106. */
  107. static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
  108. {
  109. struct tty_struct *to = tty->link;
  110. if (tty->stopped)
  111. return 0;
  112. if (c > 0) {
  113. /* Stuff the data into the input queue of the other end */
  114. c = tty_insert_flip_string(to, buf, c);
  115. /* And shovel */
  116. if (c) {
  117. tty_flip_buffer_push(to);
  118. tty_wakeup(tty);
  119. }
  120. }
  121. return c;
  122. }
  123. /**
  124. * pty_write_room - write space
  125. * @tty: tty we are writing from
  126. *
  127. * Report how many bytes the ldisc can send into the queue for
  128. * the other device.
  129. */
  130. static int pty_write_room(struct tty_struct *tty)
  131. {
  132. if (tty->stopped)
  133. return 0;
  134. return pty_space(tty->link);
  135. }
  136. /**
  137. * pty_chars_in_buffer - characters currently in our tx queue
  138. * @tty: our tty
  139. *
  140. * Report how much we have in the transmit queue. As everything is
  141. * instantly at the other end this is easy to implement.
  142. */
  143. static int pty_chars_in_buffer(struct tty_struct *tty)
  144. {
  145. return 0;
  146. }
  147. /* Set the lock flag on a pty */
  148. static int pty_set_lock(struct tty_struct *tty, int __user *arg)
  149. {
  150. int val;
  151. if (get_user(val, arg))
  152. return -EFAULT;
  153. if (val)
  154. set_bit(TTY_PTY_LOCK, &tty->flags);
  155. else
  156. clear_bit(TTY_PTY_LOCK, &tty->flags);
  157. return 0;
  158. }
  159. /* Send a signal to the slave */
  160. static int pty_signal(struct tty_struct *tty, int sig)
  161. {
  162. unsigned long flags;
  163. struct pid *pgrp;
  164. if (tty->link) {
  165. spin_lock_irqsave(&tty->link->ctrl_lock, flags);
  166. pgrp = get_pid(tty->link->pgrp);
  167. spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
  168. kill_pgrp(pgrp, sig, 1);
  169. put_pid(pgrp);
  170. }
  171. return 0;
  172. }
  173. static void pty_flush_buffer(struct tty_struct *tty)
  174. {
  175. struct tty_struct *to = tty->link;
  176. unsigned long flags;
  177. if (!to)
  178. return;
  179. /* tty_buffer_flush(to); FIXME */
  180. if (to->packet) {
  181. spin_lock_irqsave(&tty->ctrl_lock, flags);
  182. tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
  183. wake_up_interruptible(&to->read_wait);
  184. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  185. }
  186. }
  187. static int pty_open(struct tty_struct *tty, struct file *filp)
  188. {
  189. int retval = -ENODEV;
  190. if (!tty || !tty->link)
  191. goto out;
  192. retval = -EIO;
  193. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  194. goto out;
  195. if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
  196. goto out;
  197. if (tty->link->count != 1)
  198. goto out;
  199. clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  200. set_bit(TTY_THROTTLED, &tty->flags);
  201. retval = 0;
  202. out:
  203. return retval;
  204. }
  205. static void pty_set_termios(struct tty_struct *tty,
  206. struct ktermios *old_termios)
  207. {
  208. tty->termios.c_cflag &= ~(CSIZE | PARENB);
  209. tty->termios.c_cflag |= (CS8 | CREAD);
  210. }
  211. /**
  212. * pty_do_resize - resize event
  213. * @tty: tty being resized
  214. * @ws: window size being set.
  215. *
  216. * Update the termios variables and send the necessary signals to
  217. * peform a terminal resize correctly
  218. */
  219. int pty_resize(struct tty_struct *tty, struct winsize *ws)
  220. {
  221. struct pid *pgrp, *rpgrp;
  222. unsigned long flags;
  223. struct tty_struct *pty = tty->link;
  224. /* For a PTY we need to lock the tty side */
  225. mutex_lock(&tty->termios_mutex);
  226. if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
  227. goto done;
  228. /* Get the PID values and reference them so we can
  229. avoid holding the tty ctrl lock while sending signals.
  230. We need to lock these individually however. */
  231. spin_lock_irqsave(&tty->ctrl_lock, flags);
  232. pgrp = get_pid(tty->pgrp);
  233. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  234. spin_lock_irqsave(&pty->ctrl_lock, flags);
  235. rpgrp = get_pid(pty->pgrp);
  236. spin_unlock_irqrestore(&pty->ctrl_lock, flags);
  237. if (pgrp)
  238. kill_pgrp(pgrp, SIGWINCH, 1);
  239. if (rpgrp != pgrp && rpgrp)
  240. kill_pgrp(rpgrp, SIGWINCH, 1);
  241. put_pid(pgrp);
  242. put_pid(rpgrp);
  243. tty->winsize = *ws;
  244. pty->winsize = *ws; /* Never used so will go away soon */
  245. done:
  246. mutex_unlock(&tty->termios_mutex);
  247. return 0;
  248. }
  249. /**
  250. * pty_common_install - set up the pty pair
  251. * @driver: the pty driver
  252. * @tty: the tty being instantiated
  253. * @bool: legacy, true if this is BSD style
  254. *
  255. * Perform the initial set up for the tty/pty pair. Called from the
  256. * tty layer when the port is first opened.
  257. *
  258. * Locking: the caller must hold the tty_mutex
  259. */
  260. static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
  261. bool legacy)
  262. {
  263. struct tty_struct *o_tty;
  264. struct tty_port *ports[2];
  265. int idx = tty->index;
  266. int retval = -ENOMEM;
  267. o_tty = alloc_tty_struct();
  268. if (!o_tty)
  269. goto err;
  270. ports[0] = kmalloc(sizeof **ports, GFP_KERNEL);
  271. ports[1] = kmalloc(sizeof **ports, GFP_KERNEL);
  272. if (!ports[0] || !ports[1])
  273. goto err_free_tty;
  274. if (!try_module_get(driver->other->owner)) {
  275. /* This cannot in fact currently happen */
  276. goto err_free_tty;
  277. }
  278. initialize_tty_struct(o_tty, driver->other, idx);
  279. if (legacy) {
  280. /* We always use new tty termios data so we can do this
  281. the easy way .. */
  282. retval = tty_init_termios(tty);
  283. if (retval)
  284. goto err_deinit_tty;
  285. retval = tty_init_termios(o_tty);
  286. if (retval)
  287. goto err_free_termios;
  288. driver->other->ttys[idx] = o_tty;
  289. driver->ttys[idx] = tty;
  290. } else {
  291. memset(&tty->termios_locked, 0, sizeof(tty->termios_locked));
  292. tty->termios = driver->init_termios;
  293. memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked));
  294. o_tty->termios = driver->other->init_termios;
  295. }
  296. /*
  297. * Everything allocated ... set up the o_tty structure.
  298. */
  299. tty_driver_kref_get(driver->other);
  300. if (driver->subtype == PTY_TYPE_MASTER)
  301. o_tty->count++;
  302. /* Establish the links in both directions */
  303. tty->link = o_tty;
  304. o_tty->link = tty;
  305. tty_port_init(ports[0]);
  306. tty_port_init(ports[1]);
  307. o_tty->port = ports[0];
  308. tty->port = ports[1];
  309. tty_driver_kref_get(driver);
  310. tty->count++;
  311. return 0;
  312. err_free_termios:
  313. if (legacy)
  314. tty_free_termios(tty);
  315. err_deinit_tty:
  316. deinitialize_tty_struct(o_tty);
  317. module_put(o_tty->driver->owner);
  318. err_free_tty:
  319. kfree(ports[0]);
  320. kfree(ports[1]);
  321. free_tty_struct(o_tty);
  322. err:
  323. return retval;
  324. }
  325. static void pty_cleanup(struct tty_struct *tty)
  326. {
  327. kfree(tty->port);
  328. }
  329. /* Traditional BSD devices */
  330. #ifdef CONFIG_LEGACY_PTYS
  331. static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
  332. {
  333. return pty_common_install(driver, tty, true);
  334. }
  335. static void pty_remove(struct tty_driver *driver, struct tty_struct *tty)
  336. {
  337. struct tty_struct *pair = tty->link;
  338. driver->ttys[tty->index] = NULL;
  339. if (pair)
  340. pair->driver->ttys[pair->index] = NULL;
  341. }
  342. static int pty_bsd_ioctl(struct tty_struct *tty,
  343. unsigned int cmd, unsigned long arg)
  344. {
  345. switch (cmd) {
  346. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  347. return pty_set_lock(tty, (int __user *) arg);
  348. case TIOCSIG: /* Send signal to other side of pty */
  349. return pty_signal(tty, (int) arg);
  350. }
  351. return -ENOIOCTLCMD;
  352. }
  353. static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
  354. module_param(legacy_count, int, 0);
  355. /*
  356. * The master side of a pty can do TIOCSPTLCK and thus
  357. * has pty_bsd_ioctl.
  358. */
  359. static const struct tty_operations master_pty_ops_bsd = {
  360. .install = pty_install,
  361. .open = pty_open,
  362. .close = pty_close,
  363. .write = pty_write,
  364. .write_room = pty_write_room,
  365. .flush_buffer = pty_flush_buffer,
  366. .chars_in_buffer = pty_chars_in_buffer,
  367. .unthrottle = pty_unthrottle,
  368. .set_termios = pty_set_termios,
  369. .ioctl = pty_bsd_ioctl,
  370. .cleanup = pty_cleanup,
  371. .resize = pty_resize,
  372. .remove = pty_remove
  373. };
  374. static const struct tty_operations slave_pty_ops_bsd = {
  375. .install = pty_install,
  376. .open = pty_open,
  377. .close = pty_close,
  378. .write = pty_write,
  379. .write_room = pty_write_room,
  380. .flush_buffer = pty_flush_buffer,
  381. .chars_in_buffer = pty_chars_in_buffer,
  382. .unthrottle = pty_unthrottle,
  383. .set_termios = pty_set_termios,
  384. .cleanup = pty_cleanup,
  385. .resize = pty_resize,
  386. .remove = pty_remove
  387. };
  388. static void __init legacy_pty_init(void)
  389. {
  390. struct tty_driver *pty_driver, *pty_slave_driver;
  391. if (legacy_count <= 0)
  392. return;
  393. pty_driver = tty_alloc_driver(legacy_count,
  394. TTY_DRIVER_RESET_TERMIOS |
  395. TTY_DRIVER_REAL_RAW |
  396. TTY_DRIVER_DYNAMIC_ALLOC);
  397. if (!pty_driver)
  398. panic("Couldn't allocate pty driver");
  399. pty_slave_driver = tty_alloc_driver(legacy_count,
  400. TTY_DRIVER_RESET_TERMIOS |
  401. TTY_DRIVER_REAL_RAW |
  402. TTY_DRIVER_DYNAMIC_ALLOC);
  403. if (!pty_slave_driver)
  404. panic("Couldn't allocate pty slave driver");
  405. pty_driver->driver_name = "pty_master";
  406. pty_driver->name = "pty";
  407. pty_driver->major = PTY_MASTER_MAJOR;
  408. pty_driver->minor_start = 0;
  409. pty_driver->type = TTY_DRIVER_TYPE_PTY;
  410. pty_driver->subtype = PTY_TYPE_MASTER;
  411. pty_driver->init_termios = tty_std_termios;
  412. pty_driver->init_termios.c_iflag = 0;
  413. pty_driver->init_termios.c_oflag = 0;
  414. pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  415. pty_driver->init_termios.c_lflag = 0;
  416. pty_driver->init_termios.c_ispeed = 38400;
  417. pty_driver->init_termios.c_ospeed = 38400;
  418. pty_driver->other = pty_slave_driver;
  419. tty_set_operations(pty_driver, &master_pty_ops_bsd);
  420. pty_slave_driver->driver_name = "pty_slave";
  421. pty_slave_driver->name = "ttyp";
  422. pty_slave_driver->major = PTY_SLAVE_MAJOR;
  423. pty_slave_driver->minor_start = 0;
  424. pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
  425. pty_slave_driver->subtype = PTY_TYPE_SLAVE;
  426. pty_slave_driver->init_termios = tty_std_termios;
  427. pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  428. pty_slave_driver->init_termios.c_ispeed = 38400;
  429. pty_slave_driver->init_termios.c_ospeed = 38400;
  430. pty_slave_driver->other = pty_driver;
  431. tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
  432. if (tty_register_driver(pty_driver))
  433. panic("Couldn't register pty driver");
  434. if (tty_register_driver(pty_slave_driver))
  435. panic("Couldn't register pty slave driver");
  436. }
  437. #else
  438. static inline void legacy_pty_init(void) { }
  439. #endif
  440. /* Unix98 devices */
  441. #ifdef CONFIG_UNIX98_PTYS
  442. static struct cdev ptmx_cdev;
  443. static int pty_unix98_ioctl(struct tty_struct *tty,
  444. unsigned int cmd, unsigned long arg)
  445. {
  446. switch (cmd) {
  447. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  448. return pty_set_lock(tty, (int __user *)arg);
  449. case TIOCGPTN: /* Get PT Number */
  450. return put_user(tty->index, (unsigned int __user *)arg);
  451. case TIOCSIG: /* Send signal to other side of pty */
  452. return pty_signal(tty, (int) arg);
  453. }
  454. return -ENOIOCTLCMD;
  455. }
  456. /**
  457. * ptm_unix98_lookup - find a pty master
  458. * @driver: ptm driver
  459. * @idx: tty index
  460. *
  461. * Look up a pty master device. Called under the tty_mutex for now.
  462. * This provides our locking.
  463. */
  464. static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
  465. struct inode *ptm_inode, int idx)
  466. {
  467. /* Master must be open via /dev/ptmx */
  468. return ERR_PTR(-EIO);
  469. }
  470. /**
  471. * pts_unix98_lookup - find a pty slave
  472. * @driver: pts driver
  473. * @idx: tty index
  474. *
  475. * Look up a pty master device. Called under the tty_mutex for now.
  476. * This provides our locking for the tty pointer.
  477. */
  478. static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
  479. struct inode *pts_inode, int idx)
  480. {
  481. struct tty_struct *tty;
  482. mutex_lock(&devpts_mutex);
  483. tty = devpts_get_tty(pts_inode, idx);
  484. mutex_unlock(&devpts_mutex);
  485. /* Master must be open before slave */
  486. if (!tty)
  487. return ERR_PTR(-EIO);
  488. return tty;
  489. }
  490. /* We have no need to install and remove our tty objects as devpts does all
  491. the work for us */
  492. static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
  493. {
  494. return pty_common_install(driver, tty, false);
  495. }
  496. static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
  497. {
  498. }
  499. static const struct tty_operations ptm_unix98_ops = {
  500. .lookup = ptm_unix98_lookup,
  501. .install = pty_unix98_install,
  502. .remove = pty_unix98_remove,
  503. .open = pty_open,
  504. .close = pty_close,
  505. .write = pty_write,
  506. .write_room = pty_write_room,
  507. .flush_buffer = pty_flush_buffer,
  508. .chars_in_buffer = pty_chars_in_buffer,
  509. .unthrottle = pty_unthrottle,
  510. .set_termios = pty_set_termios,
  511. .ioctl = pty_unix98_ioctl,
  512. .resize = pty_resize,
  513. .cleanup = pty_cleanup
  514. };
  515. static const struct tty_operations pty_unix98_ops = {
  516. .lookup = pts_unix98_lookup,
  517. .install = pty_unix98_install,
  518. .remove = pty_unix98_remove,
  519. .open = pty_open,
  520. .close = pty_close,
  521. .write = pty_write,
  522. .write_room = pty_write_room,
  523. .flush_buffer = pty_flush_buffer,
  524. .chars_in_buffer = pty_chars_in_buffer,
  525. .unthrottle = pty_unthrottle,
  526. .set_termios = pty_set_termios,
  527. .cleanup = pty_cleanup,
  528. };
  529. /**
  530. * ptmx_open - open a unix 98 pty master
  531. * @inode: inode of device file
  532. * @filp: file pointer to tty
  533. *
  534. * Allocate a unix98 pty master device from the ptmx driver.
  535. *
  536. * Locking: tty_mutex protects the init_dev work. tty->count should
  537. * protect the rest.
  538. * allocated_ptys_lock handles the list of free pty numbers
  539. */
  540. static int ptmx_open(struct inode *inode, struct file *filp)
  541. {
  542. struct tty_struct *tty;
  543. int retval;
  544. int index;
  545. nonseekable_open(inode, filp);
  546. retval = tty_alloc_file(filp);
  547. if (retval)
  548. return retval;
  549. /* find a device that is not in use. */
  550. mutex_lock(&devpts_mutex);
  551. index = devpts_new_index(inode);
  552. if (index < 0) {
  553. retval = index;
  554. goto err_file;
  555. }
  556. mutex_unlock(&devpts_mutex);
  557. mutex_lock(&tty_mutex);
  558. tty = tty_init_dev(ptm_driver, index);
  559. if (IS_ERR(tty)) {
  560. retval = PTR_ERR(tty);
  561. goto out;
  562. }
  563. /* The tty returned here is locked so we can safely
  564. drop the mutex */
  565. mutex_unlock(&tty_mutex);
  566. set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
  567. tty_add_file(tty, filp);
  568. retval = devpts_pty_new(inode, tty->link);
  569. if (retval)
  570. goto err_release;
  571. retval = ptm_driver->ops->open(tty, filp);
  572. if (retval)
  573. goto err_release;
  574. tty_unlock(tty);
  575. return 0;
  576. err_release:
  577. tty_unlock(tty);
  578. tty_release(inode, filp);
  579. return retval;
  580. out:
  581. mutex_unlock(&tty_mutex);
  582. devpts_kill_index(inode, index);
  583. err_file:
  584. mutex_unlock(&devpts_mutex);
  585. tty_free_file(filp);
  586. return retval;
  587. }
  588. static struct file_operations ptmx_fops;
  589. static void __init unix98_pty_init(void)
  590. {
  591. ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
  592. TTY_DRIVER_RESET_TERMIOS |
  593. TTY_DRIVER_REAL_RAW |
  594. TTY_DRIVER_DYNAMIC_DEV |
  595. TTY_DRIVER_DEVPTS_MEM |
  596. TTY_DRIVER_DYNAMIC_ALLOC);
  597. if (!ptm_driver)
  598. panic("Couldn't allocate Unix98 ptm driver");
  599. pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
  600. TTY_DRIVER_RESET_TERMIOS |
  601. TTY_DRIVER_REAL_RAW |
  602. TTY_DRIVER_DYNAMIC_DEV |
  603. TTY_DRIVER_DEVPTS_MEM |
  604. TTY_DRIVER_DYNAMIC_ALLOC);
  605. if (!pts_driver)
  606. panic("Couldn't allocate Unix98 pts driver");
  607. ptm_driver->driver_name = "pty_master";
  608. ptm_driver->name = "ptm";
  609. ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
  610. ptm_driver->minor_start = 0;
  611. ptm_driver->type = TTY_DRIVER_TYPE_PTY;
  612. ptm_driver->subtype = PTY_TYPE_MASTER;
  613. ptm_driver->init_termios = tty_std_termios;
  614. ptm_driver->init_termios.c_iflag = 0;
  615. ptm_driver->init_termios.c_oflag = 0;
  616. ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  617. ptm_driver->init_termios.c_lflag = 0;
  618. ptm_driver->init_termios.c_ispeed = 38400;
  619. ptm_driver->init_termios.c_ospeed = 38400;
  620. ptm_driver->other = pts_driver;
  621. tty_set_operations(ptm_driver, &ptm_unix98_ops);
  622. pts_driver->driver_name = "pty_slave";
  623. pts_driver->name = "pts";
  624. pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
  625. pts_driver->minor_start = 0;
  626. pts_driver->type = TTY_DRIVER_TYPE_PTY;
  627. pts_driver->subtype = PTY_TYPE_SLAVE;
  628. pts_driver->init_termios = tty_std_termios;
  629. pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  630. pts_driver->init_termios.c_ispeed = 38400;
  631. pts_driver->init_termios.c_ospeed = 38400;
  632. pts_driver->other = ptm_driver;
  633. tty_set_operations(pts_driver, &pty_unix98_ops);
  634. if (tty_register_driver(ptm_driver))
  635. panic("Couldn't register Unix98 ptm driver");
  636. if (tty_register_driver(pts_driver))
  637. panic("Couldn't register Unix98 pts driver");
  638. /* Now create the /dev/ptmx special device */
  639. tty_default_fops(&ptmx_fops);
  640. ptmx_fops.open = ptmx_open;
  641. cdev_init(&ptmx_cdev, &ptmx_fops);
  642. if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
  643. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
  644. panic("Couldn't register /dev/ptmx driver\n");
  645. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
  646. }
  647. #else
  648. static inline void unix98_pty_init(void) { }
  649. #endif
  650. static int __init pty_init(void)
  651. {
  652. legacy_pty_init();
  653. unix98_pty_init();
  654. return 0;
  655. }
  656. module_init(pty_init);