pty.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. */
  8. #include <linux/module.h>
  9. #include <linux/errno.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/tty.h>
  12. #include <linux/tty_flip.h>
  13. #include <linux/fcntl.h>
  14. #include <linux/sched.h>
  15. #include <linux/string.h>
  16. #include <linux/major.h>
  17. #include <linux/mm.h>
  18. #include <linux/init.h>
  19. #include <linux/device.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/bitops.h>
  22. #include <linux/devpts_fs.h>
  23. #include <linux/slab.h>
  24. #include <linux/mutex.h>
  25. #ifdef CONFIG_UNIX98_PTYS
  26. static struct tty_driver *ptm_driver;
  27. static struct tty_driver *pts_driver;
  28. static DEFINE_MUTEX(devpts_mutex);
  29. #endif
  30. static void pty_close(struct tty_struct *tty, struct file *filp)
  31. {
  32. BUG_ON(!tty);
  33. if (tty->driver->subtype == PTY_TYPE_MASTER)
  34. WARN_ON(tty->count > 1);
  35. else {
  36. if (test_bit(TTY_IO_ERROR, &tty->flags))
  37. return;
  38. if (tty->count > 2)
  39. return;
  40. }
  41. set_bit(TTY_IO_ERROR, &tty->flags);
  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. 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. if (tty->link->driver_data)
  57. devpts_pty_kill(tty->link->driver_data);
  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->port->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->port, buf, c);
  115. /* And shovel */
  116. if (c) {
  117. tty_flip_buffer_push(to->port);
  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. static int pty_get_lock(struct tty_struct *tty, int __user *arg)
  160. {
  161. int locked = test_bit(TTY_PTY_LOCK, &tty->flags);
  162. return put_user(locked, arg);
  163. }
  164. /* Set the packet mode on a pty */
  165. static int pty_set_pktmode(struct tty_struct *tty, int __user *arg)
  166. {
  167. unsigned long flags;
  168. int pktmode;
  169. if (get_user(pktmode, arg))
  170. return -EFAULT;
  171. spin_lock_irqsave(&tty->ctrl_lock, flags);
  172. if (pktmode) {
  173. if (!tty->packet) {
  174. tty->packet = 1;
  175. tty->link->ctrl_status = 0;
  176. }
  177. } else
  178. tty->packet = 0;
  179. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  180. return 0;
  181. }
  182. /* Get the packet mode of a pty */
  183. static int pty_get_pktmode(struct tty_struct *tty, int __user *arg)
  184. {
  185. int pktmode = tty->packet;
  186. return put_user(pktmode, arg);
  187. }
  188. /* Send a signal to the slave */
  189. static int pty_signal(struct tty_struct *tty, int sig)
  190. {
  191. unsigned long flags;
  192. struct pid *pgrp;
  193. if (tty->link) {
  194. spin_lock_irqsave(&tty->link->ctrl_lock, flags);
  195. pgrp = get_pid(tty->link->pgrp);
  196. spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
  197. kill_pgrp(pgrp, sig, 1);
  198. put_pid(pgrp);
  199. }
  200. return 0;
  201. }
  202. static void pty_flush_buffer(struct tty_struct *tty)
  203. {
  204. struct tty_struct *to = tty->link;
  205. unsigned long flags;
  206. if (!to)
  207. return;
  208. /* tty_buffer_flush(to); FIXME */
  209. if (to->packet) {
  210. spin_lock_irqsave(&tty->ctrl_lock, flags);
  211. tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
  212. wake_up_interruptible(&to->read_wait);
  213. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  214. }
  215. }
  216. static int pty_open(struct tty_struct *tty, struct file *filp)
  217. {
  218. int retval = -ENODEV;
  219. if (!tty || !tty->link)
  220. goto out;
  221. set_bit(TTY_IO_ERROR, &tty->flags);
  222. retval = -EIO;
  223. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  224. goto out;
  225. if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
  226. goto out;
  227. if (tty->driver->subtype == PTY_TYPE_SLAVE && tty->link->count != 1)
  228. goto out;
  229. clear_bit(TTY_IO_ERROR, &tty->flags);
  230. clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  231. set_bit(TTY_THROTTLED, &tty->flags);
  232. retval = 0;
  233. out:
  234. return retval;
  235. }
  236. static void pty_set_termios(struct tty_struct *tty,
  237. struct ktermios *old_termios)
  238. {
  239. tty->termios.c_cflag &= ~(CSIZE | PARENB);
  240. tty->termios.c_cflag |= (CS8 | CREAD);
  241. }
  242. /**
  243. * pty_do_resize - resize event
  244. * @tty: tty being resized
  245. * @ws: window size being set.
  246. *
  247. * Update the termios variables and send the necessary signals to
  248. * peform a terminal resize correctly
  249. */
  250. static int pty_resize(struct tty_struct *tty, struct winsize *ws)
  251. {
  252. struct pid *pgrp, *rpgrp;
  253. unsigned long flags;
  254. struct tty_struct *pty = tty->link;
  255. /* For a PTY we need to lock the tty side */
  256. mutex_lock(&tty->termios_mutex);
  257. if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
  258. goto done;
  259. /* Get the PID values and reference them so we can
  260. avoid holding the tty ctrl lock while sending signals.
  261. We need to lock these individually however. */
  262. spin_lock_irqsave(&tty->ctrl_lock, flags);
  263. pgrp = get_pid(tty->pgrp);
  264. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  265. spin_lock_irqsave(&pty->ctrl_lock, flags);
  266. rpgrp = get_pid(pty->pgrp);
  267. spin_unlock_irqrestore(&pty->ctrl_lock, flags);
  268. if (pgrp)
  269. kill_pgrp(pgrp, SIGWINCH, 1);
  270. if (rpgrp != pgrp && rpgrp)
  271. kill_pgrp(rpgrp, SIGWINCH, 1);
  272. put_pid(pgrp);
  273. put_pid(rpgrp);
  274. tty->winsize = *ws;
  275. pty->winsize = *ws; /* Never used so will go away soon */
  276. done:
  277. mutex_unlock(&tty->termios_mutex);
  278. return 0;
  279. }
  280. /**
  281. * pty_common_install - set up the pty pair
  282. * @driver: the pty driver
  283. * @tty: the tty being instantiated
  284. * @bool: legacy, true if this is BSD style
  285. *
  286. * Perform the initial set up for the tty/pty pair. Called from the
  287. * tty layer when the port is first opened.
  288. *
  289. * Locking: the caller must hold the tty_mutex
  290. */
  291. static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
  292. bool legacy)
  293. {
  294. struct tty_struct *o_tty;
  295. struct tty_port *ports[2];
  296. int idx = tty->index;
  297. int retval = -ENOMEM;
  298. o_tty = alloc_tty_struct();
  299. if (!o_tty)
  300. goto err;
  301. ports[0] = kmalloc(sizeof **ports, GFP_KERNEL);
  302. ports[1] = kmalloc(sizeof **ports, GFP_KERNEL);
  303. if (!ports[0] || !ports[1])
  304. goto err_free_tty;
  305. if (!try_module_get(driver->other->owner)) {
  306. /* This cannot in fact currently happen */
  307. goto err_free_tty;
  308. }
  309. initialize_tty_struct(o_tty, driver->other, idx);
  310. if (legacy) {
  311. /* We always use new tty termios data so we can do this
  312. the easy way .. */
  313. retval = tty_init_termios(tty);
  314. if (retval)
  315. goto err_deinit_tty;
  316. retval = tty_init_termios(o_tty);
  317. if (retval)
  318. goto err_free_termios;
  319. driver->other->ttys[idx] = o_tty;
  320. driver->ttys[idx] = tty;
  321. } else {
  322. memset(&tty->termios_locked, 0, sizeof(tty->termios_locked));
  323. tty->termios = driver->init_termios;
  324. memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked));
  325. o_tty->termios = driver->other->init_termios;
  326. }
  327. /*
  328. * Everything allocated ... set up the o_tty structure.
  329. */
  330. tty_driver_kref_get(driver->other);
  331. if (driver->subtype == PTY_TYPE_MASTER)
  332. o_tty->count++;
  333. /* Establish the links in both directions */
  334. tty->link = o_tty;
  335. o_tty->link = tty;
  336. tty_port_init(ports[0]);
  337. tty_port_init(ports[1]);
  338. o_tty->port = ports[0];
  339. tty->port = ports[1];
  340. o_tty->port->itty = o_tty;
  341. tty_driver_kref_get(driver);
  342. tty->count++;
  343. return 0;
  344. err_free_termios:
  345. if (legacy)
  346. tty_free_termios(tty);
  347. err_deinit_tty:
  348. deinitialize_tty_struct(o_tty);
  349. module_put(o_tty->driver->owner);
  350. err_free_tty:
  351. kfree(ports[0]);
  352. kfree(ports[1]);
  353. free_tty_struct(o_tty);
  354. err:
  355. return retval;
  356. }
  357. /* this is called once with whichever end is closed last */
  358. static void pty_unix98_shutdown(struct tty_struct *tty)
  359. {
  360. devpts_kill_index(tty->driver_data, tty->index);
  361. }
  362. static void pty_cleanup(struct tty_struct *tty)
  363. {
  364. tty->port->itty = NULL;
  365. tty_port_put(tty->port);
  366. }
  367. /* Traditional BSD devices */
  368. #ifdef CONFIG_LEGACY_PTYS
  369. static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
  370. {
  371. return pty_common_install(driver, tty, true);
  372. }
  373. static void pty_remove(struct tty_driver *driver, struct tty_struct *tty)
  374. {
  375. struct tty_struct *pair = tty->link;
  376. driver->ttys[tty->index] = NULL;
  377. if (pair)
  378. pair->driver->ttys[pair->index] = NULL;
  379. }
  380. static int pty_bsd_ioctl(struct tty_struct *tty,
  381. unsigned int cmd, unsigned long arg)
  382. {
  383. switch (cmd) {
  384. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  385. return pty_set_lock(tty, (int __user *) arg);
  386. case TIOCGPTLCK: /* Get PT Lock status */
  387. return pty_get_lock(tty, (int __user *)arg);
  388. case TIOCPKT: /* Set PT packet mode */
  389. return pty_set_pktmode(tty, (int __user *)arg);
  390. case TIOCGPKT: /* Get PT packet mode */
  391. return pty_get_pktmode(tty, (int __user *)arg);
  392. case TIOCSIG: /* Send signal to other side of pty */
  393. return pty_signal(tty, (int) arg);
  394. case TIOCGPTN: /* TTY returns ENOTTY, but glibc expects EINVAL here */
  395. return -EINVAL;
  396. }
  397. return -ENOIOCTLCMD;
  398. }
  399. static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
  400. module_param(legacy_count, int, 0);
  401. /*
  402. * The master side of a pty can do TIOCSPTLCK and thus
  403. * has pty_bsd_ioctl.
  404. */
  405. static const struct tty_operations master_pty_ops_bsd = {
  406. .install = pty_install,
  407. .open = pty_open,
  408. .close = pty_close,
  409. .write = pty_write,
  410. .write_room = pty_write_room,
  411. .flush_buffer = pty_flush_buffer,
  412. .chars_in_buffer = pty_chars_in_buffer,
  413. .unthrottle = pty_unthrottle,
  414. .set_termios = pty_set_termios,
  415. .ioctl = pty_bsd_ioctl,
  416. .cleanup = pty_cleanup,
  417. .resize = pty_resize,
  418. .remove = pty_remove
  419. };
  420. static const struct tty_operations slave_pty_ops_bsd = {
  421. .install = pty_install,
  422. .open = pty_open,
  423. .close = pty_close,
  424. .write = pty_write,
  425. .write_room = pty_write_room,
  426. .flush_buffer = pty_flush_buffer,
  427. .chars_in_buffer = pty_chars_in_buffer,
  428. .unthrottle = pty_unthrottle,
  429. .set_termios = pty_set_termios,
  430. .cleanup = pty_cleanup,
  431. .resize = pty_resize,
  432. .remove = pty_remove
  433. };
  434. static void __init legacy_pty_init(void)
  435. {
  436. struct tty_driver *pty_driver, *pty_slave_driver;
  437. if (legacy_count <= 0)
  438. return;
  439. pty_driver = tty_alloc_driver(legacy_count,
  440. TTY_DRIVER_RESET_TERMIOS |
  441. TTY_DRIVER_REAL_RAW |
  442. TTY_DRIVER_DYNAMIC_ALLOC);
  443. if (IS_ERR(pty_driver))
  444. panic("Couldn't allocate pty driver");
  445. pty_slave_driver = tty_alloc_driver(legacy_count,
  446. TTY_DRIVER_RESET_TERMIOS |
  447. TTY_DRIVER_REAL_RAW |
  448. TTY_DRIVER_DYNAMIC_ALLOC);
  449. if (IS_ERR(pty_slave_driver))
  450. panic("Couldn't allocate pty slave driver");
  451. pty_driver->driver_name = "pty_master";
  452. pty_driver->name = "pty";
  453. pty_driver->major = PTY_MASTER_MAJOR;
  454. pty_driver->minor_start = 0;
  455. pty_driver->type = TTY_DRIVER_TYPE_PTY;
  456. pty_driver->subtype = PTY_TYPE_MASTER;
  457. pty_driver->init_termios = tty_std_termios;
  458. pty_driver->init_termios.c_iflag = 0;
  459. pty_driver->init_termios.c_oflag = 0;
  460. pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  461. pty_driver->init_termios.c_lflag = 0;
  462. pty_driver->init_termios.c_ispeed = 38400;
  463. pty_driver->init_termios.c_ospeed = 38400;
  464. pty_driver->other = pty_slave_driver;
  465. tty_set_operations(pty_driver, &master_pty_ops_bsd);
  466. pty_slave_driver->driver_name = "pty_slave";
  467. pty_slave_driver->name = "ttyp";
  468. pty_slave_driver->major = PTY_SLAVE_MAJOR;
  469. pty_slave_driver->minor_start = 0;
  470. pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
  471. pty_slave_driver->subtype = PTY_TYPE_SLAVE;
  472. pty_slave_driver->init_termios = tty_std_termios;
  473. pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  474. pty_slave_driver->init_termios.c_ispeed = 38400;
  475. pty_slave_driver->init_termios.c_ospeed = 38400;
  476. pty_slave_driver->other = pty_driver;
  477. tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
  478. if (tty_register_driver(pty_driver))
  479. panic("Couldn't register pty driver");
  480. if (tty_register_driver(pty_slave_driver))
  481. panic("Couldn't register pty slave driver");
  482. }
  483. #else
  484. static inline void legacy_pty_init(void) { }
  485. #endif
  486. /* Unix98 devices */
  487. #ifdef CONFIG_UNIX98_PTYS
  488. static struct cdev ptmx_cdev;
  489. static int pty_unix98_ioctl(struct tty_struct *tty,
  490. unsigned int cmd, unsigned long arg)
  491. {
  492. switch (cmd) {
  493. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  494. return pty_set_lock(tty, (int __user *)arg);
  495. case TIOCGPTLCK: /* Get PT Lock status */
  496. return pty_get_lock(tty, (int __user *)arg);
  497. case TIOCPKT: /* Set PT packet mode */
  498. return pty_set_pktmode(tty, (int __user *)arg);
  499. case TIOCGPKT: /* Get PT packet mode */
  500. return pty_get_pktmode(tty, (int __user *)arg);
  501. case TIOCGPTN: /* Get PT Number */
  502. return put_user(tty->index, (unsigned int __user *)arg);
  503. case TIOCSIG: /* Send signal to other side of pty */
  504. return pty_signal(tty, (int) arg);
  505. }
  506. return -ENOIOCTLCMD;
  507. }
  508. /**
  509. * ptm_unix98_lookup - find a pty master
  510. * @driver: ptm driver
  511. * @idx: tty index
  512. *
  513. * Look up a pty master device. Called under the tty_mutex for now.
  514. * This provides our locking.
  515. */
  516. static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
  517. struct inode *ptm_inode, int idx)
  518. {
  519. /* Master must be open via /dev/ptmx */
  520. return ERR_PTR(-EIO);
  521. }
  522. /**
  523. * pts_unix98_lookup - find a pty slave
  524. * @driver: pts driver
  525. * @idx: tty index
  526. *
  527. * Look up a pty master device. Called under the tty_mutex for now.
  528. * This provides our locking for the tty pointer.
  529. */
  530. static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
  531. struct inode *pts_inode, int idx)
  532. {
  533. struct tty_struct *tty;
  534. mutex_lock(&devpts_mutex);
  535. tty = devpts_get_priv(pts_inode);
  536. mutex_unlock(&devpts_mutex);
  537. /* Master must be open before slave */
  538. if (!tty)
  539. return ERR_PTR(-EIO);
  540. return tty;
  541. }
  542. /* We have no need to install and remove our tty objects as devpts does all
  543. the work for us */
  544. static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
  545. {
  546. return pty_common_install(driver, tty, false);
  547. }
  548. static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
  549. {
  550. }
  551. static const struct tty_operations ptm_unix98_ops = {
  552. .lookup = ptm_unix98_lookup,
  553. .install = pty_unix98_install,
  554. .remove = pty_unix98_remove,
  555. .open = pty_open,
  556. .close = pty_close,
  557. .write = pty_write,
  558. .write_room = pty_write_room,
  559. .flush_buffer = pty_flush_buffer,
  560. .chars_in_buffer = pty_chars_in_buffer,
  561. .unthrottle = pty_unthrottle,
  562. .set_termios = pty_set_termios,
  563. .ioctl = pty_unix98_ioctl,
  564. .resize = pty_resize,
  565. .shutdown = pty_unix98_shutdown,
  566. .cleanup = pty_cleanup
  567. };
  568. static const struct tty_operations pty_unix98_ops = {
  569. .lookup = pts_unix98_lookup,
  570. .install = pty_unix98_install,
  571. .remove = pty_unix98_remove,
  572. .open = pty_open,
  573. .close = pty_close,
  574. .write = pty_write,
  575. .write_room = pty_write_room,
  576. .flush_buffer = pty_flush_buffer,
  577. .chars_in_buffer = pty_chars_in_buffer,
  578. .unthrottle = pty_unthrottle,
  579. .set_termios = pty_set_termios,
  580. .shutdown = pty_unix98_shutdown,
  581. .cleanup = pty_cleanup,
  582. };
  583. /**
  584. * ptmx_open - open a unix 98 pty master
  585. * @inode: inode of device file
  586. * @filp: file pointer to tty
  587. *
  588. * Allocate a unix98 pty master device from the ptmx driver.
  589. *
  590. * Locking: tty_mutex protects the init_dev work. tty->count should
  591. * protect the rest.
  592. * allocated_ptys_lock handles the list of free pty numbers
  593. */
  594. static int ptmx_open(struct inode *inode, struct file *filp)
  595. {
  596. struct tty_struct *tty;
  597. struct inode *slave_inode;
  598. int retval;
  599. int index;
  600. nonseekable_open(inode, filp);
  601. retval = tty_alloc_file(filp);
  602. if (retval)
  603. return retval;
  604. /* find a device that is not in use. */
  605. mutex_lock(&devpts_mutex);
  606. index = devpts_new_index(inode);
  607. if (index < 0) {
  608. retval = index;
  609. mutex_unlock(&devpts_mutex);
  610. goto err_file;
  611. }
  612. mutex_unlock(&devpts_mutex);
  613. mutex_lock(&tty_mutex);
  614. tty = tty_init_dev(ptm_driver, index);
  615. if (IS_ERR(tty)) {
  616. retval = PTR_ERR(tty);
  617. goto out;
  618. }
  619. /* The tty returned here is locked so we can safely
  620. drop the mutex */
  621. mutex_unlock(&tty_mutex);
  622. set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
  623. tty->driver_data = inode;
  624. tty_add_file(tty, filp);
  625. slave_inode = devpts_pty_new(inode,
  626. MKDEV(UNIX98_PTY_SLAVE_MAJOR, index), index,
  627. tty->link);
  628. if (IS_ERR(slave_inode)) {
  629. retval = PTR_ERR(slave_inode);
  630. goto err_release;
  631. }
  632. tty->link->driver_data = slave_inode;
  633. retval = ptm_driver->ops->open(tty, filp);
  634. if (retval)
  635. goto err_release;
  636. tty_unlock(tty);
  637. return 0;
  638. err_release:
  639. tty_unlock(tty);
  640. tty_release(inode, filp);
  641. return retval;
  642. out:
  643. mutex_unlock(&tty_mutex);
  644. devpts_kill_index(inode, index);
  645. err_file:
  646. tty_free_file(filp);
  647. return retval;
  648. }
  649. static struct file_operations ptmx_fops;
  650. static void __init unix98_pty_init(void)
  651. {
  652. ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
  653. TTY_DRIVER_RESET_TERMIOS |
  654. TTY_DRIVER_REAL_RAW |
  655. TTY_DRIVER_DYNAMIC_DEV |
  656. TTY_DRIVER_DEVPTS_MEM |
  657. TTY_DRIVER_DYNAMIC_ALLOC);
  658. if (IS_ERR(ptm_driver))
  659. panic("Couldn't allocate Unix98 ptm driver");
  660. pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
  661. TTY_DRIVER_RESET_TERMIOS |
  662. TTY_DRIVER_REAL_RAW |
  663. TTY_DRIVER_DYNAMIC_DEV |
  664. TTY_DRIVER_DEVPTS_MEM |
  665. TTY_DRIVER_DYNAMIC_ALLOC);
  666. if (IS_ERR(pts_driver))
  667. panic("Couldn't allocate Unix98 pts driver");
  668. ptm_driver->driver_name = "pty_master";
  669. ptm_driver->name = "ptm";
  670. ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
  671. ptm_driver->minor_start = 0;
  672. ptm_driver->type = TTY_DRIVER_TYPE_PTY;
  673. ptm_driver->subtype = PTY_TYPE_MASTER;
  674. ptm_driver->init_termios = tty_std_termios;
  675. ptm_driver->init_termios.c_iflag = 0;
  676. ptm_driver->init_termios.c_oflag = 0;
  677. ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  678. ptm_driver->init_termios.c_lflag = 0;
  679. ptm_driver->init_termios.c_ispeed = 38400;
  680. ptm_driver->init_termios.c_ospeed = 38400;
  681. ptm_driver->other = pts_driver;
  682. tty_set_operations(ptm_driver, &ptm_unix98_ops);
  683. pts_driver->driver_name = "pty_slave";
  684. pts_driver->name = "pts";
  685. pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
  686. pts_driver->minor_start = 0;
  687. pts_driver->type = TTY_DRIVER_TYPE_PTY;
  688. pts_driver->subtype = PTY_TYPE_SLAVE;
  689. pts_driver->init_termios = tty_std_termios;
  690. pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  691. pts_driver->init_termios.c_ispeed = 38400;
  692. pts_driver->init_termios.c_ospeed = 38400;
  693. pts_driver->other = ptm_driver;
  694. tty_set_operations(pts_driver, &pty_unix98_ops);
  695. if (tty_register_driver(ptm_driver))
  696. panic("Couldn't register Unix98 ptm driver");
  697. if (tty_register_driver(pts_driver))
  698. panic("Couldn't register Unix98 pts driver");
  699. /* Now create the /dev/ptmx special device */
  700. tty_default_fops(&ptmx_fops);
  701. ptmx_fops.open = ptmx_open;
  702. cdev_init(&ptmx_cdev, &ptmx_fops);
  703. if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
  704. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
  705. panic("Couldn't register /dev/ptmx driver");
  706. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
  707. }
  708. #else
  709. static inline void unix98_pty_init(void) { }
  710. #endif
  711. static int __init pty_init(void)
  712. {
  713. legacy_pty_init();
  714. unix98_pty_init();
  715. return 0;
  716. }
  717. module_init(pty_init);