1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <sys/socket.h>
10 #include "memory-util.h"
11 #include "string-util.h"
17 bool client_negotiate_unix_fds
;
18 bool server_negotiate_unix_fds
;
20 bool client_anonymous_auth
;
21 bool server_anonymous_auth
;
24 static int _server(struct context
*c
) {
25 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
30 assert_se(sd_id128_randomize(&id
) >= 0);
32 assert_se(sd_bus_new(&bus
) >= 0);
33 assert_se(sd_bus_set_fd(bus
, c
->fds
[0], c
->fds
[0]) >= 0);
34 assert_se(sd_bus_set_server(bus
, 1, id
) >= 0);
35 assert_se(sd_bus_set_anonymous(bus
, c
->server_anonymous_auth
) >= 0);
36 assert_se(sd_bus_negotiate_fds(bus
, c
->server_negotiate_unix_fds
) >= 0);
37 assert_se(sd_bus_start(bus
) >= 0);
40 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
42 r
= sd_bus_process(bus
, &m
);
44 return log_error_errno(r
, "Failed to process requests: %m");
47 r
= sd_bus_wait(bus
, UINT64_MAX
);
49 return log_error_errno(r
, "Failed to wait: %m");
56 log_info("Got message! member=%s", strna(sd_bus_message_get_member(m
)));
58 if (sd_bus_message_is_method_call(m
, "org.freedesktop.systemd.test", "Exit")) {
60 assert_se((sd_bus_can_send(bus
, 'h') >= 1) ==
61 (c
->server_negotiate_unix_fds
&& c
->client_negotiate_unix_fds
));
63 r
= sd_bus_message_new_method_return(m
, &reply
);
65 return log_error_errno(r
, "Failed to allocate return: %m");
69 } else if (sd_bus_message_is_method_call(m
, NULL
, NULL
)) {
70 r
= sd_bus_message_new_method_error(
73 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD
, "Unknown method."));
75 return log_error_errno(r
, "Failed to allocate return: %m");
79 r
= sd_bus_send(bus
, reply
, NULL
);
81 return log_error_errno(r
, "Failed to send reply: %m");
88 static void* server(void *p
) {
89 return INT_TO_PTR(_server(p
));
92 static int client(struct context
*c
) {
93 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
94 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
95 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
98 assert_se(sd_bus_new(&bus
) >= 0);
99 assert_se(sd_bus_set_fd(bus
, c
->fds
[1], c
->fds
[1]) >= 0);
100 assert_se(sd_bus_negotiate_fds(bus
, c
->client_negotiate_unix_fds
) >= 0);
101 assert_se(sd_bus_set_anonymous(bus
, c
->client_anonymous_auth
) >= 0);
102 assert_se(sd_bus_start(bus
) >= 0);
104 r
= sd_bus_message_new_method_call(
107 "org.freedesktop.systemd.test",
109 "org.freedesktop.systemd.test",
112 return log_error_errno(r
, "Failed to allocate method call: %m");
114 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
116 return log_error_errno(r
, "Failed to issue method call: %s", bus_error_message(&error
, r
));
121 static int test_one(bool client_negotiate_unix_fds
, bool server_negotiate_unix_fds
,
122 bool client_anonymous_auth
, bool server_anonymous_auth
) {
131 assert_se(socketpair(AF_UNIX
, SOCK_STREAM
, 0, c
.fds
) >= 0);
133 c
.client_negotiate_unix_fds
= client_negotiate_unix_fds
;
134 c
.server_negotiate_unix_fds
= server_negotiate_unix_fds
;
135 c
.client_anonymous_auth
= client_anonymous_auth
;
136 c
.server_anonymous_auth
= server_anonymous_auth
;
138 r
= pthread_create(&s
, NULL
, server
, &c
);
144 q
= pthread_join(s
, &p
);
151 if (PTR_TO_INT(p
) < 0)
152 return PTR_TO_INT(p
);
157 int main(int argc
, char *argv
[]) {
160 test_setup_logging(LOG_DEBUG
);
162 r
= test_one(true, true, false, false);
165 r
= test_one(true, false, false, false);
168 r
= test_one(false, true, false, false);
171 r
= test_one(false, false, false, false);
174 r
= test_one(true, true, true, true);
177 r
= test_one(true, true, false, true);
180 r
= test_one(true, true, true, false);
181 assert_se(r
== -EPERM
);