pty.c 19 KB

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