pty.c 21 KB

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