pty.c 24 KB

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