]> git.ipfire.org Git - people/ms/systemd.git/blame - dbus-job.c
manager: fix GC algorithm
[people/ms/systemd.git] / dbus-job.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
86fbf370
LP
22#include <errno.h>
23
ea430986
LP
24#include "dbus.h"
25#include "log.h"
4139c1b2 26#include "dbus-job.h"
ea430986
LP
27
28static const char introspection[] =
29 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
30 "<node>"
31 " <interface name=\"org.freedesktop.systemd1.Job\">"
b548631a 32 " <method name=\"Cancel\"/>"
c1e1601e 33 " <signal name=\"Changed\"/>"
86fbf370
LP
34 " <property name=\"Id\" type=\"u\" access=\"read\"/>"
35 " <property name=\"Unit\" type=\"(so)\" access=\"read\"/>"
b548631a 36 " <property name=\"JobType\" type=\"s\" access=\"read\"/>"
86fbf370 37 " <property name=\"State\" type=\"s\" access=\"read\"/>"
ea430986
LP
38 " </interface>"
39 BUS_PROPERTIES_INTERFACE
40 BUS_INTROSPECTABLE_INTERFACE
41 "</node>";
42
4139c1b2
LP
43static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_state, job_state, JobState);
44static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_type, job_type, JobType);
86fbf370
LP
45
46static int bus_job_append_unit(Manager *m, DBusMessageIter *i, const char *property, void *data) {
47 Job *j = data;
48 DBusMessageIter sub;
49 char *p;
86fbf370
LP
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
9e2f7c11 62 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &j->unit->meta.id) ||
86fbf370
LP
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
76static DBusHandlerResult bus_job_message_dispatch(Job *j, DBusMessage *message) {
86fbf370 77 const BusProperty properties[] = {
6f4706b7
LP
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 },
86fbf370
LP
82 { NULL, NULL, NULL, NULL, NULL }
83 };
84
b548631a
LP
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) {
f278026d 98 if (!dbus_connection_send(m->api_bus, reply, NULL))
b548631a
LP
99 goto oom;
100
101 dbus_message_unref(reply);
102 }
103
104 return DBUS_HANDLER_RESULT_HANDLED;
105
106oom:
107 if (reply)
108 dbus_message_unref(reply);
109
110 return DBUS_HANDLER_RESULT_NEED_MEMORY;
86fbf370
LP
111}
112
47be870b 113static DBusHandlerResult bus_job_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
ea430986 114 Manager *m = data;
86fbf370
LP
115 Job *j;
116 int r;
ea430986
LP
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
86fbf370
LP
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);
ea430986
LP
139}
140
141const DBusObjectPathVTable bus_job_vtable = {
142 .message_function = bus_job_message_handler
143};
c1e1601e
LP
144
145void 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 return;
157
158 if (!(p = job_dbus_path(j)))
159 goto oom;
160
161 if (j->sent_dbus_new_signal) {
162 /* Send a change signal */
163
164 if (!(m = dbus_message_new_signal(p, "org.freedesktop.systemd1.Job", "Changed")))
165 goto oom;
166 } else {
167 /* Send a new signal */
168
701cc384 169 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "JobNew")))
c1e1601e
LP
170 goto oom;
171
172 if (!dbus_message_append_args(m,
173 DBUS_TYPE_UINT32, &j->id,
174 DBUS_TYPE_OBJECT_PATH, &p,
175 DBUS_TYPE_INVALID))
176 goto oom;
177 }
178
f278026d 179 if (!dbus_connection_send(j->manager->api_bus, m, NULL))
c1e1601e
LP
180 goto oom;
181
182 free(p);
183 dbus_message_unref(m);
184
185 j->sent_dbus_new_signal = true;
186
187 return;
188
189oom:
190 free(p);
191
192 if (m)
193 dbus_message_unref(m);
194
195 log_error("Failed to allocate job change signal.");
196}
197
198void bus_job_send_removed_signal(Job *j) {
199 char *p = NULL;
200 DBusMessage *m = NULL;
201
202 assert(j);
203
204 if (set_isempty(j->manager->subscribed) || !j->sent_dbus_new_signal)
205 return;
206
207 if (!(p = job_dbus_path(j)))
208 goto oom;
209
701cc384 210 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "JobRemoved")))
c1e1601e
LP
211 goto oom;
212
213 if (!dbus_message_append_args(m,
214 DBUS_TYPE_UINT32, &j->id,
215 DBUS_TYPE_OBJECT_PATH, &p,
216 DBUS_TYPE_INVALID))
217 goto oom;
218
f278026d 219 if (!dbus_connection_send(j->manager->api_bus, m, NULL))
c1e1601e
LP
220 goto oom;
221
222 free(p);
223 dbus_message_unref(m);
224
225 return;
226
227oom:
228 free(p);
229
230 if (m)
231 dbus_message_unref(m);
232
233 log_error("Failed to allocate job remove signal.");
234}