]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
11c4c249 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2013 Lennart Poettering | |
11c4c249 LP |
6 | ***/ |
7 | ||
11c4c249 | 8 | #include <pthread.h> |
07630cea | 9 | #include <stdlib.h> |
11c4c249 LP |
10 | |
11 | #include "sd-bus.h" | |
07630cea | 12 | |
11c4c249 | 13 | #include "bus-internal.h" |
057171ef | 14 | #include "bus-util.h" |
07630cea LP |
15 | #include "log.h" |
16 | #include "macro.h" | |
17 | #include "util.h" | |
11c4c249 LP |
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); | |
e82c9509 | 39 | assert_se(sd_bus_set_fd(bus, c->fds[0], c->fds[0]) >= 0); |
11c4c249 | 40 | assert_se(sd_bus_set_server(bus, 1, id) >= 0); |
11c4c249 | 41 | assert_se(sd_bus_set_anonymous(bus, c->server_anonymous_auth) >= 0); |
264ad849 | 42 | assert_se(sd_bus_negotiate_fds(bus, c->server_negotiate_unix_fds) >= 0); |
11c4c249 LP |
43 | assert_se(sd_bus_start(bus) >= 0); |
44 | ||
45 | while (!quit) { | |
4afd3348 | 46 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
11c4c249 LP |
47 | |
48 | r = sd_bus_process(bus, &m); | |
49 | if (r < 0) { | |
da927ba9 | 50 | log_error_errno(r, "Failed to process requests: %m"); |
11c4c249 LP |
51 | goto fail; |
52 | } | |
53 | ||
54 | if (r == 0) { | |
55 | r = sd_bus_wait(bus, (uint64_t) -1); | |
56 | if (r < 0) { | |
da927ba9 | 57 | log_error_errno(r, "Failed to wait: %m"); |
11c4c249 LP |
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 | ||
df2d202e | 73 | r = sd_bus_message_new_method_return(m, &reply); |
11c4c249 | 74 | if (r < 0) { |
da927ba9 | 75 | log_error_errno(r, "Failed to allocate return: %m"); |
11c4c249 LP |
76 | goto fail; |
77 | } | |
78 | ||
79 | quit = true; | |
80 | ||
81 | } else if (sd_bus_message_is_method_call(m, NULL, NULL)) { | |
c784c5ce | 82 | r = sd_bus_message_new_method_error( |
df2d202e | 83 | m, |
151b9b96 LP |
84 | &reply, |
85 | &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method.")); | |
11c4c249 | 86 | if (r < 0) { |
da927ba9 | 87 | log_error_errno(r, "Failed to allocate return: %m"); |
11c4c249 LP |
88 | goto fail; |
89 | } | |
90 | } | |
91 | ||
92 | if (reply) { | |
93 | r = sd_bus_send(bus, reply, NULL); | |
94 | if (r < 0) { | |
da927ba9 | 95 | log_error_errno(r, "Failed to send reply: %m"); |
11c4c249 LP |
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) { | |
4afd3348 LP |
113 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
114 | _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; | |
c784c5ce | 115 | sd_bus_error error = SD_BUS_ERROR_NULL; |
11c4c249 LP |
116 | int r; |
117 | ||
118 | assert_se(sd_bus_new(&bus) >= 0); | |
e82c9509 | 119 | assert_se(sd_bus_set_fd(bus, c->fds[1], c->fds[1]) >= 0); |
264ad849 | 120 | assert_se(sd_bus_negotiate_fds(bus, c->client_negotiate_unix_fds) >= 0); |
11c4c249 LP |
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, | |
151b9b96 | 126 | &m, |
11c4c249 LP |
127 | "org.freedesktop.systemd.test", |
128 | "/", | |
129 | "org.freedesktop.systemd.test", | |
151b9b96 | 130 | "Exit"); |
f647962d MS |
131 | if (r < 0) |
132 | return log_error_errno(r, "Failed to allocate method call: %m"); | |
11c4c249 | 133 | |
c49b30a2 | 134 | r = sd_bus_call(bus, m, 0, &error, &reply); |
11c4c249 LP |
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 | } |