2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "bus-internal.h"
34 bool client_negotiate_unix_fds;
35 bool server_negotiate_unix_fds;
37 bool client_anonymous_auth;
38 bool server_anonymous_auth;
41 static void *server(void *p) {
42 struct context *c = p;
48 assert_se(sd_id128_randomize(&id) >= 0);
50 assert_se(sd_bus_new(&bus) >= 0);
51 assert_se(sd_bus_set_fd(bus, c->fds[0], c->fds[0]) >= 0);
52 assert_se(sd_bus_set_server(bus, 1, id) >= 0);
53 assert_se(sd_bus_set_anonymous(bus, c->server_anonymous_auth) >= 0);
54 assert_se(sd_bus_negotiate_fds(bus, c->server_negotiate_unix_fds) >= 0);
55 assert_se(sd_bus_start(bus) >= 0);
58 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
60 r = sd_bus_process(bus, &m);
62 log_error_errno(r, "Failed to process requests: %m");
67 r = sd_bus_wait(bus, (uint64_t) -1);
69 log_error_errno(r, "Failed to wait: %m");
79 log_info("Got message! member=%s", strna(sd_bus_message_get_member(m)));
81 if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Exit")) {
83 assert_se((sd_bus_can_send(bus, 'h') >= 1) == (c->server_negotiate_unix_fds && c->client_negotiate_unix_fds));
85 r = sd_bus_message_new_method_return(m, &reply);
87 log_error_errno(r, "Failed to allocate return: %m");
93 } else if (sd_bus_message_is_method_call(m, NULL, NULL)) {
94 r = sd_bus_message_new_method_error(
97 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method."));
99 log_error_errno(r, "Failed to allocate return: %m");
105 r = sd_bus_send(bus, reply, NULL);
107 log_error_errno(r, "Failed to send reply: %m");
121 return INT_TO_PTR(r);
124 static int client(struct context *c) {
125 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
126 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
127 sd_bus_error error = SD_BUS_ERROR_NULL;
130 assert_se(sd_bus_new(&bus) >= 0);
131 assert_se(sd_bus_set_fd(bus, c->fds[1], c->fds[1]) >= 0);
132 assert_se(sd_bus_negotiate_fds(bus, c->client_negotiate_unix_fds) >= 0);
133 assert_se(sd_bus_set_anonymous(bus, c->client_anonymous_auth) >= 0);
134 assert_se(sd_bus_start(bus) >= 0);
136 r = sd_bus_message_new_method_call(
139 "org.freedesktop.systemd.test",
141 "org.freedesktop.systemd.test",
144 return log_error_errno(r, "Failed to allocate method call: %m");
146 r = sd_bus_call(bus, m, 0, &error, &reply);
148 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
155 static int test_one(bool client_negotiate_unix_fds, bool server_negotiate_unix_fds,
156 bool client_anonymous_auth, bool server_anonymous_auth) {
165 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, c.fds) >= 0);
167 c.client_negotiate_unix_fds = client_negotiate_unix_fds;
168 c.server_negotiate_unix_fds = server_negotiate_unix_fds;
169 c.client_anonymous_auth = client_anonymous_auth;
170 c.server_anonymous_auth = server_anonymous_auth;
172 r = pthread_create(&s, NULL, server, &c);
178 q = pthread_join(s, &p);
185 if (PTR_TO_INT(p) < 0)
186 return PTR_TO_INT(p);
191 int main(int argc, char *argv[]) {
194 r = test_one(true, true, false, false);
197 r = test_one(true, false, false, false);
200 r = test_one(false, true, false, false);
203 r = test_one(false, false, false, false);
206 r = test_one(true, true, true, true);
209 r = test_one(true, true, false, true);
212 r = test_one(true, true, true, false);
213 assert_se(r == -EPERM);