chiark / gitweb /
udevd: move OOM disable into --daemon option
[elogind.git] / udev / udevd.c
index 8610c6c062123da98972667e66ca578afe17cc3a..dbb1e16004a54cea15206cbf3b7cd0fd42f5bf5f 100644 (file)
@@ -47,9 +47,6 @@
 #include "udev.h"
 #include "sd-daemon.h"
 
-#define UDEVD_PRIORITY                 -4
-#define UDEV_PRIORITY                  -2
-
 static bool debug;
 
 static void log_fn(struct udev *udev, int priority,
@@ -267,7 +264,6 @@ static void worker_new(struct event *event)
                close(worker_watch[READ_END]);
                udev_log_close();
                udev_log_init("udevd-work");
-               setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
 
                /* set signal handlers */
                memset(&act, 0x00, sizeof(act));
@@ -1106,7 +1102,6 @@ static int convert_db(struct udev *udev)
 int main(int argc, char *argv[])
 {
        struct udev *udev;
-       int fd;
        FILE *f;
        sigset_t mask;
        int daemonize = false;
@@ -1153,6 +1148,8 @@ int main(int argc, char *argv[])
                        if (udev_set_run_path(udev, filename) == NULL)
                                goto exit;
                        mkdir(udev_get_run_path(udev), 0755);
+                       err(udev, "error: runtime directory '%s' not writable, for now falling back to '%s'",
+                           udev_get_run_config_path(udev), udev_get_run_path(udev));
                }
        }
        /* relabel runtime dir only if it resides below /dev */
@@ -1263,15 +1260,22 @@ int main(int argc, char *argv[])
        static_dev_create_from_modules(udev);
 
        /* before opening new files, make sure std{in,out,err} fds are in a sane state */
-       fd = open("/dev/null", O_RDWR);
-       if (fd < 0) {
-               fprintf(stderr, "cannot open /dev/null\n");
-               err(udev, "cannot open /dev/null\n");
+       if (daemonize) {
+               int fd;
+
+               fd = open("/dev/null", O_RDWR);
+               if (fd >= 0) {
+                       if (write(STDOUT_FILENO, 0, 0) < 0)
+                               dup2(fd, STDOUT_FILENO);
+                       if (write(STDERR_FILENO, 0, 0) < 0)
+                               dup2(fd, STDERR_FILENO);
+                       if (fd > STDERR_FILENO)
+                               close(fd);
+               } else {
+                       fprintf(stderr, "cannot open /dev/null\n");
+                       err(udev, "cannot open /dev/null\n");
+               }
        }
-       if (write(STDOUT_FILENO, 0, 0) < 0)
-               dup2(fd, STDOUT_FILENO);
-       if (write(STDERR_FILENO, 0, 0) < 0)
-               dup2(fd, STDERR_FILENO);
 
        /* udevadm control socket */
        if (sd_listen_fds(true) == 1 && sd_is_socket(SD_LISTEN_FDS_START, AF_LOCAL, SOCK_SEQPACKET, -1))
@@ -1304,6 +1308,7 @@ int main(int argc, char *argv[])
 
        if (daemonize) {
                pid_t pid;
+               int fd;
 
                pid = fork();
                switch (pid) {
@@ -1317,10 +1322,46 @@ int main(int argc, char *argv[])
                        rc = 0;
                        goto exit;
                }
+
+               setsid();
+
+               fd = open("/proc/self/oom_score_adj", O_RDWR);
+               if (fd < 0) {
+                       /* Fallback to old interface */
+                       fd = open("/proc/self/oom_adj", O_RDWR);
+                       if (fd < 0) {
+                               err(udev, "error disabling OOM: %m\n");
+                       } else {
+                               /* OOM_DISABLE == -17 */
+                               write(fd, "-17", 3);
+                               close(fd);
+                       }
+               } else {
+                       write(fd, "-1000", 5);
+                       close(fd);
+               }
        } else {
                sd_notify(1, "READY=1");
        }
 
+       f = fopen("/dev/kmsg", "w");
+       if (f != NULL) {
+               fprintf(f, "<30>udev[%u]: starting version " VERSION "\n", getpid());
+               fclose(f);
+       }
+
+       if (!debug) {
+               int fd;
+
+               fd = open("/dev/null", O_RDWR);
+               if (fd >= 0) {
+                       dup2(fd, STDIN_FILENO);
+                       dup2(fd, STDOUT_FILENO);
+                       dup2(fd, STDERR_FILENO);
+                       close(fd);
+               }
+       }
+
        fd_inotify = udev_watch_init(udev);
        if (fd_inotify < 0) {
                fprintf(stderr, "error initializing inotify\n");
@@ -1416,41 +1457,6 @@ int main(int argc, char *argv[])
        /* if needed, convert old database from earlier udev version */
        convert_db(udev);
 
-       if (!debug) {
-               dup2(fd, STDIN_FILENO);
-               dup2(fd, STDOUT_FILENO);
-               dup2(fd, STDERR_FILENO);
-       }
-       if (fd > STDERR_FILENO)
-               close(fd);
-
-       /* set scheduling priority for the main daemon process */
-       setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
-
-       setsid();
-
-       f = fopen("/dev/kmsg", "w");
-       if (f != NULL) {
-               fprintf(f, "<30>udev[%u]: starting version " VERSION "\n", getpid());
-               fclose(f);
-       }
-
-       fd = open("/proc/self/oom_score_adj", O_RDWR);
-       if (fd < 0) {
-               /* Fallback to old interface */
-               fd = open("/proc/self/oom_adj", O_RDWR);
-               if (fd < 0) {
-                       err(udev, "error disabling OOM: %m\n");
-               } else {
-                       /* OOM_DISABLE == -17 */
-                       write(fd, "-17", 3);
-                       close(fd);
-               }
-       } else {
-               write(fd, "-1000", 5);
-               close(fd);
-       }
-
        if (children_max <= 0) {
                int memsize = mem_size_mb();