scope.c revision 68db7a3bd9b2f8640c7297382b6d20eb995f7e1e
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 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 <signal.h>
#include <unistd.h>
#include "unit.h"
#include "scope.h"
#include "load-fragment.h"
#include "log.h"
#include "dbus-scope.h"
#include "special.h"
#include "unit-name.h"
#include "load-dropin.h"
[SCOPE_DEAD] = UNIT_INACTIVE,
[SCOPE_RUNNING] = UNIT_ACTIVE,
};
static void scope_init(Unit *u) {
assert(u);
UNIT(s)->ignore_on_isolate = true;
UNIT(s)->ignore_on_snapshot = true;
}
static void scope_done(Unit *u) {
assert(u);
}
static int scope_arm_timer(Scope *s) {
int r;
assert(s);
if (s->timeout_stop_usec <= 0) {
return 0;
}
if (s->timer_event_source) {
if (r < 0)
return r;
}
return sd_event_add_monotonic(UNIT(s)->manager->event, now(CLOCK_MONOTONIC) + s->timeout_stop_usec, 0, scope_dispatch_timer, s, &s->timer_event_source);
}
assert(s);
if (state != SCOPE_STOP_SIGTERM &&
log_debug("%s changed %s -> %s",
}
static int scope_add_default_dependencies(Scope *s) {
int r;
assert(s);
/* Make sure scopes are unloaded on shutdown */
UNIT(s),
SPECIAL_SHUTDOWN_TARGET, NULL, true);
if (r < 0)
return r;
return 0;
}
static int scope_verify(Scope *s) {
assert(s);
return 0;
return -EINVAL;
}
return 0;
}
static int scope_load(Unit *u) {
int r;
assert(s);
return -ENOENT;
u->load_state = UNIT_LOADED;
r = unit_load_dropin(u);
if (r < 0)
return r;
r = unit_add_default_slice(u);
if (r < 0)
return r;
if (u->default_dependencies) {
r = scope_add_default_dependencies(s);
if (r < 0)
return r;
}
return scope_verify(s);
}
static int scope_coldplug(Unit *u) {
int r;
assert(s);
if (s->deserialized_state != s->state) {
r = scope_arm_timer(s);
if (r < 0)
return r;
}
scope_set_state(s, s->deserialized_state);
}
return 0;
}
assert(s);
assert(f);
fprintf(f,
"%sScope State: %s\n"
"%sResult: %s\n",
}
assert(s);
if (f != SCOPE_SUCCESS)
s->result = f;
}
int r;
assert(s);
if (f != SCOPE_SUCCESS)
s->result = f;
r = unit_kill_context(
UNIT(s),
&s->kill_context,
-1, -1, false);
if (r < 0)
goto fail;
if (r > 0) {
r = scope_arm_timer(s);
if (r < 0)
goto fail;
scope_set_state(s, state);
} else
return;
fail:
}
static int scope_start(Unit *u) {
int r;
assert(s);
if (s->state == SCOPE_FAILED)
return -EPERM;
if (s->state == SCOPE_STOP_SIGTERM ||
s->state == SCOPE_STOP_SIGKILL)
return -EAGAIN;
return -ENOENT;
r = unit_realize_cgroup(u);
if (r < 0) {
return r;
}
if (r < 0)
return r;
s->result = SCOPE_SUCCESS;
return 0;
}
static int scope_stop(Unit *u) {
assert(s);
if (s->state == SCOPE_STOP_SIGTERM ||
s->state == SCOPE_STOP_SIGKILL)
return 0;
return 0;
}
static void scope_reset_failed(Unit *u) {
assert(s);
if (s->state == SCOPE_FAILED)
s->result = SCOPE_SUCCESS;
}
}
int r;
if (!s->timer_event_source)
return 0;
if (r < 0)
return r;
return 1;
}
assert(s);
assert(f);
return 0;
}
assert(u);
if (state < 0)
else
s->deserialized_state = state;
} else
return 0;
}
static bool scope_check_gc(Unit *u) {
int r;
assert(s);
/* Never clean up scopes that still have a process around,
* even if the scope is formally dead. */
if (UNIT(s)->cgroup_path) {
if (r <= 0)
return true;
}
return false;
}
assert(s);
switch (s->state) {
case SCOPE_STOP_SIGTERM:
if (s->kill_context.send_sigkill) {
} else {
}
break;
case SCOPE_STOP_SIGKILL:
break;
default:
assert_not_reached("Timeout at wrong time.");
}
return 0;
}
static void scope_notify_cgroup_empty_event(Unit *u) {
assert(u);
switch (s->state) {
case SCOPE_RUNNING:
case SCOPE_STOP_SIGTERM:
case SCOPE_STOP_SIGKILL:
break;
default:
;
}
}
assert(u);
}
assert(u);
}
static const char* const scope_state_table[_SCOPE_STATE_MAX] = {
[SCOPE_DEAD] = "dead",
[SCOPE_RUNNING] = "running",
[SCOPE_STOP_SIGTERM] = "stop-sigterm",
[SCOPE_STOP_SIGKILL] = "stop-sigkill",
[SCOPE_FAILED] = "failed",
};
static const char* const scope_result_table[_SCOPE_RESULT_MAX] = {
[SCOPE_SUCCESS] = "success",
[SCOPE_FAILURE_RESOURCES] = "resources",
[SCOPE_FAILURE_TIMEOUT] = "timeout",
};
const UnitVTable scope_vtable = {
.object_size = sizeof(Scope),
.sections =
"Unit\0"
"Scope\0"
"Install\0",
.private_section = "Scope",
.no_alias = true,
.no_instances = true,
.init = scope_init,
.load = scope_load,
.done = scope_done,
.dump = scope_dump,
.start = scope_start,
.stop = scope_stop,
.kill = scope_kill,
.bus_interface = "org.freedesktop.systemd1.Scope",
.can_transient = true
};