pty.c 11 KB

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