pty.c 20 KB

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