Debian Codegraph @

src/libsystemd/sd-bus/test-bus-server.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/socket.h>
4
5 #include "sd-bus.h"
6 #include "sd-event.h"
7 #include "sd-future.h"
8
9 #include "errno-util.h"
10 #include "log.h"
11 #include "memory-util.h"
12 #include "string-util.h"
13 #include "tests.h"
14
15 struct context {
16 int fds[2];
17
18 bool client_negotiate_unix_fds;
19 bool server_negotiate_unix_fds;
20
21 bool client_anonymous_auth;
22 bool server_anonymous_auth;
23 };
24
25 static int server(void *userdata) {
26 struct context *c = ASSERT_PTR(userdata);
27 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
28 sd_id128_t id;
29 bool quit = false;
30 int r;
31
32 ASSERT_OK(sd_id128_randomize(&id));
33
34 ASSERT_OK(sd_bus_new(&bus));
35 ASSERT_OK(sd_bus_set_description(bus, "server"));
36 ASSERT_OK(sd_bus_set_fd(bus, c->fds[0], c->fds[0]));
37 ASSERT_OK(sd_bus_set_server(bus, 1, id));
38 ASSERT_OK(sd_bus_set_anonymous(bus, c->server_anonymous_auth));
39 ASSERT_OK(sd_bus_negotiate_fds(bus, c->server_negotiate_unix_fds));
40 ASSERT_OK(sd_bus_start(bus));
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 return log_error_errno(r, "Failed to process requests: %m");
48
49 if (r == 0) {
50 ASSERT_OK(sd_bus_wait(bus, UINT64_MAX));
51 continue;
52 }
53
54 if (!m)
55 continue;
56
57 log_info("Got message! member=%s", strna(sd_bus_message_get_member(m)));
58
59 if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Exit")) {
60
61 ASSERT_EQ(sd_bus_can_send(bus, 'h') >= 1,
62 c->server_negotiate_unix_fds && c->client_negotiate_unix_fds);
63
64 ASSERT_OK(sd_bus_message_new_method_return(m, &reply));
65
66 quit = true;
67
68 } else if (sd_bus_message_is_method_call(m, NULL, NULL))
69 ASSERT_OK(sd_bus_message_new_method_error(
70 m,
71 &reply,
72 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method.")));
73
74 if (reply)
75 ASSERT_OK(sd_bus_send(bus, reply, NULL));
76 }
77
78 return 0;
79 }
80
81 static int client(void *userdata) {
82 struct context *c = ASSERT_PTR(userdata);
83 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
84 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
85 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
86
87 ASSERT_OK(sd_bus_new(&bus));
88 ASSERT_OK(sd_bus_set_description(bus, "client"));
89 ASSERT_OK(sd_bus_set_fd(bus, c->fds[1], c->fds[1]));
90 ASSERT_OK(sd_bus_attach_event(bus, sd_fiber_get_event(), 0));
91 ASSERT_OK(sd_bus_negotiate_fds(bus, c->client_negotiate_unix_fds));
92 ASSERT_OK(sd_bus_set_anonymous(bus, c->client_anonymous_auth));
93 ASSERT_OK(sd_bus_start(bus));
94
95 ASSERT_OK(sd_bus_message_new_method_call(
96 bus,
97 &m,
98 "org.freedesktop.systemd.test",
99 "/",
100 "org.freedesktop.systemd.test",
101 "Exit"));
102
103 return sd_bus_call(bus, m, 0, &error, &reply);
104 }
105
106 static int test_one(bool client_negotiate_unix_fds, bool server_negotiate_unix_fds,
107 bool client_anonymous_auth, bool server_anonymous_auth) {
108
109 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
110 _cleanup_(sd_future_unrefp) sd_future *f_server = NULL, *f_client = NULL;
111 struct context c;
112 int r = 0;
113
114 zero(c);
115
116 ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_STREAM, 0, c.fds));
117
118 c.client_negotiate_unix_fds = client_negotiate_unix_fds;
119 c.server_negotiate_unix_fds = server_negotiate_unix_fds;
120 c.client_anonymous_auth = client_anonymous_auth;
121 c.server_anonymous_auth = server_anonymous_auth;
122
123 ASSERT_OK(sd_event_new(&e));
124 ASSERT_OK(sd_event_set_exit_on_idle(e, true));
125
126 ASSERT_OK(sd_fiber_new(e, "server", server, &c, /* destroy= */ NULL, &f_server));
127 ASSERT_OK(sd_fiber_new(e, "client", client, &c, /* destroy= */ NULL, &f_client));
128
129 ASSERT_OK(sd_event_loop(e));
130
131 RET_GATHER(r, sd_future_result(f_client));
132 RET_GATHER(r, sd_future_result(f_server));
133
134 return r;
135 }
136
137 int main(int argc, char *argv[]) {
138 test_setup_logging(LOG_DEBUG);
139
140 ASSERT_OK(test_one(true, true, false, false));
141 ASSERT_OK(test_one(true, false, false, false));
142 ASSERT_OK(test_one(false, true, false, false));
143 ASSERT_OK(test_one(false, false, false, false));
144 ASSERT_OK(test_one(true, true, true, true));
145 ASSERT_OK(test_one(true, true, false, true));
146 ASSERT_ERROR(test_one(true, true, true, false), EACCES);
147
148 return EXIT_SUCCESS;
149 }