X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fpath.c;h=b3bc8a5a0d77bb38883ab57e47dca11d9c0a79ca;hp=56936fd673fd76d427c18eb8fdce7f2105279be5;hb=b9ba604e8720c30deb7095b049b577eec1cbb74a;hpb=2c966c038dc32ef39baa176371395cde4e541d01 diff --git a/src/path.c b/src/path.c index 56936fd67..b3bc8a5a0 100644 --- a/src/path.c +++ b/src/path.c @@ -1,4 +1,4 @@ -/*-*- Mode: C; c-basic-offset: 8 -*-*/ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ /*** This file is part of systemd. @@ -29,12 +29,14 @@ #include "unit-name.h" #include "path.h" #include "dbus-path.h" +#include "special.h" +#include "bus-errors.h" static const UnitActiveState state_translation_table[_PATH_STATE_MAX] = { [PATH_DEAD] = UNIT_INACTIVE, [PATH_WAITING] = UNIT_ACTIVE, [PATH_RUNNING] = UNIT_ACTIVE, - [PATH_MAINTENANCE] = UNIT_MAINTENANCE + [PATH_FAILED] = UNIT_FAILED }; static void path_done(Unit *u) { @@ -99,6 +101,18 @@ static int path_verify(Path *p) { return 0; } +static int path_add_default_dependencies(Path *p) { + int r; + + assert(p); + + if (p->meta.manager->running_as == MANAGER_SYSTEM) + if ((r = unit_add_two_dependencies_by_name(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0) + return r; + + return unit_add_two_dependencies_by_name(UNIT(p), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true); +} + static int path_load(Unit *u) { Path *p = PATH(u); int r; @@ -120,6 +134,10 @@ static int path_load(Unit *u) { if ((r = path_add_mount_links(p)) < 0) return r; + + if (p->meta.default_dependencies) + if ((r = path_add_default_dependencies(p)) < 0) + return r; } return path_verify(p); @@ -127,12 +145,10 @@ static int path_load(Unit *u) { static void path_dump(Unit *u, FILE *f, const char *prefix) { Path *p = PATH(u); - const char *prefix2; - char *p2; PathSpec *s; - p2 = strappend(prefix, "\t"); - prefix2 = p2 ? p2 : prefix; + assert(p); + assert(f); fprintf(f, "%sPath State: %s\n" @@ -146,8 +162,6 @@ static void path_dump(Unit *u, FILE *f, const char *prefix) { prefix, path_type_to_string(s->type), s->path); - - free(p2); } static void path_unwatch_one(Path *p, PathSpec *s) { @@ -287,22 +301,31 @@ static void path_enter_dead(Path *p, bool success) { if (!success) p->failure = true; - path_set_state(p, p->failure ? PATH_MAINTENANCE : PATH_DEAD); + path_set_state(p, p->failure ? PATH_FAILED : PATH_DEAD); } static void path_enter_running(Path *p) { int r; + DBusError error; + assert(p); + dbus_error_init(&error); - if ((r = manager_add_job(p->meta.manager, JOB_START, p->unit, JOB_REPLACE, true, NULL)) < 0) + /* Don't start job if we are supposed to go down */ + if (p->meta.job && p->meta.job->type == JOB_STOP) + return; + + if ((r = manager_add_job(p->meta.manager, JOB_START, p->unit, JOB_REPLACE, true, &error, NULL)) < 0) goto fail; path_set_state(p, PATH_RUNNING); return; fail: - log_warning("%s failed to queue unit startup job: %s", p->meta.id, strerror(-r)); + log_warning("%s failed to queue unit startup job: %s", p->meta.id, bus_error(&error, r)); path_enter_dead(p, false); + + dbus_error_free(&error); } @@ -360,13 +383,14 @@ static int path_start(Unit *u) { Path *p = PATH(u); assert(p); - assert(p->state == PATH_DEAD || p->state == PATH_MAINTENANCE); + assert(p->state == PATH_DEAD || p->state == PATH_FAILED); if (p->unit->meta.load_state != UNIT_LOADED) return -ENOENT; p->failure = false; -path_enter_waiting(p, true); + path_enter_waiting(p, true); + return 0; } @@ -429,7 +453,8 @@ static void path_fd_event(Unit *u, int fd, uint32_t events, Watch *w) { Path *p = PATH(u); int l; ssize_t k; - struct inotify_event *buf = NULL; + uint8_t *buf = NULL; + struct inotify_event *e; PathSpec *s; assert(p); @@ -469,16 +494,22 @@ static void path_fd_event(Unit *u, int fd, uint32_t events, Watch *w) { goto fail; } - if ((size_t) k < sizeof(struct inotify_event) || - (size_t) k < sizeof(struct inotify_event) + buf->len) { - log_error("inotify event too small."); - goto fail; - } + e = (struct inotify_event*) buf; - if (s->type == PATH_CHANGED && s->primary_wd == buf->wd) - path_enter_running(p); - else - path_enter_waiting(p, false); + while (k > 0) { + size_t step; + + if (s->type == PATH_CHANGED && s->primary_wd == e->wd) + path_enter_running(p); + else + path_enter_waiting(p, false); + + step = sizeof(struct inotify_event) + e->len; + assert(step <= (size_t) k); + + e = (struct inotify_event*) ((uint8_t*) e + step); + k -= step; + } free(buf); @@ -533,11 +564,22 @@ fail: log_error("Failed find path unit: %s", strerror(-r)); } +static void path_reset_failed(Unit *u) { + Path *p = PATH(u); + + assert(p); + + if (p->state == PATH_FAILED) + path_set_state(p, PATH_DEAD); + + p->failure = false; +} + static const char* const path_state_table[_PATH_STATE_MAX] = { [PATH_DEAD] = "dead", [PATH_WAITING] = "waiting", [PATH_RUNNING] = "running", - [PATH_MAINTENANCE] = "maintenance" + [PATH_FAILED] = "failed" }; DEFINE_STRING_TABLE_LOOKUP(path_state, PathState); @@ -571,5 +613,8 @@ const UnitVTable path_vtable = { .fd_event = path_fd_event, + .reset_failed = path_reset_failed, + + .bus_interface = "org.freedesktop.systemd1.Path", .bus_message_handler = bus_path_message_handler };