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 int path_spec_watch(PathSpec *s, Unit *u) {
44 static const int flags_table[_PATH_TYPE_MAX] = {
45 [PATH_EXISTS] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB,
46 [PATH_EXISTS_GLOB] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB,
47 [PATH_CHANGED] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO,
48 [PATH_MODIFIED] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO|IN_MODIFY,
49 [PATH_DIRECTORY_NOT_EMPTY] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CREATE|IN_MOVED_TO
59 path_spec_unwatch(s, u);
61 if (!(k = strdup(s->path)))
64 if ((s->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC)) < 0) {
69 if (unit_watch_fd(u, s->inotify_fd, EPOLLIN, &s->watch) < 0) {
74 if ((s->primary_wd = inotify_add_watch(s->inotify_fd, k, flags_table[s->type])) >= 0)
80 /* This assumes the path was passed through path_kill_slashes()! */
81 if (!(slash = strrchr(k, '/')))
84 /* Trim the path at the last slash. Keep the slash if it's the root dir. */
85 slash[slash == k] = 0;
89 flags |= IN_DELETE_SELF | IN_ATTRIB | IN_CREATE | IN_MOVED_TO;
91 if (inotify_add_watch(s->inotify_fd, k, flags) >= 0)
100 path_spec_unwatch(s, u);
104 void path_spec_unwatch(PathSpec *s, Unit *u) {
106 if (s->inotify_fd < 0)
109 unit_unwatch_fd(u, &s->watch);
111 close_nointr_nofail(s->inotify_fd);
115 int path_spec_fd_event(PathSpec *s, uint32_t events) {
117 struct inotify_event *e;
122 if (events != EPOLLIN) {
123 log_error("Got Invalid poll event on inotify.");
128 if (ioctl(s->inotify_fd, FIONREAD, &l) < 0) {
129 log_error("FIONREAD failed: %m");
136 if (!(buf = malloc(l))) {
137 log_error("Failed to allocate buffer: %m");
142 if ((k = read(s->inotify_fd, buf, l)) < 0) {
143 log_error("Failed to read inotify event: %m");
148 e = (struct inotify_event*) buf;
153 if ((s->type == PATH_CHANGED || s->type == PATH_MODIFIED) &&
154 s->primary_wd == e->wd)
157 step = sizeof(struct inotify_event) + e->len;
158 assert(step <= (size_t) k);
160 e = (struct inotify_event*) ((uint8_t*) e + step);
168 static bool path_spec_check_good(PathSpec *s, bool initial) {
174 good = access(s->path, F_OK) >= 0;
177 case PATH_EXISTS_GLOB:
178 good = glob_exists(s->path) > 0;
181 case PATH_DIRECTORY_NOT_EMPTY: {
184 k = dir_is_empty(s->path);
185 good = !(k == -ENOENT || k > 0);
190 case PATH_MODIFIED: {
193 b = access(s->path, F_OK) >= 0;
194 good = !initial && b != s->previous_exists;
195 s->previous_exists = b;
206 static bool path_spec_startswith(PathSpec *s, const char *what) {
207 return path_startswith(s->path, what);
210 static void path_spec_mkdir(PathSpec *s, mode_t mode) {
213 if (s->type == PATH_EXISTS || s->type == PATH_EXISTS_GLOB)
216 if ((r = mkdir_p(s->path, mode)) < 0)
217 log_warning("mkdir(%s) failed: %s", s->path, strerror(-r));
220 static void path_spec_dump(PathSpec *s, FILE *f, const char *prefix) {
224 path_type_to_string(s->type),
228 void path_spec_done(PathSpec *s) {
230 assert(s->inotify_fd == -1);
235 static void path_init(Unit *u) {
239 assert(u->meta.load_state == UNIT_STUB);
241 p->directory_mode = 0755;
244 static void path_done(Unit *u) {
250 unit_ref_unset(&p->unit);
252 while ((s = p->specs)) {
253 path_spec_unwatch(s, u);
254 LIST_REMOVE(PathSpec, spec, p->specs, s);
260 int path_add_one_mount_link(Path *p, Mount *m) {
267 if (p->meta.load_state != UNIT_LOADED ||
268 m->meta.load_state != UNIT_LOADED)
271 LIST_FOREACH(spec, s, p->specs) {
273 if (!path_spec_startswith(s, m->where))
276 if ((r = unit_add_two_dependencies(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
283 static int path_add_mount_links(Path *p) {
289 LIST_FOREACH(units_by_type, other, p->meta.manager->units_by_type[UNIT_MOUNT])
290 if ((r = path_add_one_mount_link(p, (Mount*) other)) < 0)
296 static int path_verify(Path *p) {
299 if (p->meta.load_state != UNIT_LOADED)
303 log_error("%s lacks path setting. Refusing.", p->meta.id);
310 static int path_add_default_dependencies(Path *p) {
315 if (p->meta.manager->running_as == MANAGER_SYSTEM) {
316 if ((r = unit_add_dependency_by_name(UNIT(p), UNIT_BEFORE, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
319 if ((r = unit_add_two_dependencies_by_name(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0)
323 return unit_add_two_dependencies_by_name(UNIT(p), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
326 static int path_load(Unit *u) {
331 assert(u->meta.load_state == UNIT_STUB);
333 if ((r = unit_load_fragment_and_dropin(u)) < 0)
336 if (u->meta.load_state == UNIT_LOADED) {
338 if (!UNIT_DEREF(p->unit)) {
341 r = unit_load_related_unit(u, ".service", &x);
345 unit_ref_set(&p->unit, x);
348 r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(p->unit), true);
352 if ((r = path_add_mount_links(p)) < 0)
355 if (p->meta.default_dependencies)
356 if ((r = path_add_default_dependencies(p)) < 0)
360 return path_verify(p);
363 static void path_dump(Unit *u, FILE *f, const char *prefix) {
373 "%sMakeDirectory: %s\n"
374 "%sDirectoryMode: %04o\n",
375 prefix, path_state_to_string(p->state),
376 prefix, UNIT_DEREF(p->unit)->meta.id,
377 prefix, yes_no(p->make_directory),
378 prefix, p->directory_mode);
380 LIST_FOREACH(spec, s, p->specs)
381 path_spec_dump(s, f, prefix);
384 static void path_unwatch(Path *p) {
389 LIST_FOREACH(spec, s, p->specs)
390 path_spec_unwatch(s, UNIT(p));
393 static int path_watch(Path *p) {
399 LIST_FOREACH(spec, s, p->specs)
400 if ((r = path_spec_watch(s, UNIT(p))) < 0)
406 static void path_set_state(Path *p, PathState state) {
410 old_state = p->state;
413 if (state != PATH_WAITING &&
414 (state != PATH_RUNNING || p->inotify_triggered))
417 if (state != old_state)
418 log_debug("%s changed %s -> %s",
420 path_state_to_string(old_state),
421 path_state_to_string(state));
423 unit_notify(UNIT(p), state_translation_table[old_state], state_translation_table[state], true);
426 static void path_enter_waiting(Path *p, bool initial, bool recheck);
428 static int path_coldplug(Unit *u) {
432 assert(p->state == PATH_DEAD);
434 if (p->deserialized_state != p->state) {
436 if (p->deserialized_state == PATH_WAITING ||
437 p->deserialized_state == PATH_RUNNING)
438 path_enter_waiting(p, true, true);
440 path_set_state(p, p->deserialized_state);
446 static void path_enter_dead(Path *p, bool success) {
452 path_set_state(p, p->failure ? PATH_FAILED : PATH_DEAD);
455 static void path_enter_running(Path *p) {
460 dbus_error_init(&error);
462 /* Don't start job if we are supposed to go down */
463 if (p->meta.job && p->meta.job->type == JOB_STOP)
466 if ((r = manager_add_job(p->meta.manager, JOB_START, UNIT_DEREF(p->unit), JOB_REPLACE, true, &error, NULL)) < 0)
469 p->inotify_triggered = false;
471 if ((r = path_watch(p)) < 0)
474 path_set_state(p, PATH_RUNNING);
478 log_warning("%s failed to queue unit startup job: %s", p->meta.id, bus_error(&error, r));
479 path_enter_dead(p, false);
481 dbus_error_free(&error);
484 static bool path_check_good(Path *p, bool initial) {
490 LIST_FOREACH(spec, s, p->specs) {
491 good = path_spec_check_good(s, initial);
500 static void path_enter_waiting(Path *p, bool initial, bool recheck) {
504 if (path_check_good(p, initial)) {
505 log_debug("%s got triggered.", p->meta.id);
506 path_enter_running(p);
510 if ((r = path_watch(p)) < 0)
513 /* Hmm, so now we have created inotify watches, but the file
514 * might have appeared/been removed by now, so we must
518 if (path_check_good(p, false)) {
519 log_debug("%s got triggered.", p->meta.id);
520 path_enter_running(p);
524 path_set_state(p, PATH_WAITING);
528 log_warning("%s failed to enter waiting state: %s", p->meta.id, strerror(-r));
529 path_enter_dead(p, false);
532 static void path_mkdir(Path *p) {
537 if (!p->make_directory)
540 LIST_FOREACH(spec, s, p->specs)
541 path_spec_mkdir(s, p->directory_mode);
544 static int path_start(Unit *u) {
548 assert(p->state == PATH_DEAD || p->state == PATH_FAILED);
550 if (UNIT_DEREF(p->unit)->meta.load_state != UNIT_LOADED)
556 path_enter_waiting(p, true, true);
561 static int path_stop(Unit *u) {
565 assert(p->state == PATH_WAITING || p->state == PATH_RUNNING);
567 path_enter_dead(p, true);
571 static int path_serialize(Unit *u, FILE *f, FDSet *fds) {
578 unit_serialize_item(u, f, "state", path_state_to_string(p->state));
583 static int path_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
591 if (streq(key, "state")) {
594 if ((state = path_state_from_string(value)) < 0)
595 log_debug("Failed to parse state value %s", value);
597 p->deserialized_state = state;
599 log_debug("Unknown serialization key '%s'", key);
604 static UnitActiveState path_active_state(Unit *u) {
607 return state_translation_table[PATH(u)->state];
610 static const char *path_sub_state_to_string(Unit *u) {
613 return path_state_to_string(PATH(u)->state);
616 static void path_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
624 if (p->state != PATH_WAITING &&
625 p->state != PATH_RUNNING)
628 /* log_debug("inotify wakeup on %s.", u->meta.id); */
630 LIST_FOREACH(spec, s, p->specs)
631 if (path_spec_owns_inotify_fd(s, fd))
635 log_error("Got event on unknown fd.");
639 changed = path_spec_fd_event(s, events);
643 /* If we are already running, then remember that one event was
644 * dispatched so that we restart the service only if something
645 * actually changed on disk */
646 p->inotify_triggered = true;
649 path_enter_running(p);
651 path_enter_waiting(p, false, true);
656 path_enter_dead(p, false);
659 void path_unit_notify(Unit *u, UnitActiveState new_state) {
663 if (u->meta.type == UNIT_PATH)
666 SET_FOREACH(k, u->meta.dependencies[UNIT_TRIGGERED_BY], i) {
669 if (k->meta.type != UNIT_PATH)
672 if (k->meta.load_state != UNIT_LOADED)
677 if (p->state == PATH_RUNNING && new_state == UNIT_INACTIVE) {
678 log_debug("%s got notified about unit deactivation.", p->meta.id);
680 /* Hmm, so inotify was triggered since the
681 * last activation, so I guess we need to
682 * recheck what is going on. */
683 path_enter_waiting(p, false, p->inotify_triggered);
688 static void path_reset_failed(Unit *u) {
693 if (p->state == PATH_FAILED)
694 path_set_state(p, PATH_DEAD);
699 static const char* const path_state_table[_PATH_STATE_MAX] = {
700 [PATH_DEAD] = "dead",
701 [PATH_WAITING] = "waiting",
702 [PATH_RUNNING] = "running",
703 [PATH_FAILED] = "failed"
706 DEFINE_STRING_TABLE_LOOKUP(path_state, PathState);
708 static const char* const path_type_table[_PATH_TYPE_MAX] = {
709 [PATH_EXISTS] = "PathExists",
710 [PATH_EXISTS_GLOB] = "PathExistsGlob",
711 [PATH_CHANGED] = "PathChanged",
712 [PATH_MODIFIED] = "PathModified",
713 [PATH_DIRECTORY_NOT_EMPTY] = "DirectoryNotEmpty"
716 DEFINE_STRING_TABLE_LOOKUP(path_type, PathType);
718 const UnitVTable path_vtable = {
729 .coldplug = path_coldplug,
736 .serialize = path_serialize,
737 .deserialize_item = path_deserialize_item,
739 .active_state = path_active_state,
740 .sub_state_to_string = path_sub_state_to_string,
742 .fd_event = path_fd_event,
744 .reset_failed = path_reset_failed,
746 .bus_interface = "org.freedesktop.systemd1.Path",
747 .bus_message_handler = bus_path_message_handler