pty.c 19 KB

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