1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "bus-internal.h"
35 bool client_negotiate_unix_fds
;
36 bool server_negotiate_unix_fds
;
38 bool client_anonymous_auth
;
39 bool server_anonymous_auth
;
42 static void *server(void *p
) {
43 struct context
*c
= p
;
49 assert_se(sd_id128_randomize(&id
) >= 0);
51 assert_se(sd_bus_new(&bus
) >= 0);
52 assert_se(sd_bus_set_fd(bus
, c
->fds
[0], c
->fds
[0]) >= 0);
53 assert_se(sd_bus_set_server(bus
, 1, id
) >= 0);
54 assert_se(sd_bus_set_anonymous(bus
, c
->server_anonymous_auth
) >= 0);
55 assert_se(sd_bus_negotiate_fds(bus
, c
->server_negotiate_unix_fds
) >= 0);
56 assert_se(sd_bus_start(bus
) >= 0);
59 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
61 r
= sd_bus_process(bus
, &m
);
63 log_error_errno(r
, "Failed to process requests: %m");
68 r
= sd_bus_wait(bus
, (uint64_t) -1);
70 log_error_errno(r
, "Failed to wait: %m");
80 log_info("Got message! member=%s", strna(sd_bus_message_get_member(m
)));
82 if (sd_bus_message_is_method_call(m
, "org.freedesktop.systemd.test", "Exit")) {
84 assert_se((sd_bus_can_send(bus
, 'h') >= 1) == (c
->server_negotiate_unix_fds
&& c
->client_negotiate_unix_fds
));
86 r
= sd_bus_message_new_method_return(m
, &reply
);
88 log_error_errno(r
, "Failed to allocate return: %m");
94 } else if (sd_bus_message_is_method_call(m
, NULL
, NULL
)) {
95 r
= sd_bus_message_new_method_error(
98 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD
, "Unknown method."));
100 log_error_errno(r
, "Failed to allocate return: %m");
106 r
= sd_bus_send(bus
, reply
, NULL
);
108 log_error_errno(r
, "Failed to send reply: %m");
122 return INT_TO_PTR(r
);
125 static int client(struct context
*c
) {
126 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
127 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
128 sd_bus_error error
= SD_BUS_ERROR_NULL
;
131 assert_se(sd_bus_new(&bus
) >= 0);
132 assert_se(sd_bus_set_fd(bus
, c
->fds
[1], c
->fds
[1]) >= 0);
133 assert_se(sd_bus_negotiate_fds(bus
, c
->client_negotiate_unix_fds
) >= 0);
134 assert_se(sd_bus_set_anonymous(bus
, c
->client_anonymous_auth
) >= 0);
135 assert_se(sd_bus_start(bus
) >= 0);
137 r
= sd_bus_message_new_method_call(
140 "org.freedesktop.systemd.test",
142 "org.freedesktop.systemd.test",
145 return log_error_errno(r
, "Failed to allocate method call: %m");
147 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
149 log_error("Failed to issue method call: %s", bus_error_message(&error
, -r
));
156 static int test_one(bool client_negotiate_unix_fds
, bool server_negotiate_unix_fds
,
157 bool client_anonymous_auth
, bool server_anonymous_auth
) {
166 assert_se(socketpair(AF_UNIX
, SOCK_STREAM
, 0, c
.fds
) >= 0);
168 c
.client_negotiate_unix_fds
= client_negotiate_unix_fds
;
169 c
.server_negotiate_unix_fds
= server_negotiate_unix_fds
;
170 c
.client_anonymous_auth
= client_anonymous_auth
;
171 c
.server_anonymous_auth
= server_anonymous_auth
;
173 r
= pthread_create(&s
, NULL
, server
, &c
);
179 q
= pthread_join(s
, &p
);
186 if (PTR_TO_INT(p
) < 0)
187 return PTR_TO_INT(p
);
192 int main(int argc
, char *argv
[]) {
195 r
= test_one(true, true, false, false);
198 r
= test_one(true, false, false, false);
201 r
= test_one(false, true, false, false);
204 r
= test_one(false, false, false, false);
207 r
= test_one(true, true, true, true);
210 r
= test_one(true, true, false, true);
213 r
= test_one(true, true, true, false);
214 assert_se(r
== -EPERM
);