]> git.ipfire.org Git - people/ms/systemd.git/blame - dbus-unit.c
service: treat 0 timeouts as no timeouts
[people/ms/systemd.git] / dbus-unit.c
CommitLineData
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-unit.h"
ea430986 27
4139c1b2
LP
28int bus_unit_append_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
29 char *t;
30 Iterator j;
31 DBusMessageIter sub;
32 Unit *u = data;
33
34 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
35 return -ENOMEM;
36
37 SET_FOREACH(t, u->meta.names, j)
38 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t))
39 return -ENOMEM;
40
41 if (!dbus_message_iter_close_container(i, &sub))
42 return -ENOMEM;
43
44 return 0;
45}
46
47int bus_unit_append_description(Manager *m, DBusMessageIter *i, const char *property, void *data) {
ea430986
LP
48 Unit *u = data;
49 const char *d;
50
51 assert(m);
52 assert(i);
53 assert(property);
54 assert(u);
55
56 d = unit_description(u);
57
58 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
59 return -ENOMEM;
60
61 return 0;
62}
63
6f4706b7 64DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state, unit_load_state, UnitLoadState);
ea430986 65
4139c1b2 66int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
ea430986
LP
67 Unit *u = data;
68 const char *state;
69
70 assert(m);
71 assert(i);
72 assert(property);
73 assert(u);
74
75 state = unit_active_state_to_string(unit_active_state(u));
76
77 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
78 return -ENOMEM;
79
80 return 0;
81}
82
4139c1b2 83int bus_unit_append_sub_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
10a94420
LP
84 Unit *u = data;
85 const char *state;
86
87 assert(m);
88 assert(i);
89 assert(property);
90 assert(u);
91
92 state = unit_sub_state_to_string(u);
93
94 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
95 return -ENOMEM;
96
97 return 0;
98}
99
4139c1b2 100int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data) {
38131695
LP
101 Unit *u = data;
102 dbus_bool_t b;
103
104 assert(m);
105 assert(i);
106 assert(property);
107 assert(u);
108
4139c1b2 109 b = unit_can_start(u);
38131695
LP
110
111 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
112 return -ENOMEM;
113
114 return 0;
115}
116
4139c1b2 117int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
38131695
LP
118 Unit *u = data;
119 dbus_bool_t b;
120
121 assert(m);
122 assert(i);
123 assert(property);
124 assert(u);
125
4139c1b2 126 b = unit_can_reload(u);
38131695
LP
127
128 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
129 return -ENOMEM;
130
131 return 0;
132}
133
4139c1b2 134int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) {
38131695
LP
135 Unit *u = data;
136 DBusMessageIter sub;
137 char *p;
138
139 assert(m);
140 assert(i);
141 assert(property);
142 assert(u);
143
144 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
145 return -ENOMEM;
146
147 if (u->meta.job) {
148
149 if (!(p = job_dbus_path(u->meta.job)))
150 return -ENOMEM;
151
152 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) ||
86ad3bc1 153 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
38131695
LP
154 free(p);
155 return -ENOMEM;
156 }
157 } else {
158 uint32_t id = 0;
159
160 /* No job, so let's fill in some placeholder
161 * data. Since we need to fill in a valid path we
162 * simple point to ourselves. */
163
164 if (!(p = unit_dbus_path(u)))
165 return -ENOMEM;
166
167 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) ||
86ad3bc1 168 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
38131695
LP
169 free(p);
170 return -ENOMEM;
171 }
172 }
173
174 free(p);
175
176 if (!dbus_message_iter_close_container(i, &sub))
177 return -ENOMEM;
178
179 return 0;
180}
181
4139c1b2
LP
182int bus_unit_append_default_cgroup(Manager *m, DBusMessageIter *i, const char *property, void *data) {
183 Unit *u = data;
184 char *t;
185 CGroupBonding *cgb;
186 bool success;
ea430986 187
4139c1b2
LP
188 assert(m);
189 assert(i);
190 assert(property);
191 assert(u);
192
193 if ((cgb = unit_get_default_cgroup(u))) {
194 if (!(t = cgroup_bonding_to_string(cgb)))
195 return -ENOMEM;
196 } else
197 t = (char*) "";
ea430986 198
4139c1b2
LP
199 success = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t);
200
201 if (cgb)
202 free(t);
203
204 return success ? 0 : -ENOMEM;
205}
206
207int bus_unit_append_cgroups(Manager *m, DBusMessageIter *i, const char *property, void *data) {
208 Unit *u = data;
209 CGroupBonding *cgb;
210 DBusMessageIter sub;
211
212 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
213 return -ENOMEM;
214
215 LIST_FOREACH(by_unit, cgb, u->meta.cgroup_bondings) {
216 char *t;
217 bool success;
218
219 if (!(t = cgroup_bonding_to_string(cgb)))
220 return -ENOMEM;
221
222 success = dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t);
223 free(t);
224
225 if (!success)
226 return -ENOMEM;
227 }
228
229 if (!dbus_message_iter_close_container(i, &sub))
230 return -ENOMEM;
231
232 return 0;
233}
234
235DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_kill_mode, kill_mode, KillMode);
236
237static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusMessage *message) {
b548631a
LP
238 DBusMessage *reply = NULL;
239 Manager *m = u->meta.manager;
240 DBusError error;
241 JobType job_type = _JOB_TYPE_INVALID;
242
243 dbus_error_init(&error);
244
245 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start"))
246 job_type = JOB_START;
247 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop"))
248 job_type = JOB_STOP;
249 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload"))
250 job_type = JOB_RELOAD;
251 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart"))
252 job_type = JOB_RESTART;
4139c1b2
LP
253 else if (UNIT_VTABLE(u)->bus_message_handler)
254 return UNIT_VTABLE(u)->bus_message_handler(u, message);
b548631a 255 else
4139c1b2 256 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
b548631a
LP
257
258 if (job_type != _JOB_TYPE_INVALID) {
259 const char *smode;
260 JobMode mode;
261 Job *j;
262 int r;
263 char *path;
264
265 if (!dbus_message_get_args(
266 message,
267 &error,
268 DBUS_TYPE_STRING, &smode,
269 DBUS_TYPE_INVALID))
270 return bus_send_error_reply(m, message, &error, -EINVAL);
271
272 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID)
273 return bus_send_error_reply(m, message, NULL, -EINVAL);
274
275 if ((r = manager_add_job(m, job_type, u, mode, true, &j)) < 0)
276 return bus_send_error_reply(m, message, NULL, r);
277
278 if (!(reply = dbus_message_new_method_return(message)))
279 goto oom;
280
281 if (!(path = job_dbus_path(j)))
282 goto oom;
283
284 if (!dbus_message_append_args(
285 reply,
286 DBUS_TYPE_OBJECT_PATH, &path,
287 DBUS_TYPE_INVALID))
288 goto oom;
289 }
290
291 if (reply) {
f278026d 292 if (!dbus_connection_send(m->api_bus, reply, NULL))
b548631a
LP
293 goto oom;
294
295 dbus_message_unref(reply);
296 }
297
298 return DBUS_HANDLER_RESULT_HANDLED;
299
300oom:
301 if (reply)
302 dbus_message_unref(reply);
303
304 dbus_error_free(&error);
305
306 return DBUS_HANDLER_RESULT_NEED_MEMORY;
ea430986
LP
307}
308
309static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
310 Manager *m = data;
311 Unit *u;
312 int r;
313
314 assert(connection);
315 assert(message);
316 assert(m);
317
318 log_debug("Got D-Bus request: %s.%s() on %s",
319 dbus_message_get_interface(message),
320 dbus_message_get_member(message),
321 dbus_message_get_path(message));
322
323 if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
324
325 if (r == -ENOMEM)
326 return DBUS_HANDLER_RESULT_NEED_MEMORY;
327
328 if (r == -ENOENT)
329 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
330
331 return bus_send_error_reply(m, message, NULL, r);
332 }
333
334 return bus_unit_message_dispatch(u, message);
335}
336
337const DBusObjectPathVTable bus_unit_vtable = {
338 .message_function = bus_unit_message_handler
339};
c1e1601e
LP
340
341void bus_unit_send_change_signal(Unit *u) {
342 char *p = NULL;
343 DBusMessage *m = NULL;
344
345 assert(u);
346 assert(u->meta.in_dbus_queue);
347
348 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
349 u->meta.in_dbus_queue = false;
350
351 if (set_isempty(u->meta.manager->subscribed))
352 return;
353
354 if (!(p = unit_dbus_path(u)))
355 goto oom;
356
357 if (u->meta.sent_dbus_new_signal) {
358 /* Send a change signal */
359
360 if (!(m = dbus_message_new_signal(p, "org.freedesktop.systemd1.Unit", "Changed")))
361 goto oom;
362 } else {
c1e1601e
LP
363 /* Send a new signal */
364
701cc384 365 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
c1e1601e
LP
366 goto oom;
367
c1e1601e 368 if (!dbus_message_append_args(m,
9e2f7c11 369 DBUS_TYPE_STRING, &u->meta.id,
c1e1601e
LP
370 DBUS_TYPE_OBJECT_PATH, &p,
371 DBUS_TYPE_INVALID))
372 goto oom;
373 }
374
f278026d 375 if (!dbus_connection_send(u->meta.manager->api_bus, m, NULL))
c1e1601e
LP
376 goto oom;
377
378 free(p);
379 dbus_message_unref(m);
380
381 u->meta.sent_dbus_new_signal = true;
382
383 return;
384
385oom:
386 free(p);
387
388 if (m)
389 dbus_message_unref(m);
390
391 log_error("Failed to allocate unit change/new signal.");
392}
393
394void bus_unit_send_removed_signal(Unit *u) {
395 char *p = NULL;
396 DBusMessage *m = NULL;
c1e1601e
LP
397
398 assert(u);
399
400 if (set_isempty(u->meta.manager->subscribed) || !u->meta.sent_dbus_new_signal)
401 return;
402
403 if (!(p = unit_dbus_path(u)))
404 goto oom;
405
701cc384 406 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
c1e1601e
LP
407 goto oom;
408
c1e1601e 409 if (!dbus_message_append_args(m,
9e2f7c11 410 DBUS_TYPE_STRING, &u->meta.id,
c1e1601e
LP
411 DBUS_TYPE_OBJECT_PATH, &p,
412 DBUS_TYPE_INVALID))
413 goto oom;
414
f278026d 415 if (!dbus_connection_send(u->meta.manager->api_bus, m, NULL))
c1e1601e
LP
416 goto oom;
417
418 free(p);
419 dbus_message_unref(m);
420
421 return;
422
423oom:
424 free(p);
425
426 if (m)
427 dbus_message_unref(m);
428
429 log_error("Failed to allocate unit remove signal.");
430}