From 7a99b37dfd03f1f281b20ab09f836d05794a9da5 Mon Sep 17 00:00:00 2001 From: benno Date: Sat, 3 Sep 2022 20:07:31 +0000 Subject: [PATCH] Move the daemon() call in the parent process from after forking the children to just before. That way the parent disasociates from its controling terminal and shell, but not from its children. Remove the dup2() bits that were copied from daemon() to solve the problem that the children still had the stdio fds open. This is now done in the parent earlier. Remove the setsid() and setpgid(). It is unclear what their intent was, but they dont seem to make sense, as daemon() covers this as well and there seems to be no reason the cildren procs need to do that. ok claudio@ bluhm@ --- usr.sbin/relayd/proc.c | 28 +++++++--------------------- usr.sbin/relayd/relayd.c | 4 +--- 2 files changed, 8 insertions(+), 24 deletions(-) diff --git a/usr.sbin/relayd/proc.c b/usr.sbin/relayd/proc.c index 1407f58fee6..ce7b056f779 100644 --- a/usr.sbin/relayd/proc.c +++ b/usr.sbin/relayd/proc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: proc.c,v 1.42 2021/12/30 20:38:43 dv Exp $ */ +/* $OpenBSD: proc.c,v 1.43 2022/09/03 20:07:31 benno Exp $ */ /* * Copyright (c) 2010 - 2016 Reyk Floeter @@ -37,7 +37,7 @@ #include "relayd.h" void proc_exec(struct privsep *, struct privsep_proc *, unsigned int, int, - int, char **); + char **); void proc_setup(struct privsep *, struct privsep_proc *, unsigned int); void proc_open(struct privsep *, int, int); void proc_accept(struct privsep *, int, enum privsep_procid, @@ -81,7 +81,7 @@ proc_getid(struct privsep_proc *procs, unsigned int nproc, void proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc, - int debug, int argc, char **argv) + int argc, char **argv) { unsigned int proc, nargc, i, proc_i; char **nargv; @@ -130,10 +130,6 @@ proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc, fatal("%s: fork", __func__); break; case 0: - /* First create a new session */ - if (setsid() == -1) - fatal("setsid"); - /* Prepare parent socket. */ if (fd != PROC_PARENT_SOCK_FILENO) { if (dup2(fd, PROC_PARENT_SOCK_FILENO) @@ -142,16 +138,6 @@ proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc, } else if (fcntl(fd, F_SETFD, 0) == -1) fatal("fcntl"); - /* Daemons detach from terminal. */ - if (!debug && (fd = - open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { - (void)dup2(fd, STDIN_FILENO); - (void)dup2(fd, STDOUT_FILENO); - (void)dup2(fd, STDERR_FILENO); - if (fd > 2) - (void)close(fd); - } - execvp(argv[0], nargv); fatal("%s: execvp", __func__); break; @@ -218,6 +204,9 @@ proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc, privsep_process = PROC_PARENT; proc_setup(ps, procs, nproc); + if (!debug && daemon(1, 0) == -1) + fatal("failed to daemonize"); + /* * Create the children sockets so we can use them * to distribute the rest of the socketpair()s using @@ -242,7 +231,7 @@ proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc, } /* Engage! */ - proc_exec(ps, procs, nproc, debug, argc, argv); + proc_exec(ps, procs, nproc, argc, argv); return; } @@ -537,9 +526,6 @@ proc_run(struct privsep *ps, struct privsep_proc *p, log_procinit(p->p_title); - /* Set the process group of the current process */ - setpgid(0, 0); - if (p->p_id == PROC_CONTROL && ps->ps_instance == 0) { if (control_init(ps, &ps->ps_csock) == -1) fatalx("%s: control_init", __func__); diff --git a/usr.sbin/relayd/relayd.c b/usr.sbin/relayd/relayd.c index 9046dfcc4cd..9a6a5c836a0 100644 --- a/usr.sbin/relayd/relayd.c +++ b/usr.sbin/relayd/relayd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: relayd.c,v 1.188 2022/08/31 16:17:18 dv Exp $ */ +/* $OpenBSD: relayd.c,v 1.189 2022/09/03 20:07:31 benno Exp $ */ /* * Copyright (c) 2007 - 2016 Reyk Floeter @@ -218,8 +218,6 @@ main(int argc, char *argv[]) proc_init(ps, procs, nitems(procs), debug, argc0, argv, proc_id); log_procinit("parent"); - if (!debug && daemon(1, 0) == -1) - err(1, "failed to daemonize"); if (ps->ps_noaction == 0) log_info("startup"); -- 2.20.1