pty.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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_space - report space left for writing
  85. * @to: tty we are writing into
  86. *
  87. * Limit the buffer space used by ptys to 8k.
  88. */
  89. static int pty_space(struct tty_struct *to)
  90. {
  91. int n = tty_buffer_space_avail(to->port);
  92. return min(n, 8192);
  93. }
  94. /**
  95. * pty_write - write to a pty
  96. * @tty: the tty we write from
  97. * @buf: kernel buffer of data
  98. * @count: bytes to write
  99. *
  100. * Our "hardware" write method. Data is coming from the ldisc which
  101. * may be in a non sleeping state. We simply throw this at the other
  102. * end of the link as if we were an IRQ handler receiving stuff for
  103. * the other side of the pty/tty pair.
  104. */
  105. static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
  106. {
  107. struct tty_struct *to = tty->link;
  108. if (tty->stopped)
  109. return 0;
  110. if (c > 0) {
  111. /* Stuff the data into the input queue of the other end */
  112. c = tty_insert_flip_string(to->port, buf, c);
  113. /* And shovel */
  114. if (c)
  115. tty_flip_buffer_push(to->port);
  116. }
  117. return c;
  118. }
  119. /**
  120. * pty_write_room - write space
  121. * @tty: tty we are writing from
  122. *
  123. * Report how many bytes the ldisc can send into the queue for
  124. * the other device.
  125. */
  126. static int pty_write_room(struct tty_struct *tty)
  127. {
  128. if (tty->stopped)
  129. return 0;
  130. return pty_space(tty->link);
  131. }
  132. /**
  133. * pty_chars_in_buffer - characters currently in our tx queue
  134. * @tty: our tty
  135. *
  136. * Report how much we have in the transmit queue. As everything is
  137. * instantly at the other end this is easy to implement.
  138. */
  139. static int pty_chars_in_buffer(struct tty_struct *tty)
  140. {
  141. return 0;
  142. }
  143. /* Set the lock flag on a pty */
  144. static int pty_set_lock(struct tty_struct *tty, int __user *arg)
  145. {
  146. int val;
  147. if (get_user(val, arg))
  148. return -EFAULT;
  149. if (val)
  150. set_bit(TTY_PTY_LOCK, &tty->flags);
  151. else
  152. clear_bit(TTY_PTY_LOCK, &tty->flags);
  153. return 0;
  154. }
  155. static int pty_get_lock(struct tty_struct *tty, int __user *arg)
  156. {
  157. int locked = test_bit(TTY_PTY_LOCK, &tty->flags);
  158. return put_user(locked, arg);
  159. }
  160. /* Set the packet mode on a pty */
  161. static int pty_set_pktmode(struct tty_struct *tty, int __user *arg)
  162. {
  163. int pktmode;
  164. if (get_user(pktmode, arg))
  165. return -EFAULT;
  166. spin_lock_irq(&tty->ctrl_lock);
  167. if (pktmode) {
  168. if (!tty->packet) {
  169. tty->link->ctrl_status = 0;
  170. smp_mb();
  171. tty->packet = 1;
  172. }
  173. } else
  174. tty->packet = 0;
  175. spin_unlock_irq(&tty->ctrl_lock);
  176. return 0;
  177. }
  178. /* Get the packet mode of a pty */
  179. static int pty_get_pktmode(struct tty_struct *tty, int __user *arg)
  180. {
  181. int pktmode = tty->packet;
  182. return put_user(pktmode, arg);
  183. }
  184. /* Send a signal to the slave */
  185. static int pty_signal(struct tty_struct *tty, int sig)
  186. {
  187. struct pid *pgrp;
  188. if (tty->link) {
  189. pgrp = tty_get_pgrp(tty->link);
  190. if (pgrp)
  191. kill_pgrp(pgrp, sig, 1);
  192. put_pid(pgrp);
  193. }
  194. return 0;
  195. }
  196. static void pty_flush_buffer(struct tty_struct *tty)
  197. {
  198. struct tty_struct *to = tty->link;
  199. if (!to)
  200. return;
  201. /* tty_buffer_flush(to); FIXME */
  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. clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  221. set_bit(TTY_THROTTLED, &tty->flags);
  222. return 0;
  223. out:
  224. set_bit(TTY_IO_ERROR, &tty->flags);
  225. return -EIO;
  226. }
  227. static void pty_set_termios(struct tty_struct *tty,
  228. struct ktermios *old_termios)
  229. {
  230. /* See if packet mode change of state. */
  231. if (tty->link && tty->link->packet) {
  232. int extproc = (old_termios->c_lflag & EXTPROC) |
  233. (tty->termios.c_lflag & EXTPROC);
  234. int old_flow = ((old_termios->c_iflag & IXON) &&
  235. (old_termios->c_cc[VSTOP] == '\023') &&
  236. (old_termios->c_cc[VSTART] == '\021'));
  237. int new_flow = (I_IXON(tty) &&
  238. STOP_CHAR(tty) == '\023' &&
  239. START_CHAR(tty) == '\021');
  240. if ((old_flow != new_flow) || extproc) {
  241. spin_lock_irq(&tty->ctrl_lock);
  242. if (old_flow != new_flow) {
  243. tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
  244. if (new_flow)
  245. tty->ctrl_status |= TIOCPKT_DOSTOP;
  246. else
  247. tty->ctrl_status |= TIOCPKT_NOSTOP;
  248. }
  249. if (extproc)
  250. tty->ctrl_status |= TIOCPKT_IOCTL;
  251. spin_unlock_irq(&tty->ctrl_lock);
  252. wake_up_interruptible(&tty->link->read_wait);
  253. }
  254. }
  255. tty->termios.c_cflag &= ~(CSIZE | PARENB);
  256. tty->termios.c_cflag |= (CS8 | CREAD);
  257. }
  258. /**
  259. * pty_do_resize - resize event
  260. * @tty: tty being resized
  261. * @ws: window size being set.
  262. *
  263. * Update the termios variables and send the necessary signals to
  264. * peform a terminal resize correctly
  265. */
  266. static int pty_resize(struct tty_struct *tty, struct winsize *ws)
  267. {
  268. struct pid *pgrp, *rpgrp;
  269. struct tty_struct *pty = tty->link;
  270. /* For a PTY we need to lock the tty side */
  271. mutex_lock(&tty->winsize_mutex);
  272. if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
  273. goto done;
  274. /* Signal the foreground process group of both ptys */
  275. pgrp = tty_get_pgrp(tty);
  276. rpgrp = tty_get_pgrp(pty);
  277. if (pgrp)
  278. kill_pgrp(pgrp, SIGWINCH, 1);
  279. if (rpgrp != pgrp && rpgrp)
  280. kill_pgrp(rpgrp, SIGWINCH, 1);
  281. put_pid(pgrp);
  282. put_pid(rpgrp);
  283. tty->winsize = *ws;
  284. pty->winsize = *ws; /* Never used so will go away soon */
  285. done:
  286. mutex_unlock(&tty->winsize_mutex);
  287. return 0;
  288. }
  289. /**
  290. * pty_start - start() handler
  291. * pty_stop - stop() handler
  292. * @tty: tty being flow-controlled
  293. *
  294. * Propagates the TIOCPKT status to the master pty.
  295. *
  296. * NB: only the master pty can be in packet mode so only the slave
  297. * needs start()/stop() handlers
  298. */
  299. static void pty_start(struct tty_struct *tty)
  300. {
  301. unsigned long flags;
  302. if (tty->link && tty->link->packet) {
  303. spin_lock_irqsave(&tty->ctrl_lock, flags);
  304. tty->ctrl_status &= ~TIOCPKT_STOP;
  305. tty->ctrl_status |= TIOCPKT_START;
  306. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  307. wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
  308. }
  309. }
  310. static void pty_stop(struct tty_struct *tty)
  311. {
  312. unsigned long flags;
  313. if (tty->link && tty->link->packet) {
  314. spin_lock_irqsave(&tty->ctrl_lock, flags);
  315. tty->ctrl_status &= ~TIOCPKT_START;
  316. tty->ctrl_status |= TIOCPKT_STOP;
  317. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  318. wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
  319. }
  320. }
  321. /**
  322. * pty_common_install - set up the pty pair
  323. * @driver: the pty driver
  324. * @tty: the tty being instantiated
  325. * @legacy: true if this is BSD style
  326. *
  327. * Perform the initial set up for the tty/pty pair. Called from the
  328. * tty layer when the port is first opened.
  329. *
  330. * Locking: the caller must hold the tty_mutex
  331. */
  332. static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
  333. bool legacy)
  334. {
  335. struct tty_struct *o_tty;
  336. struct tty_port *ports[2];
  337. int idx = tty->index;
  338. int retval = -ENOMEM;
  339. /* Opening the slave first has always returned -EIO */
  340. if (driver->subtype != PTY_TYPE_MASTER)
  341. return -EIO;
  342. ports[0] = kmalloc(sizeof **ports, GFP_KERNEL);
  343. ports[1] = kmalloc(sizeof **ports, GFP_KERNEL);
  344. if (!ports[0] || !ports[1])
  345. goto err;
  346. if (!try_module_get(driver->other->owner)) {
  347. /* This cannot in fact currently happen */
  348. goto err;
  349. }
  350. o_tty = alloc_tty_struct(driver->other, idx);
  351. if (!o_tty)
  352. goto err_put_module;
  353. tty_set_lock_subclass(o_tty);
  354. if (legacy) {
  355. /* We always use new tty termios data so we can do this
  356. the easy way .. */
  357. retval = tty_init_termios(tty);
  358. if (retval)
  359. goto err_deinit_tty;
  360. retval = tty_init_termios(o_tty);
  361. if (retval)
  362. goto err_free_termios;
  363. driver->other->ttys[idx] = o_tty;
  364. driver->ttys[idx] = tty;
  365. } else {
  366. memset(&tty->termios_locked, 0, sizeof(tty->termios_locked));
  367. tty->termios = driver->init_termios;
  368. memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked));
  369. o_tty->termios = driver->other->init_termios;
  370. }
  371. /*
  372. * Everything allocated ... set up the o_tty structure.
  373. */
  374. tty_driver_kref_get(driver->other);
  375. /* Establish the links in both directions */
  376. tty->link = o_tty;
  377. o_tty->link = tty;
  378. tty_port_init(ports[0]);
  379. tty_port_init(ports[1]);
  380. o_tty->port = ports[0];
  381. tty->port = ports[1];
  382. o_tty->port->itty = o_tty;
  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);