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