path.c revision a57f7e2c828b852eb32fd810dcea041bb2975501
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <unistd.h>
#include "unit.h"
#include "unit-name.h"
#include "path.h"
#include "mkdir.h"
#include "dbus-path.h"
#include "special.h"
#include "dbus-common.h"
#include "path-util.h"
#include "macro.h"
[PATH_DEAD] = UNIT_INACTIVE,
[PATH_WAITING] = UNIT_ACTIVE,
[PATH_RUNNING] = UNIT_ACTIVE,
};
static const int flags_table[_PATH_TYPE_MAX] = {
[PATH_CHANGED] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO,
[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,
};
bool exists = false;
int r;
assert(u);
assert(s);
path_spec_unwatch(s, u);
if (s->inotify_fd < 0) {
r = -errno;
goto fail;
}
if (r < 0)
goto fail;
/* This assumes the path was passed through path_kill_slashes()! */
int flags;
char tmp;
if (slash) {
*cut = '\0';
} else
if (r < 0) {
if (cut)
break;
}
r = -errno;
if (cut)
goto fail;
} else {
exists = true;
/* Path exists, we don't need to watch parent
too closely. */
if (oldslash) {
*cut2 = '\0';
/* Error is ignored, the worst can happen is
we get spurious events. */
}
}
if (cut)
if (slash)
else {
/* whole path has been iterated over */
s->primary_wd = r;
break;
}
}
if (!exists) {
log_error("Failed to add watch on any of the components of %s: %m",
s->path);
r = -errno; /* either EACCESS or ENOENT */
goto fail;
}
return 0;
fail:
path_spec_unwatch(s, u);
return r;
}
if (s->inotify_fd < 0)
return;
unit_unwatch_fd(u, &s->watch);
s->inotify_fd = -1;
}
struct inotify_event *e;
ssize_t k;
int l;
int r = 0;
log_error("Got invalid poll event on inotify.");
return -EINVAL;
}
log_error("FIONREAD failed: %m");
return -errno;
}
assert(l > 0);
if (!buf)
return log_oom();
if (k < 0) {
log_error("Failed to read inotify event: %m");
return -errno;
}
e = (struct inotify_event*) buf;
while (k > 0) {
s->primary_wd == e->wd)
r = 1;
k -= step;
}
return r;
}
bool good = false;
switch (s->type) {
case PATH_EXISTS:
break;
case PATH_EXISTS_GLOB:
break;
case PATH_DIRECTORY_NOT_EMPTY: {
int k;
k = dir_is_empty(s->path);
break;
}
case PATH_CHANGED:
case PATH_MODIFIED: {
bool b;
s->previous_exists = b;
break;
}
default:
;
}
return good;
}
int r;
return;
if (r < 0)
}
fprintf(f,
"%s%s: %s\n",
path_type_to_string(s->type),
s->path);
}
void path_spec_done(PathSpec *s) {
assert(s);
}
assert(u);
p->directory_mode = 0755;
}
void path_free_specs(Path *p) {
PathSpec *s;
assert(p);
while ((s = p->specs)) {
path_spec_unwatch(s, UNIT(p));
path_spec_done(s);
free(s);
}
}
assert(p);
path_free_specs(p);
}
static int path_add_mount_links(Path *p) {
PathSpec *s;
int r;
assert(p);
if (r < 0)
return r;
}
return 0;
}
static int path_verify(Path *p) {
assert(p);
return 0;
if (!p->specs) {
return -EINVAL;
}
return 0;
}
static int path_add_default_dependencies(Path *p) {
int r;
assert(p);
SPECIAL_PATHS_TARGET, NULL, true);
if (r < 0)
return r;
SPECIAL_SYSINIT_TARGET, NULL, true);
if (r < 0)
return r;
}
SPECIAL_SHUTDOWN_TARGET, NULL, true);
}
int r;
assert(u);
r = unit_load_fragment_and_dropin(u);
if (r < 0)
return r;
if (u->load_state == UNIT_LOADED) {
Unit *x;
r = unit_load_related_unit(u, ".service", &x);
if (r < 0)
return r;
if (r < 0)
return r;
}
r = path_add_mount_links(p);
if (r < 0)
return r;
if (UNIT(p)->default_dependencies) {
r = path_add_default_dependencies(p);
if (r < 0)
return r;
}
}
return path_verify(p);
}
PathSpec *s;
assert(p);
assert(f);
trigger = UNIT_TRIGGER(u);
fprintf(f,
"%sPath State: %s\n"
"%sResult: %s\n"
"%sUnit: %s\n"
"%sMakeDirectory: %s\n"
"%sDirectoryMode: %04o\n",
prefix, p->directory_mode);
path_spec_dump(s, f, prefix);
}
static void path_unwatch(Path *p) {
PathSpec *s;
assert(p);
path_spec_unwatch(s, UNIT(p));
}
static int path_watch(Path *p) {
int r;
PathSpec *s;
assert(p);
r = path_spec_watch(s, UNIT(p));
if (r < 0)
return r;
}
return 0;
}
assert(p);
if (state != PATH_WAITING &&
path_unwatch(p);
log_debug("%s changed %s -> %s",
}
static int path_coldplug(Unit *u) {
assert(p);
if (p->deserialized_state != p->state) {
if (p->deserialized_state == PATH_WAITING ||
p->deserialized_state == PATH_RUNNING)
path_enter_waiting(p, true, true);
else
path_set_state(p, p->deserialized_state);
}
return 0;
}
assert(p);
if (f != PATH_SUCCESS)
p->result = f;
}
static void path_enter_running(Path *p) {
int r;
assert(p);
/* Don't start job if we are supposed to go down */
if (unit_stop_pending(UNIT(p)))
return;
if (r < 0)
goto fail;
p->inotify_triggered = false;
r = path_watch(p);
if (r < 0)
goto fail;
return;
fail:
log_warning("%s failed to queue unit startup job: %s",
}
PathSpec *s;
bool good = false;
assert(p);
if (good)
break;
}
return good;
}
int r;
if (recheck)
if (path_check_good(p, initial)) {
return;
}
r = path_watch(p);
if (r < 0)
goto fail;
/* Hmm, so now we have created inotify watches, but the file
* recheck */
if (recheck)
if (path_check_good(p, false)) {
return;
}
return;
fail:
log_warning("%s failed to enter waiting state: %s",
}
static void path_mkdir(Path *p) {
PathSpec *s;
assert(p);
if (!p->make_directory)
return;
path_spec_mkdir(s, p->directory_mode);
}
static int path_start(Unit *u) {
assert(p);
return -ENOENT;
path_mkdir(p);
p->result = PATH_SUCCESS;
path_enter_waiting(p, true, true);
return 0;
}
assert(p);
return 0;
}
assert(u);
assert(f);
return 0;
}
assert(u);
if (state < 0)
else
p->deserialized_state = state;
PathResult f;
f = path_result_from_string(value);
if (f < 0)
else if (f != PATH_SUCCESS)
p->result = f;
} else
return 0;
}
assert(u);
}
assert(u);
}
PathSpec *s;
int changed;
assert(p);
if (p->state != PATH_WAITING &&
p->state != PATH_RUNNING)
return;
/* log_debug("inotify wakeup on %s.", u->id); */
if (path_spec_owns_inotify_fd(s, fd))
break;
if (!s) {
log_error("Got event on unknown fd.");
goto fail;
}
if (changed < 0)
goto fail;
/* If we are already running, then remember that one event was
* dispatched so that we restart the service only if something
* actually changed on disk */
p->inotify_triggered = true;
if (changed)
else
path_enter_waiting(p, false, true);
return;
fail:
}
assert(u);
/* Invoked whenever the unit we trigger changes state or gains
* or loses a job */
return;
if (p->state == PATH_RUNNING &&
"%s got notified about unit deactivation.",
/* Hmm, so inotify was triggered since the
* last activation, so I guess we need to
* recheck what is going on. */
path_enter_waiting(p, false, p->inotify_triggered);
}
}
static void path_reset_failed(Unit *u) {
assert(p);
if (p->state == PATH_FAILED)
path_set_state(p, PATH_DEAD);
p->result = PATH_SUCCESS;
}
static const char* const path_state_table[_PATH_STATE_MAX] = {
[PATH_DEAD] = "dead",
[PATH_WAITING] = "waiting",
[PATH_RUNNING] = "running",
[PATH_FAILED] = "failed"
};
static const char* const path_type_table[_PATH_TYPE_MAX] = {
[PATH_EXISTS] = "PathExists",
[PATH_EXISTS_GLOB] = "PathExistsGlob",
[PATH_CHANGED] = "PathChanged",
[PATH_MODIFIED] = "PathModified",
[PATH_DIRECTORY_NOT_EMPTY] = "DirectoryNotEmpty"
};
static const char* const path_result_table[_PATH_RESULT_MAX] = {
[PATH_SUCCESS] = "success",
[PATH_FAILURE_RESOURCES] = "resources"
};
const UnitVTable path_vtable = {
.object_size = sizeof(Path),
.sections =
"Unit\0"
"Path\0"
"Install\0",
.start = path_start,
.bus_interface = "org.freedesktop.systemd1.Path",
};