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_done(Unit *u) {
48 while ((s = p->specs)) {
49 LIST_REMOVE(PathSpec, spec, p->specs, s);
54 int path_add_one_mount_link(Path *p, Mount *m) {
61 if (p->meta.load_state != UNIT_LOADED ||
62 m->meta.load_state != UNIT_LOADED)
65 LIST_FOREACH(spec, s, p->specs) {
67 if (!path_startswith(s->path, m->where))
70 if ((r = unit_add_two_dependencies(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
77 static int path_add_mount_links(Path *p) {
83 LIST_FOREACH(units_per_type, other, p->meta.manager->units_per_type[UNIT_MOUNT])
84 if ((r = path_add_one_mount_link(p, (Mount*) other)) < 0)
90 static int path_verify(Path *p) {
93 if (p->meta.load_state != UNIT_LOADED)
97 log_error("%s lacks path setting. Refusing.", p->meta.id);
104 static int path_add_default_dependencies(Path *p) {
109 if (p->meta.manager->running_as == MANAGER_SYSTEM) {
110 if ((r = unit_add_dependency_by_name(UNIT(p), UNIT_BEFORE, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
113 if ((r = unit_add_two_dependencies_by_name(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0)
117 return unit_add_two_dependencies_by_name(UNIT(p), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
120 static int path_load(Unit *u) {
125 assert(u->meta.load_state == UNIT_STUB);
127 if ((r = unit_load_fragment_and_dropin(u)) < 0)
130 if (u->meta.load_state == UNIT_LOADED) {
133 if ((r = unit_load_related_unit(u, ".service", &p->unit)))
136 if ((r = unit_add_dependency(u, UNIT_BEFORE, p->unit, true)) < 0)
139 if ((r = path_add_mount_links(p)) < 0)
142 if (p->meta.default_dependencies)
143 if ((r = path_add_default_dependencies(p)) < 0)
147 return path_verify(p);
150 static void path_dump(Unit *u, FILE *f, const char *prefix) {
160 prefix, path_state_to_string(p->state),
161 prefix, p->unit->meta.id);
163 LIST_FOREACH(spec, s, p->specs)
167 path_type_to_string(s->type),
171 static void path_unwatch_one(Path *p, PathSpec *s) {
173 if (s->inotify_fd < 0)
176 unit_unwatch_fd(UNIT(p), &s->watch);
178 close_nointr_nofail(s->inotify_fd);
182 static int path_watch_one(Path *p, PathSpec *s) {
183 static const int flags_table[_PATH_TYPE_MAX] = {
184 [PATH_EXISTS] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB,
185 [PATH_CHANGED] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO,
186 [PATH_DIRECTORY_NOT_EMPTY] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CREATE|IN_MOVED_TO
196 path_unwatch_one(p, s);
198 if (!(k = strdup(s->path)))
201 if ((s->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC)) < 0) {
206 if (unit_watch_fd(UNIT(p), s->inotify_fd, EPOLLIN, &s->watch) < 0) {
211 if ((s->primary_wd = inotify_add_watch(s->inotify_fd, k, flags_table[s->type])) >= 0)
218 /* This assumes the path was passed through path_kill_slashes()! */
219 if (!(slash = strrchr(k, '/')))
224 flags = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB;
226 flags |= IN_CREATE | IN_MOVED_TO;
228 if (inotify_add_watch(s->inotify_fd, k, flags) >= 0)
237 path_unwatch_one(p, s);
241 static void path_unwatch(Path *p) {
246 LIST_FOREACH(spec, s, p->specs)
247 path_unwatch_one(p, s);
250 static int path_watch(Path *p) {
256 LIST_FOREACH(spec, s, p->specs)
257 if ((r = path_watch_one(p, s)) < 0)
263 static void path_set_state(Path *p, PathState state) {
267 old_state = p->state;
270 if (state != PATH_WAITING &&
271 (state != PATH_RUNNING || p->inotify_triggered))
274 if (state != old_state)
275 log_debug("%s changed %s -> %s",
277 path_state_to_string(old_state),
278 path_state_to_string(state));
280 unit_notify(UNIT(p), state_translation_table[old_state], state_translation_table[state]);
283 static void path_enter_waiting(Path *p, bool initial, bool recheck);
285 static int path_coldplug(Unit *u) {
289 assert(p->state == PATH_DEAD);
291 if (p->deserialized_state != p->state) {
293 if (p->deserialized_state == PATH_WAITING ||
294 p->deserialized_state == PATH_RUNNING)
295 path_enter_waiting(p, true, true);
297 path_set_state(p, p->deserialized_state);
303 static void path_enter_dead(Path *p, bool success) {
309 path_set_state(p, p->failure ? PATH_FAILED : PATH_DEAD);
312 static void path_enter_running(Path *p) {
317 dbus_error_init(&error);
319 /* Don't start job if we are supposed to go down */
320 if (p->meta.job && p->meta.job->type == JOB_STOP)
323 if ((r = manager_add_job(p->meta.manager, JOB_START, p->unit, JOB_REPLACE, true, &error, NULL)) < 0)
326 p->inotify_triggered = false;
328 if ((r = path_watch(p)) < 0)
331 path_set_state(p, PATH_RUNNING);
335 log_warning("%s failed to queue unit startup job: %s", p->meta.id, bus_error(&error, r));
336 path_enter_dead(p, false);
338 dbus_error_free(&error);
342 static void path_enter_waiting(Path *p, bool initial, bool recheck) {
350 LIST_FOREACH(spec, s, p->specs) {
355 good = access(s->path, F_OK) >= 0;
358 case PATH_DIRECTORY_NOT_EMPTY:
359 good = dir_is_empty(s->path) == 0;
365 b = access(s->path, F_OK) >= 0;
366 good = !initial && b != s->previous_exists;
367 s->previous_exists = b;
380 path_enter_running(p);
385 if ((r = path_watch(p)) < 0)
388 path_set_state(p, PATH_WAITING);
392 log_warning("%s failed to enter waiting state: %s", p->meta.id, strerror(-r));
393 path_enter_dead(p, false);
396 static int path_start(Unit *u) {
400 assert(p->state == PATH_DEAD || p->state == PATH_FAILED);
402 if (p->unit->meta.load_state != UNIT_LOADED)
406 path_enter_waiting(p, true, true);
411 static int path_stop(Unit *u) {
415 assert(p->state == PATH_WAITING || p->state == PATH_RUNNING);
417 path_enter_dead(p, true);
421 static int path_serialize(Unit *u, FILE *f, FDSet *fds) {
428 unit_serialize_item(u, f, "state", path_state_to_string(p->state));
433 static int path_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
441 if (streq(key, "state")) {
444 if ((state = path_state_from_string(value)) < 0)
445 log_debug("Failed to parse state value %s", value);
447 p->deserialized_state = state;
449 log_debug("Unknown serialization key '%s'", key);
454 static UnitActiveState path_active_state(Unit *u) {
457 return state_translation_table[PATH(u)->state];
460 static const char *path_sub_state_to_string(Unit *u) {
463 return path_state_to_string(PATH(u)->state);
466 static void path_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
471 struct inotify_event *e;
478 if (p->state != PATH_WAITING &&
479 p->state != PATH_RUNNING)
482 log_debug("inotify wakeup on %s.", u->meta.id);
484 if (events != EPOLLIN) {
485 log_error("Got Invalid poll event on inotify.");
489 LIST_FOREACH(spec, s, p->specs)
490 if (s->inotify_fd == fd)
494 log_error("Got event on unknown fd.");
498 if (ioctl(fd, FIONREAD, &l) < 0) {
499 log_error("FIONREAD failed: %s", strerror(errno));
503 if (!(buf = malloc(l))) {
504 log_error("Failed to allocate buffer: %s", strerror(-ENOMEM));
508 if ((k = read(fd, buf, l)) < 0) {
509 log_error("Failed to read inotify event: %s", strerror(-errno));
513 /* If we are already running, then remember that one event was
514 * dispatched so that we restart the service only if something
515 * actually changed on disk */
516 p->inotify_triggered = true;
518 e = (struct inotify_event*) buf;
524 if (s->type == PATH_CHANGED && s->primary_wd == e->wd)
527 step = sizeof(struct inotify_event) + e->len;
528 assert(step <= (size_t) k);
530 e = (struct inotify_event*) ((uint8_t*) e + step);
535 path_enter_running(p);
537 path_enter_waiting(p, false, true);
545 path_enter_dead(p, false);
548 void path_unit_notify(Unit *u, UnitActiveState new_state) {
553 if (u->meta.type == UNIT_PATH)
556 SET_FOREACH(n, u->meta.names, i) {
561 if (!(k = unit_name_change_suffix(n, ".path"))) {
566 t = manager_get_unit(u->meta.manager, k);
572 if (t->meta.load_state != UNIT_LOADED)
580 if (p->state == PATH_RUNNING && new_state == UNIT_INACTIVE) {
581 log_debug("%s got notified about unit deactivation.", p->meta.id);
583 /* Hmm, so inotify was triggered since the
584 * last activation, so I guess we need to
585 * recheck what is going on. */
586 path_enter_waiting(p, false, p->inotify_triggered);
593 log_error("Failed find path unit: %s", strerror(-r));
596 static void path_reset_failed(Unit *u) {
601 if (p->state == PATH_FAILED)
602 path_set_state(p, PATH_DEAD);
607 static const char* const path_state_table[_PATH_STATE_MAX] = {
608 [PATH_DEAD] = "dead",
609 [PATH_WAITING] = "waiting",
610 [PATH_RUNNING] = "running",
611 [PATH_FAILED] = "failed"
614 DEFINE_STRING_TABLE_LOOKUP(path_state, PathState);
616 static const char* const path_type_table[_PATH_TYPE_MAX] = {
617 [PATH_EXISTS] = "PathExists",
618 [PATH_CHANGED] = "PathChanged",
619 [PATH_DIRECTORY_NOT_EMPTY] = "DirectoryNotEmpty"
622 DEFINE_STRING_TABLE_LOOKUP(path_type, PathType);
624 const UnitVTable path_vtable = {
630 .coldplug = path_coldplug,
637 .serialize = path_serialize,
638 .deserialize_item = path_deserialize_item,
640 .active_state = path_active_state,
641 .sub_state_to_string = path_sub_state_to_string,
643 .fd_event = path_fd_event,
645 .reset_failed = path_reset_failed,
647 .bus_interface = "org.freedesktop.systemd1.Path",
648 .bus_message_handler = bus_path_message_handler