pty.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 int legacy_count = CONFIG_LEGACY_PTY_COUNT;
  218. module_param(legacy_count, int, 0);
  219. static void __init legacy_pty_init(void)
  220. {
  221. if (legacy_count <= 0)
  222. return;
  223. pty_driver = alloc_tty_driver(legacy_count);
  224. if (!pty_driver)
  225. panic("Couldn't allocate pty driver");
  226. pty_slave_driver = alloc_tty_driver(legacy_count);
  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->major = PTY_MASTER_MAJOR;
  233. pty_driver->minor_start = 0;
  234. pty_driver->type = TTY_DRIVER_TYPE_PTY;
  235. pty_driver->subtype = PTY_TYPE_MASTER;
  236. pty_driver->init_termios = tty_std_termios;
  237. pty_driver->init_termios.c_iflag = 0;
  238. pty_driver->init_termios.c_oflag = 0;
  239. pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  240. pty_driver->init_termios.c_lflag = 0;
  241. pty_driver->init_termios.c_ispeed = 38400;
  242. pty_driver->init_termios.c_ospeed = 38400;
  243. pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
  244. pty_driver->other = pty_slave_driver;
  245. tty_set_operations(pty_driver, &pty_ops);
  246. pty_driver->ioctl = pty_bsd_ioctl;
  247. pty_slave_driver->owner = THIS_MODULE;
  248. pty_slave_driver->driver_name = "pty_slave";
  249. pty_slave_driver->name = "ttyp";
  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->init_termios.c_ispeed = 38400;
  257. pty_slave_driver->init_termios.c_ospeed = 38400;
  258. pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
  259. TTY_DRIVER_REAL_RAW;
  260. pty_slave_driver->other = pty_driver;
  261. tty_set_operations(pty_slave_driver, &pty_ops);
  262. if (tty_register_driver(pty_driver))
  263. panic("Couldn't register pty driver");
  264. if (tty_register_driver(pty_slave_driver))
  265. panic("Couldn't register pty slave driver");
  266. }
  267. #else
  268. static inline void legacy_pty_init(void) { }
  269. #endif
  270. /* Unix98 devices */
  271. #ifdef CONFIG_UNIX98_PTYS
  272. /*
  273. * sysctl support for setting limits on the number of Unix98 ptys allocated.
  274. * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
  275. */
  276. int pty_limit = NR_UNIX98_PTY_DEFAULT;
  277. static int pty_limit_min = 0;
  278. static int pty_limit_max = NR_UNIX98_PTY_MAX;
  279. static struct ctl_table pty_table[] = {
  280. {
  281. .ctl_name = PTY_MAX,
  282. .procname = "max",
  283. .maxlen = sizeof(int),
  284. .mode = 0644,
  285. .data = &pty_limit,
  286. .proc_handler = &proc_dointvec_minmax,
  287. .strategy = &sysctl_intvec,
  288. .extra1 = &pty_limit_min,
  289. .extra2 = &pty_limit_max,
  290. }, {
  291. .ctl_name = PTY_NR,
  292. .procname = "nr",
  293. .maxlen = sizeof(int),
  294. .mode = 0444,
  295. .proc_handler = &proc_dointvec,
  296. }, {
  297. .ctl_name = 0
  298. }
  299. };
  300. static struct ctl_table pty_kern_table[] = {
  301. {
  302. .ctl_name = KERN_PTY,
  303. .procname = "pty",
  304. .mode = 0555,
  305. .child = pty_table,
  306. },
  307. {}
  308. };
  309. static struct ctl_table pty_root_table[] = {
  310. {
  311. .ctl_name = CTL_KERN,
  312. .procname = "kernel",
  313. .mode = 0555,
  314. .child = pty_kern_table,
  315. },
  316. {}
  317. };
  318. static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file,
  319. unsigned int cmd, unsigned long arg)
  320. {
  321. switch (cmd) {
  322. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  323. return pty_set_lock(tty, (int __user *)arg);
  324. case TIOCGPTN: /* Get PT Number */
  325. return put_user(tty->index, (unsigned int __user *)arg);
  326. }
  327. return -ENOIOCTLCMD;
  328. }
  329. static void __init unix98_pty_init(void)
  330. {
  331. ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
  332. if (!ptm_driver)
  333. panic("Couldn't allocate Unix98 ptm driver");
  334. pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
  335. if (!pts_driver)
  336. panic("Couldn't allocate Unix98 pts driver");
  337. ptm_driver->owner = THIS_MODULE;
  338. ptm_driver->driver_name = "pty_master";
  339. ptm_driver->name = "ptm";
  340. ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
  341. ptm_driver->minor_start = 0;
  342. ptm_driver->type = TTY_DRIVER_TYPE_PTY;
  343. ptm_driver->subtype = PTY_TYPE_MASTER;
  344. ptm_driver->init_termios = tty_std_termios;
  345. ptm_driver->init_termios.c_iflag = 0;
  346. ptm_driver->init_termios.c_oflag = 0;
  347. ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  348. ptm_driver->init_termios.c_lflag = 0;
  349. ptm_driver->init_termios.c_ispeed = 38400;
  350. ptm_driver->init_termios.c_ospeed = 38400;
  351. ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  352. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
  353. ptm_driver->other = pts_driver;
  354. tty_set_operations(ptm_driver, &pty_ops);
  355. ptm_driver->ioctl = pty_unix98_ioctl;
  356. pts_driver->owner = THIS_MODULE;
  357. pts_driver->driver_name = "pty_slave";
  358. pts_driver->name = "pts";
  359. pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
  360. pts_driver->minor_start = 0;
  361. pts_driver->type = TTY_DRIVER_TYPE_PTY;
  362. pts_driver->subtype = PTY_TYPE_SLAVE;
  363. pts_driver->init_termios = tty_std_termios;
  364. pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  365. pts_driver->init_termios.c_ispeed = 38400;
  366. pts_driver->init_termios.c_ospeed = 38400;
  367. pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  368. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
  369. pts_driver->other = ptm_driver;
  370. tty_set_operations(pts_driver, &pty_ops);
  371. if (tty_register_driver(ptm_driver))
  372. panic("Couldn't register Unix98 ptm driver");
  373. if (tty_register_driver(pts_driver))
  374. panic("Couldn't register Unix98 pts driver");
  375. pty_table[1].data = &ptm_driver->refcount;
  376. register_sysctl_table(pty_root_table);
  377. }
  378. #else
  379. static inline void unix98_pty_init(void) { }
  380. #endif
  381. static int __init pty_init(void)
  382. {
  383. legacy_pty_init();
  384. unix98_pty_init();
  385. return 0;
  386. }
  387. module_init(pty_init);