]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-bus/test-bus-server.c
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / libsystemd / sd-bus / test-bus-server.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
11c4c249 2/***
11c4c249
LP
3***/
4
11c4c249 5#include <pthread.h>
07630cea 6#include <stdlib.h>
11c4c249
LP
7
8#include "sd-bus.h"
07630cea 9
11c4c249 10#include "bus-internal.h"
057171ef 11#include "bus-util.h"
07630cea
LP
12#include "log.h"
13#include "macro.h"
14#include "util.h"
11c4c249
LP
15
16struct context {
17 int fds[2];
18
19 bool client_negotiate_unix_fds;
20 bool server_negotiate_unix_fds;
21
22 bool client_anonymous_auth;
23 bool server_anonymous_auth;
24};
25
26static void *server(void *p) {
27 struct context *c = p;
28 sd_bus *bus = NULL;
29 sd_id128_t id;
30 bool quit = false;
31 int r;
32
33 assert_se(sd_id128_randomize(&id) >= 0);
34
35 assert_se(sd_bus_new(&bus) >= 0);
e82c9509 36 assert_se(sd_bus_set_fd(bus, c->fds[0], c->fds[0]) >= 0);
11c4c249 37 assert_se(sd_bus_set_server(bus, 1, id) >= 0);
11c4c249 38 assert_se(sd_bus_set_anonymous(bus, c->server_anonymous_auth) >= 0);
264ad849 39 assert_se(sd_bus_negotiate_fds(bus, c->server_negotiate_unix_fds) >= 0);
11c4c249
LP
40 assert_se(sd_bus_start(bus) >= 0);
41
42 while (!quit) {
4afd3348 43 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
11c4c249
LP
44
45 r = sd_bus_process(bus, &m);
46 if (r < 0) {
da927ba9 47 log_error_errno(r, "Failed to process requests: %m");
11c4c249
LP
48 goto fail;
49 }
50
51 if (r == 0) {
52 r = sd_bus_wait(bus, (uint64_t) -1);
53 if (r < 0) {
da927ba9 54 log_error_errno(r, "Failed to wait: %m");
11c4c249
LP
55 goto fail;
56 }
57
58 continue;
59 }
60
61 if (!m)
62 continue;
63
64 log_info("Got message! member=%s", strna(sd_bus_message_get_member(m)));
65
66 if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Exit")) {
67
dcd6361e
ZJS
68 assert_se((sd_bus_can_send(bus, 'h') >= 1) ==
69 (c->server_negotiate_unix_fds && c->client_negotiate_unix_fds));
11c4c249 70
df2d202e 71 r = sd_bus_message_new_method_return(m, &reply);
11c4c249 72 if (r < 0) {
da927ba9 73 log_error_errno(r, "Failed to allocate return: %m");
11c4c249
LP
74 goto fail;
75 }
76
77 quit = true;
78
79 } else if (sd_bus_message_is_method_call(m, NULL, NULL)) {
c784c5ce 80 r = sd_bus_message_new_method_error(
df2d202e 81 m,
151b9b96
LP
82 &reply,
83 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method."));
11c4c249 84 if (r < 0) {
da927ba9 85 log_error_errno(r, "Failed to allocate return: %m");
11c4c249
LP
86 goto fail;
87 }
88 }
89
90 if (reply) {
91 r = sd_bus_send(bus, reply, NULL);
92 if (r < 0) {
da927ba9 93 log_error_errno(r, "Failed to send reply: %m");
11c4c249
LP
94 goto fail;
95 }
96 }
97 }
98
99 r = 0;
100
101fail:
102 if (bus) {
103 sd_bus_flush(bus);
104 sd_bus_unref(bus);
105 }
106
107 return INT_TO_PTR(r);
108}
109
110static int client(struct context *c) {
4afd3348
LP
111 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
112 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
c784c5ce 113 sd_bus_error error = SD_BUS_ERROR_NULL;
11c4c249
LP
114 int r;
115
116 assert_se(sd_bus_new(&bus) >= 0);
e82c9509 117 assert_se(sd_bus_set_fd(bus, c->fds[1], c->fds[1]) >= 0);
264ad849 118 assert_se(sd_bus_negotiate_fds(bus, c->client_negotiate_unix_fds) >= 0);
11c4c249
LP
119 assert_se(sd_bus_set_anonymous(bus, c->client_anonymous_auth) >= 0);
120 assert_se(sd_bus_start(bus) >= 0);
121
122 r = sd_bus_message_new_method_call(
123 bus,
151b9b96 124 &m,
11c4c249
LP
125 "org.freedesktop.systemd.test",
126 "/",
127 "org.freedesktop.systemd.test",
151b9b96 128 "Exit");
f647962d
MS
129 if (r < 0)
130 return log_error_errno(r, "Failed to allocate method call: %m");
11c4c249 131
c49b30a2 132 r = sd_bus_call(bus, m, 0, &error, &reply);
11c4c249
LP
133 if (r < 0) {
134 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
135 return r;
136 }
137
138 return 0;
139}
140
141static int test_one(bool client_negotiate_unix_fds, bool server_negotiate_unix_fds,
142 bool client_anonymous_auth, bool server_anonymous_auth) {
143
144 struct context c;
145 pthread_t s;
146 void *p;
147 int r, q;
148
149 zero(c);
150
151 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, c.fds) >= 0);
152
153 c.client_negotiate_unix_fds = client_negotiate_unix_fds;
154 c.server_negotiate_unix_fds = server_negotiate_unix_fds;
155 c.client_anonymous_auth = client_anonymous_auth;
156 c.server_anonymous_auth = server_anonymous_auth;
157
158 r = pthread_create(&s, NULL, server, &c);
159 if (r != 0)
160 return -r;
161
162 r = client(&c);
163
164 q = pthread_join(s, &p);
165 if (q != 0)
166 return -q;
167
168 if (r < 0)
169 return r;
170
171 if (PTR_TO_INT(p) < 0)
172 return PTR_TO_INT(p);
173
174 return 0;
175}
176
177int main(int argc, char *argv[]) {
178 int r;
179
180 r = test_one(true, true, false, false);
181 assert_se(r >= 0);
182
183 r = test_one(true, false, false, false);
184 assert_se(r >= 0);
185
186 r = test_one(false, true, false, false);
187 assert_se(r >= 0);
188
189 r = test_one(false, false, false, false);
190 assert_se(r >= 0);
191
192 r = test_one(true, true, true, true);
193 assert_se(r >= 0);
194
195 r = test_one(true, true, false, true);
196 assert_se(r >= 0);
197
198 r = test_one(true, true, true, false);
199 assert_se(r == -EPERM);
200
201 return EXIT_SUCCESS;
202}