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/>.
30 #include "bus-internal.h"
36 bool client_negotiate_unix_fds;
37 bool server_negotiate_unix_fds;
39 bool client_anonymous_auth;
40 bool server_anonymous_auth;
43 static void *server(void *p) {
44 struct context *c = p;
50 assert_se(sd_id128_randomize(&id) >= 0);
52 assert_se(sd_bus_new(&bus) >= 0);
53 assert_se(sd_bus_set_fd(bus, c->fds[0], c->fds[0]) >= 0);
54 assert_se(sd_bus_set_server(bus, 1, id) >= 0);
55 assert_se(sd_bus_set_anonymous(bus, c->server_anonymous_auth) >= 0);
56 assert_se(sd_bus_negotiate_fds(bus, c->server_negotiate_unix_fds) >= 0);
57 assert_se(sd_bus_start(bus) >= 0);
60 _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
62 r = sd_bus_process(bus, &m);
64 log_error_errno(r, "Failed to process requests: %m");
69 r = sd_bus_wait(bus, (uint64_t) -1);
71 log_error_errno(r, "Failed to wait: %m");
81 log_info("Got message! member=%s", strna(sd_bus_message_get_member(m)));
83 if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Exit")) {
85 assert_se((sd_bus_can_send(bus, 'h') >= 1) == (c->server_negotiate_unix_fds && c->client_negotiate_unix_fds));
87 r = sd_bus_message_new_method_return(m, &reply);
89 log_error_errno(r, "Failed to allocate return: %m");
95 } else if (sd_bus_message_is_method_call(m, NULL, NULL)) {
96 r = sd_bus_message_new_method_error(
99 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method."));
101 log_error_errno(r, "Failed to allocate return: %m");
107 r = sd_bus_send(bus, reply, NULL);
109 log_error_errno(r, "Failed to send reply: %m");
123 return INT_TO_PTR(r);
126 static int client(struct context *c) {
127 _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
128 _cleanup_bus_unref_ sd_bus *bus = NULL;
129 sd_bus_error error = SD_BUS_ERROR_NULL;
132 assert_se(sd_bus_new(&bus) >= 0);
133 assert_se(sd_bus_set_fd(bus, c->fds[1], c->fds[1]) >= 0);
134 assert_se(sd_bus_negotiate_fds(bus, c->client_negotiate_unix_fds) >= 0);
135 assert_se(sd_bus_set_anonymous(bus, c->client_anonymous_auth) >= 0);
136 assert_se(sd_bus_start(bus) >= 0);
138 r = sd_bus_message_new_method_call(
141 "org.freedesktop.systemd.test",
143 "org.freedesktop.systemd.test",
146 return log_error_errno(r, "Failed to allocate method call: %m");
148 r = sd_bus_call(bus, m, 0, &error, &reply);
150 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
157 static int test_one(bool client_negotiate_unix_fds, bool server_negotiate_unix_fds,
158 bool client_anonymous_auth, bool server_anonymous_auth) {
167 assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, c.fds) >= 0);
169 c.client_negotiate_unix_fds = client_negotiate_unix_fds;
170 c.server_negotiate_unix_fds = server_negotiate_unix_fds;
171 c.client_anonymous_auth = client_anonymous_auth;
172 c.server_anonymous_auth = server_anonymous_auth;
174 r = pthread_create(&s, NULL, server, &c);
180 q = pthread_join(s, &p);
187 if (PTR_TO_INT(p) < 0)
188 return PTR_TO_INT(p);
193 int main(int argc, char *argv[]) {
196 r = test_one(true, true, false, false);
199 r = test_one(true, false, false, false);
202 r = test_one(false, true, false, false);
205 r = test_one(false, false, false, false);
208 r = test_one(true, true, true, true);
211 r = test_one(true, true, false, true);
214 r = test_one(true, true, true, false);
215 assert_se(r == -EPERM);