chiark / gitweb /
udev: static nodes - fix default permissions if no rules is given
[elogind.git] / src / udev / udevd.c
index 0d85960e63c71ce6c2191286245f071fd73d3fc5..393e2a920c889f491b6f31ece0f10ddb29b46f7e 100644 (file)
@@ -262,6 +262,9 @@ static void worker_new(struct event *event)
                 /* request TERM signal if parent exits */
                 prctl(PR_SET_PDEATHSIG, SIGTERM);
 
+                /* reset OOM score, we only protect the main daemon */
+                write_one_line_file("/proc/self/oom_score_adj", "0");
+
                 for (;;) {
                         struct udev_event *udev_event;
                         struct worker_message msg;
@@ -803,7 +806,7 @@ static void static_dev_create_from_modules(struct udev *udev)
         FILE *f;
 
         uname(&kernel);
-        util_strscpyl(modules, sizeof(modules), "/lib/modules/", kernel.release, "/modules.devname", NULL);
+        util_strscpyl(modules, sizeof(modules), ROOTPREFIX "/lib/modules/", kernel.release, "/modules.devname", NULL);
         f = fopen(modules, "r");
         if (f == NULL)
                 return;
@@ -842,15 +845,16 @@ static void static_dev_create_from_modules(struct udev *udev)
                 if (sscanf(devno, "%c%u:%u", &type, &maj, &min) != 3)
                         continue;
 
+                mode  = 0600;
                 if (type == 'c')
-                        mode = S_IFCHR;
+                        mode |= S_IFCHR;
                 else if (type == 'b')
-                        mode = S_IFBLK;
+                        mode |= S_IFBLK;
                 else
                         continue;
 
                 util_strscpyl(filename, sizeof(filename), "/dev/", devname, NULL);
-                mkdir_parents(filename, 0755);
+                mkdir_parents_label(filename, 0755);
                 label_context_set(filename, mode);
                 log_debug("mknod '%s' %c%u:%u\n", filename, type, maj, min);
                 if (mknod(filename, mode, makedev(maj, min)) < 0 && errno == EEXIST)
@@ -896,8 +900,7 @@ static int convert_db(struct udev *udev)
                 return 0;
 
         /* make sure we do not get here again */
-        mkdir_parents("/run/udev/data", 0755);
-        mkdir(filename, 0755);
+        mkdir_p("/run/udev/data", 0755);
 
         /* old database */
         util_strscpyl(filename, sizeof(filename), "/dev/.udev/db", NULL);
@@ -906,7 +909,7 @@ static int convert_db(struct udev *udev)
 
         f = fopen("/dev/kmsg", "w");
         if (f != NULL) {
-                fprintf(f, "<30>udevd[%u]: converting old udev database\n", getpid());
+                fprintf(f, "<30>systemd-udevd[%u]: converting old udev database\n", getpid());
                 fclose(f);
         }
 
@@ -1010,6 +1013,48 @@ static int systemd_fds(struct udev *udev, int *rctrl, int *rnetlink)
         return 0;
 }
 
+/*
+ * read the kernel commandline, in case we need to get into debug mode
+ *   udev.log-priority=<level>              syslog priority
+ *   udev.children-max=<number of workers>  events are fully serialized if set to 1
+ *   udev.exec-delay=<number of seconds>    delay execution of every executed program
+ */
+static void kernel_cmdline_options(struct udev *udev)
+{
+        char *line, *w, *state;
+        size_t l;
+
+        if (read_one_line_file("/proc/cmdline", &line) < 0)
+                return;
+
+        FOREACH_WORD_QUOTED(w, l, line, state) {
+                char *s, *opt;
+
+                s = strndup(w, l);
+                if (!s)
+                        break;
+
+                /* accept the same options for the initrd, prefixed with "rd." */
+                if (in_initrd() && startswith(s, "rd."))
+                        opt = s + 3;
+                else
+                        opt = s;
+
+                if (startswith(opt, "udev.log-priority="))
+                        udev_set_log_priority(udev, util_log_priority(opt + 18));
+
+                if (startswith(opt, "udev.children-max="))
+                        children_max = strtoul(opt + 18, NULL, 0);
+
+                if (startswith(opt, "udev.exec-delay="))
+                        exec_delay = strtoul(opt + 16, NULL, 0);
+
+                free(s);
+        }
+
+        free(line);
+}
+
 int main(int argc, char *argv[])
 {
         struct udev *udev;
@@ -1098,39 +1143,7 @@ int main(int argc, char *argv[])
                 }
         }
 
-        /*
-         * read the kernel commandline, in case we need to get into debug mode
-         *   udev.log-priority=<level>              syslog priority
-         *   udev.children-max=<number of workers>  events are fully serialized if set to 1
-         *
-         */
-        f = fopen("/proc/cmdline", "r");
-        if (f != NULL) {
-                char cmdline[4096];
-
-                if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
-                        char *pos;
-
-                        pos = strstr(cmdline, "udev.log-priority=");
-                        if (pos != NULL) {
-                                pos += strlen("udev.log-priority=");
-                                udev_set_log_priority(udev, util_log_priority(pos));
-                        }
-
-                        pos = strstr(cmdline, "udev.children-max=");
-                        if (pos != NULL) {
-                                pos += strlen("udev.children-max=");
-                                children_max = strtoul(pos, NULL, 0);
-                        }
-
-                        pos = strstr(cmdline, "udev.exec-delay=");
-                        if (pos != NULL) {
-                                pos += strlen("udev.exec-delay=");
-                                exec_delay = strtoul(pos, NULL, 0);
-                        }
-                }
-                fclose(f);
-        }
+        kernel_cmdline_options(udev);
 
         if (getuid() != 0) {
                 fprintf(stderr, "root privileges required\n");
@@ -1230,7 +1243,6 @@ int main(int argc, char *argv[])
 
         if (daemonize) {
                 pid_t pid;
-                int fd;
 
                 pid = fork();
                 switch (pid) {
@@ -1247,18 +1259,14 @@ int main(int argc, char *argv[])
 
                 setsid();
 
-                fd = open("/proc/self/oom_score_adj", O_RDWR|O_CLOEXEC);
-                if (fd >= 0) {
-                        write(fd, "-1000", 5);
-                        close(fd);
-                }
+                write_one_line_file("/proc/self/oom_score_adj", "-1000");
         } else {
                 sd_notify(1, "READY=1");
         }
 
         f = fopen("/dev/kmsg", "w");
         if (f != NULL) {
-                fprintf(f, "<30>udevd[%u]: starting version " VERSION "\n", getpid());
+                fprintf(f, "<30>systemd-udevd[%u]: starting version " VERSION "\n", getpid());
                 fclose(f);
         }
 
@@ -1353,9 +1361,9 @@ int main(int argc, char *argv[])
 
                 /* set value depending on the amount of RAM */
                 if (memsize > 0)
-                        children_max = 128 + (memsize / 8);
+                        children_max = 16 + (memsize / 8);
                 else
-                        children_max = 128;
+                        children_max = 16;
         }
         log_debug("set children_max to %u\n", children_max);