chiark / gitweb /
36d6dee75ff184af62dab405f6108d9bfe7fba58
[elogind.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 "log.h"
23 #include "sd-bus.h"
24 #include "selinux-access.h"
25 #include "job.h"
26 #include "dbus-job.h"
27 #include "dbus.h"
28
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);
31
32 static int verify_sys_admin_or_owner_sync(sd_bus_message *message, Job *j, sd_bus_error *error) {
33         int r;
34
35         if (sd_bus_track_contains(j->clients, sd_bus_message_get_sender(message)))
36                 return 0; /* One of the job owners is calling us */
37
38         r = sd_bus_query_sender_privilege(message, CAP_SYS_ADMIN);
39         if (r < 0)
40                 return r;
41         if (r == 0)
42                 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Access denied to perform action");
43
44         /* Root has called us */
45         return 0;
46 }
47
48 static int property_get_unit(
49                 sd_bus *bus,
50                 const char *path,
51                 const char *interface,
52                 const char *property,
53                 sd_bus_message *reply,
54                 void *userdata,
55                 sd_bus_error *error) {
56
57         _cleanup_free_ char *p = NULL;
58         Job *j = userdata;
59
60         assert(bus);
61         assert(reply);
62         assert(j);
63
64         p = unit_dbus_path(j->unit);
65         if (!p)
66                 return -ENOMEM;
67
68         return sd_bus_message_append(reply, "(so)", j->unit->id, p);
69 }
70
71 int bus_job_method_cancel(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
72         Job *j = userdata;
73         int r;
74
75         assert(bus);
76         assert(message);
77         assert(j);
78
79         r = verify_sys_admin_or_owner_sync(message, j, error);
80         if (r < 0)
81                 return r;
82
83         r = mac_selinux_unit_access_check(j->unit, message, "stop", error);
84         if (r < 0)
85                 return r;
86
87         job_finish_and_invalidate(j, JOB_CANCELED, true);
88
89         return sd_bus_reply_method_return(message, NULL);
90 }
91
92 const sd_bus_vtable bus_job_vtable[] = {
93         SD_BUS_VTABLE_START(0),
94         SD_BUS_METHOD("Cancel", NULL, NULL, bus_job_method_cancel, SD_BUS_VTABLE_UNPRIVILEGED),
95         SD_BUS_PROPERTY("Id", "u", NULL, offsetof(Job, id), SD_BUS_VTABLE_PROPERTY_CONST),
96         SD_BUS_PROPERTY("Unit", "(so)", property_get_unit, 0, SD_BUS_VTABLE_PROPERTY_CONST),
97         SD_BUS_PROPERTY("JobType", "s", property_get_type, offsetof(Job, type), SD_BUS_VTABLE_PROPERTY_CONST),
98         SD_BUS_PROPERTY("State", "s", property_get_state, offsetof(Job, state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
99         SD_BUS_VTABLE_END
100 };
101
102 static int send_new_signal(sd_bus *bus, void *userdata) {
103         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
104         _cleanup_free_ char *p = NULL;
105         Job *j = userdata;
106         int r;
107
108         assert(bus);
109         assert(j);
110
111         p = job_dbus_path(j);
112         if (!p)
113                 return -ENOMEM;
114
115         r = sd_bus_message_new_signal(
116                         bus,
117                         &m,
118                         "/org/freedesktop/systemd1",
119                         "org.freedesktop.systemd1.Manager",
120                         "JobNew");
121         if (r < 0)
122                 return r;
123
124         r = sd_bus_message_append(m, "uos", j->id, p, j->unit->id);
125         if (r < 0)
126                 return r;
127
128         return sd_bus_send(bus, m, NULL);
129 }
130
131 static int send_changed_signal(sd_bus *bus, void *userdata) {
132         _cleanup_free_ char *p = NULL;
133         Job *j = userdata;
134
135         assert(bus);
136         assert(j);
137
138         p = job_dbus_path(j);
139         if (!p)
140                 return -ENOMEM;
141
142         return sd_bus_emit_properties_changed(bus, p, "org.freedesktop.systemd1.Job", "State", NULL);
143 }
144
145 void bus_job_send_change_signal(Job *j) {
146         int r;
147
148         assert(j);
149
150         if (j->in_dbus_queue) {
151                 LIST_REMOVE(dbus_queue, j->manager->dbus_job_queue, j);
152                 j->in_dbus_queue = false;
153         }
154
155         r = bus_foreach_bus(j->manager, j->clients, j->sent_dbus_new_signal ? send_changed_signal : send_new_signal, j);
156         if (r < 0)
157                 log_debug_errno(-r, "Failed to send job change signal for %u: %m", j->id);
158
159         j->sent_dbus_new_signal = true;
160 }
161
162 static int send_removed_signal(sd_bus *bus, void *userdata) {
163         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
164         _cleanup_free_ char *p = NULL;
165         Job *j = userdata;
166         int r;
167
168         assert(bus);
169         assert(j);
170
171         p = job_dbus_path(j);
172         if (!p)
173                 return -ENOMEM;
174
175         r = sd_bus_message_new_signal(
176                         bus,
177                         &m,
178                         "/org/freedesktop/systemd1",
179                         "org.freedesktop.systemd1.Manager",
180                         "JobRemoved");
181         if (r < 0)
182                 return r;
183
184         r = sd_bus_message_append(m, "uoss", j->id, p, j->unit->id, job_result_to_string(j->result));
185         if (r < 0)
186                 return r;
187
188         return sd_bus_send(bus, m, NULL);
189 }
190
191 void bus_job_send_removed_signal(Job *j) {
192         int r;
193
194         assert(j);
195
196         if (!j->sent_dbus_new_signal)
197                 bus_job_send_change_signal(j);
198
199         r = bus_foreach_bus(j->manager, j->clients, send_removed_signal, j);
200         if (r < 0)
201                 log_debug_errno(-r, "Failed to send job remove signal for %u: %m", j->id);
202 }