pty.c 22 KB

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