diff options
author | Lennart Poettering <lennart@poettering.net> | 2010-05-24 05:25:33 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2010-05-24 05:25:33 +0200 |
commit | 01f78473b104d28db0fa813414092bc6358ae521 (patch) | |
tree | dbc1a63d818c7420ee2a50cbd205ae90b0c7185d /src | |
parent | 871d7de47c13ee6cd78b8eefdf9128be3c740ac0 (diff) |
path: add .path unit type for monitoring files
Diffstat (limited to 'src')
-rw-r--r-- | src/automount.c | 5 | ||||
-rw-r--r-- | src/conf-parser.c | 4 | ||||
-rw-r--r-- | src/dbus-path.c | 52 | ||||
-rw-r--r-- | src/dbus-path.h | 33 | ||||
-rw-r--r-- | src/dbus.c | 2 | ||||
-rw-r--r-- | src/load-fragment.c | 99 | ||||
-rw-r--r-- | src/mount.c | 16 | ||||
-rw-r--r-- | src/path.c | 578 | ||||
-rw-r--r-- | src/path.h | 85 | ||||
-rw-r--r-- | src/service.c | 1 | ||||
-rw-r--r-- | src/socket.c | 1 | ||||
-rw-r--r-- | src/socket.h | 8 | ||||
-rw-r--r-- | src/timer.c | 12 | ||||
-rw-r--r-- | src/timer.h | 4 | ||||
-rw-r--r-- | src/unit.c | 4 | ||||
-rw-r--r-- | src/unit.h | 4 | ||||
-rw-r--r-- | src/util.c | 29 | ||||
-rw-r--r-- | src/util.h | 2 |
18 files changed, 915 insertions, 24 deletions
diff --git a/src/automount.c b/src/automount.c index 32680461c2..d83f3ed053 100644 --- a/src/automount.c +++ b/src/automount.c @@ -562,12 +562,15 @@ static int automount_start(Unit *u) { assert(a); + assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_MAINTAINANCE); + if (path_is_mount_point(a->where)) { log_error("Path %s is already a mount point, refusing start for %s", a->where, u->meta.id); return -EEXIST; } - assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_MAINTAINANCE); + if (a->mount->meta.load_state != UNIT_LOADED) + return -ENOENT; a->failure = false; automount_enter_waiting(a); diff --git a/src/conf-parser.c b/src/conf-parser.c index 6994211b10..20f7641b13 100644 --- a/src/conf-parser.c +++ b/src/conf-parser.c @@ -337,6 +337,8 @@ int config_parse_path( if (!(n = strdup(rvalue))) return -ENOMEM; + path_kill_slashes(n); + free(*s); *s = n; @@ -441,6 +443,8 @@ int config_parse_path_strv( goto fail; } + path_kill_slashes(n[k]); + k++; } diff --git a/src/dbus-path.c b/src/dbus-path.c new file mode 100644 index 0000000000..ed1dc265fc --- /dev/null +++ b/src/dbus-path.c @@ -0,0 +1,52 @@ +/*-*- Mode: C; c-basic-offset: 8 -*-*/ + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <errno.h> + +#include "dbus-unit.h" +#include "dbus-path.h" +#include "dbus-execute.h" + +#define BUS_PATH_INTERFACE \ + " <interface name=\"org.freedesktop.systemd1.Path\">\n" \ + " <property name=\"Unit\" type=\"s\" access=\"read\"/>\n" \ + " </interface>\n" \ + +#define INTROSPECTION \ + DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \ + "<node>\n" \ + BUS_UNIT_INTERFACE \ + BUS_PATH_INTERFACE \ + BUS_PROPERTIES_INTERFACE \ + BUS_INTROSPECTABLE_INTERFACE \ + "</node>\n" + +const char bus_path_interface[] = BUS_PATH_INTERFACE; + +DBusHandlerResult bus_path_message_handler(Unit *u, DBusMessage *message) { + const BusProperty properties[] = { + BUS_UNIT_PROPERTIES, + { "org.freedesktop.systemd1.Path", "Unit", bus_property_append_string, "s", &u->path.unit->meta.id }, + { NULL, NULL, NULL, NULL, NULL } + }; + + return bus_default_message_handler(u->meta.manager, message, INTROSPECTION, properties); +} diff --git a/src/dbus-path.h b/src/dbus-path.h new file mode 100644 index 0000000000..15f5869894 --- /dev/null +++ b/src/dbus-path.h @@ -0,0 +1,33 @@ +/*-*- Mode: C; c-basic-offset: 8 -*-*/ + +#ifndef foodbuspathhfoo +#define foodbuspathhfoo + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <dbus/dbus.h> + +#include "unit.h" + +DBusHandlerResult bus_path_message_handler(Unit *u, DBusMessage *message); + +extern const char bus_path_interface[]; + +#endif diff --git a/src/dbus.c b/src/dbus.c index 6dd495d4df..f5bbc5815a 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -41,6 +41,7 @@ #include "dbus-snapshot.h" #include "dbus-swap.h" #include "dbus-timer.h" +#include "dbus-path.h" static const char bus_properties_interface[] = BUS_PROPERTIES_INTERFACE; static const char bus_introspectable_interface[] = BUS_INTROSPECTABLE_INTERFACE; @@ -60,6 +61,7 @@ const char *const bus_interface_table[] = { "org.freedesktop.systemd1.Snapshot", bus_snapshot_interface, "org.freedesktop.systemd1.Swap", bus_swap_interface, "org.freedesktop.systemd1.Timer", bus_timer_interface, + "org.freedesktop.systemd1.Path", bus_path_interface, NULL }; diff --git a/src/load-fragment.c b/src/load-fragment.c index 889e621100..a706880b9a 100644 --- a/src/load-fragment.c +++ b/src/load-fragment.c @@ -213,6 +213,8 @@ static int config_parse_listen( free(p); return -ENOMEM; } + + path_kill_slashes(p->path); } else { p->type = SOCKET_SOCKET; @@ -450,6 +452,8 @@ static int config_parse_exec( nce->argv = n; nce->path = path; + path_kill_slashes(nce->path); + exec_command_append_list(e, nce); return 0; @@ -1068,6 +1072,77 @@ static int config_parse_timer_unit( return 0; } +static int config_parse_path_spec( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + Path *p = data; + PathSpec *s; + PathType b; + + assert(filename); + assert(lvalue); + assert(rvalue); + assert(data); + + if ((b = path_type_from_string(lvalue)) < 0) { + log_error("[%s:%u] Failed to parse path type: %s", filename, line, lvalue); + return -EINVAL; + } + + if (!path_is_absolute(rvalue)) { + log_error("[%s:%u] Path is not absolute: %s", filename, line, rvalue); + return -EINVAL; + } + + if (!(s = new0(PathSpec, 1))) + return -ENOMEM; + + if (!(s->path = strdup(rvalue))) { + free(s); + return -ENOMEM; + } + + path_kill_slashes(s->path); + + s->type = b; + s->inotify_fd = -1; + + LIST_PREPEND(PathSpec, spec, p->specs, s); + + return 0; +} + +static int config_parse_path_unit( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + Path *t = data; + int r; + + if (endswith(rvalue, ".path")) { + log_error("[%s:%u] Unit cannot be of type path: %s", filename, line, rvalue); + return -EINVAL; + } + + if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &t->unit)) < 0) { + log_error("[%s:%u] Failed to load unit: %s", filename, line, rvalue); + return r; + } + + return 0; +} + #define FOLLOW_MAX 8 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) { @@ -1269,7 +1344,8 @@ static int load_from_path(Unit *u, const char *path) { [UNIT_MOUNT] = "Mount", [UNIT_AUTOMOUNT] = "Automount", [UNIT_SNAPSHOT] = "Snapshot", - [UNIT_SWAP] = "Swap" + [UNIT_SWAP] = "Swap", + [UNIT_PATH] = "Path" }; #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \ @@ -1386,15 +1462,20 @@ static int load_from_path(Unit *u, const char *path) { { "Where", config_parse_path, &u->automount.where, "Automount" }, - { "What", config_parse_path, &u->swap.parameters_fragment.what, "Swap" }, - { "Priority", config_parse_int, &u->swap.parameters_fragment.priority, "Swap" }, + { "What", config_parse_path, &u->swap.parameters_fragment.what, "Swap" }, + { "Priority", config_parse_int, &u->swap.parameters_fragment.priority, "Swap" }, + + { "OnActive", config_parse_timer, &u->timer, "Timer" }, + { "OnBoot", config_parse_timer, &u->timer, "Timer" }, + { "OnStartup", config_parse_timer, &u->timer, "Timer" }, + { "OnUnitActive", config_parse_timer, &u->timer, "Timer" }, + { "OnUnitInactive", config_parse_timer, &u->timer, "Timer" }, + { "Unit", config_parse_timer_unit, &u->timer, "Timer" }, - { "OnActive", config_parse_timer, &u->timer, "Timer" }, - { "OnBoot", config_parse_timer, &u->timer, "Timer" }, - { "OnStartup", config_parse_timer, &u->timer, "Timer" }, - { "OnUnitActive", config_parse_timer, &u->timer, "Timer" }, - { "OnUnitInactive", config_parse_timer, &u->timer, "Timer" }, - { "Unit", config_parse_timer_unit, &u->timer, "Timer" }, + { "PathExists", config_parse_path_spec, &u->path, "Path" }, + { "PathChanged", config_parse_path_spec, &u->path, "Path" }, + { "DirectoryNotEmpty", config_parse_path_spec, &u->path, "Path" }, + { "Unit", config_parse_path_unit, &u->path, "Path" }, { NULL, NULL, NULL, NULL } }; diff --git a/src/mount.c b/src/mount.c index 01fc2dffbb..dfe4f875e1 100644 --- a/src/mount.c +++ b/src/mount.c @@ -167,6 +167,19 @@ static int mount_add_swap_links(Mount *m) { return 0; } +static int mount_add_path_links(Mount *m) { + Meta *other; + int r; + + assert(m); + + LIST_FOREACH(units_per_type, other, m->meta.manager->units_per_type[UNIT_PATH]) + if ((r = path_add_one_mount_link((Path*) other, m)) < 0) + return r; + + return 0; +} + static int mount_add_automount_links(Mount *m) { Meta *other; int r; @@ -341,6 +354,9 @@ static int mount_load(Unit *u) { if ((r = mount_add_swap_links(m)) < 0) return r; + if ((r = mount_add_path_links(m)) < 0) + return r; + if ((r = mount_add_automount_links(m)) < 0) return r; diff --git a/src/path.c b/src/path.c new file mode 100644 index 0000000000..b40a8297ff --- /dev/null +++ b/src/path.c @@ -0,0 +1,578 @@ +/*-*- Mode: C; c-basic-offset: 8 -*-*/ + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <sys/inotify.h> +#include <sys/epoll.h> +#include <sys/ioctl.h> +#include <errno.h> +#include <unistd.h> + +#include "unit.h" +#include "unit-name.h" +#include "path.h" +#include "dbus-path.h" + +static const UnitActiveState state_translation_table[_PATH_STATE_MAX] = { + [PATH_DEAD] = UNIT_INACTIVE, + [PATH_WAITING] = UNIT_ACTIVE, + [PATH_RUNNING] = UNIT_ACTIVE, + [PATH_MAINTAINANCE] = UNIT_INACTIVE +}; + +static void path_done(Unit *u) { + Path *p = PATH(u); + PathSpec *s; + + assert(p); + + while ((s = p->specs)) { + LIST_REMOVE(PathSpec, spec, p->specs, s); + free(s); + } +} + +int path_add_one_mount_link(Path *p, Mount *m) { + PathSpec *s; + int r; + + assert(p); + assert(m); + + if (p->meta.load_state != UNIT_LOADED || + m->meta.load_state != UNIT_LOADED) + return 0; + + LIST_FOREACH(spec, s, p->specs) { + + if (!path_startswith(s->path, m->where)) + continue; + + if ((r = unit_add_dependency(UNIT(m), UNIT_BEFORE, UNIT(p), true)) < 0) + return r; + + if ((r = unit_add_dependency(UNIT(p), UNIT_REQUIRES, UNIT(m), true)) < 0) + return r; + } + + return 0; +} + +static int path_add_mount_links(Path *p) { + Meta *other; + int r; + + assert(p); + + LIST_FOREACH(units_per_type, other, p->meta.manager->units_per_type[UNIT_MOUNT]) + if ((r = path_add_one_mount_link(p, (Mount*) other)) < 0) + return r; + + return 0; +} + +static int path_verify(Path *p) { + assert(p); + + if (UNIT(p)->meta.load_state != UNIT_LOADED) + return 0; + + if (!p->specs) { + log_error("%s lacks path setting. Refusing.", p->meta.id); + return -EINVAL; + } + + return 0; +} + +static int path_load(Unit *u) { + Path *p = PATH(u); + int r; + + assert(u); + assert(u->meta.load_state == UNIT_STUB); + + if ((r = unit_load_fragment_and_dropin(u)) < 0) + return r; + + if (u->meta.load_state == UNIT_LOADED) { + + if (!p->unit) + if ((r = unit_load_related_unit(u, ".service", &p->unit))) + return r; + + if ((r = unit_add_dependency(u, UNIT_BEFORE, p->unit, true)) < 0) + return r; + + if ((r = path_add_mount_links(p)) < 0) + return r; + } + + return path_verify(p); +} + +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; + + fprintf(f, + "%sPath State: %s\n" + "%sUnit: %s\n", + prefix, path_state_to_string(p->state), + prefix, p->unit->meta.id); + + LIST_FOREACH(spec, s, p->specs) + fprintf(f, + "%s%s: %s\n", + prefix, + path_type_to_string(s->type), + s->path); + + free(p2); +} + +static void path_unwatch_one(Path *p, PathSpec *s) { + + if (s->inotify_fd < 0) + return; + + unit_unwatch_fd(UNIT(p), &s->watch); + + close_nointr_nofail(s->inotify_fd); + s->inotify_fd = -1; +} + +static int path_watch_one(Path *p, PathSpec *s) { + static const int flags_table[_PATH_TYPE_MAX] = { + [PATH_EXISTS] = IN_DELETE_SELF|IN_MOVE_SELF, + [PATH_CHANGED] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO, + [PATH_DIRECTORY_NOT_EMPTY] = IN_DELETE_SELF|IN_MOVE_SELF|IN_CREATE|IN_MOVED_TO + }; + + bool exists = false; + char *k; + int r; + + assert(p); + assert(s); + + path_unwatch_one(p, s); + + if (!(k = strdup(s->path))) + return -ENOMEM; + + if ((s->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC)) < 0) { + r = -errno; + goto fail; + } + + if (unit_watch_fd(UNIT(p), s->inotify_fd, EPOLLIN, &s->watch) < 0) { + r = -errno; + goto fail; + } + + if ((s->primary_wd = inotify_add_watch(s->inotify_fd, k, flags_table[s->type])) >= 0) + exists = true; + + for (;;) { + int flags; + char *slash; + + /* This assumes the path was passed through path_kill_slashes()! */ + if (!(slash = strrchr(k, '/'))) + break; + + *slash = 0; + + flags = IN_DELETE_SELF|IN_MOVE_SELF; + if (!exists) + flags |= IN_CREATE | IN_MOVED_TO | IN_ATTRIB; + + if (inotify_add_watch(s->inotify_fd, k, flags) >= 0) + exists = true; + } + + return 0; + +fail: + free(k); + + path_unwatch_one(p, s); + return r; +} + +static void path_unwatch(Path *p) { + PathSpec *s; + + assert(p); + + LIST_FOREACH(spec, s, p->specs) + path_unwatch_one(p, s); +} + +static int path_watch(Path *p) { + int r; + PathSpec *s; + + assert(p); + + LIST_FOREACH(spec, s, p->specs) + if ((r = path_watch_one(p, s)) < 0) + return r; + + return 0; +} + +static void path_set_state(Path *p, PathState state) { + PathState old_state; + assert(p); + + old_state = p->state; + p->state = state; + + if (state != PATH_WAITING) + path_unwatch(p); + + if (state != old_state) + log_debug("%s changed %s -> %s", + p->meta.id, + path_state_to_string(old_state), + path_state_to_string(state)); + + unit_notify(UNIT(p), state_translation_table[old_state], state_translation_table[state]); +} + +static void path_enter_waiting(Path *p, bool initial); + +static int path_coldplug(Unit *u) { + Path *p = PATH(u); + + assert(p); + assert(p->state == PATH_DEAD); + + if (p->deserialized_state != p->state) { + + if (p->deserialized_state == PATH_WAITING || + p->deserialized_state == PATH_RUNNING) + path_enter_waiting(p, true); + else + path_set_state(p, p->deserialized_state); + } + + return 0; +} + +static void path_enter_dead(Path *p, bool success) { + assert(p); + + if (!success) + p->failure = true; + + path_set_state(p, p->failure ? PATH_MAINTAINANCE : PATH_DEAD); +} + +static void path_enter_running(Path *p) { + int r; + assert(p); + + if ((r = manager_add_job(UNIT(p)->meta.manager, JOB_START, p->unit, JOB_REPLACE, true, 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)); + path_enter_dead(p, false); +} + + +static void path_enter_waiting(Path *p, bool initial) { + PathSpec *s; + int r; + bool good = false; + + LIST_FOREACH(spec, s, p->specs) { + + switch (s->type) { + + case PATH_EXISTS: + good = access(s->path, F_OK) >= 0; + break; + + case PATH_DIRECTORY_NOT_EMPTY: + good = dir_is_empty(s->path) == 0; + break; + + case PATH_CHANGED: { + bool b; + + b = access(s->path, F_OK) >= 0; + good = !initial && b != s->previous_exists; + s->previous_exists = b; + break; + } + + default: + ; + } + + if (good) + break; + } + + if (good) { + path_enter_running(p); + return; + } + + if ((r = path_watch(p)) < 0) + goto fail; + + path_set_state(p, PATH_WAITING); + return; + +fail: + log_warning("%s failed to enter waiting state: %s", p->meta.id, strerror(-r)); + path_enter_dead(p, false); +} + +static int path_start(Unit *u) { + Path *p = PATH(u); + + assert(p); + assert(p->state == PATH_DEAD || p->state == PATH_MAINTAINANCE); + + if (p->unit->meta.load_state != UNIT_LOADED) + return -ENOENT; + + p->failure = false; +path_enter_waiting(p, true); + return 0; +} + +static int path_stop(Unit *u) { + Path *p = PATH(u); + + assert(p); + assert(p->state == PATH_WAITING || p->state == PATH_RUNNING); + + path_enter_dead(p, true); + return 0; +} + +static int path_serialize(Unit *u, FILE *f, FDSet *fds) { + Path *p = PATH(u); + + assert(u); + assert(f); + assert(fds); + + unit_serialize_item(u, f, "state", path_state_to_string(p->state)); + + return 0; +} + +static int path_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { + Path *p = PATH(u); + + assert(u); + assert(key); + assert(value); + assert(fds); + + if (streq(key, "state")) { + PathState state; + + if ((state = path_state_from_string(value)) < 0) + log_debug("Failed to parse state value %s", value); + else + p->deserialized_state = state; + } else + log_debug("Unknown serialization key '%s'", key); + + return 0; +} + +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) { + 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); + int l; + ssize_t k; + struct inotify_event *buf = NULL; + PathSpec *s; + + assert(p); + assert(fd >= 0); + + if (p->state != PATH_WAITING) + return; + + log_debug("inotify wakeup on %s.", u->meta.id); + + if (events != EPOLLIN) { + log_error("Got Invalid poll event on inotify."); + goto fail; + } + + LIST_FOREACH(spec, s, p->specs) + if (s->inotify_fd == fd) + break; + + if (!s) { + log_error("Got event on unknown fd."); + goto fail; + } + + if (ioctl(fd, FIONREAD, &l) < 0) { + log_error("FIONREAD failed: %s", strerror(errno)); + goto fail; + } + + if (!(buf = malloc(l))) { + log_error("Failed to allocate buffer: %s", strerror(-ENOMEM)); + goto fail; + } + + if ((k = read(fd, buf, l)) < 0) { + log_error("Failed to read inotify event: %s", strerror(-errno)); + 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; + } + + if (s->type == PATH_CHANGED && s->primary_wd == buf->wd) + path_enter_running(p); + else + path_enter_waiting(p, false); + + free(buf); + + return; + +fail: + free(buf); + path_enter_dead(p, false); +} + +void path_unit_notify(Unit *u, UnitActiveState new_state) { + char *n; + int r; + Iterator i; + + if (u->meta.type == UNIT_PATH) + return; + + SET_FOREACH(n, u->meta.names, i) { + char *k; + Unit *t; + Path *p; + + if (!(k = unit_name_change_suffix(n, ".path"))) { + r = -ENOMEM; + goto fail; + } + + t = manager_get_unit(u->meta.manager, k); + free(k); + + if (!t) + continue; + + if (t->meta.load_state != UNIT_LOADED) + continue; + + p = PATH(t); + + if (p->unit != u) + continue; + + if (p->state == PATH_RUNNING && new_state == UNIT_INACTIVE) { + log_debug("%s got notified about unit deactivation.", p->meta.id); + path_enter_waiting(p, false); + } + } + + return; + +fail: + log_error("Failed find path unit: %s", strerror(-r)); +} + +static const char* const path_state_table[_PATH_STATE_MAX] = { + [PATH_DEAD] = "dead", + [PATH_WAITING] = "waiting", + [PATH_RUNNING] = "running", + [PATH_MAINTAINANCE] = "maintainance" +}; + +DEFINE_STRING_TABLE_LOOKUP(path_state, PathState); + +static const char* const path_type_table[_PATH_TYPE_MAX] = { + [PATH_EXISTS] = "PathExists", + [PATH_CHANGED] = "PathChanged", + [PATH_DIRECTORY_NOT_EMPTY] = "DirectoryNotEmpty" +}; + +DEFINE_STRING_TABLE_LOOKUP(path_type, PathType); + +const UnitVTable path_vtable = { + .suffix = ".path", + + .done = path_done, + .load = path_load, + + .coldplug = path_coldplug, + + .dump = path_dump, + + .start = path_start, + .stop = path_stop, + + .serialize = path_serialize, + .deserialize_item = path_deserialize_item, + + .active_state = path_active_state, + .sub_state_to_string = path_sub_state_to_string, + + .fd_event = path_fd_event, + + .bus_message_handler = bus_path_message_handler +}; diff --git a/src/path.h b/src/path.h new file mode 100644 index 0000000000..21a7dc4934 --- /dev/null +++ b/src/path.h @@ -0,0 +1,85 @@ +/*-*- Mode: C; c-basic-offset: 8 -*-*/ + +#ifndef foopathhfoo +#define foopathhfoo + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +typedef struct Path Path; + +#include "unit.h" +#include "mount.h" + +typedef enum PathState { + PATH_DEAD, + PATH_WAITING, + PATH_RUNNING, + PATH_MAINTAINANCE, + _PATH_STATE_MAX, + _PATH_STATE_INVALID = -1 +} PathState; + +typedef enum PathType { + PATH_EXISTS, + PATH_DIRECTORY_NOT_EMPTY, + PATH_CHANGED, + _PATH_TYPE_MAX, + _PATH_TYPE_INVALID = -1 +} PathType; + +typedef struct PathSpec { + PathType type; + char *path; + + int inotify_fd; + int primary_wd; + bool previous_exists; + + Watch watch; + + LIST_FIELDS(struct PathSpec, spec); +} PathSpec; + +struct Path { + Meta meta; + + LIST_HEAD(PathSpec, specs); + + PathState state, deserialized_state; + Unit *unit; + + bool failure; +}; + +void path_unit_notify(Unit *u, UnitActiveState new_state); + +/* Called from the mount code figure out if a mount is a dependency of + * any of the paths of this path object */ +int path_add_one_mount_link(Path *p, Mount *m); + +extern const UnitVTable path_vtable; + +const char* path_state_to_string(PathState i); +PathState path_state_from_string(const char *s); + +const char* path_type_to_string(PathType i); +PathType path_type_from_string(const char *s); + +#endif diff --git a/src/service.c b/src/service.c index 07b6e4e05a..2f699cd1e6 100644 --- a/src/service.c +++ b/src/service.c @@ -1708,7 +1708,6 @@ static int service_start(Unit *u) { /* Make sure we don't enter a busy loop of some kind. */ if (!ratelimit_test(&s->ratelimit)) { log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id); - service_enter_dead(s, false, true); return -ECANCELED; } diff --git a/src/socket.c b/src/socket.c index ef7674f463..909151752c 100644 --- a/src/socket.c +++ b/src/socket.c @@ -58,7 +58,6 @@ static void socket_init(Unit *u) { assert(u); assert(u->meta.load_state == UNIT_STUB); - s->timer_watch.type = WATCH_INVALID; s->backlog = SOMAXCONN; s->timeout_usec = DEFAULT_TIMEOUT_USEC; s->directory_mode = 0755; diff --git a/src/socket.h b/src/socket.h index 5aa5f2783c..2067eb4254 100644 --- a/src/socket.h +++ b/src/socket.h @@ -62,9 +62,7 @@ typedef enum SocketType { _SOCKET_FIFO_INVALID = -1 } SocketType; -typedef struct SocketPort SocketPort; - -struct SocketPort { +typedef struct SocketPort { SocketType type; int fd; @@ -72,8 +70,8 @@ struct SocketPort { char *path; Watch fd_watch; - LIST_FIELDS(SocketPort, port); -}; + LIST_FIELDS(struct SocketPort, port); +} SocketPort; struct Socket { Meta meta; diff --git a/src/timer.c b/src/timer.c index e95b4d66e1..b025051f2b 100644 --- a/src/timer.c +++ b/src/timer.c @@ -41,7 +41,6 @@ static void timer_init(Unit *u) { assert(u->meta.load_state == UNIT_STUB); t->next_elapse = (usec_t) -1; - t->timer_watch.type = WATCH_INVALID; } static void timer_done(Unit *u) { @@ -274,8 +273,12 @@ static int timer_start(Unit *u) { Timer *t = TIMER(u); assert(t); - assert(t->state == TIMER_DEAD); + assert(t->state == TIMER_DEAD || t->state == TIMER_MAINTAINANCE); + + if (t->unit->meta.load_state != UNIT_LOADED) + return -ENOENT; + t->failure = false; timer_enter_waiting(t, true); return 0; } @@ -373,9 +376,12 @@ void timer_unit_notify(Unit *u, UnitActiveState new_state) { if (!p) continue; + if (p->meta.load_state != UNIT_LOADED) + continue; + t = TIMER(p); - if (t->meta.load_state != UNIT_LOADED) + if (t->unit != u) continue; /* Reenable all timers that depend on unit state */ diff --git a/src/timer.h b/src/timer.h index 69c5609a9c..eb22688a3b 100644 --- a/src/timer.h +++ b/src/timer.h @@ -61,11 +61,9 @@ struct Timer { Meta meta; LIST_HEAD(TimerValue, values); - - TimerState state, deserialized_state; - usec_t next_elapse; + TimerState state, deserialized_state; Unit *unit; Watch timer_watch; diff --git a/src/unit.c b/src/unit.c index 2af2685e0d..57b3b77954 100644 --- a/src/unit.c +++ b/src/unit.c @@ -48,7 +48,8 @@ const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = { [UNIT_MOUNT] = &mount_vtable, [UNIT_AUTOMOUNT] = &automount_vtable, [UNIT_SNAPSHOT] = &snapshot_vtable, - [UNIT_SWAP] = &swap_vtable + [UNIT_SWAP] = &swap_vtable, + [UNIT_PATH] = &path_vtable }; Unit *unit_new(Manager *m) { @@ -956,6 +957,7 @@ void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) { u->meta.active_exit_timestamp = ts; timer_unit_notify(u, ns); + path_unit_notify(u, ns); if (u->meta.job) { diff --git a/src/unit.h b/src/unit.h index fd0defe2d1..f585fa6238 100644 --- a/src/unit.h +++ b/src/unit.h @@ -62,6 +62,7 @@ enum UnitType { UNIT_SNAPSHOT, UNIT_TIMER, UNIT_SWAP, + UNIT_PATH, _UNIT_TYPE_MAX, _UNIT_TYPE_INVALID = -1 }; @@ -201,6 +202,7 @@ struct Meta { #include "automount.h" #include "snapshot.h" #include "swap.h" +#include "path.h" union Unit { Meta meta; @@ -213,6 +215,7 @@ union Unit { Automount automount; Snapshot snapshot; Swap swap; + Path path; }; struct UnitVTable { @@ -344,6 +347,7 @@ DEFINE_CAST(MOUNT, Mount); DEFINE_CAST(AUTOMOUNT, Automount); DEFINE_CAST(SNAPSHOT, Snapshot); DEFINE_CAST(SWAP, Swap); +DEFINE_CAST(PATH, Path); Unit *unit_new(Manager *m); void unit_free(Unit *u); diff --git a/src/util.c b/src/util.c index a8ea4a97ab..7664df59d0 100644 --- a/src/util.c +++ b/src/util.c @@ -2092,6 +2092,35 @@ bool is_device_path(const char *path) { path_startswith(path, "/sys/"); } +int dir_is_empty(const char *path) { + DIR *d; + int r; + struct dirent buf, *de; + + if (!(d = opendir(path))) + return -errno; + + for (;;) { + if ((r = readdir_r(d, &buf, &de)) > 0) { + r = -r; + break; + } + + if (!de) { + r = 1; + break; + } + + if (!ignore_file(de->d_name)) { + r = 0; + break; + } + } + + closedir(d); + return r; +} + static const char *const ioprio_class_table[] = { [IOPRIO_CLASS_NONE] = "none", [IOPRIO_CLASS_RT] = "realtime", diff --git a/src/util.h b/src/util.h index b411df0cf2..efc993c370 100644 --- a/src/util.h +++ b/src/util.h @@ -247,6 +247,8 @@ int path_is_mount_point(const char *path); bool is_device_path(const char *path); +int dir_is_empty(const char *path); + extern char * __progname; const char *ioprio_class_to_string(int i); |