pty.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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/sysctl.h>
  25. #include <linux/device.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/bitops.h>
  28. #include <linux/devpts_fs.h>
  29. #include <linux/slab.h>
  30. #include <asm/system.h>
  31. #ifdef CONFIG_UNIX98_PTYS
  32. static struct tty_driver *ptm_driver;
  33. static struct tty_driver *pts_driver;
  34. #endif
  35. static void pty_close(struct tty_struct *tty, struct file *filp)
  36. {
  37. BUG_ON(!tty);
  38. if (tty->driver->subtype == PTY_TYPE_MASTER)
  39. WARN_ON(tty->count > 1);
  40. else {
  41. if (tty->count > 2)
  42. return;
  43. }
  44. wake_up_interruptible(&tty->read_wait);
  45. wake_up_interruptible(&tty->write_wait);
  46. tty->packet = 0;
  47. if (!tty->link)
  48. return;
  49. tty->link->packet = 0;
  50. set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  51. wake_up_interruptible(&tty->link->read_wait);
  52. wake_up_interruptible(&tty->link->write_wait);
  53. if (tty->driver->subtype == PTY_TYPE_MASTER) {
  54. set_bit(TTY_OTHER_CLOSED, &tty->flags);
  55. #ifdef CONFIG_UNIX98_PTYS
  56. if (tty->driver == ptm_driver)
  57. devpts_pty_kill(tty->link);
  58. #endif
  59. tty_unlock();
  60. tty_vhangup(tty->link);
  61. tty_lock();
  62. }
  63. }
  64. /*
  65. * The unthrottle routine is called by the line discipline to signal
  66. * that it can receive more characters. For PTY's, the TTY_THROTTLED
  67. * flag is always set, to force the line discipline to always call the
  68. * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
  69. * characters in the queue. This is necessary since each time this
  70. * happens, we need to wake up any sleeping processes that could be
  71. * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
  72. * for the pty buffer to be drained.
  73. */
  74. static void pty_unthrottle(struct tty_struct *tty)
  75. {
  76. tty_wakeup(tty->link);
  77. set_bit(TTY_THROTTLED, &tty->flags);
  78. }
  79. /**
  80. * pty_space - report space left for writing
  81. * @to: tty we are writing into
  82. *
  83. * The tty buffers allow 64K but we sneak a peak and clip at 8K this
  84. * allows a lot of overspill room for echo and other fun messes to
  85. * be handled properly
  86. */
  87. static int pty_space(struct tty_struct *to)
  88. {
  89. int n = 8192 - to->buf.memory_used;
  90. if (n < 0)
  91. return 0;
  92. return n;
  93. }
  94. /**
  95. * pty_write - write to a pty
  96. * @tty: the tty we write from
  97. * @buf: kernel buffer of data
  98. * @count: bytes to write
  99. *
  100. * Our "hardware" write method. Data is coming from the ldisc which
  101. * may be in a non sleeping state. We simply throw this at the other
  102. * end of the link as if we were an IRQ handler receiving stuff for
  103. * the other side of the pty/tty pair.
  104. */
  105. static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
  106. {
  107. struct tty_struct *to = tty->link;
  108. if (tty->stopped)
  109. return 0;
  110. if (c > 0) {
  111. /* Stuff the data into the input queue of the other end */
  112. c = tty_insert_flip_string(to, buf, c);
  113. /* And shovel */
  114. if (c) {
  115. tty_flip_buffer_push(to);
  116. tty_wakeup(tty);
  117. }
  118. }
  119. return c;
  120. }
  121. /**
  122. * pty_write_room - write space
  123. * @tty: tty we are writing from
  124. *
  125. * Report how many bytes the ldisc can send into the queue for
  126. * the other device.
  127. */
  128. static int pty_write_room(struct tty_struct *tty)
  129. {
  130. if (tty->stopped)
  131. return 0;
  132. return pty_space(tty->link);
  133. }
  134. /**
  135. * pty_chars_in_buffer - characters currently in our tx queue
  136. * @tty: our tty
  137. *
  138. * Report how much we have in the transmit queue. As everything is
  139. * instantly at the other end this is easy to implement.
  140. */
  141. static int pty_chars_in_buffer(struct tty_struct *tty)
  142. {
  143. return 0;
  144. }
  145. /* Set the lock flag on a pty */
  146. static int pty_set_lock(struct tty_struct *tty, int __user *arg)
  147. {
  148. int val;
  149. if (get_user(val, arg))
  150. return -EFAULT;
  151. if (val)
  152. set_bit(TTY_PTY_LOCK, &tty->flags);
  153. else
  154. clear_bit(TTY_PTY_LOCK, &tty->flags);
  155. return 0;
  156. }
  157. /* Send a signal to the slave */
  158. static int pty_signal(struct tty_struct *tty, int sig)
  159. {
  160. unsigned long flags;
  161. struct pid *pgrp;
  162. if (tty->link) {
  163. spin_lock_irqsave(&tty->link->ctrl_lock, flags);
  164. pgrp = get_pid(tty->link->pgrp);
  165. spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
  166. kill_pgrp(pgrp, sig, 1);
  167. put_pid(pgrp);
  168. }
  169. return 0;
  170. }
  171. static void pty_flush_buffer(struct tty_struct *tty)
  172. {
  173. struct tty_struct *to = tty->link;
  174. unsigned long flags;
  175. if (!to)
  176. return;
  177. /* tty_buffer_flush(to); FIXME */
  178. if (to->packet) {
  179. spin_lock_irqsave(&tty->ctrl_lock, flags);
  180. tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
  181. wake_up_interruptible(&to->read_wait);
  182. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  183. }
  184. }
  185. static int pty_open(struct tty_struct *tty, struct file *filp)
  186. {
  187. int retval = -ENODEV;
  188. if (!tty || !tty->link)
  189. goto out;
  190. retval = -EIO;
  191. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  192. goto out;
  193. if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
  194. goto out;
  195. if (tty->link->count != 1)
  196. goto out;
  197. clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  198. set_bit(TTY_THROTTLED, &tty->flags);
  199. retval = 0;
  200. out:
  201. return retval;
  202. }
  203. static void pty_set_termios(struct tty_struct *tty,
  204. struct ktermios *old_termios)
  205. {
  206. tty->termios->c_cflag &= ~(CSIZE | PARENB);
  207. tty->termios->c_cflag |= (CS8 | CREAD);
  208. }
  209. /**
  210. * pty_do_resize - resize event
  211. * @tty: tty being resized
  212. * @ws: window size being set.
  213. *
  214. * Update the termios variables and send the necessary signals to
  215. * peform a terminal resize correctly
  216. */
  217. int pty_resize(struct tty_struct *tty, struct winsize *ws)
  218. {
  219. struct pid *pgrp, *rpgrp;
  220. unsigned long flags;
  221. struct tty_struct *pty = tty->link;
  222. /* For a PTY we need to lock the tty side */
  223. mutex_lock(&tty->termios_mutex);
  224. if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
  225. goto done;
  226. /* Get the PID values and reference them so we can
  227. avoid holding the tty ctrl lock while sending signals.
  228. We need to lock these individually however. */
  229. spin_lock_irqsave(&tty->ctrl_lock, flags);
  230. pgrp = get_pid(tty->pgrp);
  231. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  232. spin_lock_irqsave(&pty->ctrl_lock, flags);
  233. rpgrp = get_pid(pty->pgrp);
  234. spin_unlock_irqrestore(&pty->ctrl_lock, flags);
  235. if (pgrp)
  236. kill_pgrp(pgrp, SIGWINCH, 1);
  237. if (rpgrp != pgrp && rpgrp)
  238. kill_pgrp(rpgrp, SIGWINCH, 1);
  239. put_pid(pgrp);
  240. put_pid(rpgrp);
  241. tty->winsize = *ws;
  242. pty->winsize = *ws; /* Never used so will go away soon */
  243. done:
  244. mutex_unlock(&tty->termios_mutex);
  245. return 0;
  246. }
  247. /* Traditional BSD devices */
  248. #ifdef CONFIG_LEGACY_PTYS
  249. static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
  250. {
  251. struct tty_struct *o_tty;
  252. int idx = tty->index;
  253. int retval;
  254. o_tty = alloc_tty_struct();
  255. if (!o_tty)
  256. return -ENOMEM;
  257. if (!try_module_get(driver->other->owner)) {
  258. /* This cannot in fact currently happen */
  259. free_tty_struct(o_tty);
  260. return -ENOMEM;
  261. }
  262. initialize_tty_struct(o_tty, driver->other, idx);
  263. /* We always use new tty termios data so we can do this
  264. the easy way .. */
  265. retval = tty_init_termios(tty);
  266. if (retval)
  267. goto free_mem_out;
  268. retval = tty_init_termios(o_tty);
  269. if (retval) {
  270. tty_free_termios(tty);
  271. goto free_mem_out;
  272. }
  273. /*
  274. * Everything allocated ... set up the o_tty structure.
  275. */
  276. driver->other->ttys[idx] = o_tty;
  277. tty_driver_kref_get(driver->other);
  278. if (driver->subtype == PTY_TYPE_MASTER)
  279. o_tty->count++;
  280. /* Establish the links in both directions */
  281. tty->link = o_tty;
  282. o_tty->link = tty;
  283. tty_driver_kref_get(driver);
  284. tty->count++;
  285. driver->ttys[idx] = tty;
  286. return 0;
  287. free_mem_out:
  288. module_put(o_tty->driver->owner);
  289. free_tty_struct(o_tty);
  290. return -ENOMEM;
  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. free_tty_struct(o_tty);
  493. return -ENOMEM;
  494. }
  495. initialize_tty_struct(o_tty, driver->other, idx);
  496. tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
  497. if (tty->termios == NULL)
  498. goto free_mem_out;
  499. *tty->termios = driver->init_termios;
  500. tty->termios_locked = tty->termios + 1;
  501. o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
  502. if (o_tty->termios == NULL)
  503. goto free_mem_out;
  504. *o_tty->termios = driver->other->init_termios;
  505. o_tty->termios_locked = o_tty->termios + 1;
  506. tty_driver_kref_get(driver->other);
  507. if (driver->subtype == PTY_TYPE_MASTER)
  508. o_tty->count++;
  509. /* Establish the links in both directions */
  510. tty->link = o_tty;
  511. o_tty->link = tty;
  512. /*
  513. * All structures have been allocated, so now we install them.
  514. * Failures after this point use release_tty to clean up, so
  515. * there's no need to null out the local pointers.
  516. */
  517. tty_driver_kref_get(driver);
  518. tty->count++;
  519. pty_count++;
  520. return 0;
  521. free_mem_out:
  522. kfree(o_tty->termios);
  523. module_put(o_tty->driver->owner);
  524. free_tty_struct(o_tty);
  525. kfree(tty->termios);
  526. return -ENOMEM;
  527. }
  528. static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
  529. {
  530. pty_count--;
  531. }
  532. static const struct tty_operations ptm_unix98_ops = {
  533. .lookup = ptm_unix98_lookup,
  534. .install = pty_unix98_install,
  535. .remove = pty_unix98_remove,
  536. .open = pty_open,
  537. .close = pty_close,
  538. .write = pty_write,
  539. .write_room = pty_write_room,
  540. .flush_buffer = pty_flush_buffer,
  541. .chars_in_buffer = pty_chars_in_buffer,
  542. .unthrottle = pty_unthrottle,
  543. .set_termios = pty_set_termios,
  544. .ioctl = pty_unix98_ioctl,
  545. .shutdown = pty_unix98_shutdown,
  546. .resize = pty_resize
  547. };
  548. static const struct tty_operations pty_unix98_ops = {
  549. .lookup = pts_unix98_lookup,
  550. .install = pty_unix98_install,
  551. .remove = pty_unix98_remove,
  552. .open = pty_open,
  553. .close = pty_close,
  554. .write = pty_write,
  555. .write_room = pty_write_room,
  556. .flush_buffer = pty_flush_buffer,
  557. .chars_in_buffer = pty_chars_in_buffer,
  558. .unthrottle = pty_unthrottle,
  559. .set_termios = pty_set_termios,
  560. .shutdown = pty_unix98_shutdown
  561. };
  562. /**
  563. * ptmx_open - open a unix 98 pty master
  564. * @inode: inode of device file
  565. * @filp: file pointer to tty
  566. *
  567. * Allocate a unix98 pty master device from the ptmx driver.
  568. *
  569. * Locking: tty_mutex protects the init_dev work. tty->count should
  570. * protect the rest.
  571. * allocated_ptys_lock handles the list of free pty numbers
  572. */
  573. static int ptmx_open(struct inode *inode, struct file *filp)
  574. {
  575. struct tty_struct *tty;
  576. int retval;
  577. int index;
  578. nonseekable_open(inode, filp);
  579. /* find a device that is not in use. */
  580. tty_lock();
  581. index = devpts_new_index(inode);
  582. tty_unlock();
  583. if (index < 0)
  584. return index;
  585. mutex_lock(&tty_mutex);
  586. tty_lock();
  587. tty = tty_init_dev(ptm_driver, index, 1);
  588. mutex_unlock(&tty_mutex);
  589. if (IS_ERR(tty)) {
  590. retval = PTR_ERR(tty);
  591. goto out;
  592. }
  593. set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
  594. retval = tty_add_file(tty, filp);
  595. if (retval)
  596. goto out;
  597. retval = devpts_pty_new(inode, tty->link);
  598. if (retval)
  599. goto out1;
  600. retval = ptm_driver->ops->open(tty, filp);
  601. if (retval)
  602. goto out2;
  603. out1:
  604. tty_unlock();
  605. return retval;
  606. out2:
  607. tty_unlock();
  608. tty_release(inode, filp);
  609. return retval;
  610. out:
  611. devpts_kill_index(inode, index);
  612. tty_unlock();
  613. return retval;
  614. }
  615. static struct file_operations ptmx_fops;
  616. static void __init unix98_pty_init(void)
  617. {
  618. ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
  619. if (!ptm_driver)
  620. panic("Couldn't allocate Unix98 ptm driver");
  621. pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
  622. if (!pts_driver)
  623. panic("Couldn't allocate Unix98 pts driver");
  624. ptm_driver->owner = THIS_MODULE;
  625. ptm_driver->driver_name = "pty_master";
  626. ptm_driver->name = "ptm";
  627. ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
  628. ptm_driver->minor_start = 0;
  629. ptm_driver->type = TTY_DRIVER_TYPE_PTY;
  630. ptm_driver->subtype = PTY_TYPE_MASTER;
  631. ptm_driver->init_termios = tty_std_termios;
  632. ptm_driver->init_termios.c_iflag = 0;
  633. ptm_driver->init_termios.c_oflag = 0;
  634. ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  635. ptm_driver->init_termios.c_lflag = 0;
  636. ptm_driver->init_termios.c_ispeed = 38400;
  637. ptm_driver->init_termios.c_ospeed = 38400;
  638. ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  639. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
  640. ptm_driver->other = pts_driver;
  641. tty_set_operations(ptm_driver, &ptm_unix98_ops);
  642. pts_driver->owner = THIS_MODULE;
  643. pts_driver->driver_name = "pty_slave";
  644. pts_driver->name = "pts";
  645. pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
  646. pts_driver->minor_start = 0;
  647. pts_driver->type = TTY_DRIVER_TYPE_PTY;
  648. pts_driver->subtype = PTY_TYPE_SLAVE;
  649. pts_driver->init_termios = tty_std_termios;
  650. pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  651. pts_driver->init_termios.c_ispeed = 38400;
  652. pts_driver->init_termios.c_ospeed = 38400;
  653. pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  654. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
  655. pts_driver->other = ptm_driver;
  656. tty_set_operations(pts_driver, &pty_unix98_ops);
  657. if (tty_register_driver(ptm_driver))
  658. panic("Couldn't register Unix98 ptm driver");
  659. if (tty_register_driver(pts_driver))
  660. panic("Couldn't register Unix98 pts driver");
  661. register_sysctl_table(pty_root_table);
  662. /* Now create the /dev/ptmx special device */
  663. tty_default_fops(&ptmx_fops);
  664. ptmx_fops.open = ptmx_open;
  665. cdev_init(&ptmx_cdev, &ptmx_fops);
  666. if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
  667. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
  668. panic("Couldn't register /dev/ptmx driver\n");
  669. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
  670. }
  671. #else
  672. static inline void unix98_pty_init(void) { }
  673. #endif
  674. static int __init pty_init(void)
  675. {
  676. legacy_pty_init();
  677. unix98_pty_init();
  678. return 0;
  679. }
  680. module_init(pty_init);