chiark / gitweb /
373d596437f9540b5665ae40ed4b8b62567cd881
[elogind.git] / src / libelogind / sd-bus / test-bus-server.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2013 Lennart Poettering
6 ***/
7
8 #include <pthread.h>
9 #include <stdlib.h>
10
11 #include "sd-bus.h"
12
13 #include "bus-internal.h"
14 #include "bus-util.h"
15 #include "log.h"
16 #include "macro.h"
17 #include "util.h"
18
19 struct context {
20         int fds[2];
21
22         bool client_negotiate_unix_fds;
23         bool server_negotiate_unix_fds;
24
25         bool client_anonymous_auth;
26         bool server_anonymous_auth;
27 };
28
29 static void *server(void *p) {
30         struct context *c = p;
31         sd_bus *bus = NULL;
32         sd_id128_t id;
33         bool quit = false;
34         int r;
35
36         assert_se(sd_id128_randomize(&id) >= 0);
37
38         assert_se(sd_bus_new(&bus) >= 0);
39         assert_se(sd_bus_set_fd(bus, c->fds[0], c->fds[0]) >= 0);
40         assert_se(sd_bus_set_server(bus, 1, id) >= 0);
41         assert_se(sd_bus_set_anonymous(bus, c->server_anonymous_auth) >= 0);
42         assert_se(sd_bus_negotiate_fds(bus, c->server_negotiate_unix_fds) >= 0);
43         assert_se(sd_bus_start(bus) >= 0);
44
45         while (!quit) {
46                 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
47
48                 r = sd_bus_process(bus, &m);
49                 if (r < 0) {
50                         log_error_errno(r, "Failed to process requests: %m");
51                         goto fail;
52                 }
53
54                 if (r == 0) {
55                         r = sd_bus_wait(bus, (uint64_t) -1);
56                         if (r < 0) {
57                                 log_error_errno(r, "Failed to wait: %m");
58                                 goto fail;
59                         }
60
61                         continue;
62                 }
63
64                 if (!m)
65                         continue;
66
67                 log_info("Got message! member=%s", strna(sd_bus_message_get_member(m)));
68
69                 if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Exit")) {
70
71                         assert_se((sd_bus_can_send(bus, 'h') >= 1) ==
72                                   (c->server_negotiate_unix_fds && c->client_negotiate_unix_fds));
73
74                         r = sd_bus_message_new_method_return(m, &reply);
75                         if (r < 0) {
76                                 log_error_errno(r, "Failed to allocate return: %m");
77                                 goto fail;
78                         }
79
80                         quit = true;
81
82                 } else if (sd_bus_message_is_method_call(m, NULL, NULL)) {
83                         r = sd_bus_message_new_method_error(
84                                         m,
85                                         &reply,
86                                         &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method."));
87                         if (r < 0) {
88                                 log_error_errno(r, "Failed to allocate return: %m");
89                                 goto fail;
90                         }
91                 }
92
93                 if (reply) {
94                         r = sd_bus_send(bus, reply, NULL);
95                         if (r < 0) {
96                                 log_error_errno(r, "Failed to send reply: %m");
97                                 goto fail;
98                         }
99                 }
100         }
101
102         r = 0;
103
104 fail:
105         if (bus) {
106                 sd_bus_flush(bus);
107                 sd_bus_unref(bus);
108         }
109
110         return INT_TO_PTR(r);
111 }
112
113 static int client(struct context *c) {
114         _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
115         _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
116         sd_bus_error error = SD_BUS_ERROR_NULL;
117         int r;
118
119         assert_se(sd_bus_new(&bus) >= 0);
120         assert_se(sd_bus_set_fd(bus, c->fds[1], c->fds[1]) >= 0);
121         assert_se(sd_bus_negotiate_fds(bus, c->client_negotiate_unix_fds) >= 0);
122         assert_se(sd_bus_set_anonymous(bus, c->client_anonymous_auth) >= 0);
123         assert_se(sd_bus_start(bus) >= 0);
124
125         r = sd_bus_message_new_method_call(
126                         bus,
127                         &m,
128                         "org.freedesktop.systemd.test",
129                         "/",
130                         "org.freedesktop.systemd.test",
131                         "Exit");
132         if (r < 0)
133                 return log_error_errno(r, "Failed to allocate method call: %m");
134
135         r = sd_bus_call(bus, m, 0, &error, &reply);
136         if (r < 0) {
137                 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
138                 return r;
139         }
140
141         return 0;
142 }
143
144 static int test_one(bool client_negotiate_unix_fds, bool server_negotiate_unix_fds,
145                     bool client_anonymous_auth, bool server_anonymous_auth) {
146
147         struct context c;
148         pthread_t s;
149         void *p;
150         int r, q;
151
152         zero(c);
153
154         assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, c.fds) >= 0);
155
156         c.client_negotiate_unix_fds = client_negotiate_unix_fds;
157         c.server_negotiate_unix_fds = server_negotiate_unix_fds;
158         c.client_anonymous_auth = client_anonymous_auth;
159         c.server_anonymous_auth = server_anonymous_auth;
160
161         r = pthread_create(&s, NULL, server, &c);
162         if (r != 0)
163                 return -r;
164
165         r = client(&c);
166
167         q = pthread_join(s, &p);
168         if (q != 0)
169                 return -q;
170
171         if (r < 0)
172                 return r;
173
174         if (PTR_TO_INT(p) < 0)
175                 return PTR_TO_INT(p);
176
177         return 0;
178 }
179
180 int main(int argc, char *argv[]) {
181         int r;
182
183         r = test_one(true, true, false, false);
184         assert_se(r >= 0);
185
186         r = test_one(true, false, false, false);
187         assert_se(r >= 0);
188
189         r = test_one(false, true, false, false);
190         assert_se(r >= 0);
191
192         r = test_one(false, false, false, false);
193         assert_se(r >= 0);
194
195         r = test_one(true, true, true, true);
196         assert_se(r >= 0);
197
198         r = test_one(true, true, false, true);
199         assert_se(r >= 0);
200
201         r = test_one(true, true, true, false);
202         assert_se(r == -EPERM);
203
204         return EXIT_SUCCESS;
205 }