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