scope.c revision dd305ec9c6c2ef6e0b5e43de388a98825385b359
/*-*- 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 <unistd.h>
#include "unit.h"
#include "scope.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);
free(s->controller);
}
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_time(
&s->timer_event_source,
scope_dispatch_timer, s);
}
assert(s);
log_debug("%s changed %s -> %s", UNIT(s)->id, scope_state_to_string(old_state), scope_state_to_string(state));
}
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_patch_contexts(u);
if (r < 0)
return r;
r = unit_add_default_slice(u, &s->cgroup_context);
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;
}
unit_watch_all_pids(UNIT(s));
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;
}
bool skip_signal = false;
int r;
assert(s);
if (f != SCOPE_SUCCESS)
s->result = f;
unit_watch_all_pids(UNIT(s));
/* If we have a controller set let's ask the controller nicely
* to terminate the scope, instead of us going directly into
* SIGTERM beserk mode */
if (state == SCOPE_STOP_SIGTERM)
skip_signal = bus_scope_send_request_stop(s) > 0;
if (!skip_signal) {
r = unit_kill_context(
UNIT(s),
&s->kill_context,
-1, -1, false);
if (r < 0)
goto fail;
} else
r = 1;
if (r > 0) {
r = scope_arm_timer(s);
if (r < 0)
goto fail;
scope_set_state(s, state);
} else if (state == SCOPE_STOP_SIGTERM)
else
return;
fail:
}
static int scope_start(Unit *u) {
int r;
assert(s);
if (s->state == SCOPE_FAILED)
return -EPERM;
/* We can't fulfill this right now, please try again later */
if (s->state == SCOPE_STOP_SIGTERM ||
s->state == SCOPE_STOP_SIGKILL)
return -EAGAIN;
return -ENOENT;
(void) unit_realize_cgroup(u);
(void) unit_reset_cpu_usage(u);
r = unit_attach_pids_to_cgroup(u);
if (r < 0) {
log_unit_warning_errno(UNIT(s)->id, r, "%s: Failed to add PIDs to scope's control group: %m", UNIT(s)->id);
return r;
}
s->result = SCOPE_SUCCESS;
return 1;
}
static int scope_stop(Unit *u) {
assert(s);
if (s->state == SCOPE_STOP_SIGTERM ||
s->state == SCOPE_STOP_SIGKILL)
return 0;
s->state == SCOPE_ABANDONED);
return 1;
}
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) {
assert(u);
/* Never clean up scopes that still have a process around,
* even if the scope is formally dead. */
if (u->cgroup_path) {
int r;
if (r <= 0)
return true;
}
return false;
}
static void scope_notify_cgroup_empty_event(Unit *u) {
assert(u);
}
/* If we get a SIGCHLD event for one of the processes we were
interested in, then we look for others to watch, under the
assumption that we'll sooner or later get a SIGCHLD for
them, as the original process we watched was probably the
parent of them, and they are hence now our children. */
unit_tidy_watch_pids(u, 0, 0);
/* If the PID set is empty now, then let's finish this off */
if (set_isempty(u->pids))
}
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;
}
int scope_abandon(Scope *s) {
assert(s);
return -ESTALE;
free(s->controller);
s->controller = NULL;
/* The client is no longer watching the remaining processes,
* so let's step in here, under the assumption that the
* remaining processes will be sooner or later reassigned to
* us as parent. */
unit_tidy_watch_pids(UNIT(s), 0, 0);
unit_watch_all_pids(UNIT(s));
/* If the PID set is empty now, then let's finish this off */
else
return 0;
}
assert(u);
}
assert(u);
}
static const char* const scope_state_table[_SCOPE_STATE_MAX] = {
[SCOPE_DEAD] = "dead",
[SCOPE_RUNNING] = "running",
[SCOPE_ABANDONED] = "abandoned",
[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
};