pty.c 19 KB

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