1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "bus-internal.h"
19 bool client_negotiate_unix_fds;
20 bool server_negotiate_unix_fds;
22 bool client_anonymous_auth;
23 bool server_anonymous_auth;
26 static void *server(void *p) {
27 struct context *c = p;
33 assert_se(sd_id128_randomize(&id) >= 0);
35 assert_se(sd_bus_new(&bus) >= 0);
36 assert_se(sd_bus_set_fd(bus, c->fds[0], c->fds[0]) >= 0);
37 assert_se(sd_bus_set_server(bus, 1, id) >= 0);
38 assert_se(sd_bus_set_anonymous(bus, c->server_anonymous_auth) >= 0);
39 assert_se(sd_bus_negotiate_fds(bus, c->server_negotiate_unix_fds) >= 0);
40 assert_se(sd_bus_start(bus) >= 0);
43 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
45 r = sd_bus_process(bus, &m);
47 log_error_errno(r, "Failed to process requests: %m");
52 r = sd_bus_wait(bus, (uint64_t) -1);
54 log_error_errno(r, "Failed to wait: %m");
64 log_info("Got message! member=%s", strna(sd_bus_message_get_member(m)));
66 if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Exit")) {
68 assert_se((sd_bus_can_send(bus, 'h') >= 1) ==
69 (c->server_negotiate_unix_fds && c->client_negotiate_unix_fds));
71 r = sd_bus_message_new_method_return(m, &reply);
73 log_error_errno(r, "Failed to allocate return: %m");
79 } else if (sd_bus_message_is_method_call(m, NULL, NULL)) {
80 r = sd_bus_message_new_method_error(
83 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method."));
85 log_error_errno(r, "Failed to allocate return: %m");
91 r = sd_bus_send(bus, reply, NULL);
93 log_error_errno(r, "Failed to send reply: %m");
107 return INT_TO_PTR(r);
110 static int client(struct context *c) {
111 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
112 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
113 sd_bus_error error = SD_BUS_ERROR_NULL;
116 assert_se(sd_bus_new(&bus) >= 0);
117 assert_se(sd_bus_set_fd(bus, c->fds[1], c->fds[1]) >= 0);
118 assert_se(sd_bus_negotiate_fds(bus, c->client_negotiate_unix_fds) >= 0);
119 assert_se(sd_bus_set_anonymous(bus, c->client_anonymous_auth) >= 0);
120 assert_se(sd_bus_start(bus) >= 0);
122 r = sd_bus_message_new_method_call(
125 "org.freedesktop.systemd.test",
127 "org.freedesktop.systemd.test",
130 return log_error_errno(r, "Failed to allocate method call: %m");
132 r = sd_bus_call(bus, m, 0, &error, &reply);
134 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
141 static int test_one(bool client_negotiate_unix_fds, bool server_negotiate_unix_fds,
142 bool client_anonymous_auth, bool server_anonymous_auth) {
151 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, c.fds) >= 0);
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;
158 r = pthread_create(&s, NULL, server, &c);
164 q = pthread_join(s, &p);
171 if (PTR_TO_INT(p) < 0)
172 return PTR_TO_INT(p);
177 int main(int argc, char *argv[]) {
180 r = test_one(true, true, false, false);
183 r = test_one(true, false, false, false);
186 r = test_one(false, true, false, false);
189 r = test_one(false, false, false, false);
192 r = test_one(true, true, true, true);
195 r = test_one(true, true, false, true);
198 r = test_one(true, true, true, false);
199 assert_se(r == -EPERM);