pty.c 19 KB

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