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