pty.c 19 KB

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