Pass sudo's process ID to suspend_sudo_nopty() since we already know it.

Saves an unnecessary getpid(2) call.
This commit is contained in:
Todd C. Miller
2022-11-07 14:51:43 -07:00
parent f87fac3b55
commit 045d08bec9
4 changed files with 17 additions and 13 deletions

View File

@@ -757,8 +757,10 @@ handle_sigchld_nopty(struct exec_closure *ec)
}
/* If the main command is suspended, suspend sudo too. */
if (pid == ec->cmnd_pid)
suspend_sudo_nopty(ec, signo, ec->ppgrp, ec->cmnd_pid);
if (pid == ec->cmnd_pid) {
suspend_sudo_nopty(ec, signo, ec->sudo_pid, ec->ppgrp,
ec->cmnd_pid);
}
} else {
if (WIFSIGNALED(status)) {
if (sig2str(WTERMSIG(status), signame) == -1) {

View File

@@ -121,7 +121,7 @@ main(int argc, char *argv[])
const char *errstr;
sigset_t blocked, empty;
struct sigaction sa;
pid_t child, pid, ppgrp;
pid_t child, my_pid, pid, my_pgrp;
int ch, status;
debug_decl_vars(main, SUDO_DEBUG_MAIN);
@@ -173,7 +173,8 @@ main(int argc, char *argv[])
sigaction(SIGUSR1, &sa, NULL);
/* Fork a shell. */
ppgrp = getpgrp();
my_pid = getpid();
my_pgrp = getpgrp();
child = fork();
switch (child) {
case -1:
@@ -227,7 +228,8 @@ main(int argc, char *argv[])
} else if (WIFSTOPPED(status)) {
if (exec_ptrace_stopped(pid, status, &closure)) {
if (pid == child) {
suspend_sudo_nopty(NULL, WSTOPSIG(status), ppgrp, child);
suspend_sudo_nopty(NULL, WSTOPSIG(status), my_pid,
my_pgrp, child);
if (kill(child, SIGCONT) != 0)
sudo_warn("kill(%d, SIGCONT)", (int)child);
}

View File

@@ -233,6 +233,6 @@ bool set_exec_filter(void);
int exec_ptrace_seize(pid_t child);
/* suspend_nopty.c */
void suspend_sudo_nopty(struct exec_closure *ec, int signo, pid_t ppgrp, pid_t cmnd_pid);
void suspend_sudo_nopty(struct exec_closure *ec, int signo, pid_t my_pid, pid_t my_pgrp, pid_t cmnd_pid);
#endif /* SUDO_EXEC_H */

View File

@@ -34,8 +34,8 @@
#include "sudo_exec.h"
void
suspend_sudo_nopty(struct exec_closure *ec, int signo, pid_t ppgrp,
pid_t cmnd_pid)
suspend_sudo_nopty(struct exec_closure *ec, int signo, pid_t my_pid,
pid_t my_pgrp, pid_t cmnd_pid)
{
struct sigaction sa, osa;
pid_t saved_pgrp = -1;
@@ -67,9 +67,9 @@ suspend_sudo_nopty(struct exec_closure *ec, int signo, pid_t ppgrp,
* pgrp and let it continue.
*/
if (signo == SIGTTOU || signo == SIGTTIN) {
if (saved_pgrp == ppgrp) {
if (saved_pgrp == my_pgrp) {
pid_t cmnd_pgrp = getpgid(cmnd_pid);
if (cmnd_pgrp != ppgrp) {
if (cmnd_pgrp != my_pgrp) {
if (tcsetpgrp_nobg(fd, cmnd_pgrp) == 0) {
if (killpg(cmnd_pgrp, SIGCONT) != 0)
sudo_warn("kill(%d, SIGCONT)", (int)cmnd_pgrp);
@@ -92,8 +92,8 @@ suspend_sudo_nopty(struct exec_closure *ec, int signo, pid_t ppgrp,
if (sudo_sigaction(SIGTSTP, &sa, &osa) != 0)
sudo_warn(U_("unable to set handler for signal %d"), SIGTSTP);
}
if (kill(getpid(), signo) != 0)
sudo_warn("kill(%d, %d)", (int)getpid(), signo);
if (kill(my_pid, signo) != 0)
sudo_warn("kill(%d, %d)", (int)my_pid, signo);
if (signo == SIGTSTP) {
if (sudo_sigaction(SIGTSTP, &osa, NULL) != 0)
sudo_warn(U_("unable to restore handler for signal %d"), SIGTSTP);
@@ -110,7 +110,7 @@ suspend_sudo_nopty(struct exec_closure *ec, int signo, pid_t ppgrp,
* It is possible that we are no longer the foreground process,
* use tcsetpgrp_nobg() to prevent sudo from receiving SIGTTOU.
*/
if (saved_pgrp != ppgrp)
if (saved_pgrp != my_pgrp)
tcsetpgrp_nobg(fd, saved_pgrp);
close(fd);
}