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 "selinux-access.h"
27 #include "dbus-client-track.h"
29 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type
, job_type
, JobType
);
30 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_state
, job_state
, JobState
);
32 static int property_get_unit(
35 const char *interface
,
37 sd_bus_message
*reply
,
41 _cleanup_free_
char *p
= NULL
;
48 p
= unit_dbus_path(j
->unit
);
52 return sd_bus_message_append(reply
, "(so)", j
->unit
->id
, p
);
55 static int method_cancel(sd_bus
*bus
, sd_bus_message
*message
, void *userdata
) {
62 SELINUX_UNIT_ACCESS_CHECK(j
->unit
, bus
, message
, "stop");
63 job_finish_and_invalidate(j
, JOB_CANCELED
, true);
65 return sd_bus_reply_method_return(message
, NULL
);
68 const sd_bus_vtable bus_job_vtable
[] = {
69 SD_BUS_VTABLE_START(0),
70 SD_BUS_METHOD("Cancel", NULL
, NULL
, method_cancel
, 0),
71 SD_BUS_PROPERTY("Id", "u", NULL
, offsetof(Job
, id
), 0),
72 SD_BUS_PROPERTY("Unit", "(so)", property_get_unit
, 0, 0),
73 SD_BUS_PROPERTY("JobType", "s", property_get_type
, offsetof(Job
, type
), 0),
74 SD_BUS_PROPERTY("State", "s", property_get_state
, offsetof(Job
, state
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
78 static int foreach_client(Job
*j
, int (*send_message
)(sd_bus
*bus
, const char *name
, Job
*j
)) {
79 BusTrackedClient
*one_destination
= NULL
;
88 n
= set_size(j
->manager
->subscribed
);
89 m
= set_size(j
->subscribed
);
95 one_destination
= set_first(j
->manager
->subscribed
);
96 else if (n
== 0 && m
== 1)
97 one_destination
= set_first(j
->subscribed
);
99 one_destination
= NULL
;
102 return send_message(one_destination
->bus
, isempty(one_destination
->name
) ? NULL
: one_destination
->name
, j
);
104 /* Send to everybody */
105 SET_FOREACH(b
, j
->manager
->private_buses
, i
) {
106 r
= send_message(b
, NULL
, j
);
111 if (j
->manager
->api_bus
)
112 return send_message(j
->manager
->api_bus
, NULL
, j
);
117 static int send_new_signal(sd_bus
*bus
, const char *destination
, Job
*j
) {
118 _cleanup_bus_message_unref_ sd_bus_message
*m
= NULL
;
119 _cleanup_free_
char *p
= NULL
;
125 p
= job_dbus_path(j
);
129 r
= sd_bus_message_new_signal(
131 "/org/freedesktop/systemd1",
132 "org.freedesktop.systemd1.Manager",
138 r
= sd_bus_message_append(m
, "uos", j
->id
, p
, j
->unit
->id
);
142 return sd_bus_send_to(bus
, m
, destination
, NULL
);
145 static int send_changed_signal(sd_bus
*bus
, const char *destination
, Job
*j
) {
146 _cleanup_free_
char *p
= NULL
;
151 p
= job_dbus_path(j
);
155 return sd_bus_emit_properties_changed(bus
, p
, "org.freedesktop.systemd1.Job", "State", NULL
);
158 void bus_job_send_change_signal(Job
*j
) {
163 if (j
->in_dbus_queue
) {
164 LIST_REMOVE(dbus_queue
, j
->manager
->dbus_job_queue
, j
);
165 j
->in_dbus_queue
= false;
168 r
= foreach_client(j
, j
->sent_dbus_new_signal
? send_changed_signal
: send_new_signal
);
170 log_warning("Failed to send job change signal for %u/%s: %s", j
->id
, j
->unit
->id
, strerror(-r
));
172 j
->sent_dbus_new_signal
= true;
175 static int send_removed_signal(sd_bus
*bus
, const char *destination
, Job
*j
) {
176 _cleanup_bus_message_unref_ sd_bus_message
*m
= NULL
;
177 _cleanup_free_
char *p
= NULL
;
183 p
= job_dbus_path(j
);
188 r
= sd_bus_message_new_signal(
190 "/org/freedesktop/systemd1",
191 "org.freedesktop.systemd1.Manager",
197 r
= sd_bus_message_append(m
, "uoss", j
->id
, p
, j
->unit
->id
, job_result_to_string(j
->result
));
201 return sd_bus_send_to(bus
, m
, destination
, NULL
);
204 void bus_job_send_removed_signal(Job
*j
) {
209 if (!j
->sent_dbus_new_signal
)
210 bus_job_send_change_signal(j
);
212 r
= foreach_client(j
, send_removed_signal
);
214 log_warning("Failed to send job removal signal for %u/%s: %s", j
->id
, j
->unit
->id
, strerror(-r
));