mirror of
https://github.com/systemd/systemd-stable.git
synced 2024-10-30 06:25:25 +03:00
bring std(in|out|err) fd's in a sane state
If the kernel forks us as an usermodhelper, we don't have any of the standard fd's and the first open() will start with fd=0. This is inherited to all forked childs and confuses later forked helpers where we want to read from a pipe connected to the helpers stdout/stderr. # ls -l /proc/$(pidof udevd)/fd total 6 dr-x------ 2 root root 0 2005-08-18 12:44 . dr-xr-xr-x 4 root root 0 2005-08-18 12:44 .. lrwx------ 1 root root 64 2005-08-18 12:44 0 -> /dev/null lrwx------ 1 root root 64 2005-08-18 12:44 1 -> socket:[1274617] lr-x------ 1 root root 64 2005-08-18 12:44 2 -> pipe:[1274618] l-wx------ 1 root root 64 2005-08-18 12:44 3 -> pipe:[1274618] lrwx------ 1 root root 64 2005-08-18 12:44 4 -> socket:[1274619] lrwx------ 1 root root 64 2005-08-18 12:44 5 -> socket:[1274620] Ouch! This will obviously not redirect sterr, it will kill the pipe we established between the parent and the child: devnull = open("/dev/null", O_RDWR); dup2(devnull, STDERR_FILENO); Signed-off-by: Kay Sievers <kay.sievers@suse.de>
This commit is contained in:
parent
e331c54dc5
commit
3f9f8de4de
16
udev.c
16
udev.c
@ -72,6 +72,7 @@ int main(int argc, char *argv[], char *envp[])
|
||||
const char *devpath;
|
||||
const char *subsystem;
|
||||
struct sigaction act;
|
||||
int devnull;
|
||||
int retval = -EINVAL;
|
||||
|
||||
if (argc == 2 && strcmp(argv[1], "-V") == 0) {
|
||||
@ -79,7 +80,22 @@ int main(int argc, char *argv[], char *envp[])
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/* set std fd's to /dev/null, if the kernel forks us, we don't have them at all */
|
||||
devnull = open("/dev/null", O_RDWR);
|
||||
if (devnull >= 0) {
|
||||
if (devnull != STDIN_FILENO)
|
||||
dup2(devnull, STDIN_FILENO);
|
||||
if (devnull != STDOUT_FILENO)
|
||||
dup2(devnull, STDOUT_FILENO);
|
||||
if (devnull != STDERR_FILENO)
|
||||
dup2(devnull, STDERR_FILENO);
|
||||
if (devnull > STDERR_FILENO)
|
||||
close(devnull);
|
||||
}
|
||||
|
||||
logging_init("udev");
|
||||
if (devnull < 0)
|
||||
err("fatal, could not open /dev/null");
|
||||
udev_init_config();
|
||||
dbg("version %s", UDEV_VERSION);
|
||||
|
||||
|
26
udevd.c
26
udevd.c
@ -777,7 +777,23 @@ int main(int argc, char *argv[], char *envp[])
|
||||
int daemonize = 0;
|
||||
int i;
|
||||
|
||||
/* set std fd's to /dev/null, if the kernel forks us, we don't have them at all */
|
||||
devnull = open("/dev/null", O_RDWR);
|
||||
if (devnull >= 0) {
|
||||
if (devnull != STDIN_FILENO)
|
||||
dup2(devnull, STDIN_FILENO);
|
||||
if (devnull != STDOUT_FILENO)
|
||||
dup2(devnull, STDOUT_FILENO);
|
||||
if (devnull != STDERR_FILENO)
|
||||
dup2(devnull, STDERR_FILENO);
|
||||
if (devnull > STDERR_FILENO)
|
||||
close(devnull);
|
||||
}
|
||||
|
||||
logging_init("udevd");
|
||||
if (devnull < 0)
|
||||
err("fatal, could not open /dev/null");
|
||||
|
||||
udev_init_config();
|
||||
dbg("version %s", UDEV_VERSION);
|
||||
|
||||
@ -825,16 +841,6 @@ int main(int argc, char *argv[], char *envp[])
|
||||
/* set a reasonable scheduling priority for the daemon */
|
||||
setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
|
||||
|
||||
/* Set fds to dev/null */
|
||||
devnull = open( "/dev/null", O_RDWR );
|
||||
if (devnull > 0) {
|
||||
dup2(devnull, STDIN_FILENO);
|
||||
dup2(devnull, STDOUT_FILENO);
|
||||
dup2(devnull, STDERR_FILENO);
|
||||
close(devnull);
|
||||
} else
|
||||
err("error opening /dev/null %s", strerror(errno));
|
||||
|
||||
/* setup signal handler pipe */
|
||||
retval = pipe(pipefds);
|
||||
if (retval < 0) {
|
||||
|
Loading…
Reference in New Issue
Block a user