scope.c revision b26fa1a2fbcfee7d03b0c8fd15ec3aa64ae70b9f
/***
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 "alloc-util.h"
#include "dbus-scope.h"
#include "load-dropin.h"
#include "log.h"
#include "scope.h"
#include "special.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "unit-name.h"
#include "unit.h"
[SCOPE_DEAD] = UNIT_INACTIVE,
[SCOPE_RUNNING] = UNIT_ACTIVE,
};
static void scope_init(Unit *u) {
assert(u);
UNIT(s)->ignore_on_isolate = true;
}
static void scope_done(Unit *u) {
assert(u);
free(s->controller);
}
int r;
assert(s);
if (s->timer_event_source) {
if (r < 0)
return r;
}
if (usec == USEC_INFINITY)
return 0;
r = sd_event_add_time(
&s->timer_event_source,
usec, 0,
scope_dispatch_timer, s);
if (r < 0)
return r;
return 0;
}
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);
if (!UNIT(s)->default_dependencies)
return 0;
/* 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_set_default_slice(u);
if (r < 0)
return r;
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)
return 0;
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) {
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 (unit_has_name(u, SPECIAL_INIT_SCOPE))
return -EPERM;
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) {
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;
}
}
usec_t t;
int r;
if (!s->timer_event_source)
return 0;
r = sd_event_source_get_time(s->timer_event_source, &t);
if (r < 0)
return r;
if (t == USEC_INFINITY)
return 0;
*timeout = t;
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)
return false;
}
static void scope_notify_cgroup_empty_event(Unit *u) {
assert(u);
log_unit_debug(u, "cgroup is empty");
}
/* 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 -EPERM;
return -ESTALE;
/* 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 void scope_enumerate(Manager *m) {
Unit *u;
int r;
assert(m);
/* Let's unconditionally add the "init.scope" special unit
* that encapsulates PID 1. Note that PID 1 already is in the
* cgroup for this, we hence just need to allocate the object
* for it and that's it. */
u = manager_get_unit(m, SPECIAL_INIT_SCOPE);
if (!u) {
if (!u) {
log_oom();
return;
}
r = unit_add_name(u, SPECIAL_INIT_SCOPE);
if (r < 0) {
unit_free(u);
log_error_errno(r, "Failed to add init.scope name");
return;
}
}
u->transient = true;
u->default_dependencies = false;
u->no_gc = true;
u->ignore_on_isolate = true;
u->refuse_manual_start = true;
u->refuse_manual_stop = true;
/* Prettify things, if we can. */
if (!u->description)
if (!u->documentation)
}
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,
.can_transient = true,
.init = scope_init,
.load = scope_load,
.done = scope_done,
.dump = scope_dump,
.start = scope_start,
.stop = scope_stop,
.kill = scope_kill,
};