pty.c 19 KB

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