ef72031ab5b95ddf7af62660acfea79ce52b824d
[muen/linux.git] / drivers / tty / pty.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright (C) 1991, 1992  Linus Torvalds
4  *
5  *  Added support for a Unix98-style ptmx device.
6  *    -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
7  *
8  */
9
10 #include <linux/module.h>
11 #include <linux/errno.h>
12 #include <linux/interrupt.h>
13 #include <linux/tty.h>
14 #include <linux/tty_flip.h>
15 #include <linux/fcntl.h>
16 #include <linux/sched/signal.h>
17 #include <linux/string.h>
18 #include <linux/major.h>
19 #include <linux/mm.h>
20 #include <linux/init.h>
21 #include <linux/device.h>
22 #include <linux/uaccess.h>
23 #include <linux/bitops.h>
24 #include <linux/devpts_fs.h>
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
27 #include <linux/poll.h>
28 #include <linux/mount.h>
29 #include <linux/file.h>
30 #include <linux/ioctl.h>
31 #include <linux/compat.h>
32
33 #undef TTY_DEBUG_HANGUP
34 #ifdef TTY_DEBUG_HANGUP
35 # define tty_debug_hangup(tty, f, args...)      tty_debug(tty, f, ##args)
36 #else
37 # define tty_debug_hangup(tty, f, args...)      do {} while (0)
38 #endif
39
40 #ifdef CONFIG_UNIX98_PTYS
41 static struct tty_driver *ptm_driver;
42 static struct tty_driver *pts_driver;
43 static DEFINE_MUTEX(devpts_mutex);
44 #endif
45
46 static void pty_close(struct tty_struct *tty, struct file *filp)
47 {
48         BUG_ON(!tty);
49         if (tty->driver->subtype == PTY_TYPE_MASTER)
50                 WARN_ON(tty->count > 1);
51         else {
52                 if (tty_io_error(tty))
53                         return;
54                 if (tty->count > 2)
55                         return;
56         }
57         set_bit(TTY_IO_ERROR, &tty->flags);
58         wake_up_interruptible(&tty->read_wait);
59         wake_up_interruptible(&tty->write_wait);
60         spin_lock_irq(&tty->ctrl_lock);
61         tty->packet = 0;
62         spin_unlock_irq(&tty->ctrl_lock);
63         /* Review - krefs on tty_link ?? */
64         if (!tty->link)
65                 return;
66         set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
67         wake_up_interruptible(&tty->link->read_wait);
68         wake_up_interruptible(&tty->link->write_wait);
69         if (tty->driver->subtype == PTY_TYPE_MASTER) {
70                 set_bit(TTY_OTHER_CLOSED, &tty->flags);
71 #ifdef CONFIG_UNIX98_PTYS
72                 if (tty->driver == ptm_driver) {
73                         mutex_lock(&devpts_mutex);
74                         if (tty->link->driver_data)
75                                 devpts_pty_kill(tty->link->driver_data);
76                         mutex_unlock(&devpts_mutex);
77                 }
78 #endif
79                 tty_vhangup(tty->link);
80         }
81 }
82
83 /*
84  * The unthrottle routine is called by the line discipline to signal
85  * that it can receive more characters.  For PTY's, the TTY_THROTTLED
86  * flag is always set, to force the line discipline to always call the
87  * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
88  * characters in the queue.  This is necessary since each time this
89  * happens, we need to wake up any sleeping processes that could be
90  * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
91  * for the pty buffer to be drained.
92  */
93 static void pty_unthrottle(struct tty_struct *tty)
94 {
95         tty_wakeup(tty->link);
96         set_bit(TTY_THROTTLED, &tty->flags);
97 }
98
99 /**
100  *      pty_write               -       write to a pty
101  *      @tty: the tty we write from
102  *      @buf: kernel buffer of data
103  *      @count: bytes to write
104  *
105  *      Our "hardware" write method. Data is coming from the ldisc which
106  *      may be in a non sleeping state. We simply throw this at the other
107  *      end of the link as if we were an IRQ handler receiving stuff for
108  *      the other side of the pty/tty pair.
109  */
110
111 static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
112 {
113         struct tty_struct *to = tty->link;
114         unsigned long flags;
115
116         if (tty->stopped)
117                 return 0;
118
119         mutex_lock(&tty_mutex);
120         if (to->magic != TTY_MAGIC) {
121                 mutex_unlock(&tty_mutex);
122                 return -EIO;
123         }
124
125         if (c > 0) {
126                 spin_lock_irqsave(&to->port->lock, flags);
127                 /* Stuff the data into the input queue of the other end */
128                 c = tty_insert_flip_string(to->port, buf, c);
129                 /* And shovel */
130                 if (c)
131                         tty_flip_buffer_push(to->port);
132                 spin_unlock_irqrestore(&to->port->lock, flags);
133         }
134         mutex_unlock(&tty_mutex);
135         return c;
136 }
137
138 /**
139  *      pty_write_room  -       write space
140  *      @tty: tty we are writing from
141  *
142  *      Report how many bytes the ldisc can send into the queue for
143  *      the other device.
144  */
145
146 static int pty_write_room(struct tty_struct *tty)
147 {
148         if (tty->stopped)
149                 return 0;
150         return tty_buffer_space_avail(tty->link->port);
151 }
152
153 /**
154  *      pty_chars_in_buffer     -       characters currently in our tx queue
155  *      @tty: our tty
156  *
157  *      Report how much we have in the transmit queue. As everything is
158  *      instantly at the other end this is easy to implement.
159  */
160
161 static int pty_chars_in_buffer(struct tty_struct *tty)
162 {
163         return 0;
164 }
165
166 /* Set the lock flag on a pty */
167 static int pty_set_lock(struct tty_struct *tty, int __user *arg)
168 {
169         int val;
170         if (get_user(val, arg))
171                 return -EFAULT;
172         if (val)
173                 set_bit(TTY_PTY_LOCK, &tty->flags);
174         else
175                 clear_bit(TTY_PTY_LOCK, &tty->flags);
176         return 0;
177 }
178
179 static int pty_get_lock(struct tty_struct *tty, int __user *arg)
180 {
181         int locked = test_bit(TTY_PTY_LOCK, &tty->flags);
182         return put_user(locked, arg);
183 }
184
185 /* Set the packet mode on a pty */
186 static int pty_set_pktmode(struct tty_struct *tty, int __user *arg)
187 {
188         int pktmode;
189
190         if (get_user(pktmode, arg))
191                 return -EFAULT;
192
193         spin_lock_irq(&tty->ctrl_lock);
194         if (pktmode) {
195                 if (!tty->packet) {
196                         tty->link->ctrl_status = 0;
197                         smp_mb();
198                         tty->packet = 1;
199                 }
200         } else
201                 tty->packet = 0;
202         spin_unlock_irq(&tty->ctrl_lock);
203
204         return 0;
205 }
206
207 /* Get the packet mode of a pty */
208 static int pty_get_pktmode(struct tty_struct *tty, int __user *arg)
209 {
210         int pktmode = tty->packet;
211         return put_user(pktmode, arg);
212 }
213
214 /* Send a signal to the slave */
215 static int pty_signal(struct tty_struct *tty, int sig)
216 {
217         struct pid *pgrp;
218
219         if (sig != SIGINT && sig != SIGQUIT && sig != SIGTSTP)
220                 return -EINVAL;
221
222         if (tty->link) {
223                 pgrp = tty_get_pgrp(tty->link);
224                 if (pgrp)
225                         kill_pgrp(pgrp, sig, 1);
226                 put_pid(pgrp);
227         }
228         return 0;
229 }
230
231 static void pty_flush_buffer(struct tty_struct *tty)
232 {
233         struct tty_struct *to = tty->link;
234
235         if (!to)
236                 return;
237
238         tty_buffer_flush(to, NULL);
239         if (to->packet) {
240                 spin_lock_irq(&tty->ctrl_lock);
241                 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
242                 wake_up_interruptible(&to->read_wait);
243                 spin_unlock_irq(&tty->ctrl_lock);
244         }
245 }
246
247 static int pty_open(struct tty_struct *tty, struct file *filp)
248 {
249         if (!tty || !tty->link)
250                 return -ENODEV;
251
252         if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
253                 goto out;
254         if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
255                 goto out;
256         if (tty->driver->subtype == PTY_TYPE_SLAVE && tty->link->count != 1)
257                 goto out;
258
259         clear_bit(TTY_IO_ERROR, &tty->flags);
260         clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
261         set_bit(TTY_THROTTLED, &tty->flags);
262         return 0;
263
264 out:
265         set_bit(TTY_IO_ERROR, &tty->flags);
266         return -EIO;
267 }
268
269 static void pty_set_termios(struct tty_struct *tty,
270                                         struct ktermios *old_termios)
271 {
272         /* See if packet mode change of state. */
273         if (tty->link && tty->link->packet) {
274                 int extproc = (old_termios->c_lflag & EXTPROC) | L_EXTPROC(tty);
275                 int old_flow = ((old_termios->c_iflag & IXON) &&
276                                 (old_termios->c_cc[VSTOP] == '\023') &&
277                                 (old_termios->c_cc[VSTART] == '\021'));
278                 int new_flow = (I_IXON(tty) &&
279                                 STOP_CHAR(tty) == '\023' &&
280                                 START_CHAR(tty) == '\021');
281                 if ((old_flow != new_flow) || extproc) {
282                         spin_lock_irq(&tty->ctrl_lock);
283                         if (old_flow != new_flow) {
284                                 tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
285                                 if (new_flow)
286                                         tty->ctrl_status |= TIOCPKT_DOSTOP;
287                                 else
288                                         tty->ctrl_status |= TIOCPKT_NOSTOP;
289                         }
290                         if (extproc)
291                                 tty->ctrl_status |= TIOCPKT_IOCTL;
292                         spin_unlock_irq(&tty->ctrl_lock);
293                         wake_up_interruptible(&tty->link->read_wait);
294                 }
295         }
296
297         tty->termios.c_cflag &= ~(CSIZE | PARENB);
298         tty->termios.c_cflag |= (CS8 | CREAD);
299 }
300
301 /**
302  *      pty_do_resize           -       resize event
303  *      @tty: tty being resized
304  *      @ws: window size being set.
305  *
306  *      Update the termios variables and send the necessary signals to
307  *      peform a terminal resize correctly
308  */
309
310 static int pty_resize(struct tty_struct *tty,  struct winsize *ws)
311 {
312         struct pid *pgrp, *rpgrp;
313         struct tty_struct *pty = tty->link;
314
315         /* For a PTY we need to lock the tty side */
316         mutex_lock(&tty->winsize_mutex);
317         if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
318                 goto done;
319
320         /* Signal the foreground process group of both ptys */
321         pgrp = tty_get_pgrp(tty);
322         rpgrp = tty_get_pgrp(pty);
323
324         if (pgrp)
325                 kill_pgrp(pgrp, SIGWINCH, 1);
326         if (rpgrp != pgrp && rpgrp)
327                 kill_pgrp(rpgrp, SIGWINCH, 1);
328
329         put_pid(pgrp);
330         put_pid(rpgrp);
331
332         tty->winsize = *ws;
333         pty->winsize = *ws;     /* Never used so will go away soon */
334 done:
335         mutex_unlock(&tty->winsize_mutex);
336         return 0;
337 }
338
339 /**
340  *      pty_start - start() handler
341  *      pty_stop  - stop() handler
342  *      @tty: tty being flow-controlled
343  *
344  *      Propagates the TIOCPKT status to the master pty.
345  *
346  *      NB: only the master pty can be in packet mode so only the slave
347  *          needs start()/stop() handlers
348  */
349 static void pty_start(struct tty_struct *tty)
350 {
351         unsigned long flags;
352
353         if (tty->link && tty->link->packet) {
354                 spin_lock_irqsave(&tty->ctrl_lock, flags);
355                 tty->ctrl_status &= ~TIOCPKT_STOP;
356                 tty->ctrl_status |= TIOCPKT_START;
357                 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
358                 wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
359         }
360 }
361
362 static void pty_stop(struct tty_struct *tty)
363 {
364         unsigned long flags;
365
366         if (tty->link && tty->link->packet) {
367                 spin_lock_irqsave(&tty->ctrl_lock, flags);
368                 tty->ctrl_status &= ~TIOCPKT_START;
369                 tty->ctrl_status |= TIOCPKT_STOP;
370                 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
371                 wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
372         }
373 }
374
375 /**
376  *      pty_common_install              -       set up the pty pair
377  *      @driver: the pty driver
378  *      @tty: the tty being instantiated
379  *      @legacy: true if this is BSD style
380  *
381  *      Perform the initial set up for the tty/pty pair. Called from the
382  *      tty layer when the port is first opened.
383  *
384  *      Locking: the caller must hold the tty_mutex
385  */
386 static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
387                 bool legacy)
388 {
389         struct tty_struct *o_tty;
390         struct tty_port *ports[2];
391         int idx = tty->index;
392         int retval = -ENOMEM;
393
394         /* Opening the slave first has always returned -EIO */
395         if (driver->subtype != PTY_TYPE_MASTER)
396                 return -EIO;
397
398         ports[0] = kmalloc(sizeof **ports, GFP_KERNEL);
399         ports[1] = kmalloc(sizeof **ports, GFP_KERNEL);
400         if (!ports[0] || !ports[1])
401                 goto err;
402         if (!try_module_get(driver->other->owner)) {
403                 /* This cannot in fact currently happen */
404                 goto err;
405         }
406         o_tty = alloc_tty_struct(driver->other, idx);
407         if (!o_tty)
408                 goto err_put_module;
409
410         tty_set_lock_subclass(o_tty);
411         lockdep_set_subclass(&o_tty->termios_rwsem, TTY_LOCK_SLAVE);
412
413         if (legacy) {
414                 /* We always use new tty termios data so we can do this
415                    the easy way .. */
416                 tty_init_termios(tty);
417                 tty_init_termios(o_tty);
418
419                 driver->other->ttys[idx] = o_tty;
420                 driver->ttys[idx] = tty;
421         } else {
422                 memset(&tty->termios_locked, 0, sizeof(tty->termios_locked));
423                 tty->termios = driver->init_termios;
424                 memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked));
425                 o_tty->termios = driver->other->init_termios;
426         }
427
428         /*
429          * Everything allocated ... set up the o_tty structure.
430          */
431         tty_driver_kref_get(driver->other);
432         /* Establish the links in both directions */
433         tty->link   = o_tty;
434         o_tty->link = tty;
435         tty_port_init(ports[0]);
436         tty_port_init(ports[1]);
437         tty_buffer_set_limit(ports[0], 8192);
438         tty_buffer_set_limit(ports[1], 8192);
439         o_tty->port = ports[0];
440         tty->port = ports[1];
441         o_tty->port->itty = o_tty;
442
443         tty_buffer_set_lock_subclass(o_tty->port);
444
445         tty_driver_kref_get(driver);
446         tty->count++;
447         o_tty->count++;
448         return 0;
449
450 err_put_module:
451         module_put(driver->other->owner);
452 err:
453         kfree(ports[0]);
454         kfree(ports[1]);
455         return retval;
456 }
457
458 static void pty_cleanup(struct tty_struct *tty)
459 {
460         tty_port_put(tty->port);
461 }
462
463 /* Traditional BSD devices */
464 #ifdef CONFIG_LEGACY_PTYS
465
466 static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
467 {
468         return pty_common_install(driver, tty, true);
469 }
470
471 static void pty_remove(struct tty_driver *driver, struct tty_struct *tty)
472 {
473         struct tty_struct *pair = tty->link;
474         driver->ttys[tty->index] = NULL;
475         if (pair)
476                 pair->driver->ttys[pair->index] = NULL;
477 }
478
479 static int pty_bsd_ioctl(struct tty_struct *tty,
480                          unsigned int cmd, unsigned long arg)
481 {
482         switch (cmd) {
483         case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
484                 return pty_set_lock(tty, (int __user *) arg);
485         case TIOCGPTLCK: /* Get PT Lock status */
486                 return pty_get_lock(tty, (int __user *)arg);
487         case TIOCPKT: /* Set PT packet mode */
488                 return pty_set_pktmode(tty, (int __user *)arg);
489         case TIOCGPKT: /* Get PT packet mode */
490                 return pty_get_pktmode(tty, (int __user *)arg);
491         case TIOCSIG:    /* Send signal to other side of pty */
492                 return pty_signal(tty, (int) arg);
493         case TIOCGPTN: /* TTY returns ENOTTY, but glibc expects EINVAL here */
494                 return -EINVAL;
495         }
496         return -ENOIOCTLCMD;
497 }
498
499 #ifdef CONFIG_COMPAT
500 static long pty_bsd_compat_ioctl(struct tty_struct *tty,
501                                  unsigned int cmd, unsigned long arg)
502 {
503         /*
504          * PTY ioctls don't require any special translation between 32-bit and
505          * 64-bit userspace, they are already compatible.
506          */
507         return pty_bsd_ioctl(tty, cmd, (unsigned long)compat_ptr(arg));
508 }
509 #else
510 #define pty_bsd_compat_ioctl NULL
511 #endif
512
513 static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
514 /*
515  * not really modular, but the easiest way to keep compat with existing
516  * bootargs behaviour is to continue using module_param here.
517  */
518 module_param(legacy_count, int, 0);
519
520 /*
521  * The master side of a pty can do TIOCSPTLCK and thus
522  * has pty_bsd_ioctl.
523  */
524 static const struct tty_operations master_pty_ops_bsd = {
525         .install = pty_install,
526         .open = pty_open,
527         .close = pty_close,
528         .write = pty_write,
529         .write_room = pty_write_room,
530         .flush_buffer = pty_flush_buffer,
531         .chars_in_buffer = pty_chars_in_buffer,
532         .unthrottle = pty_unthrottle,
533         .ioctl = pty_bsd_ioctl,
534         .compat_ioctl = pty_bsd_compat_ioctl,
535         .cleanup = pty_cleanup,
536         .resize = pty_resize,
537         .remove = pty_remove
538 };
539
540 static const struct tty_operations slave_pty_ops_bsd = {
541         .install = pty_install,
542         .open = pty_open,
543         .close = pty_close,
544         .write = pty_write,
545         .write_room = pty_write_room,
546         .flush_buffer = pty_flush_buffer,
547         .chars_in_buffer = pty_chars_in_buffer,
548         .unthrottle = pty_unthrottle,
549         .set_termios = pty_set_termios,
550         .cleanup = pty_cleanup,
551         .resize = pty_resize,
552         .start = pty_start,
553         .stop = pty_stop,
554         .remove = pty_remove
555 };
556
557 static void __init legacy_pty_init(void)
558 {
559         struct tty_driver *pty_driver, *pty_slave_driver;
560
561         if (legacy_count <= 0)
562                 return;
563
564         pty_driver = tty_alloc_driver(legacy_count,
565                         TTY_DRIVER_RESET_TERMIOS |
566                         TTY_DRIVER_REAL_RAW |
567                         TTY_DRIVER_DYNAMIC_ALLOC);
568         if (IS_ERR(pty_driver))
569                 panic("Couldn't allocate pty driver");
570
571         pty_slave_driver = tty_alloc_driver(legacy_count,
572                         TTY_DRIVER_RESET_TERMIOS |
573                         TTY_DRIVER_REAL_RAW |
574                         TTY_DRIVER_DYNAMIC_ALLOC);
575         if (IS_ERR(pty_slave_driver))
576                 panic("Couldn't allocate pty slave driver");
577
578         pty_driver->driver_name = "pty_master";
579         pty_driver->name = "pty";
580         pty_driver->major = PTY_MASTER_MAJOR;
581         pty_driver->minor_start = 0;
582         pty_driver->type = TTY_DRIVER_TYPE_PTY;
583         pty_driver->subtype = PTY_TYPE_MASTER;
584         pty_driver->init_termios = tty_std_termios;
585         pty_driver->init_termios.c_iflag = 0;
586         pty_driver->init_termios.c_oflag = 0;
587         pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
588         pty_driver->init_termios.c_lflag = 0;
589         pty_driver->init_termios.c_ispeed = 38400;
590         pty_driver->init_termios.c_ospeed = 38400;
591         pty_driver->other = pty_slave_driver;
592         tty_set_operations(pty_driver, &master_pty_ops_bsd);
593
594         pty_slave_driver->driver_name = "pty_slave";
595         pty_slave_driver->name = "ttyp";
596         pty_slave_driver->major = PTY_SLAVE_MAJOR;
597         pty_slave_driver->minor_start = 0;
598         pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
599         pty_slave_driver->subtype = PTY_TYPE_SLAVE;
600         pty_slave_driver->init_termios = tty_std_termios;
601         pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
602         pty_slave_driver->init_termios.c_ispeed = 38400;
603         pty_slave_driver->init_termios.c_ospeed = 38400;
604         pty_slave_driver->other = pty_driver;
605         tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
606
607         if (tty_register_driver(pty_driver))
608                 panic("Couldn't register pty driver");
609         if (tty_register_driver(pty_slave_driver))
610                 panic("Couldn't register pty slave driver");
611 }
612 #else
613 static inline void legacy_pty_init(void) { }
614 #endif
615
616 /* Unix98 devices */
617 #ifdef CONFIG_UNIX98_PTYS
618 static struct cdev ptmx_cdev;
619
620 /**
621  *      ptm_open_peer - open the peer of a pty
622  *      @master: the open struct file of the ptmx device node
623  *      @tty: the master of the pty being opened
624  *      @flags: the flags for open
625  *
626  *      Provide a race free way for userspace to open the slave end of a pty
627  *      (where they have the master fd and cannot access or trust the mount
628  *      namespace /dev/pts was mounted inside).
629  */
630 int ptm_open_peer(struct file *master, struct tty_struct *tty, int flags)
631 {
632         int fd = -1;
633         struct file *filp;
634         int retval = -EINVAL;
635         struct path path;
636
637         if (tty->driver != ptm_driver)
638                 return -EIO;
639
640         fd = get_unused_fd_flags(flags);
641         if (fd < 0) {
642                 retval = fd;
643                 goto err;
644         }
645
646         /* Compute the slave's path */
647         path.mnt = devpts_mntget(master, tty->driver_data);
648         if (IS_ERR(path.mnt)) {
649                 retval = PTR_ERR(path.mnt);
650                 goto err_put;
651         }
652         path.dentry = tty->link->driver_data;
653
654         filp = dentry_open(&path, flags, current_cred());
655         mntput(path.mnt);
656         if (IS_ERR(filp)) {
657                 retval = PTR_ERR(filp);
658                 goto err_put;
659         }
660
661         fd_install(fd, filp);
662         return fd;
663
664 err_put:
665         put_unused_fd(fd);
666 err:
667         return retval;
668 }
669
670 static int pty_unix98_ioctl(struct tty_struct *tty,
671                             unsigned int cmd, unsigned long arg)
672 {
673         switch (cmd) {
674         case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
675                 return pty_set_lock(tty, (int __user *)arg);
676         case TIOCGPTLCK: /* Get PT Lock status */
677                 return pty_get_lock(tty, (int __user *)arg);
678         case TIOCPKT: /* Set PT packet mode */
679                 return pty_set_pktmode(tty, (int __user *)arg);
680         case TIOCGPKT: /* Get PT packet mode */
681                 return pty_get_pktmode(tty, (int __user *)arg);
682         case TIOCGPTN: /* Get PT Number */
683                 return put_user(tty->index, (unsigned int __user *)arg);
684         case TIOCSIG:    /* Send signal to other side of pty */
685                 return pty_signal(tty, (int) arg);
686         }
687
688         return -ENOIOCTLCMD;
689 }
690
691 #ifdef CONFIG_COMPAT
692 static long pty_unix98_compat_ioctl(struct tty_struct *tty,
693                                  unsigned int cmd, unsigned long arg)
694 {
695         /*
696          * PTY ioctls don't require any special translation between 32-bit and
697          * 64-bit userspace, they are already compatible.
698          */
699         return pty_unix98_ioctl(tty, cmd,
700                 cmd == TIOCSIG ? arg : (unsigned long)compat_ptr(arg));
701 }
702 #else
703 #define pty_unix98_compat_ioctl NULL
704 #endif
705
706 /**
707  *      ptm_unix98_lookup       -       find a pty master
708  *      @driver: ptm driver
709  *      @idx: tty index
710  *
711  *      Look up a pty master device. Called under the tty_mutex for now.
712  *      This provides our locking.
713  */
714
715 static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
716                 struct file *file, int idx)
717 {
718         /* Master must be open via /dev/ptmx */
719         return ERR_PTR(-EIO);
720 }
721
722 /**
723  *      pts_unix98_lookup       -       find a pty slave
724  *      @driver: pts driver
725  *      @idx: tty index
726  *
727  *      Look up a pty master device. Called under the tty_mutex for now.
728  *      This provides our locking for the tty pointer.
729  */
730
731 static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
732                 struct file *file, int idx)
733 {
734         struct tty_struct *tty;
735
736         mutex_lock(&devpts_mutex);
737         tty = devpts_get_priv(file->f_path.dentry);
738         mutex_unlock(&devpts_mutex);
739         /* Master must be open before slave */
740         if (!tty)
741                 return ERR_PTR(-EIO);
742         return tty;
743 }
744
745 static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
746 {
747         return pty_common_install(driver, tty, false);
748 }
749
750 /* this is called once with whichever end is closed last */
751 static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
752 {
753         struct pts_fs_info *fsi;
754
755         if (tty->driver->subtype == PTY_TYPE_MASTER)
756                 fsi = tty->driver_data;
757         else
758                 fsi = tty->link->driver_data;
759
760         if (fsi) {
761                 devpts_kill_index(fsi, tty->index);
762                 devpts_release(fsi);
763         }
764 }
765
766 static void pty_show_fdinfo(struct tty_struct *tty, struct seq_file *m)
767 {
768         seq_printf(m, "tty-index:\t%d\n", tty->index);
769 }
770
771 static const struct tty_operations ptm_unix98_ops = {
772         .lookup = ptm_unix98_lookup,
773         .install = pty_unix98_install,
774         .remove = pty_unix98_remove,
775         .open = pty_open,
776         .close = pty_close,
777         .write = pty_write,
778         .write_room = pty_write_room,
779         .flush_buffer = pty_flush_buffer,
780         .chars_in_buffer = pty_chars_in_buffer,
781         .unthrottle = pty_unthrottle,
782         .ioctl = pty_unix98_ioctl,
783         .compat_ioctl = pty_unix98_compat_ioctl,
784         .resize = pty_resize,
785         .cleanup = pty_cleanup,
786         .show_fdinfo = pty_show_fdinfo,
787 };
788
789 static const struct tty_operations pty_unix98_ops = {
790         .lookup = pts_unix98_lookup,
791         .install = pty_unix98_install,
792         .remove = pty_unix98_remove,
793         .open = pty_open,
794         .close = pty_close,
795         .write = pty_write,
796         .write_room = pty_write_room,
797         .flush_buffer = pty_flush_buffer,
798         .chars_in_buffer = pty_chars_in_buffer,
799         .unthrottle = pty_unthrottle,
800         .set_termios = pty_set_termios,
801         .start = pty_start,
802         .stop = pty_stop,
803         .cleanup = pty_cleanup,
804 };
805
806 /**
807  *      ptmx_open               -       open a unix 98 pty master
808  *      @inode: inode of device file
809  *      @filp: file pointer to tty
810  *
811  *      Allocate a unix98 pty master device from the ptmx driver.
812  *
813  *      Locking: tty_mutex protects the init_dev work. tty->count should
814  *              protect the rest.
815  *              allocated_ptys_lock handles the list of free pty numbers
816  */
817
818 static int ptmx_open(struct inode *inode, struct file *filp)
819 {
820         struct pts_fs_info *fsi;
821         struct tty_struct *tty;
822         struct dentry *dentry;
823         int retval;
824         int index;
825
826         nonseekable_open(inode, filp);
827
828         /* We refuse fsnotify events on ptmx, since it's a shared resource */
829         filp->f_mode |= FMODE_NONOTIFY;
830
831         retval = tty_alloc_file(filp);
832         if (retval)
833                 return retval;
834
835         fsi = devpts_acquire(filp);
836         if (IS_ERR(fsi)) {
837                 retval = PTR_ERR(fsi);
838                 goto out_free_file;
839         }
840
841         /* find a device that is not in use. */
842         mutex_lock(&devpts_mutex);
843         index = devpts_new_index(fsi);
844         mutex_unlock(&devpts_mutex);
845
846         retval = index;
847         if (index < 0)
848                 goto out_put_fsi;
849
850
851         mutex_lock(&tty_mutex);
852         tty = tty_init_dev(ptm_driver, index);
853         /* The tty returned here is locked so we can safely
854            drop the mutex */
855         mutex_unlock(&tty_mutex);
856
857         retval = PTR_ERR(tty);
858         if (IS_ERR(tty))
859                 goto out;
860
861         /*
862          * From here on out, the tty is "live", and the index and
863          * fsi will be killed/put by the tty_release()
864          */
865         set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
866         tty->driver_data = fsi;
867
868         tty_add_file(tty, filp);
869
870         dentry = devpts_pty_new(fsi, index, tty->link);
871         if (IS_ERR(dentry)) {
872                 retval = PTR_ERR(dentry);
873                 goto err_release;
874         }
875         tty->link->driver_data = dentry;
876
877         retval = ptm_driver->ops->open(tty, filp);
878         if (retval)
879                 goto err_release;
880
881         tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
882
883         tty_unlock(tty);
884         return 0;
885 err_release:
886         tty_unlock(tty);
887         // This will also put-ref the fsi
888         tty_release(inode, filp);
889         return retval;
890 out:
891         devpts_kill_index(fsi, index);
892 out_put_fsi:
893         devpts_release(fsi);
894 out_free_file:
895         tty_free_file(filp);
896         return retval;
897 }
898
899 static struct file_operations ptmx_fops __ro_after_init;
900
901 static void __init unix98_pty_init(void)
902 {
903         ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
904                         TTY_DRIVER_RESET_TERMIOS |
905                         TTY_DRIVER_REAL_RAW |
906                         TTY_DRIVER_DYNAMIC_DEV |
907                         TTY_DRIVER_DEVPTS_MEM |
908                         TTY_DRIVER_DYNAMIC_ALLOC);
909         if (IS_ERR(ptm_driver))
910                 panic("Couldn't allocate Unix98 ptm driver");
911         pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
912                         TTY_DRIVER_RESET_TERMIOS |
913                         TTY_DRIVER_REAL_RAW |
914                         TTY_DRIVER_DYNAMIC_DEV |
915                         TTY_DRIVER_DEVPTS_MEM |
916                         TTY_DRIVER_DYNAMIC_ALLOC);
917         if (IS_ERR(pts_driver))
918                 panic("Couldn't allocate Unix98 pts driver");
919
920         ptm_driver->driver_name = "pty_master";
921         ptm_driver->name = "ptm";
922         ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
923         ptm_driver->minor_start = 0;
924         ptm_driver->type = TTY_DRIVER_TYPE_PTY;
925         ptm_driver->subtype = PTY_TYPE_MASTER;
926         ptm_driver->init_termios = tty_std_termios;
927         ptm_driver->init_termios.c_iflag = 0;
928         ptm_driver->init_termios.c_oflag = 0;
929         ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
930         ptm_driver->init_termios.c_lflag = 0;
931         ptm_driver->init_termios.c_ispeed = 38400;
932         ptm_driver->init_termios.c_ospeed = 38400;
933         ptm_driver->other = pts_driver;
934         tty_set_operations(ptm_driver, &ptm_unix98_ops);
935
936         pts_driver->driver_name = "pty_slave";
937         pts_driver->name = "pts";
938         pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
939         pts_driver->minor_start = 0;
940         pts_driver->type = TTY_DRIVER_TYPE_PTY;
941         pts_driver->subtype = PTY_TYPE_SLAVE;
942         pts_driver->init_termios = tty_std_termios;
943         pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
944         pts_driver->init_termios.c_ispeed = 38400;
945         pts_driver->init_termios.c_ospeed = 38400;
946         pts_driver->other = ptm_driver;
947         tty_set_operations(pts_driver, &pty_unix98_ops);
948
949         if (tty_register_driver(ptm_driver))
950                 panic("Couldn't register Unix98 ptm driver");
951         if (tty_register_driver(pts_driver))
952                 panic("Couldn't register Unix98 pts driver");
953
954         /* Now create the /dev/ptmx special device */
955         tty_default_fops(&ptmx_fops);
956         ptmx_fops.open = ptmx_open;
957
958         cdev_init(&ptmx_cdev, &ptmx_fops);
959         if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
960             register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
961                 panic("Couldn't register /dev/ptmx driver");
962         device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
963 }
964
965 #else
966 static inline void unix98_pty_init(void) { }
967 #endif
968
969 static int __init pty_init(void)
970 {
971         legacy_pty_init();
972         unix98_pty_init();
973         return 0;
974 }
975 device_initcall(pty_init);