pty.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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.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.chars_in_buffer)
  136. return 0;
  137. /* The ldisc must report 0 if no characters available to be read */
  138. count = to->ldisc.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. if (!to)
  163. return;
  164. if (to->ldisc.flush_buffer)
  165. to->ldisc.flush_buffer(to);
  166. if (to->packet) {
  167. tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
  168. wake_up_interruptible(&to->read_wait);
  169. }
  170. }
  171. static int pty_open(struct tty_struct *tty, struct file * filp)
  172. {
  173. int retval = -ENODEV;
  174. if (!tty || !tty->link)
  175. goto out;
  176. retval = -EIO;
  177. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  178. goto out;
  179. if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
  180. goto out;
  181. if (tty->link->count != 1)
  182. goto out;
  183. clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  184. set_bit(TTY_THROTTLED, &tty->flags);
  185. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  186. retval = 0;
  187. out:
  188. return retval;
  189. }
  190. static void pty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  191. {
  192. tty->termios->c_cflag &= ~(CSIZE | PARENB);
  193. tty->termios->c_cflag |= (CS8 | CREAD);
  194. }
  195. static const struct tty_operations pty_ops = {
  196. .open = pty_open,
  197. .close = pty_close,
  198. .write = pty_write,
  199. .write_room = pty_write_room,
  200. .flush_buffer = pty_flush_buffer,
  201. .chars_in_buffer = pty_chars_in_buffer,
  202. .unthrottle = pty_unthrottle,
  203. .set_termios = pty_set_termios,
  204. };
  205. /* Traditional BSD devices */
  206. #ifdef CONFIG_LEGACY_PTYS
  207. static struct tty_driver *pty_driver, *pty_slave_driver;
  208. static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file,
  209. unsigned int cmd, unsigned long arg)
  210. {
  211. switch (cmd) {
  212. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  213. return pty_set_lock(tty, (int __user *) arg);
  214. }
  215. return -ENOIOCTLCMD;
  216. }
  217. static void __init legacy_pty_init(void)
  218. {
  219. pty_driver = alloc_tty_driver(NR_PTYS);
  220. if (!pty_driver)
  221. panic("Couldn't allocate pty driver");
  222. pty_slave_driver = alloc_tty_driver(NR_PTYS);
  223. if (!pty_slave_driver)
  224. panic("Couldn't allocate pty slave driver");
  225. pty_driver->owner = THIS_MODULE;
  226. pty_driver->driver_name = "pty_master";
  227. pty_driver->name = "pty";
  228. pty_driver->major = PTY_MASTER_MAJOR;
  229. pty_driver->minor_start = 0;
  230. pty_driver->type = TTY_DRIVER_TYPE_PTY;
  231. pty_driver->subtype = PTY_TYPE_MASTER;
  232. pty_driver->init_termios = tty_std_termios;
  233. pty_driver->init_termios.c_iflag = 0;
  234. pty_driver->init_termios.c_oflag = 0;
  235. pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  236. pty_driver->init_termios.c_lflag = 0;
  237. pty_driver->init_termios.c_ispeed = 38400;
  238. pty_driver->init_termios.c_ospeed = 38400;
  239. pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
  240. pty_driver->other = pty_slave_driver;
  241. tty_set_operations(pty_driver, &pty_ops);
  242. pty_driver->ioctl = pty_bsd_ioctl;
  243. pty_slave_driver->owner = THIS_MODULE;
  244. pty_slave_driver->driver_name = "pty_slave";
  245. pty_slave_driver->name = "ttyp";
  246. pty_slave_driver->major = PTY_SLAVE_MAJOR;
  247. pty_slave_driver->minor_start = 0;
  248. pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
  249. pty_slave_driver->subtype = PTY_TYPE_SLAVE;
  250. pty_slave_driver->init_termios = tty_std_termios;
  251. pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  252. pty_slave_driver->init_termios.c_ispeed = 38400;
  253. pty_slave_driver->init_termios.c_ospeed = 38400;
  254. pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
  255. TTY_DRIVER_REAL_RAW;
  256. pty_slave_driver->other = pty_driver;
  257. tty_set_operations(pty_slave_driver, &pty_ops);
  258. if (tty_register_driver(pty_driver))
  259. panic("Couldn't register pty driver");
  260. if (tty_register_driver(pty_slave_driver))
  261. panic("Couldn't register pty slave driver");
  262. }
  263. #else
  264. static inline void legacy_pty_init(void) { }
  265. #endif
  266. /* Unix98 devices */
  267. #ifdef CONFIG_UNIX98_PTYS
  268. /*
  269. * sysctl support for setting limits on the number of Unix98 ptys allocated.
  270. * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
  271. */
  272. int pty_limit = NR_UNIX98_PTY_DEFAULT;
  273. static int pty_limit_min = 0;
  274. static int pty_limit_max = NR_UNIX98_PTY_MAX;
  275. ctl_table pty_table[] = {
  276. {
  277. .ctl_name = PTY_MAX,
  278. .procname = "max",
  279. .maxlen = sizeof(int),
  280. .mode = 0644,
  281. .data = &pty_limit,
  282. .proc_handler = &proc_dointvec_minmax,
  283. .strategy = &sysctl_intvec,
  284. .extra1 = &pty_limit_min,
  285. .extra2 = &pty_limit_max,
  286. }, {
  287. .ctl_name = PTY_NR,
  288. .procname = "nr",
  289. .maxlen = sizeof(int),
  290. .mode = 0444,
  291. .proc_handler = &proc_dointvec,
  292. }, {
  293. .ctl_name = 0
  294. }
  295. };
  296. static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file,
  297. unsigned int cmd, unsigned long arg)
  298. {
  299. switch (cmd) {
  300. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  301. return pty_set_lock(tty, (int __user *)arg);
  302. case TIOCGPTN: /* Get PT Number */
  303. return put_user(tty->index, (unsigned int __user *)arg);
  304. }
  305. return -ENOIOCTLCMD;
  306. }
  307. static void __init unix98_pty_init(void)
  308. {
  309. ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
  310. if (!ptm_driver)
  311. panic("Couldn't allocate Unix98 ptm driver");
  312. pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
  313. if (!pts_driver)
  314. panic("Couldn't allocate Unix98 pts driver");
  315. ptm_driver->owner = THIS_MODULE;
  316. ptm_driver->driver_name = "pty_master";
  317. ptm_driver->name = "ptm";
  318. ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
  319. ptm_driver->minor_start = 0;
  320. ptm_driver->type = TTY_DRIVER_TYPE_PTY;
  321. ptm_driver->subtype = PTY_TYPE_MASTER;
  322. ptm_driver->init_termios = tty_std_termios;
  323. ptm_driver->init_termios.c_iflag = 0;
  324. ptm_driver->init_termios.c_oflag = 0;
  325. ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  326. ptm_driver->init_termios.c_lflag = 0;
  327. ptm_driver->init_termios.c_ispeed = 38400;
  328. ptm_driver->init_termios.c_ospeed = 38400;
  329. ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  330. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
  331. ptm_driver->other = pts_driver;
  332. tty_set_operations(ptm_driver, &pty_ops);
  333. ptm_driver->ioctl = pty_unix98_ioctl;
  334. pts_driver->owner = THIS_MODULE;
  335. pts_driver->driver_name = "pty_slave";
  336. pts_driver->name = "pts";
  337. pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
  338. pts_driver->minor_start = 0;
  339. pts_driver->type = TTY_DRIVER_TYPE_PTY;
  340. pts_driver->subtype = PTY_TYPE_SLAVE;
  341. pts_driver->init_termios = tty_std_termios;
  342. pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  343. pts_driver->init_termios.c_ispeed = 38400;
  344. pts_driver->init_termios.c_ospeed = 38400;
  345. pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  346. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
  347. pts_driver->other = ptm_driver;
  348. tty_set_operations(pts_driver, &pty_ops);
  349. if (tty_register_driver(ptm_driver))
  350. panic("Couldn't register Unix98 ptm driver");
  351. if (tty_register_driver(pts_driver))
  352. panic("Couldn't register Unix98 pts driver");
  353. pty_table[1].data = &ptm_driver->refcount;
  354. }
  355. #else
  356. static inline void unix98_pty_init(void) { }
  357. #endif
  358. static int __init pty_init(void)
  359. {
  360. legacy_pty_init();
  361. unix98_pty_init();
  362. return 0;
  363. }
  364. module_init(pty_init);