]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
ea430986 | 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 | 22 | #include <errno.h> |
871c44a7 | 23 | #include <unistd.h> |
ea430986 LP |
24 | |
25 | #include "dbus.h" | |
26 | #include "log.h" | |
4139c1b2 | 27 | #include "dbus-manager.h" |
1137a57c | 28 | #include "strv.h" |
398ef8ba | 29 | #include "bus-errors.h" |
09bde77f | 30 | #include "build.h" |
ea430986 | 31 | |
07459bb6 FF |
32 | #define BUS_MANAGER_INTERFACE_BEGIN \ |
33 | " <interface name=\"org.freedesktop.systemd1.Manager\">\n" | |
34 | ||
35 | #define BUS_MANAGER_INTERFACE_METHODS \ | |
4288f619 LP |
36 | " <method name=\"GetUnit\">\n" \ |
37 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
38 | " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \ | |
39 | " </method>\n" \ | |
598b557b | 40 | " <method name=\"GetUnitByPID\">\n" \ |
07459bb6 | 41 | " <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n" \ |
598b557b LP |
42 | " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \ |
43 | " </method>\n" \ | |
4288f619 LP |
44 | " <method name=\"LoadUnit\">\n" \ |
45 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
46 | " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \ | |
47 | " </method>\n" \ | |
c87eba54 LP |
48 | " <method name=\"StartUnit\">\n" \ |
49 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
50 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ | |
51 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \ | |
52 | " </method>\n" \ | |
90bb85e1 LP |
53 | " <method name=\"StartUnitReplace\">\n" \ |
54 | " <arg name=\"old_unit\" type=\"s\" direction=\"in\"/>\n" \ | |
55 | " <arg name=\"new_unit\" type=\"s\" direction=\"in\"/>\n" \ | |
56 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ | |
57 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \ | |
58 | " </method>\n" \ | |
c87eba54 LP |
59 | " <method name=\"StopUnit\">\n" \ |
60 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
61 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ | |
62 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \ | |
63 | " </method>\n" \ | |
64 | " <method name=\"ReloadUnit\">\n" \ | |
65 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
66 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ | |
67 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \ | |
68 | " </method>\n" \ | |
69 | " <method name=\"RestartUnit\">\n" \ | |
70 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
71 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ | |
72 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \ | |
73 | " </method>\n" \ | |
9a1ac7b9 LP |
74 | " <method name=\"TryRestartUnit\">\n" \ |
75 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
76 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ | |
77 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \ | |
78 | " </method>\n" \ | |
6f28c033 LP |
79 | " <method name=\"ReloadOrRestartUnit\">\n" \ |
80 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
81 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ | |
82 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \ | |
83 | " </method>\n" \ | |
84 | " <method name=\"ReloadOrTryRestartUnit\">\n" \ | |
85 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
86 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ | |
87 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \ | |
88 | " </method>\n" \ | |
8a0867d6 LP |
89 | " <method name=\"KillUnit\">\n" \ |
90 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
91 | " <arg name=\"who\" type=\"s\" direction=\"in\"/>\n" \ | |
92 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \ | |
93 | " <arg name=\"signal\" type=\"i\" direction=\"in\"/>\n" \ | |
94 | " </method>\n" \ | |
fdf20a31 | 95 | " <method name=\"ResetFailedUnit\">\n" \ |
5632e374 LP |
96 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ |
97 | " </method>\n" \ | |
4288f619 LP |
98 | " <method name=\"GetJob\">\n" \ |
99 | " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \ | |
100 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \ | |
101 | " </method>\n" \ | |
102 | " <method name=\"ClearJobs\"/>\n" \ | |
fdf20a31 | 103 | " <method name=\"ResetFailed\"/>\n" \ |
4288f619 | 104 | " <method name=\"ListUnits\">\n" \ |
8fe914ec | 105 | " <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \ |
4288f619 LP |
106 | " </method>\n" \ |
107 | " <method name=\"ListJobs\">\n" \ | |
108 | " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \ | |
109 | " </method>\n" \ | |
110 | " <method name=\"Subscribe\"/>\n" \ | |
111 | " <method name=\"Unsubscribe\"/>\n" \ | |
112 | " <method name=\"Dump\"/>\n" \ | |
113 | " <method name=\"CreateSnapshot\">\n" \ | |
114 | " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \ | |
5948ee7c | 115 | " <arg name=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \ |
4288f619 LP |
116 | " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \ |
117 | " </method>\n" \ | |
118 | " <method name=\"Reload\"/>\n" \ | |
119 | " <method name=\"Reexecute\"/>\n" \ | |
120 | " <method name=\"Exit\"/>\n" \ | |
6652a2b9 LP |
121 | " <method name=\"Reboot\"/>\n" \ |
122 | " <method name=\"PowerOff\"/>\n" \ | |
123 | " <method name=\"Halt\"/>\n" \ | |
124 | " <method name=\"KExec\"/>\n" \ | |
4288f619 LP |
125 | " <method name=\"SetEnvironment\">\n" \ |
126 | " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \ | |
127 | " </method>\n" \ | |
128 | " <method name=\"UnsetEnvironment\">\n" \ | |
129 | " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \ | |
07459bb6 FF |
130 | " </method>\n" |
131 | ||
132 | #define BUS_MANAGER_INTERFACE_SIGNALS \ | |
4288f619 LP |
133 | " <signal name=\"UnitNew\">\n" \ |
134 | " <arg name=\"id\" type=\"s\"/>\n" \ | |
135 | " <arg name=\"unit\" type=\"o\"/>\n" \ | |
136 | " </signal>\n" \ | |
137 | " <signal name=\"UnitRemoved\">\n" \ | |
138 | " <arg name=\"id\" type=\"s\"/>\n" \ | |
139 | " <arg name=\"unit\" type=\"o\"/>\n" \ | |
140 | " </signal>\n" \ | |
141 | " <signal name=\"JobNew\">\n" \ | |
142 | " <arg name=\"id\" type=\"u\"/>\n" \ | |
143 | " <arg name=\"job\" type=\"o\"/>\n" \ | |
144 | " </signal>\n" \ | |
145 | " <signal name=\"JobRemoved\">\n" \ | |
146 | " <arg name=\"id\" type=\"u\"/>\n" \ | |
147 | " <arg name=\"job\" type=\"o\"/>\n" \ | |
5d44db4a | 148 | " <arg name=\"result\" type=\"s\"/>\n" \ |
07459bb6 FF |
149 | " </signal>" |
150 | ||
07459bb6 | 151 | #define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \ |
4288f619 | 152 | " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \ |
09bde77f | 153 | " <property name=\"Distribution\" type=\"s\" access=\"read\"/>\n" \ |
c5d34390 LP |
154 | " <property name=\"Features\" type=\"s\" access=\"read\"/>\n" \ |
155 | " <property name=\"Tainted\" type=\"s\" access=\"read\"/>\n" \ | |
4288f619 | 156 | " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \ |
e9ddabc2 | 157 | " <property name=\"InitRDTimestamp\" type=\"t\" access=\"read\"/>\n" \ |
0442c13b | 158 | " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \ |
e9ddabc2 | 159 | " <property name=\"FinishTimestamp\" type=\"t\" access=\"read\"/>\n" \ |
c826cda4 AB |
160 | " <property name=\"LogLevel\" type=\"s\" access=\"readwrite\"/>\n" \ |
161 | " <property name=\"LogTarget\" type=\"s\" access=\"readwrite\"/>\n" \ | |
4288f619 LP |
162 | " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \ |
163 | " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \ | |
e409f875 | 164 | " <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \ |
76bf48b7 | 165 | " <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \ |
fa70128d | 166 | " <property name=\"Progress\" type=\"d\" access=\"read\"/>\n" \ |
4288f619 | 167 | " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \ |
f295f5c0 | 168 | " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \ |
9e58ff9c | 169 | " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \ |
f295f5c0 | 170 | " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \ |
f295f5c0 | 171 | " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \ |
c6c18be3 | 172 | " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \ |
d3689161 | 173 | " <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \ |
06d4c99a | 174 | " <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n" \ |
0a494f1f LP |
175 | " <property name=\"DefaultControllers\" type=\"as\" access=\"read\"/>\n" \ |
176 | " <property name=\"DefaultStandardOutput\" type=\"s\" access=\"read\"/>\n" \ | |
177 | " <property name=\"DefaultStandardError\" type=\"s\" access=\"read\"/>\n" | |
07459bb6 FF |
178 | |
179 | #ifdef HAVE_SYSV_COMPAT | |
180 | #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \ | |
181 | " <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \ | |
182 | " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \ | |
183 | " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n" | |
184 | #else | |
185 | #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV | |
186 | #endif | |
187 | ||
188 | #define BUS_MANAGER_INTERFACE_END \ | |
4288f619 LP |
189 | " </interface>\n" |
190 | ||
07459bb6 FF |
191 | #define BUS_MANAGER_INTERFACE \ |
192 | BUS_MANAGER_INTERFACE_BEGIN \ | |
193 | BUS_MANAGER_INTERFACE_METHODS \ | |
194 | BUS_MANAGER_INTERFACE_SIGNALS \ | |
195 | BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \ | |
196 | BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \ | |
197 | BUS_MANAGER_INTERFACE_END | |
198 | ||
ea430986 LP |
199 | #define INTROSPECTION_BEGIN \ |
200 | DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \ | |
4288f619 LP |
201 | "<node>\n" \ |
202 | BUS_MANAGER_INTERFACE \ | |
ea430986 | 203 | BUS_PROPERTIES_INTERFACE \ |
c4e2ceae | 204 | BUS_PEER_INTERFACE \ |
ea430986 LP |
205 | BUS_INTROSPECTABLE_INTERFACE |
206 | ||
207 | #define INTROSPECTION_END \ | |
4288f619 LP |
208 | "</node>\n" |
209 | ||
05feefe0 LP |
210 | #define INTERFACES_LIST \ |
211 | BUS_GENERIC_INTERFACES_LIST \ | |
212 | "org.freedesktop.systemd1.Manager\0" | |
213 | ||
9a60da28 | 214 | const char bus_manager_interface[] _introspect_("Manager") = BUS_MANAGER_INTERFACE; |
ea430986 | 215 | |
4139c1b2 | 216 | static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs); |
0a494f1f | 217 | static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_exec_output, exec_output, ExecOutput); |
1adf1049 | 218 | |
c5d34390 LP |
219 | static int bus_manager_append_tainted(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
220 | const char *t; | |
20c03b7b | 221 | char buf[LINE_MAX] = "", *e = buf, *p = NULL; |
c5d34390 LP |
222 | |
223 | assert(m); | |
224 | assert(i); | |
225 | assert(property); | |
226 | ||
72bc8d00 | 227 | if (m->taint_usr) |
871c44a7 | 228 | e = stpcpy(e, "usr-separate-fs "); |
c5d34390 | 229 | |
871c44a7 LP |
230 | if (readlink_malloc("/etc/mtab", &p) < 0) |
231 | e = stpcpy(e, "etc-mtab-not-symlink "); | |
232 | else | |
c5d34390 LP |
233 | free(p); |
234 | ||
871c44a7 LP |
235 | if (access("/proc/cgroups", F_OK) < 0) |
236 | e = stpcpy(e, "cgroups-missing "); | |
237 | ||
238 | t = strstrip(buf); | |
c5d34390 LP |
239 | |
240 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t)) | |
241 | return -ENOMEM; | |
242 | ||
243 | return 0; | |
244 | } | |
245 | ||
1adf1049 LP |
246 | static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
247 | const char *t; | |
248 | ||
249 | assert(m); | |
250 | assert(i); | |
251 | assert(property); | |
252 | ||
253 | t = log_target_to_string(log_get_target()); | |
254 | ||
255 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t)) | |
256 | return -ENOMEM; | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
c826cda4 AB |
261 | static int bus_manager_set_log_target(Manager *m, DBusMessageIter *i, const char *property) { |
262 | const char *t; | |
263 | ||
264 | assert(m); | |
265 | assert(i); | |
266 | assert(property); | |
267 | ||
268 | dbus_message_iter_get_basic(i, &t); | |
269 | ||
270 | return log_set_target_from_string(t); | |
271 | } | |
272 | ||
1adf1049 LP |
273 | static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
274 | const char *t; | |
275 | ||
276 | assert(m); | |
277 | assert(i); | |
278 | assert(property); | |
279 | ||
280 | t = log_level_to_string(log_get_max_level()); | |
281 | ||
282 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t)) | |
283 | return -ENOMEM; | |
284 | ||
285 | return 0; | |
286 | } | |
287 | ||
c826cda4 AB |
288 | static int bus_manager_set_log_level(Manager *m, DBusMessageIter *i, const char *property) { |
289 | const char *t; | |
290 | ||
291 | assert(m); | |
292 | assert(i); | |
293 | assert(property); | |
294 | ||
295 | dbus_message_iter_get_basic(i, &t); | |
296 | ||
297 | return log_set_max_level_from_string(t); | |
298 | } | |
299 | ||
4f0f902f LP |
300 | static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
301 | uint32_t u; | |
302 | ||
303 | assert(m); | |
304 | assert(i); | |
305 | assert(property); | |
306 | ||
307 | u = hashmap_size(m->units); | |
308 | ||
309 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u)) | |
310 | return -ENOMEM; | |
311 | ||
312 | return 0; | |
313 | } | |
314 | ||
315 | static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
316 | uint32_t u; | |
317 | ||
318 | assert(m); | |
319 | assert(i); | |
320 | assert(property); | |
321 | ||
322 | u = hashmap_size(m->jobs); | |
323 | ||
324 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u)) | |
325 | return -ENOMEM; | |
326 | ||
327 | return 0; | |
328 | } | |
329 | ||
05d6a3b6 LP |
330 | static int bus_manager_append_progress(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
331 | double d; | |
332 | ||
333 | assert(m); | |
334 | assert(i); | |
335 | assert(property); | |
336 | ||
337 | if (dual_timestamp_is_set(&m->finish_timestamp)) | |
338 | d = 1.0; | |
339 | else | |
340 | d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs); | |
341 | ||
342 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_DOUBLE, &d)) | |
343 | return -ENOMEM; | |
344 | ||
345 | return 0; | |
346 | } | |
347 | ||
894ba510 LP |
348 | static const char *message_get_sender_with_fallback(DBusMessage *m) { |
349 | const char *s; | |
350 | ||
351 | assert(m); | |
352 | ||
353 | if ((s = dbus_message_get_sender(m))) | |
354 | return s; | |
355 | ||
356 | /* When the message came in from a direct connection the | |
357 | * message will have no sender. We fix that here. */ | |
358 | ||
359 | return ":no-sender"; | |
360 | } | |
361 | ||
5e8d1c9a | 362 | static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) { |
ea430986 | 363 | Manager *m = data; |
1adf1049 LP |
364 | |
365 | const BusProperty properties[] = { | |
f295f5c0 | 366 | { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING }, |
09bde77f LP |
367 | { "org.freedesktop.systemd1.Manager", "Distribution", bus_property_append_string, "s", DISTRIBUTION }, |
368 | { "org.freedesktop.systemd1.Manager", "Features", bus_property_append_string, "s", SYSTEMD_FEATURES }, | |
f295f5c0 | 369 | { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as }, |
c5d34390 | 370 | { "org.freedesktop.systemd1.Manager", "Tainted", bus_manager_append_tainted, "s", m }, |
e9ddabc2 | 371 | { "org.freedesktop.systemd1.Manager", "InitRDTimestamp", bus_property_append_uint64, "t", &m->initrd_timestamp.realtime }, |
f295f5c0 | 372 | { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime }, |
b0c918b9 | 373 | { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64, "t", &m->finish_timestamp.realtime }, |
c826cda4 AB |
374 | { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL, bus_manager_set_log_level}, |
375 | { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL, bus_manager_set_log_target}, | |
f295f5c0 LP |
376 | { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL }, |
377 | { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL }, | |
e409f875 | 378 | { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32, "u", &m->n_installed_jobs }, |
76bf48b7 | 379 | { "org.freedesktop.systemd1.Manager", "NFailedJobs", bus_property_append_uint32, "u", &m->n_failed_jobs }, |
05d6a3b6 | 380 | { "org.freedesktop.systemd1.Manager", "Progress", bus_manager_append_progress, "d", NULL }, |
f295f5c0 LP |
381 | { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment }, |
382 | { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn }, | |
9e58ff9c | 383 | { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status }, |
f295f5c0 | 384 | { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path }, |
f295f5c0 | 385 | { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket }, |
c6c18be3 | 386 | { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy }, |
d3689161 | 387 | { "org.freedesktop.systemd1.Manager", "MountAuto", bus_property_append_bool, "b", &m->mount_auto }, |
06d4c99a LP |
388 | { "org.freedesktop.systemd1.Manager", "SwapAuto", bus_property_append_bool, "b", &m->swap_auto }, |
389 | { "org.freedesktop.systemd1.Manager", "DefaultControllers", bus_property_append_strv, "as", m->default_controllers }, | |
0a494f1f LP |
390 | { "org.freedesktop.systemd1.Manager", "DefaultStandardOutput", bus_manager_append_exec_output, "s", &m->default_std_output }, |
391 | { "org.freedesktop.systemd1.Manager", "DefaultStandardError", bus_manager_append_exec_output, "s", &m->default_std_error }, | |
07459bb6 FF |
392 | #ifdef HAVE_SYSV_COMPAT |
393 | { "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool, "b", &m->sysv_console }, | |
394 | { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path }, | |
395 | { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path }, | |
396 | #endif | |
1adf1049 LP |
397 | { NULL, NULL, NULL, NULL, NULL } |
398 | }; | |
399 | ||
400 | int r; | |
ea430986 LP |
401 | DBusError error; |
402 | DBusMessage *reply = NULL; | |
403 | char * path = NULL; | |
c87eba54 | 404 | JobType job_type = _JOB_TYPE_INVALID; |
6f28c033 | 405 | bool reload_if_possible = false; |
ea430986 LP |
406 | |
407 | assert(connection); | |
408 | assert(message); | |
409 | assert(m); | |
410 | ||
411 | dbus_error_init(&error); | |
412 | ||
4139c1b2 | 413 | if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) { |
ea430986 LP |
414 | const char *name; |
415 | Unit *u; | |
416 | ||
417 | if (!dbus_message_get_args( | |
418 | message, | |
419 | &error, | |
420 | DBUS_TYPE_STRING, &name, | |
421 | DBUS_TYPE_INVALID)) | |
5e8d1c9a | 422 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); |
ea430986 | 423 | |
398ef8ba LP |
424 | if (!(u = manager_get_unit(m, name))) { |
425 | dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name); | |
426 | return bus_send_error_reply(m, connection, message, &error, -ENOENT); | |
427 | } | |
ea430986 LP |
428 | |
429 | if (!(reply = dbus_message_new_method_return(message))) | |
430 | goto oom; | |
431 | ||
432 | if (!(path = unit_dbus_path(u))) | |
433 | goto oom; | |
434 | ||
435 | if (!dbus_message_append_args( | |
436 | reply, | |
437 | DBUS_TYPE_OBJECT_PATH, &path, | |
438 | DBUS_TYPE_INVALID)) | |
439 | goto oom; | |
598b557b | 440 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) { |
598b557b LP |
441 | Unit *u; |
442 | uint32_t pid; | |
443 | ||
444 | if (!dbus_message_get_args( | |
445 | message, | |
446 | &error, | |
447 | DBUS_TYPE_UINT32, &pid, | |
448 | DBUS_TYPE_INVALID)) | |
449 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); | |
450 | ||
451 | if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) { | |
452 | dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid); | |
453 | return bus_send_error_reply(m, connection, message, &error, -ENOENT); | |
454 | } | |
455 | ||
456 | if (!(reply = dbus_message_new_method_return(message))) | |
457 | goto oom; | |
458 | ||
459 | if (!(path = unit_dbus_path(u))) | |
460 | goto oom; | |
ea430986 | 461 | |
598b557b LP |
462 | if (!dbus_message_append_args( |
463 | reply, | |
464 | DBUS_TYPE_OBJECT_PATH, &path, | |
465 | DBUS_TYPE_INVALID)) | |
466 | goto oom; | |
4139c1b2 | 467 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) { |
ea430986 LP |
468 | const char *name; |
469 | Unit *u; | |
470 | ||
471 | if (!dbus_message_get_args( | |
472 | message, | |
473 | &error, | |
474 | DBUS_TYPE_STRING, &name, | |
475 | DBUS_TYPE_INVALID)) | |
5e8d1c9a | 476 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); |
ea430986 | 477 | |
398ef8ba LP |
478 | if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0) |
479 | return bus_send_error_reply(m, connection, message, &error, r); | |
ea430986 LP |
480 | |
481 | if (!(reply = dbus_message_new_method_return(message))) | |
482 | goto oom; | |
483 | ||
484 | if (!(path = unit_dbus_path(u))) | |
485 | goto oom; | |
486 | ||
487 | if (!dbus_message_append_args( | |
488 | reply, | |
489 | DBUS_TYPE_OBJECT_PATH, &path, | |
490 | DBUS_TYPE_INVALID)) | |
491 | goto oom; | |
492 | ||
c87eba54 LP |
493 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit")) |
494 | job_type = JOB_START; | |
90bb85e1 LP |
495 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace")) |
496 | job_type = JOB_START; | |
c87eba54 LP |
497 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit")) |
498 | job_type = JOB_STOP; | |
499 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit")) | |
500 | job_type = JOB_RELOAD; | |
501 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit")) | |
502 | job_type = JOB_RESTART; | |
9a1ac7b9 LP |
503 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit")) |
504 | job_type = JOB_TRY_RESTART; | |
6f28c033 LP |
505 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) { |
506 | reload_if_possible = true; | |
507 | job_type = JOB_RESTART; | |
508 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) { | |
509 | reload_if_possible = true; | |
510 | job_type = JOB_TRY_RESTART; | |
8a0867d6 LP |
511 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KillUnit")) { |
512 | const char *name, *swho, *smode; | |
513 | int32_t signo; | |
514 | Unit *u; | |
515 | KillMode mode; | |
516 | KillWho who; | |
517 | ||
518 | if (!dbus_message_get_args( | |
519 | message, | |
520 | &error, | |
521 | DBUS_TYPE_STRING, &name, | |
522 | DBUS_TYPE_STRING, &swho, | |
523 | DBUS_TYPE_STRING, &smode, | |
524 | DBUS_TYPE_INT32, &signo, | |
525 | DBUS_TYPE_INVALID)) | |
526 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); | |
527 | ||
528 | if ((mode = kill_mode_from_string(smode)) < 0 || | |
529 | (who = kill_who_from_string(swho)) < 0 || | |
530 | signo <= 0 || | |
531 | signo >= _NSIG) | |
532 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); | |
533 | ||
534 | if (!(u = manager_get_unit(m, name))) { | |
535 | dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name); | |
536 | return bus_send_error_reply(m, connection, message, &error, -ENOENT); | |
537 | } | |
538 | ||
539 | if ((r = unit_kill(u, who, mode, signo, &error)) < 0) | |
540 | return bus_send_error_reply(m, connection, message, &error, r); | |
541 | ||
542 | if (!(reply = dbus_message_new_method_return(message))) | |
543 | goto oom; | |
544 | ||
6f28c033 | 545 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) { |
ea430986 LP |
546 | uint32_t id; |
547 | Job *j; | |
548 | ||
549 | if (!dbus_message_get_args( | |
550 | message, | |
551 | &error, | |
552 | DBUS_TYPE_UINT32, &id, | |
553 | DBUS_TYPE_INVALID)) | |
5e8d1c9a | 554 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); |
ea430986 | 555 | |
398ef8ba LP |
556 | if (!(j = manager_get_job(m, id))) { |
557 | dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id); | |
558 | return bus_send_error_reply(m, connection, message, &error, -ENOENT); | |
559 | } | |
ea430986 LP |
560 | |
561 | if (!(reply = dbus_message_new_method_return(message))) | |
562 | goto oom; | |
563 | ||
564 | if (!(path = job_dbus_path(j))) | |
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; | |
572 | ||
4139c1b2 | 573 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) { |
ea430986 LP |
574 | |
575 | manager_clear_jobs(m); | |
576 | ||
577 | if (!(reply = dbus_message_new_method_return(message))) | |
578 | goto oom; | |
579 | ||
fdf20a31 | 580 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) { |
5632e374 | 581 | |
fdf20a31 | 582 | manager_reset_failed(m); |
5632e374 LP |
583 | |
584 | if (!(reply = dbus_message_new_method_return(message))) | |
585 | goto oom; | |
586 | ||
fdf20a31 | 587 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) { |
5632e374 LP |
588 | const char *name; |
589 | Unit *u; | |
590 | ||
591 | if (!dbus_message_get_args( | |
592 | message, | |
593 | &error, | |
594 | DBUS_TYPE_STRING, &name, | |
595 | DBUS_TYPE_INVALID)) | |
596 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); | |
597 | ||
598 | if (!(u = manager_get_unit(m, name))) { | |
599 | dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name); | |
600 | return bus_send_error_reply(m, connection, message, &error, -ENOENT); | |
601 | } | |
602 | ||
fdf20a31 | 603 | unit_reset_failed(u); |
5632e374 LP |
604 | |
605 | if (!(reply = dbus_message_new_method_return(message))) | |
606 | goto oom; | |
607 | ||
4139c1b2 | 608 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) { |
ea430986 LP |
609 | DBusMessageIter iter, sub; |
610 | Iterator i; | |
611 | Unit *u; | |
612 | const char *k; | |
613 | ||
614 | if (!(reply = dbus_message_new_method_return(message))) | |
615 | goto oom; | |
616 | ||
617 | dbus_message_iter_init_append(reply, &iter); | |
618 | ||
8fe914ec | 619 | if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub)) |
ea430986 LP |
620 | goto oom; |
621 | ||
622 | HASHMAP_FOREACH_KEY(u, k, m->units, i) { | |
47be870b | 623 | char *u_path, *j_path; |
8fe914ec | 624 | const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following; |
ea430986 LP |
625 | DBusMessageIter sub2; |
626 | uint32_t job_id; | |
a7f241db | 627 | Unit *f; |
ea430986 | 628 | |
9e2f7c11 | 629 | if (k != u->meta.id) |
ea430986 LP |
630 | continue; |
631 | ||
632 | if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2)) | |
633 | goto oom; | |
634 | ||
635 | description = unit_description(u); | |
636 | load_state = unit_load_state_to_string(u->meta.load_state); | |
637 | active_state = unit_active_state_to_string(unit_active_state(u)); | |
10a94420 | 638 | sub_state = unit_sub_state_to_string(u); |
a7f241db LP |
639 | |
640 | f = unit_following(u); | |
641 | following = f ? f->meta.id : ""; | |
ea430986 | 642 | |
47be870b | 643 | if (!(u_path = unit_dbus_path(u))) |
ea430986 LP |
644 | goto oom; |
645 | ||
646 | if (u->meta.job) { | |
647 | job_id = (uint32_t) u->meta.job->id; | |
648 | ||
47be870b LP |
649 | if (!(j_path = job_dbus_path(u->meta.job))) { |
650 | free(u_path); | |
ea430986 LP |
651 | goto oom; |
652 | } | |
653 | ||
c87eba54 | 654 | sjob_type = job_type_to_string(u->meta.job->type); |
ea430986 LP |
655 | } else { |
656 | job_id = 0; | |
47be870b | 657 | j_path = u_path; |
c87eba54 | 658 | sjob_type = ""; |
ea430986 LP |
659 | } |
660 | ||
9e2f7c11 | 661 | if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) || |
ea430986 LP |
662 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) || |
663 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) || | |
664 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) || | |
10a94420 | 665 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) || |
8fe914ec | 666 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) || |
47be870b | 667 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) || |
ea430986 | 668 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) || |
c87eba54 | 669 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) || |
47be870b LP |
670 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) { |
671 | free(u_path); | |
ea430986 | 672 | if (u->meta.job) |
47be870b | 673 | free(j_path); |
ea430986 LP |
674 | goto oom; |
675 | } | |
676 | ||
47be870b | 677 | free(u_path); |
ea430986 | 678 | if (u->meta.job) |
47be870b | 679 | free(j_path); |
ea430986 LP |
680 | |
681 | if (!dbus_message_iter_close_container(&sub, &sub2)) | |
682 | goto oom; | |
683 | } | |
684 | ||
685 | if (!dbus_message_iter_close_container(&iter, &sub)) | |
686 | goto oom; | |
687 | ||
4139c1b2 | 688 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) { |
ea430986 LP |
689 | DBusMessageIter iter, sub; |
690 | Iterator i; | |
691 | Job *j; | |
692 | ||
693 | if (!(reply = dbus_message_new_method_return(message))) | |
694 | goto oom; | |
695 | ||
696 | dbus_message_iter_init_append(reply, &iter); | |
697 | ||
698 | if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub)) | |
699 | goto oom; | |
700 | ||
701 | HASHMAP_FOREACH(j, m->jobs, i) { | |
47be870b | 702 | char *u_path, *j_path; |
9e2f7c11 | 703 | const char *state, *type; |
ea430986 LP |
704 | uint32_t id; |
705 | DBusMessageIter sub2; | |
706 | ||
707 | if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2)) | |
708 | goto oom; | |
709 | ||
710 | id = (uint32_t) j->id; | |
ea430986 | 711 | state = job_state_to_string(j->state); |
2b53c70b | 712 | type = job_type_to_string(j->type); |
ea430986 | 713 | |
47be870b | 714 | if (!(j_path = job_dbus_path(j))) |
ea430986 LP |
715 | goto oom; |
716 | ||
47be870b LP |
717 | if (!(u_path = unit_dbus_path(j->unit))) { |
718 | free(j_path); | |
ea430986 LP |
719 | goto oom; |
720 | } | |
721 | ||
722 | if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) || | |
9e2f7c11 | 723 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) || |
ea430986 LP |
724 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) || |
725 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) || | |
47be870b LP |
726 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) || |
727 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) { | |
728 | free(j_path); | |
729 | free(u_path); | |
ea430986 LP |
730 | goto oom; |
731 | } | |
732 | ||
47be870b LP |
733 | free(j_path); |
734 | free(u_path); | |
ea430986 LP |
735 | |
736 | if (!dbus_message_iter_close_container(&sub, &sub2)) | |
737 | goto oom; | |
738 | } | |
739 | ||
740 | if (!dbus_message_iter_close_container(&iter, &sub)) | |
741 | goto oom; | |
742 | ||
4139c1b2 | 743 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) { |
c1e1601e | 744 | char *client; |
a567261a LP |
745 | Set *s; |
746 | ||
747 | if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) { | |
748 | if (!(s = set_new(string_hash_func, string_compare_func))) | |
749 | goto oom; | |
750 | ||
751 | if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) { | |
752 | set_free(s); | |
753 | goto oom; | |
754 | } | |
755 | } | |
c1e1601e | 756 | |
894ba510 | 757 | if (!(client = strdup(message_get_sender_with_fallback(message)))) |
c1e1601e LP |
758 | goto oom; |
759 | ||
a567261a LP |
760 | if ((r = set_put(s, client)) < 0) { |
761 | free(client); | |
5e8d1c9a | 762 | return bus_send_error_reply(m, connection, message, NULL, r); |
a567261a | 763 | } |
c1e1601e LP |
764 | |
765 | if (!(reply = dbus_message_new_method_return(message))) | |
766 | goto oom; | |
767 | ||
4139c1b2 | 768 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) { |
c1e1601e LP |
769 | char *client; |
770 | ||
398ef8ba LP |
771 | if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) { |
772 | dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed."); | |
773 | return bus_send_error_reply(m, connection, message, &error, -ENOENT); | |
774 | } | |
c1e1601e LP |
775 | |
776 | free(client); | |
777 | ||
778 | if (!(reply = dbus_message_new_method_return(message))) | |
779 | goto oom; | |
780 | ||
4139c1b2 | 781 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) { |
b152adec LP |
782 | FILE *f; |
783 | char *dump = NULL; | |
784 | size_t size; | |
785 | ||
786 | if (!(reply = dbus_message_new_method_return(message))) | |
787 | goto oom; | |
788 | ||
789 | if (!(f = open_memstream(&dump, &size))) | |
790 | goto oom; | |
791 | ||
792 | manager_dump_units(m, f, NULL); | |
793 | manager_dump_jobs(m, f, NULL); | |
794 | ||
795 | if (ferror(f)) { | |
796 | fclose(f); | |
797 | free(dump); | |
798 | goto oom; | |
799 | } | |
800 | ||
801 | fclose(f); | |
802 | ||
803 | if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) { | |
804 | free(dump); | |
805 | goto oom; | |
806 | } | |
807 | ||
808 | free(dump); | |
4139c1b2 LP |
809 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) { |
810 | const char *name; | |
811 | dbus_bool_t cleanup; | |
812 | Snapshot *s; | |
813 | ||
814 | if (!dbus_message_get_args( | |
815 | message, | |
816 | &error, | |
817 | DBUS_TYPE_STRING, &name, | |
818 | DBUS_TYPE_BOOLEAN, &cleanup, | |
819 | DBUS_TYPE_INVALID)) | |
5e8d1c9a | 820 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); |
4139c1b2 LP |
821 | |
822 | if (name && name[0] == 0) | |
823 | name = NULL; | |
824 | ||
398ef8ba LP |
825 | if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0) |
826 | return bus_send_error_reply(m, connection, message, &error, r); | |
4139c1b2 LP |
827 | |
828 | if (!(reply = dbus_message_new_method_return(message))) | |
829 | goto oom; | |
830 | ||
831 | if (!(path = unit_dbus_path(UNIT(s)))) | |
832 | goto oom; | |
833 | ||
834 | if (!dbus_message_append_args( | |
835 | reply, | |
836 | DBUS_TYPE_OBJECT_PATH, &path, | |
837 | DBUS_TYPE_INVALID)) | |
838 | goto oom; | |
b152adec | 839 | |
ea430986 LP |
840 | } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) { |
841 | char *introspection = NULL; | |
842 | FILE *f; | |
843 | Iterator i; | |
844 | Unit *u; | |
845 | Job *j; | |
846 | const char *k; | |
847 | size_t size; | |
848 | ||
849 | if (!(reply = dbus_message_new_method_return(message))) | |
850 | goto oom; | |
851 | ||
852 | /* We roll our own introspection code here, instead of | |
853 | * relying on bus_default_message_handler() because we | |
854 | * need to generate our introspection string | |
855 | * dynamically. */ | |
856 | ||
857 | if (!(f = open_memstream(&introspection, &size))) | |
858 | goto oom; | |
859 | ||
860 | fputs(INTROSPECTION_BEGIN, f); | |
861 | ||
862 | HASHMAP_FOREACH_KEY(u, k, m->units, i) { | |
863 | char *p; | |
864 | ||
9e2f7c11 | 865 | if (k != u->meta.id) |
ea430986 LP |
866 | continue; |
867 | ||
868 | if (!(p = bus_path_escape(k))) { | |
869 | fclose(f); | |
870 | free(introspection); | |
871 | goto oom; | |
872 | } | |
873 | ||
874 | fprintf(f, "<node name=\"unit/%s\"/>", p); | |
875 | free(p); | |
876 | } | |
877 | ||
878 | HASHMAP_FOREACH(j, m->jobs, i) | |
879 | fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id); | |
880 | ||
881 | fputs(INTROSPECTION_END, f); | |
882 | ||
883 | if (ferror(f)) { | |
884 | fclose(f); | |
885 | free(introspection); | |
886 | goto oom; | |
887 | } | |
888 | ||
889 | fclose(f); | |
890 | ||
891 | if (!introspection) | |
892 | goto oom; | |
893 | ||
894 | if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) { | |
895 | free(introspection); | |
896 | goto oom; | |
897 | } | |
898 | ||
899 | free(introspection); | |
900 | ||
a16e1123 LP |
901 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) { |
902 | ||
903 | assert(!m->queued_message); | |
904 | ||
905 | /* Instead of sending the reply back right away, we | |
906 | * just remember that we need to and then send it | |
907 | * after the reload is finished. That way the caller | |
908 | * knows when the reload finished. */ | |
909 | ||
910 | if (!(m->queued_message = dbus_message_new_method_return(message))) | |
911 | goto oom; | |
912 | ||
7b97f477 | 913 | m->queued_message_connection = connection; |
a16e1123 LP |
914 | m->exit_code = MANAGER_RELOAD; |
915 | ||
916 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) { | |
917 | ||
918 | if (!(reply = dbus_message_new_method_return(message))) | |
919 | goto oom; | |
920 | ||
921 | m->exit_code = MANAGER_REEXECUTE; | |
922 | ||
923 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) { | |
924 | ||
398ef8ba | 925 | if (m->running_as == MANAGER_SYSTEM) { |
af2d49f7 | 926 | dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for user service managers."); |
398ef8ba LP |
927 | return bus_send_error_reply(m, connection, message, &error, -ENOTSUP); |
928 | } | |
a16e1123 LP |
929 | |
930 | if (!(reply = dbus_message_new_method_return(message))) | |
931 | goto oom; | |
932 | ||
933 | m->exit_code = MANAGER_EXIT; | |
934 | ||
6652a2b9 LP |
935 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reboot")) { |
936 | ||
937 | if (m->running_as != MANAGER_SYSTEM) { | |
938 | dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers."); | |
939 | return bus_send_error_reply(m, connection, message, &error, -ENOTSUP); | |
940 | } | |
941 | ||
942 | if (!(reply = dbus_message_new_method_return(message))) | |
943 | goto oom; | |
944 | ||
945 | m->exit_code = MANAGER_REBOOT; | |
946 | ||
947 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PowerOff")) { | |
948 | ||
949 | if (m->running_as != MANAGER_SYSTEM) { | |
950 | dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers."); | |
951 | return bus_send_error_reply(m, connection, message, &error, -ENOTSUP); | |
952 | } | |
953 | ||
954 | if (!(reply = dbus_message_new_method_return(message))) | |
955 | goto oom; | |
956 | ||
957 | m->exit_code = MANAGER_POWEROFF; | |
958 | ||
959 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Halt")) { | |
960 | ||
961 | if (m->running_as != MANAGER_SYSTEM) { | |
962 | dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers."); | |
963 | return bus_send_error_reply(m, connection, message, &error, -ENOTSUP); | |
964 | } | |
965 | ||
966 | if (!(reply = dbus_message_new_method_return(message))) | |
967 | goto oom; | |
968 | ||
969 | m->exit_code = MANAGER_HALT; | |
970 | ||
971 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KExec")) { | |
972 | ||
973 | if (m->running_as != MANAGER_SYSTEM) { | |
974 | dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system managers."); | |
975 | return bus_send_error_reply(m, connection, message, &error, -ENOTSUP); | |
976 | } | |
977 | ||
978 | if (!(reply = dbus_message_new_method_return(message))) | |
979 | goto oom; | |
980 | ||
981 | m->exit_code = MANAGER_KEXEC; | |
982 | ||
1137a57c LP |
983 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) { |
984 | char **l = NULL, **e = NULL; | |
985 | ||
986 | if ((r = bus_parse_strv(message, &l)) < 0) { | |
987 | if (r == -ENOMEM) | |
988 | goto oom; | |
989 | ||
5e8d1c9a | 990 | return bus_send_error_reply(m, connection, message, NULL, r); |
1137a57c LP |
991 | } |
992 | ||
5b6319dc | 993 | e = strv_env_merge(2, m->environment, l); |
1137a57c LP |
994 | strv_free(l); |
995 | ||
996 | if (!e) | |
997 | goto oom; | |
998 | ||
999 | if (!(reply = dbus_message_new_method_return(message))) { | |
1000 | strv_free(e); | |
1001 | goto oom; | |
1002 | } | |
1003 | ||
1004 | strv_free(m->environment); | |
1005 | m->environment = e; | |
1006 | ||
1007 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) { | |
1008 | char **l = NULL, **e = NULL; | |
1009 | ||
1010 | if ((r = bus_parse_strv(message, &l)) < 0) { | |
1011 | if (r == -ENOMEM) | |
1012 | goto oom; | |
1013 | ||
5e8d1c9a | 1014 | return bus_send_error_reply(m, connection, message, NULL, r); |
1137a57c LP |
1015 | } |
1016 | ||
5b6319dc | 1017 | e = strv_env_delete(m->environment, 1, l); |
1137a57c LP |
1018 | strv_free(l); |
1019 | ||
1020 | if (!e) | |
1021 | goto oom; | |
1022 | ||
da19d5c1 LP |
1023 | if (!(reply = dbus_message_new_method_return(message))) { |
1024 | strv_free(e); | |
1137a57c | 1025 | goto oom; |
da19d5c1 | 1026 | } |
1137a57c LP |
1027 | |
1028 | strv_free(m->environment); | |
1029 | m->environment = e; | |
1030 | ||
ea430986 | 1031 | } else |
05feefe0 | 1032 | return bus_default_message_handler(m, connection, message, NULL, INTERFACES_LIST, properties); |
ea430986 | 1033 | |
c87eba54 | 1034 | if (job_type != _JOB_TYPE_INVALID) { |
90bb85e1 | 1035 | const char *name, *smode, *old_name = NULL; |
c87eba54 LP |
1036 | JobMode mode; |
1037 | Job *j; | |
1038 | Unit *u; | |
90bb85e1 LP |
1039 | bool b; |
1040 | ||
1041 | if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace")) | |
1042 | b = dbus_message_get_args( | |
1043 | message, | |
1044 | &error, | |
1045 | DBUS_TYPE_STRING, &old_name, | |
1046 | DBUS_TYPE_STRING, &name, | |
1047 | DBUS_TYPE_STRING, &smode, | |
1048 | DBUS_TYPE_INVALID); | |
1049 | else | |
1050 | b = dbus_message_get_args( | |
1051 | message, | |
1052 | &error, | |
1053 | DBUS_TYPE_STRING, &name, | |
1054 | DBUS_TYPE_STRING, &smode, | |
1055 | DBUS_TYPE_INVALID); | |
1056 | ||
1057 | if (!b) | |
5e8d1c9a | 1058 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); |
c87eba54 | 1059 | |
90bb85e1 LP |
1060 | if (old_name) |
1061 | if (!(u = manager_get_unit(m, old_name)) || | |
1062 | !u->meta.job || | |
1063 | u->meta.job->type != JOB_START) { | |
1064 | dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name); | |
1065 | return bus_send_error_reply(m, connection, message, &error, -ENOENT); | |
1066 | } | |
1067 | ||
1068 | ||
398ef8ba LP |
1069 | if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) { |
1070 | dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode); | |
1071 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); | |
1072 | } | |
c87eba54 | 1073 | |
398ef8ba LP |
1074 | if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0) |
1075 | return bus_send_error_reply(m, connection, message, &error, r); | |
c87eba54 | 1076 | |
6f28c033 LP |
1077 | if (reload_if_possible && unit_can_reload(u)) { |
1078 | if (job_type == JOB_RESTART) | |
1079 | job_type = JOB_RELOAD_OR_START; | |
1080 | else if (job_type == JOB_TRY_RESTART) | |
1081 | job_type = JOB_RELOAD; | |
1082 | } | |
1083 | ||
b5e9dba8 LP |
1084 | if ((job_type == JOB_START && u->meta.refuse_manual_start) || |
1085 | (job_type == JOB_STOP && u->meta.refuse_manual_stop) || | |
1086 | ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) && | |
1087 | (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) { | |
1088 | dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only."); | |
398ef8ba LP |
1089 | return bus_send_error_reply(m, connection, message, &error, -EPERM); |
1090 | } | |
c87eba54 | 1091 | |
398ef8ba LP |
1092 | if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0) |
1093 | return bus_send_error_reply(m, connection, message, &error, r); | |
c87eba54 | 1094 | |
894ba510 | 1095 | if (!(j->bus_client = strdup(message_get_sender_with_fallback(message)))) |
a567261a LP |
1096 | goto oom; |
1097 | ||
1098 | j->bus = connection; | |
1099 | ||
c87eba54 LP |
1100 | if (!(reply = dbus_message_new_method_return(message))) |
1101 | goto oom; | |
1102 | ||
1103 | if (!(path = job_dbus_path(j))) | |
1104 | goto oom; | |
1105 | ||
1106 | if (!dbus_message_append_args( | |
1107 | reply, | |
1108 | DBUS_TYPE_OBJECT_PATH, &path, | |
1109 | DBUS_TYPE_INVALID)) | |
1110 | goto oom; | |
1111 | } | |
1112 | ||
ea430986 LP |
1113 | if (reply) { |
1114 | if (!dbus_connection_send(connection, reply, NULL)) | |
1115 | goto oom; | |
1116 | ||
1117 | dbus_message_unref(reply); | |
1118 | } | |
1119 | ||
da19d5c1 LP |
1120 | free(path); |
1121 | ||
ea430986 LP |
1122 | return DBUS_HANDLER_RESULT_HANDLED; |
1123 | ||
1124 | oom: | |
1125 | free(path); | |
1126 | ||
1127 | if (reply) | |
1128 | dbus_message_unref(reply); | |
1129 | ||
1130 | dbus_error_free(&error); | |
1131 | ||
1132 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
1133 | } | |
1134 | ||
1135 | const DBusObjectPathVTable bus_manager_vtable = { | |
1136 | .message_function = bus_manager_message_handler | |
1137 | }; |