pty.c 20 KB

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