pty.c 19 KB

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