From: bluhm Date: Mon, 10 Sep 2018 10:36:01 +0000 (+0000) Subject: During the fork+exec implementation, daemon(3) was moved after X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=a9955862d6f72f7052d014379ab8b79e9489dbd5;p=openbsd During the fork+exec implementation, daemon(3) was moved after proc_init(). As a consequence vmd(8) child processes did not detach from the terminal anymore. Dup /dev/null to the stdio file descriptors in the children. OK mlarkin@ reyk@ --- diff --git a/usr.sbin/vmd/proc.c b/usr.sbin/vmd/proc.c index b37036908cd..fde98994bc1 100644 --- a/usr.sbin/vmd/proc.c +++ b/usr.sbin/vmd/proc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: proc.c,v 1.17 2018/08/05 08:20:54 mestre Exp $ */ +/* $OpenBSD: proc.c,v 1.18 2018/09/10 10:36:01 bluhm 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/vmd/proc.h b/usr.sbin/vmd/proc.h index 01361076f54..5dfccb9cf09 100644 --- a/usr.sbin/vmd/proc.h +++ b/usr.sbin/vmd/proc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: proc.h,v 1.15 2018/08/05 08:20:54 mestre Exp $ */ +/* $OpenBSD: proc.h,v 1.16 2018/09/10 10:36:01 bluhm Exp $ */ /* * Copyright (c) 2010-2015 Reyk Floeter @@ -156,7 +156,7 @@ struct privsep_fd { #define PROC_MAX_INSTANCES 32 /* 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/vmd/vmd.c b/usr.sbin/vmd/vmd.c index 2b07a8463b7..ac4d1635bd4 100644 --- a/usr.sbin/vmd/vmd.c +++ b/usr.sbin/vmd/vmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vmd.c,v 1.98 2018/07/15 14:36:54 reyk Exp $ */ +/* $OpenBSD: vmd.c,v 1.99 2018/09/10 10:36:01 bluhm Exp $ */ /* * Copyright (c) 2015 Reyk Floeter @@ -792,7 +792,8 @@ 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), env->vmd_debug, argc0, argv, + proc_id); log_procinit("parent"); if (!env->vmd_debug && daemon(0, 0) == -1)