1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
12 #include "selinux-access.h"
13 #include "string-util.h"
16 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type
, job_type
, JobType
);
17 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_state
, job_state
, JobState
);
19 static int property_get_unit(
22 const char *interface
,
24 sd_bus_message
*reply
,
26 sd_bus_error
*error
) {
28 _cleanup_free_
char *p
= NULL
;
35 p
= unit_dbus_path(j
->unit
);
39 return sd_bus_message_append(reply
, "(so)", j
->unit
->id
, p
);
42 int bus_job_method_cancel(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
49 r
= mac_selinux_unit_access_check(j
->unit
, message
, "stop", error
);
53 /* Access is granted to the job owner */
54 if (!sd_bus_track_contains(j
->bus_track
, sd_bus_message_get_sender(message
))) {
56 /* And for everybody else consult polkit */
57 r
= bus_verify_manage_units_async(j
->unit
->manager
, message
, error
);
61 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
64 job_finish_and_invalidate(j
, JOB_CANCELED
, true, false);
66 return sd_bus_reply_method_return(message
, NULL
);
69 int bus_job_method_get_waiting_jobs(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
70 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
71 _cleanup_free_ Job
**list
= NULL
;
75 if (strstr(sd_bus_message_get_member(message
), "After"))
76 n
= job_get_after(j
, &list
);
78 n
= job_get_before(j
, &list
);
82 r
= sd_bus_message_new_method_return(message
, &reply
);
86 r
= sd_bus_message_open_container(reply
, 'a', "(usssoo)");
90 for (i
= 0; i
< n
; i
++) {
91 _cleanup_free_
char *unit_path
= NULL
, *job_path
= NULL
;
93 job_path
= job_dbus_path(list
[i
]);
97 unit_path
= unit_dbus_path(list
[i
]->unit
);
101 r
= sd_bus_message_append(reply
, "(usssoo)",
104 job_type_to_string(list
[i
]->type
),
105 job_state_to_string(list
[i
]->state
),
112 r
= sd_bus_message_close_container(reply
);
116 return sd_bus_send(NULL
, reply
, NULL
);
119 const sd_bus_vtable bus_job_vtable
[] = {
120 SD_BUS_VTABLE_START(0),
122 SD_BUS_METHOD("Cancel", NULL
, NULL
, bus_job_method_cancel
, SD_BUS_VTABLE_UNPRIVILEGED
),
123 SD_BUS_METHOD_WITH_NAMES("GetAfter",
127 bus_job_method_get_waiting_jobs
,
128 SD_BUS_VTABLE_UNPRIVILEGED
),
129 SD_BUS_METHOD_WITH_NAMES("GetBefore",
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
),
143 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
144 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
145 _cleanup_free_
char *p
= NULL
;
152 p
= job_dbus_path(j
);
156 r
= sd_bus_message_new_signal(
159 "/org/freedesktop/systemd1",
160 "org.freedesktop.systemd1.Manager",
165 r
= sd_bus_message_append(m
, "uos", j
->id
, p
, j
->unit
->id
);
169 return sd_bus_send(bus
, m
, NULL
);
172 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
173 _cleanup_free_
char *p
= NULL
;
179 p
= job_dbus_path(j
);
183 return sd_bus_emit_properties_changed(bus
, p
, "org.freedesktop.systemd1.Job", "State", NULL
);
186 void bus_job_send_change_signal(Job
*j
) {
191 /* Make sure that any change signal on the unit is reflected before we send out the change signal on the job */
192 bus_unit_send_pending_change_signal(j
->unit
, true);
194 if (j
->in_dbus_queue
) {
195 LIST_REMOVE(dbus_queue
, j
->manager
->dbus_job_queue
, j
);
196 j
->in_dbus_queue
= false;
199 r
= bus_foreach_bus(j
->manager
, j
->bus_track
, j
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, j
);
201 log_debug_errno(r
, "Failed to send job change signal for %u: %m", j
->id
);
203 j
->sent_dbus_new_signal
= true;
206 void bus_job_send_pending_change_signal(Job
*j
, bool including_new
) {
209 if (!j
->in_dbus_queue
)
212 if (!j
->sent_dbus_new_signal
&& !including_new
)
215 if (MANAGER_IS_RELOADING(j
->unit
->manager
))
218 bus_job_send_change_signal(j
);
221 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
222 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
223 _cleanup_free_
char *p
= NULL
;
230 p
= job_dbus_path(j
);
234 r
= sd_bus_message_new_signal(
237 "/org/freedesktop/systemd1",
238 "org.freedesktop.systemd1.Manager",
243 r
= sd_bus_message_append(m
, "uoss", j
->id
, p
, j
->unit
->id
, job_result_to_string(j
->result
));
247 return sd_bus_send(bus
, m
, NULL
);
250 void bus_job_send_removed_signal(Job
*j
) {
255 if (!j
->sent_dbus_new_signal
)
256 bus_job_send_change_signal(j
);
258 /* Make sure that any change signal on the unit is reflected before we send out the change signal on the job */
259 bus_unit_send_pending_change_signal(j
->unit
, true);
261 r
= bus_foreach_bus(j
->manager
, j
->bus_track
, send_removed_signal
, j
);
263 log_debug_errno(r
, "Failed to send job remove signal for %u: %m", j
->id
);
266 static int bus_job_track_handler(sd_bus_track
*t
, void *userdata
) {
272 j
->bus_track
= sd_bus_track_unref(j
->bus_track
); /* make sure we aren't called again */
274 /* Last client dropped off the bus, maybe we should GC this now? */
275 job_add_to_gc_queue(j
);
279 static int bus_job_allocate_bus_track(Job
*j
) {
286 return sd_bus_track_new(j
->unit
->manager
->api_bus
, &j
->bus_track
, bus_job_track_handler
, j
);
289 int bus_job_coldplug_bus_track(Job
*j
) {
291 _cleanup_strv_free_
char **deserialized_clients
= NULL
;
295 deserialized_clients
= TAKE_PTR(j
->deserialized_clients
);
297 if (strv_isempty(deserialized_clients
))
300 if (!j
->manager
->api_bus
)
303 r
= bus_job_allocate_bus_track(j
);
307 return bus_track_add_name_many(j
->bus_track
, deserialized_clients
);
310 int bus_job_track_sender(Job
*j
, sd_bus_message
*m
) {
316 if (sd_bus_message_get_bus(m
) != j
->unit
->manager
->api_bus
) {
317 j
->ref_by_private_bus
= true;
321 r
= bus_job_allocate_bus_track(j
);
325 return sd_bus_track_add_sender(j
->bus_track
, m
);