diff options
author | Brian Somers <brian@FreeBSD.org> | 2001-07-04 03:34:20 +0000 |
---|---|---|
committer | Brian Somers <brian@FreeBSD.org> | 2001-07-04 03:34:20 +0000 |
commit | 108e336ab5eba7e6ed11f8e93732f332d6e30baa (patch) | |
tree | 7a9a6d5a608388edca4eee1f442419c937608a1b /usr.sbin/ppp | |
parent | 9316aed2efe3332354772193bfc2b47232f7c5ef (diff) | |
download | src-108e336ab5eba7e6ed11f8e93732f332d6e30baa.tar.gz src-108e336ab5eba7e6ed11f8e93732f332d6e30baa.zip |
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.
MFC after: 1 week
Notes
Notes:
svn path=/head/; revision=79173
Diffstat (limited to 'usr.sbin/ppp')
-rw-r--r-- | usr.sbin/ppp/main.c | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/usr.sbin/ppp/main.c b/usr.sbin/ppp/main.c index c094e9a88824..968e6d261333 100644 --- a/usr.sbin/ppp/main.c +++ b/usr.sbin/ppp/main.c @@ -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); |