From 38b341808c90fc68f4b3301d6743519a6922fd5f Mon Sep 17 00:00:00 2001 From: bluhm Date: Sun, 9 Sep 2018 21:06:51 +0000 Subject: [PATCH] During the fork+exec implementation, daemon(3) was moved after proc_init(). As a consequence httpd(8) and relayd(8) child processes did not detach from the terminal anymore. Dup /dev/null to the stdio file descriptors in the children. OK benno@ --- usr.sbin/httpd/httpd.c | 4 ++-- usr.sbin/httpd/httpd.h | 4 ++-- usr.sbin/httpd/proc.c | 21 ++++++++++++++++----- usr.sbin/relayd/proc.c | 21 ++++++++++++++++----- usr.sbin/relayd/relayd.c | 4 ++-- usr.sbin/relayd/relayd.h | 4 ++-- 6 files changed, 40 insertions(+), 18 deletions(-) diff --git a/usr.sbin/httpd/httpd.c b/usr.sbin/httpd/httpd.c index 6d1d1ff34fe..ae0fa879ef5 100644 --- a/usr.sbin/httpd/httpd.c +++ b/usr.sbin/httpd/httpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: httpd.c,v 1.67 2017/05/28 10:37:26 benno Exp $ */ +/* $OpenBSD: httpd.c,v 1.68 2018/09/09 21:06:51 bluhm Exp $ */ /* * Copyright (c) 2014 Reyk Floeter @@ -215,7 +215,7 @@ main(int argc, char *argv[]) } /* 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); log_procinit("parent"); if (!debug && daemon(1, 0) == -1) diff --git a/usr.sbin/httpd/httpd.h b/usr.sbin/httpd/httpd.h index 5cfbd996bad..4b1d9d72237 100644 --- a/usr.sbin/httpd/httpd.h +++ b/usr.sbin/httpd/httpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: httpd.h,v 1.139 2018/08/19 18:03:35 jasper Exp $ */ +/* $OpenBSD: httpd.h,v 1.140 2018/09/09 21:06:51 bluhm Exp $ */ /* * Copyright (c) 2006 - 2015 Reyk Floeter @@ -780,7 +780,7 @@ __dead void fatalx(const char *, ...) /* proc.c */ enum privsep_procid proc_getid(struct privsep_proc *, unsigned int, const char *); -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 *); diff --git a/usr.sbin/httpd/proc.c b/usr.sbin/httpd/proc.c index cd387a17903..729ecff2489 100644 --- a/usr.sbin/httpd/proc.c +++ b/usr.sbin/httpd/proc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: proc.c,v 1.37 2017/05/28 10:37:26 benno Exp $ */ +/* $OpenBSD: proc.c,v 1.38 2018/09/09 21:06:51 bluhm Exp $ */ /* * Copyright (c) 2010 - 2016 Reyk Floeter @@ -29,13 +29,14 @@ #include #include #include +#include #include #include #include #include "httpd.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/relayd/proc.c b/usr.sbin/relayd/proc.c index 4577476b8af..55083dc29dd 100644 --- a/usr.sbin/relayd/proc.c +++ b/usr.sbin/relayd/proc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: proc.c,v 1.39 2017/05/28 10:39:15 benno Exp $ */ +/* $OpenBSD: proc.c,v 1.40 2018/09/09 21:06:51 bluhm Exp $ */ /* * Copyright (c) 2010 - 2016 Reyk Floeter @@ -29,13 +29,14 @@ #include #include #include +#include #include #include #include #include "relayd.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/relayd/relayd.c b/usr.sbin/relayd/relayd.c index ad6bce1c178..5781389f379 100644 --- a/usr.sbin/relayd/relayd.c +++ b/usr.sbin/relayd/relayd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: relayd.c,v 1.173 2018/09/01 18:09:14 bluhm Exp $ */ +/* $OpenBSD: relayd.c,v 1.174 2018/09/09 21:06:51 bluhm Exp $ */ /* * Copyright (c) 2007 - 2016 Reyk Floeter @@ -213,7 +213,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); log_procinit("parent"); if (!debug && daemon(1, 0) == -1) diff --git a/usr.sbin/relayd/relayd.h b/usr.sbin/relayd/relayd.h index ac43aa50608..fe55c3a8478 100644 --- a/usr.sbin/relayd/relayd.h +++ b/usr.sbin/relayd/relayd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: relayd.h,v 1.250 2018/08/06 17:31:31 benno Exp $ */ +/* $OpenBSD: relayd.h,v 1.251 2018/09/09 21:06:51 bluhm Exp $ */ /* * Copyright (c) 2006 - 2016 Reyk Floeter @@ -1383,7 +1383,7 @@ __dead void fatalx(const char *, ...) enum privsep_procid proc_getid(struct privsep_proc *, unsigned int, const char *); int proc_flush_imsg(struct privsep *, enum privsep_procid, int); -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 *); -- 2.20.1