1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "alloc-util.h"
29 #include "selinux-access.h"
30 #include "string-util.h"
32 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type
, job_type
, JobType
);
33 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_state
, job_state
, JobState
);
35 static int property_get_unit(
38 const char *interface
,
40 sd_bus_message
*reply
,
42 sd_bus_error
*error
) {
44 _cleanup_free_
char *p
= NULL
;
51 p
= unit_dbus_path(j
->unit
);
55 return sd_bus_message_append(reply
, "(so)", j
->unit
->id
, p
);
58 int bus_job_method_cancel(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
65 r
= mac_selinux_unit_access_check(j
->unit
, message
, "stop", error
);
69 /* Access is granted to the job owner */
70 if (!sd_bus_track_contains(j
->clients
, sd_bus_message_get_sender(message
))) {
72 /* And for everybody else consult PolicyKit */
73 r
= bus_verify_manage_units_async(j
->unit
->manager
, message
, error
);
77 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
80 job_finish_and_invalidate(j
, JOB_CANCELED
, true);
82 return sd_bus_reply_method_return(message
, NULL
);
85 const sd_bus_vtable bus_job_vtable
[] = {
86 SD_BUS_VTABLE_START(0),
87 SD_BUS_METHOD("Cancel", NULL
, NULL
, bus_job_method_cancel
, SD_BUS_VTABLE_UNPRIVILEGED
),
88 SD_BUS_PROPERTY("Id", "u", NULL
, offsetof(Job
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
89 SD_BUS_PROPERTY("Unit", "(so)", property_get_unit
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
90 SD_BUS_PROPERTY("JobType", "s", property_get_type
, offsetof(Job
, type
), SD_BUS_VTABLE_PROPERTY_CONST
),
91 SD_BUS_PROPERTY("State", "s", property_get_state
, offsetof(Job
, state
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
95 static int send_new_signal(sd_bus
*bus
, void *userdata
) {
96 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
97 _cleanup_free_
char *p
= NULL
;
104 p
= job_dbus_path(j
);
108 r
= sd_bus_message_new_signal(
111 "/org/freedesktop/systemd1",
112 "org.freedesktop.systemd1.Manager",
117 r
= sd_bus_message_append(m
, "uos", j
->id
, p
, j
->unit
->id
);
121 return sd_bus_send(bus
, m
, NULL
);
124 static int send_changed_signal(sd_bus
*bus
, void *userdata
) {
125 _cleanup_free_
char *p
= NULL
;
131 p
= job_dbus_path(j
);
135 return sd_bus_emit_properties_changed(bus
, p
, "org.freedesktop.systemd1.Job", "State", NULL
);
138 void bus_job_send_change_signal(Job
*j
) {
143 if (j
->in_dbus_queue
) {
144 LIST_REMOVE(dbus_queue
, j
->manager
->dbus_job_queue
, j
);
145 j
->in_dbus_queue
= false;
148 r
= bus_foreach_bus(j
->manager
, j
->clients
, j
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
, j
);
150 log_debug_errno(r
, "Failed to send job change signal for %u: %m", j
->id
);
152 j
->sent_dbus_new_signal
= true;
155 static int send_removed_signal(sd_bus
*bus
, void *userdata
) {
156 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
157 _cleanup_free_
char *p
= NULL
;
164 p
= job_dbus_path(j
);
168 r
= sd_bus_message_new_signal(
171 "/org/freedesktop/systemd1",
172 "org.freedesktop.systemd1.Manager",
177 r
= sd_bus_message_append(m
, "uoss", j
->id
, p
, j
->unit
->id
, job_result_to_string(j
->result
));
181 return sd_bus_send(bus
, m
, NULL
);
184 void bus_job_send_removed_signal(Job
*j
) {
189 if (!j
->sent_dbus_new_signal
)
190 bus_job_send_change_signal(j
);
192 r
= bus_foreach_bus(j
->manager
, j
->clients
, send_removed_signal
, j
);
194 log_debug_errno(r
, "Failed to send job remove signal for %u: %m", j
->id
);