path.c revision 36af55d99711e9accdf42d8a7df60e069f4086c0
/*-*- 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 General Public License as published by
the Free Software Foundation; either version 2 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
General Public License for more details.
You should have received a copy of the GNU 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 "dbus-path.h"
#include "special.h"
#include "bus-errors.h"
[PATH_DEAD] = UNIT_INACTIVE,
[PATH_WAITING] = UNIT_ACTIVE,
[PATH_RUNNING] = UNIT_ACTIVE,
};
PathSpec *s;
assert(p);
while ((s = p->specs)) {
free(s);
}
}
PathSpec *s;
int r;
assert(p);
assert(m);
return 0;
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 (!p->unit)
return r;
return r;
if ((r = path_add_mount_links(p)) < 0)
return r;
if (p->meta.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"
"%sUnit: %s\n",
fprintf(f,
"%s%s: %s\n",
path_type_to_string(s->type),
s->path);
}
if (s->inotify_fd < 0)
return;
s->inotify_fd = -1;
}
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,
};
bool exists = false;
char *k;
int r;
assert(p);
assert(s);
path_unwatch_one(p, s);
return -ENOMEM;
r = -errno;
goto fail;
}
r = -errno;
goto fail;
}
exists = true;
for (;;) {
int flags;
char *slash;
/* This assumes the path was passed through path_kill_slashes()! */
break;
*slash = 0;
if (!exists)
exists = true;
}
return 0;
fail:
free(k);
path_unwatch_one(p, s);
return r;
}
static void path_unwatch(Path *p) {
PathSpec *s;
assert(p);
path_unwatch_one(p, s);
}
static int path_watch(Path *p) {
int r;
PathSpec *s;
assert(p);
if ((r = path_watch_one(p, s)) < 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 (!success)
p->failure = true;
}
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(p->meta.manager, JOB_START, p->unit, JOB_REPLACE, true, &error, NULL)) < 0)
goto fail;
p->inotify_triggered = false;
if ((r = path_watch(p)) < 0)
goto fail;
return;
fail:
path_enter_dead(p, false);
}
PathSpec *s;
int r;
bool good = false;
if (!recheck)
goto waiting;
switch (s->type) {
case PATH_EXISTS:
break;
case PATH_DIRECTORY_NOT_EMPTY: {
int k;
k = dir_is_empty(s->path);
break;
}
case PATH_CHANGED: {
bool b;
s->previous_exists = b;
break;
}
default:
;
}
if (good)
break;
}
if (good) {
return;
}
if ((r = path_watch(p)) < 0)
goto fail;
return;
fail:
path_enter_dead(p, false);
}
static int path_start(Unit *u) {
assert(p);
return -ENOENT;
p->failure = false;
path_enter_waiting(p, true, true);
return 0;
}
assert(p);
path_enter_dead(p, true);
return 0;
}
assert(u);
assert(f);
return 0;
}
assert(u);
else
p->deserialized_state = state;
} else
return 0;
}
assert(u);
}
static const char *path_sub_state_to_string(Unit *u) {
assert(u);
}
int l;
ssize_t k;
struct inotify_event *e;
PathSpec *s;
bool changed;
assert(p);
if (p->state != PATH_WAITING &&
p->state != PATH_RUNNING)
return;
log_error("Got Invalid poll event on inotify.");
goto fail;
}
if (s->inotify_fd == fd)
break;
if (!s) {
log_error("Got event on unknown fd.");
goto fail;
}
goto fail;
}
goto fail;
}
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;
e = (struct inotify_event*) buf;
changed = false;
while (k > 0) {
changed = true;
k -= step;
}
if (changed)
else
path_enter_waiting(p, false, true);
return;
fail:
path_enter_dead(p, false);
}
char *n;
int r;
Iterator i;
return;
char *k;
Unit *t;
Path *p;
if (!(k = unit_name_change_suffix(n, ".path"))) {
r = -ENOMEM;
goto fail;
}
free(k);
if (!t)
continue;
continue;
p = PATH(t);
if (p->unit != u)
continue;
/* 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);
}
}
return;
fail:
}
static void path_reset_failed(Unit *u) {
assert(p);
if (p->state == PATH_FAILED)
path_set_state(p, PATH_DEAD);
p->failure = false;
}
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_CHANGED] = "PathChanged",
[PATH_DIRECTORY_NOT_EMPTY] = "DirectoryNotEmpty"
};
const UnitVTable path_vtable = {
.suffix = ".path",
.start = path_start,
.bus_interface = "org.freedesktop.systemd1.Path",
};