pty.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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. tty_flush_to_ldisc(tty->link);
  52. set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  53. wake_up_interruptible(&tty->link->read_wait);
  54. wake_up_interruptible(&tty->link->write_wait);
  55. if (tty->driver->subtype == PTY_TYPE_MASTER) {
  56. set_bit(TTY_OTHER_CLOSED, &tty->flags);
  57. #ifdef CONFIG_UNIX98_PTYS
  58. if (tty->driver == ptm_driver) {
  59. mutex_lock(&devpts_mutex);
  60. if (tty->link->driver_data)
  61. devpts_pty_kill(tty->link->driver_data);
  62. mutex_unlock(&devpts_mutex);
  63. }
  64. #endif
  65. tty_vhangup(tty->link);
  66. }
  67. }
  68. /*
  69. * The unthrottle routine is called by the line discipline to signal
  70. * that it can receive more characters. For PTY's, the TTY_THROTTLED
  71. * flag is always set, to force the line discipline to always call the
  72. * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
  73. * characters in the queue. This is necessary since each time this
  74. * happens, we need to wake up any sleeping processes that could be
  75. * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
  76. * for the pty buffer to be drained.
  77. */
  78. static void pty_unthrottle(struct tty_struct *tty)
  79. {
  80. tty_wakeup(tty->link);
  81. set_bit(TTY_THROTTLED, &tty->flags);
  82. }
  83. /**
  84. * pty_write - write to a pty
  85. * @tty: the tty we write from
  86. * @buf: kernel buffer of data
  87. * @count: bytes to write
  88. *
  89. * Our "hardware" write method. Data is coming from the ldisc which
  90. * may be in a non sleeping state. We simply throw this at the other
  91. * end of the link as if we were an IRQ handler receiving stuff for
  92. * the other side of the pty/tty pair.
  93. */
  94. static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
  95. {
  96. struct tty_struct *to = tty->link;
  97. if (tty->stopped)
  98. return 0;
  99. if (c > 0) {
  100. /* Stuff the data into the input queue of the other end */
  101. c = tty_insert_flip_string(to->port, buf, c);
  102. /* And shovel */
  103. if (c)
  104. tty_flip_buffer_push(to->port);
  105. }
  106. return c;
  107. }
  108. /**
  109. * pty_write_room - write space
  110. * @tty: tty we are writing from
  111. *
  112. * Report how many bytes the ldisc can send into the queue for
  113. * the other device.
  114. */
  115. static int pty_write_room(struct tty_struct *tty)
  116. {
  117. if (tty->stopped)
  118. return 0;
  119. return tty_buffer_space_avail(tty->link->port);
  120. }
  121. /**
  122. * pty_chars_in_buffer - characters currently in our tx queue
  123. * @tty: our tty
  124. *
  125. * Report how much we have in the transmit queue. As everything is
  126. * instantly at the other end this is easy to implement.
  127. */
  128. static int pty_chars_in_buffer(struct tty_struct *tty)
  129. {
  130. return 0;
  131. }
  132. /* Set the lock flag on a pty */
  133. static int pty_set_lock(struct tty_struct *tty, int __user *arg)
  134. {
  135. int val;
  136. if (get_user(val, arg))
  137. return -EFAULT;
  138. if (val)
  139. set_bit(TTY_PTY_LOCK, &tty->flags);
  140. else
  141. clear_bit(TTY_PTY_LOCK, &tty->flags);
  142. return 0;
  143. }
  144. static int pty_get_lock(struct tty_struct *tty, int __user *arg)
  145. {
  146. int locked = test_bit(TTY_PTY_LOCK, &tty->flags);
  147. return put_user(locked, arg);
  148. }
  149. /* Set the packet mode on a pty */
  150. static int pty_set_pktmode(struct tty_struct *tty, int __user *arg)
  151. {
  152. int pktmode;
  153. if (get_user(pktmode, arg))
  154. return -EFAULT;
  155. spin_lock_irq(&tty->ctrl_lock);
  156. if (pktmode) {
  157. if (!tty->packet) {
  158. tty->link->ctrl_status = 0;
  159. smp_mb();
  160. tty->packet = 1;
  161. }
  162. } else
  163. tty->packet = 0;
  164. spin_unlock_irq(&tty->ctrl_lock);
  165. return 0;
  166. }
  167. /* Get the packet mode of a pty */
  168. static int pty_get_pktmode(struct tty_struct *tty, int __user *arg)
  169. {
  170. int pktmode = tty->packet;
  171. return put_user(pktmode, arg);
  172. }
  173. /* Send a signal to the slave */
  174. static int pty_signal(struct tty_struct *tty, int sig)
  175. {
  176. struct pid *pgrp;
  177. if (sig != SIGINT && sig != SIGQUIT && sig != SIGTSTP)
  178. return -EINVAL;
  179. if (tty->link) {
  180. pgrp = tty_get_pgrp(tty->link);
  181. if (pgrp)
  182. kill_pgrp(pgrp, sig, 1);
  183. put_pid(pgrp);
  184. }
  185. return 0;
  186. }
  187. static void pty_flush_buffer(struct tty_struct *tty)
  188. {
  189. struct tty_struct *to = tty->link;
  190. struct tty_ldisc *ld;
  191. if (!to)
  192. return;
  193. ld = tty_ldisc_ref(to);
  194. tty_buffer_flush(to, ld);
  195. if (ld)
  196. tty_ldisc_deref(ld);
  197. if (to->packet) {
  198. spin_lock_irq(&tty->ctrl_lock);
  199. tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
  200. wake_up_interruptible(&to->read_wait);
  201. spin_unlock_irq(&tty->ctrl_lock);
  202. }
  203. }
  204. static int pty_open(struct tty_struct *tty, struct file *filp)
  205. {
  206. if (!tty || !tty->link)
  207. return -ENODEV;
  208. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  209. goto out;
  210. if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
  211. goto out;
  212. if (tty->driver->subtype == PTY_TYPE_SLAVE && tty->link->count != 1)
  213. goto out;
  214. clear_bit(TTY_IO_ERROR, &tty->flags);
  215. clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  216. set_bit(TTY_THROTTLED, &tty->flags);
  217. return 0;
  218. out:
  219. set_bit(TTY_IO_ERROR, &tty->flags);
  220. return -EIO;
  221. }
  222. static void pty_set_termios(struct tty_struct *tty,
  223. struct ktermios *old_termios)
  224. {
  225. /* See if packet mode change of state. */
  226. if (tty->link && tty->link->packet) {
  227. int extproc = (old_termios->c_lflag & EXTPROC) |
  228. (tty->termios.c_lflag & EXTPROC);
  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. retval = tty_init_termios(tty);
  354. if (retval)
  355. goto err_deinit_tty;
  356. retval = tty_init_termios(o_tty);
  357. if (retval)
  358. goto err_free_termios;
  359. driver->other->ttys[idx] = o_tty;
  360. driver->ttys[idx] = tty;
  361. } else {
  362. memset(&tty->termios_locked, 0, sizeof(tty->termios_locked));
  363. tty->termios = driver->init_termios;
  364. memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked));
  365. o_tty->termios = driver->other->init_termios;
  366. }
  367. /*
  368. * Everything allocated ... set up the o_tty structure.
  369. */
  370. tty_driver_kref_get(driver->other);
  371. /* Establish the links in both directions */
  372. tty->link = o_tty;
  373. o_tty->link = tty;
  374. tty_port_init(ports[0]);
  375. tty_port_init(ports[1]);
  376. tty_buffer_set_limit(ports[0], 8192);
  377. tty_buffer_set_limit(ports[1], 8192);
  378. o_tty->port = ports[0];
  379. tty->port = ports[1];
  380. o_tty->port->itty = o_tty;
  381. tty_buffer_set_lock_subclass(o_tty->port);
  382. tty_driver_kref_get(driver);
  383. tty->count++;
  384. o_tty->count++;
  385. return 0;
  386. err_free_termios:
  387. if (legacy)
  388. tty_free_termios(tty);
  389. err_deinit_tty:
  390. deinitialize_tty_struct(o_tty);
  391. free_tty_struct(o_tty);
  392. err_put_module:
  393. module_put(driver->other->owner);
  394. err:
  395. kfree(ports[0]);
  396. kfree(ports[1]);
  397. return retval;
  398. }
  399. static void pty_cleanup(struct tty_struct *tty)
  400. {
  401. tty_port_put(tty->port);
  402. }
  403. /* Traditional BSD devices */
  404. #ifdef CONFIG_LEGACY_PTYS
  405. static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
  406. {
  407. return pty_common_install(driver, tty, true);
  408. }
  409. static void pty_remove(struct tty_driver *driver, struct tty_struct *tty)
  410. {
  411. struct tty_struct *pair = tty->link;
  412. driver->ttys[tty->index] = NULL;
  413. if (pair)
  414. pair->driver->ttys[pair->index] = NULL;
  415. }
  416. static int pty_bsd_ioctl(struct tty_struct *tty,
  417. unsigned int cmd, unsigned long arg)
  418. {
  419. switch (cmd) {
  420. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  421. return pty_set_lock(tty, (int __user *) arg);
  422. case TIOCGPTLCK: /* Get PT Lock status */
  423. return pty_get_lock(tty, (int __user *)arg);
  424. case TIOCPKT: /* Set PT packet mode */
  425. return pty_set_pktmode(tty, (int __user *)arg);
  426. case TIOCGPKT: /* Get PT packet mode */
  427. return pty_get_pktmode(tty, (int __user *)arg);
  428. case TIOCSIG: /* Send signal to other side of pty */
  429. return pty_signal(tty, (int) arg);
  430. case TIOCGPTN: /* TTY returns ENOTTY, but glibc expects EINVAL here */
  431. return -EINVAL;
  432. }
  433. return -ENOIOCTLCMD;
  434. }
  435. static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
  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. /* We have no need to install and remove our tty objects as devpts does all
  580. the work for us */
  581. static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
  582. {
  583. return pty_common_install(driver, tty, false);
  584. }
  585. static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
  586. {
  587. }
  588. /* this is called once with whichever end is closed last */
  589. static void pty_unix98_shutdown(struct tty_struct *tty)
  590. {
  591. devpts_kill_index(tty->driver_data, tty->index);
  592. }
  593. static const struct tty_operations ptm_unix98_ops = {
  594. .lookup = ptm_unix98_lookup,
  595. .install = pty_unix98_install,
  596. .remove = pty_unix98_remove,
  597. .open = pty_open,
  598. .close = pty_close,
  599. .write = pty_write,
  600. .write_room = pty_write_room,
  601. .flush_buffer = pty_flush_buffer,
  602. .chars_in_buffer = pty_chars_in_buffer,
  603. .unthrottle = pty_unthrottle,
  604. .ioctl = pty_unix98_ioctl,
  605. .resize = pty_resize,
  606. .shutdown = pty_unix98_shutdown,
  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. .shutdown = pty_unix98_shutdown,
  624. .cleanup = pty_cleanup,
  625. };
  626. /**
  627. * ptmx_open - open a unix 98 pty master
  628. * @inode: inode of device file
  629. * @filp: file pointer to tty
  630. *
  631. * Allocate a unix98 pty master device from the ptmx driver.
  632. *
  633. * Locking: tty_mutex protects the init_dev work. tty->count should
  634. * protect the rest.
  635. * allocated_ptys_lock handles the list of free pty numbers
  636. */
  637. static int ptmx_open(struct inode *inode, struct file *filp)
  638. {
  639. struct tty_struct *tty;
  640. struct inode *slave_inode;
  641. int retval;
  642. int index;
  643. nonseekable_open(inode, filp);
  644. /* We refuse fsnotify events on ptmx, since it's a shared resource */
  645. filp->f_mode |= FMODE_NONOTIFY;
  646. retval = tty_alloc_file(filp);
  647. if (retval)
  648. return retval;
  649. /* find a device that is not in use. */
  650. mutex_lock(&devpts_mutex);
  651. index = devpts_new_index(inode);
  652. if (index < 0) {
  653. retval = index;
  654. mutex_unlock(&devpts_mutex);
  655. goto err_file;
  656. }
  657. mutex_unlock(&devpts_mutex);
  658. mutex_lock(&tty_mutex);
  659. tty = tty_init_dev(ptm_driver, index);
  660. if (IS_ERR(tty)) {
  661. retval = PTR_ERR(tty);
  662. goto out;
  663. }
  664. /* The tty returned here is locked so we can safely
  665. drop the mutex */
  666. mutex_unlock(&tty_mutex);
  667. set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
  668. tty->driver_data = inode;
  669. tty_add_file(tty, filp);
  670. slave_inode = devpts_pty_new(inode,
  671. MKDEV(UNIX98_PTY_SLAVE_MAJOR, index), index,
  672. tty->link);
  673. if (IS_ERR(slave_inode)) {
  674. retval = PTR_ERR(slave_inode);
  675. goto err_release;
  676. }
  677. tty->link->driver_data = slave_inode;
  678. retval = ptm_driver->ops->open(tty, filp);
  679. if (retval)
  680. goto err_release;
  681. tty_unlock(tty);
  682. return 0;
  683. err_release:
  684. tty_unlock(tty);
  685. tty_release(inode, filp);
  686. return retval;
  687. out:
  688. mutex_unlock(&tty_mutex);
  689. devpts_kill_index(inode, index);
  690. err_file:
  691. tty_free_file(filp);
  692. return retval;
  693. }
  694. static struct file_operations ptmx_fops;
  695. static void __init unix98_pty_init(void)
  696. {
  697. ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
  698. TTY_DRIVER_RESET_TERMIOS |
  699. TTY_DRIVER_REAL_RAW |
  700. TTY_DRIVER_DYNAMIC_DEV |
  701. TTY_DRIVER_DEVPTS_MEM |
  702. TTY_DRIVER_DYNAMIC_ALLOC);
  703. if (IS_ERR(ptm_driver))
  704. panic("Couldn't allocate Unix98 ptm driver");
  705. pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
  706. TTY_DRIVER_RESET_TERMIOS |
  707. TTY_DRIVER_REAL_RAW |
  708. TTY_DRIVER_DYNAMIC_DEV |
  709. TTY_DRIVER_DEVPTS_MEM |
  710. TTY_DRIVER_DYNAMIC_ALLOC);
  711. if (IS_ERR(pts_driver))
  712. panic("Couldn't allocate Unix98 pts driver");
  713. ptm_driver->driver_name = "pty_master";
  714. ptm_driver->name = "ptm";
  715. ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
  716. ptm_driver->minor_start = 0;
  717. ptm_driver->type = TTY_DRIVER_TYPE_PTY;
  718. ptm_driver->subtype = PTY_TYPE_MASTER;
  719. ptm_driver->init_termios = tty_std_termios;
  720. ptm_driver->init_termios.c_iflag = 0;
  721. ptm_driver->init_termios.c_oflag = 0;
  722. ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  723. ptm_driver->init_termios.c_lflag = 0;
  724. ptm_driver->init_termios.c_ispeed = 38400;
  725. ptm_driver->init_termios.c_ospeed = 38400;
  726. ptm_driver->other = pts_driver;
  727. tty_set_operations(ptm_driver, &ptm_unix98_ops);
  728. pts_driver->driver_name = "pty_slave";
  729. pts_driver->name = "pts";
  730. pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
  731. pts_driver->minor_start = 0;
  732. pts_driver->type = TTY_DRIVER_TYPE_PTY;
  733. pts_driver->subtype = PTY_TYPE_SLAVE;
  734. pts_driver->init_termios = tty_std_termios;
  735. pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  736. pts_driver->init_termios.c_ispeed = 38400;
  737. pts_driver->init_termios.c_ospeed = 38400;
  738. pts_driver->other = ptm_driver;
  739. tty_set_operations(pts_driver, &pty_unix98_ops);
  740. if (tty_register_driver(ptm_driver))
  741. panic("Couldn't register Unix98 ptm driver");
  742. if (tty_register_driver(pts_driver))
  743. panic("Couldn't register Unix98 pts driver");
  744. /* Now create the /dev/ptmx special device */
  745. tty_default_fops(&ptmx_fops);
  746. ptmx_fops.open = ptmx_open;
  747. cdev_init(&ptmx_cdev, &ptmx_fops);
  748. if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
  749. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
  750. panic("Couldn't register /dev/ptmx driver");
  751. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
  752. }
  753. #else
  754. static inline void unix98_pty_init(void) { }
  755. #endif
  756. static int __init pty_init(void)
  757. {
  758. legacy_pty_init();
  759. unix98_pty_init();
  760. return 0;
  761. }
  762. module_init(pty_init);