pty.c 19 KB

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