pty.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * linux/drivers/char/pty.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * Added support for a Unix98-style ptmx device.
  7. * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
  8. * Added TTY_DO_WRITE_WAKEUP to enable n_tty to send POLL_OUT to
  9. * waiting writers -- Sapan Bhatia <sapan@corewars.org>
  10. *
  11. *
  12. */
  13. #include <linux/module.h> /* For EXPORT_SYMBOL */
  14. #include <linux/errno.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/tty.h>
  17. #include <linux/tty_flip.h>
  18. #include <linux/fcntl.h>
  19. #include <linux/string.h>
  20. #include <linux/major.h>
  21. #include <linux/mm.h>
  22. #include <linux/init.h>
  23. #include <linux/sysctl.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/system.h>
  26. #include <linux/bitops.h>
  27. #include <linux/devpts_fs.h>
  28. /* These are global because they are accessed in tty_io.c */
  29. #ifdef CONFIG_UNIX98_PTYS
  30. struct tty_driver *ptm_driver;
  31. static struct tty_driver *pts_driver;
  32. #endif
  33. static void pty_close(struct tty_struct * tty, struct file * filp)
  34. {
  35. if (!tty)
  36. return;
  37. if (tty->driver->subtype == PTY_TYPE_MASTER) {
  38. if (tty->count > 1)
  39. printk("master pty_close: count = %d!!\n", tty->count);
  40. } else {
  41. if (tty->count > 2)
  42. return;
  43. }
  44. wake_up_interruptible(&tty->read_wait);
  45. wake_up_interruptible(&tty->write_wait);
  46. tty->packet = 0;
  47. if (!tty->link)
  48. return;
  49. tty->link->packet = 0;
  50. set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  51. wake_up_interruptible(&tty->link->read_wait);
  52. wake_up_interruptible(&tty->link->write_wait);
  53. if (tty->driver->subtype == PTY_TYPE_MASTER) {
  54. set_bit(TTY_OTHER_CLOSED, &tty->flags);
  55. #ifdef CONFIG_UNIX98_PTYS
  56. if (tty->driver == ptm_driver)
  57. devpts_pty_kill(tty->index);
  58. #endif
  59. tty_vhangup(tty->link);
  60. }
  61. }
  62. /*
  63. * The unthrottle routine is called by the line discipline to signal
  64. * that it can receive more characters. For PTY's, the TTY_THROTTLED
  65. * flag is always set, to force the line discipline to always call the
  66. * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
  67. * characters in the queue. This is necessary since each time this
  68. * happens, we need to wake up any sleeping processes that could be
  69. * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
  70. * for the pty buffer to be drained.
  71. */
  72. static void pty_unthrottle(struct tty_struct * tty)
  73. {
  74. struct tty_struct *o_tty = tty->link;
  75. if (!o_tty)
  76. return;
  77. tty_wakeup(o_tty);
  78. set_bit(TTY_THROTTLED, &tty->flags);
  79. }
  80. /*
  81. * WSH 05/24/97: modified to
  82. * (1) use space in tty->flip instead of a shared temp buffer
  83. * The flip buffers aren't being used for a pty, so there's lots
  84. * of space available. The buffer is protected by a per-pty
  85. * semaphore that should almost never come under contention.
  86. * (2) avoid redundant copying for cases where count >> receive_room
  87. * N.B. Calls from user space may now return an error code instead of
  88. * a count.
  89. *
  90. * FIXME: Our pty_write method is called with our ldisc lock held but
  91. * not our partners. We can't just take the other one blindly without
  92. * risking deadlocks.
  93. */
  94. static int pty_write(struct tty_struct * tty, const unsigned char *buf, int count)
  95. {
  96. struct tty_struct *to = tty->link;
  97. int c;
  98. if (!to || tty->stopped)
  99. return 0;
  100. c = to->receive_room;
  101. if (c > count)
  102. c = count;
  103. to->ldisc.ops->receive_buf(to, buf, NULL, c);
  104. return c;
  105. }
  106. static int pty_write_room(struct tty_struct *tty)
  107. {
  108. struct tty_struct *to = tty->link;
  109. if (!to || tty->stopped)
  110. return 0;
  111. return to->receive_room;
  112. }
  113. /*
  114. * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior
  115. * The chars_in_buffer() value is used by the ldisc select() function
  116. * to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256).
  117. * The pty driver chars_in_buffer() Master/Slave must behave differently:
  118. *
  119. * The Master side needs to allow typed-ahead commands to accumulate
  120. * while being canonicalized, so we report "our buffer" as empty until
  121. * some threshold is reached, and then report the count. (Any count >
  122. * WAKEUP_CHARS is regarded by select() as "full".) To avoid deadlock
  123. * the count returned must be 0 if no canonical data is available to be
  124. * read. (The N_TTY ldisc.chars_in_buffer now knows this.)
  125. *
  126. * The Slave side passes all characters in raw mode to the Master side's
  127. * buffer where they can be read immediately, so in this case we can
  128. * return the true count in the buffer.
  129. */
  130. static int pty_chars_in_buffer(struct tty_struct *tty)
  131. {
  132. struct tty_struct *to = tty->link;
  133. int count;
  134. /* We should get the line discipline lock for "tty->link" */
  135. if (!to || !to->ldisc.ops->chars_in_buffer)
  136. return 0;
  137. /* The ldisc must report 0 if no characters available to be read */
  138. count = to->ldisc.ops->chars_in_buffer(to);
  139. if (tty->driver->subtype == PTY_TYPE_SLAVE) return count;
  140. /* Master side driver ... if the other side's read buffer is less than
  141. * half full, return 0 to allow writers to proceed; otherwise return
  142. * the count. This leaves a comfortable margin to avoid overflow,
  143. * and still allows half a buffer's worth of typed-ahead commands.
  144. */
  145. return ((count < N_TTY_BUF_SIZE/2) ? 0 : count);
  146. }
  147. /* Set the lock flag on a pty */
  148. static int pty_set_lock(struct tty_struct *tty, int __user * arg)
  149. {
  150. int val;
  151. if (get_user(val,arg))
  152. return -EFAULT;
  153. if (val)
  154. set_bit(TTY_PTY_LOCK, &tty->flags);
  155. else
  156. clear_bit(TTY_PTY_LOCK, &tty->flags);
  157. return 0;
  158. }
  159. static void pty_flush_buffer(struct tty_struct *tty)
  160. {
  161. struct tty_struct *to = tty->link;
  162. unsigned long flags;
  163. if (!to)
  164. return;
  165. if (to->ldisc.ops->flush_buffer)
  166. to->ldisc.ops->flush_buffer(to);
  167. if (to->packet) {
  168. spin_lock_irqsave(&tty->ctrl_lock, flags);
  169. tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
  170. wake_up_interruptible(&to->read_wait);
  171. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  172. }
  173. }
  174. static int pty_open(struct tty_struct *tty, struct file * filp)
  175. {
  176. int retval = -ENODEV;
  177. if (!tty || !tty->link)
  178. goto out;
  179. retval = -EIO;
  180. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  181. goto out;
  182. if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
  183. goto out;
  184. if (tty->link->count != 1)
  185. goto out;
  186. clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  187. set_bit(TTY_THROTTLED, &tty->flags);
  188. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  189. retval = 0;
  190. out:
  191. return retval;
  192. }
  193. static void pty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  194. {
  195. tty->termios->c_cflag &= ~(CSIZE | PARENB);
  196. tty->termios->c_cflag |= (CS8 | CREAD);
  197. }
  198. static const struct tty_operations pty_ops = {
  199. .open = pty_open,
  200. .close = pty_close,
  201. .write = pty_write,
  202. .write_room = pty_write_room,
  203. .flush_buffer = pty_flush_buffer,
  204. .chars_in_buffer = pty_chars_in_buffer,
  205. .unthrottle = pty_unthrottle,
  206. .set_termios = pty_set_termios,
  207. };
  208. /* Traditional BSD devices */
  209. #ifdef CONFIG_LEGACY_PTYS
  210. static struct tty_driver *pty_driver, *pty_slave_driver;
  211. static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file,
  212. unsigned int cmd, unsigned long arg)
  213. {
  214. switch (cmd) {
  215. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  216. return pty_set_lock(tty, (int __user *) arg);
  217. }
  218. return -ENOIOCTLCMD;
  219. }
  220. static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
  221. module_param(legacy_count, int, 0);
  222. static const struct tty_operations pty_ops_bsd = {
  223. .open = pty_open,
  224. .close = pty_close,
  225. .write = pty_write,
  226. .write_room = pty_write_room,
  227. .flush_buffer = pty_flush_buffer,
  228. .chars_in_buffer = pty_chars_in_buffer,
  229. .unthrottle = pty_unthrottle,
  230. .set_termios = pty_set_termios,
  231. .ioctl = pty_bsd_ioctl,
  232. };
  233. static void __init legacy_pty_init(void)
  234. {
  235. if (legacy_count <= 0)
  236. return;
  237. pty_driver = alloc_tty_driver(legacy_count);
  238. if (!pty_driver)
  239. panic("Couldn't allocate pty driver");
  240. pty_slave_driver = alloc_tty_driver(legacy_count);
  241. if (!pty_slave_driver)
  242. panic("Couldn't allocate pty slave driver");
  243. pty_driver->owner = THIS_MODULE;
  244. pty_driver->driver_name = "pty_master";
  245. pty_driver->name = "pty";
  246. pty_driver->major = PTY_MASTER_MAJOR;
  247. pty_driver->minor_start = 0;
  248. pty_driver->type = TTY_DRIVER_TYPE_PTY;
  249. pty_driver->subtype = PTY_TYPE_MASTER;
  250. pty_driver->init_termios = tty_std_termios;
  251. pty_driver->init_termios.c_iflag = 0;
  252. pty_driver->init_termios.c_oflag = 0;
  253. pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  254. pty_driver->init_termios.c_lflag = 0;
  255. pty_driver->init_termios.c_ispeed = 38400;
  256. pty_driver->init_termios.c_ospeed = 38400;
  257. pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
  258. pty_driver->other = pty_slave_driver;
  259. tty_set_operations(pty_driver, &pty_ops);
  260. pty_slave_driver->owner = THIS_MODULE;
  261. pty_slave_driver->driver_name = "pty_slave";
  262. pty_slave_driver->name = "ttyp";
  263. pty_slave_driver->major = PTY_SLAVE_MAJOR;
  264. pty_slave_driver->minor_start = 0;
  265. pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
  266. pty_slave_driver->subtype = PTY_TYPE_SLAVE;
  267. pty_slave_driver->init_termios = tty_std_termios;
  268. pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  269. pty_slave_driver->init_termios.c_ispeed = 38400;
  270. pty_slave_driver->init_termios.c_ospeed = 38400;
  271. pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
  272. TTY_DRIVER_REAL_RAW;
  273. pty_slave_driver->other = pty_driver;
  274. tty_set_operations(pty_slave_driver, &pty_ops);
  275. if (tty_register_driver(pty_driver))
  276. panic("Couldn't register pty driver");
  277. if (tty_register_driver(pty_slave_driver))
  278. panic("Couldn't register pty slave driver");
  279. }
  280. #else
  281. static inline void legacy_pty_init(void) { }
  282. #endif
  283. /* Unix98 devices */
  284. #ifdef CONFIG_UNIX98_PTYS
  285. /*
  286. * sysctl support for setting limits on the number of Unix98 ptys allocated.
  287. * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
  288. */
  289. int pty_limit = NR_UNIX98_PTY_DEFAULT;
  290. static int pty_limit_min = 0;
  291. static int pty_limit_max = NR_UNIX98_PTY_MAX;
  292. static struct ctl_table pty_table[] = {
  293. {
  294. .ctl_name = PTY_MAX,
  295. .procname = "max",
  296. .maxlen = sizeof(int),
  297. .mode = 0644,
  298. .data = &pty_limit,
  299. .proc_handler = &proc_dointvec_minmax,
  300. .strategy = &sysctl_intvec,
  301. .extra1 = &pty_limit_min,
  302. .extra2 = &pty_limit_max,
  303. }, {
  304. .ctl_name = PTY_NR,
  305. .procname = "nr",
  306. .maxlen = sizeof(int),
  307. .mode = 0444,
  308. .proc_handler = &proc_dointvec,
  309. }, {
  310. .ctl_name = 0
  311. }
  312. };
  313. static struct ctl_table pty_kern_table[] = {
  314. {
  315. .ctl_name = KERN_PTY,
  316. .procname = "pty",
  317. .mode = 0555,
  318. .child = pty_table,
  319. },
  320. {}
  321. };
  322. static struct ctl_table pty_root_table[] = {
  323. {
  324. .ctl_name = CTL_KERN,
  325. .procname = "kernel",
  326. .mode = 0555,
  327. .child = pty_kern_table,
  328. },
  329. {}
  330. };
  331. static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file,
  332. unsigned int cmd, unsigned long arg)
  333. {
  334. switch (cmd) {
  335. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  336. return pty_set_lock(tty, (int __user *)arg);
  337. case TIOCGPTN: /* Get PT Number */
  338. return put_user(tty->index, (unsigned int __user *)arg);
  339. }
  340. return -ENOIOCTLCMD;
  341. }
  342. static const struct tty_operations pty_unix98_ops = {
  343. .open = pty_open,
  344. .close = pty_close,
  345. .write = pty_write,
  346. .write_room = pty_write_room,
  347. .flush_buffer = pty_flush_buffer,
  348. .chars_in_buffer = pty_chars_in_buffer,
  349. .unthrottle = pty_unthrottle,
  350. .set_termios = pty_set_termios,
  351. .ioctl = pty_unix98_ioctl
  352. };
  353. static void __init unix98_pty_init(void)
  354. {
  355. ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
  356. if (!ptm_driver)
  357. panic("Couldn't allocate Unix98 ptm driver");
  358. pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
  359. if (!pts_driver)
  360. panic("Couldn't allocate Unix98 pts driver");
  361. ptm_driver->owner = THIS_MODULE;
  362. ptm_driver->driver_name = "pty_master";
  363. ptm_driver->name = "ptm";
  364. ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
  365. ptm_driver->minor_start = 0;
  366. ptm_driver->type = TTY_DRIVER_TYPE_PTY;
  367. ptm_driver->subtype = PTY_TYPE_MASTER;
  368. ptm_driver->init_termios = tty_std_termios;
  369. ptm_driver->init_termios.c_iflag = 0;
  370. ptm_driver->init_termios.c_oflag = 0;
  371. ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  372. ptm_driver->init_termios.c_lflag = 0;
  373. ptm_driver->init_termios.c_ispeed = 38400;
  374. ptm_driver->init_termios.c_ospeed = 38400;
  375. ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  376. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
  377. ptm_driver->other = pts_driver;
  378. tty_set_operations(ptm_driver, &pty_unix98_ops);
  379. pts_driver->owner = THIS_MODULE;
  380. pts_driver->driver_name = "pty_slave";
  381. pts_driver->name = "pts";
  382. pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
  383. pts_driver->minor_start = 0;
  384. pts_driver->type = TTY_DRIVER_TYPE_PTY;
  385. pts_driver->subtype = PTY_TYPE_SLAVE;
  386. pts_driver->init_termios = tty_std_termios;
  387. pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  388. pts_driver->init_termios.c_ispeed = 38400;
  389. pts_driver->init_termios.c_ospeed = 38400;
  390. pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  391. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
  392. pts_driver->other = ptm_driver;
  393. tty_set_operations(pts_driver, &pty_ops);
  394. if (tty_register_driver(ptm_driver))
  395. panic("Couldn't register Unix98 ptm driver");
  396. if (tty_register_driver(pts_driver))
  397. panic("Couldn't register Unix98 pts driver");
  398. pty_table[1].data = &ptm_driver->refcount;
  399. register_sysctl_table(pty_root_table);
  400. }
  401. #else
  402. static inline void unix98_pty_init(void) { }
  403. #endif
  404. static int __init pty_init(void)
  405. {
  406. legacy_pty_init();
  407. unix98_pty_init();
  408. return 0;
  409. }
  410. module_init(pty_init);