1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
10 #include "alloc-util.h"
15 #include "selinux-access.h"
16 #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 PolicyKit */
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 (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),
123 SD_BUS_METHOD("Cancel", NULL
, NULL
, bus_job_method_cancel
, SD_BUS_VTABLE_UNPRIVILEGED
),
124 SD_BUS_METHOD("GetAfter", NULL
, "a(usssoo)", bus_job_method_get_waiting_jobs
, SD_BUS_VTABLE_UNPRIVILEGED
),
125 SD_BUS_METHOD("GetBefore", NULL
, "a(usssoo)", bus_job_method_get_waiting_jobs
, SD_BUS_VTABLE_UNPRIVILEGED
),
126 SD_BUS_PROPERTY("Id", "u", NULL
, offsetof(Job
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
127 SD_BUS_PROPERTY("Unit", "(so)", property_get_unit
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
128 SD_BUS_PROPERTY("JobType", "s", property_get_type
, offsetof(Job
, type
), SD_BUS_VTABLE_PROPERTY_CONST
),
129 SD_BUS_PROPERTY("State", "s", property_get_state
, offsetof(Job
, state
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
133 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
134 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
135 _cleanup_free_
char *p
= NULL
;
142 p
= job_dbus_path(j
);
146 r
= sd_bus_message_new_signal(
149 "/org/freedesktop/systemd1",
150 "org.freedesktop.systemd1.Manager",
155 r
= sd_bus_message_append(m
, "uos", j
->id
, p
, j
->unit
->id
);
159 return sd_bus_send(bus
, m
, NULL
);
162 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
163 _cleanup_free_
char *p
= NULL
;
169 p
= job_dbus_path(j
);
173 return sd_bus_emit_properties_changed(bus
, p
, "org.freedesktop.systemd1.Job", "State", NULL
);
176 void bus_job_send_change_signal(Job
*j
) {
181 if (j
->in_dbus_queue
) {
182 LIST_REMOVE(dbus_queue
, j
->manager
->dbus_job_queue
, j
);
183 j
->in_dbus_queue
= false;
186 r
= bus_foreach_bus(j
->manager
, j
->bus_track
, j
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, j
);
188 log_debug_errno(r
, "Failed to send job change signal for %u: %m", j
->id
);
190 j
->sent_dbus_new_signal
= true;
193 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
194 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
195 _cleanup_free_
char *p
= NULL
;
202 p
= job_dbus_path(j
);
206 r
= sd_bus_message_new_signal(
209 "/org/freedesktop/systemd1",
210 "org.freedesktop.systemd1.Manager",
215 r
= sd_bus_message_append(m
, "uoss", j
->id
, p
, j
->unit
->id
, job_result_to_string(j
->result
));
219 return sd_bus_send(bus
, m
, NULL
);
222 void bus_job_send_removed_signal(Job
*j
) {
227 if (!j
->sent_dbus_new_signal
)
228 bus_job_send_change_signal(j
);
230 r
= bus_foreach_bus(j
->manager
, j
->bus_track
, send_removed_signal
, j
);
232 log_debug_errno(r
, "Failed to send job remove signal for %u: %m", j
->id
);
235 static int bus_job_track_handler(sd_bus_track
*t
, void *userdata
) {
241 j
->bus_track
= sd_bus_track_unref(j
->bus_track
); /* make sure we aren't called again */
243 /* Last client dropped off the bus, maybe we should GC this now? */
244 job_add_to_gc_queue(j
);
248 static int bus_job_allocate_bus_track(Job
*j
) {
255 return sd_bus_track_new(j
->unit
->manager
->api_bus
, &j
->bus_track
, bus_job_track_handler
, j
);
258 int bus_job_coldplug_bus_track(Job
*j
) {
260 _cleanup_strv_free_
char **deserialized_clients
= NULL
;
264 deserialized_clients
= TAKE_PTR(j
->deserialized_clients
);
266 if (strv_isempty(deserialized_clients
))
269 if (!j
->manager
->api_bus
)
272 r
= bus_job_allocate_bus_track(j
);
276 return bus_track_add_name_many(j
->bus_track
, deserialized_clients
);
279 int bus_job_track_sender(Job
*j
, sd_bus_message
*m
) {
285 if (sd_bus_message_get_bus(m
) != j
->unit
->manager
->api_bus
) {
286 j
->ref_by_private_bus
= true;
290 r
= bus_job_allocate_bus_track(j
);
294 return sd_bus_track_add_sender(j
->bus_track
, m
);