pty.c 19 KB

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