1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/inotify.h>
23 #include <sys/epoll.h>
24 #include <sys/ioctl.h>
29 #include "unit-name.h"
31 #include "dbus-path.h"
33 #include "bus-errors.h"
35 static const UnitActiveState state_translation_table[_PATH_STATE_MAX] = {
36 [PATH_DEAD] = UNIT_INACTIVE,
37 [PATH_WAITING] = UNIT_ACTIVE,
38 [PATH_RUNNING] = UNIT_ACTIVE,
39 [PATH_FAILED] = UNIT_FAILED
42 static void path_unwatch_one(Path *p, PathSpec *s) {
44 if (s->inotify_fd < 0)
47 unit_unwatch_fd(UNIT(p), &s->watch);
49 close_nointr_nofail(s->inotify_fd);
53 static void path_done(Unit *u) {
59 while ((s = p->specs)) {
60 path_unwatch_one(p, s);
61 LIST_REMOVE(PathSpec, spec, p->specs, s);
67 int path_add_one_mount_link(Path *p, Mount *m) {
74 if (p->meta.load_state != UNIT_LOADED ||
75 m->meta.load_state != UNIT_LOADED)
78 LIST_FOREACH(spec, s, p->specs) {
80 if (!path_startswith(s->path, m->where))
83 if ((r = unit_add_two_dependencies(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
90 static int path_add_mount_links(Path *p) {
96 LIST_FOREACH(units_per_type, other, p->meta.manager->units_per_type[UNIT_MOUNT])
97 if ((r = path_add_one_mount_link(p, (Mount*) other)) < 0)
103 static int path_verify(Path *p) {
106 if (p->meta.load_state != UNIT_LOADED)
110 log_error("%s lacks path setting. Refusing.", p->meta.id);
117 static int path_add_default_dependencies(Path *p) {
122 if (p->meta.manager->running_as == MANAGER_SYSTEM) {
123 if ((r = unit_add_dependency_by_name(UNIT(p), UNIT_BEFORE, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
126 if ((r = unit_add_two_dependencies_by_name(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0)
130 return unit_add_two_dependencies_by_name(UNIT(p), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
133 static int path_load(Unit *u) {
138 assert(u->meta.load_state == UNIT_STUB);
140 if ((r = unit_load_fragment_and_dropin(u)) < 0)
143 if (u->meta.load_state == UNIT_LOADED) {
146 if ((r = unit_load_related_unit(u, ".service", &p->unit)))
149 if ((r = unit_add_dependency(u, UNIT_BEFORE, p->unit, true)) < 0)
152 if ((r = path_add_mount_links(p)) < 0)
155 if (p->meta.default_dependencies)
156 if ((r = path_add_default_dependencies(p)) < 0)
160 return path_verify(p);
163 static void path_dump(Unit *u, FILE *f, const char *prefix) {
173 prefix, path_state_to_string(p->state),
174 prefix, p->unit->meta.id);
176 LIST_FOREACH(spec, s, p->specs)
180 path_type_to_string(s->type),
184 static int path_watch_one(Path *p, PathSpec *s) {
185 static const int flags_table[_PATH_TYPE_MAX] = {
186 [PATH_EXISTS] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB,
187 [PATH_CHANGED] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO,
188 [PATH_DIRECTORY_NOT_EMPTY] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CREATE|IN_MOVED_TO
198 path_unwatch_one(p, s);
200 if (!(k = strdup(s->path)))
203 if ((s->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC)) < 0) {
208 if (unit_watch_fd(UNIT(p), s->inotify_fd, EPOLLIN, &s->watch) < 0) {
213 if ((s->primary_wd = inotify_add_watch(s->inotify_fd, k, flags_table[s->type])) >= 0)
219 /* This assumes the path was passed through path_kill_slashes()! */
220 if (!(slash = strrchr(k, '/')))
223 /* Trim the path at the last slash. Keep the slash if it's the root dir. */
224 slash[slash == k] = 0;
226 flags = IN_MOVE_SELF;
228 flags |= IN_DELETE_SELF | IN_ATTRIB | IN_CREATE | IN_MOVED_TO;
230 if (inotify_add_watch(s->inotify_fd, k, flags) >= 0)
232 } while (slash != k);
239 path_unwatch_one(p, s);
243 static void path_unwatch(Path *p) {
248 LIST_FOREACH(spec, s, p->specs)
249 path_unwatch_one(p, s);
252 static int path_watch(Path *p) {
258 LIST_FOREACH(spec, s, p->specs)
259 if ((r = path_watch_one(p, s)) < 0)
265 static void path_set_state(Path *p, PathState state) {
269 old_state = p->state;
272 if (state != PATH_WAITING &&
273 (state != PATH_RUNNING || p->inotify_triggered))
276 if (state != old_state)
277 log_debug("%s changed %s -> %s",
279 path_state_to_string(old_state),
280 path_state_to_string(state));
282 unit_notify(UNIT(p), state_translation_table[old_state], state_translation_table[state], true);
285 static void path_enter_waiting(Path *p, bool initial, bool recheck, bool skip_watch);
287 static int path_coldplug(Unit *u) {
291 assert(p->state == PATH_DEAD);
293 if (p->deserialized_state != p->state) {
295 if (p->deserialized_state == PATH_WAITING ||
296 p->deserialized_state == PATH_RUNNING)
297 path_enter_waiting(p, true, true, false);
299 path_set_state(p, p->deserialized_state);
305 static void path_enter_dead(Path *p, bool success) {
311 path_set_state(p, p->failure ? PATH_FAILED : PATH_DEAD);
314 static void path_enter_running(Path *p) {
319 dbus_error_init(&error);
321 /* Don't start job if we are supposed to go down */
322 if (p->meta.job && p->meta.job->type == JOB_STOP)
325 if ((r = manager_add_job(p->meta.manager, JOB_START, p->unit, JOB_REPLACE, true, &error, NULL)) < 0)
328 p->inotify_triggered = false;
330 if ((r = path_watch(p)) < 0)
333 path_set_state(p, PATH_RUNNING);
337 log_warning("%s failed to queue unit startup job: %s", p->meta.id, bus_error(&error, r));
338 path_enter_dead(p, false);
340 dbus_error_free(&error);
344 static void path_enter_waiting(Path *p, bool initial, bool recheck, bool skip_watch) {
352 LIST_FOREACH(spec, s, p->specs) {
357 good = access(s->path, F_OK) >= 0;
360 case PATH_DIRECTORY_NOT_EMPTY: {
363 k = dir_is_empty(s->path);
364 good = !(k == -ENOENT || k > 0);
371 b = access(s->path, F_OK) >= 0;
372 good = !initial && b != s->previous_exists;
373 s->previous_exists = b;
386 log_debug("%s got triggered.", p->meta.id);
387 path_enter_running(p);
393 if ((r = path_watch(p)) < 0)
396 /* Hmm, so now we have created inotify watches, but the file
397 * might have appeared/been removed by now, so we must
399 path_enter_waiting(p, false, true, true);
403 path_set_state(p, PATH_WAITING);
407 log_warning("%s failed to enter waiting state: %s", p->meta.id, strerror(-r));
408 path_enter_dead(p, false);
411 static int path_start(Unit *u) {
415 assert(p->state == PATH_DEAD || p->state == PATH_FAILED);
417 if (p->unit->meta.load_state != UNIT_LOADED)
421 path_enter_waiting(p, true, true, false);
426 static int path_stop(Unit *u) {
430 assert(p->state == PATH_WAITING || p->state == PATH_RUNNING);
432 path_enter_dead(p, true);
436 static int path_serialize(Unit *u, FILE *f, FDSet *fds) {
443 unit_serialize_item(u, f, "state", path_state_to_string(p->state));
448 static int path_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
456 if (streq(key, "state")) {
459 if ((state = path_state_from_string(value)) < 0)
460 log_debug("Failed to parse state value %s", value);
462 p->deserialized_state = state;
464 log_debug("Unknown serialization key '%s'", key);
469 static UnitActiveState path_active_state(Unit *u) {
472 return state_translation_table[PATH(u)->state];
475 static const char *path_sub_state_to_string(Unit *u) {
478 return path_state_to_string(PATH(u)->state);
481 static void path_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
486 struct inotify_event *e;
493 if (p->state != PATH_WAITING &&
494 p->state != PATH_RUNNING)
497 /* log_debug("inotify wakeup on %s.", u->meta.id); */
499 if (events != EPOLLIN) {
500 log_error("Got Invalid poll event on inotify.");
504 LIST_FOREACH(spec, s, p->specs)
505 if (s->inotify_fd == fd)
509 log_error("Got event on unknown fd.");
513 if (ioctl(fd, FIONREAD, &l) < 0) {
514 log_error("FIONREAD failed: %s", strerror(errno));
518 if (!(buf = malloc(l))) {
519 log_error("Failed to allocate buffer: %s", strerror(-ENOMEM));
523 if ((k = read(fd, buf, l)) < 0) {
524 log_error("Failed to read inotify event: %s", strerror(-errno));
528 /* If we are already running, then remember that one event was
529 * dispatched so that we restart the service only if something
530 * actually changed on disk */
531 p->inotify_triggered = true;
533 e = (struct inotify_event*) buf;
539 if (s->type == PATH_CHANGED && s->primary_wd == e->wd)
542 step = sizeof(struct inotify_event) + e->len;
543 assert(step <= (size_t) k);
545 e = (struct inotify_event*) ((uint8_t*) e + step);
550 path_enter_running(p);
552 path_enter_waiting(p, false, true, false);
560 path_enter_dead(p, false);
563 void path_unit_notify(Unit *u, UnitActiveState new_state) {
568 if (u->meta.type == UNIT_PATH)
571 SET_FOREACH(n, u->meta.names, i) {
576 if (!(k = unit_name_change_suffix(n, ".path"))) {
581 t = manager_get_unit(u->meta.manager, k);
587 if (t->meta.load_state != UNIT_LOADED)
595 if (p->state == PATH_RUNNING && new_state == UNIT_INACTIVE) {
596 log_debug("%s got notified about unit deactivation.", p->meta.id);
598 /* Hmm, so inotify was triggered since the
599 * last activation, so I guess we need to
600 * recheck what is going on. */
601 path_enter_waiting(p, false, p->inotify_triggered, false);
608 log_error("Failed find path unit: %s", strerror(-r));
611 static void path_reset_failed(Unit *u) {
616 if (p->state == PATH_FAILED)
617 path_set_state(p, PATH_DEAD);
622 static const char* const path_state_table[_PATH_STATE_MAX] = {
623 [PATH_DEAD] = "dead",
624 [PATH_WAITING] = "waiting",
625 [PATH_RUNNING] = "running",
626 [PATH_FAILED] = "failed"
629 DEFINE_STRING_TABLE_LOOKUP(path_state, PathState);
631 static const char* const path_type_table[_PATH_TYPE_MAX] = {
632 [PATH_EXISTS] = "PathExists",
633 [PATH_CHANGED] = "PathChanged",
634 [PATH_DIRECTORY_NOT_EMPTY] = "DirectoryNotEmpty"
637 DEFINE_STRING_TABLE_LOOKUP(path_type, PathType);
639 const UnitVTable path_vtable = {
645 .coldplug = path_coldplug,
652 .serialize = path_serialize,
653 .deserialize_item = path_deserialize_item,
655 .active_state = path_active_state,
656 .sub_state_to_string = path_sub_state_to_string,
658 .fd_event = path_fd_event,
660 .reset_failed = path_reset_failed,
662 .bus_interface = "org.freedesktop.systemd1.Path",
663 .bus_message_handler = bus_path_message_handler