]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/test-bus-server.c
treewide: auto-convert the simple cases to log_*_errno()
[thirdparty/systemd.git] / src / libsystemd / sd-bus / test-bus-server.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2013 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <pthread.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27
28 #include "log.h"
29 #include "util.h"
30 #include "macro.h"
31
32 #include "sd-bus.h"
33 #include "bus-internal.h"
34 #include "bus-message.h"
35 #include "bus-util.h"
36
37 struct context {
38 int fds[2];
39
40 bool client_negotiate_unix_fds;
41 bool server_negotiate_unix_fds;
42
43 bool client_anonymous_auth;
44 bool server_anonymous_auth;
45 };
46
47 static void *server(void *p) {
48 struct context *c = p;
49 sd_bus *bus = NULL;
50 sd_id128_t id;
51 bool quit = false;
52 int r;
53
54 assert_se(sd_id128_randomize(&id) >= 0);
55
56 assert_se(sd_bus_new(&bus) >= 0);
57 assert_se(sd_bus_set_fd(bus, c->fds[0], c->fds[0]) >= 0);
58 assert_se(sd_bus_set_server(bus, 1, id) >= 0);
59 assert_se(sd_bus_set_anonymous(bus, c->server_anonymous_auth) >= 0);
60 assert_se(sd_bus_negotiate_fds(bus, c->server_negotiate_unix_fds) >= 0);
61 assert_se(sd_bus_start(bus) >= 0);
62
63 while (!quit) {
64 _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
65
66 r = sd_bus_process(bus, &m);
67 if (r < 0) {
68 log_error_errno(-r, "Failed to process requests: %m");
69 goto fail;
70 }
71
72 if (r == 0) {
73 r = sd_bus_wait(bus, (uint64_t) -1);
74 if (r < 0) {
75 log_error_errno(-r, "Failed to wait: %m");
76 goto fail;
77 }
78
79 continue;
80 }
81
82 if (!m)
83 continue;
84
85 log_info("Got message! member=%s", strna(sd_bus_message_get_member(m)));
86
87 if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Exit")) {
88
89 assert_se((sd_bus_can_send(bus, 'h') >= 1) == (c->server_negotiate_unix_fds && c->client_negotiate_unix_fds));
90
91 r = sd_bus_message_new_method_return(m, &reply);
92 if (r < 0) {
93 log_error_errno(-r, "Failed to allocate return: %m");
94 goto fail;
95 }
96
97 quit = true;
98
99 } else if (sd_bus_message_is_method_call(m, NULL, NULL)) {
100 r = sd_bus_message_new_method_error(
101 m,
102 &reply,
103 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method."));
104 if (r < 0) {
105 log_error_errno(-r, "Failed to allocate return: %m");
106 goto fail;
107 }
108 }
109
110 if (reply) {
111 r = sd_bus_send(bus, reply, NULL);
112 if (r < 0) {
113 log_error_errno(-r, "Failed to send reply: %m");
114 goto fail;
115 }
116 }
117 }
118
119 r = 0;
120
121 fail:
122 if (bus) {
123 sd_bus_flush(bus);
124 sd_bus_unref(bus);
125 }
126
127 return INT_TO_PTR(r);
128 }
129
130 static int client(struct context *c) {
131 _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
132 _cleanup_bus_unref_ sd_bus *bus = NULL;
133 sd_bus_error error = SD_BUS_ERROR_NULL;
134 int r;
135
136 assert_se(sd_bus_new(&bus) >= 0);
137 assert_se(sd_bus_set_fd(bus, c->fds[1], c->fds[1]) >= 0);
138 assert_se(sd_bus_negotiate_fds(bus, c->client_negotiate_unix_fds) >= 0);
139 assert_se(sd_bus_set_anonymous(bus, c->client_anonymous_auth) >= 0);
140 assert_se(sd_bus_start(bus) >= 0);
141
142 r = sd_bus_message_new_method_call(
143 bus,
144 &m,
145 "org.freedesktop.systemd.test",
146 "/",
147 "org.freedesktop.systemd.test",
148 "Exit");
149 if (r < 0) {
150 log_error_errno(-r, "Failed to allocate method call: %m");
151 return r;
152 }
153
154 r = sd_bus_call(bus, m, 0, &error, &reply);
155 if (r < 0) {
156 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
157 return r;
158 }
159
160 return 0;
161 }
162
163 static int test_one(bool client_negotiate_unix_fds, bool server_negotiate_unix_fds,
164 bool client_anonymous_auth, bool server_anonymous_auth) {
165
166 struct context c;
167 pthread_t s;
168 void *p;
169 int r, q;
170
171 zero(c);
172
173 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, c.fds) >= 0);
174
175 c.client_negotiate_unix_fds = client_negotiate_unix_fds;
176 c.server_negotiate_unix_fds = server_negotiate_unix_fds;
177 c.client_anonymous_auth = client_anonymous_auth;
178 c.server_anonymous_auth = server_anonymous_auth;
179
180 r = pthread_create(&s, NULL, server, &c);
181 if (r != 0)
182 return -r;
183
184 r = client(&c);
185
186 q = pthread_join(s, &p);
187 if (q != 0)
188 return -q;
189
190 if (r < 0)
191 return r;
192
193 if (PTR_TO_INT(p) < 0)
194 return PTR_TO_INT(p);
195
196 return 0;
197 }
198
199 int main(int argc, char *argv[]) {
200 int r;
201
202 r = test_one(true, true, false, false);
203 assert_se(r >= 0);
204
205 r = test_one(true, false, false, false);
206 assert_se(r >= 0);
207
208 r = test_one(false, true, false, false);
209 assert_se(r >= 0);
210
211 r = test_one(false, false, false, false);
212 assert_se(r >= 0);
213
214 r = test_one(true, true, true, true);
215 assert_se(r >= 0);
216
217 r = test_one(true, true, false, true);
218 assert_se(r >= 0);
219
220 r = test_one(true, true, true, false);
221 assert_se(r == -EPERM);
222
223 return EXIT_SUCCESS;
224 }