From 8a73e050f051c27ad8c86f7ba329a31e8809e550 Mon Sep 17 00:00:00 2001 From: akoshibe Date: Mon, 10 Sep 2018 13:21:39 +0000 Subject: [PATCH] Mirror bluhm's fixes for proc.c daemons to dup /dev/null for child processes in switchd(8). OK henning@ bluhm@ --- usr.sbin/switchd/proc.c | 21 ++++++++++++++++----- usr.sbin/switchd/proc.h | 4 ++-- usr.sbin/switchd/switchd.c | 4 ++-- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/usr.sbin/switchd/proc.c b/usr.sbin/switchd/proc.c index 6a69d458fa6..3e1b1752d59 100644 --- a/usr.sbin/switchd/proc.c +++ b/usr.sbin/switchd/proc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: proc.c,v 1.13 2018/08/05 08:16:24 mestre Exp $ */ +/* $OpenBSD: proc.c,v 1.14 2018/09/10 13:21:39 akoshibe Exp $ */ /* * Copyright (c) 2010 - 2016 Reyk Floeter @@ -29,13 +29,14 @@ #include #include #include +#include #include #include #include #include "proc.h" -void proc_exec(struct privsep *, struct privsep_proc *, unsigned int, +void proc_exec(struct privsep *, struct privsep_proc *, unsigned int, int, int, char **); void proc_setup(struct privsep *, struct privsep_proc *, unsigned int); void proc_open(struct privsep *, int, int); @@ -80,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 argc, char **argv) + int debug, int argc, char **argv) { unsigned int proc, nargc, i, proc_i; char **nargv; @@ -141,6 +142,16 @@ 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; @@ -191,7 +202,7 @@ proc_connect(struct privsep *ps) void proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc, - int argc, char **argv, enum privsep_procid proc_id) + int debug, int argc, char **argv, enum privsep_procid proc_id) { struct privsep_proc *p = NULL; struct privsep_pipes *pa, *pb; @@ -231,7 +242,7 @@ proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc, } /* Engage! */ - proc_exec(ps, procs, nproc, argc, argv); + proc_exec(ps, procs, nproc, debug, argc, argv); return; } diff --git a/usr.sbin/switchd/proc.h b/usr.sbin/switchd/proc.h index db857b68107..37a1466a929 100644 --- a/usr.sbin/switchd/proc.h +++ b/usr.sbin/switchd/proc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: proc.h,v 1.7 2018/08/05 08:16:24 mestre Exp $ */ +/* $OpenBSD: proc.h,v 1.8 2018/09/10 13:21:39 akoshibe Exp $ */ /* * Copyright (c) 2010-2015 Reyk Floeter @@ -126,7 +126,7 @@ TAILQ_HEAD(ctl_connlist, ctl_conn); extern struct ctl_connlist ctl_conns; /* proc.c */ -void proc_init(struct privsep *, struct privsep_proc *, unsigned int, +void proc_init(struct privsep *, struct privsep_proc *, unsigned int, int, int, char **, enum privsep_procid); void proc_kill(struct privsep *); void proc_connect(struct privsep *ps); diff --git a/usr.sbin/switchd/switchd.c b/usr.sbin/switchd/switchd.c index 5d4fde3a272..c42ee62fad8 100644 --- a/usr.sbin/switchd/switchd.c +++ b/usr.sbin/switchd/switchd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: switchd.c,v 1.15 2017/01/09 14:49:22 reyk Exp $ */ +/* $OpenBSD: switchd.c,v 1.16 2018/09/10 13:21:39 akoshibe Exp $ */ /* * Copyright (c) 2013-2016 Reyk Floeter @@ -184,7 +184,7 @@ main(int argc, char *argv[]) ps->ps_title[proc_id] = title; /* Only the parent returns. */ - proc_init(ps, procs, nitems(procs), argc0, argv, proc_id); + proc_init(ps, procs, nitems(procs), debug, argc0, argv, proc_id); if (!debug && daemon(0, 0) == -1) fatal("failed to daemonize"); -- 2.20.1