chiark / gitweb /
tree-wide: drop license boilerplate
[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) == (c->server_negotiate_unix_fds && c->client_negotiate_unix_fds));
72
73                         r = sd_bus_message_new_method_return(m, &reply);
74                         if (r < 0) {
75                                 log_error_errno(r, "Failed to allocate return: %m");
76                                 goto fail;
77                         }
78
79                         quit = true;
80
81                 } else if (sd_bus_message_is_method_call(m, NULL, NULL)) {
82                         r = sd_bus_message_new_method_error(
83                                         m,
84                                         &reply,
85                                         &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method."));
86                         if (r < 0) {
87                                 log_error_errno(r, "Failed to allocate return: %m");
88                                 goto fail;
89                         }
90                 }
91
92                 if (reply) {
93                         r = sd_bus_send(bus, reply, NULL);
94                         if (r < 0) {
95                                 log_error_errno(r, "Failed to send reply: %m");
96                                 goto fail;
97                         }
98                 }
99         }
100
101         r = 0;
102
103 fail:
104         if (bus) {
105                 sd_bus_flush(bus);
106                 sd_bus_unref(bus);
107         }
108
109         return INT_TO_PTR(r);
110 }
111
112 static int client(struct context *c) {
113         _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
114         _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
115         sd_bus_error error = SD_BUS_ERROR_NULL;
116         int r;
117
118         assert_se(sd_bus_new(&bus) >= 0);
119         assert_se(sd_bus_set_fd(bus, c->fds[1], c->fds[1]) >= 0);
120         assert_se(sd_bus_negotiate_fds(bus, c->client_negotiate_unix_fds) >= 0);
121         assert_se(sd_bus_set_anonymous(bus, c->client_anonymous_auth) >= 0);
122         assert_se(sd_bus_start(bus) >= 0);
123
124         r = sd_bus_message_new_method_call(
125                         bus,
126                         &m,
127                         "org.freedesktop.systemd.test",
128                         "/",
129                         "org.freedesktop.systemd.test",
130                         "Exit");
131         if (r < 0)
132                 return log_error_errno(r, "Failed to allocate method call: %m");
133
134         r = sd_bus_call(bus, m, 0, &error, &reply);
135         if (r < 0) {
136                 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
137                 return r;
138         }
139
140         return 0;
141 }
142
143 static int test_one(bool client_negotiate_unix_fds, bool server_negotiate_unix_fds,
144                     bool client_anonymous_auth, bool server_anonymous_auth) {
145
146         struct context c;
147         pthread_t s;
148         void *p;
149         int r, q;
150
151         zero(c);
152
153         assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, c.fds) >= 0);
154
155         c.client_negotiate_unix_fds = client_negotiate_unix_fds;
156         c.server_negotiate_unix_fds = server_negotiate_unix_fds;
157         c.client_anonymous_auth = client_anonymous_auth;
158         c.server_anonymous_auth = server_anonymous_auth;
159
160         r = pthread_create(&s, NULL, server, &c);
161         if (r != 0)
162                 return -r;
163
164         r = client(&c);
165
166         q = pthread_join(s, &p);
167         if (q != 0)
168                 return -q;
169
170         if (r < 0)
171                 return r;
172
173         if (PTR_TO_INT(p) < 0)
174                 return PTR_TO_INT(p);
175
176         return 0;
177 }
178
179 int main(int argc, char *argv[]) {
180         int r;
181
182         r = test_one(true, true, false, false);
183         assert_se(r >= 0);
184
185         r = test_one(true, false, false, false);
186         assert_se(r >= 0);
187
188         r = test_one(false, true, false, false);
189         assert_se(r >= 0);
190
191         r = test_one(false, false, false, false);
192         assert_se(r >= 0);
193
194         r = test_one(true, true, true, true);
195         assert_se(r >= 0);
196
197         r = test_one(true, true, false, true);
198         assert_se(r >= 0);
199
200         r = test_one(true, true, true, false);
201         assert_se(r == -EPERM);
202
203         return EXIT_SUCCESS;
204 }