pty.c 20 KB

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