summaryrefslogtreecommitdiff
path: root/udevd.c
diff options
context:
space:
mode:
authorMatthias Schwarzott <zzam@gentoo.org>2007-03-13 22:27:21 +0100
committerKay Sievers <kay.sievers@vrfy.org>2007-03-13 22:27:21 +0100
commit5edec024b1ed7bbdf73095791c13df324038c31d (patch)
tree364b87755b639ccd101b21890570f6d678da3f42 /udevd.c
parentd7eeab1194a370c23405eecf2074f4b25d7a7ce0 (diff)
udevd: cleanup std{in,our,err} on startup
It occurs, when root-partition has no /dev/console, meaning that kernel could not open it, and such udevd is started without open filedescriptors 0 1 2. In that case udevd openes its sockets (netlink and control). They get fds between 0 and 2. Later duping /dev/null to 0 1 2 closes the sockets and replaces them with /dev/null. The error condition can also be reproduced by starting udevd with this command-line: udevd --daemon <&- >&- 2>&-
Diffstat (limited to 'udevd.c')
-rw-r--r--udevd.c30
1 files changed, 19 insertions, 11 deletions
diff --git a/udevd.c b/udevd.c
index 23f5fd6980..961ceb587a 100644
--- a/udevd.c
+++ b/udevd.c
@@ -989,6 +989,19 @@ int main(int argc, char *argv[], char *envp[])
goto exit;
}
+ /* make sure std{in,out,err} fd's are in a sane state */
+ fd = open("/dev/null", O_RDWR);
+ if (fd < 0) {
+ fprintf(stderr, "cannot open /dev/null\n");
+ err("cannot open /dev/null");
+ }
+ if (fd > STDIN_FILENO)
+ dup2(fd, STDIN_FILENO);
+ if (write(STDOUT_FILENO, 0, 0) < 0)
+ dup2(fd, STDOUT_FILENO);
+ if (write(STDERR_FILENO, 0, 0) < 0)
+ dup2(fd, STDERR_FILENO);
+
/* init sockets to receive events */
if (init_udevd_socket() < 0) {
if (errno == EADDRINUSE) {
@@ -1064,17 +1077,12 @@ int main(int argc, char *argv[], char *envp[])
}
}
- /* redirect std fd's */
- fd = open("/dev/null", O_RDWR);
- if (fd >= 0) {
- dup2(fd, STDIN_FILENO);
- if (!verbose)
- dup2(fd, STDOUT_FILENO);
- dup2(fd, STDERR_FILENO);
- if (fd > STDERR_FILENO)
- close(fd);
- } else
- err("error opening /dev/null: %s", strerror(errno));
+ /* redirect std{out,err} fd's */
+ if (!verbose)
+ dup2(fd, STDOUT_FILENO);
+ dup2(fd, STDERR_FILENO);
+ if (fd > STDERR_FILENO)
+ close(fd);
/* set scheduling priority for the daemon */
setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);