pty.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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 (test_bit(TTY_IO_ERROR, &tty->flags))
  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. tty_flip_buffer_push(tty->link->port);
  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. struct tty_ldisc *ld;
  196. if (!to)
  197. return;
  198. ld = tty_ldisc_ref(to);
  199. tty_buffer_flush(to, ld);
  200. if (ld)
  201. tty_ldisc_deref(ld);
  202. if (to->packet) {
  203. spin_lock_irq(&tty->ctrl_lock);
  204. tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
  205. wake_up_interruptible(&to->read_wait);
  206. spin_unlock_irq(&tty->ctrl_lock);
  207. }
  208. }
  209. static int pty_open(struct tty_struct *tty, struct file *filp)
  210. {
  211. if (!tty || !tty->link)
  212. return -ENODEV;
  213. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  214. goto out;
  215. if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
  216. goto out;
  217. if (tty->driver->subtype == PTY_TYPE_SLAVE && tty->link->count != 1)
  218. goto out;
  219. clear_bit(TTY_IO_ERROR, &tty->flags);
  220. /* TTY_OTHER_CLOSED must be cleared before TTY_OTHER_DONE */
  221. clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  222. clear_bit(TTY_OTHER_DONE, &tty->link->flags);
  223. set_bit(TTY_THROTTLED, &tty->flags);
  224. return 0;
  225. out:
  226. set_bit(TTY_IO_ERROR, &tty->flags);
  227. return -EIO;
  228. }
  229. static void pty_set_termios(struct tty_struct *tty,
  230. struct ktermios *old_termios)
  231. {
  232. /* See if packet mode change of state. */
  233. if (tty->link && tty->link->packet) {
  234. int extproc = (old_termios->c_lflag & EXTPROC) | L_EXTPROC(tty);
  235. int old_flow = ((old_termios->c_iflag & IXON) &&
  236. (old_termios->c_cc[VSTOP] == '\023') &&
  237. (old_termios->c_cc[VSTART] == '\021'));
  238. int new_flow = (I_IXON(tty) &&
  239. STOP_CHAR(tty) == '\023' &&
  240. START_CHAR(tty) == '\021');
  241. if ((old_flow != new_flow) || extproc) {
  242. spin_lock_irq(&tty->ctrl_lock);
  243. if (old_flow != new_flow) {
  244. tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
  245. if (new_flow)
  246. tty->ctrl_status |= TIOCPKT_DOSTOP;
  247. else
  248. tty->ctrl_status |= TIOCPKT_NOSTOP;
  249. }
  250. if (extproc)
  251. tty->ctrl_status |= TIOCPKT_IOCTL;
  252. spin_unlock_irq(&tty->ctrl_lock);
  253. wake_up_interruptible(&tty->link->read_wait);
  254. }
  255. }
  256. tty->termios.c_cflag &= ~(CSIZE | PARENB);
  257. tty->termios.c_cflag |= (CS8 | CREAD);
  258. }
  259. /**
  260. * pty_do_resize - resize event
  261. * @tty: tty being resized
  262. * @ws: window size being set.
  263. *
  264. * Update the termios variables and send the necessary signals to
  265. * peform a terminal resize correctly
  266. */
  267. static int pty_resize(struct tty_struct *tty, struct winsize *ws)
  268. {
  269. struct pid *pgrp, *rpgrp;
  270. struct tty_struct *pty = tty->link;
  271. /* For a PTY we need to lock the tty side */
  272. mutex_lock(&tty->winsize_mutex);
  273. if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
  274. goto done;
  275. /* Signal the foreground process group of both ptys */
  276. pgrp = tty_get_pgrp(tty);
  277. rpgrp = tty_get_pgrp(pty);
  278. if (pgrp)
  279. kill_pgrp(pgrp, SIGWINCH, 1);
  280. if (rpgrp != pgrp && rpgrp)
  281. kill_pgrp(rpgrp, SIGWINCH, 1);
  282. put_pid(pgrp);
  283. put_pid(rpgrp);
  284. tty->winsize = *ws;
  285. pty->winsize = *ws; /* Never used so will go away soon */
  286. done:
  287. mutex_unlock(&tty->winsize_mutex);
  288. return 0;
  289. }
  290. /**
  291. * pty_start - start() handler
  292. * pty_stop - stop() handler
  293. * @tty: tty being flow-controlled
  294. *
  295. * Propagates the TIOCPKT status to the master pty.
  296. *
  297. * NB: only the master pty can be in packet mode so only the slave
  298. * needs start()/stop() handlers
  299. */
  300. static void pty_start(struct tty_struct *tty)
  301. {
  302. unsigned long flags;
  303. if (tty->link && tty->link->packet) {
  304. spin_lock_irqsave(&tty->ctrl_lock, flags);
  305. tty->ctrl_status &= ~TIOCPKT_STOP;
  306. tty->ctrl_status |= TIOCPKT_START;
  307. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  308. wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
  309. }
  310. }
  311. static void pty_stop(struct tty_struct *tty)
  312. {
  313. unsigned long flags;
  314. if (tty->link && tty->link->packet) {
  315. spin_lock_irqsave(&tty->ctrl_lock, flags);
  316. tty->ctrl_status &= ~TIOCPKT_START;
  317. tty->ctrl_status |= TIOCPKT_STOP;
  318. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  319. wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
  320. }
  321. }
  322. /**
  323. * pty_common_install - set up the pty pair
  324. * @driver: the pty driver
  325. * @tty: the tty being instantiated
  326. * @legacy: true if this is BSD style
  327. *
  328. * Perform the initial set up for the tty/pty pair. Called from the
  329. * tty layer when the port is first opened.
  330. *
  331. * Locking: the caller must hold the tty_mutex
  332. */
  333. static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
  334. bool legacy)
  335. {
  336. struct tty_struct *o_tty;
  337. struct tty_port *ports[2];
  338. int idx = tty->index;
  339. int retval = -ENOMEM;
  340. /* Opening the slave first has always returned -EIO */
  341. if (driver->subtype != PTY_TYPE_MASTER)
  342. return -EIO;
  343. ports[0] = kmalloc(sizeof **ports, GFP_KERNEL);
  344. ports[1] = kmalloc(sizeof **ports, GFP_KERNEL);
  345. if (!ports[0] || !ports[1])
  346. goto err;
  347. if (!try_module_get(driver->other->owner)) {
  348. /* This cannot in fact currently happen */
  349. goto err;
  350. }
  351. o_tty = alloc_tty_struct(driver->other, idx);
  352. if (!o_tty)
  353. goto err_put_module;
  354. tty_set_lock_subclass(o_tty);
  355. lockdep_set_subclass(&o_tty->termios_rwsem, TTY_LOCK_SLAVE);
  356. if (legacy) {
  357. /* We always use new tty termios data so we can do this
  358. the easy way .. */
  359. tty_init_termios(tty);
  360. tty_init_termios(o_tty);
  361. driver->other->ttys[idx] = o_tty;
  362. driver->ttys[idx] = tty;
  363. } else {
  364. memset(&tty->termios_locked, 0, sizeof(tty->termios_locked));
  365. tty->termios = driver->init_termios;
  366. memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked));
  367. o_tty->termios = driver->other->init_termios;
  368. }
  369. /*
  370. * Everything allocated ... set up the o_tty structure.
  371. */
  372. tty_driver_kref_get(driver->other);
  373. /* Establish the links in both directions */
  374. tty->link = o_tty;
  375. o_tty->link = tty;
  376. tty_port_init(ports[0]);
  377. tty_port_init(ports[1]);
  378. tty_buffer_set_limit(ports[0], 8192);
  379. tty_buffer_set_limit(ports[1], 8192);
  380. o_tty->port = ports[0];
  381. tty->port = ports[1];
  382. o_tty->port->itty = o_tty;
  383. tty_buffer_set_lock_subclass(o_tty->port);
  384. tty_driver_kref_get(driver);
  385. tty->count++;
  386. o_tty->count++;
  387. return 0;
  388. err_put_module:
  389. module_put(driver->other->owner);
  390. err:
  391. kfree(ports[0]);
  392. kfree(ports[1]);
  393. return retval;
  394. }
  395. static void pty_cleanup(struct tty_struct *tty)
  396. {
  397. tty_port_put(tty->port);
  398. }
  399. /* Traditional BSD devices */
  400. #ifdef CONFIG_LEGACY_PTYS
  401. static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
  402. {
  403. return pty_common_install(driver, tty, true);
  404. }
  405. static void pty_remove(struct tty_driver *driver, struct tty_struct *tty)
  406. {
  407. struct tty_struct *pair = tty->link;
  408. driver->ttys[tty->index] = NULL;
  409. if (pair)
  410. pair->driver->ttys[pair->index] = NULL;
  411. }
  412. static int pty_bsd_ioctl(struct tty_struct *tty,
  413. unsigned int cmd, unsigned long arg)
  414. {
  415. switch (cmd) {
  416. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  417. return pty_set_lock(tty, (int __user *) arg);
  418. case TIOCGPTLCK: /* Get PT Lock status */
  419. return pty_get_lock(tty, (int __user *)arg);
  420. case TIOCPKT: /* Set PT packet mode */
  421. return pty_set_pktmode(tty, (int __user *)arg);
  422. case TIOCGPKT: /* Get PT packet mode */
  423. return pty_get_pktmode(tty, (int __user *)arg);
  424. case TIOCSIG: /* Send signal to other side of pty */
  425. return pty_signal(tty, (int) arg);
  426. case TIOCGPTN: /* TTY returns ENOTTY, but glibc expects EINVAL here */
  427. return -EINVAL;
  428. }
  429. return -ENOIOCTLCMD;
  430. }
  431. static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
  432. /*
  433. * not really modular, but the easiest way to keep compat with existing
  434. * bootargs behaviour is to continue using module_param here.
  435. */
  436. module_param(legacy_count, int, 0);
  437. /*
  438. * The master side of a pty can do TIOCSPTLCK and thus
  439. * has pty_bsd_ioctl.
  440. */
  441. static const struct tty_operations master_pty_ops_bsd = {
  442. .install = pty_install,
  443. .open = pty_open,
  444. .close = pty_close,
  445. .write = pty_write,
  446. .write_room = pty_write_room,
  447. .flush_buffer = pty_flush_buffer,
  448. .chars_in_buffer = pty_chars_in_buffer,
  449. .unthrottle = pty_unthrottle,
  450. .ioctl = pty_bsd_ioctl,
  451. .cleanup = pty_cleanup,
  452. .resize = pty_resize,
  453. .remove = pty_remove
  454. };
  455. static const struct tty_operations slave_pty_ops_bsd = {
  456. .install = pty_install,
  457. .open = pty_open,
  458. .close = pty_close,
  459. .write = pty_write,
  460. .write_room = pty_write_room,
  461. .flush_buffer = pty_flush_buffer,
  462. .chars_in_buffer = pty_chars_in_buffer,
  463. .unthrottle = pty_unthrottle,
  464. .set_termios = pty_set_termios,
  465. .cleanup = pty_cleanup,
  466. .resize = pty_resize,
  467. .start = pty_start,
  468. .stop = pty_stop,
  469. .remove = pty_remove
  470. };
  471. static void __init legacy_pty_init(void)
  472. {
  473. struct tty_driver *pty_driver, *pty_slave_driver;
  474. if (legacy_count <= 0)
  475. return;
  476. pty_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_driver))
  481. panic("Couldn't allocate pty driver");
  482. pty_slave_driver = tty_alloc_driver(legacy_count,
  483. TTY_DRIVER_RESET_TERMIOS |
  484. TTY_DRIVER_REAL_RAW |
  485. TTY_DRIVER_DYNAMIC_ALLOC);
  486. if (IS_ERR(pty_slave_driver))
  487. panic("Couldn't allocate pty slave driver");
  488. pty_driver->driver_name = "pty_master";
  489. pty_driver->name = "pty";
  490. pty_driver->major = PTY_MASTER_MAJOR;
  491. pty_driver->minor_start = 0;
  492. pty_driver->type = TTY_DRIVER_TYPE_PTY;
  493. pty_driver->subtype = PTY_TYPE_MASTER;
  494. pty_driver->init_termios = tty_std_termios;
  495. pty_driver->init_termios.c_iflag = 0;
  496. pty_driver->init_termios.c_oflag = 0;
  497. pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  498. pty_driver->init_termios.c_lflag = 0;
  499. pty_driver->init_termios.c_ispeed = 38400;
  500. pty_driver->init_termios.c_ospeed = 38400;
  501. pty_driver->other = pty_slave_driver;
  502. tty_set_operations(pty_driver, &master_pty_ops_bsd);
  503. pty_slave_driver->driver_name = "pty_slave";
  504. pty_slave_driver->name = "ttyp";
  505. pty_slave_driver->major = PTY_SLAVE_MAJOR;
  506. pty_slave_driver->minor_start = 0;
  507. pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
  508. pty_slave_driver->subtype = PTY_TYPE_SLAVE;
  509. pty_slave_driver->init_termios = tty_std_termios;
  510. pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  511. pty_slave_driver->init_termios.c_ispeed = 38400;
  512. pty_slave_driver->init_termios.c_ospeed = 38400;
  513. pty_slave_driver->other = pty_driver;
  514. tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
  515. if (tty_register_driver(pty_driver))
  516. panic("Couldn't register pty driver");
  517. if (tty_register_driver(pty_slave_driver))
  518. panic("Couldn't register pty slave driver");
  519. }
  520. #else
  521. static inline void legacy_pty_init(void) { }
  522. #endif
  523. /* Unix98 devices */
  524. #ifdef CONFIG_UNIX98_PTYS
  525. static struct cdev ptmx_cdev;
  526. static int pty_unix98_ioctl(struct tty_struct *tty,
  527. unsigned int cmd, unsigned long arg)
  528. {
  529. switch (cmd) {
  530. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  531. return pty_set_lock(tty, (int __user *)arg);
  532. case TIOCGPTLCK: /* Get PT Lock status */
  533. return pty_get_lock(tty, (int __user *)arg);
  534. case TIOCPKT: /* Set PT packet mode */
  535. return pty_set_pktmode(tty, (int __user *)arg);
  536. case TIOCGPKT: /* Get PT packet mode */
  537. return pty_get_pktmode(tty, (int __user *)arg);
  538. case TIOCGPTN: /* Get PT Number */
  539. return put_user(tty->index, (unsigned int __user *)arg);
  540. case TIOCSIG: /* Send signal to other side of pty */
  541. return pty_signal(tty, (int) arg);
  542. }
  543. return -ENOIOCTLCMD;
  544. }
  545. /**
  546. * ptm_unix98_lookup - find a pty master
  547. * @driver: ptm driver
  548. * @idx: tty index
  549. *
  550. * Look up a pty master device. Called under the tty_mutex for now.
  551. * This provides our locking.
  552. */
  553. static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
  554. struct inode *ptm_inode, int idx)
  555. {
  556. /* Master must be open via /dev/ptmx */
  557. return ERR_PTR(-EIO);
  558. }
  559. /**
  560. * pts_unix98_lookup - find a pty slave
  561. * @driver: pts driver
  562. * @idx: tty index
  563. *
  564. * Look up a pty master device. Called under the tty_mutex for now.
  565. * This provides our locking for the tty pointer.
  566. */
  567. static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
  568. struct inode *pts_inode, int idx)
  569. {
  570. struct tty_struct *tty;
  571. mutex_lock(&devpts_mutex);
  572. tty = devpts_get_priv(pts_inode);
  573. mutex_unlock(&devpts_mutex);
  574. /* Master must be open before slave */
  575. if (!tty)
  576. return ERR_PTR(-EIO);
  577. return tty;
  578. }
  579. static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
  580. {
  581. return pty_common_install(driver, tty, false);
  582. }
  583. /* this is called once with whichever end is closed last */
  584. static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
  585. {
  586. struct inode *ptmx_inode;
  587. if (tty->driver->subtype == PTY_TYPE_MASTER)
  588. ptmx_inode = tty->driver_data;
  589. else
  590. ptmx_inode = tty->link->driver_data;
  591. devpts_kill_index(ptmx_inode, tty->index);
  592. devpts_del_ref(ptmx_inode);
  593. }
  594. static const struct tty_operations ptm_unix98_ops = {
  595. .lookup = ptm_unix98_lookup,
  596. .install = pty_unix98_install,
  597. .remove = pty_unix98_remove,
  598. .open = pty_open,
  599. .close = pty_close,
  600. .write = pty_write,
  601. .write_room = pty_write_room,
  602. .flush_buffer = pty_flush_buffer,
  603. .chars_in_buffer = pty_chars_in_buffer,
  604. .unthrottle = pty_unthrottle,
  605. .ioctl = pty_unix98_ioctl,
  606. .resize = pty_resize,
  607. .cleanup = pty_cleanup
  608. };
  609. static const struct tty_operations pty_unix98_ops = {
  610. .lookup = pts_unix98_lookup,
  611. .install = pty_unix98_install,
  612. .remove = pty_unix98_remove,
  613. .open = pty_open,
  614. .close = pty_close,
  615. .write = pty_write,
  616. .write_room = pty_write_room,
  617. .flush_buffer = pty_flush_buffer,
  618. .chars_in_buffer = pty_chars_in_buffer,
  619. .unthrottle = pty_unthrottle,
  620. .set_termios = pty_set_termios,
  621. .start = pty_start,
  622. .stop = pty_stop,
  623. .cleanup = pty_cleanup,
  624. };
  625. /**
  626. * ptmx_open - open a unix 98 pty master
  627. * @inode: inode of device file
  628. * @filp: file pointer to tty
  629. *
  630. * Allocate a unix98 pty master device from the ptmx driver.
  631. *
  632. * Locking: tty_mutex protects the init_dev work. tty->count should
  633. * protect the rest.
  634. * allocated_ptys_lock handles the list of free pty numbers
  635. */
  636. static int ptmx_open(struct inode *inode, struct file *filp)
  637. {
  638. struct tty_struct *tty;
  639. struct inode *slave_inode;
  640. int retval;
  641. int index;
  642. nonseekable_open(inode, filp);
  643. /* We refuse fsnotify events on ptmx, since it's a shared resource */
  644. filp->f_mode |= FMODE_NONOTIFY;
  645. retval = tty_alloc_file(filp);
  646. if (retval)
  647. return retval;
  648. /* find a device that is not in use. */
  649. mutex_lock(&devpts_mutex);
  650. index = devpts_new_index(inode);
  651. if (index < 0) {
  652. retval = index;
  653. mutex_unlock(&devpts_mutex);
  654. goto err_file;
  655. }
  656. mutex_unlock(&devpts_mutex);
  657. mutex_lock(&tty_mutex);
  658. tty = tty_init_dev(ptm_driver, index);
  659. if (IS_ERR(tty)) {
  660. retval = PTR_ERR(tty);
  661. goto out;
  662. }
  663. /* The tty returned here is locked so we can safely
  664. drop the mutex */
  665. mutex_unlock(&tty_mutex);
  666. set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
  667. tty->driver_data = inode;
  668. /*
  669. * In the case where all references to ptmx inode are dropped and we
  670. * still have /dev/tty opened pointing to the master/slave pair (ptmx
  671. * is closed/released before /dev/tty), we must make sure that the inode
  672. * is still valid when we call the final pty_unix98_shutdown, thus we
  673. * hold an additional reference to the ptmx inode. For the same /dev/tty
  674. * last close case, we also need to make sure the super_block isn't
  675. * destroyed (devpts instance unmounted), before /dev/tty is closed and
  676. * on its release devpts_kill_index is called.
  677. */
  678. devpts_add_ref(inode);
  679. tty_add_file(tty, filp);
  680. slave_inode = devpts_pty_new(inode,
  681. MKDEV(UNIX98_PTY_SLAVE_MAJOR, index), index,
  682. tty->link);
  683. if (IS_ERR(slave_inode)) {
  684. retval = PTR_ERR(slave_inode);
  685. goto err_release;
  686. }
  687. tty->link->driver_data = slave_inode;
  688. retval = ptm_driver->ops->open(tty, filp);
  689. if (retval)
  690. goto err_release;
  691. tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
  692. tty_unlock(tty);
  693. return 0;
  694. err_release:
  695. tty_unlock(tty);
  696. tty_release(inode, filp);
  697. return retval;
  698. out:
  699. mutex_unlock(&tty_mutex);
  700. devpts_kill_index(inode, index);
  701. err_file:
  702. tty_free_file(filp);
  703. return retval;
  704. }
  705. static struct file_operations ptmx_fops;
  706. static void __init unix98_pty_init(void)
  707. {
  708. ptm_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(ptm_driver))
  715. panic("Couldn't allocate Unix98 ptm driver");
  716. pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
  717. TTY_DRIVER_RESET_TERMIOS |
  718. TTY_DRIVER_REAL_RAW |
  719. TTY_DRIVER_DYNAMIC_DEV |
  720. TTY_DRIVER_DEVPTS_MEM |
  721. TTY_DRIVER_DYNAMIC_ALLOC);
  722. if (IS_ERR(pts_driver))
  723. panic("Couldn't allocate Unix98 pts driver");
  724. ptm_driver->driver_name = "pty_master";
  725. ptm_driver->name = "ptm";
  726. ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
  727. ptm_driver->minor_start = 0;
  728. ptm_driver->type = TTY_DRIVER_TYPE_PTY;
  729. ptm_driver->subtype = PTY_TYPE_MASTER;
  730. ptm_driver->init_termios = tty_std_termios;
  731. ptm_driver->init_termios.c_iflag = 0;
  732. ptm_driver->init_termios.c_oflag = 0;
  733. ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  734. ptm_driver->init_termios.c_lflag = 0;
  735. ptm_driver->init_termios.c_ispeed = 38400;
  736. ptm_driver->init_termios.c_ospeed = 38400;
  737. ptm_driver->other = pts_driver;
  738. tty_set_operations(ptm_driver, &ptm_unix98_ops);
  739. pts_driver->driver_name = "pty_slave";
  740. pts_driver->name = "pts";
  741. pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
  742. pts_driver->minor_start = 0;
  743. pts_driver->type = TTY_DRIVER_TYPE_PTY;
  744. pts_driver->subtype = PTY_TYPE_SLAVE;
  745. pts_driver->init_termios = tty_std_termios;
  746. pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  747. pts_driver->init_termios.c_ispeed = 38400;
  748. pts_driver->init_termios.c_ospeed = 38400;
  749. pts_driver->other = ptm_driver;
  750. tty_set_operations(pts_driver, &pty_unix98_ops);
  751. if (tty_register_driver(ptm_driver))
  752. panic("Couldn't register Unix98 ptm driver");
  753. if (tty_register_driver(pts_driver))
  754. panic("Couldn't register Unix98 pts driver");
  755. /* Now create the /dev/ptmx special device */
  756. tty_default_fops(&ptmx_fops);
  757. ptmx_fops.open = ptmx_open;
  758. cdev_init(&ptmx_cdev, &ptmx_fops);
  759. if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
  760. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
  761. panic("Couldn't register /dev/ptmx driver");
  762. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
  763. }
  764. #else
  765. static inline void unix98_pty_init(void) { }
  766. #endif
  767. static int __init pty_init(void)
  768. {
  769. legacy_pty_init();
  770. unix98_pty_init();
  771. return 0;
  772. }
  773. device_initcall(pty_init);