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_init(Unit *u) {
46 assert(u->meta.load_state == UNIT_STUB);
48 p->directory_mode = 0755;
51 static void path_unwatch_one(Path *p, PathSpec *s) {
53 if (s->inotify_fd < 0)
56 unit_unwatch_fd(UNIT(p), &s->watch);
58 close_nointr_nofail(s->inotify_fd);
62 static void path_done(Unit *u) {
68 while ((s = p->specs)) {
69 path_unwatch_one(p, s);
70 LIST_REMOVE(PathSpec, spec, p->specs, s);
76 int path_add_one_mount_link(Path *p, Mount *m) {
83 if (p->meta.load_state != UNIT_LOADED ||
84 m->meta.load_state != UNIT_LOADED)
87 LIST_FOREACH(spec, s, p->specs) {
89 if (!path_startswith(s->path, m->where))
92 if ((r = unit_add_two_dependencies(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
99 static int path_add_mount_links(Path *p) {
105 LIST_FOREACH(units_by_type, other, p->meta.manager->units_by_type[UNIT_MOUNT])
106 if ((r = path_add_one_mount_link(p, (Mount*) other)) < 0)
112 static int path_verify(Path *p) {
115 if (p->meta.load_state != UNIT_LOADED)
119 log_error("%s lacks path setting. Refusing.", p->meta.id);
126 static int path_add_default_dependencies(Path *p) {
131 if (p->meta.manager->running_as == MANAGER_SYSTEM) {
132 if ((r = unit_add_dependency_by_name(UNIT(p), UNIT_BEFORE, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
135 if ((r = unit_add_two_dependencies_by_name(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0)
139 return unit_add_two_dependencies_by_name(UNIT(p), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
142 static int path_load(Unit *u) {
147 assert(u->meta.load_state == UNIT_STUB);
149 if ((r = unit_load_fragment_and_dropin(u)) < 0)
152 if (u->meta.load_state == UNIT_LOADED) {
155 if ((r = unit_load_related_unit(u, ".service", &p->unit)))
158 if ((r = unit_add_dependency(u, UNIT_BEFORE, p->unit, true)) < 0)
161 if ((r = path_add_mount_links(p)) < 0)
164 if (p->meta.default_dependencies)
165 if ((r = path_add_default_dependencies(p)) < 0)
169 return path_verify(p);
172 static void path_dump(Unit *u, FILE *f, const char *prefix) {
182 "%sMakeDirectory: %s\n"
183 "%sDirectoryMode: %04o\n",
184 prefix, path_state_to_string(p->state),
185 prefix, p->unit->meta.id,
186 prefix, yes_no(p->make_directory),
187 prefix, p->directory_mode);
189 LIST_FOREACH(spec, s, p->specs)
193 path_type_to_string(s->type),
197 static int path_watch_one(Path *p, PathSpec *s) {
198 static const int flags_table[_PATH_TYPE_MAX] = {
199 [PATH_EXISTS] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB,
200 [PATH_CHANGED] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO,
201 [PATH_DIRECTORY_NOT_EMPTY] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CREATE|IN_MOVED_TO
211 path_unwatch_one(p, s);
213 if (!(k = strdup(s->path)))
216 if ((s->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC)) < 0) {
221 if (unit_watch_fd(UNIT(p), s->inotify_fd, EPOLLIN, &s->watch) < 0) {
226 if ((s->primary_wd = inotify_add_watch(s->inotify_fd, k, flags_table[s->type])) >= 0)
232 /* This assumes the path was passed through path_kill_slashes()! */
233 if (!(slash = strrchr(k, '/')))
236 /* Trim the path at the last slash. Keep the slash if it's the root dir. */
237 slash[slash == k] = 0;
239 flags = IN_MOVE_SELF;
241 flags |= IN_DELETE_SELF | IN_ATTRIB | IN_CREATE | IN_MOVED_TO;
243 if (inotify_add_watch(s->inotify_fd, k, flags) >= 0)
245 } while (slash != k);
252 path_unwatch_one(p, s);
256 static void path_unwatch(Path *p) {
261 LIST_FOREACH(spec, s, p->specs)
262 path_unwatch_one(p, s);
265 static int path_watch(Path *p) {
271 LIST_FOREACH(spec, s, p->specs)
272 if ((r = path_watch_one(p, s)) < 0)
278 static void path_set_state(Path *p, PathState state) {
282 old_state = p->state;
285 if (state != PATH_WAITING &&
286 (state != PATH_RUNNING || p->inotify_triggered))
289 if (state != old_state)
290 log_debug("%s changed %s -> %s",
292 path_state_to_string(old_state),
293 path_state_to_string(state));
295 unit_notify(UNIT(p), state_translation_table[old_state], state_translation_table[state], true);
298 static void path_enter_waiting(Path *p, bool initial, bool recheck);
300 static int path_coldplug(Unit *u) {
304 assert(p->state == PATH_DEAD);
306 if (p->deserialized_state != p->state) {
308 if (p->deserialized_state == PATH_WAITING ||
309 p->deserialized_state == PATH_RUNNING)
310 path_enter_waiting(p, true, true);
312 path_set_state(p, p->deserialized_state);
318 static void path_enter_dead(Path *p, bool success) {
324 path_set_state(p, p->failure ? PATH_FAILED : PATH_DEAD);
327 static void path_enter_running(Path *p) {
332 dbus_error_init(&error);
334 /* Don't start job if we are supposed to go down */
335 if (p->meta.job && p->meta.job->type == JOB_STOP)
338 if ((r = manager_add_job(p->meta.manager, JOB_START, p->unit, JOB_REPLACE, true, &error, NULL)) < 0)
341 p->inotify_triggered = false;
343 if ((r = path_watch(p)) < 0)
346 path_set_state(p, PATH_RUNNING);
350 log_warning("%s failed to queue unit startup job: %s", p->meta.id, bus_error(&error, r));
351 path_enter_dead(p, false);
353 dbus_error_free(&error);
356 static bool path_check_good(Path *p, bool initial) {
362 LIST_FOREACH(spec, s, p->specs) {
367 good = access(s->path, F_OK) >= 0;
370 case PATH_DIRECTORY_NOT_EMPTY: {
373 k = dir_is_empty(s->path);
374 good = !(k == -ENOENT || k > 0);
381 b = access(s->path, F_OK) >= 0;
382 good = !initial && b != s->previous_exists;
383 s->previous_exists = b;
398 static void path_enter_waiting(Path *p, bool initial, bool recheck) {
402 if (path_check_good(p, initial)) {
403 log_debug("%s got triggered.", p->meta.id);
404 path_enter_running(p);
408 if ((r = path_watch(p)) < 0)
411 /* Hmm, so now we have created inotify watches, but the file
412 * might have appeared/been removed by now, so we must
416 if (path_check_good(p, false)) {
417 log_debug("%s got triggered.", p->meta.id);
418 path_enter_running(p);
422 path_set_state(p, PATH_WAITING);
426 log_warning("%s failed to enter waiting state: %s", p->meta.id, strerror(-r));
427 path_enter_dead(p, false);
430 static void path_mkdir(Path *p) {
435 if (!p->make_directory)
438 LIST_FOREACH(spec, s, p->specs) {
441 if (s->type == PATH_EXISTS)
444 if ((r = mkdir_p(s->path, p->directory_mode)) < 0)
445 log_warning("mkdir(%s) failed: %s", s->path, strerror(-r));
449 static int path_start(Unit *u) {
453 assert(p->state == PATH_DEAD || p->state == PATH_FAILED);
455 if (p->unit->meta.load_state != UNIT_LOADED)
461 path_enter_waiting(p, true, true);
466 static int path_stop(Unit *u) {
470 assert(p->state == PATH_WAITING || p->state == PATH_RUNNING);
472 path_enter_dead(p, true);
476 static int path_serialize(Unit *u, FILE *f, FDSet *fds) {
483 unit_serialize_item(u, f, "state", path_state_to_string(p->state));
488 static int path_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
496 if (streq(key, "state")) {
499 if ((state = path_state_from_string(value)) < 0)
500 log_debug("Failed to parse state value %s", value);
502 p->deserialized_state = state;
504 log_debug("Unknown serialization key '%s'", key);
509 static UnitActiveState path_active_state(Unit *u) {
512 return state_translation_table[PATH(u)->state];
515 static const char *path_sub_state_to_string(Unit *u) {
518 return path_state_to_string(PATH(u)->state);
521 static void path_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
526 struct inotify_event *e;
533 if (p->state != PATH_WAITING &&
534 p->state != PATH_RUNNING)
537 /* log_debug("inotify wakeup on %s.", u->meta.id); */
539 if (events != EPOLLIN) {
540 log_error("Got Invalid poll event on inotify.");
544 LIST_FOREACH(spec, s, p->specs)
545 if (s->inotify_fd == fd)
549 log_error("Got event on unknown fd.");
553 if (ioctl(fd, FIONREAD, &l) < 0) {
554 log_error("FIONREAD failed: %s", strerror(errno));
560 if (!(buf = malloc(l))) {
561 log_error("Failed to allocate buffer: %s", strerror(-ENOMEM));
565 if ((k = read(fd, buf, l)) < 0) {
566 log_error("Failed to read inotify event: %s", strerror(-errno));
570 /* If we are already running, then remember that one event was
571 * dispatched so that we restart the service only if something
572 * actually changed on disk */
573 p->inotify_triggered = true;
575 e = (struct inotify_event*) buf;
581 if (s->type == PATH_CHANGED && s->primary_wd == e->wd)
584 step = sizeof(struct inotify_event) + e->len;
585 assert(step <= (size_t) k);
587 e = (struct inotify_event*) ((uint8_t*) e + step);
592 path_enter_running(p);
594 path_enter_waiting(p, false, true);
602 path_enter_dead(p, false);
605 void path_unit_notify(Unit *u, UnitActiveState new_state) {
610 if (u->meta.type == UNIT_PATH)
613 SET_FOREACH(n, u->meta.names, i) {
618 if (!(k = unit_name_change_suffix(n, ".path"))) {
623 t = manager_get_unit(u->meta.manager, k);
629 if (t->meta.load_state != UNIT_LOADED)
637 if (p->state == PATH_RUNNING && new_state == UNIT_INACTIVE) {
638 log_debug("%s got notified about unit deactivation.", p->meta.id);
640 /* Hmm, so inotify was triggered since the
641 * last activation, so I guess we need to
642 * recheck what is going on. */
643 path_enter_waiting(p, false, p->inotify_triggered);
650 log_error("Failed find path unit: %s", strerror(-r));
653 static void path_reset_failed(Unit *u) {
658 if (p->state == PATH_FAILED)
659 path_set_state(p, PATH_DEAD);
664 static const char* const path_state_table[_PATH_STATE_MAX] = {
665 [PATH_DEAD] = "dead",
666 [PATH_WAITING] = "waiting",
667 [PATH_RUNNING] = "running",
668 [PATH_FAILED] = "failed"
671 DEFINE_STRING_TABLE_LOOKUP(path_state, PathState);
673 static const char* const path_type_table[_PATH_TYPE_MAX] = {
674 [PATH_EXISTS] = "PathExists",
675 [PATH_CHANGED] = "PathChanged",
676 [PATH_DIRECTORY_NOT_EMPTY] = "DirectoryNotEmpty"
679 DEFINE_STRING_TABLE_LOOKUP(path_type, PathType);
681 const UnitVTable path_vtable = {
688 .coldplug = path_coldplug,
695 .serialize = path_serialize,
696 .deserialize_item = path_deserialize_item,
698 .active_state = path_active_state,
699 .sub_state_to_string = path_sub_state_to_string,
701 .fd_event = path_fd_event,
703 .reset_failed = path_reset_failed,
705 .bus_interface = "org.freedesktop.systemd1.Path",
706 .bus_message_handler = bus_path_message_handler