pty.c 25 KB

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