pty.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /*
  2. * linux/drivers/char/pty.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * Added support for a Unix98-style ptmx device.
  7. * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
  8. *
  9. * When reading this code see also fs/devpts. In particular note that the
  10. * driver_data field is used by the devpts side as a binding to the devpts
  11. * inode.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/errno.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/tty.h>
  17. #include <linux/tty_flip.h>
  18. #include <linux/fcntl.h>
  19. #include <linux/sched.h>
  20. #include <linux/string.h>
  21. #include <linux/major.h>
  22. #include <linux/mm.h>
  23. #include <linux/init.h>
  24. #include <linux/smp_lock.h>
  25. #include <linux/sysctl.h>
  26. #include <linux/device.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/bitops.h>
  29. #include <linux/devpts_fs.h>
  30. #include <linux/slab.h>
  31. #include <asm/system.h>
  32. #ifdef CONFIG_UNIX98_PTYS
  33. static struct tty_driver *ptm_driver;
  34. static struct tty_driver *pts_driver;
  35. #endif
  36. static void pty_close(struct tty_struct *tty, struct file *filp)
  37. {
  38. BUG_ON(!tty);
  39. if (tty->driver->subtype == PTY_TYPE_MASTER)
  40. WARN_ON(tty->count > 1);
  41. else {
  42. if (tty->count > 2)
  43. return;
  44. }
  45. wake_up_interruptible(&tty->read_wait);
  46. wake_up_interruptible(&tty->write_wait);
  47. tty->packet = 0;
  48. if (!tty->link)
  49. return;
  50. tty->link->packet = 0;
  51. set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  52. wake_up_interruptible(&tty->link->read_wait);
  53. wake_up_interruptible(&tty->link->write_wait);
  54. if (tty->driver->subtype == PTY_TYPE_MASTER) {
  55. set_bit(TTY_OTHER_CLOSED, &tty->flags);
  56. #ifdef CONFIG_UNIX98_PTYS
  57. if (tty->driver == ptm_driver)
  58. devpts_pty_kill(tty->link);
  59. #endif
  60. tty_unlock();
  61. tty_vhangup(tty->link);
  62. tty_lock();
  63. }
  64. }
  65. /*
  66. * The unthrottle routine is called by the line discipline to signal
  67. * that it can receive more characters. For PTY's, the TTY_THROTTLED
  68. * flag is always set, to force the line discipline to always call the
  69. * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
  70. * characters in the queue. This is necessary since each time this
  71. * happens, we need to wake up any sleeping processes that could be
  72. * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
  73. * for the pty buffer to be drained.
  74. */
  75. static void pty_unthrottle(struct tty_struct *tty)
  76. {
  77. tty_wakeup(tty->link);
  78. set_bit(TTY_THROTTLED, &tty->flags);
  79. }
  80. /**
  81. * pty_space - report space left for writing
  82. * @to: tty we are writing into
  83. *
  84. * The tty buffers allow 64K but we sneak a peak and clip at 8K this
  85. * allows a lot of overspill room for echo and other fun messes to
  86. * be handled properly
  87. */
  88. static int pty_space(struct tty_struct *to)
  89. {
  90. int n = 8192 - to->buf.memory_used;
  91. if (n < 0)
  92. return 0;
  93. return n;
  94. }
  95. /**
  96. * pty_write - write to a pty
  97. * @tty: the tty we write from
  98. * @buf: kernel buffer of data
  99. * @count: bytes to write
  100. *
  101. * Our "hardware" write method. Data is coming from the ldisc which
  102. * may be in a non sleeping state. We simply throw this at the other
  103. * end of the link as if we were an IRQ handler receiving stuff for
  104. * the other side of the pty/tty pair.
  105. */
  106. static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
  107. {
  108. struct tty_struct *to = tty->link;
  109. if (tty->stopped)
  110. return 0;
  111. if (c > 0) {
  112. /* Stuff the data into the input queue of the other end */
  113. c = tty_insert_flip_string(to, buf, c);
  114. /* And shovel */
  115. if (c) {
  116. tty_flip_buffer_push(to);
  117. tty_wakeup(tty);
  118. }
  119. }
  120. return c;
  121. }
  122. /**
  123. * pty_write_room - write space
  124. * @tty: tty we are writing from
  125. *
  126. * Report how many bytes the ldisc can send into the queue for
  127. * the other device.
  128. */
  129. static int pty_write_room(struct tty_struct *tty)
  130. {
  131. if (tty->stopped)
  132. return 0;
  133. return pty_space(tty->link);
  134. }
  135. /**
  136. * pty_chars_in_buffer - characters currently in our tx queue
  137. * @tty: our tty
  138. *
  139. * Report how much we have in the transmit queue. As everything is
  140. * instantly at the other end this is easy to implement.
  141. */
  142. static int pty_chars_in_buffer(struct tty_struct *tty)
  143. {
  144. return 0;
  145. }
  146. /* Set the lock flag on a pty */
  147. static int pty_set_lock(struct tty_struct *tty, int __user *arg)
  148. {
  149. int val;
  150. if (get_user(val, arg))
  151. return -EFAULT;
  152. if (val)
  153. set_bit(TTY_PTY_LOCK, &tty->flags);
  154. else
  155. clear_bit(TTY_PTY_LOCK, &tty->flags);
  156. return 0;
  157. }
  158. /* Send a signal to the slave */
  159. static int pty_signal(struct tty_struct *tty, int sig)
  160. {
  161. unsigned long flags;
  162. struct pid *pgrp;
  163. if (tty->link) {
  164. spin_lock_irqsave(&tty->link->ctrl_lock, flags);
  165. pgrp = get_pid(tty->link->pgrp);
  166. spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
  167. kill_pgrp(pgrp, sig, 1);
  168. put_pid(pgrp);
  169. }
  170. return 0;
  171. }
  172. static void pty_flush_buffer(struct tty_struct *tty)
  173. {
  174. struct tty_struct *to = tty->link;
  175. unsigned long flags;
  176. if (!to)
  177. return;
  178. /* tty_buffer_flush(to); FIXME */
  179. if (to->packet) {
  180. spin_lock_irqsave(&tty->ctrl_lock, flags);
  181. tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
  182. wake_up_interruptible(&to->read_wait);
  183. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  184. }
  185. }
  186. static int pty_open(struct tty_struct *tty, struct file *filp)
  187. {
  188. int retval = -ENODEV;
  189. if (!tty || !tty->link)
  190. goto out;
  191. retval = -EIO;
  192. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  193. goto out;
  194. if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
  195. goto out;
  196. if (tty->link->count != 1)
  197. goto out;
  198. clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  199. set_bit(TTY_THROTTLED, &tty->flags);
  200. retval = 0;
  201. out:
  202. return retval;
  203. }
  204. static void pty_set_termios(struct tty_struct *tty,
  205. struct ktermios *old_termios)
  206. {
  207. tty->termios->c_cflag &= ~(CSIZE | PARENB);
  208. tty->termios->c_cflag |= (CS8 | CREAD);
  209. }
  210. /**
  211. * pty_do_resize - resize event
  212. * @tty: tty being resized
  213. * @ws: window size being set.
  214. *
  215. * Update the termios variables and send the necessary signals to
  216. * peform a terminal resize correctly
  217. */
  218. int pty_resize(struct tty_struct *tty, struct winsize *ws)
  219. {
  220. struct pid *pgrp, *rpgrp;
  221. unsigned long flags;
  222. struct tty_struct *pty = tty->link;
  223. /* For a PTY we need to lock the tty side */
  224. mutex_lock(&tty->termios_mutex);
  225. if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
  226. goto done;
  227. /* Get the PID values and reference them so we can
  228. avoid holding the tty ctrl lock while sending signals.
  229. We need to lock these individually however. */
  230. spin_lock_irqsave(&tty->ctrl_lock, flags);
  231. pgrp = get_pid(tty->pgrp);
  232. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  233. spin_lock_irqsave(&pty->ctrl_lock, flags);
  234. rpgrp = get_pid(pty->pgrp);
  235. spin_unlock_irqrestore(&pty->ctrl_lock, flags);
  236. if (pgrp)
  237. kill_pgrp(pgrp, SIGWINCH, 1);
  238. if (rpgrp != pgrp && rpgrp)
  239. kill_pgrp(rpgrp, SIGWINCH, 1);
  240. put_pid(pgrp);
  241. put_pid(rpgrp);
  242. tty->winsize = *ws;
  243. pty->winsize = *ws; /* Never used so will go away soon */
  244. done:
  245. mutex_unlock(&tty->termios_mutex);
  246. return 0;
  247. }
  248. /* Traditional BSD devices */
  249. #ifdef CONFIG_LEGACY_PTYS
  250. static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
  251. {
  252. struct tty_struct *o_tty;
  253. int idx = tty->index;
  254. int retval;
  255. o_tty = alloc_tty_struct();
  256. if (!o_tty)
  257. return -ENOMEM;
  258. if (!try_module_get(driver->other->owner)) {
  259. /* This cannot in fact currently happen */
  260. free_tty_struct(o_tty);
  261. return -ENOMEM;
  262. }
  263. initialize_tty_struct(o_tty, driver->other, idx);
  264. /* We always use new tty termios data so we can do this
  265. the easy way .. */
  266. retval = tty_init_termios(tty);
  267. if (retval)
  268. goto free_mem_out;
  269. retval = tty_init_termios(o_tty);
  270. if (retval) {
  271. tty_free_termios(tty);
  272. goto free_mem_out;
  273. }
  274. /*
  275. * Everything allocated ... set up the o_tty structure.
  276. */
  277. driver->other->ttys[idx] = o_tty;
  278. tty_driver_kref_get(driver->other);
  279. if (driver->subtype == PTY_TYPE_MASTER)
  280. o_tty->count++;
  281. /* Establish the links in both directions */
  282. tty->link = o_tty;
  283. o_tty->link = tty;
  284. tty_driver_kref_get(driver);
  285. tty->count++;
  286. driver->ttys[idx] = tty;
  287. return 0;
  288. free_mem_out:
  289. module_put(o_tty->driver->owner);
  290. free_tty_struct(o_tty);
  291. return -ENOMEM;
  292. }
  293. static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file,
  294. unsigned int cmd, unsigned long arg)
  295. {
  296. switch (cmd) {
  297. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  298. return pty_set_lock(tty, (int __user *) arg);
  299. case TIOCSIG: /* Send signal to other side of pty */
  300. return pty_signal(tty, (int) arg);
  301. }
  302. return -ENOIOCTLCMD;
  303. }
  304. static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
  305. module_param(legacy_count, int, 0);
  306. /*
  307. * The master side of a pty can do TIOCSPTLCK and thus
  308. * has pty_bsd_ioctl.
  309. */
  310. static const struct tty_operations master_pty_ops_bsd = {
  311. .install = pty_install,
  312. .open = pty_open,
  313. .close = pty_close,
  314. .write = pty_write,
  315. .write_room = pty_write_room,
  316. .flush_buffer = pty_flush_buffer,
  317. .chars_in_buffer = pty_chars_in_buffer,
  318. .unthrottle = pty_unthrottle,
  319. .set_termios = pty_set_termios,
  320. .ioctl = pty_bsd_ioctl,
  321. .resize = pty_resize
  322. };
  323. static const struct tty_operations slave_pty_ops_bsd = {
  324. .install = pty_install,
  325. .open = pty_open,
  326. .close = pty_close,
  327. .write = pty_write,
  328. .write_room = pty_write_room,
  329. .flush_buffer = pty_flush_buffer,
  330. .chars_in_buffer = pty_chars_in_buffer,
  331. .unthrottle = pty_unthrottle,
  332. .set_termios = pty_set_termios,
  333. .resize = pty_resize
  334. };
  335. static void __init legacy_pty_init(void)
  336. {
  337. struct tty_driver *pty_driver, *pty_slave_driver;
  338. if (legacy_count <= 0)
  339. return;
  340. pty_driver = alloc_tty_driver(legacy_count);
  341. if (!pty_driver)
  342. panic("Couldn't allocate pty driver");
  343. pty_slave_driver = alloc_tty_driver(legacy_count);
  344. if (!pty_slave_driver)
  345. panic("Couldn't allocate pty slave driver");
  346. pty_driver->owner = THIS_MODULE;
  347. pty_driver->driver_name = "pty_master";
  348. pty_driver->name = "pty";
  349. pty_driver->major = PTY_MASTER_MAJOR;
  350. pty_driver->minor_start = 0;
  351. pty_driver->type = TTY_DRIVER_TYPE_PTY;
  352. pty_driver->subtype = PTY_TYPE_MASTER;
  353. pty_driver->init_termios = tty_std_termios;
  354. pty_driver->init_termios.c_iflag = 0;
  355. pty_driver->init_termios.c_oflag = 0;
  356. pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  357. pty_driver->init_termios.c_lflag = 0;
  358. pty_driver->init_termios.c_ispeed = 38400;
  359. pty_driver->init_termios.c_ospeed = 38400;
  360. pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
  361. pty_driver->other = pty_slave_driver;
  362. tty_set_operations(pty_driver, &master_pty_ops_bsd);
  363. pty_slave_driver->owner = THIS_MODULE;
  364. pty_slave_driver->driver_name = "pty_slave";
  365. pty_slave_driver->name = "ttyp";
  366. pty_slave_driver->major = PTY_SLAVE_MAJOR;
  367. pty_slave_driver->minor_start = 0;
  368. pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
  369. pty_slave_driver->subtype = PTY_TYPE_SLAVE;
  370. pty_slave_driver->init_termios = tty_std_termios;
  371. pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  372. pty_slave_driver->init_termios.c_ispeed = 38400;
  373. pty_slave_driver->init_termios.c_ospeed = 38400;
  374. pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
  375. TTY_DRIVER_REAL_RAW;
  376. pty_slave_driver->other = pty_driver;
  377. tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
  378. if (tty_register_driver(pty_driver))
  379. panic("Couldn't register pty driver");
  380. if (tty_register_driver(pty_slave_driver))
  381. panic("Couldn't register pty slave driver");
  382. }
  383. #else
  384. static inline void legacy_pty_init(void) { }
  385. #endif
  386. /* Unix98 devices */
  387. #ifdef CONFIG_UNIX98_PTYS
  388. /*
  389. * sysctl support for setting limits on the number of Unix98 ptys allocated.
  390. * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
  391. */
  392. int pty_limit = NR_UNIX98_PTY_DEFAULT;
  393. static int pty_limit_min;
  394. static int pty_limit_max = NR_UNIX98_PTY_MAX;
  395. static int pty_count;
  396. static struct cdev ptmx_cdev;
  397. static struct ctl_table pty_table[] = {
  398. {
  399. .procname = "max",
  400. .maxlen = sizeof(int),
  401. .mode = 0644,
  402. .data = &pty_limit,
  403. .proc_handler = proc_dointvec_minmax,
  404. .extra1 = &pty_limit_min,
  405. .extra2 = &pty_limit_max,
  406. }, {
  407. .procname = "nr",
  408. .maxlen = sizeof(int),
  409. .mode = 0444,
  410. .data = &pty_count,
  411. .proc_handler = proc_dointvec,
  412. },
  413. {}
  414. };
  415. static struct ctl_table pty_kern_table[] = {
  416. {
  417. .procname = "pty",
  418. .mode = 0555,
  419. .child = pty_table,
  420. },
  421. {}
  422. };
  423. static struct ctl_table pty_root_table[] = {
  424. {
  425. .procname = "kernel",
  426. .mode = 0555,
  427. .child = pty_kern_table,
  428. },
  429. {}
  430. };
  431. static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file,
  432. unsigned int cmd, unsigned long arg)
  433. {
  434. switch (cmd) {
  435. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  436. return pty_set_lock(tty, (int __user *)arg);
  437. case TIOCGPTN: /* Get PT Number */
  438. return put_user(tty->index, (unsigned int __user *)arg);
  439. case TIOCSIG: /* Send signal to other side of pty */
  440. return pty_signal(tty, (int) arg);
  441. }
  442. return -ENOIOCTLCMD;
  443. }
  444. /**
  445. * ptm_unix98_lookup - find a pty master
  446. * @driver: ptm driver
  447. * @idx: tty index
  448. *
  449. * Look up a pty master device. Called under the tty_mutex for now.
  450. * This provides our locking.
  451. */
  452. static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
  453. struct inode *ptm_inode, int idx)
  454. {
  455. struct tty_struct *tty = devpts_get_tty(ptm_inode, idx);
  456. if (tty)
  457. tty = tty->link;
  458. return tty;
  459. }
  460. /**
  461. * pts_unix98_lookup - find a pty slave
  462. * @driver: pts driver
  463. * @idx: tty index
  464. *
  465. * Look up a pty master device. Called under the tty_mutex for now.
  466. * This provides our locking.
  467. */
  468. static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
  469. struct inode *pts_inode, int idx)
  470. {
  471. struct tty_struct *tty = devpts_get_tty(pts_inode, idx);
  472. /* Master must be open before slave */
  473. if (!tty)
  474. return ERR_PTR(-EIO);
  475. return tty;
  476. }
  477. static void pty_unix98_shutdown(struct tty_struct *tty)
  478. {
  479. /* We have our own method as we don't use the tty index */
  480. kfree(tty->termios);
  481. }
  482. /* We have no need to install and remove our tty objects as devpts does all
  483. the work for us */
  484. static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
  485. {
  486. struct tty_struct *o_tty;
  487. int idx = tty->index;
  488. o_tty = alloc_tty_struct();
  489. if (!o_tty)
  490. return -ENOMEM;
  491. if (!try_module_get(driver->other->owner)) {
  492. /* This cannot in fact currently happen */
  493. free_tty_struct(o_tty);
  494. return -ENOMEM;
  495. }
  496. initialize_tty_struct(o_tty, driver->other, idx);
  497. tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
  498. if (tty->termios == NULL)
  499. goto free_mem_out;
  500. *tty->termios = driver->init_termios;
  501. tty->termios_locked = tty->termios + 1;
  502. o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
  503. if (o_tty->termios == NULL)
  504. goto free_mem_out;
  505. *o_tty->termios = driver->other->init_termios;
  506. o_tty->termios_locked = o_tty->termios + 1;
  507. tty_driver_kref_get(driver->other);
  508. if (driver->subtype == PTY_TYPE_MASTER)
  509. o_tty->count++;
  510. /* Establish the links in both directions */
  511. tty->link = o_tty;
  512. o_tty->link = tty;
  513. /*
  514. * All structures have been allocated, so now we install them.
  515. * Failures after this point use release_tty to clean up, so
  516. * there's no need to null out the local pointers.
  517. */
  518. tty_driver_kref_get(driver);
  519. tty->count++;
  520. pty_count++;
  521. return 0;
  522. free_mem_out:
  523. kfree(o_tty->termios);
  524. module_put(o_tty->driver->owner);
  525. free_tty_struct(o_tty);
  526. kfree(tty->termios);
  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);