chiark / gitweb /
systemctl: don't hit an assert when we are run from a non-systemd boot
[elogind.git] / src / dbus-job.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
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
22 #include <errno.h>
23
24 #include "dbus.h"
25 #include "log.h"
26 #include "dbus-job.h"
27
28 #define BUS_JOB_INTERFACE                                             \
29         " <interface name=\"org.freedesktop.systemd1.Job\">\n"        \
30         "  <method name=\"Cancel\"/>\n"                               \
31         "  <signal name=\"Changed\"/>\n"                              \
32         "  <property name=\"Id\" type=\"u\" access=\"read\"/>\n"      \
33         "  <property name=\"Unit\" type=\"(so)\" access=\"read\"/>\n" \
34         "  <property name=\"JobType\" type=\"s\" access=\"read\"/>\n" \
35         "  <property name=\"State\" type=\"s\" access=\"read\"/>\n"   \
36         " </interface>\n"
37
38 #define INTROSPECTION                                                 \
39         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                     \
40         "<node>\n"                                                    \
41         BUS_JOB_INTERFACE                                             \
42         BUS_PROPERTIES_INTERFACE                                      \
43         BUS_INTROSPECTABLE_INTERFACE                                  \
44         "</node>\n"
45
46 const char bus_job_interface[] = BUS_JOB_INTERFACE;
47
48 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_state, job_state, JobState);
49 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_type, job_type, JobType);
50
51 static int bus_job_append_unit(Manager *m, DBusMessageIter *i, const char *property, void *data) {
52         Job *j = data;
53         DBusMessageIter sub;
54         char *p;
55
56         assert(m);
57         assert(i);
58         assert(property);
59         assert(j);
60
61         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
62                 return -ENOMEM;
63
64         if (!(p = unit_dbus_path(j->unit)))
65                 return -ENOMEM;
66
67         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &j->unit->meta.id) ||
68             !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
69                 free(p);
70                 return -ENOMEM;
71         }
72
73         free(p);
74
75         if (!dbus_message_iter_close_container(i, &sub))
76                 return -ENOMEM;
77
78         return 0;
79 }
80
81 static DBusHandlerResult bus_job_message_dispatch(Job *j, DBusConnection *connection, DBusMessage *message) {
82         const BusProperty properties[] = {
83                 { "org.freedesktop.systemd1.Job", "Id",      bus_property_append_uint32, "u",    &j->id    },
84                 { "org.freedesktop.systemd1.Job", "State",   bus_job_append_state,       "s",    &j->state },
85                 { "org.freedesktop.systemd1.Job", "JobType", bus_job_append_type,        "s",    &j->type  },
86                 { "org.freedesktop.systemd1.Job", "Unit",    bus_job_append_unit,        "(so)", j         },
87                 { NULL, NULL, NULL, NULL, NULL }
88         };
89
90         DBusMessage *reply = NULL;
91
92         if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Job", "Cancel")) {
93                 if (!(reply = dbus_message_new_method_return(message)))
94                         goto oom;
95
96                 job_free(j);
97
98         } else
99                 return bus_default_message_handler(j->manager, connection, message, INTROSPECTION, properties);
100
101         if (reply) {
102                 if (!dbus_connection_send(connection, reply, NULL))
103                         goto oom;
104
105                 dbus_message_unref(reply);
106         }
107
108         return DBUS_HANDLER_RESULT_HANDLED;
109
110 oom:
111         if (reply)
112                 dbus_message_unref(reply);
113
114         return DBUS_HANDLER_RESULT_NEED_MEMORY;
115 }
116
117 static DBusHandlerResult bus_job_message_handler(DBusConnection *connection, DBusMessage  *message, void *data) {
118         Manager *m = data;
119         Job *j;
120         int r;
121
122         assert(connection);
123         assert(message);
124         assert(m);
125
126         if ((r = manager_get_job_from_dbus_path(m, dbus_message_get_path(message), &j)) < 0) {
127
128                 if (r == -ENOMEM)
129                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
130
131                 if (r == -ENOENT)
132                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
133
134                 return bus_send_error_reply(m, connection, message, NULL, r);
135         }
136
137         return bus_job_message_dispatch(j, connection, message);
138 }
139
140 const DBusObjectPathVTable bus_job_vtable = {
141         .message_function = bus_job_message_handler
142 };
143
144 static int job_send_message(Job *j, DBusMessage *m) {
145         int r;
146
147         assert(j);
148         assert(m);
149
150         if (bus_has_subscriber(j->manager)) {
151                 if ((r = bus_broadcast(j->manager, m)) < 0)
152                         return r;
153
154         } else  if (j->bus_client) {
155                 /* If nobody is subscribed, we just send the message
156                  * to the client which created the job */
157
158                 assert(j->bus);
159
160                 if (!dbus_message_set_destination(m, j->bus_client))
161                         return -ENOMEM;
162
163                 if (!dbus_connection_send(j->bus, m, NULL))
164                         return -ENOMEM;
165         }
166
167         return 0;
168 }
169
170 void bus_job_send_change_signal(Job *j) {
171         char *p = NULL;
172         DBusMessage *m = NULL;
173
174         assert(j);
175
176         if (j->in_dbus_queue) {
177                 LIST_REMOVE(Job, dbus_queue, j->manager->dbus_job_queue, j);
178                 j->in_dbus_queue = false;
179         }
180
181         if (!bus_has_subscriber(j->manager) && !j->bus_client) {
182                 j->sent_dbus_new_signal = true;
183                 return;
184         }
185
186         if (!(p = job_dbus_path(j)))
187                 goto oom;
188
189         if (j->sent_dbus_new_signal) {
190                 /* Send a change signal */
191
192                 if (!(m = dbus_message_new_signal(p, "org.freedesktop.systemd1.Job", "Changed")))
193                         goto oom;
194         } else {
195                 /* Send a new signal */
196
197                 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "JobNew")))
198                         goto oom;
199
200                 if (!dbus_message_append_args(m,
201                                               DBUS_TYPE_UINT32, &j->id,
202                                               DBUS_TYPE_OBJECT_PATH, &p,
203                                               DBUS_TYPE_INVALID))
204                         goto oom;
205         }
206
207         if (job_send_message(j, m) < 0)
208                 goto oom;
209
210         free(p);
211         dbus_message_unref(m);
212
213         j->sent_dbus_new_signal = true;
214
215         return;
216
217 oom:
218         free(p);
219
220         if (m)
221                 dbus_message_unref(m);
222
223         log_error("Failed to allocate job change signal.");
224 }
225
226 void bus_job_send_removed_signal(Job *j, bool success) {
227         char *p = NULL;
228         DBusMessage *m = NULL;
229         dbus_bool_t b = success;
230
231         assert(j);
232
233         if (!bus_has_subscriber(j->manager) && !j->bus_client)
234                 return;
235
236         if (!j->sent_dbus_new_signal)
237                 bus_job_send_change_signal(j);
238
239         if (!(p = job_dbus_path(j)))
240                 goto oom;
241
242         if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "JobRemoved")))
243                 goto oom;
244
245         if (!dbus_message_append_args(m,
246                                       DBUS_TYPE_UINT32, &j->id,
247                                       DBUS_TYPE_OBJECT_PATH, &p,
248                                       DBUS_TYPE_BOOLEAN, &b,
249                                       DBUS_TYPE_INVALID))
250                 goto oom;
251
252         if (job_send_message(j, m) < 0)
253                 goto oom;
254
255         free(p);
256         dbus_message_unref(m);
257
258         return;
259
260 oom:
261         free(p);
262
263         if (m)
264                 dbus_message_unref(m);
265
266         log_error("Failed to allocate job remove signal.");
267 }