pty.c 20 KB

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