]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/bus-proxyd/synthesize.c
bus-proxyd: move synthesize_name_acquired()
[thirdparty/systemd.git] / src / bus-proxyd / synthesize.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 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
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <stddef.h>
29
30 #include "log.h"
31 #include "util.h"
32 #include "sd-bus.h"
33 #include "bus-internal.h"
34 #include "bus-message.h"
35 #include "bus-util.h"
36 #include "strv.h"
37 #include "def.h"
38 #include "bus-control.h"
39 #include "synthesize.h"
40
41 static int synthetic_driver_send(sd_bus *b, sd_bus_message *m) {
42 int r;
43
44 assert(b);
45 assert(m);
46
47 r = bus_message_append_sender(m, "org.freedesktop.DBus");
48 if (r < 0)
49 return r;
50
51 r = bus_seal_synthetic_message(b, m);
52 if (r < 0)
53 return r;
54
55 return sd_bus_send(b, m, NULL);
56 }
57
58 int synthetic_reply_method_error(sd_bus_message *call, const sd_bus_error *e) {
59 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
60 int r;
61
62 assert(call);
63
64 if (call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
65 return 0;
66
67 r = sd_bus_message_new_method_error(call, &m, e);
68 if (r < 0)
69 return r;
70
71 return synthetic_driver_send(call->bus, m);
72 }
73
74 int synthetic_reply_method_errorf(sd_bus_message *call, const char *name, const char *format, ...) {
75 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
76 va_list ap;
77
78 va_start(ap, format);
79 bus_error_setfv(&error, name, format, ap);
80 va_end(ap);
81
82 return synthetic_reply_method_error(call, &error);
83 }
84
85 int synthetic_reply_method_errno(sd_bus_message *call, int error, const sd_bus_error *p) {
86
87 _cleanup_bus_error_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
88
89 assert(call);
90
91 if (call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
92 return 0;
93
94 if (sd_bus_error_is_set(p))
95 return synthetic_reply_method_error(call, p);
96
97 sd_bus_error_set_errno(&berror, error);
98
99 return synthetic_reply_method_error(call, &berror);
100 }
101
102 int synthetic_reply_method_return(sd_bus_message *call, const char *types, ...) {
103 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
104 int r;
105
106 assert(call);
107
108 if (call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
109 return 0;
110
111 r = sd_bus_message_new_method_return(call, &m);
112 if (r < 0)
113 return r;
114
115 if (!isempty(types)) {
116 va_list ap;
117
118 va_start(ap, types);
119 r = bus_message_append_ap(m, types, ap);
120 va_end(ap);
121 if (r < 0)
122 return r;
123 }
124
125 return synthetic_driver_send(call->bus, m);
126 }
127
128 int synthetic_reply_return_strv(sd_bus_message *call, char **l) {
129 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
130 int r;
131
132 assert(call);
133
134 if (call->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
135 return 0;
136
137 r = sd_bus_message_new_method_return(call, &m);
138 if (r < 0)
139 return synthetic_reply_method_errno(call, r, NULL);
140
141 r = sd_bus_message_append_strv(m, l);
142 if (r < 0)
143 return synthetic_reply_method_errno(call, r, NULL);
144
145 return synthetic_driver_send(call->bus, m);
146 }
147
148 int synthesize_name_acquired(sd_bus *a, sd_bus *b, sd_bus_message *m) {
149 _cleanup_bus_message_unref_ sd_bus_message *n = NULL;
150 const char *name, *old_owner, *new_owner;
151 int r;
152
153 assert(a);
154 assert(b);
155 assert(m);
156
157 /* If we get NameOwnerChanged for our own name, we need to
158 * synthesize NameLost/NameAcquired, since socket clients need
159 * that, even though it is obsoleted on kdbus */
160
161 if (!a->is_kernel)
162 return 0;
163
164 if (!sd_bus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged") ||
165 !streq_ptr(m->path, "/org/freedesktop/DBus") ||
166 !streq_ptr(m->sender, "org.freedesktop.DBus"))
167 return 0;
168
169 r = sd_bus_message_read(m, "sss", &name, &old_owner, &new_owner);
170 if (r < 0)
171 return r;
172
173 r = sd_bus_message_rewind(m, true);
174 if (r < 0)
175 return r;
176
177 if (streq(old_owner, a->unique_name)) {
178
179 r = sd_bus_message_new_signal(
180 b,
181 &n,
182 "/org/freedesktop/DBus",
183 "org.freedesktop.DBus",
184 "NameLost");
185
186 } else if (streq(new_owner, a->unique_name)) {
187
188 r = sd_bus_message_new_signal(
189 b,
190 &n,
191 "/org/freedesktop/DBus",
192 "org.freedesktop.DBus",
193 "NameAcquired");
194 } else
195 return 0;
196
197 if (r < 0)
198 return r;
199
200 r = sd_bus_message_append(n, "s", name);
201 if (r < 0)
202 return r;
203
204 r = bus_message_append_sender(n, "org.freedesktop.DBus");
205 if (r < 0)
206 return r;
207
208 r = bus_seal_synthetic_message(b, n);
209 if (r < 0)
210 return r;
211
212 return sd_bus_send(b, n, NULL);
213 }