/***
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 "bus-error.h"
#include "bus-util.h"
#include "dbus-path.h"
#include "fd-util.h"
#include "fs-util.h"
#include "glob-util.h"
#include "macro.h"
#include "mkdir.h"
#include "path.h"
#include "special.h"
#include "stat-util.h"
#include "string-table.h"
#include "string-util.h"
#include "unit-name.h"
#include "unit.h"
[PATH_DEAD] = UNIT_INACTIVE,
[PATH_WAITING] = UNIT_ACTIVE,
[PATH_RUNNING] = UNIT_ACTIVE,
};
[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(s);
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 = log_warning_errno(errno, "Failed to add watch on %s: %s", s->path, errno == ENOSPC ? "too many watches" : strerror(-r));
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) {
/* either EACCESS or ENOENT */
goto fail;
}
return 0;
fail:
return r;
}
assert(s);
}
struct inotify_event *e;
ssize_t l;
int r = 0;
log_error("Got invalid poll event on inotify.");
return -EINVAL;
}
if (l < 0) {
return 0;
}
s->primary_wd == e->wd)
r = 1;
}
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);
}
assert(s);
}
assert(u);
p->directory_mode = 0755;
}
PathSpec *s;
assert(p);
while ((s = p->specs)) {
path_spec_done(s);
free(s);
}
}
assert(p);
path_free_specs(p);
}
PathSpec *s;
int r;
assert(p);
if (r < 0)
return r;
}
return 0;
}
assert(p);
return 0;
if (!p->specs) {
return -EINVAL;
}
return 0;
}
int r;
assert(p);
if (!UNIT(p)->default_dependencies)
return 0;
if (r < 0)
return r;
r = unit_add_two_dependencies_by_name(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
if (r < 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);
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;
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);
}
PathSpec *s;
assert(p);
}
int r;
PathSpec *s;
assert(p);
r = path_spec_watch(s, path_dispatch_io);
if (r < 0)
return r;
}
return 0;
}
assert(p);
if (state != PATH_WAITING &&
path_unwatch(p);
log_unit_debug(UNIT(p), "Changed %s -> %s", path_state_to_string(old_state), path_state_to_string(state));
}
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;
}
int r;
assert(p);
/* Don't start job if we are supposed to go down */
if (unit_stop_pending(UNIT(p)))
return;
if (!trigger) {
return;
}
if (r < 0)
goto fail;
p->inotify_triggered = false;
r = path_watch(p);
if (r < 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;
}
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:
}
PathSpec *s;
assert(p);
if (!p->make_directory)
return;
path_spec_mkdir(s, p->directory_mode);
}
assert(p);
trigger = UNIT_TRIGGER(u);
log_unit_error(u, "Refusing to start, unit to trigger not loaded.");
return -ENOENT;
}
path_mkdir(p);
p->result = PATH_SUCCESS;
path_enter_waiting(p, true, true);
return 1;
}
assert(p);
return 1;
}
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);
}
Path *p;
int changed;
assert(s);
if (p->state != PATH_WAITING &&
p->state != PATH_RUNNING)
return 0;
/* 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 0;
fail:
return 0;
}
assert(u);
/* Invoked whenever the unit we trigger changes state or gains
* or loses a job */
return;
if (p->state == PATH_RUNNING &&
/* 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);
}
}
assert(p);
if (p->state == PATH_FAILED)
path_set_state(p, PATH_DEAD);
p->result = PATH_SUCCESS;
}
[PATH_EXISTS] = "PathExists",
[PATH_EXISTS_GLOB] = "PathExistsGlob",
[PATH_DIRECTORY_NOT_EMPTY] = "DirectoryNotEmpty",
[PATH_CHANGED] = "PathChanged",
[PATH_MODIFIED] = "PathModified",
};
[PATH_SUCCESS] = "success",
[PATH_FAILURE_RESOURCES] = "resources",
};
.object_size = sizeof(Path),
.sections =
"Unit\0"
"Path\0"
"Install\0",
.start = path_start,
};