Merge tag 'hwmon-for-v5.11' of git://git.kernel.org/pub/scm/linux/kernel/git/groeck...
[linux-2.6-microblaze.git] / drivers / tty / tty_jobctrl.c
index 28a23a0..4b751b9 100644 (file)
@@ -21,6 +21,7 @@ static int is_ignored(int sig)
 /**
  *     tty_check_change        -       check for POSIX terminal changes
  *     @tty: tty to check
+ *     @sig: signal to send
  *
  *     If we try to write to, or set the state of, a terminal and we're
  *     not in the foreground, send a SIGTTOU.  If the signal is blocked or
@@ -83,6 +84,7 @@ void proc_clear_tty(struct task_struct *p)
 
 /**
  * proc_set_tty -  set the controlling terminal
+ *     @tty: tty structure
  *
  * Only callable by the session leader and only if it does not already have
  * a controlling terminal.
@@ -103,8 +105,8 @@ static void __proc_set_tty(struct tty_struct *tty)
        put_pid(tty->session);
        put_pid(tty->pgrp);
        tty->pgrp = get_pid(task_pgrp(current));
-       spin_unlock_irqrestore(&tty->ctrl_lock, flags);
        tty->session = get_pid(task_session(current));
+       spin_unlock_irqrestore(&tty->ctrl_lock, flags);
        if (current->signal->tty) {
                tty_debug(tty, "current tty %s not NULL!!\n",
                          current->signal->tty->name);
@@ -293,20 +295,23 @@ void disassociate_ctty(int on_exit)
        spin_lock_irq(&current->sighand->siglock);
        put_pid(current->signal->tty_old_pgrp);
        current->signal->tty_old_pgrp = NULL;
-
        tty = tty_kref_get(current->signal->tty);
+       spin_unlock_irq(&current->sighand->siglock);
+
        if (tty) {
                unsigned long flags;
+
+               tty_lock(tty);
                spin_lock_irqsave(&tty->ctrl_lock, flags);
                put_pid(tty->session);
                put_pid(tty->pgrp);
                tty->session = NULL;
                tty->pgrp = NULL;
                spin_unlock_irqrestore(&tty->ctrl_lock, flags);
+               tty_unlock(tty);
                tty_kref_put(tty);
        }
 
-       spin_unlock_irq(&current->sighand->siglock);
        /* Now clear signal->tty under the lock */
        read_lock(&tasklist_lock);
        session_clear_tty(task_session(current));
@@ -330,6 +335,7 @@ void no_tty(void)
 /**
  *     tiocsctty       -       set controlling tty
  *     @tty: tty structure
+ *     @file: file structure used to check permissions
  *     @arg: user argument
  *
  *     This ioctl is used to manage job control. It permits a session
@@ -477,14 +483,19 @@ static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t
                return -ENOTTY;
        if (retval)
                return retval;
-       if (!current->signal->tty ||
-           (current->signal->tty != real_tty) ||
-           (real_tty->session != task_session(current)))
-               return -ENOTTY;
+
        if (get_user(pgrp_nr, p))
                return -EFAULT;
        if (pgrp_nr < 0)
                return -EINVAL;
+
+       spin_lock_irq(&real_tty->ctrl_lock);
+       if (!current->signal->tty ||
+           (current->signal->tty != real_tty) ||
+           (real_tty->session != task_session(current))) {
+               retval = -ENOTTY;
+               goto out_unlock_ctrl;
+       }
        rcu_read_lock();
        pgrp = find_vpid(pgrp_nr);
        retval = -ESRCH;
@@ -494,12 +505,12 @@ static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t
        if (session_of_pgrp(pgrp) != task_session(current))
                goto out_unlock;
        retval = 0;
-       spin_lock_irq(&tty->ctrl_lock);
        put_pid(real_tty->pgrp);
        real_tty->pgrp = get_pid(pgrp);
-       spin_unlock_irq(&tty->ctrl_lock);
 out_unlock:
        rcu_read_unlock();
+out_unlock_ctrl:
+       spin_unlock_irq(&real_tty->ctrl_lock);
        return retval;
 }
 
@@ -511,20 +522,30 @@ out_unlock:
  *
  *     Obtain the session id of the tty. If there is no session
  *     return an error.
- *
- *     Locking: none. Reference to current->signal->tty is safe.
  */
 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
 {
+       unsigned long flags;
+       pid_t sid;
+
        /*
         * (tty == real_tty) is a cheap way of
         * testing if the tty is NOT a master pty.
        */
        if (tty == real_tty && current->signal->tty != real_tty)
                return -ENOTTY;
+
+       spin_lock_irqsave(&real_tty->ctrl_lock, flags);
        if (!real_tty->session)
-               return -ENOTTY;
-       return put_user(pid_vnr(real_tty->session), p);
+               goto err;
+       sid = pid_vnr(real_tty->session);
+       spin_unlock_irqrestore(&real_tty->ctrl_lock, flags);
+
+       return put_user(sid, p);
+
+err:
+       spin_unlock_irqrestore(&real_tty->ctrl_lock, flags);
+       return -ENOTTY;
 }
 
 /*