pty.c 12 KB

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