pty.c 19 KB

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