path.c revision e2f3b44cfc8864bfea7c77ff4c383ce9b535f27e
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch This file is part of systemd.
bdd36cfdba3ff66d25570a9ff568d69e1eb543cfTimo Sirainen Copyright 2010 Lennart Poettering
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch systemd is free software; you can redistribute it and/or modify it
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch under the terms of the GNU General Public License as published by
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch the Free Software Foundation; either version 2 of the License, or
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch (at your option) any later version.
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch systemd is distributed in the hope that it will be useful, but
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch WITHOUT ANY WARRANTY; without even the implied warranty of
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch General Public License for more details.
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch You should have received a copy of the GNU General Public License
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch along with systemd; If not, see <http://www.gnu.org/licenses/>.
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic const UnitActiveState state_translation_table[_PATH_STATE_MAX] = {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic void path_unwatch_one(Path *p, PathSpec *s) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch while ((s = p->specs)) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschint path_add_one_mount_link(Path *p, Mount *m) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if ((r = unit_add_two_dependencies(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch LIST_FOREACH(units_per_type, other, p->meta.manager->units_per_type[UNIT_MOUNT])
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if ((r = path_add_one_mount_link(p, (Mount*) other)) < 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch log_error("%s lacks path setting. Refusing.", p->meta.id);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic int path_add_default_dependencies(Path *p) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (p->meta.manager->running_as == MANAGER_SYSTEM) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if ((r = unit_add_dependency_by_name(UNIT(p), UNIT_BEFORE, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if ((r = unit_add_two_dependencies_by_name(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return unit_add_two_dependencies_by_name(UNIT(p), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if ((r = unit_load_fragment_and_dropin(u)) < 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if ((r = unit_load_related_unit(u, ".service", &p->unit)))
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if ((r = unit_add_dependency(u, UNIT_BEFORE, p->unit, true)) < 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if ((r = path_add_mount_links(p)) < 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if ((r = path_add_default_dependencies(p)) < 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic void path_dump(Unit *u, FILE *f, const char *prefix) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch "%sPath State: %s\n"
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch "%sUnit: %s\n",
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch "%s%s: %s\n",
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic int path_watch_one(Path *p, PathSpec *s) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch static const int flags_table[_PATH_TYPE_MAX] = {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch [PATH_EXISTS] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB,
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch [PATH_CHANGED] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO,
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch [PATH_DIRECTORY_NOT_EMPTY] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CREATE|IN_MOVED_TO
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch bool exists = false;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if ((s->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC)) < 0) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (unit_watch_fd(UNIT(p), s->inotify_fd, EPOLLIN, &s->watch) < 0) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if ((s->primary_wd = inotify_add_watch(s->inotify_fd, k, flags_table[s->type])) >= 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch /* This assumes the path was passed through path_kill_slashes()! */
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch /* Trim the path at the last slash. Keep the slash if it's the root dir. */
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch flags = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (inotify_add_watch(s->inotify_fd, k, flags) >= 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch } while (slash != k);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if ((r = path_watch_one(p, s)) < 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic void path_set_state(Path *p, PathState state) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch (state != PATH_RUNNING || p->inotify_triggered))
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch unit_notify(UNIT(p), state_translation_table[old_state], state_translation_table[state], true);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic void path_enter_waiting(Path *p, bool initial, bool recheck);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic void path_enter_dead(Path *p, bool success) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch path_set_state(p, p->failure ? PATH_FAILED : PATH_DEAD);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch /* Don't start job if we are supposed to go down */
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (p->meta.job && p->meta.job->type == JOB_STOP)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if ((r = manager_add_job(p->meta.manager, JOB_START, p->unit, JOB_REPLACE, true, &error, NULL)) < 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if ((r = path_watch(p)) < 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch log_warning("%s failed to queue unit startup job: %s", p->meta.id, bus_error(&error, r));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic void path_enter_waiting(Path *p, bool initial, bool recheck) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch bool good = false;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch switch (s->type) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if ((r = path_watch(p)) < 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch log_warning("%s failed to enter waiting state: %s", p->meta.id, strerror(-r));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch assert(p->state == PATH_DEAD || p->state == PATH_FAILED);
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch assert(p->state == PATH_WAITING || p->state == PATH_RUNNING);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic int path_serialize(Unit *u, FILE *f, FDSet *fds) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch unit_serialize_item(u, f, "state", path_state_to_string(p->state));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic int path_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if ((state = path_state_from_string(value)) < 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch log_debug("Failed to parse state value %s", value);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch log_debug("Unknown serialization key '%s'", key);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic UnitActiveState path_active_state(Unit *u) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return state_translation_table[PATH(u)->state];
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic const char *path_sub_state_to_string(Unit *u) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic void path_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch /* log_debug("inotify wakeup on %s.", u->meta.id); */
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch log_error("Got Invalid poll event on inotify.");
009217abb57a24a4076092e8e4e165545747839eStephan Bosch log_error("FIONREAD failed: %s", strerror(errno));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch log_error("Failed to allocate buffer: %s", strerror(-ENOMEM));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch log_error("Failed to read inotify event: %s", strerror(-errno));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch /* If we are already running, then remember that one event was
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * dispatched so that we restart the service only if something
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * actually changed on disk */
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch while (k > 0) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (s->type == PATH_CHANGED && s->primary_wd == e->wd)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch e = (struct inotify_event*) ((uint8_t*) e + step);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch path_enter_waiting(p, false, true);
bcd6c13936df11167350ac86598a781dce9038c3Stephan Boschvoid path_unit_notify(Unit *u, UnitActiveState new_state) {
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch if (!(k = unit_name_change_suffix(n, ".path"))) {
df70bf8c997cd91452cdb3a5c2e20605d30446d2Stephan Bosch if (p->state == PATH_RUNNING && new_state == UNIT_INACTIVE) {
df70bf8c997cd91452cdb3a5c2e20605d30446d2Stephan Bosch log_debug("%s got notified about unit deactivation.", p->meta.id);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch /* Hmm, so inotify was triggered since the
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * last activation, so I guess we need to
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * recheck what is going on. */
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch path_enter_waiting(p, false, p->inotify_triggered);
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch log_error("Failed find path unit: %s", strerror(-r));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic const char* const path_state_table[_PATH_STATE_MAX] = {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan BoschDEFINE_STRING_TABLE_LOOKUP(path_state, PathState);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic const char* const path_type_table[_PATH_TYPE_MAX] = {
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch [PATH_DIRECTORY_NOT_EMPTY] = "DirectoryNotEmpty"
7a545edbd1fca7a330bcb4a807002373ee18762aStephan BoschDEFINE_STRING_TABLE_LOOKUP(path_type, PathType);
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch .sub_state_to_string = path_sub_state_to_string,
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch .bus_interface = "org.freedesktop.systemd1.Path",