1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
33 #include "bus-internal.h"
34 #include "bus-message.h"
39 bool client_negotiate_unix_fds;
40 bool server_negotiate_unix_fds;
42 bool client_anonymous_auth;
43 bool server_anonymous_auth;
46 static void *server(void *p) {
47 struct context *c = p;
53 assert_se(sd_id128_randomize(&id) >= 0);
55 assert_se(sd_bus_new(&bus) >= 0);
56 assert_se(sd_bus_set_fd(bus, c->fds[0], c->fds[0]) >= 0);
57 assert_se(sd_bus_set_server(bus, 1, id) >= 0);
58 assert_se(sd_bus_set_negotiate_fds(bus, c->server_negotiate_unix_fds) >= 0);
59 assert_se(sd_bus_set_anonymous(bus, c->server_anonymous_auth) >= 0);
60 assert_se(sd_bus_start(bus) >= 0);
63 _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
65 r = sd_bus_process(bus, &m);
67 log_error("Failed to process requests: %s", strerror(-r));
72 r = sd_bus_wait(bus, (uint64_t) -1);
74 log_error("Failed to wait: %s", strerror(-r));
84 log_info("Got message! member=%s", strna(sd_bus_message_get_member(m)));
86 if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Exit")) {
88 assert_se((sd_bus_can_send(bus, 'h') >= 1) == (c->server_negotiate_unix_fds && c->client_negotiate_unix_fds));
90 r = sd_bus_message_new_method_return(bus, m, &reply);
92 log_error("Failed to allocate return: %s", strerror(-r));
98 } else if (sd_bus_message_is_method_call(m, NULL, NULL)) {
99 const sd_bus_error e = SD_BUS_ERROR_INIT_CONST("org.freedesktop.DBus.Error.UnknownMethod", "Unknown method.");
101 r = sd_bus_message_new_method_error(bus, m, &e, &reply);
103 log_error("Failed to allocate return: %s", strerror(-r));
109 r = sd_bus_send(bus, reply, NULL);
111 log_error("Failed to send reply: %s", strerror(-r));
125 return INT_TO_PTR(r);
128 static int client(struct context *c) {
129 _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
130 _cleanup_bus_unref_ sd_bus *bus = NULL;
131 sd_bus_error error = SD_BUS_ERROR_INIT;
134 assert_se(sd_bus_new(&bus) >= 0);
135 assert_se(sd_bus_set_fd(bus, c->fds[1], c->fds[1]) >= 0);
136 assert_se(sd_bus_set_negotiate_fds(bus, c->client_negotiate_unix_fds) >= 0);
137 assert_se(sd_bus_set_anonymous(bus, c->client_anonymous_auth) >= 0);
138 assert_se(sd_bus_start(bus) >= 0);
140 r = sd_bus_message_new_method_call(
142 "org.freedesktop.systemd.test",
144 "org.freedesktop.systemd.test",
148 log_error("Failed to allocate method call: %s", strerror(-r));
152 r = sd_bus_send_with_reply_and_block(bus, m, 0, &error, &reply);
154 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
161 static int test_one(bool client_negotiate_unix_fds, bool server_negotiate_unix_fds,
162 bool client_anonymous_auth, bool server_anonymous_auth) {
171 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, c.fds) >= 0);
173 c.client_negotiate_unix_fds = client_negotiate_unix_fds;
174 c.server_negotiate_unix_fds = server_negotiate_unix_fds;
175 c.client_anonymous_auth = client_anonymous_auth;
176 c.server_anonymous_auth = server_anonymous_auth;
178 r = pthread_create(&s, NULL, server, &c);
184 q = pthread_join(s, &p);
191 if (PTR_TO_INT(p) < 0)
192 return PTR_TO_INT(p);
197 int main(int argc, char *argv[]) {
200 r = test_one(true, true, false, false);
203 r = test_one(true, false, false, false);
206 r = test_one(false, true, false, false);
209 r = test_one(false, false, false, false);
212 r = test_one(true, true, true, true);
215 r = test_one(true, true, false, true);
218 r = test_one(true, true, true, false);
219 assert_se(r == -EPERM);