pty.c 22 KB

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