+ /* init sockets to receive events */
+ if (init_udevd_socket() < 0) {
+ if (errno == EADDRINUSE) {
+ fprintf(stderr, "another udev daemon already running\n");
+ err("another udev daemon already running");
+ rc = 1;
+ } else {
+ fprintf(stderr, "error initializing udevd socket\n");
+ err("error initializing udevd socket");
+ rc = 2;
+ }
+ goto exit;
+ }
+
+ if (init_uevent_netlink_sock() < 0) {
+ fprintf(stderr, "error initializing netlink socket\n");
+ err("error initializing netlink socket");
+ rc = 3;
+ goto exit;
+ }
+
+ /* setup signal handler pipe */
+ retval = pipe(signal_pipe);
+ if (retval < 0) {
+ err("error getting pipes: %s", strerror(errno));
+ goto exit;
+ }
+
+ retval = fcntl(signal_pipe[READ_END], F_GETFL, 0);
+ if (retval < 0) {
+ err("error fcntl on read pipe: %s", strerror(errno));
+ goto exit;
+ }
+ retval = fcntl(signal_pipe[READ_END], F_SETFL, retval | O_NONBLOCK);
+ if (retval < 0) {
+ err("error fcntl on read pipe: %s", strerror(errno));
+ goto exit;
+ }
+
+ retval = fcntl(signal_pipe[WRITE_END], F_GETFL, 0);
+ if (retval < 0) {
+ err("error fcntl on write pipe: %s", strerror(errno));
+ goto exit;
+ }
+ retval = fcntl(signal_pipe[WRITE_END], F_SETFL, retval | O_NONBLOCK);
+ if (retval < 0) {
+ err("error fcntl on write pipe: %s", strerror(errno));
+ goto exit;
+ }
+
+ /* parse the rules and keep them in memory */
+ sysfs_init();
+ udev_rules_init(&rules, 1);
+
+ export_initial_seqnum();
+
+ if (daemonize) {
+ pid_t pid;
+
+ pid = fork();
+ switch (pid) {
+ case 0:
+ dbg("daemonized fork running");
+ break;
+ case -1:
+ err("fork of daemon failed: %s", strerror(errno));
+ rc = 4;
+ goto exit;
+ default:
+ dbg("child [%u] running, parent exits", pid);
+ rc = 0;
+ goto exit;
+ }
+ }
+
+ /* 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));
+
+ /* set scheduling priority for the daemon */
+ setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
+
+ chdir("/");
+ umask(022);
+
+ /* become session leader */
+ sid = setsid();
+ dbg("our session is %d", sid);
+
+ /* OOM_DISABLE == -17 */
+ fd = open("/proc/self/oom_adj", O_RDWR);
+ if (fd < 0)
+ err("error disabling OOM: %s", strerror(errno));
+ else {
+ write(fd, "-17", 3);
+ close(fd);
+ }
+
+ /* set signal handlers */
+ memset(&act, 0x00, sizeof(struct sigaction));
+ act.sa_handler = (void (*)(int)) sig_handler;
+ sigemptyset(&act.sa_mask);