pty.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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 tty_count;
  395. static int pty_count;
  396. static inline void pty_inc_count(void)
  397. {
  398. pty_count = (++tty_count) / 2;
  399. }
  400. static inline void pty_dec_count(void)
  401. {
  402. pty_count = (--tty_count) / 2;
  403. }
  404. static struct cdev ptmx_cdev;
  405. static struct ctl_table pty_table[] = {
  406. {
  407. .procname = "max",
  408. .maxlen = sizeof(int),
  409. .mode = 0644,
  410. .data = &pty_limit,
  411. .proc_handler = proc_dointvec_minmax,
  412. .extra1 = &pty_limit_min,
  413. .extra2 = &pty_limit_max,
  414. }, {
  415. .procname = "nr",
  416. .maxlen = sizeof(int),
  417. .mode = 0444,
  418. .data = &pty_count,
  419. .proc_handler = proc_dointvec,
  420. },
  421. {}
  422. };
  423. static struct ctl_table pty_kern_table[] = {
  424. {
  425. .procname = "pty",
  426. .mode = 0555,
  427. .child = pty_table,
  428. },
  429. {}
  430. };
  431. static struct ctl_table pty_root_table[] = {
  432. {
  433. .procname = "kernel",
  434. .mode = 0555,
  435. .child = pty_kern_table,
  436. },
  437. {}
  438. };
  439. static int pty_unix98_ioctl(struct tty_struct *tty,
  440. unsigned int cmd, unsigned long arg)
  441. {
  442. switch (cmd) {
  443. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  444. return pty_set_lock(tty, (int __user *)arg);
  445. case TIOCGPTN: /* Get PT Number */
  446. return put_user(tty->index, (unsigned int __user *)arg);
  447. case TIOCSIG: /* Send signal to other side of pty */
  448. return pty_signal(tty, (int) arg);
  449. }
  450. return -ENOIOCTLCMD;
  451. }
  452. /**
  453. * ptm_unix98_lookup - find a pty master
  454. * @driver: ptm driver
  455. * @idx: tty index
  456. *
  457. * Look up a pty master device. Called under the tty_mutex for now.
  458. * This provides our locking.
  459. */
  460. static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
  461. struct inode *ptm_inode, int idx)
  462. {
  463. struct tty_struct *tty = devpts_get_tty(ptm_inode, idx);
  464. if (tty)
  465. tty = tty->link;
  466. return tty;
  467. }
  468. /**
  469. * pts_unix98_lookup - find a pty slave
  470. * @driver: pts driver
  471. * @idx: tty index
  472. *
  473. * Look up a pty master device. Called under the tty_mutex for now.
  474. * This provides our locking.
  475. */
  476. static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
  477. struct inode *pts_inode, int idx)
  478. {
  479. struct tty_struct *tty = devpts_get_tty(pts_inode, idx);
  480. /* Master must be open before slave */
  481. if (!tty)
  482. return ERR_PTR(-EIO);
  483. return tty;
  484. }
  485. static void pty_unix98_shutdown(struct tty_struct *tty)
  486. {
  487. tty_driver_remove_tty(tty->driver, tty);
  488. /* We have our own method as we don't use the tty index */
  489. kfree(tty->termios);
  490. }
  491. /* We have no need to install and remove our tty objects as devpts does all
  492. the work for us */
  493. static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
  494. {
  495. struct tty_struct *o_tty;
  496. int idx = tty->index;
  497. o_tty = alloc_tty_struct();
  498. if (!o_tty)
  499. return -ENOMEM;
  500. if (!try_module_get(driver->other->owner)) {
  501. /* This cannot in fact currently happen */
  502. goto err_free_tty;
  503. }
  504. initialize_tty_struct(o_tty, driver->other, idx);
  505. tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
  506. if (tty->termios == NULL)
  507. goto err_free_mem;
  508. *tty->termios = driver->init_termios;
  509. tty->termios_locked = tty->termios + 1;
  510. o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
  511. if (o_tty->termios == NULL)
  512. goto err_free_mem;
  513. *o_tty->termios = driver->other->init_termios;
  514. o_tty->termios_locked = o_tty->termios + 1;
  515. tty_driver_kref_get(driver->other);
  516. if (driver->subtype == PTY_TYPE_MASTER)
  517. o_tty->count++;
  518. /* Establish the links in both directions */
  519. tty->link = o_tty;
  520. o_tty->link = tty;
  521. /*
  522. * All structures have been allocated, so now we install them.
  523. * Failures after this point use release_tty to clean up, so
  524. * there's no need to null out the local pointers.
  525. */
  526. tty_driver_kref_get(driver);
  527. tty->count++;
  528. pty_inc_count(); /* tty */
  529. pty_inc_count(); /* tty->link */
  530. return 0;
  531. err_free_mem:
  532. deinitialize_tty_struct(o_tty);
  533. kfree(o_tty->termios);
  534. kfree(tty->termios);
  535. module_put(o_tty->driver->owner);
  536. err_free_tty:
  537. free_tty_struct(o_tty);
  538. return -ENOMEM;
  539. }
  540. static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
  541. {
  542. pty_dec_count();
  543. }
  544. static const struct tty_operations ptm_unix98_ops = {
  545. .lookup = ptm_unix98_lookup,
  546. .install = pty_unix98_install,
  547. .remove = pty_unix98_remove,
  548. .open = pty_open,
  549. .close = pty_close,
  550. .write = pty_write,
  551. .write_room = pty_write_room,
  552. .flush_buffer = pty_flush_buffer,
  553. .chars_in_buffer = pty_chars_in_buffer,
  554. .unthrottle = pty_unthrottle,
  555. .set_termios = pty_set_termios,
  556. .ioctl = pty_unix98_ioctl,
  557. .shutdown = pty_unix98_shutdown,
  558. .resize = pty_resize
  559. };
  560. static const struct tty_operations pty_unix98_ops = {
  561. .lookup = pts_unix98_lookup,
  562. .install = pty_unix98_install,
  563. .remove = pty_unix98_remove,
  564. .open = pty_open,
  565. .close = pty_close,
  566. .write = pty_write,
  567. .write_room = pty_write_room,
  568. .flush_buffer = pty_flush_buffer,
  569. .chars_in_buffer = pty_chars_in_buffer,
  570. .unthrottle = pty_unthrottle,
  571. .set_termios = pty_set_termios,
  572. .shutdown = pty_unix98_shutdown
  573. };
  574. /**
  575. * ptmx_open - open a unix 98 pty master
  576. * @inode: inode of device file
  577. * @filp: file pointer to tty
  578. *
  579. * Allocate a unix98 pty master device from the ptmx driver.
  580. *
  581. * Locking: tty_mutex protects the init_dev work. tty->count should
  582. * protect the rest.
  583. * allocated_ptys_lock handles the list of free pty numbers
  584. */
  585. static int ptmx_open(struct inode *inode, struct file *filp)
  586. {
  587. struct tty_struct *tty;
  588. int retval;
  589. int index;
  590. nonseekable_open(inode, filp);
  591. retval = tty_alloc_file(filp);
  592. if (retval)
  593. return retval;
  594. /* find a device that is not in use. */
  595. tty_lock();
  596. index = devpts_new_index(inode);
  597. tty_unlock();
  598. if (index < 0) {
  599. retval = index;
  600. goto err_file;
  601. }
  602. mutex_lock(&tty_mutex);
  603. tty_lock();
  604. tty = tty_init_dev(ptm_driver, index, 1);
  605. mutex_unlock(&tty_mutex);
  606. if (IS_ERR(tty)) {
  607. retval = PTR_ERR(tty);
  608. goto out;
  609. }
  610. set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
  611. tty_add_file(tty, filp);
  612. retval = devpts_pty_new(inode, tty->link);
  613. if (retval)
  614. goto err_release;
  615. retval = ptm_driver->ops->open(tty, filp);
  616. if (retval)
  617. goto err_release;
  618. tty_unlock();
  619. return 0;
  620. err_release:
  621. tty_unlock();
  622. tty_release(inode, filp);
  623. return retval;
  624. out:
  625. devpts_kill_index(inode, index);
  626. tty_unlock();
  627. err_file:
  628. tty_free_file(filp);
  629. return retval;
  630. }
  631. static struct file_operations ptmx_fops;
  632. static void __init unix98_pty_init(void)
  633. {
  634. ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
  635. if (!ptm_driver)
  636. panic("Couldn't allocate Unix98 ptm driver");
  637. pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
  638. if (!pts_driver)
  639. panic("Couldn't allocate Unix98 pts driver");
  640. ptm_driver->owner = THIS_MODULE;
  641. ptm_driver->driver_name = "pty_master";
  642. ptm_driver->name = "ptm";
  643. ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
  644. ptm_driver->minor_start = 0;
  645. ptm_driver->type = TTY_DRIVER_TYPE_PTY;
  646. ptm_driver->subtype = PTY_TYPE_MASTER;
  647. ptm_driver->init_termios = tty_std_termios;
  648. ptm_driver->init_termios.c_iflag = 0;
  649. ptm_driver->init_termios.c_oflag = 0;
  650. ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  651. ptm_driver->init_termios.c_lflag = 0;
  652. ptm_driver->init_termios.c_ispeed = 38400;
  653. ptm_driver->init_termios.c_ospeed = 38400;
  654. ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  655. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
  656. ptm_driver->other = pts_driver;
  657. tty_set_operations(ptm_driver, &ptm_unix98_ops);
  658. pts_driver->owner = THIS_MODULE;
  659. pts_driver->driver_name = "pty_slave";
  660. pts_driver->name = "pts";
  661. pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
  662. pts_driver->minor_start = 0;
  663. pts_driver->type = TTY_DRIVER_TYPE_PTY;
  664. pts_driver->subtype = PTY_TYPE_SLAVE;
  665. pts_driver->init_termios = tty_std_termios;
  666. pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  667. pts_driver->init_termios.c_ispeed = 38400;
  668. pts_driver->init_termios.c_ospeed = 38400;
  669. pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  670. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
  671. pts_driver->other = ptm_driver;
  672. tty_set_operations(pts_driver, &pty_unix98_ops);
  673. if (tty_register_driver(ptm_driver))
  674. panic("Couldn't register Unix98 ptm driver");
  675. if (tty_register_driver(pts_driver))
  676. panic("Couldn't register Unix98 pts driver");
  677. register_sysctl_table(pty_root_table);
  678. /* Now create the /dev/ptmx special device */
  679. tty_default_fops(&ptmx_fops);
  680. ptmx_fops.open = ptmx_open;
  681. cdev_init(&ptmx_cdev, &ptmx_fops);
  682. if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
  683. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
  684. panic("Couldn't register /dev/ptmx driver\n");
  685. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
  686. }
  687. #else
  688. static inline void unix98_pty_init(void) { }
  689. #endif
  690. static int __init pty_init(void)
  691. {
  692. legacy_pty_init();
  693. unix98_pty_init();
  694. return 0;
  695. }
  696. module_init(pty_init);