]>
Commit | Line | Data |
---|---|---|
6c12b52e LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2013 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
b5efdb8a | 20 | #include "alloc-util.h" |
96aad8d1 | 21 | #include "bus-common-errors.h" |
b5efdb8a LP |
22 | #include "bus-internal.h" |
23 | #include "bus-util.h" | |
1d22e906 LP |
24 | #include "dbus-cgroup.h" |
25 | #include "dbus-kill.h" | |
26 | #include "dbus-scope.h" | |
b5efdb8a LP |
27 | #include "dbus-unit.h" |
28 | #include "dbus.h" | |
29 | #include "scope.h" | |
30 | #include "selinux-access.h" | |
31 | #include "unit.h" | |
6c12b52e | 32 | |
19070062 | 33 | static int bus_scope_abandon(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
a911bb9a | 34 | Scope *s = userdata; |
4e2f8d27 | 35 | int r; |
a911bb9a | 36 | |
a911bb9a LP |
37 | assert(message); |
38 | assert(s); | |
39 | ||
1d22e906 LP |
40 | r = mac_selinux_unit_access_check(UNIT(s), message, "stop", error); |
41 | if (r < 0) | |
42 | return r; | |
43 | ||
44 | r = bus_verify_manage_units_async(UNIT(s)->manager, message, error); | |
283868e1 SW |
45 | if (r < 0) |
46 | return r; | |
47 | if (r == 0) | |
48 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
49 | ||
4e2f8d27 | 50 | r = scope_abandon(s); |
4e2f8d27 LP |
51 | if (r == -ESTALE) |
52 | return sd_bus_error_setf(error, BUS_ERROR_SCOPE_NOT_RUNNING, "Scope %s is not running, cannot abandon.", UNIT(s)->id); | |
1d22e906 LP |
53 | if (r < 0) |
54 | return r; | |
4e2f8d27 LP |
55 | |
56 | return sd_bus_reply_method_return(message, NULL); | |
a911bb9a LP |
57 | } |
58 | ||
718db961 | 59 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, scope_result, ScopeResult); |
6c12b52e | 60 | |
718db961 LP |
61 | const sd_bus_vtable bus_scope_vtable[] = { |
62 | SD_BUS_VTABLE_START(0), | |
2d4a39e7 | 63 | SD_BUS_PROPERTY("Controller", "s", NULL, offsetof(Scope, controller), SD_BUS_VTABLE_PROPERTY_CONST), |
556089dc | 64 | SD_BUS_PROPERTY("TimeoutStopUSec", "t", bus_property_get_usec, offsetof(Scope, timeout_stop_usec), SD_BUS_VTABLE_PROPERTY_CONST), |
718db961 | 65 | SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Scope, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
2d4a39e7 | 66 | SD_BUS_SIGNAL("RequestStop", NULL, 0), |
1d22e906 | 67 | SD_BUS_METHOD("Abandon", NULL, NULL, bus_scope_abandon, SD_BUS_VTABLE_UNPRIVILEGED), |
718db961 LP |
68 | SD_BUS_VTABLE_END |
69 | }; | |
6c12b52e | 70 | |
9f2e86af | 71 | static int bus_scope_set_transient_property( |
6c12b52e LP |
72 | Scope *s, |
73 | const char *name, | |
718db961 | 74 | sd_bus_message *message, |
6c12b52e | 75 | UnitSetPropertiesMode mode, |
718db961 | 76 | sd_bus_error *error) { |
6c12b52e LP |
77 | |
78 | int r; | |
79 | ||
6c12b52e | 80 | assert(s); |
718db961 LP |
81 | assert(name); |
82 | assert(message); | |
6c12b52e LP |
83 | |
84 | if (streq(name, "PIDs")) { | |
294a90cc | 85 | unsigned n = 0; |
718db961 | 86 | uint32_t pid; |
6c12b52e | 87 | |
718db961 LP |
88 | r = sd_bus_message_enter_container(message, 'a', "u"); |
89 | if (r < 0) | |
90 | return r; | |
6c12b52e | 91 | |
718db961 | 92 | while ((r = sd_bus_message_read(message, "u", &pid)) > 0) { |
6c12b52e LP |
93 | |
94 | if (pid <= 1) | |
95 | return -EINVAL; | |
96 | ||
adb3a45d | 97 | if (mode != UNIT_CHECK) { |
a911bb9a | 98 | r = unit_watch_pid(UNIT(s), pid); |
adb3a45d LP |
99 | if (r < 0 && r != -EEXIST) |
100 | return r; | |
101 | } | |
6c12b52e | 102 | |
adb3a45d | 103 | n++; |
6c12b52e | 104 | } |
718db961 LP |
105 | if (r < 0) |
106 | return r; | |
107 | ||
108 | r = sd_bus_message_exit_container(message); | |
109 | if (r < 0) | |
110 | return r; | |
6c12b52e | 111 | |
adb3a45d | 112 | if (n <= 0) |
6c12b52e LP |
113 | return -EINVAL; |
114 | ||
115 | return 1; | |
cc23f9f1 | 116 | |
2d4a39e7 LP |
117 | } else if (streq(name, "Controller")) { |
118 | const char *controller; | |
119 | char *c; | |
120 | ||
121 | r = sd_bus_message_read(message, "s", &controller); | |
122 | if (r < 0) | |
123 | return r; | |
124 | ||
125 | if (!isempty(controller) && !service_name_is_valid(controller)) | |
126 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Controller '%s' is not a valid bus name.", controller); | |
127 | ||
128 | if (mode != UNIT_CHECK) { | |
129 | if (isempty(controller)) | |
130 | c = NULL; | |
131 | else { | |
132 | c = strdup(controller); | |
133 | if (!c) | |
134 | return -ENOMEM; | |
135 | } | |
136 | ||
137 | free(s->controller); | |
138 | s->controller = c; | |
139 | } | |
140 | ||
141 | return 1; | |
142 | ||
cc23f9f1 LP |
143 | } else if (streq(name, "TimeoutStopUSec")) { |
144 | ||
cc23f9f1 | 145 | if (mode != UNIT_CHECK) { |
718db961 LP |
146 | r = sd_bus_message_read(message, "t", &s->timeout_stop_usec); |
147 | if (r < 0) | |
148 | return r; | |
149 | ||
de0671ee | 150 | unit_write_drop_in_format(UNIT(s), mode, name, "[Scope]\nTimeoutStopSec="USEC_FMT"us\n", s->timeout_stop_usec); |
718db961 LP |
151 | } else { |
152 | r = sd_bus_message_skip(message, "t"); | |
153 | if (r < 0) | |
154 | return r; | |
cc23f9f1 LP |
155 | } |
156 | ||
157 | return 1; | |
6c12b52e LP |
158 | } |
159 | ||
160 | return 0; | |
161 | } | |
162 | ||
163 | int bus_scope_set_property( | |
164 | Unit *u, | |
165 | const char *name, | |
718db961 | 166 | sd_bus_message *message, |
6c12b52e | 167 | UnitSetPropertiesMode mode, |
718db961 | 168 | sd_bus_error *error) { |
6c12b52e LP |
169 | |
170 | Scope *s = SCOPE(u); | |
171 | int r; | |
172 | ||
718db961 | 173 | assert(s); |
6c12b52e | 174 | assert(name); |
718db961 | 175 | assert(message); |
6c12b52e | 176 | |
718db961 | 177 | r = bus_cgroup_set_property(u, &s->cgroup_context, name, message, mode, error); |
6c12b52e LP |
178 | if (r != 0) |
179 | return r; | |
180 | ||
181 | if (u->load_state == UNIT_STUB) { | |
182 | /* While we are created we still accept PIDs */ | |
183 | ||
718db961 | 184 | r = bus_scope_set_transient_property(s, name, message, mode, error); |
6c12b52e LP |
185 | if (r != 0) |
186 | return r; | |
a6c0353b | 187 | |
718db961 | 188 | r = bus_kill_context_set_transient_property(u, &s->kill_context, name, message, mode, error); |
a6c0353b LP |
189 | if (r != 0) |
190 | return r; | |
6c12b52e LP |
191 | } |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
196 | int bus_scope_commit_properties(Unit *u) { | |
197 | assert(u); | |
198 | ||
bc432dc7 | 199 | unit_update_cgroup_members_masks(u); |
6c12b52e | 200 | unit_realize_cgroup(u); |
bc432dc7 | 201 | |
6c12b52e LP |
202 | return 0; |
203 | } | |
2d4a39e7 LP |
204 | |
205 | int bus_scope_send_request_stop(Scope *s) { | |
4afd3348 | 206 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
2d4a39e7 LP |
207 | _cleanup_free_ char *p = NULL; |
208 | int r; | |
209 | ||
210 | assert(s); | |
211 | ||
212 | if (!s->controller) | |
213 | return 0; | |
214 | ||
215 | p = unit_dbus_path(UNIT(s)); | |
216 | if (!p) | |
217 | return -ENOMEM; | |
218 | ||
219 | r = sd_bus_message_new_signal( | |
220 | UNIT(s)->manager->api_bus, | |
151b9b96 | 221 | &m, |
2d4a39e7 LP |
222 | p, |
223 | "org.freedesktop.systemd1.Scope", | |
151b9b96 | 224 | "RequestStop"); |
2d4a39e7 LP |
225 | if (r < 0) |
226 | return r; | |
227 | ||
228 | return sd_bus_send_to(UNIT(s)->manager->api_bus, m, /* s->controller */ NULL, NULL); | |
229 | } |