diff options
author | brian <brian@cvs.openbsd.org> | 2001-07-04 03:33:42 +0000 |
---|---|---|
committer | brian <brian@cvs.openbsd.org> | 2001-07-04 03:33:42 +0000 |
commit | 50f93997dd232b14a90b7fae18ef1878e977423c (patch) | |
tree | ba501c1f8a47119d09cb8286d852dab510fb4838 | |
parent | f36e6dc435ec99d35c4b12b2c5f4cda0bbec9c66 (diff) |
Handle any of descriptors 0, 1 or 2 being closed when we're
envoked -- don't use them (as return values from open()), then
(say) close(STDIN_FILENO) when daemonising.
This is done by grabbing 3 descriptors to /dev/null at startup and
releasing them after we've daemonised.
-rw-r--r-- | usr.sbin/ppp/ppp/main.c | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/usr.sbin/ppp/ppp/main.c b/usr.sbin/ppp/ppp/main.c index 2f00ffe9ce5..f16becbb533 100644 --- a/usr.sbin/ppp/ppp/main.c +++ b/usr.sbin/ppp/ppp/main.c @@ -25,7 +25,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $OpenBSD: main.c,v 1.27 2001/06/13 21:33:41 brian Exp $ + * $OpenBSD: main.c,v 1.28 2001/07/04 03:33:41 brian Exp $ */ #include <sys/param.h> @@ -295,11 +295,24 @@ main(int argc, char **argv) { char *name; const char *lastlabel; - int label, arg; + int arg, f, holdfd[3], label; struct bundle *bundle; struct prompt *prompt; struct switches sw; + /* + * We open 3 descriptors to ensure that STDIN_FILENO, STDOUT_FILENO and + * STDERR_FILENO are always open. These are closed before DoLoop(), + * but *after* we've avoided the possibility of erroneously closing + * an important descriptor with close(STD{IN,OUT,ERR}_FILENO). + */ + if ((holdfd[0] = open(_PATH_DEVNULL, O_RDWR)) == -1) { + fprintf(stderr, "Cannot open %s !\n", _PATH_DEVNULL); + return 2; + } + for (f = 1; f < sizeof holdfd / sizeof *holdfd; f++) + dup2(holdfd[0], holdfd[f]); + name = strrchr(argv[0], '/'); log_Open(name ? name + 1 : argv[0]); @@ -498,6 +511,10 @@ main(int argc, char **argv) prompt_Required(prompt); } + /* We can get rid of these now */ + for (f = 0; f < sizeof holdfd / sizeof *holdfd; f++) + close(holdfd[f]); + log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(sw.mode)); DoLoop(bundle); AbortProgram(EX_NORMAL); |