]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-bus/bus-internal.h
bus: add benchmark tool to determine the right threshold for copying vs. memfd
[thirdparty/systemd.git] / src / libsystemd-bus / bus-internal.h
CommitLineData
de1c301e
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3#pragma once
4
5/***
6 This file is part of systemd.
7
8 Copyright 2013 Lennart Poettering
9
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22***/
23
24#include <sys/socket.h>
25#include <sys/un.h>
26#include <netinet/in.h>
45fbe937 27#include <pthread.h>
de1c301e
LP
28
29#include "hashmap.h"
e3017af9 30#include "prioq.h"
de1c301e
LP
31#include "list.h"
32#include "util.h"
e4ee6e5c 33#include "refcnt.h"
de1c301e
LP
34
35#include "sd-bus.h"
36#include "bus-error.h"
392d5b37 37#include "bus-match.h"
bc7fd8cd 38#include "bus-kernel.h"
de1c301e
LP
39
40struct reply_callback {
52f3ba91 41 sd_bus_message_handler_t callback;
de1c301e
LP
42 void *userdata;
43 usec_t timeout;
44 uint64_t serial;
e3017af9 45 unsigned prioq_idx;
de1c301e
LP
46};
47
48struct filter_callback {
52f3ba91 49 sd_bus_message_handler_t callback;
de1c301e
LP
50 void *userdata;
51
7286037f
LP
52 unsigned last_iteration;
53
de1c301e
LP
54 LIST_FIELDS(struct filter_callback, callbacks);
55};
56
a652755d 57struct object_callback {
52f3ba91 58 sd_bus_message_handler_t callback;
a652755d
LP
59 void *userdata;
60
61 char *path;
62 bool is_fallback;
7286037f
LP
63
64 unsigned last_iteration;
a652755d
LP
65};
66
de1c301e 67enum bus_state {
021a1e78 68 BUS_UNSET,
de1c301e
LP
69 BUS_OPENING,
70 BUS_AUTHENTICATING,
71 BUS_HELLO,
f54514f3
LP
72 BUS_RUNNING,
73 BUS_CLOSED
de1c301e
LP
74};
75
f54514f3
LP
76static inline bool BUS_IS_OPEN(enum bus_state state) {
77 return state > BUS_UNSET && state < BUS_CLOSED;
78}
79
2181a7f5
LP
80enum bus_auth {
81 _BUS_AUTH_INVALID,
82 BUS_AUTH_EXTERNAL,
83 BUS_AUTH_ANONYMOUS
84};
85
de1c301e 86struct sd_bus {
e4ee6e5c
LP
87 /* We use atomic ref counting here since sd_bus_message
88 objects retain references to their originating sd_bus but
89 we want to allow them to be processed in a different
90 thread. We won't provide full thread safety, but only the
91 bare minimum that makes it possible to use sd_bus and
92 sd_bus_message objects independently and on different
93 threads as long as each object is used only once at the
94 same time. */
95 RefCount n_ref;
96
de1c301e 97 enum bus_state state;
e82c9509 98 int input_fd, output_fd;
de1c301e 99 int message_version;
021a1e78 100
6629161f 101 bool is_kernel:1;
de1c301e 102 bool can_fds:1;
94bbf1ba 103 bool bus_client:1;
2571ead1 104 bool ucred_valid:1;
2181a7f5
LP
105 bool is_server:1;
106 bool anonymous_auth:1;
15d5af81
LP
107 bool prefer_readv:1;
108 bool prefer_writev:1;
6807947e 109 bool processing:1;
7286037f
LP
110 bool match_callbacks_modified:1;
111 bool filter_callbacks_modified:1;
112 bool object_callbacks_modified:1;
de1c301e 113
8f155917
LP
114 int use_memfd;
115
de1c301e
LP
116 void *rbuffer;
117 size_t rbuffer_size;
118
119 sd_bus_message **rqueue;
120 unsigned rqueue_size;
121
122 sd_bus_message **wqueue;
123 unsigned wqueue_size;
124 size_t windex;
125
126 uint64_t serial;
127
128 char *unique_name;
129
392d5b37 130 struct bus_match_node match_callbacks;
e3017af9 131 Prioq *reply_callbacks_prioq;
de1c301e
LP
132 Hashmap *reply_callbacks;
133 LIST_HEAD(struct filter_callback, filter_callbacks);
a652755d 134 Hashmap *object_callbacks;
de1c301e
LP
135
136 union {
137 struct sockaddr sa;
138 struct sockaddr_un un;
139 struct sockaddr_in in;
140 struct sockaddr_in6 in6;
141 } sockaddr;
142 socklen_t sockaddr_size;
143
6629161f
LP
144 char *kernel;
145
98178d39 146 sd_id128_t server_id;
de1c301e
LP
147
148 char *address;
149 unsigned address_index;
150
151 int last_connect_error;
152
2181a7f5
LP
153 enum bus_auth auth;
154 size_t auth_rbegin;
de1c301e
LP
155 struct iovec auth_iovec[3];
156 unsigned auth_index;
2181a7f5 157 char *auth_buffer;
e3017af9 158 usec_t auth_timeout;
2571ead1
LP
159
160 struct ucred ucred;
161 char label[NAME_MAX];
2c93b4ef
LP
162
163 int *fds;
164 unsigned n_fds;
2fd9ae2e
LP
165
166 char *exec_path;
167 char **exec_argv;
9d373862
LP
168
169 uint64_t hello_serial;
7286037f 170 unsigned iteration_counter;
fd8d62d9
LP
171
172 void *kdbus_buffer;
bc7fd8cd 173
45fbe937
LP
174 /* We do locking around the memfd cache, since we want to
175 * allow people to process a sd_bus_message in a different
176 * thread then it was generated on and free it there. Since
177 * adding something to the memfd cache might happen when a
178 * message is released, we hence need to protect this bit with
179 * a mutex. */
180 pthread_mutex_t memfd_cache_mutex;
bc7fd8cd
LP
181 struct memfd_cache memfd_cache[MEMFD_CACHE_MAX];
182 unsigned n_memfd_cache;
d5a2b9a6
LP
183
184 pid_t original_pid;
264ad849
LP
185
186 uint64_t hello_flags;
c7819669
LP
187
188 uint64_t match_cookie;
de1c301e 189};
89ffcd2a
LP
190
191static inline void bus_unrefp(sd_bus **b) {
192 sd_bus_unref(*b);
193}
194
195#define _cleanup_bus_unref_ __attribute__((cleanup(bus_unrefp)))
b9bf7e2b 196#define _cleanup_bus_error_free_ __attribute__((cleanup(sd_bus_error_free)))
e3017af9
LP
197
198#define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
25220239
LP
199
200#define BUS_WQUEUE_MAX 128
201#define BUS_RQUEUE_MAX 128
202
203#define BUS_MESSAGE_SIZE_MAX (64*1024*1024)
204#define BUS_AUTH_SIZE_MAX (64*1024)
ac89bf1d 205
ed205a6b
LP
206#define BUS_CONTAINER_DEPTH 128
207
ac89bf1d
LP
208/* Defined by the specification as maximum size of an array in
209 * bytes */
210#define BUS_ARRAY_MAX_SIZE 67108864
211
2c93b4ef
LP
212#define BUS_FDS_MAX 1024
213
2fd9ae2e
LP
214#define BUS_EXEC_ARGV_MAX 256
215
ac89bf1d 216bool object_path_is_valid(const char *p);
6693860f
LP
217bool interface_name_is_valid(const char *p);
218bool service_name_is_valid(const char *p);
219bool member_name_is_valid(const char *p);
220
392d5b37
LP
221bool namespace_complex_pattern(const char *pattern, const char *value);
222bool path_complex_pattern(const char *pattern, const char *value);
223
224bool namespace_simple_pattern(const char *pattern, const char *value);
225bool path_simple_pattern(const char *pattern, const char *value);
226
227int bus_message_type_from_string(const char *s, uint8_t *u);
a56f19c4 228const char *bus_message_type_to_string(uint8_t u);
392d5b37 229
6693860f 230#define error_name_is_valid interface_name_is_valid
20902f3e
LP
231
232int bus_ensure_running(sd_bus *bus);
a7e3212d
LP
233int bus_start_running(sd_bus *bus);
234int bus_next_address(sd_bus *bus);
d5a2b9a6
LP
235
236bool bus_pid_changed(sd_bus *bus);