+
+int main(int argc, char *argv[], char *envp[])
+{
+ int maxsockplus;
+ int retval;
+ int fd;
+ struct sigaction act;
+ fd_set readfds;
+ const char *value;
+ int uevent_netlink_active = 0;
+ int daemonize = 0;
+ int i;
+
+ logging_init("udevd");
+ udev_init_config();
+ dbg("version %s", UDEV_VERSION);
+
+ if (getuid() != 0) {
+ err("need to be root, exit");
+ goto exit;
+ }
+
+ for (i = 1 ; i < argc; i++) {
+ char *arg = argv[i];
+ if (strcmp(arg, "--daemon") == 0 || strcmp(arg, "-d") == 0) {
+ info("will daemonize");
+ daemonize = 1;
+ }
+ if (strcmp(arg, "--stop-exec-queue") == 0) {
+ info("will not execute events until START_EXEC_QUEUE is received");
+ stop_exec_q = 1;
+ }
+ }
+ if (daemonize) {
+ pid_t pid;
+
+ pid = fork();
+ switch (pid) {
+ case 0:
+ dbg("damonized fork running");
+ break;
+ case -1:
+ err("fork of daemon failed");
+ goto exit;
+ default:
+ logging_close();
+ exit(0);
+ }
+ }
+
+ /* become session leader */
+ sid = setsid();
+ dbg("our session is %d", sid);
+
+ chdir("/");
+ umask(umask(077) | 022);
+
+ /*set a reasonable scheduling priority for the daemon */
+ setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
+
+ /* Set fds to dev/null */
+ fd = open( "/dev/null", O_RDWR );
+ if (fd >= 0) {
+ dup2(fd, 0);
+ dup2(fd, 1);
+ dup2(fd, 2);
+ if (fd > 2)
+ close(fd);
+ } else
+ err("error opening /dev/null %s", strerror(errno));
+
+ /* setup signal handler pipe */
+ retval = pipe(pipefds);
+ if (retval < 0) {
+ err("error getting pipes: %s", strerror(errno));
+ goto exit;
+ }
+
+ retval = fcntl(pipefds[0], F_SETFL, O_NONBLOCK);
+ if (retval < 0) {
+ err("error fcntl on read pipe: %s", strerror(errno));
+ goto exit;
+ }
+ retval = fcntl(pipefds[0], F_SETFD, FD_CLOEXEC);
+ if (retval < 0)
+ err("error fcntl on read pipe: %s", strerror(errno));
+
+ retval = fcntl(pipefds[1], F_SETFL, O_NONBLOCK);
+ if (retval < 0) {
+ err("error fcntl on write pipe: %s", strerror(errno));
+ goto exit;
+ }
+ retval = fcntl(pipefds[1], F_SETFD, FD_CLOEXEC);
+ if (retval < 0)
+ err("error fcntl on write pipe: %s", strerror(errno));
+
+ /* set signal handlers */
+ memset(&act, 0x00, sizeof(struct sigaction));
+ act.sa_handler = (void (*)(int)) sig_handler;
+ sigemptyset(&act.sa_mask);
+ act.sa_flags = SA_RESTART;
+ sigaction(SIGINT, &act, NULL);
+ sigaction(SIGTERM, &act, NULL);
+ sigaction(SIGALRM, &act, NULL);
+ sigaction(SIGCHLD, &act, NULL);
+
+ if (init_uevent_netlink_sock() < 0) {
+ dbg("uevent socket not available");
+ }
+
+ if (init_udevd_socket() < 0) {
+ if (errno == EADDRINUSE)
+ dbg("another udevd running, exit");
+ else
+ dbg("error initialising udevd socket: %s", strerror(errno));
+
+ goto exit;
+ }
+
+ /* override of forked udev binary, used for testing */
+ udev_bin = getenv("UDEV_BIN");
+ if (udev_bin != NULL)
+ info("udev binary is set to '%s'", udev_bin);
+ else
+ udev_bin = UDEV_BIN;
+
+ /* init of expected_seqnum value */
+ value = getenv("UDEVD_EXPECTED_SEQNUM");
+ if (value) {
+ expected_seqnum = strtoull(value, NULL, 10);
+ info("initialize expected_seqnum to %llu", expected_seqnum);
+ }
+
+ /* timeout to wait for missing events */
+ value = getenv("UDEVD_EVENT_TIMEOUT");
+ if (value)
+ event_timeout = strtoul(value, NULL, 10);
+ else
+ event_timeout = UDEVD_EVENT_TIMEOUT;
+ info("initialize event_timeout to %u", event_timeout);
+
+ /* maximum limit of forked childs */
+ value = getenv("UDEVD_MAX_CHILDS");
+ if (value)
+ max_childs = strtoul(value, NULL, 10);
+ else
+ max_childs = UDEVD_MAX_CHILDS;
+ info("initialize max_childs to %u", max_childs);
+
+ /* start to throttle forking if maximum number of _running_ childs is reached */
+ value = getenv("UDEVD_MAX_CHILDS_RUNNING");
+ if (value)
+ max_childs_running = strtoull(value, NULL, 10);
+ else
+ max_childs_running = UDEVD_MAX_CHILDS_RUNNING;
+ info("initialize max_childs_running to %u", max_childs_running);
+
+ FD_ZERO(&readfds);
+ FD_SET(udevd_sock, &readfds);
+ if (uevent_netlink_sock != -1)
+ FD_SET(uevent_netlink_sock, &readfds);
+ FD_SET(pipefds[0], &readfds);
+ maxsockplus = udevd_sock+1;
+ while (1) {
+ struct uevent_msg *msg;
+
+ fd_set workreadfds = readfds;
+ retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
+
+ if (retval < 0) {
+ if (errno != EINTR)
+ dbg("error in select: %s", strerror(errno));
+ continue;
+ }
+
+ if (FD_ISSET(udevd_sock, &workreadfds)) {
+ msg = get_udevd_msg();
+ if (msg) {
+ /* discard kernel messages if netlink is active */
+ if (uevent_netlink_active && msg->type == UDEVD_UEVENT_UDEVSEND && msg->seqnum != 0) {
+ dbg("skip uevent_helper message, netlink is active");
+ free(msg);
+ continue;
+ }
+ msg_queue_insert(msg);
+ }
+ }
+
+ if (FD_ISSET(uevent_netlink_sock, &workreadfds)) {
+ msg = get_netlink_msg();
+ if (msg) {
+ msg_queue_insert(msg);
+ /* disable udevsend with first netlink message */
+ if (!uevent_netlink_active) {
+ info("uevent_nl message received, disable udevsend messages");
+ uevent_netlink_active = 1;
+ }
+ }
+ }
+
+ if (FD_ISSET(pipefds[0], &workreadfds))
+ user_sighandler();
+
+ if (sigchilds_waiting) {
+ sigchilds_waiting = 0;
+ reap_sigchilds();
+ }
+
+ if (run_msg_q) {
+ run_msg_q = 0;
+ msg_queue_manager();
+ }
+
+ if (run_exec_q) {
+ /* clean up running_list before calling exec_queue_manager() */
+ if (sigchilds_waiting) {
+ sigchilds_waiting = 0;
+ reap_sigchilds();
+ }
+
+ run_exec_q = 0;
+ if (!stop_exec_q)
+ exec_queue_manager();
+ }
+ }
+
+exit:
+ logging_close();
+ return 1;
+}