Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  Copyright (C) 1991, 1992  Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/sched/task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) static int is_ignored(int sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	return (sigismember(&current->blocked, sig) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 		current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *	tty_check_change	-	check for POSIX terminal changes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *	@tty: tty to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *	If we try to write to, or set the state of, a terminal and we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *	not in the foreground, send a SIGTTOU.  If the signal is blocked or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *	ignored, go ahead and perform the operation.  (POSIX 7.2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  *	Locking: ctrl_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) int __tty_check_change(struct tty_struct *tty, int sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct pid *pgrp, *tty_pgrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	if (current->signal->tty != tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	pgrp = task_pgrp(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	spin_lock_irqsave(&tty->ctrl_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	tty_pgrp = tty->pgrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	if (tty_pgrp && pgrp != tty_pgrp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		if (is_ignored(sig)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 			if (sig == SIGTTIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 				ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		} else if (is_current_pgrp_orphaned())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 			kill_pgrp(pgrp, sig, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 			set_thread_flag(TIF_SIGPENDING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 			ret = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (!tty_pgrp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		tty_warn(tty, "sig=%d, tty->pgrp == NULL!\n", sig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) int tty_check_change(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	return __tty_check_change(tty, SIGTTOU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) EXPORT_SYMBOL(tty_check_change);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) void proc_clear_tty(struct task_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct tty_struct *tty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	spin_lock_irqsave(&p->sighand->siglock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	tty = p->signal->tty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	p->signal->tty = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	spin_unlock_irqrestore(&p->sighand->siglock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	tty_kref_put(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  * proc_set_tty -  set the controlling terminal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * Only callable by the session leader and only if it does not already have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * a controlling terminal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  * Caller must hold:  tty_lock()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  *		      a readlock on tasklist_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  *		      sighand lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static void __proc_set_tty(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	spin_lock_irqsave(&tty->ctrl_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	 * The session and fg pgrp references will be non-NULL if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	 * tiocsctty() is stealing the controlling tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	put_pid(tty->session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	put_pid(tty->pgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	tty->pgrp = get_pid(task_pgrp(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	tty->session = get_pid(task_session(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	if (current->signal->tty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		tty_debug(tty, "current tty %s not NULL!!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			  current->signal->tty->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		tty_kref_put(current->signal->tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	put_pid(current->signal->tty_old_pgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	current->signal->tty = tty_kref_get(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	current->signal->tty_old_pgrp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static void proc_set_tty(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	spin_lock_irq(&current->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	__proc_set_tty(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	spin_unlock_irq(&current->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)  * Called by tty_open() to set the controlling tty if applicable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) void tty_open_proc_set_tty(struct file *filp, struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	read_lock(&tasklist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	spin_lock_irq(&current->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if (current->signal->leader &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	    !current->signal->tty &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	    tty->session == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		 * Don't let a process that only has write access to the tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		 * obtain the privileges associated with having a tty as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		 * controlling terminal (being able to reopen it with full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		 * access through /dev/tty, being able to perform pushback).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		 * Many distributions set the group of all ttys to "tty" and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		 * grant write-only access to all terminals for setgid tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		 * binaries, which should not imply full privileges on all ttys.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		 * This could theoretically break old code that performs open()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		 * on a write-only file descriptor. In that case, it might be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		 * necessary to also permit this if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		 * inode_permission(inode, MAY_READ) == 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		if (filp->f_mode & FMODE_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			__proc_set_tty(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	spin_unlock_irq(&current->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	read_unlock(&tasklist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct tty_struct *get_current_tty(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	struct tty_struct *tty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	spin_lock_irqsave(&current->sighand->siglock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	tty = tty_kref_get(current->signal->tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	spin_unlock_irqrestore(&current->sighand->siglock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	return tty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) EXPORT_SYMBOL_GPL(get_current_tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  * Called from tty_release().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) void session_clear_tty(struct pid *session)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct task_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	do_each_pid_task(session, PIDTYPE_SID, p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		proc_clear_tty(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	} while_each_pid_task(session, PIDTYPE_SID, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  *	tty_signal_session_leader	- sends SIGHUP to session leader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  *	@tty: controlling tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  *	@exit_session: if non-zero, signal all foreground group processes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  *	Send SIGHUP and SIGCONT to the session leader and its process group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  *	Optionally, signal all processes in the foreground process group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)  *	Returns the number of processes in the session with this tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)  *	as their controlling terminal. This value is used to drop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)  *	tty references for those processes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) int tty_signal_session_leader(struct tty_struct *tty, int exit_session)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	struct task_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	int refs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	struct pid *tty_pgrp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	read_lock(&tasklist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	if (tty->session) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		do_each_pid_task(tty->session, PIDTYPE_SID, p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			spin_lock_irq(&p->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			if (p->signal->tty == tty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 				p->signal->tty = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 				/* We defer the dereferences outside fo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 				   the tasklist lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 				refs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			if (!p->signal->leader) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 				spin_unlock_irq(&p->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			__group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			__group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			put_pid(p->signal->tty_old_pgrp);  /* A noop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			spin_lock(&tty->ctrl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			tty_pgrp = get_pid(tty->pgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			if (tty->pgrp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 				p->signal->tty_old_pgrp = get_pid(tty->pgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			spin_unlock(&tty->ctrl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			spin_unlock_irq(&p->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		} while_each_pid_task(tty->session, PIDTYPE_SID, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	read_unlock(&tasklist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (tty_pgrp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		if (exit_session)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 			kill_pgrp(tty_pgrp, SIGHUP, exit_session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		put_pid(tty_pgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	return refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  *	disassociate_ctty	-	disconnect controlling tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  *	@on_exit: true if exiting so need to "hang up" the session
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)  *	This function is typically called only by the session leader, when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)  *	it wants to disassociate itself from its controlling tty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  *	It performs the following functions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)  * 	(1)  Sends a SIGHUP and SIGCONT to the foreground process group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)  * 	(2)  Clears the tty from being controlling the session
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)  * 	(3)  Clears the controlling tty for all processes in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)  * 		session group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)  *	The argument on_exit is set to 1 if called when a process is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)  *	exiting; it is 0 if called by the ioctl TIOCNOTTY.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)  *	Locking:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)  *		BTM is taken for hysterical raisons, and held when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)  *		  called from no_tty().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)  *		  tty_mutex is taken to protect tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)  *		  ->siglock is taken to protect ->signal/->sighand
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)  *		  tasklist_lock is taken to walk process list for sessions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)  *		    ->siglock is taken to protect ->signal/->sighand
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) void disassociate_ctty(int on_exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	struct tty_struct *tty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (!current->signal->leader)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	tty = get_current_tty();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (tty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			tty_vhangup_session(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			struct pid *tty_pgrp = tty_get_pgrp(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			if (tty_pgrp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 				kill_pgrp(tty_pgrp, SIGHUP, on_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 				if (!on_exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 					kill_pgrp(tty_pgrp, SIGCONT, on_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 				put_pid(tty_pgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		tty_kref_put(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	} else if (on_exit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		struct pid *old_pgrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		spin_lock_irq(&current->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		old_pgrp = current->signal->tty_old_pgrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		current->signal->tty_old_pgrp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		spin_unlock_irq(&current->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		if (old_pgrp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 			kill_pgrp(old_pgrp, SIGHUP, on_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			kill_pgrp(old_pgrp, SIGCONT, on_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			put_pid(old_pgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	spin_lock_irq(&current->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	put_pid(current->signal->tty_old_pgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	current->signal->tty_old_pgrp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	tty = tty_kref_get(current->signal->tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	spin_unlock_irq(&current->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (tty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		tty_lock(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		spin_lock_irqsave(&tty->ctrl_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		put_pid(tty->session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		put_pid(tty->pgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		tty->session = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		tty->pgrp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		spin_unlock_irqrestore(&tty->ctrl_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		tty_unlock(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		tty_kref_put(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	/* Now clear signal->tty under the lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	read_lock(&tasklist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	session_clear_tty(task_session(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	read_unlock(&tasklist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)  *	no_tty	- Ensure the current process does not have a controlling tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) void no_tty(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	/* FIXME: Review locking here. The tty_lock never covered any race
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	   between a new association and proc_clear_tty but possible we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	   to protect against this anyway */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	struct task_struct *tsk = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	disassociate_ctty(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	proc_clear_tty(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)  *	tiocsctty	-	set controlling tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)  *	@tty: tty structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)  *	@arg: user argument
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)  *	This ioctl is used to manage job control. It permits a session
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)  *	leader to set this tty as the controlling tty for the session.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)  *	Locking:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)  *		Takes tty_lock() to serialize proc_set_tty() for this tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  *		Takes tasklist_lock internally to walk sessions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  *		Takes ->siglock() when updating signal->tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static int tiocsctty(struct tty_struct *tty, struct file *file, int arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	tty_lock(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	read_lock(&tasklist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	if (current->signal->leader && (task_session(current) == tty->session))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	 * The process must be a session leader and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	 * not have a controlling tty already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	if (!current->signal->leader || current->signal->tty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	if (tty->session) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		 * This tty is already the controlling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		 * tty for another session group!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		if (arg == 1 && capable(CAP_SYS_ADMIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			 * Steal it away
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			session_clear_tty(tty->session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 			ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	/* See the comment in tty_open_proc_set_tty(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	if ((file->f_mode & FMODE_READ) == 0 && !capable(CAP_SYS_ADMIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	proc_set_tty(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	read_unlock(&tasklist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	tty_unlock(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)  *	tty_get_pgrp	-	return a ref counted pgrp pid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)  *	@tty: tty to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)  *	Returns a refcounted instance of the pid struct for the process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)  *	group controlling the tty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) struct pid *tty_get_pgrp(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	struct pid *pgrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	spin_lock_irqsave(&tty->ctrl_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	pgrp = get_pid(tty->pgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	return pgrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) EXPORT_SYMBOL_GPL(tty_get_pgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)  * This checks not only the pgrp, but falls back on the pid if no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)  * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)  * without this...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)  * The caller must hold rcu lock or the tasklist lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) static struct pid *session_of_pgrp(struct pid *pgrp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	struct task_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	struct pid *sid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	p = pid_task(pgrp, PIDTYPE_PGID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	if (p == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		p = pid_task(pgrp, PIDTYPE_PID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	if (p != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		sid = task_session(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	return sid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)  *	tiocgpgrp		-	get process group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)  *	@tty: tty passed by user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)  *	@real_tty: tty side of the tty passed by the user if a pty else the tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)  *	@p: returned pid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)  *	Obtain the process group of the tty. If there is no process group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)  *	return an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)  *	Locking: none. Reference to current->signal->tty is safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	struct pid *pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	 * (tty == real_tty) is a cheap way of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	 * testing if the tty is NOT a master pty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	if (tty == real_tty && current->signal->tty != real_tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	pid = tty_get_pgrp(real_tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	ret =  put_user(pid_vnr(pid), p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	put_pid(pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)  *	tiocspgrp		-	attempt to set process group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)  *	@tty: tty passed by user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)  *	@real_tty: tty side device matching tty passed by user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)  *	@p: pid pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)  *	Set the process group of the tty to the session passed. Only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)  *	permitted where the tty session is our session.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)  *	Locking: RCU, ctrl lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	struct pid *pgrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	pid_t pgrp_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	int retval = tty_check_change(real_tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	if (retval == -EIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	if (get_user(pgrp_nr, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	if (pgrp_nr < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	spin_lock_irq(&real_tty->ctrl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	if (!current->signal->tty ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	    (current->signal->tty != real_tty) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	    (real_tty->session != task_session(current))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		retval = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		goto out_unlock_ctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	pgrp = find_vpid(pgrp_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	retval = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	if (!pgrp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	retval = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	if (session_of_pgrp(pgrp) != task_session(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	put_pid(real_tty->pgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	real_tty->pgrp = get_pid(pgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) out_unlock_ctrl:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	spin_unlock_irq(&real_tty->ctrl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)  *	tiocgsid		-	get session id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)  *	@tty: tty passed by user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)  *	@real_tty: tty side of the tty passed by the user if a pty else the tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)  *	@p: pointer to returned session id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)  *	Obtain the session id of the tty. If there is no session
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)  *	return an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	pid_t sid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	 * (tty == real_tty) is a cheap way of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	 * testing if the tty is NOT a master pty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	if (tty == real_tty && current->signal->tty != real_tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	spin_lock_irqsave(&real_tty->ctrl_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	if (!real_tty->session)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	sid = pid_vnr(real_tty->session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	spin_unlock_irqrestore(&real_tty->ctrl_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	return put_user(sid, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	spin_unlock_irqrestore(&real_tty->ctrl_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)  * Called from tty_ioctl(). If tty is a pty then real_tty is the slave side,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)  * if not then tty == real_tty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) long tty_jobctrl_ioctl(struct tty_struct *tty, struct tty_struct *real_tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		       struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	void __user *p = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	case TIOCNOTTY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		if (current->signal->tty != tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 			return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		no_tty();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	case TIOCSCTTY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		return tiocsctty(real_tty, file, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	case TIOCGPGRP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		return tiocgpgrp(tty, real_tty, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	case TIOCSPGRP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		return tiocspgrp(tty, real_tty, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	case TIOCGSID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 		return tiocgsid(tty, real_tty, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }