chiark / gitweb /
dbus-timer: fix bus_timer_vtable to have the correct times
[elogind.git] / src / core / path.c
index 3936971b41a63c5325fbe7ec62da05e15144d164..1d6c6cc6cb6217a4ae6feda0dcc056f74a37f582 100644 (file)
 #include "mkdir.h"
 #include "dbus-path.h"
 #include "special.h"
-#include "bus-errors.h"
 #include "path-util.h"
+#include "macro.h"
+#include "bus-util.h"
+#include "bus-error.h"
 
 static const UnitActiveState state_translation_table[_PATH_STATE_MAX] = {
         [PATH_DEAD] = UNIT_INACTIVE,
@@ -41,7 +43,9 @@ static const UnitActiveState state_translation_table[_PATH_STATE_MAX] = {
         [PATH_FAILED] = UNIT_FAILED
 };
 
-int path_spec_watch(PathSpec *s, Unit *u) {
+static int path_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
+
+int path_spec_watch(PathSpec *s, sd_event_io_handler_t handler) {
 
         static const int flags_table[_PATH_TYPE_MAX] = {
                 [PATH_EXISTS] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB,
@@ -52,101 +56,136 @@ int path_spec_watch(PathSpec *s, Unit *u) {
         };
 
         bool exists = false;
-        char *k, *slash;
+        char *slash, *oldslash = NULL;
         int r;
 
-        assert(u);
         assert(s);
+        assert(s->unit);
+        assert(handler);
 
-        path_spec_unwatch(s, u);
-
-        if (!(k = strdup(s->path)))
-                return -ENOMEM;
+        path_spec_unwatch(s);
 
-        if ((s->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC)) < 0) {
+        s->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
+        if (s->inotify_fd < 0) {
                 r = -errno;
                 goto fail;
         }
 
-        if (unit_watch_fd(u, s->inotify_fd, EPOLLIN, &s->watch) < 0) {
-                r = -errno;
+        r = sd_event_add_io(s->unit->manager->event, &s->event_source, s->inotify_fd, EPOLLIN, handler, s);
+        if (r < 0)
                 goto fail;
-        }
 
-        s->primary_wd = inotify_add_watch(s->inotify_fd, k, flags_table[s->type]);
-        if (s->primary_wd >= 0)
-                exists = true;
+        /* This assumes the path was passed through path_kill_slashes()! */
 
-        do {
+        for (slash = strchr(s->path, '/'); ; slash = strchr(slash+1, '/')) {
+                char *cut = NULL;
                 int flags;
+                char tmp;
+
+                if (slash) {
+                        cut = slash + (slash == s->path);
+                        tmp = *cut;
+                        *cut = '\0';
+
+                        flags = IN_MOVE_SELF | IN_DELETE_SELF | IN_ATTRIB | IN_CREATE | IN_MOVED_TO;
+                } else
+                        flags = flags_table[s->type];
+
+                r = inotify_add_watch(s->inotify_fd, s->path, flags);
+                if (r < 0) {
+                        if (errno == EACCES || errno == ENOENT) {
+                                if (cut)
+                                        *cut = tmp;
+                                break;
+                        }
+
+                        log_warning("Failed to add watch on %s: %m", s->path);
+                        r = -errno;
+                        if (cut)
+                                *cut = tmp;
+                        goto fail;
+                } else {
+                        exists = true;
 
-                /* This assumes the path was passed through path_kill_slashes()! */
-                slash = strrchr(k, '/');
-                if (!slash)
-                        break;
+                        /* Path exists, we don't need to watch parent
+                           too closely. */
+                        if (oldslash) {
+                                char *cut2 = oldslash + (oldslash == s->path);
+                                char tmp2 = *cut2;
+                                *cut2 = '\0';
 
-                /* Trim the path at the last slash. Keep the slash if it's the root dir. */
-                slash[slash == k] = 0;
+                                inotify_add_watch(s->inotify_fd, s->path, IN_MOVE_SELF);
+                                /* Error is ignored, the worst can happen is
+                                   we get spurious events. */
 
-                flags = IN_MOVE_SELF;
-                if (!exists)
-                        flags |= IN_DELETE_SELF | IN_ATTRIB | IN_CREATE | IN_MOVED_TO;
+                                *cut2 = tmp2;
+                        }
+                }
 
-                if (inotify_add_watch(s->inotify_fd, k, flags) >= 0)
-                        exists = true;
-        } while (slash != k);
+                if (cut)
+                        *cut = tmp;
+
+                if (slash)
+                        oldslash = slash;
+                else {
+                        /* whole path has been iterated over */
+                        s->primary_wd = r;
+                        break;
+                }
+        }
+
+        if (!exists) {
+                log_error("Failed to add watch on any of the components of %s: %m",
+                          s->path);
+                r = -errno; /* either EACCESS or ENOENT */
+                goto fail;
+        }
 
         return 0;
 
 fail:
-        free(k);
-
-        path_spec_unwatch(s, u);
+        path_spec_unwatch(s);
         return r;
 }
 
-void path_spec_unwatch(PathSpec *s, Unit *u) {
-
-        if (s->inotify_fd < 0)
-                return;
+void path_spec_unwatch(PathSpec *s) {
+        assert(s);
 
-        unit_unwatch_fd(u, &s->watch);
+        s->event_source = sd_event_source_unref(s->event_source);
 
-        close_nointr_nofail(s->inotify_fd);
-        s->inotify_fd = -1;
+        if (s->inotify_fd >= 0) {
+                close_nointr_nofail(s->inotify_fd);
+                s->inotify_fd = -1;
+        }
 }
 
-int path_spec_fd_event(PathSpec *s, uint32_t events) {
-        uint8_t *buf = NULL;
+int path_spec_fd_event(PathSpec *s, uint32_t revents) {
+        _cleanup_free_ uint8_t *buf = NULL;
         struct inotify_event *e;
         ssize_t k;
         int l;
         int r = 0;
 
-        if (events != EPOLLIN) {
-                log_error("Got Invalid poll event on inotify.");
-                r = -EINVAL;
-                goto out;
+        if (revents != EPOLLIN) {
+                log_error("Got invalid poll event on inotify.");
+                return -EINVAL;
         }
 
         if (ioctl(s->inotify_fd, FIONREAD, &l) < 0) {
                 log_error("FIONREAD failed: %m");
-                r = -errno;
-                goto out;
+                return -errno;
         }
 
         assert(l > 0);
 
-        if (!(buf = malloc(l))) {
-                log_error("Failed to allocate buffer: %m");
-                r = -errno;
-                goto out;
-        }
+        buf = malloc(l);
+        if (!buf)
+                return log_oom();
 
-        if ((k = read(s->inotify_fd, buf, l)) < 0) {
+        k = read(s->inotify_fd, buf, l);
+        if (k < 0) {
                 log_error("Failed to read inotify event: %m");
-                r = -errno;
-                goto out;
+                return -errno;
         }
 
         e = (struct inotify_event*) buf;
@@ -164,8 +203,7 @@ int path_spec_fd_event(PathSpec *s, uint32_t events) {
                 e = (struct inotify_event*) ((uint8_t*) e + step);
                 k -= step;
         }
-out:
-        free(buf);
+
         return r;
 }
 
@@ -207,17 +245,14 @@ static bool path_spec_check_good(PathSpec *s, bool initial) {
         return good;
 }
 
-static bool path_spec_startswith(PathSpec *s, const char *what) {
-        return path_startswith(s->path, what);
-}
-
 static void path_spec_mkdir(PathSpec *s, mode_t mode) {
         int r;
 
         if (s->type == PATH_EXISTS || s->type == PATH_EXISTS_GLOB)
                 return;
 
-        if ((r = mkdir_p_label(s->path, mode)) < 0)
+        r = mkdir_p_label(s->path, mode);
+        if (r < 0)
                 log_warning("mkdir(%s) failed: %s", s->path, strerror(-r));
 }
 
@@ -245,54 +280,38 @@ static void path_init(Unit *u) {
         p->directory_mode = 0755;
 }
 
-static void path_done(Unit *u) {
-        Path *p = PATH(u);
+void path_free_specs(Path *p) {
         PathSpec *s;
 
         assert(p);
 
-        unit_ref_unset(&p->unit);
-
         while ((s = p->specs)) {
-                path_spec_unwatch(s, u);
-                LIST_REMOVE(PathSpec, spec, p->specs, s);
+                path_spec_unwatch(s);
+                LIST_REMOVE(spec, p->specs, s);
                 path_spec_done(s);
                 free(s);
         }
 }
 
-int path_add_one_mount_link(Path *p, Mount *m) {
-        PathSpec *s;
-        int r;
+static void path_done(Unit *u) {
+        Path *p = PATH(u);
 
         assert(p);
-        assert(m);
-
-        if (UNIT(p)->load_state != UNIT_LOADED ||
-            UNIT(m)->load_state != UNIT_LOADED)
-                return 0;
-
-        LIST_FOREACH(spec, s, p->specs) {
-
-                if (!path_spec_startswith(s, m->where))
-                        continue;
-
-                if ((r = unit_add_two_dependencies(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
-                        return r;
-        }
 
-        return 0;
+        path_free_specs(p);
 }
 
 static int path_add_mount_links(Path *p) {
-        Unit *other;
+        PathSpec *s;
         int r;
 
         assert(p);
 
-        LIST_FOREACH(units_by_type, other, UNIT(p)->manager->units_by_type[UNIT_MOUNT])
-                if ((r = path_add_one_mount_link(p, MOUNT(other))) < 0)
+        LIST_FOREACH(spec, s, p->specs) {
+                r = unit_require_mounts_for(UNIT(p), s->path);
+                if (r < 0)
                         return r;
+        }
 
         return 0;
 }
@@ -304,7 +323,8 @@ static int path_verify(Path *p) {
                 return 0;
 
         if (!p->specs) {
-                log_error("%s lacks path setting. Refusing.", UNIT(p)->id);
+                log_error_unit(UNIT(p)->id,
+                               "%s lacks path setting. Refusing.", UNIT(p)->id);
                 return -EINVAL;
         }
 
@@ -316,15 +336,20 @@ static int path_add_default_dependencies(Path *p) {
 
         assert(p);
 
-        if (UNIT(p)->manager->running_as == SYSTEMD_SYSTEM) {
-                if ((r = unit_add_dependency_by_name(UNIT(p), UNIT_BEFORE, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
-                        return r;
+        r = unit_add_dependency_by_name(UNIT(p), UNIT_BEFORE,
+                                        SPECIAL_PATHS_TARGET, NULL, true);
+        if (r < 0)
+                return r;
 
-                if ((r = unit_add_two_dependencies_by_name(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0)
+        if (UNIT(p)->manager->running_as == SYSTEMD_SYSTEM) {
+                r = unit_add_two_dependencies_by_name(UNIT(p), UNIT_AFTER, UNIT_REQUIRES,
+                                                      SPECIAL_SYSINIT_TARGET, NULL, true);
+                if (r < 0)
                         return r;
         }
 
-        return unit_add_two_dependencies_by_name(UNIT(p), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
+        return unit_add_two_dependencies_by_name(UNIT(p), UNIT_BEFORE, UNIT_CONFLICTS,
+                                                 SPECIAL_SHUTDOWN_TARGET, NULL, true);
 }
 
 static int path_load(Unit *u) {
@@ -334,31 +359,33 @@ static int path_load(Unit *u) {
         assert(u);
         assert(u->load_state == UNIT_STUB);
 
-        if ((r = unit_load_fragment_and_dropin(u)) < 0)
+        r = unit_load_fragment_and_dropin(u);
+        if (r < 0)
                 return r;
 
         if (u->load_state == UNIT_LOADED) {
 
-                if (!UNIT_DEREF(p->unit)) {
+                if (set_isempty(u->dependencies[UNIT_TRIGGERS])) {
                         Unit *x;
 
                         r = unit_load_related_unit(u, ".service", &x);
                         if (r < 0)
                                 return r;
 
-                        unit_ref_set(&p->unit, x);
+                        r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, x, true);
+                        if (r < 0)
+                                return r;
                 }
 
-                r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(p->unit), true);
+                r = path_add_mount_links(p);
                 if (r < 0)
                         return r;
 
-                if ((r = path_add_mount_links(p)) < 0)
-                        return r;
-
-                if (UNIT(p)->default_dependencies)
-                        if ((r = path_add_default_dependencies(p)) < 0)
+                if (UNIT(p)->default_dependencies) {
+                        r = path_add_default_dependencies(p);
+                        if (r < 0)
                                 return r;
+                }
         }
 
         return path_verify(p);
@@ -366,11 +393,14 @@ static int path_load(Unit *u) {
 
 static void path_dump(Unit *u, FILE *f, const char *prefix) {
         Path *p = PATH(u);
+        Unit *trigger;
         PathSpec *s;
 
         assert(p);
         assert(f);
 
+        trigger = UNIT_TRIGGER(u);
+
         fprintf(f,
                 "%sPath State: %s\n"
                 "%sResult: %s\n"
@@ -379,7 +409,7 @@ static void path_dump(Unit *u, FILE *f, const char *prefix) {
                 "%sDirectoryMode: %04o\n",
                 prefix, path_state_to_string(p->state),
                 prefix, path_result_to_string(p->result),
-                prefix, UNIT_DEREF(p->unit)->id,
+                prefix, trigger ? trigger->id : "n/a",
                 prefix, yes_no(p->make_directory),
                 prefix, p->directory_mode);
 
@@ -393,7 +423,7 @@ static void path_unwatch(Path *p) {
         assert(p);
 
         LIST_FOREACH(spec, s, p->specs)
-                path_spec_unwatch(s, UNIT(p));
+                path_spec_unwatch(s);
 }
 
 static int path_watch(Path *p) {
@@ -402,9 +432,11 @@ static int path_watch(Path *p) {
 
         assert(p);
 
-        LIST_FOREACH(spec, s, p->specs)
-                if ((r = path_spec_watch(s, UNIT(p))) < 0)
+        LIST_FOREACH(spec, s, p->specs) {
+                r = path_spec_watch(s, path_dispatch_io);
+                if (r < 0)
                         return r;
+        }
 
         return 0;
 }
@@ -459,32 +491,33 @@ static void path_enter_dead(Path *p, PathResult f) {
 }
 
 static void path_enter_running(Path *p) {
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         int r;
-        DBusError error;
 
         assert(p);
-        dbus_error_init(&error);
 
         /* Don't start job if we are supposed to go down */
-        if (UNIT(p)->job && UNIT(p)->job->type == JOB_STOP)
+        if (unit_stop_pending(UNIT(p)))
                 return;
 
-        if ((r = manager_add_job(UNIT(p)->manager, JOB_START, UNIT_DEREF(p->unit), JOB_REPLACE, true, &error, NULL)) < 0)
+        r = manager_add_job(UNIT(p)->manager, JOB_START, UNIT_TRIGGER(UNIT(p)),
+                            JOB_REPLACE, true, &error, NULL);
+        if (r < 0)
                 goto fail;
 
         p->inotify_triggered = false;
 
-        if ((r = path_watch(p)) < 0)
+        r = path_watch(p);
+        if (r < 0)
                 goto fail;
 
         path_set_state(p, PATH_RUNNING);
         return;
 
 fail:
-        log_warning("%s failed to queue unit startup job: %s", UNIT(p)->id, bus_error(&error, r));
+        log_warning("%s failed to queue unit startup job: %s",
+                    UNIT(p)->id, bus_error_message(&error, r));
         path_enter_dead(p, PATH_FAILURE_RESOURCES);
-
-        dbus_error_free(&error);
 }
 
 static bool path_check_good(Path *p, bool initial) {
@@ -513,7 +546,8 @@ static void path_enter_waiting(Path *p, bool initial, bool recheck) {
                         return;
                 }
 
-        if ((r = path_watch(p)) < 0)
+        r = path_watch(p);
+        if (r < 0)
                 goto fail;
 
         /* Hmm, so now we have created inotify watches, but the file
@@ -531,7 +565,8 @@ static void path_enter_waiting(Path *p, bool initial, bool recheck) {
         return;
 
 fail:
-        log_warning("%s failed to enter waiting state: %s", UNIT(p)->id, strerror(-r));
+        log_warning("%s failed to enter waiting state: %s",
+                    UNIT(p)->id, strerror(-r));
         path_enter_dead(p, PATH_FAILURE_RESOURCES);
 }
 
@@ -553,7 +588,7 @@ static int path_start(Unit *u) {
         assert(p);
         assert(p->state == PATH_DEAD || p->state == PATH_FAILED);
 
-        if (UNIT_DEREF(p->unit)->load_state != UNIT_LOADED)
+        if (UNIT_TRIGGER(u)->load_state != UNIT_LOADED)
                 return -ENOENT;
 
         path_mkdir(p);
@@ -598,7 +633,8 @@ static int path_deserialize_item(Unit *u, const char *key, const char *value, FD
         if (streq(key, "state")) {
                 PathState state;
 
-                if ((state = path_state_from_string(value)) < 0)
+                state = path_state_from_string(value);
+                if (state < 0)
                         log_debug("Failed to parse state value %s", value);
                 else
                         p->deserialized_state = state;
@@ -618,29 +654,32 @@ static int path_deserialize_item(Unit *u, const char *key, const char *value, FD
         return 0;
 }
 
-static UnitActiveState path_active_state(Unit *u) {
+_pure_ static UnitActiveState path_active_state(Unit *u) {
         assert(u);
 
         return state_translation_table[PATH(u)->state];
 }
 
-static const char *path_sub_state_to_string(Unit *u) {
+_pure_ static const char *path_sub_state_to_string(Unit *u) {
         assert(u);
 
         return path_state_to_string(PATH(u)->state);
 }
 
-static void path_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
-        Path *p = PATH(u);
-        PathSpec *s;
+static int path_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
+        PathSpec *s = userdata;
+        Path *p;
         int changed;
 
-        assert(p);
+        assert(s);
+        assert(s->unit);
         assert(fd >= 0);
 
+        p = PATH(s->unit);
+
         if (p->state != PATH_WAITING &&
             p->state != PATH_RUNNING)
-                return;
+                return 0;
 
         /* log_debug("inotify wakeup on %s.", u->id); */
 
@@ -653,7 +692,7 @@ static void path_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
                 goto fail;
         }
 
-        changed = path_spec_fd_event(s, events);
+        changed = path_spec_fd_event(s, revents);
         if (changed < 0)
                 goto fail;
 
@@ -667,38 +706,35 @@ static void path_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
         else
                 path_enter_waiting(p, false, true);
 
-        return;
+        return 0;
 
 fail:
         path_enter_dead(p, PATH_FAILURE_RESOURCES);
+        return 0;
 }
 
-void path_unit_notify(Unit *u, UnitActiveState new_state) {
-        Iterator i;
-        Unit *k;
-
-        if (u->type == UNIT_PATH)
-                return;
-
-        SET_FOREACH(k, u->dependencies[UNIT_TRIGGERED_BY], i) {
-                Path *p;
+static void path_trigger_notify(Unit *u, Unit *other) {
+        Path *p = PATH(u);
 
-                if (k->type != UNIT_PATH)
-                        continue;
+        assert(u);
+        assert(other);
 
-                if (k->load_state != UNIT_LOADED)
-                        continue;
+        /* Invoked whenever the unit we trigger changes state or gains
+         * or loses a job */
 
-                p = PATH(k);
+        if (other->load_state != UNIT_LOADED)
+                return;
 
-                if (p->state == PATH_RUNNING && new_state == UNIT_INACTIVE) {
-                        log_debug("%s got notified about unit deactivation.", UNIT(p)->id);
+        if (p->state == PATH_RUNNING &&
+            UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) {
+                log_debug_unit(UNIT(p)->id,
+                               "%s got notified about unit deactivation.",
+                               UNIT(p)->id);
 
-                        /* Hmm, so inotify was triggered since the
-                         * last activation, so I guess we need to
-                         * recheck what is going on. */
-                        path_enter_waiting(p, false, p->inotify_triggered);
-                }
+                /* Hmm, so inotify was triggered since the
+                 * last activation, so I guess we need to
+                 * recheck what is going on. */
+                path_enter_waiting(p, false, p->inotify_triggered);
         }
 }
 
@@ -741,6 +777,7 @@ DEFINE_STRING_TABLE_LOOKUP(path_result, PathResult);
 
 const UnitVTable path_vtable = {
         .object_size = sizeof(Path),
+
         .sections =
                 "Unit\0"
                 "Path\0"
@@ -763,11 +800,10 @@ const UnitVTable path_vtable = {
         .active_state = path_active_state,
         .sub_state_to_string = path_sub_state_to_string,
 
-        .fd_event = path_fd_event,
+        .trigger_notify = path_trigger_notify,
 
         .reset_failed = path_reset_failed,
 
         .bus_interface = "org.freedesktop.systemd1.Path",
-        .bus_message_handler = bus_path_message_handler,
-        .bus_invalidating_properties = bus_path_invalidating_properties
+        .bus_vtable = bus_path_vtable
 };