mirror of
https://git.FreeBSD.org/src.git
synced 2025-01-19 15:33:56 +00:00
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
This commit is contained in:
parent
9316aed2ef
commit
108e336ab5
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=79173
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user