From: Al Viro <v...@zeniv.linux.org.uk>

->set_serial() and ->get_serial() resp., both taking tty and
a kernel pointer to serial_struct.

Signed-off-by: Al Viro <v...@zeniv.linux.org.uk>
---
 drivers/tty/tty_io.c       | 36 ++++++++++++++++++++++++++++++------
 include/linux/tty_driver.h |  3 +++
 2 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index 2b34ccf269e0..91d73732e11c 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -97,6 +97,7 @@
 #include <linux/seq_file.h>
 #include <linux/serial.h>
 #include <linux/ratelimit.h>
+#include <linux/compat.h>
 
 #include <linux/uaccess.h>
 
@@ -2455,22 +2456,40 @@ static int tty_tiocgicount(struct tty_struct *tty, void 
__user *arg)
        return 0;
 }
 
-static void tty_warn_deprecated_flags(struct serial_struct __user *ss)
+static int tty_tiocsserial(struct tty_struct *tty, struct serial_struct __user 
*ss)
 {
        static DEFINE_RATELIMIT_STATE(depr_flags,
                        DEFAULT_RATELIMIT_INTERVAL,
                        DEFAULT_RATELIMIT_BURST);
        char comm[TASK_COMM_LEN];
+       struct serial_struct v;
        int flags;
 
-       if (get_user(flags, &ss->flags))
-               return;
+       if (copy_from_user(&v, ss, sizeof(struct serial_struct)))
+               return -EFAULT;
 
-       flags &= ASYNC_DEPRECATED;
+       flags = v.flags & ASYNC_DEPRECATED;
 
        if (flags && __ratelimit(&depr_flags))
                pr_warn("%s: '%s' is using deprecated serial flags (with no 
effect): %.8x\n",
                        __func__, get_task_comm(comm, current), flags);
+       if (!tty->ops->set_serial)
+               return -ENOIOCTLCMD;
+       return tty->ops->set_serial(tty, &v);
+}
+
+static int tty_tiocgserial(struct tty_struct *tty, struct serial_struct __user 
*ss)
+{
+       struct serial_struct v;
+       int err;
+       memset(&v, 0, sizeof(struct serial_struct));
+
+       if (!tty->ops->set_serial)
+               return -ENOIOCTLCMD;
+       err = tty->ops->get_serial(tty, &v);
+       if (!err && copy_to_user(ss, &v, sizeof(struct serial_struct)))
+               err = -EFAULT;
+       return err;
 }
 
 /*
@@ -2602,8 +2621,13 @@ long tty_ioctl(struct file *file, unsigned int cmd, 
unsigned long arg)
                }
                break;
        case TIOCSSERIAL:
-               tty_warn_deprecated_flags(p);
-               break;
+               retval = tty_tiocsserial(tty, p);
+               if (retval != -ENOIOCTLCMD)
+                       return retval;
+       case TIOCGSERIAL:
+               retval = tty_tiocgserial(tty, p);
+               if (retval != -ENOIOCTLCMD)
+                       return retval;
        case TIOCGPTPEER:
                /* Special because the struct file is needed */
                return ptm_open_peer(file, tty, (int)arg);
diff --git a/include/linux/tty_driver.h b/include/linux/tty_driver.h
index 71dbc891851a..358446247ccd 100644
--- a/include/linux/tty_driver.h
+++ b/include/linux/tty_driver.h
@@ -249,6 +249,7 @@
 struct tty_struct;
 struct tty_driver;
 struct serial_icounter_struct;
+struct serial_struct;
 
 struct tty_operations {
        struct tty_struct * (*lookup)(struct tty_driver *driver,
@@ -287,6 +288,8 @@ struct tty_operations {
        int (*set_termiox)(struct tty_struct *tty, struct termiox *tnew);
        int (*get_icount)(struct tty_struct *tty,
                                struct serial_icounter_struct *icount);
+       int  (*get_serial)(struct tty_struct *tty, struct serial_struct *p);
+       int  (*set_serial)(struct tty_struct *tty, struct serial_struct *p);
        void (*show_fdinfo)(struct tty_struct *tty, struct seq_file *m);
 #ifdef CONFIG_CONSOLE_POLL
        int (*poll_init)(struct tty_driver *driver, int line, char *options);
-- 
2.11.0

Reply via email to