pty.c 19 KB

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