]> git.ipfire.org Git - thirdparty/systemd.git/blame - 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
CommitLineData
453a0c29
LP
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
453a0c29
LP
22#include <sys/mman.h>
23
453a0c29 24#include "sd-bus.h"
07630cea 25
b5efdb8a 26#include "alloc-util.h"
2b5c5383 27#include "bus-dump.h"
07630cea
LP
28#include "bus-kernel.h"
29#include "bus-message.h"
cf0fbc49 30#include "fd-util.h"
07630cea
LP
31#include "log.h"
32#include "memfd-util.h"
33#include "string-util.h"
34#include "util.h"
453a0c29 35
66b26c5c
LP
36#define FIRST_ARRAY 17
37#define SECOND_ARRAY 33
38
5cbe7492
LP
39#define STRING_SIZE 123
40
453a0c29 41int main(int argc, char *argv[]) {
79ccff06 42 _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL;
4d2b1e0a 43 const char *unique;
66b26c5c 44 uint8_t *p;
453a0c29
LP
45 sd_bus *a, *b;
46 int r, bus_ref;
47 sd_bus_message *m;
fac9c0d5 48 int f;
a392d361 49 uint64_t sz;
66b26c5c
LP
50 uint32_t u32;
51 size_t i, l;
5cbe7492 52 char *s;
7dcd79c2 53 _cleanup_close_ int sfd = -1;
453a0c29
LP
54
55 log_set_max_level(LOG_DEBUG);
56
79ccff06
ZJS
57 assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
58
59 bus_ref = bus_kernel_create_bus(name, false, &bus_name);
453a0c29
LP
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
4d2b1e0a
DH
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");
453a0c29
LP
90 assert_se(r >= 0);
91
5cbe7492 92 r = sd_bus_message_open_container(m, 'r', "aysay");
453a0c29
LP
93 assert_se(r >= 0);
94
66b26c5c 95 r = sd_bus_message_append_array_space(m, 'y', FIRST_ARRAY, (void**) &p);
453a0c29
LP
96 assert_se(r >= 0);
97
72d87e65
LP
98 p[0] = '<';
99 memset(p+1, 'L', FIRST_ARRAY-2);
100 p[FIRST_ARRAY-1] = '>';
453a0c29 101
4531a9bc
LP
102 f = memfd_new_and_map(NULL, STRING_SIZE, (void**) &s);
103 assert_se(f >= 0);
5cbe7492 104
72d87e65
LP
105 s[0] = '<';
106 for (i = 1; i < STRING_SIZE-2; i++)
5cbe7492 107 s[i] = '0' + (i % 10);
72d87e65 108 s[STRING_SIZE-2] = '>';
5cbe7492
LP
109 s[STRING_SIZE-1] = 0;
110 munmap(s, STRING_SIZE);
111
fac9c0d5 112 r = memfd_get_size(f, &sz);
5cbe7492
LP
113 assert_se(r >= 0);
114 assert_se(sz == STRING_SIZE);
115
7dcd79c2 116 r = sd_bus_message_append_string_memfd(m, f, 0, (uint64_t) -1);
5cbe7492
LP
117 assert_se(r >= 0);
118
fac9c0d5 119 close(f);
5cbe7492 120
4531a9bc
LP
121 f = memfd_new_and_map(NULL, SECOND_ARRAY, (void**) &p);
122 assert_se(f >= 0);
453a0c29 123
72d87e65
LP
124 p[0] = '<';
125 memset(p+1, 'P', SECOND_ARRAY-2);
126 p[SECOND_ARRAY-1] = '>';
66b26c5c 127 munmap(p, SECOND_ARRAY);
453a0c29 128
fac9c0d5 129 r = memfd_get_size(f, &sz);
453a0c29 130 assert_se(r >= 0);
66b26c5c 131 assert_se(sz == SECOND_ARRAY);
453a0c29 132
7dcd79c2 133 r = sd_bus_message_append_array_memfd(m, 'y', f, 0, (uint64_t) -1);
453a0c29
LP
134 assert_se(r >= 0);
135
fac9c0d5 136 close(f);
a392d361 137
453a0c29
LP
138 r = sd_bus_message_close_container(m);
139 assert_se(r >= 0);
140
a392d361
LP
141 r = sd_bus_message_append(m, "u", 4711);
142 assert_se(r >= 0);
143
7dcd79c2
LP
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
6e41a3e5 149 r = bus_message_seal(m, 55, 99*USEC_PER_SEC);
453a0c29
LP
150 assert_se(r >= 0);
151
d55192ad 152 bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
453a0c29
LP
153
154 r = sd_bus_send(b, m, NULL);
155 assert_se(r >= 0);
156
157 sd_bus_message_unref(m);
158
1307c3ff
LP
159 r = sd_bus_process(a, &m);
160 assert_se(r > 0);
161
d55192ad 162 bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
66b26c5c
LP
163 sd_bus_message_rewind(m, true);
164
5cbe7492 165 r = sd_bus_message_enter_container(m, 'r', "aysay");
66b26c5c
LP
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
72d87e65
LP
172 assert_se(p[0] == '<');
173 for (i = 1; i < l-1; i++)
66b26c5c 174 assert_se(p[i] == 'L');
72d87e65 175 assert_se(p[l-1] == '>');
66b26c5c 176
5cbe7492
LP
177 r = sd_bus_message_read(m, "s", &s);
178 assert_se(r > 0);
179
72d87e65
LP
180 assert_se(s[0] == '<');
181 for (i = 1; i < STRING_SIZE-2; i++)
5cbe7492 182 assert_se(s[i] == (char) ('0' + (i % 10)));
72d87e65 183 assert_se(s[STRING_SIZE-2] == '>');
5cbe7492
LP
184 assert_se(s[STRING_SIZE-1] == 0);
185
66b26c5c
LP
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
72d87e65
LP
190 assert_se(p[0] == '<');
191 for (i = 1; i < l-1; i++)
66b26c5c 192 assert_se(p[i] == 'P');
72d87e65 193 assert_se(p[l-1] == '>');
66b26c5c
LP
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
7dcd79c2
LP
202 r = sd_bus_message_read(m, "s", &s);
203 assert_se(r > 0);
204 assert_se(streq_ptr(s, "bcd"));
205
1307c3ff
LP
206 sd_bus_message_unref(m);
207
453a0c29
LP
208 sd_bus_unref(a);
209 sd_bus_unref(b);
453a0c29
LP
210
211 return 0;
212}