pty.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. *
  5. * Added support for a Unix98-style ptmx device.
  6. * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
  7. *
  8. */
  9. #include <linux/module.h>
  10. #include <linux/errno.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/tty.h>
  13. #include <linux/tty_flip.h>
  14. #include <linux/fcntl.h>
  15. #include <linux/sched/signal.h>
  16. #include <linux/string.h>
  17. #include <linux/major.h>
  18. #include <linux/mm.h>
  19. #include <linux/init.h>
  20. #include <linux/device.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/bitops.h>
  23. #include <linux/devpts_fs.h>
  24. #include <linux/slab.h>
  25. #include <linux/mutex.h>
  26. #include <linux/poll.h>
  27. #include <linux/mount.h>
  28. #include <linux/file.h>
  29. #include <linux/ioctl.h>
  30. #undef TTY_DEBUG_HANGUP
  31. #ifdef TTY_DEBUG_HANGUP
  32. # define tty_debug_hangup(tty, f, args...) tty_debug(tty, f, ##args)
  33. #else
  34. # define tty_debug_hangup(tty, f, args...) do {} while (0)
  35. #endif
  36. #ifdef CONFIG_UNIX98_PTYS
  37. static struct tty_driver *ptm_driver;
  38. static struct tty_driver *pts_driver;
  39. static DEFINE_MUTEX(devpts_mutex);
  40. #endif
  41. static void pty_close(struct tty_struct *tty, struct file *filp)
  42. {
  43. BUG_ON(!tty);
  44. if (tty->driver->subtype == PTY_TYPE_MASTER)
  45. WARN_ON(tty->count > 1);
  46. else {
  47. if (tty_io_error(tty))
  48. return;
  49. if (tty->count > 2)
  50. return;
  51. }
  52. set_bit(TTY_IO_ERROR, &tty->flags);
  53. wake_up_interruptible(&tty->read_wait);
  54. wake_up_interruptible(&tty->write_wait);
  55. spin_lock_irq(&tty->ctrl_lock);
  56. tty->packet = 0;
  57. spin_unlock_irq(&tty->ctrl_lock);
  58. /* Review - krefs on tty_link ?? */
  59. if (!tty->link)
  60. return;
  61. set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  62. wake_up_interruptible(&tty->link->read_wait);
  63. wake_up_interruptible(&tty->link->write_wait);
  64. if (tty->driver->subtype == PTY_TYPE_MASTER) {
  65. set_bit(TTY_OTHER_CLOSED, &tty->flags);
  66. #ifdef CONFIG_UNIX98_PTYS
  67. if (tty->driver == ptm_driver) {
  68. mutex_lock(&devpts_mutex);
  69. if (tty->link->driver_data)
  70. devpts_pty_kill(tty->link->driver_data);
  71. mutex_unlock(&devpts_mutex);
  72. }
  73. #endif
  74. tty_vhangup(tty->link);
  75. }
  76. }
  77. /*
  78. * The unthrottle routine is called by the line discipline to signal
  79. * that it can receive more characters. For PTY's, the TTY_THROTTLED
  80. * flag is always set, to force the line discipline to always call the
  81. * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
  82. * characters in the queue. This is necessary since each time this
  83. * happens, we need to wake up any sleeping processes that could be
  84. * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
  85. * for the pty buffer to be drained.
  86. */
  87. static void pty_unthrottle(struct tty_struct *tty)
  88. {
  89. tty_wakeup(tty->link);
  90. set_bit(TTY_THROTTLED, &tty->flags);
  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 tty_buffer_space_avail(tty->link->port);
  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. int pktmode;
  162. if (get_user(pktmode, arg))
  163. return -EFAULT;
  164. spin_lock_irq(&tty->ctrl_lock);
  165. if (pktmode) {
  166. if (!tty->packet) {
  167. tty->link->ctrl_status = 0;
  168. smp_mb();
  169. tty->packet = 1;
  170. }
  171. } else
  172. tty->packet = 0;
  173. spin_unlock_irq(&tty->ctrl_lock);
  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. struct pid *pgrp;
  186. if (sig != SIGINT && sig != SIGQUIT && sig != SIGTSTP)
  187. return -EINVAL;
  188. if (tty->link) {
  189. pgrp = tty_get_pgrp(tty->link);
  190. if (pgrp)
  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. if (!to)
  200. return;
  201. tty_buffer_flush(to, NULL);
  202. if (to->packet) {
  203. spin_lock_irq(&tty->ctrl_lock);
  204. tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
  205. wake_up_interruptible(&to->read_wait);
  206. spin_unlock_irq(&tty->ctrl_lock);
  207. }
  208. }
  209. static int pty_open(struct tty_struct *tty, struct file *filp)
  210. {
  211. if (!tty || !tty->link)
  212. return -ENODEV;
  213. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  214. goto out;
  215. if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
  216. goto out;
  217. if (tty->driver->subtype == PTY_TYPE_SLAVE && tty->link->count != 1)
  218. goto out;
  219. clear_bit(TTY_IO_ERROR, &tty->flags);
  220. clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  221. set_bit(TTY_THROTTLED, &tty->flags);
  222. return 0;
  223. out:
  224. set_bit(TTY_IO_ERROR, &tty->flags);
  225. return -EIO;
  226. }
  227. static void pty_set_termios(struct tty_struct *tty,
  228. struct ktermios *old_termios)
  229. {
  230. /* See if packet mode change of state. */
  231. if (tty->link && tty->link->packet) {
  232. int extproc = (old_termios->c_lflag & EXTPROC) | L_EXTPROC(tty);
  233. int old_flow = ((old_termios->c_iflag & IXON) &&
  234. (old_termios->c_cc[VSTOP] == '\023') &&
  235. (old_termios->c_cc[VSTART] == '\021'));
  236. int new_flow = (I_IXON(tty) &&
  237. STOP_CHAR(tty) == '\023' &&
  238. START_CHAR(tty) == '\021');
  239. if ((old_flow != new_flow) || extproc) {
  240. spin_lock_irq(&tty->ctrl_lock);
  241. if (old_flow != new_flow) {
  242. tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
  243. if (new_flow)
  244. tty->ctrl_status |= TIOCPKT_DOSTOP;
  245. else
  246. tty->ctrl_status |= TIOCPKT_NOSTOP;
  247. }
  248. if (extproc)
  249. tty->ctrl_status |= TIOCPKT_IOCTL;
  250. spin_unlock_irq(&tty->ctrl_lock);
  251. wake_up_interruptible(&tty->link->read_wait);
  252. }
  253. }
  254. tty->termios.c_cflag &= ~(CSIZE | PARENB);
  255. tty->termios.c_cflag |= (CS8 | CREAD);
  256. }
  257. /**
  258. * pty_do_resize - resize event
  259. * @tty: tty being resized
  260. * @ws: window size being set.
  261. *
  262. * Update the termios variables and send the necessary signals to
  263. * peform a terminal resize correctly
  264. */
  265. static int pty_resize(struct tty_struct *tty, struct winsize *ws)
  266. {
  267. struct pid *pgrp, *rpgrp;
  268. struct tty_struct *pty = tty->link;
  269. /* For a PTY we need to lock the tty side */
  270. mutex_lock(&tty->winsize_mutex);
  271. if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
  272. goto done;
  273. /* Signal the foreground process group of both ptys */
  274. pgrp = tty_get_pgrp(tty);
  275. rpgrp = tty_get_pgrp(pty);
  276. if (pgrp)
  277. kill_pgrp(pgrp, SIGWINCH, 1);
  278. if (rpgrp != pgrp && rpgrp)
  279. kill_pgrp(rpgrp, SIGWINCH, 1);
  280. put_pid(pgrp);
  281. put_pid(rpgrp);
  282. tty->winsize = *ws;
  283. pty->winsize = *ws; /* Never used so will go away soon */
  284. done:
  285. mutex_unlock(&tty->winsize_mutex);
  286. return 0;
  287. }
  288. /**
  289. * pty_start - start() handler
  290. * pty_stop - stop() handler
  291. * @tty: tty being flow-controlled
  292. *
  293. * Propagates the TIOCPKT status to the master pty.
  294. *
  295. * NB: only the master pty can be in packet mode so only the slave
  296. * needs start()/stop() handlers
  297. */
  298. static void pty_start(struct tty_struct *tty)
  299. {
  300. unsigned long flags;
  301. if (tty->link && tty->link->packet) {
  302. spin_lock_irqsave(&tty->ctrl_lock, flags);
  303. tty->ctrl_status &= ~TIOCPKT_STOP;
  304. tty->ctrl_status |= TIOCPKT_START;
  305. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  306. wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
  307. }
  308. }
  309. static void pty_stop(struct tty_struct *tty)
  310. {
  311. unsigned long flags;
  312. if (tty->link && tty->link->packet) {
  313. spin_lock_irqsave(&tty->ctrl_lock, flags);
  314. tty->ctrl_status &= ~TIOCPKT_START;
  315. tty->ctrl_status |= TIOCPKT_STOP;
  316. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  317. wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
  318. }
  319. }
  320. /**
  321. * pty_common_install - set up the pty pair
  322. * @driver: the pty driver
  323. * @tty: the tty being instantiated
  324. * @legacy: true if this is BSD style
  325. *
  326. * Perform the initial set up for the tty/pty pair. Called from the
  327. * tty layer when the port is first opened.
  328. *
  329. * Locking: the caller must hold the tty_mutex
  330. */
  331. static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
  332. bool legacy)
  333. {
  334. struct tty_struct *o_tty;
  335. struct tty_port *ports[2];
  336. int idx = tty->index;
  337. int retval = -ENOMEM;
  338. /* Opening the slave first has always returned -EIO */
  339. if (driver->subtype != PTY_TYPE_MASTER)
  340. return -EIO;
  341. ports[0] = kmalloc(sizeof **ports, GFP_KERNEL);
  342. ports[1] = kmalloc(sizeof **ports, GFP_KERNEL);
  343. if (!ports[0] || !ports[1])
  344. goto err;
  345. if (!try_module_get(driver->other->owner)) {
  346. /* This cannot in fact currently happen */
  347. goto err;
  348. }
  349. o_tty = alloc_tty_struct(driver->other, idx);
  350. if (!o_tty)
  351. goto err_put_module;
  352. tty_set_lock_subclass(o_tty);
  353. lockdep_set_subclass(&o_tty->termios_rwsem, TTY_LOCK_SLAVE);
  354. if (legacy) {
  355. /* We always use new tty termios data so we can do this
  356. the easy way .. */
  357. tty_init_termios(tty);
  358. tty_init_termios(o_tty);
  359. driver->other->ttys[idx] = o_tty;
  360. driver->ttys[idx] = tty;
  361. } else {
  362. memset(&tty->termios_locked, 0, sizeof(tty->termios_locked));
  363. tty->termios = driver->init_termios;
  364. memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked));
  365. o_tty->termios = driver->other->init_termios;
  366. }
  367. /*
  368. * Everything allocated ... set up the o_tty structure.
  369. */
  370. tty_driver_kref_get(driver->other);
  371. /* Establish the links in both directions */
  372. tty->link = o_tty;
  373. o_tty->link = tty;
  374. tty_port_init(ports[0]);
  375. tty_port_init(ports[1]);
  376. tty_buffer_set_limit(ports[0], 8192);
  377. tty_buffer_set_limit(ports[1], 8192);
  378. o_tty->port = ports[0];
  379. tty->port = ports[1];
  380. o_tty->port->itty = o_tty;
  381. tty_buffer_set_lock_subclass(o_tty->port);
  382. tty_driver_kref_get(driver);
  383. tty->count++;
  384. o_tty->count++;
  385. return 0;
  386. err_put_module:
  387. module_put(driver->other->owner);
  388. err:
  389. kfree(ports[0]);
  390. kfree(ports[1]);
  391. return retval;
  392. }
  393. static void pty_cleanup(struct tty_struct *tty)
  394. {
  395. tty_port_put(tty->port);
  396. }
  397. /* Traditional BSD devices */
  398. #ifdef CONFIG_LEGACY_PTYS
  399. static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
  400. {
  401. return pty_common_install(driver, tty, true);
  402. }
  403. static void pty_remove(struct tty_driver *driver, struct tty_struct *tty)
  404. {
  405. struct tty_struct *pair = tty->link;
  406. driver->ttys[tty->index] = NULL;
  407. if (pair)
  408. pair->driver->ttys[pair->index] = NULL;
  409. }
  410. static int pty_bsd_ioctl(struct tty_struct *tty,
  411. unsigned int cmd, unsigned long arg)
  412. {
  413. switch (cmd) {
  414. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  415. return pty_set_lock(tty, (int __user *) arg);
  416. case TIOCGPTLCK: /* Get PT Lock status */
  417. return pty_get_lock(tty, (int __user *)arg);
  418. case TIOCPKT: /* Set PT packet mode */
  419. return pty_set_pktmode(tty, (int __user *)arg);
  420. case TIOCGPKT: /* Get PT packet mode */
  421. return pty_get_pktmode(tty, (int __user *)arg);
  422. case TIOCSIG: /* Send signal to other side of pty */
  423. return pty_signal(tty, (int) arg);
  424. case TIOCGPTN: /* TTY returns ENOTTY, but glibc expects EINVAL here */
  425. return -EINVAL;
  426. }
  427. return -ENOIOCTLCMD;
  428. }
  429. static long pty_bsd_compat_ioctl(struct tty_struct *tty,
  430. unsigned int cmd, unsigned long arg)
  431. {
  432. /*
  433. * PTY ioctls don't require any special translation between 32-bit and
  434. * 64-bit userspace, they are already compatible.
  435. */
  436. return pty_bsd_ioctl(tty, cmd, arg);
  437. }
  438. static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
  439. /*
  440. * not really modular, but the easiest way to keep compat with existing
  441. * bootargs behaviour is to continue using module_param here.
  442. */
  443. module_param(legacy_count, int, 0);
  444. /*
  445. * The master side of a pty can do TIOCSPTLCK and thus
  446. * has pty_bsd_ioctl.
  447. */
  448. static const struct tty_operations master_pty_ops_bsd = {
  449. .install = pty_install,
  450. .open = pty_open,
  451. .close = pty_close,
  452. .write = pty_write,
  453. .write_room = pty_write_room,
  454. .flush_buffer = pty_flush_buffer,
  455. .chars_in_buffer = pty_chars_in_buffer,
  456. .unthrottle = pty_unthrottle,
  457. .ioctl = pty_bsd_ioctl,
  458. .compat_ioctl = pty_bsd_compat_ioctl,
  459. .cleanup = pty_cleanup,
  460. .resize = pty_resize,
  461. .remove = pty_remove
  462. };
  463. static const struct tty_operations slave_pty_ops_bsd = {
  464. .install = pty_install,
  465. .open = pty_open,
  466. .close = pty_close,
  467. .write = pty_write,
  468. .write_room = pty_write_room,
  469. .flush_buffer = pty_flush_buffer,
  470. .chars_in_buffer = pty_chars_in_buffer,
  471. .unthrottle = pty_unthrottle,
  472. .set_termios = pty_set_termios,
  473. .cleanup = pty_cleanup,
  474. .resize = pty_resize,
  475. .start = pty_start,
  476. .stop = pty_stop,
  477. .remove = pty_remove
  478. };
  479. static void __init legacy_pty_init(void)
  480. {
  481. struct tty_driver *pty_driver, *pty_slave_driver;
  482. if (legacy_count <= 0)
  483. return;
  484. pty_driver = tty_alloc_driver(legacy_count,
  485. TTY_DRIVER_RESET_TERMIOS |
  486. TTY_DRIVER_REAL_RAW |
  487. TTY_DRIVER_DYNAMIC_ALLOC);
  488. if (IS_ERR(pty_driver))
  489. panic("Couldn't allocate pty driver");
  490. pty_slave_driver = tty_alloc_driver(legacy_count,
  491. TTY_DRIVER_RESET_TERMIOS |
  492. TTY_DRIVER_REAL_RAW |
  493. TTY_DRIVER_DYNAMIC_ALLOC);
  494. if (IS_ERR(pty_slave_driver))
  495. panic("Couldn't allocate pty slave driver");
  496. pty_driver->driver_name = "pty_master";
  497. pty_driver->name = "pty";
  498. pty_driver->major = PTY_MASTER_MAJOR;
  499. pty_driver->minor_start = 0;
  500. pty_driver->type = TTY_DRIVER_TYPE_PTY;
  501. pty_driver->subtype = PTY_TYPE_MASTER;
  502. pty_driver->init_termios = tty_std_termios;
  503. pty_driver->init_termios.c_iflag = 0;
  504. pty_driver->init_termios.c_oflag = 0;
  505. pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  506. pty_driver->init_termios.c_lflag = 0;
  507. pty_driver->init_termios.c_ispeed = 38400;
  508. pty_driver->init_termios.c_ospeed = 38400;
  509. pty_driver->other = pty_slave_driver;
  510. tty_set_operations(pty_driver, &master_pty_ops_bsd);
  511. pty_slave_driver->driver_name = "pty_slave";
  512. pty_slave_driver->name = "ttyp";
  513. pty_slave_driver->major = PTY_SLAVE_MAJOR;
  514. pty_slave_driver->minor_start = 0;
  515. pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
  516. pty_slave_driver->subtype = PTY_TYPE_SLAVE;
  517. pty_slave_driver->init_termios = tty_std_termios;
  518. pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  519. pty_slave_driver->init_termios.c_ispeed = 38400;
  520. pty_slave_driver->init_termios.c_ospeed = 38400;
  521. pty_slave_driver->other = pty_driver;
  522. tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
  523. if (tty_register_driver(pty_driver))
  524. panic("Couldn't register pty driver");
  525. if (tty_register_driver(pty_slave_driver))
  526. panic("Couldn't register pty slave driver");
  527. }
  528. #else
  529. static inline void legacy_pty_init(void) { }
  530. #endif
  531. /* Unix98 devices */
  532. #ifdef CONFIG_UNIX98_PTYS
  533. static struct cdev ptmx_cdev;
  534. /**
  535. * ptm_open_peer - open the peer of a pty
  536. * @master: the open struct file of the ptmx device node
  537. * @tty: the master of the pty being opened
  538. * @flags: the flags for open
  539. *
  540. * Provide a race free way for userspace to open the slave end of a pty
  541. * (where they have the master fd and cannot access or trust the mount
  542. * namespace /dev/pts was mounted inside).
  543. */
  544. int ptm_open_peer(struct file *master, struct tty_struct *tty, int flags)
  545. {
  546. int fd = -1;
  547. struct file *filp;
  548. int retval = -EINVAL;
  549. struct path path;
  550. if (tty->driver != ptm_driver)
  551. return -EIO;
  552. fd = get_unused_fd_flags(0);
  553. if (fd < 0) {
  554. retval = fd;
  555. goto err;
  556. }
  557. /* Compute the slave's path */
  558. path.mnt = devpts_mntget(master, tty->driver_data);
  559. if (IS_ERR(path.mnt)) {
  560. retval = PTR_ERR(path.mnt);
  561. goto err_put;
  562. }
  563. path.dentry = tty->link->driver_data;
  564. filp = dentry_open(&path, flags, current_cred());
  565. mntput(path.mnt);
  566. if (IS_ERR(filp)) {
  567. retval = PTR_ERR(filp);
  568. goto err_put;
  569. }
  570. fd_install(fd, filp);
  571. return fd;
  572. err_put:
  573. put_unused_fd(fd);
  574. err:
  575. return retval;
  576. }
  577. static int pty_unix98_ioctl(struct tty_struct *tty,
  578. unsigned int cmd, unsigned long arg)
  579. {
  580. switch (cmd) {
  581. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  582. return pty_set_lock(tty, (int __user *)arg);
  583. case TIOCGPTLCK: /* Get PT Lock status */
  584. return pty_get_lock(tty, (int __user *)arg);
  585. case TIOCPKT: /* Set PT packet mode */
  586. return pty_set_pktmode(tty, (int __user *)arg);
  587. case TIOCGPKT: /* Get PT packet mode */
  588. return pty_get_pktmode(tty, (int __user *)arg);
  589. case TIOCGPTN: /* Get PT Number */
  590. return put_user(tty->index, (unsigned int __user *)arg);
  591. case TIOCSIG: /* Send signal to other side of pty */
  592. return pty_signal(tty, (int) arg);
  593. }
  594. return -ENOIOCTLCMD;
  595. }
  596. static long pty_unix98_compat_ioctl(struct tty_struct *tty,
  597. unsigned int cmd, unsigned long arg)
  598. {
  599. /*
  600. * PTY ioctls don't require any special translation between 32-bit and
  601. * 64-bit userspace, they are already compatible.
  602. */
  603. return pty_unix98_ioctl(tty, cmd, arg);
  604. }
  605. /**
  606. * ptm_unix98_lookup - find a pty master
  607. * @driver: ptm driver
  608. * @idx: tty index
  609. *
  610. * Look up a pty master device. Called under the tty_mutex for now.
  611. * This provides our locking.
  612. */
  613. static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
  614. struct file *file, int idx)
  615. {
  616. /* Master must be open via /dev/ptmx */
  617. return ERR_PTR(-EIO);
  618. }
  619. /**
  620. * pts_unix98_lookup - find a pty slave
  621. * @driver: pts driver
  622. * @idx: tty index
  623. *
  624. * Look up a pty master device. Called under the tty_mutex for now.
  625. * This provides our locking for the tty pointer.
  626. */
  627. static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
  628. struct file *file, int idx)
  629. {
  630. struct tty_struct *tty;
  631. mutex_lock(&devpts_mutex);
  632. tty = devpts_get_priv(file->f_path.dentry);
  633. mutex_unlock(&devpts_mutex);
  634. /* Master must be open before slave */
  635. if (!tty)
  636. return ERR_PTR(-EIO);
  637. return tty;
  638. }
  639. static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
  640. {
  641. return pty_common_install(driver, tty, false);
  642. }
  643. /* this is called once with whichever end is closed last */
  644. static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
  645. {
  646. struct pts_fs_info *fsi;
  647. if (tty->driver->subtype == PTY_TYPE_MASTER)
  648. fsi = tty->driver_data;
  649. else
  650. fsi = tty->link->driver_data;
  651. if (fsi) {
  652. devpts_kill_index(fsi, tty->index);
  653. devpts_release(fsi);
  654. }
  655. }
  656. static void pty_show_fdinfo(struct tty_struct *tty, struct seq_file *m)
  657. {
  658. seq_printf(m, "tty-index:\t%d\n", tty->index);
  659. }
  660. static const struct tty_operations ptm_unix98_ops = {
  661. .lookup = ptm_unix98_lookup,
  662. .install = pty_unix98_install,
  663. .remove = pty_unix98_remove,
  664. .open = pty_open,
  665. .close = pty_close,
  666. .write = pty_write,
  667. .write_room = pty_write_room,
  668. .flush_buffer = pty_flush_buffer,
  669. .chars_in_buffer = pty_chars_in_buffer,
  670. .unthrottle = pty_unthrottle,
  671. .ioctl = pty_unix98_ioctl,
  672. .compat_ioctl = pty_unix98_compat_ioctl,
  673. .resize = pty_resize,
  674. .cleanup = pty_cleanup,
  675. .show_fdinfo = pty_show_fdinfo,
  676. };
  677. static const struct tty_operations pty_unix98_ops = {
  678. .lookup = pts_unix98_lookup,
  679. .install = pty_unix98_install,
  680. .remove = pty_unix98_remove,
  681. .open = pty_open,
  682. .close = pty_close,
  683. .write = pty_write,
  684. .write_room = pty_write_room,
  685. .flush_buffer = pty_flush_buffer,
  686. .chars_in_buffer = pty_chars_in_buffer,
  687. .unthrottle = pty_unthrottle,
  688. .set_termios = pty_set_termios,
  689. .start = pty_start,
  690. .stop = pty_stop,
  691. .cleanup = pty_cleanup,
  692. };
  693. /**
  694. * ptmx_open - open a unix 98 pty master
  695. * @inode: inode of device file
  696. * @filp: file pointer to tty
  697. *
  698. * Allocate a unix98 pty master device from the ptmx driver.
  699. *
  700. * Locking: tty_mutex protects the init_dev work. tty->count should
  701. * protect the rest.
  702. * allocated_ptys_lock handles the list of free pty numbers
  703. */
  704. static int ptmx_open(struct inode *inode, struct file *filp)
  705. {
  706. struct pts_fs_info *fsi;
  707. struct tty_struct *tty;
  708. struct dentry *dentry;
  709. int retval;
  710. int index;
  711. nonseekable_open(inode, filp);
  712. /* We refuse fsnotify events on ptmx, since it's a shared resource */
  713. filp->f_mode |= FMODE_NONOTIFY;
  714. retval = tty_alloc_file(filp);
  715. if (retval)
  716. return retval;
  717. fsi = devpts_acquire(filp);
  718. if (IS_ERR(fsi)) {
  719. retval = PTR_ERR(fsi);
  720. goto out_free_file;
  721. }
  722. /* find a device that is not in use. */
  723. mutex_lock(&devpts_mutex);
  724. index = devpts_new_index(fsi);
  725. mutex_unlock(&devpts_mutex);
  726. retval = index;
  727. if (index < 0)
  728. goto out_put_fsi;
  729. mutex_lock(&tty_mutex);
  730. tty = tty_init_dev(ptm_driver, index);
  731. /* The tty returned here is locked so we can safely
  732. drop the mutex */
  733. mutex_unlock(&tty_mutex);
  734. retval = PTR_ERR(tty);
  735. if (IS_ERR(tty))
  736. goto out;
  737. /*
  738. * From here on out, the tty is "live", and the index and
  739. * fsi will be killed/put by the tty_release()
  740. */
  741. set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
  742. tty->driver_data = fsi;
  743. tty_add_file(tty, filp);
  744. dentry = devpts_pty_new(fsi, index, tty->link);
  745. if (IS_ERR(dentry)) {
  746. retval = PTR_ERR(dentry);
  747. goto err_release;
  748. }
  749. tty->link->driver_data = dentry;
  750. retval = ptm_driver->ops->open(tty, filp);
  751. if (retval)
  752. goto err_release;
  753. tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
  754. tty_unlock(tty);
  755. return 0;
  756. err_release:
  757. tty_unlock(tty);
  758. // This will also put-ref the fsi
  759. tty_release(inode, filp);
  760. return retval;
  761. out:
  762. devpts_kill_index(fsi, index);
  763. out_put_fsi:
  764. devpts_release(fsi);
  765. out_free_file:
  766. tty_free_file(filp);
  767. return retval;
  768. }
  769. static struct file_operations ptmx_fops __ro_after_init;
  770. static void __init unix98_pty_init(void)
  771. {
  772. ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
  773. TTY_DRIVER_RESET_TERMIOS |
  774. TTY_DRIVER_REAL_RAW |
  775. TTY_DRIVER_DYNAMIC_DEV |
  776. TTY_DRIVER_DEVPTS_MEM |
  777. TTY_DRIVER_DYNAMIC_ALLOC);
  778. if (IS_ERR(ptm_driver))
  779. panic("Couldn't allocate Unix98 ptm driver");
  780. pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
  781. TTY_DRIVER_RESET_TERMIOS |
  782. TTY_DRIVER_REAL_RAW |
  783. TTY_DRIVER_DYNAMIC_DEV |
  784. TTY_DRIVER_DEVPTS_MEM |
  785. TTY_DRIVER_DYNAMIC_ALLOC);
  786. if (IS_ERR(pts_driver))
  787. panic("Couldn't allocate Unix98 pts driver");
  788. ptm_driver->driver_name = "pty_master";
  789. ptm_driver->name = "ptm";
  790. ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
  791. ptm_driver->minor_start = 0;
  792. ptm_driver->type = TTY_DRIVER_TYPE_PTY;
  793. ptm_driver->subtype = PTY_TYPE_MASTER;
  794. ptm_driver->init_termios = tty_std_termios;
  795. ptm_driver->init_termios.c_iflag = 0;
  796. ptm_driver->init_termios.c_oflag = 0;
  797. ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  798. ptm_driver->init_termios.c_lflag = 0;
  799. ptm_driver->init_termios.c_ispeed = 38400;
  800. ptm_driver->init_termios.c_ospeed = 38400;
  801. ptm_driver->other = pts_driver;
  802. tty_set_operations(ptm_driver, &ptm_unix98_ops);
  803. pts_driver->driver_name = "pty_slave";
  804. pts_driver->name = "pts";
  805. pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
  806. pts_driver->minor_start = 0;
  807. pts_driver->type = TTY_DRIVER_TYPE_PTY;
  808. pts_driver->subtype = PTY_TYPE_SLAVE;
  809. pts_driver->init_termios = tty_std_termios;
  810. pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  811. pts_driver->init_termios.c_ispeed = 38400;
  812. pts_driver->init_termios.c_ospeed = 38400;
  813. pts_driver->other = ptm_driver;
  814. tty_set_operations(pts_driver, &pty_unix98_ops);
  815. if (tty_register_driver(ptm_driver))
  816. panic("Couldn't register Unix98 ptm driver");
  817. if (tty_register_driver(pts_driver))
  818. panic("Couldn't register Unix98 pts driver");
  819. /* Now create the /dev/ptmx special device */
  820. tty_default_fops(&ptmx_fops);
  821. ptmx_fops.open = ptmx_open;
  822. cdev_init(&ptmx_cdev, &ptmx_fops);
  823. if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
  824. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
  825. panic("Couldn't register /dev/ptmx driver");
  826. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
  827. }
  828. #else
  829. static inline void unix98_pty_init(void) { }
  830. #endif
  831. static int __init pty_init(void)
  832. {
  833. legacy_pty_init();
  834. unix98_pty_init();
  835. return 0;
  836. }
  837. device_initcall(pty_init);