1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
6 #include "bus-get-properties.h"
10 #include "dbus-util.h"
14 #include "selinux-access.h"
15 #include "string-util.h"
18 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type
, job_type
, JobType
);
19 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_state
, job_state
, JobState
);
21 static int property_get_unit(
24 const char *interface
,
26 sd_bus_message
*reply
,
28 sd_bus_error
*error
) {
30 _cleanup_free_
char *p
= NULL
;
37 p
= unit_dbus_path(j
->unit
);
41 return sd_bus_message_append(reply
, "(so)", j
->unit
->id
, p
);
44 int bus_job_method_cancel(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
51 r
= mac_selinux_unit_access_check(j
->unit
, message
, "stop", error
);
55 /* Access is granted to the job owner */
56 if (!sd_bus_track_contains(j
->bus_track
, sd_bus_message_get_sender(message
))) {
58 /* And for everybody else consult polkit */
59 r
= bus_verify_manage_units_async(j
->unit
->manager
, message
, error
);
63 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
66 job_finish_and_invalidate(j
, JOB_CANCELED
, true, false);
68 return sd_bus_reply_method_return(message
, NULL
);
71 int bus_job_method_get_waiting_jobs(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
72 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
73 _cleanup_free_ Job
**list
= NULL
;
77 if (strstr(sd_bus_message_get_member(message
), "After"))
78 n
= job_get_after(j
, &list
);
80 n
= job_get_before(j
, &list
);
84 r
= sd_bus_message_new_method_return(message
, &reply
);
88 r
= sd_bus_message_open_container(reply
, 'a', "(usssoo)");
92 for (int i
= 0; i
< n
; i
++) {
93 _cleanup_free_
char *unit_path
= NULL
, *job_path
= NULL
;
95 job_path
= job_dbus_path(list
[i
]);
99 unit_path
= unit_dbus_path(list
[i
]->unit
);
103 r
= sd_bus_message_append(reply
, "(usssoo)",
106 job_type_to_string(list
[i
]->type
),
107 job_state_to_string(list
[i
]->state
),
114 r
= sd_bus_message_close_container(reply
);
118 return sd_bus_send(NULL
, reply
, NULL
);
121 const sd_bus_vtable bus_job_vtable
[] = {
122 SD_BUS_VTABLE_START(0),
124 SD_BUS_METHOD("Cancel", NULL
, NULL
, bus_job_method_cancel
, SD_BUS_VTABLE_UNPRIVILEGED
),
125 SD_BUS_METHOD_WITH_ARGS("GetAfter",
127 SD_BUS_RESULT("a(usssoo)", jobs
),
128 bus_job_method_get_waiting_jobs
,
129 SD_BUS_VTABLE_UNPRIVILEGED
),
130 SD_BUS_METHOD_WITH_ARGS("GetBefore",
132 SD_BUS_RESULT("a(usssoo)", jobs
),
133 bus_job_method_get_waiting_jobs
,
134 SD_BUS_VTABLE_UNPRIVILEGED
),
136 SD_BUS_PROPERTY("Id", "u", NULL
, offsetof(Job
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
137 SD_BUS_PROPERTY("Unit", "(so)", property_get_unit
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
138 SD_BUS_PROPERTY("JobType", "s", property_get_type
, offsetof(Job
, type
), SD_BUS_VTABLE_PROPERTY_CONST
),
139 SD_BUS_PROPERTY("State", "s", property_get_state
, offsetof(Job
, state
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
140 SD_BUS_PROPERTY("ActivationDetails", "a(ss)", bus_property_get_activation_details
, offsetof(Job
, activation_details
), SD_BUS_VTABLE_PROPERTY_CONST
),
144 static int bus_job_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
145 Manager
*m
= userdata
;
155 r
= manager_get_job_from_dbus_path(m
, path
, &j
);
163 static int bus_job_enumerate(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
164 _cleanup_strv_free_
char **l
= NULL
;
165 Manager
*m
= userdata
;
169 l
= new0(char*, hashmap_size(m
->jobs
)+1);
173 HASHMAP_FOREACH(j
, m
->jobs
) {
174 l
[k
] = job_dbus_path(j
);
181 assert(hashmap_size(m
->jobs
) == k
);
183 *nodes
= TAKE_PTR(l
);
188 const BusObjectImplementation job_object
= {
189 "/org/freedesktop/systemd1/job",
190 "org.freedesktop.systemd1.Job",
191 .fallback_vtables
= BUS_FALLBACK_VTABLES({bus_job_vtable
, bus_job_find
}),
192 .node_enumerator
= bus_job_enumerate
,
195 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
196 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
197 _cleanup_free_
char *p
= NULL
;
204 p
= job_dbus_path(j
);
208 r
= sd_bus_message_new_signal(
211 "/org/freedesktop/systemd1",
212 "org.freedesktop.systemd1.Manager",
217 r
= sd_bus_message_append(m
, "uos", j
->id
, p
, j
->unit
->id
);
221 return sd_bus_send(bus
, m
, NULL
);
224 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
225 _cleanup_free_
char *p
= NULL
;
231 p
= job_dbus_path(j
);
235 return sd_bus_emit_properties_changed(bus
, p
, "org.freedesktop.systemd1.Job", "State", NULL
);
238 void bus_job_send_change_signal(Job
*j
) {
243 /* Make sure that any change signal on the unit is reflected before we send out the change signal on the job */
244 bus_unit_send_pending_change_signal(j
->unit
, true);
246 if (j
->in_dbus_queue
) {
247 LIST_REMOVE(dbus_queue
, j
->manager
->dbus_job_queue
, j
);
248 j
->in_dbus_queue
= false;
251 r
= bus_foreach_bus(j
->manager
, j
->bus_track
, j
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, j
);
253 log_debug_errno(r
, "Failed to send job change signal for %u: %m", j
->id
);
255 j
->sent_dbus_new_signal
= true;
258 void bus_job_send_pending_change_signal(Job
*j
, bool including_new
) {
261 if (!j
->in_dbus_queue
)
264 if (!j
->sent_dbus_new_signal
&& !including_new
)
267 if (MANAGER_IS_RELOADING(j
->unit
->manager
))
270 bus_job_send_change_signal(j
);
273 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
274 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
275 _cleanup_free_
char *p
= NULL
;
282 p
= job_dbus_path(j
);
286 r
= sd_bus_message_new_signal(
289 "/org/freedesktop/systemd1",
290 "org.freedesktop.systemd1.Manager",
295 r
= sd_bus_message_append(m
, "uoss", j
->id
, p
, j
->unit
->id
, job_result_to_string(j
->result
));
299 return sd_bus_send(bus
, m
, NULL
);
302 void bus_job_send_removed_signal(Job
*j
) {
307 if (!j
->sent_dbus_new_signal
)
308 bus_job_send_change_signal(j
);
310 /* Make sure that any change signal on the unit is reflected before we send out the change signal on the job */
311 bus_unit_send_pending_change_signal(j
->unit
, true);
313 r
= bus_foreach_bus(j
->manager
, j
->bus_track
, send_removed_signal
, j
);
315 log_debug_errno(r
, "Failed to send job remove signal for %u: %m", j
->id
);
318 static int bus_job_track_handler(sd_bus_track
*t
, void *userdata
) {
324 j
->bus_track
= sd_bus_track_unref(j
->bus_track
); /* make sure we aren't called again */
326 /* Last client dropped off the bus, maybe we should GC this now? */
327 job_add_to_gc_queue(j
);
331 static int bus_job_allocate_bus_track(Job
*j
) {
338 return sd_bus_track_new(j
->unit
->manager
->api_bus
, &j
->bus_track
, bus_job_track_handler
, j
);
341 int bus_job_coldplug_bus_track(Job
*j
) {
343 _cleanup_strv_free_
char **deserialized_clients
= NULL
;
347 deserialized_clients
= TAKE_PTR(j
->deserialized_clients
);
349 if (strv_isempty(deserialized_clients
))
352 if (!j
->manager
->api_bus
)
355 r
= bus_job_allocate_bus_track(j
);
359 return bus_track_add_name_many(j
->bus_track
, deserialized_clients
);
362 int bus_job_track_sender(Job
*j
, sd_bus_message
*m
) {
368 if (sd_bus_message_get_bus(m
) != j
->unit
->manager
->api_bus
) {
369 j
->ref_by_private_bus
= true;
373 r
= bus_job_allocate_bus_track(j
);
377 return sd_bus_track_add_sender(j
->bus_track
, m
);