1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Lennart Poettering
13 #include "bus-internal.h"
22 bool client_negotiate_unix_fds;
23 bool server_negotiate_unix_fds;
25 bool client_anonymous_auth;
26 bool server_anonymous_auth;
29 static void *server(void *p) {
30 struct context *c = p;
36 assert_se(sd_id128_randomize(&id) >= 0);
38 assert_se(sd_bus_new(&bus) >= 0);
39 assert_se(sd_bus_set_fd(bus, c->fds[0], c->fds[0]) >= 0);
40 assert_se(sd_bus_set_server(bus, 1, id) >= 0);
41 assert_se(sd_bus_set_anonymous(bus, c->server_anonymous_auth) >= 0);
42 assert_se(sd_bus_negotiate_fds(bus, c->server_negotiate_unix_fds) >= 0);
43 assert_se(sd_bus_start(bus) >= 0);
46 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
48 r = sd_bus_process(bus, &m);
50 log_error_errno(r, "Failed to process requests: %m");
55 r = sd_bus_wait(bus, (uint64_t) -1);
57 log_error_errno(r, "Failed to wait: %m");
67 log_info("Got message! member=%s", strna(sd_bus_message_get_member(m)));
69 if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Exit")) {
71 assert_se((sd_bus_can_send(bus, 'h') >= 1) ==
72 (c->server_negotiate_unix_fds && c->client_negotiate_unix_fds));
74 r = sd_bus_message_new_method_return(m, &reply);
76 log_error_errno(r, "Failed to allocate return: %m");
82 } else if (sd_bus_message_is_method_call(m, NULL, NULL)) {
83 r = sd_bus_message_new_method_error(
86 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method."));
88 log_error_errno(r, "Failed to allocate return: %m");
94 r = sd_bus_send(bus, reply, NULL);
96 log_error_errno(r, "Failed to send reply: %m");
110 return INT_TO_PTR(r);
113 static int client(struct context *c) {
114 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
115 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
116 sd_bus_error error = SD_BUS_ERROR_NULL;
119 assert_se(sd_bus_new(&bus) >= 0);
120 assert_se(sd_bus_set_fd(bus, c->fds[1], c->fds[1]) >= 0);
121 assert_se(sd_bus_negotiate_fds(bus, c->client_negotiate_unix_fds) >= 0);
122 assert_se(sd_bus_set_anonymous(bus, c->client_anonymous_auth) >= 0);
123 assert_se(sd_bus_start(bus) >= 0);
125 r = sd_bus_message_new_method_call(
128 "org.freedesktop.systemd.test",
130 "org.freedesktop.systemd.test",
133 return log_error_errno(r, "Failed to allocate method call: %m");
135 r = sd_bus_call(bus, m, 0, &error, &reply);
137 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
144 static int test_one(bool client_negotiate_unix_fds, bool server_negotiate_unix_fds,
145 bool client_anonymous_auth, bool server_anonymous_auth) {
154 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, c.fds) >= 0);
156 c.client_negotiate_unix_fds = client_negotiate_unix_fds;
157 c.server_negotiate_unix_fds = server_negotiate_unix_fds;
158 c.client_anonymous_auth = client_anonymous_auth;
159 c.server_anonymous_auth = server_anonymous_auth;
161 r = pthread_create(&s, NULL, server, &c);
167 q = pthread_join(s, &p);
174 if (PTR_TO_INT(p) < 0)
175 return PTR_TO_INT(p);
180 int main(int argc, char *argv[]) {
183 r = test_one(true, true, false, false);
186 r = test_one(true, false, false, false);
189 r = test_one(false, true, false, false);
192 r = test_one(false, false, false, false);
195 r = test_one(true, true, true, true);
198 r = test_one(true, true, false, true);
201 r = test_one(true, true, true, false);
202 assert_se(r == -EPERM);