pty.c 22 KB

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