]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/dbus-job.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / core / dbus-job.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include "sd-bus.h"
23
24 #include "dbus.h"
25 #include "job.h"
26 #include "log.h"
27 #include "selinux-access.h"
28 #include "string-util.h"
29 #include "dbus-job.h"
30
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);
33
34 static int property_get_unit(
35 sd_bus *bus,
36 const char *path,
37 const char *interface,
38 const char *property,
39 sd_bus_message *reply,
40 void *userdata,
41 sd_bus_error *error) {
42
43 _cleanup_free_ char *p = NULL;
44 Job *j = userdata;
45
46 assert(bus);
47 assert(reply);
48 assert(j);
49
50 p = unit_dbus_path(j->unit);
51 if (!p)
52 return -ENOMEM;
53
54 return sd_bus_message_append(reply, "(so)", j->unit->id, p);
55 }
56
57 int bus_job_method_cancel(sd_bus_message *message, void *userdata, sd_bus_error *error) {
58 Job *j = userdata;
59 int r;
60
61 assert(message);
62 assert(j);
63
64 r = mac_selinux_unit_access_check(j->unit, message, "stop", error);
65 if (r < 0)
66 return r;
67
68 /* Access is granted to the job owner */
69 if (!sd_bus_track_contains(j->clients, sd_bus_message_get_sender(message))) {
70
71 /* And for everybody else consult PolicyKit */
72 r = bus_verify_manage_units_async(j->unit->manager, message, error);
73 if (r < 0)
74 return r;
75 if (r == 0)
76 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
77 }
78
79 job_finish_and_invalidate(j, JOB_CANCELED, true);
80
81 return sd_bus_reply_method_return(message, NULL);
82 }
83
84 const sd_bus_vtable bus_job_vtable[] = {
85 SD_BUS_VTABLE_START(0),
86 SD_BUS_METHOD("Cancel", NULL, NULL, bus_job_method_cancel, SD_BUS_VTABLE_UNPRIVILEGED),
87 SD_BUS_PROPERTY("Id", "u", NULL, offsetof(Job, id), SD_BUS_VTABLE_PROPERTY_CONST),
88 SD_BUS_PROPERTY("Unit", "(so)", property_get_unit, 0, SD_BUS_VTABLE_PROPERTY_CONST),
89 SD_BUS_PROPERTY("JobType", "s", property_get_type, offsetof(Job, type), SD_BUS_VTABLE_PROPERTY_CONST),
90 SD_BUS_PROPERTY("State", "s", property_get_state, offsetof(Job, state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
91 SD_BUS_VTABLE_END
92 };
93
94 static int send_new_signal(sd_bus *bus, void *userdata) {
95 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
96 _cleanup_free_ char *p = NULL;
97 Job *j = userdata;
98 int r;
99
100 assert(bus);
101 assert(j);
102
103 p = job_dbus_path(j);
104 if (!p)
105 return -ENOMEM;
106
107 r = sd_bus_message_new_signal(
108 bus,
109 &m,
110 "/org/freedesktop/systemd1",
111 "org.freedesktop.systemd1.Manager",
112 "JobNew");
113 if (r < 0)
114 return r;
115
116 r = sd_bus_message_append(m, "uos", j->id, p, j->unit->id);
117 if (r < 0)
118 return r;
119
120 return sd_bus_send(bus, m, NULL);
121 }
122
123 static int send_changed_signal(sd_bus *bus, void *userdata) {
124 _cleanup_free_ char *p = NULL;
125 Job *j = userdata;
126
127 assert(bus);
128 assert(j);
129
130 p = job_dbus_path(j);
131 if (!p)
132 return -ENOMEM;
133
134 return sd_bus_emit_properties_changed(bus, p, "org.freedesktop.systemd1.Job", "State", NULL);
135 }
136
137 void bus_job_send_change_signal(Job *j) {
138 int r;
139
140 assert(j);
141
142 if (j->in_dbus_queue) {
143 LIST_REMOVE(dbus_queue, j->manager->dbus_job_queue, j);
144 j->in_dbus_queue = false;
145 }
146
147 r = bus_foreach_bus(j->manager, j->clients, j->sent_dbus_new_signal ? send_changed_signal : send_new_signal, j);
148 if (r < 0)
149 log_debug_errno(r, "Failed to send job change signal for %u: %m", j->id);
150
151 j->sent_dbus_new_signal = true;
152 }
153
154 static int send_removed_signal(sd_bus *bus, void *userdata) {
155 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
156 _cleanup_free_ char *p = NULL;
157 Job *j = userdata;
158 int r;
159
160 assert(bus);
161 assert(j);
162
163 p = job_dbus_path(j);
164 if (!p)
165 return -ENOMEM;
166
167 r = sd_bus_message_new_signal(
168 bus,
169 &m,
170 "/org/freedesktop/systemd1",
171 "org.freedesktop.systemd1.Manager",
172 "JobRemoved");
173 if (r < 0)
174 return r;
175
176 r = sd_bus_message_append(m, "uoss", j->id, p, j->unit->id, job_result_to_string(j->result));
177 if (r < 0)
178 return r;
179
180 return sd_bus_send(bus, m, NULL);
181 }
182
183 void bus_job_send_removed_signal(Job *j) {
184 int r;
185
186 assert(j);
187
188 if (!j->sent_dbus_new_signal)
189 bus_job_send_change_signal(j);
190
191 r = bus_foreach_bus(j->manager, j->clients, send_removed_signal, j);
192 if (r < 0)
193 log_debug_errno(r, "Failed to send job remove signal for %u: %m", j->id);
194 }