]>
Commit | Line | Data |
---|---|---|
ea430986 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
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 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
ea430986 LP |
22 | #include <errno.h> |
23 | ||
24 | #include "dbus.h" | |
25 | #include "log.h" | |
4139c1b2 | 26 | #include "dbus-manager.h" |
1137a57c | 27 | #include "strv.h" |
398ef8ba | 28 | #include "bus-errors.h" |
ea430986 | 29 | |
4288f619 LP |
30 | #define BUS_MANAGER_INTERFACE \ |
31 | " <interface name=\"org.freedesktop.systemd1.Manager\">\n" \ | |
32 | " <method name=\"GetUnit\">\n" \ | |
33 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
34 | " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \ | |
35 | " </method>\n" \ | |
36 | " <method name=\"LoadUnit\">\n" \ | |
37 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
38 | " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \ | |
39 | " </method>\n" \ | |
c87eba54 LP |
40 | " <method name=\"StartUnit\">\n" \ |
41 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
42 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ | |
43 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \ | |
44 | " </method>\n" \ | |
45 | " <method name=\"StopUnit\">\n" \ | |
46 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
47 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ | |
48 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \ | |
49 | " </method>\n" \ | |
50 | " <method name=\"ReloadUnit\">\n" \ | |
51 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
52 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ | |
53 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \ | |
54 | " </method>\n" \ | |
55 | " <method name=\"RestartUnit\">\n" \ | |
56 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
57 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ | |
58 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \ | |
59 | " </method>\n" \ | |
9a1ac7b9 LP |
60 | " <method name=\"TryRestartUnit\">\n" \ |
61 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
62 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ | |
63 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \ | |
64 | " </method>\n" \ | |
6f28c033 LP |
65 | " <method name=\"ReloadOrRestartUnit\">\n" \ |
66 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
67 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ | |
68 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \ | |
69 | " </method>\n" \ | |
70 | " <method name=\"ReloadOrTryRestartUnit\">\n" \ | |
71 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
72 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ | |
73 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \ | |
74 | " </method>\n" \ | |
5632e374 LP |
75 | " <method name=\"ResetMaintenanceUnit\">\n" \ |
76 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
77 | " </method>\n" \ | |
4288f619 LP |
78 | " <method name=\"GetJob\">\n" \ |
79 | " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \ | |
80 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \ | |
81 | " </method>\n" \ | |
82 | " <method name=\"ClearJobs\"/>\n" \ | |
5632e374 | 83 | " <method name=\"ResetMaintenance\"/>\n" \ |
4288f619 | 84 | " <method name=\"ListUnits\">\n" \ |
8fe914ec | 85 | " <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \ |
4288f619 LP |
86 | " </method>\n" \ |
87 | " <method name=\"ListJobs\">\n" \ | |
88 | " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \ | |
89 | " </method>\n" \ | |
90 | " <method name=\"Subscribe\"/>\n" \ | |
91 | " <method name=\"Unsubscribe\"/>\n" \ | |
92 | " <method name=\"Dump\"/>\n" \ | |
93 | " <method name=\"CreateSnapshot\">\n" \ | |
94 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
95 | " <arg nane=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \ | |
96 | " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \ | |
97 | " </method>\n" \ | |
98 | " <method name=\"Reload\"/>\n" \ | |
99 | " <method name=\"Reexecute\"/>\n" \ | |
100 | " <method name=\"Exit\"/>\n" \ | |
101 | " <method name=\"SetEnvironment\">\n" \ | |
102 | " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \ | |
103 | " </method>\n" \ | |
104 | " <method name=\"UnsetEnvironment\">\n" \ | |
105 | " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \ | |
106 | " </method>\n" \ | |
107 | " <signal name=\"UnitNew\">\n" \ | |
108 | " <arg name=\"id\" type=\"s\"/>\n" \ | |
109 | " <arg name=\"unit\" type=\"o\"/>\n" \ | |
110 | " </signal>\n" \ | |
111 | " <signal name=\"UnitRemoved\">\n" \ | |
112 | " <arg name=\"id\" type=\"s\"/>\n" \ | |
113 | " <arg name=\"unit\" type=\"o\"/>\n" \ | |
114 | " </signal>\n" \ | |
115 | " <signal name=\"JobNew\">\n" \ | |
116 | " <arg name=\"id\" type=\"u\"/>\n" \ | |
117 | " <arg name=\"job\" type=\"o\"/>\n" \ | |
118 | " </signal>\n" \ | |
119 | " <signal name=\"JobRemoved\">\n" \ | |
120 | " <arg name=\"id\" type=\"u\"/>\n" \ | |
121 | " <arg name=\"job\" type=\"o\"/>\n" \ | |
122 | " <arg name=\"success\" type=\"b\"/>\n" \ | |
123 | " </signal>" \ | |
124 | " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \ | |
125 | " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \ | |
0442c13b | 126 | " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \ |
4288f619 LP |
127 | " <property name=\"LogLevel\" type=\"s\" access=\"read\"/>\n" \ |
128 | " <property name=\"LogTarget\" type=\"s\" access=\"read\"/>\n" \ | |
129 | " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \ | |
130 | " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \ | |
131 | " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \ | |
f295f5c0 | 132 | " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \ |
9e58ff9c | 133 | " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \ |
f295f5c0 LP |
134 | " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \ |
135 | " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \ | |
136 | " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n" \ | |
137 | " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \ | |
c6c18be3 | 138 | " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \ |
4288f619 LP |
139 | " </interface>\n" |
140 | ||
ea430986 LP |
141 | #define INTROSPECTION_BEGIN \ |
142 | DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \ | |
4288f619 LP |
143 | "<node>\n" \ |
144 | BUS_MANAGER_INTERFACE \ | |
ea430986 LP |
145 | BUS_PROPERTIES_INTERFACE \ |
146 | BUS_INTROSPECTABLE_INTERFACE | |
147 | ||
148 | #define INTROSPECTION_END \ | |
4288f619 LP |
149 | "</node>\n" |
150 | ||
151 | const char bus_manager_interface[] = BUS_MANAGER_INTERFACE; | |
ea430986 | 152 | |
4139c1b2 | 153 | static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs); |
1adf1049 LP |
154 | |
155 | static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
156 | const char *t; | |
157 | ||
158 | assert(m); | |
159 | assert(i); | |
160 | assert(property); | |
161 | ||
162 | t = log_target_to_string(log_get_target()); | |
163 | ||
164 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t)) | |
165 | return -ENOMEM; | |
166 | ||
167 | return 0; | |
168 | } | |
169 | ||
170 | static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
171 | const char *t; | |
172 | ||
173 | assert(m); | |
174 | assert(i); | |
175 | assert(property); | |
176 | ||
177 | t = log_level_to_string(log_get_max_level()); | |
178 | ||
179 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t)) | |
180 | return -ENOMEM; | |
181 | ||
182 | return 0; | |
183 | } | |
184 | ||
4f0f902f LP |
185 | static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
186 | uint32_t u; | |
187 | ||
188 | assert(m); | |
189 | assert(i); | |
190 | assert(property); | |
191 | ||
192 | u = hashmap_size(m->units); | |
193 | ||
194 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u)) | |
195 | return -ENOMEM; | |
196 | ||
197 | return 0; | |
198 | } | |
199 | ||
200 | static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
201 | uint32_t u; | |
202 | ||
203 | assert(m); | |
204 | assert(i); | |
205 | assert(property); | |
206 | ||
207 | u = hashmap_size(m->jobs); | |
208 | ||
209 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u)) | |
210 | return -ENOMEM; | |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
894ba510 LP |
215 | static const char *message_get_sender_with_fallback(DBusMessage *m) { |
216 | const char *s; | |
217 | ||
218 | assert(m); | |
219 | ||
220 | if ((s = dbus_message_get_sender(m))) | |
221 | return s; | |
222 | ||
223 | /* When the message came in from a direct connection the | |
224 | * message will have no sender. We fix that here. */ | |
225 | ||
226 | return ":no-sender"; | |
227 | } | |
228 | ||
5e8d1c9a | 229 | static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) { |
ea430986 | 230 | Manager *m = data; |
1adf1049 LP |
231 | |
232 | const BusProperty properties[] = { | |
f295f5c0 LP |
233 | { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING }, |
234 | { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as }, | |
235 | { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime }, | |
236 | { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL }, | |
237 | { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL }, | |
238 | { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL }, | |
239 | { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL }, | |
240 | { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment }, | |
241 | { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn }, | |
9e58ff9c | 242 | { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status }, |
f295f5c0 LP |
243 | { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path }, |
244 | { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path }, | |
245 | { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path }, | |
246 | { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket }, | |
c6c18be3 | 247 | { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy }, |
1adf1049 LP |
248 | { NULL, NULL, NULL, NULL, NULL } |
249 | }; | |
250 | ||
251 | int r; | |
ea430986 LP |
252 | DBusError error; |
253 | DBusMessage *reply = NULL; | |
254 | char * path = NULL; | |
c87eba54 | 255 | JobType job_type = _JOB_TYPE_INVALID; |
6f28c033 | 256 | bool reload_if_possible = false; |
ea430986 LP |
257 | |
258 | assert(connection); | |
259 | assert(message); | |
260 | assert(m); | |
261 | ||
262 | dbus_error_init(&error); | |
263 | ||
4139c1b2 | 264 | if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) { |
ea430986 LP |
265 | const char *name; |
266 | Unit *u; | |
267 | ||
268 | if (!dbus_message_get_args( | |
269 | message, | |
270 | &error, | |
271 | DBUS_TYPE_STRING, &name, | |
272 | DBUS_TYPE_INVALID)) | |
5e8d1c9a | 273 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); |
ea430986 | 274 | |
398ef8ba LP |
275 | if (!(u = manager_get_unit(m, name))) { |
276 | dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name); | |
277 | return bus_send_error_reply(m, connection, message, &error, -ENOENT); | |
278 | } | |
ea430986 LP |
279 | |
280 | if (!(reply = dbus_message_new_method_return(message))) | |
281 | goto oom; | |
282 | ||
283 | if (!(path = unit_dbus_path(u))) | |
284 | goto oom; | |
285 | ||
286 | if (!dbus_message_append_args( | |
287 | reply, | |
288 | DBUS_TYPE_OBJECT_PATH, &path, | |
289 | DBUS_TYPE_INVALID)) | |
290 | goto oom; | |
291 | ||
4139c1b2 | 292 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) { |
ea430986 LP |
293 | const char *name; |
294 | Unit *u; | |
295 | ||
296 | if (!dbus_message_get_args( | |
297 | message, | |
298 | &error, | |
299 | DBUS_TYPE_STRING, &name, | |
300 | DBUS_TYPE_INVALID)) | |
5e8d1c9a | 301 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); |
ea430986 | 302 | |
398ef8ba LP |
303 | if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0) |
304 | return bus_send_error_reply(m, connection, message, &error, r); | |
ea430986 LP |
305 | |
306 | if (!(reply = dbus_message_new_method_return(message))) | |
307 | goto oom; | |
308 | ||
309 | if (!(path = unit_dbus_path(u))) | |
310 | goto oom; | |
311 | ||
312 | if (!dbus_message_append_args( | |
313 | reply, | |
314 | DBUS_TYPE_OBJECT_PATH, &path, | |
315 | DBUS_TYPE_INVALID)) | |
316 | goto oom; | |
317 | ||
c87eba54 LP |
318 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit")) |
319 | job_type = JOB_START; | |
320 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit")) | |
321 | job_type = JOB_STOP; | |
322 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit")) | |
323 | job_type = JOB_RELOAD; | |
324 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit")) | |
325 | job_type = JOB_RESTART; | |
9a1ac7b9 LP |
326 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit")) |
327 | job_type = JOB_TRY_RESTART; | |
6f28c033 LP |
328 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) { |
329 | reload_if_possible = true; | |
330 | job_type = JOB_RESTART; | |
331 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) { | |
332 | reload_if_possible = true; | |
333 | job_type = JOB_TRY_RESTART; | |
334 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) { | |
ea430986 LP |
335 | uint32_t id; |
336 | Job *j; | |
337 | ||
338 | if (!dbus_message_get_args( | |
339 | message, | |
340 | &error, | |
341 | DBUS_TYPE_UINT32, &id, | |
342 | DBUS_TYPE_INVALID)) | |
5e8d1c9a | 343 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); |
ea430986 | 344 | |
398ef8ba LP |
345 | if (!(j = manager_get_job(m, id))) { |
346 | dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id); | |
347 | return bus_send_error_reply(m, connection, message, &error, -ENOENT); | |
348 | } | |
ea430986 LP |
349 | |
350 | if (!(reply = dbus_message_new_method_return(message))) | |
351 | goto oom; | |
352 | ||
353 | if (!(path = job_dbus_path(j))) | |
354 | goto oom; | |
355 | ||
356 | if (!dbus_message_append_args( | |
357 | reply, | |
358 | DBUS_TYPE_OBJECT_PATH, &path, | |
359 | DBUS_TYPE_INVALID)) | |
360 | goto oom; | |
361 | ||
4139c1b2 | 362 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) { |
ea430986 LP |
363 | |
364 | manager_clear_jobs(m); | |
365 | ||
366 | if (!(reply = dbus_message_new_method_return(message))) | |
367 | goto oom; | |
368 | ||
5632e374 LP |
369 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetMaintenance")) { |
370 | ||
371 | manager_reset_maintenance(m); | |
372 | ||
373 | if (!(reply = dbus_message_new_method_return(message))) | |
374 | goto oom; | |
375 | ||
376 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetMaintenanceUnit")) { | |
377 | const char *name; | |
378 | Unit *u; | |
379 | ||
380 | if (!dbus_message_get_args( | |
381 | message, | |
382 | &error, | |
383 | DBUS_TYPE_STRING, &name, | |
384 | DBUS_TYPE_INVALID)) | |
385 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); | |
386 | ||
387 | if (!(u = manager_get_unit(m, name))) { | |
388 | dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name); | |
389 | return bus_send_error_reply(m, connection, message, &error, -ENOENT); | |
390 | } | |
391 | ||
392 | unit_reset_maintenance(u); | |
393 | ||
394 | if (!(reply = dbus_message_new_method_return(message))) | |
395 | goto oom; | |
396 | ||
4139c1b2 | 397 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) { |
ea430986 LP |
398 | DBusMessageIter iter, sub; |
399 | Iterator i; | |
400 | Unit *u; | |
401 | const char *k; | |
402 | ||
403 | if (!(reply = dbus_message_new_method_return(message))) | |
404 | goto oom; | |
405 | ||
406 | dbus_message_iter_init_append(reply, &iter); | |
407 | ||
8fe914ec | 408 | if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub)) |
ea430986 LP |
409 | goto oom; |
410 | ||
411 | HASHMAP_FOREACH_KEY(u, k, m->units, i) { | |
47be870b | 412 | char *u_path, *j_path; |
8fe914ec | 413 | const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following; |
ea430986 LP |
414 | DBusMessageIter sub2; |
415 | uint32_t job_id; | |
a7f241db | 416 | Unit *f; |
ea430986 | 417 | |
9e2f7c11 | 418 | if (k != u->meta.id) |
ea430986 LP |
419 | continue; |
420 | ||
421 | if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2)) | |
422 | goto oom; | |
423 | ||
424 | description = unit_description(u); | |
425 | load_state = unit_load_state_to_string(u->meta.load_state); | |
426 | active_state = unit_active_state_to_string(unit_active_state(u)); | |
10a94420 | 427 | sub_state = unit_sub_state_to_string(u); |
a7f241db LP |
428 | |
429 | f = unit_following(u); | |
430 | following = f ? f->meta.id : ""; | |
ea430986 | 431 | |
47be870b | 432 | if (!(u_path = unit_dbus_path(u))) |
ea430986 LP |
433 | goto oom; |
434 | ||
435 | if (u->meta.job) { | |
436 | job_id = (uint32_t) u->meta.job->id; | |
437 | ||
47be870b LP |
438 | if (!(j_path = job_dbus_path(u->meta.job))) { |
439 | free(u_path); | |
ea430986 LP |
440 | goto oom; |
441 | } | |
442 | ||
c87eba54 | 443 | sjob_type = job_type_to_string(u->meta.job->type); |
ea430986 LP |
444 | } else { |
445 | job_id = 0; | |
47be870b | 446 | j_path = u_path; |
c87eba54 | 447 | sjob_type = ""; |
ea430986 LP |
448 | } |
449 | ||
9e2f7c11 | 450 | if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) || |
ea430986 LP |
451 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) || |
452 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) || | |
453 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) || | |
10a94420 | 454 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) || |
8fe914ec | 455 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) || |
47be870b | 456 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) || |
ea430986 | 457 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) || |
c87eba54 | 458 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) || |
47be870b LP |
459 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) { |
460 | free(u_path); | |
ea430986 | 461 | if (u->meta.job) |
47be870b | 462 | free(j_path); |
ea430986 LP |
463 | goto oom; |
464 | } | |
465 | ||
47be870b | 466 | free(u_path); |
ea430986 | 467 | if (u->meta.job) |
47be870b | 468 | free(j_path); |
ea430986 LP |
469 | |
470 | if (!dbus_message_iter_close_container(&sub, &sub2)) | |
471 | goto oom; | |
472 | } | |
473 | ||
474 | if (!dbus_message_iter_close_container(&iter, &sub)) | |
475 | goto oom; | |
476 | ||
4139c1b2 | 477 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) { |
ea430986 LP |
478 | DBusMessageIter iter, sub; |
479 | Iterator i; | |
480 | Job *j; | |
481 | ||
482 | if (!(reply = dbus_message_new_method_return(message))) | |
483 | goto oom; | |
484 | ||
485 | dbus_message_iter_init_append(reply, &iter); | |
486 | ||
487 | if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub)) | |
488 | goto oom; | |
489 | ||
490 | HASHMAP_FOREACH(j, m->jobs, i) { | |
47be870b | 491 | char *u_path, *j_path; |
9e2f7c11 | 492 | const char *state, *type; |
ea430986 LP |
493 | uint32_t id; |
494 | DBusMessageIter sub2; | |
495 | ||
496 | if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2)) | |
497 | goto oom; | |
498 | ||
499 | id = (uint32_t) j->id; | |
ea430986 | 500 | state = job_state_to_string(j->state); |
2b53c70b | 501 | type = job_type_to_string(j->type); |
ea430986 | 502 | |
47be870b | 503 | if (!(j_path = job_dbus_path(j))) |
ea430986 LP |
504 | goto oom; |
505 | ||
47be870b LP |
506 | if (!(u_path = unit_dbus_path(j->unit))) { |
507 | free(j_path); | |
ea430986 LP |
508 | goto oom; |
509 | } | |
510 | ||
511 | if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) || | |
9e2f7c11 | 512 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) || |
ea430986 LP |
513 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) || |
514 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) || | |
47be870b LP |
515 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) || |
516 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) { | |
517 | free(j_path); | |
518 | free(u_path); | |
ea430986 LP |
519 | goto oom; |
520 | } | |
521 | ||
47be870b LP |
522 | free(j_path); |
523 | free(u_path); | |
ea430986 LP |
524 | |
525 | if (!dbus_message_iter_close_container(&sub, &sub2)) | |
526 | goto oom; | |
527 | } | |
528 | ||
529 | if (!dbus_message_iter_close_container(&iter, &sub)) | |
530 | goto oom; | |
531 | ||
4139c1b2 | 532 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) { |
c1e1601e | 533 | char *client; |
a567261a LP |
534 | Set *s; |
535 | ||
536 | if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) { | |
537 | if (!(s = set_new(string_hash_func, string_compare_func))) | |
538 | goto oom; | |
539 | ||
540 | if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) { | |
541 | set_free(s); | |
542 | goto oom; | |
543 | } | |
544 | } | |
c1e1601e | 545 | |
894ba510 | 546 | if (!(client = strdup(message_get_sender_with_fallback(message)))) |
c1e1601e LP |
547 | goto oom; |
548 | ||
a567261a LP |
549 | if ((r = set_put(s, client)) < 0) { |
550 | free(client); | |
5e8d1c9a | 551 | return bus_send_error_reply(m, connection, message, NULL, r); |
a567261a | 552 | } |
c1e1601e LP |
553 | |
554 | if (!(reply = dbus_message_new_method_return(message))) | |
555 | goto oom; | |
556 | ||
4139c1b2 | 557 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) { |
c1e1601e LP |
558 | char *client; |
559 | ||
398ef8ba LP |
560 | if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) { |
561 | dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed."); | |
562 | return bus_send_error_reply(m, connection, message, &error, -ENOENT); | |
563 | } | |
c1e1601e LP |
564 | |
565 | free(client); | |
566 | ||
567 | if (!(reply = dbus_message_new_method_return(message))) | |
568 | goto oom; | |
569 | ||
4139c1b2 | 570 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) { |
b152adec LP |
571 | FILE *f; |
572 | char *dump = NULL; | |
573 | size_t size; | |
574 | ||
575 | if (!(reply = dbus_message_new_method_return(message))) | |
576 | goto oom; | |
577 | ||
578 | if (!(f = open_memstream(&dump, &size))) | |
579 | goto oom; | |
580 | ||
581 | manager_dump_units(m, f, NULL); | |
582 | manager_dump_jobs(m, f, NULL); | |
583 | ||
584 | if (ferror(f)) { | |
585 | fclose(f); | |
586 | free(dump); | |
587 | goto oom; | |
588 | } | |
589 | ||
590 | fclose(f); | |
591 | ||
592 | if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) { | |
593 | free(dump); | |
594 | goto oom; | |
595 | } | |
596 | ||
597 | free(dump); | |
4139c1b2 LP |
598 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) { |
599 | const char *name; | |
600 | dbus_bool_t cleanup; | |
601 | Snapshot *s; | |
602 | ||
603 | if (!dbus_message_get_args( | |
604 | message, | |
605 | &error, | |
606 | DBUS_TYPE_STRING, &name, | |
607 | DBUS_TYPE_BOOLEAN, &cleanup, | |
608 | DBUS_TYPE_INVALID)) | |
5e8d1c9a | 609 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); |
4139c1b2 LP |
610 | |
611 | if (name && name[0] == 0) | |
612 | name = NULL; | |
613 | ||
398ef8ba LP |
614 | if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0) |
615 | return bus_send_error_reply(m, connection, message, &error, r); | |
4139c1b2 LP |
616 | |
617 | if (!(reply = dbus_message_new_method_return(message))) | |
618 | goto oom; | |
619 | ||
620 | if (!(path = unit_dbus_path(UNIT(s)))) | |
621 | goto oom; | |
622 | ||
623 | if (!dbus_message_append_args( | |
624 | reply, | |
625 | DBUS_TYPE_OBJECT_PATH, &path, | |
626 | DBUS_TYPE_INVALID)) | |
627 | goto oom; | |
b152adec | 628 | |
ea430986 LP |
629 | } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) { |
630 | char *introspection = NULL; | |
631 | FILE *f; | |
632 | Iterator i; | |
633 | Unit *u; | |
634 | Job *j; | |
635 | const char *k; | |
636 | size_t size; | |
637 | ||
638 | if (!(reply = dbus_message_new_method_return(message))) | |
639 | goto oom; | |
640 | ||
641 | /* We roll our own introspection code here, instead of | |
642 | * relying on bus_default_message_handler() because we | |
643 | * need to generate our introspection string | |
644 | * dynamically. */ | |
645 | ||
646 | if (!(f = open_memstream(&introspection, &size))) | |
647 | goto oom; | |
648 | ||
649 | fputs(INTROSPECTION_BEGIN, f); | |
650 | ||
651 | HASHMAP_FOREACH_KEY(u, k, m->units, i) { | |
652 | char *p; | |
653 | ||
9e2f7c11 | 654 | if (k != u->meta.id) |
ea430986 LP |
655 | continue; |
656 | ||
657 | if (!(p = bus_path_escape(k))) { | |
658 | fclose(f); | |
659 | free(introspection); | |
660 | goto oom; | |
661 | } | |
662 | ||
663 | fprintf(f, "<node name=\"unit/%s\"/>", p); | |
664 | free(p); | |
665 | } | |
666 | ||
667 | HASHMAP_FOREACH(j, m->jobs, i) | |
668 | fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id); | |
669 | ||
670 | fputs(INTROSPECTION_END, f); | |
671 | ||
672 | if (ferror(f)) { | |
673 | fclose(f); | |
674 | free(introspection); | |
675 | goto oom; | |
676 | } | |
677 | ||
678 | fclose(f); | |
679 | ||
680 | if (!introspection) | |
681 | goto oom; | |
682 | ||
683 | if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) { | |
684 | free(introspection); | |
685 | goto oom; | |
686 | } | |
687 | ||
688 | free(introspection); | |
689 | ||
a16e1123 LP |
690 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) { |
691 | ||
692 | assert(!m->queued_message); | |
693 | ||
694 | /* Instead of sending the reply back right away, we | |
695 | * just remember that we need to and then send it | |
696 | * after the reload is finished. That way the caller | |
697 | * knows when the reload finished. */ | |
698 | ||
699 | if (!(m->queued_message = dbus_message_new_method_return(message))) | |
700 | goto oom; | |
701 | ||
7b97f477 | 702 | m->queued_message_connection = connection; |
a16e1123 LP |
703 | m->exit_code = MANAGER_RELOAD; |
704 | ||
705 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) { | |
706 | ||
707 | if (!(reply = dbus_message_new_method_return(message))) | |
708 | goto oom; | |
709 | ||
710 | m->exit_code = MANAGER_REEXECUTE; | |
711 | ||
712 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) { | |
713 | ||
398ef8ba LP |
714 | if (m->running_as == MANAGER_SYSTEM) { |
715 | dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for session managers."); | |
716 | return bus_send_error_reply(m, connection, message, &error, -ENOTSUP); | |
717 | } | |
a16e1123 LP |
718 | |
719 | if (!(reply = dbus_message_new_method_return(message))) | |
720 | goto oom; | |
721 | ||
722 | m->exit_code = MANAGER_EXIT; | |
723 | ||
1137a57c LP |
724 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) { |
725 | char **l = NULL, **e = NULL; | |
726 | ||
727 | if ((r = bus_parse_strv(message, &l)) < 0) { | |
728 | if (r == -ENOMEM) | |
729 | goto oom; | |
730 | ||
5e8d1c9a | 731 | return bus_send_error_reply(m, connection, message, NULL, r); |
1137a57c LP |
732 | } |
733 | ||
5b6319dc | 734 | e = strv_env_merge(2, m->environment, l); |
1137a57c LP |
735 | strv_free(l); |
736 | ||
737 | if (!e) | |
738 | goto oom; | |
739 | ||
740 | if (!(reply = dbus_message_new_method_return(message))) { | |
741 | strv_free(e); | |
742 | goto oom; | |
743 | } | |
744 | ||
745 | strv_free(m->environment); | |
746 | m->environment = e; | |
747 | ||
748 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) { | |
749 | char **l = NULL, **e = NULL; | |
750 | ||
751 | if ((r = bus_parse_strv(message, &l)) < 0) { | |
752 | if (r == -ENOMEM) | |
753 | goto oom; | |
754 | ||
5e8d1c9a | 755 | return bus_send_error_reply(m, connection, message, NULL, r); |
1137a57c LP |
756 | } |
757 | ||
5b6319dc | 758 | e = strv_env_delete(m->environment, 1, l); |
1137a57c LP |
759 | strv_free(l); |
760 | ||
761 | if (!e) | |
762 | goto oom; | |
763 | ||
764 | if (!(reply = dbus_message_new_method_return(message))) | |
765 | goto oom; | |
766 | ||
767 | strv_free(m->environment); | |
768 | m->environment = e; | |
769 | ||
ea430986 | 770 | } else |
5e8d1c9a | 771 | return bus_default_message_handler(m, connection, message, NULL, properties); |
ea430986 | 772 | |
c87eba54 LP |
773 | if (job_type != _JOB_TYPE_INVALID) { |
774 | const char *name, *smode; | |
775 | JobMode mode; | |
776 | Job *j; | |
777 | Unit *u; | |
778 | ||
779 | if (!dbus_message_get_args( | |
780 | message, | |
781 | &error, | |
782 | DBUS_TYPE_STRING, &name, | |
783 | DBUS_TYPE_STRING, &smode, | |
784 | DBUS_TYPE_INVALID)) | |
5e8d1c9a | 785 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); |
c87eba54 | 786 | |
398ef8ba LP |
787 | if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) { |
788 | dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode); | |
789 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); | |
790 | } | |
c87eba54 | 791 | |
398ef8ba LP |
792 | if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0) |
793 | return bus_send_error_reply(m, connection, message, &error, r); | |
c87eba54 | 794 | |
6f28c033 LP |
795 | if (reload_if_possible && unit_can_reload(u)) { |
796 | if (job_type == JOB_RESTART) | |
797 | job_type = JOB_RELOAD_OR_START; | |
798 | else if (job_type == JOB_TRY_RESTART) | |
799 | job_type = JOB_RELOAD; | |
800 | } | |
801 | ||
398ef8ba LP |
802 | if (job_type == JOB_START && u->meta.only_by_dependency) { |
803 | dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Unit may be activated by dependency only."); | |
804 | return bus_send_error_reply(m, connection, message, &error, -EPERM); | |
805 | } | |
c87eba54 | 806 | |
398ef8ba LP |
807 | if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0) |
808 | return bus_send_error_reply(m, connection, message, &error, r); | |
c87eba54 | 809 | |
894ba510 | 810 | if (!(j->bus_client = strdup(message_get_sender_with_fallback(message)))) |
a567261a LP |
811 | goto oom; |
812 | ||
813 | j->bus = connection; | |
814 | ||
c87eba54 LP |
815 | if (!(reply = dbus_message_new_method_return(message))) |
816 | goto oom; | |
817 | ||
818 | if (!(path = job_dbus_path(j))) | |
819 | goto oom; | |
820 | ||
821 | if (!dbus_message_append_args( | |
822 | reply, | |
823 | DBUS_TYPE_OBJECT_PATH, &path, | |
824 | DBUS_TYPE_INVALID)) | |
825 | goto oom; | |
826 | } | |
827 | ||
ea430986 LP |
828 | free(path); |
829 | ||
830 | if (reply) { | |
831 | if (!dbus_connection_send(connection, reply, NULL)) | |
832 | goto oom; | |
833 | ||
834 | dbus_message_unref(reply); | |
835 | } | |
836 | ||
837 | return DBUS_HANDLER_RESULT_HANDLED; | |
838 | ||
839 | oom: | |
840 | free(path); | |
841 | ||
842 | if (reply) | |
843 | dbus_message_unref(reply); | |
844 | ||
845 | dbus_error_free(&error); | |
846 | ||
847 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
848 | } | |
849 | ||
850 | const DBusObjectPathVTable bus_manager_vtable = { | |
851 | .message_function = bus_manager_message_handler | |
852 | }; |