pty.c 19 KB

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