]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-bus/test-bus-zero-copy.c
tmpfiles: accurately report creation results
[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
22#include <fcntl.h>
23#include <sys/mman.h>
24
25#include "util.h"
26#include "log.h"
a09abc4a 27#include "memfd-util.h"
453a0c29
LP
28
29#include "sd-bus.h"
453a0c29
LP
30#include "bus-message.h"
31#include "bus-error.h"
32#include "bus-kernel.h"
2b5c5383 33#include "bus-dump.h"
453a0c29 34
66b26c5c
LP
35#define FIRST_ARRAY 17
36#define SECOND_ARRAY 33
37
5cbe7492
LP
38#define STRING_SIZE 123
39
453a0c29 40int main(int argc, char *argv[]) {
79ccff06 41 _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL;
66b26c5c 42 uint8_t *p;
453a0c29
LP
43 sd_bus *a, *b;
44 int r, bus_ref;
45 sd_bus_message *m;
fac9c0d5 46 int f;
a392d361 47 uint64_t sz;
66b26c5c
LP
48 uint32_t u32;
49 size_t i, l;
5cbe7492 50 char *s;
7dcd79c2 51 _cleanup_close_ int sfd = -1;
453a0c29
LP
52
53 log_set_max_level(LOG_DEBUG);
54
79ccff06
ZJS
55 assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
56
57 bus_ref = bus_kernel_create_bus(name, false, &bus_name);
453a0c29
LP
58 if (bus_ref == -ENOENT)
59 return EXIT_TEST_SKIP;
60
61 assert_se(bus_ref >= 0);
62
63 address = strappend("kernel:path=", bus_name);
64 assert_se(address);
65
66 r = sd_bus_new(&a);
67 assert_se(r >= 0);
68
69 r = sd_bus_new(&b);
70 assert_se(r >= 0);
71
72 r = sd_bus_set_address(a, address);
73 assert_se(r >= 0);
74
75 r = sd_bus_set_address(b, address);
76 assert_se(r >= 0);
77
78 r = sd_bus_start(a);
79 assert_se(r >= 0);
80
81 r = sd_bus_start(b);
82 assert_se(r >= 0);
83
151b9b96 84 r = sd_bus_message_new_method_call(b, &m, ":1.1", "/a/path", "an.inter.face", "AMethod");
453a0c29
LP
85 assert_se(r >= 0);
86
5cbe7492 87 r = sd_bus_message_open_container(m, 'r', "aysay");
453a0c29
LP
88 assert_se(r >= 0);
89
66b26c5c 90 r = sd_bus_message_append_array_space(m, 'y', FIRST_ARRAY, (void**) &p);
453a0c29
LP
91 assert_se(r >= 0);
92
72d87e65
LP
93 p[0] = '<';
94 memset(p+1, 'L', FIRST_ARRAY-2);
95 p[FIRST_ARRAY-1] = '>';
453a0c29 96
4531a9bc
LP
97 f = memfd_new_and_map(NULL, STRING_SIZE, (void**) &s);
98 assert_se(f >= 0);
5cbe7492 99
72d87e65
LP
100 s[0] = '<';
101 for (i = 1; i < STRING_SIZE-2; i++)
5cbe7492 102 s[i] = '0' + (i % 10);
72d87e65 103 s[STRING_SIZE-2] = '>';
5cbe7492
LP
104 s[STRING_SIZE-1] = 0;
105 munmap(s, STRING_SIZE);
106
fac9c0d5 107 r = memfd_get_size(f, &sz);
5cbe7492
LP
108 assert_se(r >= 0);
109 assert_se(sz == STRING_SIZE);
110
7dcd79c2 111 r = sd_bus_message_append_string_memfd(m, f, 0, (uint64_t) -1);
5cbe7492
LP
112 assert_se(r >= 0);
113
fac9c0d5 114 close(f);
5cbe7492 115
4531a9bc
LP
116 f = memfd_new_and_map(NULL, SECOND_ARRAY, (void**) &p);
117 assert_se(f >= 0);
453a0c29 118
72d87e65
LP
119 p[0] = '<';
120 memset(p+1, 'P', SECOND_ARRAY-2);
121 p[SECOND_ARRAY-1] = '>';
66b26c5c 122 munmap(p, SECOND_ARRAY);
453a0c29 123
fac9c0d5 124 r = memfd_get_size(f, &sz);
453a0c29 125 assert_se(r >= 0);
66b26c5c 126 assert_se(sz == SECOND_ARRAY);
453a0c29 127
7dcd79c2 128 r = sd_bus_message_append_array_memfd(m, 'y', f, 0, (uint64_t) -1);
453a0c29
LP
129 assert_se(r >= 0);
130
fac9c0d5 131 close(f);
a392d361 132
453a0c29
LP
133 r = sd_bus_message_close_container(m);
134 assert_se(r >= 0);
135
a392d361
LP
136 r = sd_bus_message_append(m, "u", 4711);
137 assert_se(r >= 0);
138
7dcd79c2
LP
139 assert_se((sfd = memfd_new_and_map(NULL, 6, (void**) &p)) >= 0);
140 memcpy(p, "abcd\0", 6);
141 munmap(p, 6);
142 assert_se(sd_bus_message_append_string_memfd(m, sfd, 1, 4) >= 0);
143
6e41a3e5 144 r = bus_message_seal(m, 55, 99*USEC_PER_SEC);
453a0c29
LP
145 assert_se(r >= 0);
146
d55192ad 147 bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
453a0c29
LP
148
149 r = sd_bus_send(b, m, NULL);
150 assert_se(r >= 0);
151
152 sd_bus_message_unref(m);
153
1307c3ff
LP
154 r = sd_bus_process(a, &m);
155 assert_se(r > 0);
156
d55192ad 157 bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
66b26c5c
LP
158 sd_bus_message_rewind(m, true);
159
5cbe7492 160 r = sd_bus_message_enter_container(m, 'r', "aysay");
66b26c5c
LP
161 assert_se(r > 0);
162
163 r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
164 assert_se(r > 0);
165 assert_se(l == FIRST_ARRAY);
166
72d87e65
LP
167 assert_se(p[0] == '<');
168 for (i = 1; i < l-1; i++)
66b26c5c 169 assert_se(p[i] == 'L');
72d87e65 170 assert_se(p[l-1] == '>');
66b26c5c 171
5cbe7492
LP
172 r = sd_bus_message_read(m, "s", &s);
173 assert_se(r > 0);
174
72d87e65
LP
175 assert_se(s[0] == '<');
176 for (i = 1; i < STRING_SIZE-2; i++)
5cbe7492 177 assert_se(s[i] == (char) ('0' + (i % 10)));
72d87e65 178 assert_se(s[STRING_SIZE-2] == '>');
5cbe7492
LP
179 assert_se(s[STRING_SIZE-1] == 0);
180
66b26c5c
LP
181 r = sd_bus_message_read_array(m, 'y', (const void**) &p, &l);
182 assert_se(r > 0);
183 assert_se(l == SECOND_ARRAY);
184
72d87e65
LP
185 assert_se(p[0] == '<');
186 for (i = 1; i < l-1; i++)
66b26c5c 187 assert_se(p[i] == 'P');
72d87e65 188 assert_se(p[l-1] == '>');
66b26c5c
LP
189
190 r = sd_bus_message_exit_container(m);
191 assert_se(r > 0);
192
193 r = sd_bus_message_read(m, "u", &u32);
194 assert_se(r > 0);
195 assert_se(u32 == 4711);
196
7dcd79c2
LP
197 r = sd_bus_message_read(m, "s", &s);
198 assert_se(r > 0);
199 assert_se(streq_ptr(s, "bcd"));
200
1307c3ff
LP
201 sd_bus_message_unref(m);
202
453a0c29
LP
203 sd_bus_unref(a);
204 sd_bus_unref(b);
453a0c29
LP
205
206 return 0;
207}