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