1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "alloc-util.h"
28 #include "selinux-access.h"
29 #include "string-util.h"
31 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type
, job_type
, JobType
);
32 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_state
, job_state
, JobState
);
34 static int property_get_unit(
37 const char *interface
,
39 sd_bus_message
*reply
,
41 sd_bus_error
*error
) {
43 _cleanup_free_
char *p
= NULL
;
50 p
= unit_dbus_path(j
->unit
);
54 return sd_bus_message_append(reply
, "(so)", j
->unit
->id
, p
);
57 int bus_job_method_cancel(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
64 r
= mac_selinux_unit_access_check(j
->unit
, message
, "stop", error
);
68 /* Access is granted to the job owner */
69 if (!sd_bus_track_contains(j
->bus_track
, sd_bus_message_get_sender(message
))) {
71 /* And for everybody else consult PolicyKit */
72 r
= bus_verify_manage_units_async(j
->unit
->manager
, message
, error
);
76 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
79 job_finish_and_invalidate(j
, JOB_CANCELED
, true, false);
81 return sd_bus_reply_method_return(message
, NULL
);
84 int bus_job_method_get_waiting_jobs(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
85 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
86 _cleanup_free_ Job
**list
= NULL
;
90 if (strstr(sd_bus_message_get_member(message
), "After"))
91 n
= job_get_after(j
, &list
);
93 n
= job_get_before(j
, &list
);
97 r
= sd_bus_message_new_method_return(message
, &reply
);
101 r
= sd_bus_message_open_container(reply
, 'a', "(usssoo)");
105 for (i
= 0; i
< n
; i
++) {
106 _cleanup_free_
char *unit_path
= NULL
, *job_path
= NULL
;
108 job_path
= job_dbus_path(list
[i
]);
112 unit_path
= unit_dbus_path(list
[i
]->unit
);
116 r
= sd_bus_message_append(reply
, "(usssoo)",
119 job_type_to_string(list
[i
]->type
),
120 job_state_to_string(list
[i
]->state
),
127 r
= sd_bus_message_close_container(reply
);
131 return sd_bus_send(NULL
, reply
, NULL
);
134 const sd_bus_vtable bus_job_vtable
[] = {
135 SD_BUS_VTABLE_START(0),
136 SD_BUS_METHOD("Cancel", NULL
, NULL
, bus_job_method_cancel
, SD_BUS_VTABLE_UNPRIVILEGED
),
137 SD_BUS_METHOD("GetAfter", NULL
, "a(usssoo)", bus_job_method_get_waiting_jobs
, SD_BUS_VTABLE_UNPRIVILEGED
),
138 SD_BUS_METHOD("GetBefore", NULL
, "a(usssoo)", bus_job_method_get_waiting_jobs
, SD_BUS_VTABLE_UNPRIVILEGED
),
139 SD_BUS_PROPERTY("Id", "u", NULL
, offsetof(Job
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
140 SD_BUS_PROPERTY("Unit", "(so)", property_get_unit
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
141 SD_BUS_PROPERTY("JobType", "s", property_get_type
, offsetof(Job
, type
), SD_BUS_VTABLE_PROPERTY_CONST
),
142 SD_BUS_PROPERTY("State", "s", property_get_state
, offsetof(Job
, state
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
146 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
147 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
148 _cleanup_free_
char *p
= NULL
;
155 p
= job_dbus_path(j
);
159 r
= sd_bus_message_new_signal(
162 "/org/freedesktop/systemd1",
163 "org.freedesktop.systemd1.Manager",
168 r
= sd_bus_message_append(m
, "uos", j
->id
, p
, j
->unit
->id
);
172 return sd_bus_send(bus
, m
, NULL
);
175 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
176 _cleanup_free_
char *p
= NULL
;
182 p
= job_dbus_path(j
);
186 return sd_bus_emit_properties_changed(bus
, p
, "org.freedesktop.systemd1.Job", "State", NULL
);
189 void bus_job_send_change_signal(Job
*j
) {
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 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
207 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
208 _cleanup_free_
char *p
= NULL
;
215 p
= job_dbus_path(j
);
219 r
= sd_bus_message_new_signal(
222 "/org/freedesktop/systemd1",
223 "org.freedesktop.systemd1.Manager",
228 r
= sd_bus_message_append(m
, "uoss", j
->id
, p
, j
->unit
->id
, job_result_to_string(j
->result
));
232 return sd_bus_send(bus
, m
, NULL
);
235 void bus_job_send_removed_signal(Job
*j
) {
240 if (!j
->sent_dbus_new_signal
)
241 bus_job_send_change_signal(j
);
243 r
= bus_foreach_bus(j
->manager
, j
->bus_track
, send_removed_signal
, j
);
245 log_debug_errno(r
, "Failed to send job remove signal for %u: %m", j
->id
);
248 static int bus_job_track_handler(sd_bus_track
*t
, void *userdata
) {
254 j
->bus_track
= sd_bus_track_unref(j
->bus_track
); /* make sure we aren't called again */
256 /* Last client dropped off the bus, maybe we should GC this now? */
257 job_add_to_gc_queue(j
);
261 static int bus_job_allocate_bus_track(Job
*j
) {
268 return sd_bus_track_new(j
->unit
->manager
->api_bus
, &j
->bus_track
, bus_job_track_handler
, j
);
271 int bus_job_coldplug_bus_track(Job
*j
) {
273 _cleanup_strv_free_
char **deserialized_clients
= NULL
;
277 deserialized_clients
= j
->deserialized_clients
;
278 j
->deserialized_clients
= NULL
;
280 if (strv_isempty(deserialized_clients
))
283 if (!j
->manager
->api_bus
)
286 r
= bus_job_allocate_bus_track(j
);
290 return bus_track_add_name_many(j
->bus_track
, deserialized_clients
);
293 int bus_job_track_sender(Job
*j
, sd_bus_message
*m
) {
299 if (sd_bus_message_get_bus(m
) != j
->unit
->manager
->api_bus
) {
300 j
->ref_by_private_bus
= true;
304 r
= bus_job_allocate_bus_track(j
);
308 return sd_bus_track_add_sender(j
->bus_track
, m
);