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