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 static const UnitActiveState state_translation_table[_PATH_STATE_MAX] = {
34 [PATH_DEAD] = UNIT_INACTIVE,
35 [PATH_WAITING] = UNIT_ACTIVE,
36 [PATH_RUNNING] = UNIT_ACTIVE,
37 [PATH_MAINTENANCE] = UNIT_INACTIVE
40 static void path_done(Unit *u) {
46 while ((s = p->specs)) {
47 LIST_REMOVE(PathSpec, spec, p->specs, s);
52 int path_add_one_mount_link(Path *p, Mount *m) {
59 if (p->meta.load_state != UNIT_LOADED ||
60 m->meta.load_state != UNIT_LOADED)
63 LIST_FOREACH(spec, s, p->specs) {
65 if (!path_startswith(s->path, m->where))
68 if ((r = unit_add_dependency(UNIT(m), UNIT_BEFORE, UNIT(p), true)) < 0)
71 if ((r = unit_add_dependency(UNIT(p), UNIT_REQUIRES, UNIT(m), true)) < 0)
78 static int path_add_mount_links(Path *p) {
84 LIST_FOREACH(units_per_type, other, p->meta.manager->units_per_type[UNIT_MOUNT])
85 if ((r = path_add_one_mount_link(p, (Mount*) other)) < 0)
91 static int path_verify(Path *p) {
94 if (p->meta.load_state != UNIT_LOADED)
98 log_error("%s lacks path setting. Refusing.", p->meta.id);
105 static int path_load(Unit *u) {
110 assert(u->meta.load_state == UNIT_STUB);
112 if ((r = unit_load_fragment_and_dropin(u)) < 0)
115 if (u->meta.load_state == UNIT_LOADED) {
118 if ((r = unit_load_related_unit(u, ".service", &p->unit)))
121 if ((r = unit_add_dependency(u, UNIT_BEFORE, p->unit, true)) < 0)
124 if ((r = path_add_mount_links(p)) < 0)
128 return path_verify(p);
131 static void path_dump(Unit *u, FILE *f, const char *prefix) {
137 p2 = strappend(prefix, "\t");
138 prefix2 = p2 ? p2 : prefix;
143 prefix, path_state_to_string(p->state),
144 prefix, p->unit->meta.id);
146 LIST_FOREACH(spec, s, p->specs)
150 path_type_to_string(s->type),
156 static void path_unwatch_one(Path *p, PathSpec *s) {
158 if (s->inotify_fd < 0)
161 unit_unwatch_fd(UNIT(p), &s->watch);
163 close_nointr_nofail(s->inotify_fd);
167 static int path_watch_one(Path *p, PathSpec *s) {
168 static const int flags_table[_PATH_TYPE_MAX] = {
169 [PATH_EXISTS] = IN_DELETE_SELF|IN_MOVE_SELF,
170 [PATH_CHANGED] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO,
171 [PATH_DIRECTORY_NOT_EMPTY] = IN_DELETE_SELF|IN_MOVE_SELF|IN_CREATE|IN_MOVED_TO
181 path_unwatch_one(p, s);
183 if (!(k = strdup(s->path)))
186 if ((s->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC)) < 0) {
191 if (unit_watch_fd(UNIT(p), s->inotify_fd, EPOLLIN, &s->watch) < 0) {
196 if ((s->primary_wd = inotify_add_watch(s->inotify_fd, k, flags_table[s->type])) >= 0)
203 /* This assumes the path was passed through path_kill_slashes()! */
204 if (!(slash = strrchr(k, '/')))
209 flags = IN_DELETE_SELF|IN_MOVE_SELF;
211 flags |= IN_CREATE | IN_MOVED_TO | IN_ATTRIB;
213 if (inotify_add_watch(s->inotify_fd, k, flags) >= 0)
222 path_unwatch_one(p, s);
226 static void path_unwatch(Path *p) {
231 LIST_FOREACH(spec, s, p->specs)
232 path_unwatch_one(p, s);
235 static int path_watch(Path *p) {
241 LIST_FOREACH(spec, s, p->specs)
242 if ((r = path_watch_one(p, s)) < 0)
248 static void path_set_state(Path *p, PathState state) {
252 old_state = p->state;
255 if (state != PATH_WAITING)
258 if (state != old_state)
259 log_debug("%s changed %s -> %s",
261 path_state_to_string(old_state),
262 path_state_to_string(state));
264 unit_notify(UNIT(p), state_translation_table[old_state], state_translation_table[state]);
267 static void path_enter_waiting(Path *p, bool initial);
269 static int path_coldplug(Unit *u) {
273 assert(p->state == PATH_DEAD);
275 if (p->deserialized_state != p->state) {
277 if (p->deserialized_state == PATH_WAITING ||
278 p->deserialized_state == PATH_RUNNING)
279 path_enter_waiting(p, true);
281 path_set_state(p, p->deserialized_state);
287 static void path_enter_dead(Path *p, bool success) {
293 path_set_state(p, p->failure ? PATH_MAINTENANCE : PATH_DEAD);
296 static void path_enter_running(Path *p) {
300 if ((r = manager_add_job(p->meta.manager, JOB_START, p->unit, JOB_REPLACE, true, NULL)) < 0)
303 path_set_state(p, PATH_RUNNING);
307 log_warning("%s failed to queue unit startup job: %s", p->meta.id, strerror(-r));
308 path_enter_dead(p, false);
312 static void path_enter_waiting(Path *p, bool initial) {
317 LIST_FOREACH(spec, s, p->specs) {
322 good = access(s->path, F_OK) >= 0;
325 case PATH_DIRECTORY_NOT_EMPTY:
326 good = dir_is_empty(s->path) == 0;
332 b = access(s->path, F_OK) >= 0;
333 good = !initial && b != s->previous_exists;
334 s->previous_exists = b;
347 path_enter_running(p);
351 if ((r = path_watch(p)) < 0)
354 path_set_state(p, PATH_WAITING);
358 log_warning("%s failed to enter waiting state: %s", p->meta.id, strerror(-r));
359 path_enter_dead(p, false);
362 static int path_start(Unit *u) {
366 assert(p->state == PATH_DEAD || p->state == PATH_MAINTENANCE);
368 if (p->unit->meta.load_state != UNIT_LOADED)
372 path_enter_waiting(p, true);
376 static int path_stop(Unit *u) {
380 assert(p->state == PATH_WAITING || p->state == PATH_RUNNING);
382 path_enter_dead(p, true);
386 static int path_serialize(Unit *u, FILE *f, FDSet *fds) {
393 unit_serialize_item(u, f, "state", path_state_to_string(p->state));
398 static int path_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
406 if (streq(key, "state")) {
409 if ((state = path_state_from_string(value)) < 0)
410 log_debug("Failed to parse state value %s", value);
412 p->deserialized_state = state;
414 log_debug("Unknown serialization key '%s'", key);
419 static UnitActiveState path_active_state(Unit *u) {
422 return state_translation_table[PATH(u)->state];
425 static const char *path_sub_state_to_string(Unit *u) {
428 return path_state_to_string(PATH(u)->state);
431 static void path_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
435 struct inotify_event *buf = NULL;
441 if (p->state != PATH_WAITING)
444 log_debug("inotify wakeup on %s.", u->meta.id);
446 if (events != EPOLLIN) {
447 log_error("Got Invalid poll event on inotify.");
451 LIST_FOREACH(spec, s, p->specs)
452 if (s->inotify_fd == fd)
456 log_error("Got event on unknown fd.");
460 if (ioctl(fd, FIONREAD, &l) < 0) {
461 log_error("FIONREAD failed: %s", strerror(errno));
465 if (!(buf = malloc(l))) {
466 log_error("Failed to allocate buffer: %s", strerror(-ENOMEM));
470 if ((k = read(fd, buf, l)) < 0) {
471 log_error("Failed to read inotify event: %s", strerror(-errno));
475 if ((size_t) k < sizeof(struct inotify_event) ||
476 (size_t) k < sizeof(struct inotify_event) + buf->len) {
477 log_error("inotify event too small.");
481 if (s->type == PATH_CHANGED && s->primary_wd == buf->wd)
482 path_enter_running(p);
484 path_enter_waiting(p, false);
492 path_enter_dead(p, false);
495 void path_unit_notify(Unit *u, UnitActiveState new_state) {
500 if (u->meta.type == UNIT_PATH)
503 SET_FOREACH(n, u->meta.names, i) {
508 if (!(k = unit_name_change_suffix(n, ".path"))) {
513 t = manager_get_unit(u->meta.manager, k);
519 if (t->meta.load_state != UNIT_LOADED)
527 if (p->state == PATH_RUNNING && new_state == UNIT_INACTIVE) {
528 log_debug("%s got notified about unit deactivation.", p->meta.id);
529 path_enter_waiting(p, false);
536 log_error("Failed find path unit: %s", strerror(-r));
539 static const char* const path_state_table[_PATH_STATE_MAX] = {
540 [PATH_DEAD] = "dead",
541 [PATH_WAITING] = "waiting",
542 [PATH_RUNNING] = "running",
543 [PATH_MAINTENANCE] = "maintenance"
546 DEFINE_STRING_TABLE_LOOKUP(path_state, PathState);
548 static const char* const path_type_table[_PATH_TYPE_MAX] = {
549 [PATH_EXISTS] = "PathExists",
550 [PATH_CHANGED] = "PathChanged",
551 [PATH_DIRECTORY_NOT_EMPTY] = "DirectoryNotEmpty"
554 DEFINE_STRING_TABLE_LOOKUP(path_type, PathType);
556 const UnitVTable path_vtable = {
562 .coldplug = path_coldplug,
569 .serialize = path_serialize,
570 .deserialize_item = path_deserialize_item,
572 .active_state = path_active_state,
573 .sub_state_to_string = path_sub_state_to_string,
575 .fd_event = path_fd_event,
577 .bus_message_handler = bus_path_message_handler