]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/test-bus-zero-copy.c
tree-wide: sort includes
[thirdparty/systemd.git] / src / libsystemd / sd-bus / test-bus-zero-copy.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2013 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/mman.h>
23
24 #include "sd-bus.h"
25
26 #include "alloc-util.h"
27 #include "bus-dump.h"
28 #include "bus-kernel.h"
29 #include "bus-message.h"
30 #include "fd-util.h"
31 #include "log.h"
32 #include "memfd-util.h"
33 #include "string-util.h"
34 #include "util.h"
35
36 #define FIRST_ARRAY 17
37 #define SECOND_ARRAY 33
38
39 #define STRING_SIZE 123
40
41 int main(int argc, char *argv[]) {
42 _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL;
43 const char *unique;
44 uint8_t *p;
45 sd_bus *a, *b;
46 int r, bus_ref;
47 sd_bus_message *m;
48 int f;
49 uint64_t sz;
50 uint32_t u32;
51 size_t i, l;
52 char *s;
53 _cleanup_close_ int sfd = -1;
54
55 log_set_max_level(LOG_DEBUG);
56
57 assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
58
59 bus_ref = bus_kernel_create_bus(name, false, &bus_name);
60 if (bus_ref == -ENOENT)
61 return EXIT_TEST_SKIP;
62
63 assert_se(bus_ref >= 0);
64
65 address = strappend("kernel:path=", bus_name);
66 assert_se(address);
67
68 r = sd_bus_new(&a);
69 assert_se(r >= 0);
70
71 r = sd_bus_new(&b);
72 assert_se(r >= 0);
73
74 r = sd_bus_set_address(a, address);
75 assert_se(r >= 0);
76
77 r = sd_bus_set_address(b, address);
78 assert_se(r >= 0);
79
80 r = sd_bus_start(a);
81 assert_se(r >= 0);
82
83 r = sd_bus_start(b);
84 assert_se(r >= 0);
85
86 r = sd_bus_get_unique_name(a, &unique);
87 assert_se(r >= 0);
88
89 r = sd_bus_message_new_method_call(b, &m, unique, "/a/path", "an.inter.face", "AMethod");
90 assert_se(r >= 0);
91
92 r = sd_bus_message_open_container(m, 'r', "aysay");
93 assert_se(r >= 0);
94
95 r = sd_bus_message_append_array_space(m, 'y', FIRST_ARRAY, (void**) &p);
96 assert_se(r >= 0);
97
98 p[0] = '<';
99 memset(p+1, 'L', FIRST_ARRAY-2);
100 p[FIRST_ARRAY-1] = '>';
101
102 f = memfd_new_and_map(NULL, STRING_SIZE, (void**) &s);
103 assert_se(f >= 0);
104
105 s[0] = '<';
106 for (i = 1; i < STRING_SIZE-2; i++)
107 s[i] = '0' + (i % 10);
108 s[STRING_SIZE-2] = '>';
109 s[STRING_SIZE-1] = 0;
110 munmap(s, STRING_SIZE);
111
112 r = memfd_get_size(f, &sz);
113 assert_se(r >= 0);
114 assert_se(sz == STRING_SIZE);
115
116 r = sd_bus_message_append_string_memfd(m, f, 0, (uint64_t) -1);
117 assert_se(r >= 0);
118
119 close(f);
120
121 f = memfd_new_and_map(NULL, SECOND_ARRAY, (void**) &p);
122 assert_se(f >= 0);
123
124 p[0] = '<';
125 memset(p+1, 'P', SECOND_ARRAY-2);
126 p[SECOND_ARRAY-1] = '>';
127 munmap(p, SECOND_ARRAY);
128
129 r = memfd_get_size(f, &sz);
130 assert_se(r >= 0);
131 assert_se(sz == SECOND_ARRAY);
132
133 r = sd_bus_message_append_array_memfd(m, 'y', f, 0, (uint64_t) -1);
134 assert_se(r >= 0);
135
136 close(f);
137
138 r = sd_bus_message_close_container(m);
139 assert_se(r >= 0);
140
141 r = sd_bus_message_append(m, "u", 4711);
142 assert_se(r >= 0);
143
144 assert_se((sfd = memfd_new_and_map(NULL, 6, (void**) &p)) >= 0);
145 memcpy(p, "abcd\0", 6);
146 munmap(p, 6);
147 assert_se(sd_bus_message_append_string_memfd(m, sfd, 1, 4) >= 0);
148
149 r = bus_message_seal(m, 55, 99*USEC_PER_SEC);
150 assert_se(r >= 0);
151
152 bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
153
154 r = sd_bus_send(b, m, NULL);
155 assert_se(r >= 0);
156
157 sd_bus_message_unref(m);
158
159 r = sd_bus_process(a, &m);
160 assert_se(r > 0);
161
162 bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
163 sd_bus_message_rewind(m, true);
164
165 r = sd_bus_message_enter_container(m, 'r', "aysay");
166 assert_se(r > 0);
167
168 r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
169 assert_se(r > 0);
170 assert_se(l == FIRST_ARRAY);
171
172 assert_se(p[0] == '<');
173 for (i = 1; i < l-1; i++)
174 assert_se(p[i] == 'L');
175 assert_se(p[l-1] == '>');
176
177 r = sd_bus_message_read(m, "s", &s);
178 assert_se(r > 0);
179
180 assert_se(s[0] == '<');
181 for (i = 1; i < STRING_SIZE-2; i++)
182 assert_se(s[i] == (char) ('0' + (i % 10)));
183 assert_se(s[STRING_SIZE-2] == '>');
184 assert_se(s[STRING_SIZE-1] == 0);
185
186 r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
187 assert_se(r > 0);
188 assert_se(l == SECOND_ARRAY);
189
190 assert_se(p[0] == '<');
191 for (i = 1; i < l-1; i++)
192 assert_se(p[i] == 'P');
193 assert_se(p[l-1] == '>');
194
195 r = sd_bus_message_exit_container(m);
196 assert_se(r > 0);
197
198 r = sd_bus_message_read(m, "u", &u32);
199 assert_se(r > 0);
200 assert_se(u32 == 4711);
201
202 r = sd_bus_message_read(m, "s", &s);
203 assert_se(r > 0);
204 assert_se(streq_ptr(s, "bcd"));
205
206 sd_bus_message_unref(m);
207
208 sd_bus_unref(a);
209 sd_bus_unref(b);
210
211 return 0;
212 }