]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/bus-proxyd/synthesize.c
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
[thirdparty/systemd.git] / src / bus-proxyd / synthesize.c
CommitLineData
f3c47246
DM
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 Copyright 2013 Daniel Mack
8 Copyright 2014 Kay Sievers
9
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.
14
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.
19
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/>.
22***/
23
f3c47246
DM
24#include <stddef.h>
25
f3c47246 26#include "sd-bus.h"
07630cea 27
f3c47246 28#include "bus-internal.h"
07630cea 29#include "bus-match.h"
f3c47246
DM
30#include "bus-message.h"
31#include "bus-util.h"
f3c47246 32#include "synthesize.h"
07630cea 33#include "util.h"
f3c47246 34
52fa7a3a 35int synthetic_driver_send(sd_bus *b, sd_bus_message *m) {
f3c47246
DM
36 int r;
37
38 assert(b);
39 assert(m);
40
41 r = bus_message_append_sender(m, "org.freedesktop.DBus");
42 if (r < 0)
43 return r;
44
45 r = bus_seal_synthetic_message(b, m);
46 if (r < 0)
47 return r;
48
49 return sd_bus_send(b, m, NULL);
50}
51
52int synthetic_reply_method_error(sd_bus_message *call, const sd_bus_error *e) {
4afd3348 53 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
f3c47246
DM
54 int r;
55
56 assert(call);
57
58 if (call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
59 return 0;
60
61 r = sd_bus_message_new_method_error(call, &m, e);
62 if (r < 0)
63 return r;
64
65 return synthetic_driver_send(call->bus, m);
66}
67
68int synthetic_reply_method_errorf(sd_bus_message *call, const char *name, const char *format, ...) {
4afd3348 69 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f3c47246
DM
70 va_list ap;
71
72 va_start(ap, format);
73 bus_error_setfv(&error, name, format, ap);
74 va_end(ap);
75
76 return synthetic_reply_method_error(call, &error);
77}
78
79int synthetic_reply_method_errno(sd_bus_message *call, int error, const sd_bus_error *p) {
4afd3348 80 _cleanup_(sd_bus_error_free) sd_bus_error berror = SD_BUS_ERROR_NULL;
f3c47246
DM
81
82 assert(call);
83
84 if (call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
85 return 0;
86
87 if (sd_bus_error_is_set(p))
88 return synthetic_reply_method_error(call, p);
89
90 sd_bus_error_set_errno(&berror, error);
91
92 return synthetic_reply_method_error(call, &berror);
93}
94
5f6cb091 95int synthetic_reply_method_errnof(sd_bus_message *call, int error, const char *format, ...) {
4afd3348 96 _cleanup_(sd_bus_error_free) sd_bus_error berror = SD_BUS_ERROR_NULL;
5f6cb091
LP
97 va_list ap;
98
99 assert(call);
100
101 if (call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
102 return 0;
103
104 va_start(ap, format);
105 sd_bus_error_set_errnofv(&berror, error, format, ap);
106 va_end(ap);
107
108 return synthetic_reply_method_error(call, &berror);
109}
110
f3c47246 111int synthetic_reply_method_return(sd_bus_message *call, const char *types, ...) {
4afd3348 112 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
f3c47246
DM
113 int r;
114
115 assert(call);
116
117 if (call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
118 return 0;
119
120 r = sd_bus_message_new_method_return(call, &m);
121 if (r < 0)
122 return r;
123
124 if (!isempty(types)) {
125 va_list ap;
126
127 va_start(ap, types);
128 r = bus_message_append_ap(m, types, ap);
129 va_end(ap);
130 if (r < 0)
131 return r;
132 }
133
134 return synthetic_driver_send(call->bus, m);
135}
136
1433efd2 137int synthetic_reply_method_return_strv(sd_bus_message *call, char **l) {
4afd3348 138 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
f3c47246
DM
139 int r;
140
141 assert(call);
142
143 if (call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
144 return 0;
145
146 r = sd_bus_message_new_method_return(call, &m);
147 if (r < 0)
148 return synthetic_reply_method_errno(call, r, NULL);
149
150 r = sd_bus_message_append_strv(m, l);
151 if (r < 0)
152 return synthetic_reply_method_errno(call, r, NULL);
153
154 return synthetic_driver_send(call->bus, m);
155}
5e2de0eb 156
e23bc0e7 157int synthesize_name_acquired(Proxy *p, sd_bus *a, sd_bus *b, sd_bus_message *m) {
4afd3348 158 _cleanup_(sd_bus_message_unrefp) sd_bus_message *n = NULL;
5e2de0eb
DM
159 const char *name, *old_owner, *new_owner;
160 int r;
161
e23bc0e7 162 assert(p);
5e2de0eb
DM
163 assert(a);
164 assert(b);
165 assert(m);
166
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 */
170
171 if (!a->is_kernel)
172 return 0;
173
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"))
177 return 0;
178
179 r = sd_bus_message_read(m, "sss", &name, &old_owner, &new_owner);
180 if (r < 0)
181 return r;
182
183 r = sd_bus_message_rewind(m, true);
184 if (r < 0)
185 return r;
186
187 if (streq(old_owner, a->unique_name)) {
188
189 r = sd_bus_message_new_signal(
190 b,
191 &n,
192 "/org/freedesktop/DBus",
193 "org.freedesktop.DBus",
194 "NameLost");
195
196 } else if (streq(new_owner, a->unique_name)) {
197
198 r = sd_bus_message_new_signal(
199 b,
200 &n,
201 "/org/freedesktop/DBus",
202 "org.freedesktop.DBus",
203 "NameAcquired");
204 } else
205 return 0;
206
207 if (r < 0)
208 return r;
209
210 r = sd_bus_message_append(n, "s", name);
211 if (r < 0)
212 return r;
213
214 r = bus_message_append_sender(n, "org.freedesktop.DBus");
215 if (r < 0)
216 return r;
217
e3c57a86
DH
218 r = sd_bus_message_set_destination(n, a->unique_name);
219 if (r < 0)
220 return r;
221
5e2de0eb
DM
222 r = bus_seal_synthetic_message(b, n);
223 if (r < 0)
224 return r;
225
a87d2ed1 226 return sd_bus_send(b, n, NULL);
5e2de0eb 227}