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