]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/udev/udevd.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / udev / udevd.c
index 0b05913a7863ac4f647483f62ffe22caa141bdb5..a3f0298ae7da03de27fb1eab3282d885f24d1c06 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <stddef.h>
-#include <signal.h>
-#include <unistd.h>
 #include <errno.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <stdbool.h>
 #include <string.h>
-#include <fcntl.h>
-#include <getopt.h>
+#include <sys/epoll.h>
 #include <sys/file.h>
-#include <sys/time.h>
+#include <sys/inotify.h>
+#include <sys/ioctl.h>
+#include <sys/mount.h>
 #include <sys/prctl.h>
-#include <sys/socket.h>
 #include <sys/signalfd.h>
-#include <sys/epoll.h>
-#include <sys/mount.h>
-#include <sys/wait.h>
+#include <sys/socket.h>
 #include <sys/stat.h>
-#include <sys/ioctl.h>
-#include <sys/inotify.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <unistd.h>
 
 #include "sd-daemon.h"
 #include "sd-event.h"
 
-#include "terminal-util.h"
-#include "signal-util.h"
-#include "event-util.h"
-#include "netlink-util.h"
 #include "cgroup-util.h"
-#include "process-util.h"
+#include "cpu-set-util.h"
 #include "dev-setup.h"
+#include "event-util.h"
 #include "fileio.h"
-#include "selinux-util.h"
-#include "udev.h"
-#include "udev-util.h"
 #include "formats-util.h"
 #include "hashmap.h"
+#include "netlink-util.h"
+#include "process-util.h"
+#include "selinux-util.h"
+#include "signal-util.h"
+#include "string-util.h"
+#include "terminal-util.h"
+#include "udev-util.h"
+#include "udev.h"
 
 static bool arg_debug = false;
 static int arg_daemonize = false;
@@ -261,7 +263,6 @@ static int on_event_timeout_warning(sd_event_source *s, uint64_t usec, void *use
 static void worker_attach_event(struct worker *worker, struct event *event) {
         sd_event *e;
         uint64_t usec;
-        int r;
 
         assert(worker);
         assert(worker->manager);
@@ -276,9 +277,7 @@ static void worker_attach_event(struct worker *worker, struct event *event) {
 
         e = worker->manager->event;
 
-        r = sd_event_now(e, clock_boottime_or_monotonic(), &usec);
-        if (r < 0)
-                return;
+        assert_se(sd_event_now(e, clock_boottime_or_monotonic(), &usec) >= 0);
 
         (void) sd_event_add_time(e, &event->timeout_warning, clock_boottime_or_monotonic(),
                                  usec + arg_event_timeout_warn_usec, USEC_PER_SEC, on_event_timeout_warning, event);
@@ -398,7 +397,7 @@ static void worker_spawn(Manager *manager, struct event *event) {
                 prctl(PR_SET_PDEATHSIG, SIGTERM);
 
                 /* reset OOM score, we only protect the main daemon */
-                write_string_file("/proc/self/oom_score_adj", "0");
+                write_string_file("/proc/self/oom_score_adj", "0", 0);
 
                 for (;;) {
                         struct udev_event *udev_event;
@@ -749,9 +748,7 @@ static void manager_exit(Manager *manager) {
         event_queue_cleanup(manager, EVENT_QUEUED);
         manager_kill_workers(manager);
 
-        r = sd_event_now(manager->event, clock_boottime_or_monotonic(), &usec);
-        if (r < 0)
-                return;
+        assert_se(sd_event_now(manager->event, clock_boottime_or_monotonic(), &usec) >= 0);
 
         r = sd_event_add_time(manager->event, NULL, clock_boottime_or_monotonic(),
                               usec + 30 * USEC_PER_SEC, USEC_PER_SEC, on_exit_timeout, manager);
@@ -780,7 +777,6 @@ static void manager_reload(Manager *manager) {
 static void event_queue_start(Manager *manager) {
         struct udev_list_node *loop;
         usec_t usec;
-        int r;
 
         assert(manager);
 
@@ -788,17 +784,15 @@ static void event_queue_start(Manager *manager) {
             manager->exit || manager->stop_exec_queue)
                 return;
 
-        r = sd_event_now(manager->event, clock_boottime_or_monotonic(), &usec);
-        if (r >= 0) {
-                /* check for changed config, every 3 seconds at most */
-                if (manager->last_usec == 0 ||
-                    (usec - manager->last_usec) > 3 * USEC_PER_SEC) {
-                        if (udev_rules_check_timestamp(manager->rules) ||
-                            udev_builtin_validate(manager->udev))
-                                manager_reload(manager);
+        assert_se(sd_event_now(manager->event, clock_boottime_or_monotonic(), &usec) >= 0);
+        /* check for changed config, every 3 seconds at most */
+        if (manager->last_usec == 0 ||
+            (usec - manager->last_usec) > 3 * USEC_PER_SEC) {
+                if (udev_rules_check_timestamp(manager->rules) ||
+                    udev_builtin_validate(manager->udev))
+                        manager_reload(manager);
 
-                        manager->last_usec = usec;
-                }
+                manager->last_usec = usec;
         }
 
         udev_builtin_init(manager->udev);
@@ -1091,7 +1085,7 @@ static int synthesize_change(struct udev_device *dev) {
                  */
                 log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
                 strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
-                write_string_file(filename, "change");
+                write_string_file(filename, "change", WRITE_STRING_FILE_CREATE);
 
                 udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) {
                         _cleanup_udev_device_unref_ struct udev_device *d = NULL;
@@ -1106,7 +1100,7 @@ static int synthesize_change(struct udev_device *dev) {
                         log_debug("device %s closed, synthesising partition '%s' 'change'",
                                   udev_device_get_devnode(dev), udev_device_get_devnode(d));
                         strscpyl(filename, sizeof(filename), udev_device_get_syspath(d), "/uevent", NULL);
-                        write_string_file(filename, "change");
+                        write_string_file(filename, "change", WRITE_STRING_FILE_CREATE);
                 }
 
                 return 0;
@@ -1114,7 +1108,7 @@ static int synthesize_change(struct udev_device *dev) {
 
         log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
         strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
-        write_string_file(filename, "change");
+        write_string_file(filename, "change", WRITE_STRING_FILE_CREATE);
 
         return 0;
 }
@@ -1358,6 +1352,7 @@ static int listen_fds(int *rctrl, int *rnetlink) {
  *   udev.event-timeout=<number of seconds>    seconds to wait before terminating an event
  */
 static int parse_proc_cmdline_item(const char *key, const char *value) {
+        const char *full_key = key;
         int r;
 
         assert(key);
@@ -1377,25 +1372,28 @@ static int parse_proc_cmdline_item(const char *key, const char *value) {
                 int prio;
 
                 prio = util_log_priority(value);
+                if (prio < 0)
+                        goto invalid;
                 log_set_max_level(prio);
         } else if (streq(key, "children-max")) {
                 r = safe_atou(value, &arg_children_max);
                 if (r < 0)
-                        log_warning("invalid udev.children-max ignored: %s", value);
+                        goto invalid;
         } else if (streq(key, "exec-delay")) {
                 r = safe_atoi(value, &arg_exec_delay);
                 if (r < 0)
-                        log_warning("invalid udev.exec-delay ignored: %s", value);
+                        goto invalid;
         } else if (streq(key, "event-timeout")) {
                 r = safe_atou64(value, &arg_event_timeout_usec);
                 if (r < 0)
-                        log_warning("invalid udev.event-timeout ignored: %s", value);
-                else {
-                        arg_event_timeout_usec *= USEC_PER_SEC;
-                        arg_event_timeout_warn_usec = (arg_event_timeout_usec / 3) ? : 1;
-                }
+                        goto invalid;
+                arg_event_timeout_usec *= USEC_PER_SEC;
+                arg_event_timeout_warn_usec = (arg_event_timeout_usec / 3) ? : 1;
         }
 
+        return 0;
+invalid:
+        log_warning("invalid %s ignored: %s", full_key, value);
         return 0;
 }
 
@@ -1432,7 +1430,7 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "c:de:DtN:hV", options, NULL)) >= 0) {
+        while ((c = getopt_long(argc, argv, "c:de:Dt:N:hV", options, NULL)) >= 0) {
                 int r;
 
                 switch (c) {
@@ -1659,8 +1657,10 @@ int main(int argc, char *argv[]) {
         if (r < 0)
                 log_warning_errno(r, "failed to parse kernel command line, ignoring: %m");
 
-        if (arg_debug)
+        if (arg_debug) {
+                log_set_target(LOG_TARGET_CONSOLE);
                 log_set_max_level(LOG_DEBUG);
+        }
 
         if (getuid() != 0) {
                 r = log_error_errno(EPERM, "root privileges required");
@@ -1672,9 +1672,8 @@ int main(int argc, char *argv[]) {
 
                 arg_children_max = 8;
 
-                if (sched_getaffinity(0, sizeof (cpu_set), &cpu_set) == 0) {
+                if (sched_getaffinity(0, sizeof(cpu_set), &cpu_set) == 0)
                         arg_children_max += CPU_COUNT(&cpu_set) * 2;
-                }
 
                 log_debug("set children_max to %u", arg_children_max);
         }
@@ -1708,7 +1707,7 @@ int main(int argc, char *argv[]) {
                    by PID1. otherwise we are not guaranteed to have a dedicated cgroup */
                 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &cgroup);
                 if (r < 0) {
-                        if (r == -ENOENT)
+                        if (r == -ENOENT || r == -ENOEXEC)
                                 log_debug_errno(r, "did not find dedicated cgroup: %m");
                         else
                                 log_warning_errno(r, "failed to get cgroup: %m");
@@ -1745,7 +1744,7 @@ int main(int argc, char *argv[]) {
 
                 setsid();
 
-                write_string_file("/proc/self/oom_score_adj", "-1000");
+                write_string_file("/proc/self/oom_score_adj", "-1000", 0);
         }
 
         r = run(fd_ctrl, fd_uevent, cgroup);