1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 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/>.
22 #include "bus-internal.h"
23 #include "bus-message.h"
24 #include "bus-signature.h"
27 int sd_bus_emit_signal(
30 const char *interface,
32 const char *types, ...) {
34 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
37 assert_return(bus, -EINVAL);
38 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
39 assert_return(!bus_pid_changed(bus), -ECHILD);
41 r = sd_bus_message_new_signal(bus, path, interface, member, &m);
45 if (!isempty(types)) {
49 r = bus_message_append_ap(m, types, ap);
55 return sd_bus_send(bus, m, NULL);
58 int sd_bus_call_method(
60 const char *destination,
62 const char *interface,
65 sd_bus_message **reply,
66 const char *types, ...) {
68 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
71 assert_return(bus, -EINVAL);
72 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
73 assert_return(!bus_pid_changed(bus), -ECHILD);
75 r = sd_bus_message_new_method_call(bus, destination, path, interface, member, &m);
79 if (!isempty(types)) {
83 r = bus_message_append_ap(m, types, ap);
89 return sd_bus_send_with_reply_and_block(bus, m, 0, error, reply);
92 int sd_bus_reply_method_return(
95 const char *types, ...) {
97 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
100 assert_return(bus, -EINVAL);
101 assert_return(call, -EINVAL);
102 assert_return(call->sealed, -EPERM);
103 assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
104 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
105 assert_return(!bus_pid_changed(bus), -ECHILD);
107 if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
110 r = sd_bus_message_new_method_return(bus, call, &m);
114 if (!isempty(types)) {
118 r = bus_message_append_ap(m, types, ap);
124 return sd_bus_send(bus, m, NULL);
127 int sd_bus_reply_method_error(
129 sd_bus_message *call,
130 const sd_bus_error *e) {
132 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
135 assert_return(bus, -EINVAL);
136 assert_return(call, -EINVAL);
137 assert_return(call->sealed, -EPERM);
138 assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
139 assert_return(sd_bus_error_is_set(e), -EINVAL);
140 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
141 assert_return(!bus_pid_changed(bus), -ECHILD);
143 if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
146 r = sd_bus_message_new_method_error(bus, call, e, &m);
150 return sd_bus_send(bus, m, NULL);
153 int sd_bus_reply_method_errorf(
155 sd_bus_message *call,
160 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
164 assert_return(bus, -EINVAL);
165 assert_return(call, -EINVAL);
166 assert_return(call->sealed, -EPERM);
167 assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
168 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
169 assert_return(!bus_pid_changed(bus), -ECHILD);
171 if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
174 va_start(ap, format);
175 r = bus_error_setfv(&error, name, format, ap);
181 return sd_bus_reply_method_error(bus, call, &error);
184 int sd_bus_reply_method_errno(
186 sd_bus_message *call,
188 const sd_bus_error *p) {
190 _cleanup_bus_error_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
192 assert_return(bus, -EINVAL);
193 assert_return(call, -EINVAL);
194 assert_return(call->sealed, -EPERM);
195 assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
196 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
197 assert_return(!bus_pid_changed(bus), -ECHILD);
199 if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
202 if (sd_bus_error_is_set(p))
203 return sd_bus_reply_method_error(bus, call, p);
205 sd_bus_error_set_errno(&berror, error);
207 return sd_bus_reply_method_error(bus, call, &berror);
210 int sd_bus_reply_method_errnof(
212 sd_bus_message *call,
217 _cleanup_bus_error_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
220 assert_return(bus, -EINVAL);
221 assert_return(call, -EINVAL);
222 assert_return(call->sealed, -EPERM);
223 assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
224 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
225 assert_return(!bus_pid_changed(bus), -ECHILD);
227 if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
230 va_start(ap, format);
231 bus_error_set_errnofv(&berror, error, format, ap);
234 return sd_bus_reply_method_error(bus, call, &berror);
237 int sd_bus_get_property(
239 const char *destination,
241 const char *interface,
244 sd_bus_message **reply,
247 sd_bus_message *rep = NULL;
250 assert_return(bus, -EINVAL);
251 assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL);
252 assert_return(member_name_is_valid(member), -EINVAL);
253 assert_return(reply, -EINVAL);
254 assert_return(signature_is_single(type, false), -EINVAL);
255 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
256 assert_return(!bus_pid_changed(bus), -ECHILD);
258 r = sd_bus_call_method(bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &rep, "ss", strempty(interface), member);
262 r = sd_bus_message_enter_container(rep, 'v', type);
264 sd_bus_message_unref(rep);
272 int sd_bus_set_property(
274 const char *destination,
276 const char *interface,
279 const char *type, ...) {
281 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
285 assert_return(bus, -EINVAL);
286 assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL);
287 assert_return(member_name_is_valid(member), -EINVAL);
288 assert_return(signature_is_single(type, false), -EINVAL);
289 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
290 assert_return(!bus_pid_changed(bus), -ECHILD);
292 r = sd_bus_message_new_method_call(bus, destination, path, "org.freedesktop.DBus.Properties", "Set", &m);
296 r = sd_bus_message_append(m, "ss", strempty(interface), member);
300 r = sd_bus_message_open_container(m, 'v', type);
305 r = bus_message_append_ap(m, type, ap);
310 r = sd_bus_message_close_container(m);
314 return sd_bus_send_with_reply_and_block(bus, m, 0, error, NULL);