1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
7 Copyright 2013 Daniel Mack
8 Copyright 2014 Kay Sievers
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "bus-internal.h"
29 #include "bus-match.h"
30 #include "bus-message.h"
32 #include "synthesize.h"
35 int synthetic_driver_send(sd_bus
*b
, sd_bus_message
*m
) {
41 r
= bus_message_append_sender(m
, "org.freedesktop.DBus");
45 r
= bus_seal_synthetic_message(b
, m
);
49 return sd_bus_send(b
, m
, NULL
);
52 int synthetic_reply_method_error(sd_bus_message
*call
, const sd_bus_error
*e
) {
53 _cleanup_bus_message_unref_ sd_bus_message
*m
= NULL
;
58 if (call
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
)
61 r
= sd_bus_message_new_method_error(call
, &m
, e
);
65 return synthetic_driver_send(call
->bus
, m
);
68 int synthetic_reply_method_errorf(sd_bus_message
*call
, const char *name
, const char *format
, ...) {
69 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
73 bus_error_setfv(&error
, name
, format
, ap
);
76 return synthetic_reply_method_error(call
, &error
);
79 int synthetic_reply_method_errno(sd_bus_message
*call
, int error
, const sd_bus_error
*p
) {
80 _cleanup_bus_error_free_ sd_bus_error berror
= SD_BUS_ERROR_NULL
;
84 if (call
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
)
87 if (sd_bus_error_is_set(p
))
88 return synthetic_reply_method_error(call
, p
);
90 sd_bus_error_set_errno(&berror
, error
);
92 return synthetic_reply_method_error(call
, &berror
);
95 int synthetic_reply_method_errnof(sd_bus_message
*call
, int error
, const char *format
, ...) {
96 _cleanup_bus_error_free_ sd_bus_error berror
= SD_BUS_ERROR_NULL
;
101 if (call
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
)
104 va_start(ap
, format
);
105 sd_bus_error_set_errnofv(&berror
, error
, format
, ap
);
108 return synthetic_reply_method_error(call
, &berror
);
111 int synthetic_reply_method_return(sd_bus_message
*call
, const char *types
, ...) {
112 _cleanup_bus_message_unref_ sd_bus_message
*m
= NULL
;
117 if (call
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
)
120 r
= sd_bus_message_new_method_return(call
, &m
);
124 if (!isempty(types
)) {
128 r
= bus_message_append_ap(m
, types
, ap
);
134 return synthetic_driver_send(call
->bus
, m
);
137 int synthetic_reply_method_return_strv(sd_bus_message
*call
, char **l
) {
138 _cleanup_bus_message_unref_ sd_bus_message
*m
= NULL
;
143 if (call
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
)
146 r
= sd_bus_message_new_method_return(call
, &m
);
148 return synthetic_reply_method_errno(call
, r
, NULL
);
150 r
= sd_bus_message_append_strv(m
, l
);
152 return synthetic_reply_method_errno(call
, r
, NULL
);
154 return synthetic_driver_send(call
->bus
, m
);
157 int synthesize_name_acquired(Proxy
*p
, sd_bus
*a
, sd_bus
*b
, sd_bus_message
*m
) {
158 _cleanup_bus_message_unref_ sd_bus_message
*n
= NULL
;
159 const char *name
, *old_owner
, *new_owner
;
167 /* If we get NameOwnerChanged for our own name, we need to
168 * synthesize NameLost/NameAcquired, since socket clients need
169 * that, even though it is obsoleted on kdbus */
174 if (!sd_bus_message_is_signal(m
, "org.freedesktop.DBus", "NameOwnerChanged") ||
175 !streq_ptr(m
->path
, "/org/freedesktop/DBus") ||
176 !streq_ptr(m
->sender
, "org.freedesktop.DBus"))
179 r
= sd_bus_message_read(m
, "sss", &name
, &old_owner
, &new_owner
);
183 r
= sd_bus_message_rewind(m
, true);
187 if (streq(old_owner
, a
->unique_name
)) {
189 r
= sd_bus_message_new_signal(
192 "/org/freedesktop/DBus",
193 "org.freedesktop.DBus",
196 } else if (streq(new_owner
, a
->unique_name
)) {
198 r
= sd_bus_message_new_signal(
201 "/org/freedesktop/DBus",
202 "org.freedesktop.DBus",
210 r
= sd_bus_message_append(n
, "s", name
);
214 r
= bus_message_append_sender(n
, "org.freedesktop.DBus");
218 r
= sd_bus_message_set_destination(n
, a
->unique_name
);
222 r
= bus_seal_synthetic_message(b
, n
);
226 return sd_bus_send(b
, n
, NULL
);