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