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"
26 int sd_bus_emit_signal(
29 const char *interface,
31 const char *types, ...) {
33 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
38 if (!BUS_IS_OPEN(bus->state))
40 if (bus_pid_changed(bus))
43 r = sd_bus_message_new_signal(bus, path, interface, member, &m);
47 if (!isempty(types)) {
51 r = bus_message_append_ap(m, types, ap);
57 return sd_bus_send(bus, m, NULL);
60 int sd_bus_call_method(
62 const char *destination,
64 const char *interface,
67 sd_bus_message **reply,
68 const char *types, ...) {
70 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
76 if (!BUS_IS_OPEN(bus->state))
78 if (bus_pid_changed(bus))
81 r = sd_bus_message_new_method_call(bus, destination, path, interface, member, &m);
85 if (!isempty(types)) {
89 r = bus_message_append_ap(m, types, ap);
95 return sd_bus_send_with_reply_and_block(bus, m, 0, error, reply);
98 int sd_bus_reply_method_return(
100 sd_bus_message *call,
101 const char *types, ...) {
103 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
112 if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
114 if (!BUS_IS_OPEN(bus->state))
116 if (bus_pid_changed(bus))
119 if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
122 r = sd_bus_message_new_method_return(bus, call, &m);
126 if (!isempty(types)) {
130 r = bus_message_append_ap(m, types, ap);
136 return sd_bus_send(bus, m, NULL);
139 int sd_bus_reply_method_error(
141 sd_bus_message *call,
142 const sd_bus_error *e) {
144 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
153 if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
155 if (!sd_bus_error_is_set(e))
157 if (!BUS_IS_OPEN(bus->state))
159 if (bus_pid_changed(bus))
162 if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
165 r = sd_bus_message_new_method_error(bus, call, e, &m);
169 return sd_bus_send(bus, m, NULL);
172 int sd_bus_reply_method_errorf(
174 sd_bus_message *call,
179 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
183 error.name = strdup(name);
187 error.need_free = true;
190 va_start(ap, format);
191 r = vasprintf((char**) &error.message, format, ap);
198 return sd_bus_reply_method_error(bus, call, &error);
201 int sd_bus_get_property(
203 const char *destination,
205 const char *interface,
208 sd_bus_message **reply,
211 sd_bus_message *rep = NULL;
214 if (interface && !interface_name_is_valid(interface))
216 if (!member_name_is_valid(member))
218 if (!signature_is_single(type, false))
223 r = sd_bus_call_method(bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &rep, "ss", strempty(interface), member);
227 r = sd_bus_message_enter_container(rep, 'v', type);
229 sd_bus_message_unref(rep);
237 int sd_bus_set_property(
239 const char *destination,
241 const char *interface,
244 const char *type, ...) {
246 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
250 if (interface && !interface_name_is_valid(interface))
252 if (!member_name_is_valid(member))
254 if (!signature_is_single(type, false))
257 r = sd_bus_message_new_method_call(bus, destination, path, "org.freedesktop.DBus.Properties", "Set", &m);
261 r = sd_bus_message_append(m, "ss", strempty(interface), member);
265 r = sd_bus_message_open_container(m, 'v', type);
270 r = bus_message_append_ap(m, type, ap);
275 r = sd_bus_message_close_container(m);
279 return sd_bus_send_with_reply_and_block(bus, m, 0, error, NULL);