pty.c 22 KB

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