On Mon, Sep 10, 2018 at 02:37:07AM -0700, Ayaka Koshibe wrote:
> Mirroring bluhm's fixes for proc.c daemons to dup /dev/null for child
> processes, for switchd(8).
>
> OK?
OK bluhm@
> Index: proc.c
> ===================================================================
> RCS file: /cvs/src/usr.sbin/switchd/proc.c,v
> retrieving revision 1.13
> diff -u -p -u -r1.13 proc.c
> --- proc.c 5 Aug 2018 08:16:24 -0000 1.13
> +++ proc.c 10 Sep 2018 09:12:45 -0000
> @@ -29,13 +29,14 @@
> #include <string.h>
> #include <errno.h>
> #include <signal.h>
> +#include <paths.h>
> #include <pwd.h>
> #include <event.h>
> #include <imsg.h>
>
> #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, u
>
> 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 pri
> } 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 pri
> }
>
> /* Engage! */
> - proc_exec(ps, procs, nproc, argc, argv);
> + proc_exec(ps, procs, nproc, debug, argc, argv);
> return;
> }
>
> Index: proc.h
> ===================================================================
> RCS file: /cvs/src/usr.sbin/switchd/proc.h,v
> retrieving revision 1.7
> diff -u -p -u -r1.7 proc.h
> --- proc.h 5 Aug 2018 08:16:24 -0000 1.7
> +++ proc.h 10 Sep 2018 09:12:45 -0000
> @@ -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);
> Index: switchd.c
> ===================================================================
> RCS file: /cvs/src/usr.sbin/switchd/switchd.c,v
> retrieving revision 1.15
> diff -u -p -u -r1.15 switchd.c
> --- switchd.c 9 Jan 2017 14:49:22 -0000 1.15
> +++ switchd.c 10 Sep 2018 09:12:45 -0000
> @@ -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");