Commit 01f78473 authored by Lennart Poettering's avatar Lennart Poettering
Browse files

path: add .path unit type for monitoring files

parent 871d7de4
......@@ -77,7 +77,8 @@ interface_DATA = \
org.freedesktop.systemd1.Mount.xml \
org.freedesktop.systemd1.Automount.xml \
org.freedesktop.systemd1.Snapshot.xml \
org.freedesktop.systemd1.Swap.xml
org.freedesktop.systemd1.Swap.xml \
org.freedesktop.systemd1.Path.xml
dist_systemunit_DATA = \
units/emergency.service \
......@@ -189,6 +190,7 @@ COMMON_SOURCES = \
src/snapshot.c \
src/socket.c \
src/timer.c \
src/path.c \
src/load-dropin.c \
src/execute.c \
src/dbus.c \
......@@ -205,6 +207,7 @@ COMMON_SOURCES = \
src/dbus-snapshot.c \
src/dbus-device.c \
src/dbus-execute.c \
src/dbus-path.c \
src/cgroup.c \
src/mount-setup.c \
src/hostname-setup.c \
......
......@@ -562,12 +562,15 @@ static int automount_start(Unit *u) {
assert(a);
assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_MAINTAINANCE);
if (path_is_mount_point(a->where)) {
log_error("Path %s is already a mount point, refusing start for %s", a->where, u->meta.id);
return -EEXIST;
}
assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_MAINTAINANCE);
if (a->mount->meta.load_state != UNIT_LOADED)
return -ENOENT;
a->failure = false;
automount_enter_waiting(a);
......
......@@ -337,6 +337,8 @@ int config_parse_path(
if (!(n = strdup(rvalue)))
return -ENOMEM;
path_kill_slashes(n);
free(*s);
*s = n;
......@@ -441,6 +443,8 @@ int config_parse_path_strv(
goto fail;
}
path_kill_slashes(n[k]);
k++;
}
......
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
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 "dbus-unit.h"
#include "dbus-path.h"
#include "dbus-execute.h"
#define BUS_PATH_INTERFACE \
" <interface name=\"org.freedesktop.systemd1.Path\">\n" \
" <property name=\"Unit\" type=\"s\" access=\"read\"/>\n" \
" </interface>\n" \
#define INTROSPECTION \
DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
"<node>\n" \
BUS_UNIT_INTERFACE \
BUS_PATH_INTERFACE \
BUS_PROPERTIES_INTERFACE \
BUS_INTROSPECTABLE_INTERFACE \
"</node>\n"
const char bus_path_interface[] = BUS_PATH_INTERFACE;
DBusHandlerResult bus_path_message_handler(Unit *u, DBusMessage *message) {
const BusProperty properties[] = {
BUS_UNIT_PROPERTIES,
{ "org.freedesktop.systemd1.Path", "Unit", bus_property_append_string, "s", &u->path.unit->meta.id },
{ NULL, NULL, NULL, NULL, NULL }
};
return bus_default_message_handler(u->meta.manager, message, INTROSPECTION, properties);
}
/*-*- Mode: C; c-basic-offset: 8 -*-*/
#ifndef foodbuspathhfoo
#define foodbuspathhfoo
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
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 <dbus/dbus.h>
#include "unit.h"
DBusHandlerResult bus_path_message_handler(Unit *u, DBusMessage *message);
extern const char bus_path_interface[];
#endif
......@@ -41,6 +41,7 @@
#include "dbus-snapshot.h"
#include "dbus-swap.h"
#include "dbus-timer.h"
#include "dbus-path.h"
static const char bus_properties_interface[] = BUS_PROPERTIES_INTERFACE;
static const char bus_introspectable_interface[] = BUS_INTROSPECTABLE_INTERFACE;
......@@ -60,6 +61,7 @@ const char *const bus_interface_table[] = {
"org.freedesktop.systemd1.Snapshot", bus_snapshot_interface,
"org.freedesktop.systemd1.Swap", bus_swap_interface,
"org.freedesktop.systemd1.Timer", bus_timer_interface,
"org.freedesktop.systemd1.Path", bus_path_interface,
NULL
};
......
......@@ -213,6 +213,8 @@ static int config_parse_listen(
free(p);
return -ENOMEM;
}
path_kill_slashes(p->path);
} else {
p->type = SOCKET_SOCKET;
......@@ -450,6 +452,8 @@ static int config_parse_exec(
nce->argv = n;
nce->path = path;
path_kill_slashes(nce->path);
exec_command_append_list(e, nce);
return 0;
......@@ -1068,6 +1072,77 @@ static int config_parse_timer_unit(
return 0;
}
static int config_parse_path_spec(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
const char *rvalue,
void *data,
void *userdata) {
Path *p = data;
PathSpec *s;
PathType b;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
if ((b = path_type_from_string(lvalue)) < 0) {
log_error("[%s:%u] Failed to parse path type: %s", filename, line, lvalue);
return -EINVAL;
}
if (!path_is_absolute(rvalue)) {
log_error("[%s:%u] Path is not absolute: %s", filename, line, rvalue);
return -EINVAL;
}
if (!(s = new0(PathSpec, 1)))
return -ENOMEM;
if (!(s->path = strdup(rvalue))) {
free(s);
return -ENOMEM;
}
path_kill_slashes(s->path);
s->type = b;
s->inotify_fd = -1;
LIST_PREPEND(PathSpec, spec, p->specs, s);
return 0;
}
static int config_parse_path_unit(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
const char *rvalue,
void *data,
void *userdata) {
Path *t = data;
int r;
if (endswith(rvalue, ".path")) {
log_error("[%s:%u] Unit cannot be of type path: %s", filename, line, rvalue);
return -EINVAL;
}
if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &t->unit)) < 0) {
log_error("[%s:%u] Failed to load unit: %s", filename, line, rvalue);
return r;
}
return 0;
}
#define FOLLOW_MAX 8
static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
......@@ -1269,7 +1344,8 @@ static int load_from_path(Unit *u, const char *path) {
[UNIT_MOUNT] = "Mount",
[UNIT_AUTOMOUNT] = "Automount",
[UNIT_SNAPSHOT] = "Snapshot",
[UNIT_SWAP] = "Swap"
[UNIT_SWAP] = "Swap",
[UNIT_PATH] = "Path"
};
#define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
......@@ -1386,15 +1462,20 @@ static int load_from_path(Unit *u, const char *path) {
{ "Where", config_parse_path, &u->automount.where, "Automount" },
{ "What", config_parse_path, &u->swap.parameters_fragment.what, "Swap" },
{ "Priority", config_parse_int, &u->swap.parameters_fragment.priority, "Swap" },
{ "What", config_parse_path, &u->swap.parameters_fragment.what, "Swap" },
{ "Priority", config_parse_int, &u->swap.parameters_fragment.priority, "Swap" },
{ "OnActive", config_parse_timer, &u->timer, "Timer" },
{ "OnBoot", config_parse_timer, &u->timer, "Timer" },
{ "OnStartup", config_parse_timer, &u->timer, "Timer" },
{ "OnUnitActive", config_parse_timer, &u->timer, "Timer" },
{ "OnUnitInactive", config_parse_timer, &u->timer, "Timer" },
{ "Unit", config_parse_timer_unit, &u->timer, "Timer" },
{ "OnActive", config_parse_timer, &u->timer, "Timer" },
{ "OnBoot", config_parse_timer, &u->timer, "Timer" },
{ "OnStartup", config_parse_timer, &u->timer, "Timer" },
{ "OnUnitActive", config_parse_timer, &u->timer, "Timer" },
{ "OnUnitInactive", config_parse_timer, &u->timer, "Timer" },
{ "Unit", config_parse_timer_unit, &u->timer, "Timer" },
{ "PathExists", config_parse_path_spec, &u->path, "Path" },
{ "PathChanged", config_parse_path_spec, &u->path, "Path" },
{ "DirectoryNotEmpty", config_parse_path_spec, &u->path, "Path" },
{ "Unit", config_parse_path_unit, &u->path, "Path" },
{ NULL, NULL, NULL, NULL }
};
......
......@@ -167,6 +167,19 @@ static int mount_add_swap_links(Mount *m) {
return 0;
}
static int mount_add_path_links(Mount *m) {
Meta *other;
int r;
assert(m);
LIST_FOREACH(units_per_type, other, m->meta.manager->units_per_type[UNIT_PATH])
if ((r = path_add_one_mount_link((Path*) other, m)) < 0)
return r;
return 0;
}
static int mount_add_automount_links(Mount *m) {
Meta *other;
int r;
......@@ -341,6 +354,9 @@ static int mount_load(Unit *u) {
if ((r = mount_add_swap_links(m)) < 0)
return r;
if ((r = mount_add_path_links(m)) < 0)
return r;
if ((r = mount_add_automount_links(m)) < 0)
return r;
......
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
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 <sys/inotify.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <unistd.h>
#include "unit.h"
#include "unit-name.h"
#include "path.h"
#include "dbus-path.h"
static const UnitActiveState state_translation_table[_PATH_STATE_MAX] = {
[PATH_DEAD] = UNIT_INACTIVE,
[PATH_WAITING] = UNIT_ACTIVE,
[PATH_RUNNING] = UNIT_ACTIVE,
[PATH_MAINTAINANCE] = UNIT_INACTIVE
};
static void path_done(Unit *u) {
Path *p = PATH(u);
PathSpec *s;
assert(p);
while ((s = p->specs)) {
LIST_REMOVE(PathSpec, spec, p->specs, s);
free(s);
}
}
int path_add_one_mount_link(Path *p, Mount *m) {
PathSpec *s;
int r;
assert(p);
assert(m);
if (p->meta.load_state != UNIT_LOADED ||
m->meta.load_state != UNIT_LOADED)
return 0;
LIST_FOREACH(spec, s, p->specs) {
if (!path_startswith(s->path, m->where))
continue;
if ((r = unit_add_dependency(UNIT(m), UNIT_BEFORE, UNIT(p), true)) < 0)
return r;
if ((r = unit_add_dependency(UNIT(p), UNIT_REQUIRES, UNIT(m), true)) < 0)
return r;
}
return 0;
}
static int path_add_mount_links(Path *p) {
Meta *other;
int r;
assert(p);
LIST_FOREACH(units_per_type, other, p->meta.manager->units_per_type[UNIT_MOUNT])
if ((r = path_add_one_mount_link(p, (Mount*) other)) < 0)
return r;
return 0;
}
static int path_verify(Path *p) {
assert(p);
if (UNIT(p)->meta.load_state != UNIT_LOADED)
return 0;
if (!p->specs) {
log_error("%s lacks path setting. Refusing.", p->meta.id);
return -EINVAL;
}
return 0;
}
static int path_load(Unit *u) {
Path *p = PATH(u);
int r;
assert(u);
assert(u->meta.load_state == UNIT_STUB);
if ((r = unit_load_fragment_and_dropin(u)) < 0)
return r;
if (u->meta.load_state == UNIT_LOADED) {
if (!p->unit)
if ((r = unit_load_related_unit(u, ".service", &p->unit)))
return r;
if ((r = unit_add_dependency(u, UNIT_BEFORE, p->unit, true)) < 0)
return r;
if ((r = path_add_mount_links(p)) < 0)
return r;
}
return path_verify(p);
}
static void path_dump(Unit *u, FILE *f, const char *prefix) {
Path *p = PATH(u);
const char *prefix2;
char *p2;
PathSpec *s;
p2 = strappend(prefix, "\t");
prefix2 = p2 ? p2 : prefix;
fprintf(f,
"%sPath State: %s\n"
"%sUnit: %s\n",
prefix, path_state_to_string(p->state),
prefix, p->unit->meta.id);
LIST_FOREACH(spec, s, p->specs)
fprintf(f,
"%s%s: %s\n",
prefix,
path_type_to_string(s->type),
s->path);
free(p2);
}
static void path_unwatch_one(Path *p, PathSpec *s) {
if (s->inotify_fd < 0)
return;
unit_unwatch_fd(UNIT(p), &s->watch);
close_nointr_nofail(s->inotify_fd);
s->inotify_fd = -1;
}
static int path_watch_one(Path *p, PathSpec *s) {
static const int flags_table[_PATH_TYPE_MAX] = {
[PATH_EXISTS] = IN_DELETE_SELF|IN_MOVE_SELF,
[PATH_CHANGED] = IN_DELETE_SELF|IN_MOVE_SELF|IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO,
[PATH_DIRECTORY_NOT_EMPTY] = IN_DELETE_SELF|IN_MOVE_SELF|IN_CREATE|IN_MOVED_TO
};
bool exists = false;
char *k;
int r;
assert(p);
assert(s);
path_unwatch_one(p, s);
if (!(k = strdup(s->path)))
return -ENOMEM;
if ((s->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC)) < 0) {
r = -errno;
goto fail;
}
if (unit_watch_fd(UNIT(p), s->inotify_fd, EPOLLIN, &s->watch) < 0) {
r = -errno;
goto fail;
}
if ((s->primary_wd = inotify_add_watch(s->inotify_fd, k, flags_table[s->type])) >= 0)
exists = true;
for (;;) {
int flags;
char *slash;
/* This assumes the path was passed through path_kill_slashes()! */
if (!(slash = strrchr(k, '/')))
break;
*slash = 0;
flags = IN_DELETE_SELF|IN_MOVE_SELF;
if (!exists)
flags |= IN_CREATE | IN_MOVED_TO | IN_ATTRIB;
if (inotify_add_watch(s->inotify_fd, k, flags) >= 0)
exists = true;
}
return 0;
fail:
free(k);
path_unwatch_one(p, s);
return r;
}
static void path_unwatch(Path *p) {
PathSpec *s;
assert(p);
LIST_FOREACH(spec, s, p->specs)
path_unwatch_one(p, s);
}
static int path_watch(Path *p) {
int r;
PathSpec *s;
assert(p);
LIST_FOREACH(spec, s, p->specs)
if ((r = path_watch_one(p, s)) < 0)
return r;
return 0;
}
static void path_set_state(Path *p, PathState state) {
PathState old_state;
assert(p);
old_state = p->state;
p->state = state;
if (state != PATH_WAITING)
path_unwatch(p);
if (state != old_state)
log_debug("%s changed %s -> %s",
p->meta.id,
path_state_to_string(old_state),
path_state_to_string(state));
unit_notify(UNIT(p), state_translation_table[old_state], state_translation_table[state]);
}
static void path_enter_waiting(Path *p, bool initial);
static int path_coldplug(Unit *u) {
Path *p = PATH(u);
assert(p);
assert(p->state == PATH_DEAD);
if (p->deserialized_state != p->state) {
if (p->deserialized_state == PATH_WAITING ||
p->deserialized_state == PATH_RUNNING)
path_enter_waiting(p, true);
else
path_set_state(p, p->deserialized_state);
}
return 0;
}
static void path_enter_dead(Path *p, bool success) {
assert(p);
if (!success)
p->failure = true;