pty.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. *
  9. * When reading this code see also fs/devpts. In particular note that the
  10. * driver_data field is used by the devpts side as a binding to the devpts
  11. * inode.
  12. */
  13. #include <linux/module.h>
  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 <linux/device.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/bitops.h>
  27. #include <linux/devpts_fs.h>
  28. #include <asm/system.h>
  29. #ifdef CONFIG_UNIX98_PTYS
  30. static 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. BUG_ON(!tty);
  36. if (tty->driver->subtype == PTY_TYPE_MASTER)
  37. WARN_ON(tty->count > 1);
  38. else {
  39. if (tty->count > 2)
  40. return;
  41. }
  42. wake_up_interruptible(&tty->read_wait);
  43. wake_up_interruptible(&tty->write_wait);
  44. tty->packet = 0;
  45. if (!tty->link)
  46. return;
  47. tty->link->packet = 0;
  48. set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  49. wake_up_interruptible(&tty->link->read_wait);
  50. wake_up_interruptible(&tty->link->write_wait);
  51. if (tty->driver->subtype == PTY_TYPE_MASTER) {
  52. set_bit(TTY_OTHER_CLOSED, &tty->flags);
  53. #ifdef CONFIG_UNIX98_PTYS
  54. if (tty->driver == ptm_driver)
  55. devpts_pty_kill(tty->link);
  56. #endif
  57. tty_vhangup(tty->link);
  58. }
  59. }
  60. /*
  61. * The unthrottle routine is called by the line discipline to signal
  62. * that it can receive more characters. For PTY's, the TTY_THROTTLED
  63. * flag is always set, to force the line discipline to always call the
  64. * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
  65. * characters in the queue. This is necessary since each time this
  66. * happens, we need to wake up any sleeping processes that could be
  67. * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
  68. * for the pty buffer to be drained.
  69. */
  70. static void pty_unthrottle(struct tty_struct *tty)
  71. {
  72. struct tty_struct *o_tty = tty->link;
  73. if (!o_tty)
  74. return;
  75. tty_wakeup(o_tty);
  76. set_bit(TTY_THROTTLED, &tty->flags);
  77. }
  78. /*
  79. * WSH 05/24/97: modified to
  80. * (1) use space in tty->flip instead of a shared temp buffer
  81. * The flip buffers aren't being used for a pty, so there's lots
  82. * of space available. The buffer is protected by a per-pty
  83. * semaphore that should almost never come under contention.
  84. * (2) avoid redundant copying for cases where count >> receive_room
  85. * N.B. Calls from user space may now return an error code instead of
  86. * a count.
  87. *
  88. * FIXME: Our pty_write method is called with our ldisc lock held but
  89. * not our partners. We can't just wait on the other one blindly without
  90. * risking deadlocks. At some point when everything has settled down we need
  91. * to look into making pty_write at least able to sleep over an ldisc change.
  92. *
  93. * The return on no ldisc is a bit counter intuitive but the logic works
  94. * like this. During an ldisc change the other end will flush its buffers. We
  95. * thus return the full length which is identical to the case where we had
  96. * proper locking and happened to queue the bytes just before the flush during
  97. * the ldisc change.
  98. */
  99. static int pty_write(struct tty_struct *tty, const unsigned char *buf,
  100. int count)
  101. {
  102. struct tty_struct *to = tty->link;
  103. struct tty_ldisc *ld;
  104. int c = count;
  105. if (!to || tty->stopped)
  106. return 0;
  107. ld = tty_ldisc_ref(to);
  108. if (ld) {
  109. c = to->receive_room;
  110. if (c > count)
  111. c = count;
  112. ld->ops->receive_buf(to, buf, NULL, c);
  113. tty_ldisc_deref(ld);
  114. }
  115. return c;
  116. }
  117. static int pty_write_room(struct tty_struct *tty)
  118. {
  119. struct tty_struct *to = tty->link;
  120. if (!to || tty->stopped)
  121. return 0;
  122. return to->receive_room;
  123. }
  124. /*
  125. * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior
  126. * The chars_in_buffer() value is used by the ldisc select() function
  127. * to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256).
  128. * The pty driver chars_in_buffer() Master/Slave must behave differently:
  129. *
  130. * The Master side needs to allow typed-ahead commands to accumulate
  131. * while being canonicalized, so we report "our buffer" as empty until
  132. * some threshold is reached, and then report the count. (Any count >
  133. * WAKEUP_CHARS is regarded by select() as "full".) To avoid deadlock
  134. * the count returned must be 0 if no canonical data is available to be
  135. * read. (The N_TTY ldisc.chars_in_buffer now knows this.)
  136. *
  137. * The Slave side passes all characters in raw mode to the Master side's
  138. * buffer where they can be read immediately, so in this case we can
  139. * return the true count in the buffer.
  140. */
  141. static int pty_chars_in_buffer(struct tty_struct *tty)
  142. {
  143. struct tty_struct *to = tty->link;
  144. struct tty_ldisc *ld;
  145. int count = 0;
  146. /* We should get the line discipline lock for "tty->link" */
  147. if (!to)
  148. return 0;
  149. /* We cannot take a sleeping reference here without deadlocking with
  150. an ldisc change - but it doesn't really matter */
  151. ld = tty_ldisc_ref(to);
  152. if (ld == NULL)
  153. return 0;
  154. /* The ldisc must report 0 if no characters available to be read */
  155. if (ld->ops->chars_in_buffer)
  156. count = ld->ops->chars_in_buffer(to);
  157. tty_ldisc_deref(ld);
  158. if (tty->driver->subtype == PTY_TYPE_SLAVE)
  159. return count;
  160. /* Master side driver ... if the other side's read buffer is less than
  161. * half full, return 0 to allow writers to proceed; otherwise return
  162. * the count. This leaves a comfortable margin to avoid overflow,
  163. * and still allows half a buffer's worth of typed-ahead commands.
  164. */
  165. return (count < N_TTY_BUF_SIZE/2) ? 0 : count;
  166. }
  167. /* Set the lock flag on a pty */
  168. static int pty_set_lock(struct tty_struct *tty, int __user *arg)
  169. {
  170. int val;
  171. if (get_user(val, arg))
  172. return -EFAULT;
  173. if (val)
  174. set_bit(TTY_PTY_LOCK, &tty->flags);
  175. else
  176. clear_bit(TTY_PTY_LOCK, &tty->flags);
  177. return 0;
  178. }
  179. static void pty_flush_buffer(struct tty_struct *tty)
  180. {
  181. struct tty_struct *to = tty->link;
  182. unsigned long flags;
  183. struct tty_ldisc *ld;
  184. if (!to)
  185. return;
  186. ld = tty_ldisc_ref(to);
  187. /* The other end is changing discipline */
  188. if (!ld)
  189. return;
  190. if (ld->ops->flush_buffer)
  191. to->ldisc->ops->flush_buffer(to);
  192. tty_ldisc_deref(ld);
  193. if (to->packet) {
  194. spin_lock_irqsave(&tty->ctrl_lock, flags);
  195. tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
  196. wake_up_interruptible(&to->read_wait);
  197. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  198. }
  199. }
  200. static int pty_open(struct tty_struct *tty, struct file *filp)
  201. {
  202. int retval = -ENODEV;
  203. if (!tty || !tty->link)
  204. goto out;
  205. retval = -EIO;
  206. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  207. goto out;
  208. if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
  209. goto out;
  210. if (tty->link->count != 1)
  211. goto out;
  212. clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  213. set_bit(TTY_THROTTLED, &tty->flags);
  214. retval = 0;
  215. out:
  216. return retval;
  217. }
  218. static void pty_set_termios(struct tty_struct *tty,
  219. struct ktermios *old_termios)
  220. {
  221. tty->termios->c_cflag &= ~(CSIZE | PARENB);
  222. tty->termios->c_cflag |= (CS8 | CREAD);
  223. }
  224. /**
  225. * pty_do_resize - resize event
  226. * @tty: tty being resized
  227. * @ws: window size being set.
  228. *
  229. * Update the termios variables and send the neccessary signals to
  230. * peform a terminal resize correctly
  231. */
  232. int pty_resize(struct tty_struct *tty, struct winsize *ws)
  233. {
  234. struct pid *pgrp, *rpgrp;
  235. unsigned long flags;
  236. struct tty_struct *pty = tty->link;
  237. /* For a PTY we need to lock the tty side */
  238. mutex_lock(&tty->termios_mutex);
  239. if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
  240. goto done;
  241. /* Get the PID values and reference them so we can
  242. avoid holding the tty ctrl lock while sending signals.
  243. We need to lock these individually however. */
  244. spin_lock_irqsave(&tty->ctrl_lock, flags);
  245. pgrp = get_pid(tty->pgrp);
  246. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  247. spin_lock_irqsave(&pty->ctrl_lock, flags);
  248. rpgrp = get_pid(pty->pgrp);
  249. spin_unlock_irqrestore(&pty->ctrl_lock, flags);
  250. if (pgrp)
  251. kill_pgrp(pgrp, SIGWINCH, 1);
  252. if (rpgrp != pgrp && rpgrp)
  253. kill_pgrp(rpgrp, SIGWINCH, 1);
  254. put_pid(pgrp);
  255. put_pid(rpgrp);
  256. tty->winsize = *ws;
  257. pty->winsize = *ws; /* Never used so will go away soon */
  258. done:
  259. mutex_unlock(&tty->termios_mutex);
  260. return 0;
  261. }
  262. static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
  263. {
  264. struct tty_struct *o_tty;
  265. int idx = tty->index;
  266. int retval;
  267. o_tty = alloc_tty_struct();
  268. if (!o_tty)
  269. return -ENOMEM;
  270. if (!try_module_get(driver->other->owner)) {
  271. /* This cannot in fact currently happen */
  272. free_tty_struct(o_tty);
  273. return -ENOMEM;
  274. }
  275. initialize_tty_struct(o_tty, driver->other, idx);
  276. /* We always use new tty termios data so we can do this
  277. the easy way .. */
  278. retval = tty_init_termios(tty);
  279. if (retval)
  280. goto free_mem_out;
  281. retval = tty_init_termios(o_tty);
  282. if (retval) {
  283. tty_free_termios(tty);
  284. goto free_mem_out;
  285. }
  286. /*
  287. * Everything allocated ... set up the o_tty structure.
  288. */
  289. driver->other->ttys[idx] = o_tty;
  290. tty_driver_kref_get(driver->other);
  291. if (driver->subtype == PTY_TYPE_MASTER)
  292. o_tty->count++;
  293. /* Establish the links in both directions */
  294. tty->link = o_tty;
  295. o_tty->link = tty;
  296. tty_driver_kref_get(driver);
  297. tty->count++;
  298. driver->ttys[idx] = tty;
  299. return 0;
  300. free_mem_out:
  301. module_put(o_tty->driver->owner);
  302. free_tty_struct(o_tty);
  303. return -ENOMEM;
  304. }
  305. static const struct tty_operations pty_ops = {
  306. .install = pty_install,
  307. .open = pty_open,
  308. .close = pty_close,
  309. .write = pty_write,
  310. .write_room = pty_write_room,
  311. .flush_buffer = pty_flush_buffer,
  312. .chars_in_buffer = pty_chars_in_buffer,
  313. .unthrottle = pty_unthrottle,
  314. .set_termios = pty_set_termios,
  315. .resize = pty_resize
  316. };
  317. /* Traditional BSD devices */
  318. #ifdef CONFIG_LEGACY_PTYS
  319. static struct tty_driver *pty_driver, *pty_slave_driver;
  320. static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file,
  321. unsigned int cmd, unsigned long arg)
  322. {
  323. switch (cmd) {
  324. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  325. return pty_set_lock(tty, (int __user *) arg);
  326. }
  327. return -ENOIOCTLCMD;
  328. }
  329. static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
  330. module_param(legacy_count, int, 0);
  331. static const struct tty_operations pty_ops_bsd = {
  332. .open = pty_open,
  333. .close = pty_close,
  334. .write = pty_write,
  335. .write_room = pty_write_room,
  336. .flush_buffer = pty_flush_buffer,
  337. .chars_in_buffer = pty_chars_in_buffer,
  338. .unthrottle = pty_unthrottle,
  339. .set_termios = pty_set_termios,
  340. .ioctl = pty_bsd_ioctl,
  341. .resize = pty_resize
  342. };
  343. static void __init legacy_pty_init(void)
  344. {
  345. if (legacy_count <= 0)
  346. return;
  347. pty_driver = alloc_tty_driver(legacy_count);
  348. if (!pty_driver)
  349. panic("Couldn't allocate pty driver");
  350. pty_slave_driver = alloc_tty_driver(legacy_count);
  351. if (!pty_slave_driver)
  352. panic("Couldn't allocate pty slave driver");
  353. pty_driver->owner = THIS_MODULE;
  354. pty_driver->driver_name = "pty_master";
  355. pty_driver->name = "pty";
  356. pty_driver->major = PTY_MASTER_MAJOR;
  357. pty_driver->minor_start = 0;
  358. pty_driver->type = TTY_DRIVER_TYPE_PTY;
  359. pty_driver->subtype = PTY_TYPE_MASTER;
  360. pty_driver->init_termios = tty_std_termios;
  361. pty_driver->init_termios.c_iflag = 0;
  362. pty_driver->init_termios.c_oflag = 0;
  363. pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  364. pty_driver->init_termios.c_lflag = 0;
  365. pty_driver->init_termios.c_ispeed = 38400;
  366. pty_driver->init_termios.c_ospeed = 38400;
  367. pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
  368. pty_driver->other = pty_slave_driver;
  369. tty_set_operations(pty_driver, &pty_ops);
  370. pty_slave_driver->owner = THIS_MODULE;
  371. pty_slave_driver->driver_name = "pty_slave";
  372. pty_slave_driver->name = "ttyp";
  373. pty_slave_driver->major = PTY_SLAVE_MAJOR;
  374. pty_slave_driver->minor_start = 0;
  375. pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
  376. pty_slave_driver->subtype = PTY_TYPE_SLAVE;
  377. pty_slave_driver->init_termios = tty_std_termios;
  378. pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  379. pty_slave_driver->init_termios.c_ispeed = 38400;
  380. pty_slave_driver->init_termios.c_ospeed = 38400;
  381. pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
  382. TTY_DRIVER_REAL_RAW;
  383. pty_slave_driver->other = pty_driver;
  384. tty_set_operations(pty_slave_driver, &pty_ops);
  385. if (tty_register_driver(pty_driver))
  386. panic("Couldn't register pty driver");
  387. if (tty_register_driver(pty_slave_driver))
  388. panic("Couldn't register pty slave driver");
  389. }
  390. #else
  391. static inline void legacy_pty_init(void) { }
  392. #endif
  393. /* Unix98 devices */
  394. #ifdef CONFIG_UNIX98_PTYS
  395. /*
  396. * sysctl support for setting limits on the number of Unix98 ptys allocated.
  397. * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
  398. */
  399. int pty_limit = NR_UNIX98_PTY_DEFAULT;
  400. static int pty_limit_min;
  401. static int pty_limit_max = NR_UNIX98_PTY_MAX;
  402. static int pty_count;
  403. static struct cdev ptmx_cdev;
  404. static struct ctl_table pty_table[] = {
  405. {
  406. .ctl_name = PTY_MAX,
  407. .procname = "max",
  408. .maxlen = sizeof(int),
  409. .mode = 0644,
  410. .data = &pty_limit,
  411. .proc_handler = &proc_dointvec_minmax,
  412. .strategy = &sysctl_intvec,
  413. .extra1 = &pty_limit_min,
  414. .extra2 = &pty_limit_max,
  415. }, {
  416. .ctl_name = PTY_NR,
  417. .procname = "nr",
  418. .maxlen = sizeof(int),
  419. .mode = 0444,
  420. .data = &pty_count,
  421. .proc_handler = &proc_dointvec,
  422. }, {
  423. .ctl_name = 0
  424. }
  425. };
  426. static struct ctl_table pty_kern_table[] = {
  427. {
  428. .ctl_name = KERN_PTY,
  429. .procname = "pty",
  430. .mode = 0555,
  431. .child = pty_table,
  432. },
  433. {}
  434. };
  435. static struct ctl_table pty_root_table[] = {
  436. {
  437. .ctl_name = CTL_KERN,
  438. .procname = "kernel",
  439. .mode = 0555,
  440. .child = pty_kern_table,
  441. },
  442. {}
  443. };
  444. static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file,
  445. unsigned int cmd, unsigned long arg)
  446. {
  447. switch (cmd) {
  448. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  449. return pty_set_lock(tty, (int __user *)arg);
  450. case TIOCGPTN: /* Get PT Number */
  451. return put_user(tty->index, (unsigned int __user *)arg);
  452. }
  453. return -ENOIOCTLCMD;
  454. }
  455. /**
  456. * ptm_unix98_lookup - find a pty master
  457. * @driver: ptm driver
  458. * @idx: tty index
  459. *
  460. * Look up a pty master device. Called under the tty_mutex for now.
  461. * This provides our locking.
  462. */
  463. static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
  464. struct inode *ptm_inode, int idx)
  465. {
  466. struct tty_struct *tty = devpts_get_tty(ptm_inode, idx);
  467. if (tty)
  468. tty = tty->link;
  469. return tty;
  470. }
  471. /**
  472. * pts_unix98_lookup - find a pty slave
  473. * @driver: pts driver
  474. * @idx: tty index
  475. *
  476. * Look up a pty master device. Called under the tty_mutex for now.
  477. * This provides our locking.
  478. */
  479. static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
  480. struct inode *pts_inode, int idx)
  481. {
  482. struct tty_struct *tty = devpts_get_tty(pts_inode, idx);
  483. /* Master must be open before slave */
  484. if (!tty)
  485. return ERR_PTR(-EIO);
  486. return tty;
  487. }
  488. static void pty_unix98_shutdown(struct tty_struct *tty)
  489. {
  490. /* We have our own method as we don't use the tty index */
  491. kfree(tty->termios);
  492. }
  493. /* We have no need to install and remove our tty objects as devpts does all
  494. the work for us */
  495. static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
  496. {
  497. struct tty_struct *o_tty;
  498. int idx = tty->index;
  499. o_tty = alloc_tty_struct();
  500. if (!o_tty)
  501. return -ENOMEM;
  502. if (!try_module_get(driver->other->owner)) {
  503. /* This cannot in fact currently happen */
  504. free_tty_struct(o_tty);
  505. return -ENOMEM;
  506. }
  507. initialize_tty_struct(o_tty, driver->other, idx);
  508. tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
  509. if (tty->termios == NULL)
  510. goto free_mem_out;
  511. *tty->termios = driver->init_termios;
  512. tty->termios_locked = tty->termios + 1;
  513. o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
  514. if (o_tty->termios == NULL)
  515. goto free_mem_out;
  516. *o_tty->termios = driver->other->init_termios;
  517. o_tty->termios_locked = o_tty->termios + 1;
  518. tty_driver_kref_get(driver->other);
  519. if (driver->subtype == PTY_TYPE_MASTER)
  520. o_tty->count++;
  521. /* Establish the links in both directions */
  522. tty->link = o_tty;
  523. o_tty->link = tty;
  524. /*
  525. * All structures have been allocated, so now we install them.
  526. * Failures after this point use release_tty to clean up, so
  527. * there's no need to null out the local pointers.
  528. */
  529. tty_driver_kref_get(driver);
  530. tty->count++;
  531. pty_count++;
  532. return 0;
  533. free_mem_out:
  534. kfree(o_tty->termios);
  535. module_put(o_tty->driver->owner);
  536. free_tty_struct(o_tty);
  537. kfree(tty->termios);
  538. return -ENOMEM;
  539. }
  540. static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
  541. {
  542. pty_count--;
  543. }
  544. static const struct tty_operations ptm_unix98_ops = {
  545. .lookup = ptm_unix98_lookup,
  546. .install = pty_unix98_install,
  547. .remove = pty_unix98_remove,
  548. .open = pty_open,
  549. .close = pty_close,
  550. .write = pty_write,
  551. .write_room = pty_write_room,
  552. .flush_buffer = pty_flush_buffer,
  553. .chars_in_buffer = pty_chars_in_buffer,
  554. .unthrottle = pty_unthrottle,
  555. .set_termios = pty_set_termios,
  556. .ioctl = pty_unix98_ioctl,
  557. .shutdown = pty_unix98_shutdown,
  558. .resize = pty_resize
  559. };
  560. static const struct tty_operations pty_unix98_ops = {
  561. .lookup = pts_unix98_lookup,
  562. .install = pty_unix98_install,
  563. .remove = pty_unix98_remove,
  564. .open = pty_open,
  565. .close = pty_close,
  566. .write = pty_write,
  567. .write_room = pty_write_room,
  568. .flush_buffer = pty_flush_buffer,
  569. .chars_in_buffer = pty_chars_in_buffer,
  570. .unthrottle = pty_unthrottle,
  571. .set_termios = pty_set_termios,
  572. .shutdown = pty_unix98_shutdown
  573. };
  574. /**
  575. * ptmx_open - open a unix 98 pty master
  576. * @inode: inode of device file
  577. * @filp: file pointer to tty
  578. *
  579. * Allocate a unix98 pty master device from the ptmx driver.
  580. *
  581. * Locking: tty_mutex protects the init_dev work. tty->count should
  582. * protect the rest.
  583. * allocated_ptys_lock handles the list of free pty numbers
  584. */
  585. static int __ptmx_open(struct inode *inode, struct file *filp)
  586. {
  587. struct tty_struct *tty;
  588. int retval;
  589. int index;
  590. nonseekable_open(inode, filp);
  591. /* find a device that is not in use. */
  592. index = devpts_new_index(inode);
  593. if (index < 0)
  594. return index;
  595. mutex_lock(&tty_mutex);
  596. tty = tty_init_dev(ptm_driver, index, 1);
  597. mutex_unlock(&tty_mutex);
  598. if (IS_ERR(tty)) {
  599. retval = PTR_ERR(tty);
  600. goto out;
  601. }
  602. set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
  603. filp->private_data = tty;
  604. file_move(filp, &tty->tty_files);
  605. retval = devpts_pty_new(inode, tty->link);
  606. if (retval)
  607. goto out1;
  608. retval = ptm_driver->ops->open(tty, filp);
  609. if (!retval)
  610. return 0;
  611. out1:
  612. tty_release_dev(filp);
  613. return retval;
  614. out:
  615. devpts_kill_index(inode, index);
  616. return retval;
  617. }
  618. static int ptmx_open(struct inode *inode, struct file *filp)
  619. {
  620. int ret;
  621. lock_kernel();
  622. ret = __ptmx_open(inode, filp);
  623. unlock_kernel();
  624. return ret;
  625. }
  626. static struct file_operations ptmx_fops;
  627. static void __init unix98_pty_init(void)
  628. {
  629. ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
  630. if (!ptm_driver)
  631. panic("Couldn't allocate Unix98 ptm driver");
  632. pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
  633. if (!pts_driver)
  634. panic("Couldn't allocate Unix98 pts driver");
  635. ptm_driver->owner = THIS_MODULE;
  636. ptm_driver->driver_name = "pty_master";
  637. ptm_driver->name = "ptm";
  638. ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
  639. ptm_driver->minor_start = 0;
  640. ptm_driver->type = TTY_DRIVER_TYPE_PTY;
  641. ptm_driver->subtype = PTY_TYPE_MASTER;
  642. ptm_driver->init_termios = tty_std_termios;
  643. ptm_driver->init_termios.c_iflag = 0;
  644. ptm_driver->init_termios.c_oflag = 0;
  645. ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  646. ptm_driver->init_termios.c_lflag = 0;
  647. ptm_driver->init_termios.c_ispeed = 38400;
  648. ptm_driver->init_termios.c_ospeed = 38400;
  649. ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  650. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
  651. ptm_driver->other = pts_driver;
  652. tty_set_operations(ptm_driver, &ptm_unix98_ops);
  653. pts_driver->owner = THIS_MODULE;
  654. pts_driver->driver_name = "pty_slave";
  655. pts_driver->name = "pts";
  656. pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
  657. pts_driver->minor_start = 0;
  658. pts_driver->type = TTY_DRIVER_TYPE_PTY;
  659. pts_driver->subtype = PTY_TYPE_SLAVE;
  660. pts_driver->init_termios = tty_std_termios;
  661. pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  662. pts_driver->init_termios.c_ispeed = 38400;
  663. pts_driver->init_termios.c_ospeed = 38400;
  664. pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  665. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
  666. pts_driver->other = ptm_driver;
  667. tty_set_operations(pts_driver, &pty_unix98_ops);
  668. if (tty_register_driver(ptm_driver))
  669. panic("Couldn't register Unix98 ptm driver");
  670. if (tty_register_driver(pts_driver))
  671. panic("Couldn't register Unix98 pts driver");
  672. register_sysctl_table(pty_root_table);
  673. /* Now create the /dev/ptmx special device */
  674. tty_default_fops(&ptmx_fops);
  675. ptmx_fops.open = ptmx_open;
  676. cdev_init(&ptmx_cdev, &ptmx_fops);
  677. if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
  678. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
  679. panic("Couldn't register /dev/ptmx driver\n");
  680. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
  681. }
  682. #else
  683. static inline void unix98_pty_init(void) { }
  684. #endif
  685. static int __init pty_init(void)
  686. {
  687. legacy_pty_init();
  688. unix98_pty_init();
  689. return 0;
  690. }
  691. module_init(pty_init);