1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "alloc-util.h"
25 #include "dbus-slice.h"
29 #include "string-util.h"
31 #include "unit-name.h"
34 static const UnitActiveState state_translation_table
[_SLICE_STATE_MAX
] = {
35 [SLICE_DEAD
] = UNIT_INACTIVE
,
36 [SLICE_ACTIVE
] = UNIT_ACTIVE
39 static void slice_set_state(Slice
*t
, SliceState state
) {
46 if (state
!= old_state
)
47 log_debug("%s changed %s -> %s",
49 slice_state_to_string(old_state
),
50 slice_state_to_string(state
));
52 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
], true);
55 static int slice_add_parent_slice(Slice
*s
) {
62 if (UNIT_ISSET(UNIT(s
)->slice
))
65 if (unit_has_name(UNIT(s
), SPECIAL_ROOT_SLICE
))
68 a
= strdupa(UNIT(s
)->id
);
69 dash
= strrchr(a
, '-');
71 strcpy(dash
, ".slice");
73 a
= (char*) SPECIAL_ROOT_SLICE
;
75 r
= manager_load_unit(UNIT(s
)->manager
, a
, NULL
, NULL
, &parent
);
79 unit_ref_set(&UNIT(s
)->slice
, parent
);
83 static int slice_add_default_dependencies(Slice
*s
) {
88 if (!UNIT(s
)->default_dependencies
)
91 /* Make sure slices are unloaded on shutdown */
92 r
= unit_add_two_dependencies_by_name(
94 UNIT_BEFORE
, UNIT_CONFLICTS
,
95 SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
102 static int slice_verify(Slice
*s
) {
103 _cleanup_free_
char *parent
= NULL
;
108 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
111 if (!slice_name_is_valid(UNIT(s
)->id
)) {
112 log_unit_error(UNIT(s
), "Slice name %s is not valid. Refusing.", UNIT(s
)->id
);
116 r
= slice_build_parent_slice(UNIT(s
)->id
, &parent
);
118 return log_unit_error_errno(UNIT(s
), r
, "Failed to determine parent slice: %m");
120 if (parent
? !unit_has_name(UNIT_DEREF(UNIT(s
)->slice
), parent
) : UNIT_ISSET(UNIT(s
)->slice
)) {
121 log_unit_error(UNIT(s
), "Located outside of parent slice. Refusing.");
128 static int slice_load(Unit
*u
) {
134 r
= unit_load_fragment_and_dropin_optional(u
);
138 /* This is a new unit? Then let's add in some extras */
139 if (u
->load_state
== UNIT_LOADED
) {
141 r
= unit_patch_contexts(u
);
145 r
= slice_add_parent_slice(s
);
149 r
= slice_add_default_dependencies(s
);
154 return slice_verify(s
);
157 static int slice_coldplug(Unit
*u
) {
161 assert(t
->state
== SLICE_DEAD
);
163 if (t
->deserialized_state
!= t
->state
)
164 slice_set_state(t
, t
->deserialized_state
);
169 static void slice_dump(Unit
*u
, FILE *f
, const char *prefix
) {
176 "%sSlice State: %s\n",
177 prefix
, slice_state_to_string(t
->state
));
179 cgroup_context_dump(&t
->cgroup_context
, f
, prefix
);
182 static int slice_start(Unit
*u
) {
186 assert(t
->state
== SLICE_DEAD
);
188 (void) unit_realize_cgroup(u
);
189 (void) unit_reset_cpu_usage(u
);
191 slice_set_state(t
, SLICE_ACTIVE
);
195 static int slice_stop(Unit
*u
) {
199 assert(t
->state
== SLICE_ACTIVE
);
201 /* We do not need to destroy the cgroup explicitly,
202 * unit_notify() will do that for us anyway. */
204 slice_set_state(t
, SLICE_DEAD
);
208 static int slice_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
209 return unit_kill_common(u
, who
, signo
, -1, -1, error
);
212 static int slice_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
219 unit_serialize_item(u
, f
, "state", slice_state_to_string(s
->state
));
223 static int slice_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
231 if (streq(key
, "state")) {
234 state
= slice_state_from_string(value
);
236 log_debug("Failed to parse state value %s", value
);
238 s
->deserialized_state
= state
;
241 log_debug("Unknown serialization key '%s'", key
);
246 _pure_
static UnitActiveState
slice_active_state(Unit
*u
) {
249 return state_translation_table
[SLICE(u
)->state
];
252 _pure_
static const char *slice_sub_state_to_string(Unit
*u
) {
255 return slice_state_to_string(SLICE(u
)->state
);
258 static void slice_enumerate(Manager
*m
) {
264 u
= manager_get_unit(m
, SPECIAL_ROOT_SLICE
);
266 u
= unit_new(m
, sizeof(Slice
));
272 r
= unit_add_name(u
, SPECIAL_ROOT_SLICE
);
275 log_error_errno(r
, "Failed to add -.slice name");
280 u
->default_dependencies
= false;
282 u
->ignore_on_isolate
= true;
283 u
->refuse_manual_start
= true;
284 u
->refuse_manual_stop
= true;
285 SLICE(u
)->deserialized_state
= SLICE_ACTIVE
;
288 u
->description
= strdup("Root Slice");
289 if (!u
->documentation
)
290 (void) strv_extend(&u
->documentation
, "man:systemd.special(7)");
292 unit_add_to_load_queue(u
);
293 unit_add_to_dbus_queue(u
);
296 const UnitVTable slice_vtable
= {
297 .object_size
= sizeof(Slice
),
298 .cgroup_context_offset
= offsetof(Slice
, cgroup_context
),
304 .private_section
= "Slice",
307 .no_instances
= true,
308 .can_transient
= true,
312 .coldplug
= slice_coldplug
,
316 .start
= slice_start
,
321 .serialize
= slice_serialize
,
322 .deserialize_item
= slice_deserialize_item
,
324 .active_state
= slice_active_state
,
325 .sub_state_to_string
= slice_sub_state_to_string
,
327 .bus_vtable
= bus_slice_vtable
,
328 .bus_set_property
= bus_slice_set_property
,
329 .bus_commit_properties
= bus_slice_commit_properties
,
331 .enumerate
= slice_enumerate
,
333 .status_message_formats
= {
334 .finished_start_job
= {
335 [JOB_DONE
] = "Created slice %s.",
337 .finished_stop_job
= {
338 [JOB_DONE
] = "Removed slice %s.",