pty.c 24 KB

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