path.c revision f8c16f42fb6d8e0425ff2b867aa9af07d9b6b4ba
/*-*- 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 "bus-errors.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;
char _cleanup_free_ *k = NULL;
char *slash;
int r;
assert(u);
assert(s);
path_spec_unwatch(s, u);
if (!k)
return -ENOMEM;
if (s->inotify_fd < 0) {
r = -errno;
goto fail;
}
if (r < 0)
goto fail;
if (s->primary_wd >= 0)
exists = true;
do {
int flags;
/* This assumes the path was passed through path_kill_slashes()! */
if (!slash)
break;
/* Trim the path at the last slash. Keep the slash if it's the root dir. */
if (!exists)
exists = true;
} while (slash != k);
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.");
r = -EINVAL;
goto out;
}
log_error("FIONREAD failed: %m");
r = -errno;
goto out;
}
assert(l > 0);
if (!buf) {
log_error("Failed to allocate buffer: %m");
r = -errno;
goto out;
}
if (k < 0) {
log_error("Failed to read inotify event: %m");
r = -errno;
goto out;
}
e = (struct inotify_event*) buf;
while (k > 0) {
s->primary_wd == e->wd)
r = 1;
k -= step;
}
out:
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);
unit_ref_unset(&p->unit);
path_free_specs(p);
}
PathSpec *s;
int r;
assert(p);
assert(m);
return 0;
if (!path_spec_startswith(s, m->where))
continue;
return r;
}
return 0;
}
static int path_add_mount_links(Path *p) {
int r;
assert(p);
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);
return r;
if ((r = unit_add_two_dependencies_by_name(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0)
return r;
}
return unit_add_two_dependencies_by_name(UNIT(p), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
}
int r;
assert(u);
if ((r = unit_load_fragment_and_dropin(u)) < 0)
return r;
if (u->load_state == UNIT_LOADED) {
if (!UNIT_DEREF(p->unit)) {
Unit *x;
r = unit_load_related_unit(u, ".service", &x);
if (r < 0)
return r;
unit_ref_set(&p->unit, x);
}
if (r < 0)
return r;
if ((r = path_add_mount_links(p)) < 0)
return r;
if (UNIT(p)->default_dependencies)
if ((r = path_add_default_dependencies(p)) < 0)
return r;
}
return path_verify(p);
}
PathSpec *s;
assert(p);
assert(f);
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);
if ((r = path_spec_watch(s, UNIT(p))) < 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 */
return;
if ((r = manager_add_job(UNIT(p)->manager, JOB_START, UNIT_DEREF(p->unit), JOB_REPLACE, true, &error, NULL)) < 0)
goto fail;
p->inotify_triggered = false;
if ((r = path_watch(p)) < 0)
goto fail;
return;
fail:
}
PathSpec *s;
bool good = false;
assert(p);
if (good)
break;
}
return good;
}
int r;
if (recheck)
if (path_check_good(p, initial)) {
return;
}
if ((r = path_watch(p)) < 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:
}
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);
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);
}
static const char *path_sub_state_to_string(Unit *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:
}
Iterator i;
Unit *k;
return;
Path *p;
continue;
if (k->load_state != UNIT_LOADED)
continue;
p = PATH(k);
/* 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",
};