pty.c 24 KB

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