pty.c 20 KB

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