1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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_MAINTENANCE] = UNIT_MAINTENANCE
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_load(Unit *u) {
109 assert(u->meta.load_state == UNIT_STUB);
111 if ((r = unit_load_fragment_and_dropin(u)) < 0)
114 if (u->meta.load_state == UNIT_LOADED) {
117 if ((r = unit_load_related_unit(u, ".service", &p->unit)))
120 if ((r = unit_add_dependency(u, UNIT_BEFORE, p->unit, true)) < 0)
123 if ((r = path_add_mount_links(p)) < 0)
126 /* Path units shouldn't stay around on shutdown */
127 if (p->meta.default_dependencies)
128 if ((r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
132 return path_verify(p);
135 static void path_dump(Unit *u, FILE *f, const char *prefix) {
141 p2 = strappend(prefix, "\t");
142 prefix2 = p2 ? p2 : prefix;
147 prefix, path_state_to_string(p->state),
148 prefix, p->unit->meta.id);
150 LIST_FOREACH(spec, s, p->specs)
154 path_type_to_string(s->type),
160 static void path_unwatch_one(Path *p, PathSpec *s) {
162 if (s->inotify_fd < 0)
165 unit_unwatch_fd(UNIT(p), &s->watch);
167 close_nointr_nofail(s->inotify_fd);
171 static int path_watch_one(Path *p, PathSpec *s) {
172 static const int flags_table[_PATH_TYPE_MAX] = {
173 [PATH_EXISTS] = IN_DELETE_SELF|IN_MOVE_SELF,
174 [PATH_CHANGED] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO,
175 [PATH_DIRECTORY_NOT_EMPTY] = IN_DELETE_SELF|IN_MOVE_SELF|IN_CREATE|IN_MOVED_TO
185 path_unwatch_one(p, s);
187 if (!(k = strdup(s->path)))
190 if ((s->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC)) < 0) {
195 if (unit_watch_fd(UNIT(p), s->inotify_fd, EPOLLIN, &s->watch) < 0) {
200 if ((s->primary_wd = inotify_add_watch(s->inotify_fd, k, flags_table[s->type])) >= 0)
207 /* This assumes the path was passed through path_kill_slashes()! */
208 if (!(slash = strrchr(k, '/')))
213 flags = IN_DELETE_SELF|IN_MOVE_SELF;
215 flags |= IN_CREATE | IN_MOVED_TO | IN_ATTRIB;
217 if (inotify_add_watch(s->inotify_fd, k, flags) >= 0)
226 path_unwatch_one(p, s);
230 static void path_unwatch(Path *p) {
235 LIST_FOREACH(spec, s, p->specs)
236 path_unwatch_one(p, s);
239 static int path_watch(Path *p) {
245 LIST_FOREACH(spec, s, p->specs)
246 if ((r = path_watch_one(p, s)) < 0)
252 static void path_set_state(Path *p, PathState state) {
256 old_state = p->state;
259 if (state != PATH_WAITING)
262 if (state != old_state)
263 log_debug("%s changed %s -> %s",
265 path_state_to_string(old_state),
266 path_state_to_string(state));
268 unit_notify(UNIT(p), state_translation_table[old_state], state_translation_table[state]);
271 static void path_enter_waiting(Path *p, bool initial);
273 static int path_coldplug(Unit *u) {
277 assert(p->state == PATH_DEAD);
279 if (p->deserialized_state != p->state) {
281 if (p->deserialized_state == PATH_WAITING ||
282 p->deserialized_state == PATH_RUNNING)
283 path_enter_waiting(p, true);
285 path_set_state(p, p->deserialized_state);
291 static void path_enter_dead(Path *p, bool success) {
297 path_set_state(p, p->failure ? PATH_MAINTENANCE : PATH_DEAD);
300 static void path_enter_running(Path *p) {
305 dbus_error_init(&error);
307 /* Don't start job if we are supposed to go down */
308 if (p->meta.job && p->meta.job->type == JOB_STOP)
311 if ((r = manager_add_job(p->meta.manager, JOB_START, p->unit, JOB_REPLACE, true, &error, NULL)) < 0)
314 path_set_state(p, PATH_RUNNING);
318 log_warning("%s failed to queue unit startup job: %s", p->meta.id, bus_error(&error, r));
319 path_enter_dead(p, false);
321 dbus_error_free(&error);
325 static void path_enter_waiting(Path *p, bool initial) {
330 LIST_FOREACH(spec, s, p->specs) {
335 good = access(s->path, F_OK) >= 0;
338 case PATH_DIRECTORY_NOT_EMPTY:
339 good = dir_is_empty(s->path) == 0;
345 b = access(s->path, F_OK) >= 0;
346 good = !initial && b != s->previous_exists;
347 s->previous_exists = b;
360 path_enter_running(p);
364 if ((r = path_watch(p)) < 0)
367 path_set_state(p, PATH_WAITING);
371 log_warning("%s failed to enter waiting state: %s", p->meta.id, strerror(-r));
372 path_enter_dead(p, false);
375 static int path_start(Unit *u) {
379 assert(p->state == PATH_DEAD || p->state == PATH_MAINTENANCE);
381 if (p->unit->meta.load_state != UNIT_LOADED)
385 path_enter_waiting(p, true);
389 static int path_stop(Unit *u) {
393 assert(p->state == PATH_WAITING || p->state == PATH_RUNNING);
395 path_enter_dead(p, true);
399 static int path_serialize(Unit *u, FILE *f, FDSet *fds) {
406 unit_serialize_item(u, f, "state", path_state_to_string(p->state));
411 static int path_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
419 if (streq(key, "state")) {
422 if ((state = path_state_from_string(value)) < 0)
423 log_debug("Failed to parse state value %s", value);
425 p->deserialized_state = state;
427 log_debug("Unknown serialization key '%s'", key);
432 static UnitActiveState path_active_state(Unit *u) {
435 return state_translation_table[PATH(u)->state];
438 static const char *path_sub_state_to_string(Unit *u) {
441 return path_state_to_string(PATH(u)->state);
444 static void path_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
448 struct inotify_event *buf = NULL;
454 if (p->state != PATH_WAITING)
457 log_debug("inotify wakeup on %s.", u->meta.id);
459 if (events != EPOLLIN) {
460 log_error("Got Invalid poll event on inotify.");
464 LIST_FOREACH(spec, s, p->specs)
465 if (s->inotify_fd == fd)
469 log_error("Got event on unknown fd.");
473 if (ioctl(fd, FIONREAD, &l) < 0) {
474 log_error("FIONREAD failed: %s", strerror(errno));
478 if (!(buf = malloc(l))) {
479 log_error("Failed to allocate buffer: %s", strerror(-ENOMEM));
483 if ((k = read(fd, buf, l)) < 0) {
484 log_error("Failed to read inotify event: %s", strerror(-errno));
488 if ((size_t) k < sizeof(struct inotify_event) ||
489 (size_t) k < sizeof(struct inotify_event) + buf->len) {
490 log_error("inotify event too small.");
494 if (s->type == PATH_CHANGED && s->primary_wd == buf->wd)
495 path_enter_running(p);
497 path_enter_waiting(p, false);
505 path_enter_dead(p, false);
508 void path_unit_notify(Unit *u, UnitActiveState new_state) {
513 if (u->meta.type == UNIT_PATH)
516 SET_FOREACH(n, u->meta.names, i) {
521 if (!(k = unit_name_change_suffix(n, ".path"))) {
526 t = manager_get_unit(u->meta.manager, k);
532 if (t->meta.load_state != UNIT_LOADED)
540 if (p->state == PATH_RUNNING && new_state == UNIT_INACTIVE) {
541 log_debug("%s got notified about unit deactivation.", p->meta.id);
542 path_enter_waiting(p, false);
549 log_error("Failed find path unit: %s", strerror(-r));
552 static const char* const path_state_table[_PATH_STATE_MAX] = {
553 [PATH_DEAD] = "dead",
554 [PATH_WAITING] = "waiting",
555 [PATH_RUNNING] = "running",
556 [PATH_MAINTENANCE] = "maintenance"
559 DEFINE_STRING_TABLE_LOOKUP(path_state, PathState);
561 static const char* const path_type_table[_PATH_TYPE_MAX] = {
562 [PATH_EXISTS] = "PathExists",
563 [PATH_CHANGED] = "PathChanged",
564 [PATH_DIRECTORY_NOT_EMPTY] = "DirectoryNotEmpty"
567 DEFINE_STRING_TABLE_LOOKUP(path_type, PathType);
569 const UnitVTable path_vtable = {
575 .coldplug = path_coldplug,
582 .serialize = path_serialize,
583 .deserialize_item = path_deserialize_item,
585 .active_state = path_active_state,
586 .sub_state_to_string = path_sub_state_to_string,
588 .fd_event = path_fd_event,
590 .bus_message_handler = bus_path_message_handler