]> git.ipfire.org Git - people/ms/systemd.git/blob - dbus-job.c
socket: optionally call accept() for incoming connections and spawn one service insta...
[people/ms/systemd.git] / 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
27 static const char introspection[] =
28 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
29 "<node>"
30 " <interface name=\"org.freedesktop.systemd1.Job\">"
31 " <method name=\"Cancel\"/>"
32 " <signal name=\"Changed\"/>"
33 " <property name=\"Id\" type=\"u\" access=\"read\"/>"
34 " <property name=\"Unit\" type=\"(so)\" access=\"read\"/>"
35 " <property name=\"JobType\" type=\"s\" access=\"read\"/>"
36 " <property name=\"State\" type=\"s\" access=\"read\"/>"
37 " </interface>"
38 BUS_PROPERTIES_INTERFACE
39 BUS_INTROSPECTABLE_INTERFACE
40 "</node>";
41
42 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_state, job_state, JobState);
43 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_type, job_type, JobType);
44
45 static int bus_job_append_unit(Manager *m, DBusMessageIter *i, const char *property, void *data) {
46 Job *j = data;
47 DBusMessageIter sub;
48 char *p;
49
50 assert(m);
51 assert(i);
52 assert(property);
53 assert(j);
54
55 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
56 return -ENOMEM;
57
58 if (!(p = unit_dbus_path(j->unit)))
59 return -ENOMEM;
60
61 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &j->unit->meta.id) ||
62 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
63 free(p);
64 return -ENOMEM;
65 }
66
67 free(p);
68
69 if (!dbus_message_iter_close_container(i, &sub))
70 return -ENOMEM;
71
72 return 0;
73 }
74
75 static DBusHandlerResult bus_job_message_dispatch(Job *j, DBusMessage *message) {
76 const BusProperty properties[] = {
77 { "org.freedesktop.systemd1.Job", "Id", bus_property_append_uint32, "u", &j->id },
78 { "org.freedesktop.systemd1.Job", "State", bus_job_append_state, "s", &j->state },
79 { "org.freedesktop.systemd1.Job", "JobType", bus_job_append_type, "s", &j->type },
80 { "org.freedesktop.systemd1.Job", "Unit", bus_job_append_unit, "(so)", j },
81 { NULL, NULL, NULL, NULL, NULL }
82 };
83
84 DBusMessage *reply = NULL;
85 Manager *m = j->manager;
86
87 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Job", "Cancel")) {
88 if (!(reply = dbus_message_new_method_return(message)))
89 goto oom;
90
91 job_free(j);
92
93 } else
94 return bus_default_message_handler(j->manager, message, introspection, properties);
95
96 if (reply) {
97 if (!dbus_connection_send(m->api_bus, reply, NULL))
98 goto oom;
99
100 dbus_message_unref(reply);
101 }
102
103 return DBUS_HANDLER_RESULT_HANDLED;
104
105 oom:
106 if (reply)
107 dbus_message_unref(reply);
108
109 return DBUS_HANDLER_RESULT_NEED_MEMORY;
110 }
111
112 static DBusHandlerResult bus_job_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
113 Manager *m = data;
114 Job *j;
115 int r;
116
117 assert(connection);
118 assert(message);
119 assert(m);
120
121 log_debug("Got D-Bus request: %s.%s() on %s",
122 dbus_message_get_interface(message),
123 dbus_message_get_member(message),
124 dbus_message_get_path(message));
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, message, NULL, r);
135 }
136
137 return bus_job_message_dispatch(j, message);
138 }
139
140 const DBusObjectPathVTable bus_job_vtable = {
141 .message_function = bus_job_message_handler
142 };
143
144 void bus_job_send_change_signal(Job *j) {
145 char *p = NULL;
146 DBusMessage *m = NULL;
147
148 assert(j);
149 assert(j->in_dbus_queue);
150
151 LIST_REMOVE(Job, dbus_queue, j->manager->dbus_job_queue, j);
152 j->in_dbus_queue = false;
153
154 if (set_isempty(j->manager->subscribed))
155 return;
156
157 if (!(p = job_dbus_path(j)))
158 goto oom;
159
160 if (j->sent_dbus_new_signal) {
161 /* Send a change signal */
162
163 if (!(m = dbus_message_new_signal(p, "org.freedesktop.systemd1.Job", "Changed")))
164 goto oom;
165 } else {
166 /* Send a new signal */
167
168 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "JobNew")))
169 goto oom;
170
171 if (!dbus_message_append_args(m,
172 DBUS_TYPE_UINT32, &j->id,
173 DBUS_TYPE_OBJECT_PATH, &p,
174 DBUS_TYPE_INVALID))
175 goto oom;
176 }
177
178 if (!dbus_connection_send(j->manager->api_bus, m, NULL))
179 goto oom;
180
181 free(p);
182 dbus_message_unref(m);
183
184 j->sent_dbus_new_signal = true;
185
186 return;
187
188 oom:
189 free(p);
190
191 if (m)
192 dbus_message_unref(m);
193
194 log_error("Failed to allocate job change signal.");
195 }
196
197 void bus_job_send_removed_signal(Job *j) {
198 char *p = NULL;
199 DBusMessage *m = NULL;
200
201 assert(j);
202
203 if (set_isempty(j->manager->subscribed) || !j->sent_dbus_new_signal)
204 return;
205
206 if (!(p = job_dbus_path(j)))
207 goto oom;
208
209 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "JobRemoved")))
210 goto oom;
211
212 if (!dbus_message_append_args(m,
213 DBUS_TYPE_UINT32, &j->id,
214 DBUS_TYPE_OBJECT_PATH, &p,
215 DBUS_TYPE_INVALID))
216 goto oom;
217
218 if (!dbus_connection_send(j->manager->api_bus, m, NULL))
219 goto oom;
220
221 free(p);
222 dbus_message_unref(m);
223
224 return;
225
226 oom:
227 free(p);
228
229 if (m)
230 dbus_message_unref(m);
231
232 log_error("Failed to allocate job remove signal.");
233 }