pty.c 19 KB

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