pty.c 22 KB

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