pty.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. *
  4. * Added support for a Unix98-style ptmx device.
  5. * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
  6. *
  7. * When reading this code see also fs/devpts. In particular note that the
  8. * driver_data field is used by the devpts side as a binding to the devpts
  9. * inode.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/errno.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/tty.h>
  15. #include <linux/tty_flip.h>
  16. #include <linux/fcntl.h>
  17. #include <linux/sched.h>
  18. #include <linux/string.h>
  19. #include <linux/major.h>
  20. #include <linux/mm.h>
  21. #include <linux/init.h>
  22. #include <linux/device.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/bitops.h>
  25. #include <linux/devpts_fs.h>
  26. #include <linux/slab.h>
  27. #ifdef CONFIG_UNIX98_PTYS
  28. static struct tty_driver *ptm_driver;
  29. static struct tty_driver *pts_driver;
  30. #endif
  31. static void pty_close(struct tty_struct *tty, struct file *filp)
  32. {
  33. BUG_ON(!tty);
  34. if (tty->driver->subtype == PTY_TYPE_MASTER)
  35. WARN_ON(tty->count > 1);
  36. else {
  37. if (tty->count > 2)
  38. return;
  39. }
  40. wake_up_interruptible(&tty->read_wait);
  41. wake_up_interruptible(&tty->write_wait);
  42. tty->packet = 0;
  43. if (!tty->link)
  44. return;
  45. set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  46. wake_up_interruptible(&tty->link->read_wait);
  47. wake_up_interruptible(&tty->link->write_wait);
  48. if (tty->driver->subtype == PTY_TYPE_MASTER) {
  49. set_bit(TTY_OTHER_CLOSED, &tty->flags);
  50. #ifdef CONFIG_UNIX98_PTYS
  51. if (tty->driver == ptm_driver)
  52. devpts_pty_kill(tty->link);
  53. #endif
  54. tty_unlock();
  55. tty_vhangup(tty->link);
  56. tty_lock();
  57. }
  58. }
  59. /*
  60. * The unthrottle routine is called by the line discipline to signal
  61. * that it can receive more characters. For PTY's, the TTY_THROTTLED
  62. * flag is always set, to force the line discipline to always call the
  63. * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
  64. * characters in the queue. This is necessary since each time this
  65. * happens, we need to wake up any sleeping processes that could be
  66. * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
  67. * for the pty buffer to be drained.
  68. */
  69. static void pty_unthrottle(struct tty_struct *tty)
  70. {
  71. tty_wakeup(tty->link);
  72. set_bit(TTY_THROTTLED, &tty->flags);
  73. }
  74. /**
  75. * pty_space - report space left for writing
  76. * @to: tty we are writing into
  77. *
  78. * The tty buffers allow 64K but we sneak a peak and clip at 8K this
  79. * allows a lot of overspill room for echo and other fun messes to
  80. * be handled properly
  81. */
  82. static int pty_space(struct tty_struct *to)
  83. {
  84. int n = 8192 - to->buf.memory_used;
  85. if (n < 0)
  86. return 0;
  87. return n;
  88. }
  89. /**
  90. * pty_write - write to a pty
  91. * @tty: the tty we write from
  92. * @buf: kernel buffer of data
  93. * @count: bytes to write
  94. *
  95. * Our "hardware" write method. Data is coming from the ldisc which
  96. * may be in a non sleeping state. We simply throw this at the other
  97. * end of the link as if we were an IRQ handler receiving stuff for
  98. * the other side of the pty/tty pair.
  99. */
  100. static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
  101. {
  102. struct tty_struct *to = tty->link;
  103. if (tty->stopped)
  104. return 0;
  105. if (c > 0) {
  106. /* Stuff the data into the input queue of the other end */
  107. c = tty_insert_flip_string(to, buf, c);
  108. /* And shovel */
  109. if (c) {
  110. tty_flip_buffer_push(to);
  111. tty_wakeup(tty);
  112. }
  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. /* Send a signal to the slave */
  153. static int pty_signal(struct tty_struct *tty, int sig)
  154. {
  155. unsigned long flags;
  156. struct pid *pgrp;
  157. if (sig != SIGINT && sig != SIGQUIT && sig != SIGTSTP)
  158. return -EINVAL;
  159. if (tty->link) {
  160. spin_lock_irqsave(&tty->link->ctrl_lock, flags);
  161. pgrp = get_pid(tty->link->pgrp);
  162. spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
  163. kill_pgrp(pgrp, sig, 1);
  164. put_pid(pgrp);
  165. }
  166. return 0;
  167. }
  168. static void pty_flush_buffer(struct tty_struct *tty)
  169. {
  170. struct tty_struct *to = tty->link;
  171. unsigned long flags;
  172. if (!to)
  173. return;
  174. /* tty_buffer_flush(to); FIXME */
  175. if (to->packet) {
  176. spin_lock_irqsave(&tty->ctrl_lock, flags);
  177. tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
  178. wake_up_interruptible(&to->read_wait);
  179. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  180. }
  181. }
  182. static int pty_open(struct tty_struct *tty, struct file *filp)
  183. {
  184. int retval = -ENODEV;
  185. if (!tty || !tty->link)
  186. goto out;
  187. retval = -EIO;
  188. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  189. goto out;
  190. if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
  191. goto out;
  192. if (tty->link->count != 1)
  193. goto out;
  194. clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  195. set_bit(TTY_THROTTLED, &tty->flags);
  196. retval = 0;
  197. out:
  198. return retval;
  199. }
  200. static void pty_set_termios(struct tty_struct *tty,
  201. struct ktermios *old_termios)
  202. {
  203. tty->termios->c_cflag &= ~(CSIZE | PARENB);
  204. tty->termios->c_cflag |= (CS8 | CREAD);
  205. }
  206. /**
  207. * pty_do_resize - resize event
  208. * @tty: tty being resized
  209. * @ws: window size being set.
  210. *
  211. * Update the termios variables and send the necessary signals to
  212. * peform a terminal resize correctly
  213. */
  214. int pty_resize(struct tty_struct *tty, struct winsize *ws)
  215. {
  216. struct pid *pgrp, *rpgrp;
  217. unsigned long flags;
  218. struct tty_struct *pty = tty->link;
  219. /* For a PTY we need to lock the tty side */
  220. mutex_lock(&tty->termios_mutex);
  221. if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
  222. goto done;
  223. /* Get the PID values and reference them so we can
  224. avoid holding the tty ctrl lock while sending signals.
  225. We need to lock these individually however. */
  226. spin_lock_irqsave(&tty->ctrl_lock, flags);
  227. pgrp = get_pid(tty->pgrp);
  228. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  229. spin_lock_irqsave(&pty->ctrl_lock, flags);
  230. rpgrp = get_pid(pty->pgrp);
  231. spin_unlock_irqrestore(&pty->ctrl_lock, flags);
  232. if (pgrp)
  233. kill_pgrp(pgrp, SIGWINCH, 1);
  234. if (rpgrp != pgrp && rpgrp)
  235. kill_pgrp(rpgrp, SIGWINCH, 1);
  236. put_pid(pgrp);
  237. put_pid(rpgrp);
  238. tty->winsize = *ws;
  239. pty->winsize = *ws; /* Never used so will go away soon */
  240. done:
  241. mutex_unlock(&tty->termios_mutex);
  242. return 0;
  243. }
  244. /* Traditional BSD devices */
  245. #ifdef CONFIG_LEGACY_PTYS
  246. static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
  247. {
  248. struct tty_struct *o_tty;
  249. int idx = tty->index;
  250. int retval;
  251. o_tty = alloc_tty_struct();
  252. if (!o_tty)
  253. return -ENOMEM;
  254. if (!try_module_get(driver->other->owner)) {
  255. /* This cannot in fact currently happen */
  256. retval = -ENOMEM;
  257. goto err_free_tty;
  258. }
  259. initialize_tty_struct(o_tty, driver->other, idx);
  260. /* We always use new tty termios data so we can do this
  261. the easy way .. */
  262. retval = tty_init_termios(tty);
  263. if (retval)
  264. goto err_deinit_tty;
  265. retval = tty_init_termios(o_tty);
  266. if (retval)
  267. goto err_free_termios;
  268. /*
  269. * Everything allocated ... set up the o_tty structure.
  270. */
  271. driver->other->ttys[idx] = o_tty;
  272. tty_driver_kref_get(driver->other);
  273. if (driver->subtype == PTY_TYPE_MASTER)
  274. o_tty->count++;
  275. /* Establish the links in both directions */
  276. tty->link = o_tty;
  277. o_tty->link = tty;
  278. tty_driver_kref_get(driver);
  279. tty->count++;
  280. driver->ttys[idx] = tty;
  281. return 0;
  282. err_free_termios:
  283. tty_free_termios(tty);
  284. err_deinit_tty:
  285. deinitialize_tty_struct(o_tty);
  286. module_put(o_tty->driver->owner);
  287. err_free_tty:
  288. free_tty_struct(o_tty);
  289. return retval;
  290. }
  291. static int pty_bsd_ioctl(struct tty_struct *tty,
  292. unsigned int cmd, unsigned long arg)
  293. {
  294. switch (cmd) {
  295. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  296. return pty_set_lock(tty, (int __user *) arg);
  297. case TIOCSIG: /* Send signal to other side of pty */
  298. return pty_signal(tty, (int) arg);
  299. }
  300. return -ENOIOCTLCMD;
  301. }
  302. static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
  303. module_param(legacy_count, int, 0);
  304. /*
  305. * The master side of a pty can do TIOCSPTLCK and thus
  306. * has pty_bsd_ioctl.
  307. */
  308. static const struct tty_operations master_pty_ops_bsd = {
  309. .install = pty_install,
  310. .open = pty_open,
  311. .close = pty_close,
  312. .write = pty_write,
  313. .write_room = pty_write_room,
  314. .flush_buffer = pty_flush_buffer,
  315. .chars_in_buffer = pty_chars_in_buffer,
  316. .unthrottle = pty_unthrottle,
  317. .set_termios = pty_set_termios,
  318. .ioctl = pty_bsd_ioctl,
  319. .resize = pty_resize
  320. };
  321. static const struct tty_operations slave_pty_ops_bsd = {
  322. .install = pty_install,
  323. .open = pty_open,
  324. .close = pty_close,
  325. .write = pty_write,
  326. .write_room = pty_write_room,
  327. .flush_buffer = pty_flush_buffer,
  328. .chars_in_buffer = pty_chars_in_buffer,
  329. .unthrottle = pty_unthrottle,
  330. .set_termios = pty_set_termios,
  331. .resize = pty_resize
  332. };
  333. static void __init legacy_pty_init(void)
  334. {
  335. struct tty_driver *pty_driver, *pty_slave_driver;
  336. if (legacy_count <= 0)
  337. return;
  338. pty_driver = alloc_tty_driver(legacy_count);
  339. if (!pty_driver)
  340. panic("Couldn't allocate pty driver");
  341. pty_slave_driver = alloc_tty_driver(legacy_count);
  342. if (!pty_slave_driver)
  343. panic("Couldn't allocate pty slave driver");
  344. pty_driver->driver_name = "pty_master";
  345. pty_driver->name = "pty";
  346. pty_driver->major = PTY_MASTER_MAJOR;
  347. pty_driver->minor_start = 0;
  348. pty_driver->type = TTY_DRIVER_TYPE_PTY;
  349. pty_driver->subtype = PTY_TYPE_MASTER;
  350. pty_driver->init_termios = tty_std_termios;
  351. pty_driver->init_termios.c_iflag = 0;
  352. pty_driver->init_termios.c_oflag = 0;
  353. pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  354. pty_driver->init_termios.c_lflag = 0;
  355. pty_driver->init_termios.c_ispeed = 38400;
  356. pty_driver->init_termios.c_ospeed = 38400;
  357. pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
  358. pty_driver->other = pty_slave_driver;
  359. tty_set_operations(pty_driver, &master_pty_ops_bsd);
  360. pty_slave_driver->driver_name = "pty_slave";
  361. pty_slave_driver->name = "ttyp";
  362. pty_slave_driver->major = PTY_SLAVE_MAJOR;
  363. pty_slave_driver->minor_start = 0;
  364. pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
  365. pty_slave_driver->subtype = PTY_TYPE_SLAVE;
  366. pty_slave_driver->init_termios = tty_std_termios;
  367. pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  368. pty_slave_driver->init_termios.c_ispeed = 38400;
  369. pty_slave_driver->init_termios.c_ospeed = 38400;
  370. pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
  371. TTY_DRIVER_REAL_RAW;
  372. pty_slave_driver->other = pty_driver;
  373. tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
  374. if (tty_register_driver(pty_driver))
  375. panic("Couldn't register pty driver");
  376. if (tty_register_driver(pty_slave_driver))
  377. panic("Couldn't register pty slave driver");
  378. }
  379. #else
  380. static inline void legacy_pty_init(void) { }
  381. #endif
  382. /* Unix98 devices */
  383. #ifdef CONFIG_UNIX98_PTYS
  384. static struct cdev ptmx_cdev;
  385. static int pty_unix98_ioctl(struct tty_struct *tty,
  386. unsigned int cmd, unsigned long arg)
  387. {
  388. switch (cmd) {
  389. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  390. return pty_set_lock(tty, (int __user *)arg);
  391. case TIOCGPTN: /* Get PT Number */
  392. return put_user(tty->index, (unsigned int __user *)arg);
  393. case TIOCSIG: /* Send signal to other side of pty */
  394. return pty_signal(tty, (int) arg);
  395. }
  396. return -ENOIOCTLCMD;
  397. }
  398. /**
  399. * ptm_unix98_lookup - find a pty master
  400. * @driver: ptm driver
  401. * @idx: tty index
  402. *
  403. * Look up a pty master device. Called under the tty_mutex for now.
  404. * This provides our locking.
  405. */
  406. static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
  407. struct inode *ptm_inode, int idx)
  408. {
  409. /* Master must be open via /dev/ptmx */
  410. return ERR_PTR(-EIO);
  411. }
  412. /**
  413. * pts_unix98_lookup - find a pty slave
  414. * @driver: pts driver
  415. * @idx: tty index
  416. *
  417. * Look up a pty master device. Called under the tty_mutex for now.
  418. * This provides our locking.
  419. */
  420. static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
  421. struct inode *pts_inode, int idx)
  422. {
  423. struct tty_struct *tty = devpts_get_tty(pts_inode, idx);
  424. /* Master must be open before slave */
  425. if (!tty)
  426. return ERR_PTR(-EIO);
  427. return tty;
  428. }
  429. static void pty_unix98_shutdown(struct tty_struct *tty)
  430. {
  431. tty_driver_remove_tty(tty->driver, tty);
  432. /* We have our own method as we don't use the tty index */
  433. kfree(tty->termios);
  434. }
  435. /* We have no need to install and remove our tty objects as devpts does all
  436. the work for us */
  437. static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
  438. {
  439. struct tty_struct *o_tty;
  440. int idx = tty->index;
  441. o_tty = alloc_tty_struct();
  442. if (!o_tty)
  443. return -ENOMEM;
  444. if (!try_module_get(driver->other->owner)) {
  445. /* This cannot in fact currently happen */
  446. goto err_free_tty;
  447. }
  448. initialize_tty_struct(o_tty, driver->other, idx);
  449. tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
  450. if (tty->termios == NULL)
  451. goto err_free_mem;
  452. *tty->termios = driver->init_termios;
  453. tty->termios_locked = tty->termios + 1;
  454. o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
  455. if (o_tty->termios == NULL)
  456. goto err_free_mem;
  457. *o_tty->termios = driver->other->init_termios;
  458. o_tty->termios_locked = o_tty->termios + 1;
  459. tty_driver_kref_get(driver->other);
  460. if (driver->subtype == PTY_TYPE_MASTER)
  461. o_tty->count++;
  462. /* Establish the links in both directions */
  463. tty->link = o_tty;
  464. o_tty->link = tty;
  465. /*
  466. * All structures have been allocated, so now we install them.
  467. * Failures after this point use release_tty to clean up, so
  468. * there's no need to null out the local pointers.
  469. */
  470. tty_driver_kref_get(driver);
  471. tty->count++;
  472. return 0;
  473. err_free_mem:
  474. deinitialize_tty_struct(o_tty);
  475. kfree(o_tty->termios);
  476. kfree(tty->termios);
  477. module_put(o_tty->driver->owner);
  478. err_free_tty:
  479. free_tty_struct(o_tty);
  480. return -ENOMEM;
  481. }
  482. static void ptm_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
  483. {
  484. }
  485. static void pts_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
  486. {
  487. }
  488. static const struct tty_operations ptm_unix98_ops = {
  489. .lookup = ptm_unix98_lookup,
  490. .install = pty_unix98_install,
  491. .remove = ptm_unix98_remove,
  492. .open = pty_open,
  493. .close = pty_close,
  494. .write = pty_write,
  495. .write_room = pty_write_room,
  496. .flush_buffer = pty_flush_buffer,
  497. .chars_in_buffer = pty_chars_in_buffer,
  498. .unthrottle = pty_unthrottle,
  499. .set_termios = pty_set_termios,
  500. .ioctl = pty_unix98_ioctl,
  501. .shutdown = pty_unix98_shutdown,
  502. .resize = pty_resize
  503. };
  504. static const struct tty_operations pty_unix98_ops = {
  505. .lookup = pts_unix98_lookup,
  506. .install = pty_unix98_install,
  507. .remove = pts_unix98_remove,
  508. .open = pty_open,
  509. .close = pty_close,
  510. .write = pty_write,
  511. .write_room = pty_write_room,
  512. .flush_buffer = pty_flush_buffer,
  513. .chars_in_buffer = pty_chars_in_buffer,
  514. .unthrottle = pty_unthrottle,
  515. .set_termios = pty_set_termios,
  516. .shutdown = pty_unix98_shutdown
  517. };
  518. /**
  519. * ptmx_open - open a unix 98 pty master
  520. * @inode: inode of device file
  521. * @filp: file pointer to tty
  522. *
  523. * Allocate a unix98 pty master device from the ptmx driver.
  524. *
  525. * Locking: tty_mutex protects the init_dev work. tty->count should
  526. * protect the rest.
  527. * allocated_ptys_lock handles the list of free pty numbers
  528. */
  529. static int ptmx_open(struct inode *inode, struct file *filp)
  530. {
  531. struct tty_struct *tty;
  532. int retval;
  533. int index;
  534. nonseekable_open(inode, filp);
  535. /* We refuse fsnotify events on ptmx, since it's a shared resource */
  536. filp->f_mode |= FMODE_NONOTIFY;
  537. retval = tty_alloc_file(filp);
  538. if (retval)
  539. return retval;
  540. /* find a device that is not in use. */
  541. tty_lock();
  542. index = devpts_new_index(inode);
  543. tty_unlock();
  544. if (index < 0) {
  545. retval = index;
  546. goto err_file;
  547. }
  548. mutex_lock(&tty_mutex);
  549. tty_lock();
  550. tty = tty_init_dev(ptm_driver, index);
  551. mutex_unlock(&tty_mutex);
  552. if (IS_ERR(tty)) {
  553. retval = PTR_ERR(tty);
  554. goto out;
  555. }
  556. set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
  557. tty_add_file(tty, filp);
  558. retval = devpts_pty_new(inode, tty->link);
  559. if (retval)
  560. goto err_release;
  561. retval = ptm_driver->ops->open(tty, filp);
  562. if (retval)
  563. goto err_release;
  564. tty_unlock();
  565. return 0;
  566. err_release:
  567. tty_unlock();
  568. tty_release(inode, filp);
  569. return retval;
  570. out:
  571. devpts_kill_index(inode, index);
  572. tty_unlock();
  573. err_file:
  574. tty_free_file(filp);
  575. return retval;
  576. }
  577. static struct file_operations ptmx_fops;
  578. static void __init unix98_pty_init(void)
  579. {
  580. ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
  581. if (!ptm_driver)
  582. panic("Couldn't allocate Unix98 ptm driver");
  583. pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
  584. if (!pts_driver)
  585. panic("Couldn't allocate Unix98 pts driver");
  586. ptm_driver->driver_name = "pty_master";
  587. ptm_driver->name = "ptm";
  588. ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
  589. ptm_driver->minor_start = 0;
  590. ptm_driver->type = TTY_DRIVER_TYPE_PTY;
  591. ptm_driver->subtype = PTY_TYPE_MASTER;
  592. ptm_driver->init_termios = tty_std_termios;
  593. ptm_driver->init_termios.c_iflag = 0;
  594. ptm_driver->init_termios.c_oflag = 0;
  595. ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  596. ptm_driver->init_termios.c_lflag = 0;
  597. ptm_driver->init_termios.c_ispeed = 38400;
  598. ptm_driver->init_termios.c_ospeed = 38400;
  599. ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  600. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
  601. ptm_driver->other = pts_driver;
  602. tty_set_operations(ptm_driver, &ptm_unix98_ops);
  603. pts_driver->driver_name = "pty_slave";
  604. pts_driver->name = "pts";
  605. pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
  606. pts_driver->minor_start = 0;
  607. pts_driver->type = TTY_DRIVER_TYPE_PTY;
  608. pts_driver->subtype = PTY_TYPE_SLAVE;
  609. pts_driver->init_termios = tty_std_termios;
  610. pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  611. pts_driver->init_termios.c_ispeed = 38400;
  612. pts_driver->init_termios.c_ospeed = 38400;
  613. pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  614. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
  615. pts_driver->other = ptm_driver;
  616. tty_set_operations(pts_driver, &pty_unix98_ops);
  617. if (tty_register_driver(ptm_driver))
  618. panic("Couldn't register Unix98 ptm driver");
  619. if (tty_register_driver(pts_driver))
  620. panic("Couldn't register Unix98 pts driver");
  621. /* Now create the /dev/ptmx special device */
  622. tty_default_fops(&ptmx_fops);
  623. ptmx_fops.open = ptmx_open;
  624. cdev_init(&ptmx_cdev, &ptmx_fops);
  625. if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
  626. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
  627. panic("Couldn't register /dev/ptmx driver\n");
  628. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
  629. }
  630. #else
  631. static inline void unix98_pty_init(void) { }
  632. #endif
  633. static int __init pty_init(void)
  634. {
  635. legacy_pty_init();
  636. unix98_pty_init();
  637. return 0;
  638. }
  639. module_init(pty_init);