pty.c 19 KB

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