pty.c 20 KB

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