chiark / gitweb /
bus: add minimal locking around the memfd cache
[elogind.git] / src / libsystemd-bus / bus-internal.h
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>
27 #include <pthread.h>
28
29 #include "hashmap.h"
30 #include "prioq.h"
31 #include "list.h"
32 #include "util.h"
33 #include "refcnt.h"
34
35 #include "sd-bus.h"
36 #include "bus-error.h"
37 #include "bus-match.h"
38 #include "bus-kernel.h"
39
40 struct reply_callback {
41         sd_bus_message_handler_t callback;
42         void *userdata;
43         usec_t timeout;
44         uint64_t serial;
45         unsigned prioq_idx;
46 };
47
48 struct filter_callback {
49         sd_bus_message_handler_t callback;
50         void *userdata;
51
52         unsigned last_iteration;
53
54         LIST_FIELDS(struct filter_callback, callbacks);
55 };
56
57 struct object_callback {
58         sd_bus_message_handler_t callback;
59         void *userdata;
60
61         char *path;
62         bool is_fallback;
63
64         unsigned last_iteration;
65 };
66
67 enum bus_state {
68         BUS_UNSET,
69         BUS_OPENING,
70         BUS_AUTHENTICATING,
71         BUS_HELLO,
72         BUS_RUNNING,
73         BUS_CLOSED
74 };
75
76 static inline bool BUS_IS_OPEN(enum bus_state state) {
77         return state > BUS_UNSET && state < BUS_CLOSED;
78 }
79
80 enum bus_auth {
81         _BUS_AUTH_INVALID,
82         BUS_AUTH_EXTERNAL,
83         BUS_AUTH_ANONYMOUS
84 };
85
86 struct sd_bus {
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
97         enum bus_state state;
98         int input_fd, output_fd;
99         int message_version;
100
101         bool is_kernel:1;
102         bool negotiate_fds:1;
103         bool can_fds:1;
104         bool bus_client:1;
105         bool ucred_valid:1;
106         bool is_server:1;
107         bool anonymous_auth:1;
108         bool prefer_readv:1;
109         bool prefer_writev:1;
110         bool processing:1;
111         bool match_callbacks_modified:1;
112         bool filter_callbacks_modified:1;
113         bool object_callbacks_modified:1;
114
115         void *rbuffer;
116         size_t rbuffer_size;
117
118         sd_bus_message **rqueue;
119         unsigned rqueue_size;
120
121         sd_bus_message **wqueue;
122         unsigned wqueue_size;
123         size_t windex;
124
125         uint64_t serial;
126
127         char *unique_name;
128
129         struct bus_match_node match_callbacks;
130         Prioq *reply_callbacks_prioq;
131         Hashmap *reply_callbacks;
132         LIST_HEAD(struct filter_callback, filter_callbacks);
133         Hashmap *object_callbacks;
134
135         union {
136                 struct sockaddr sa;
137                 struct sockaddr_un un;
138                 struct sockaddr_in in;
139                 struct sockaddr_in6 in6;
140         } sockaddr;
141         socklen_t sockaddr_size;
142
143         char *kernel;
144
145         sd_id128_t server_id;
146
147         char *address;
148         unsigned address_index;
149
150         int last_connect_error;
151
152         enum bus_auth auth;
153         size_t auth_rbegin;
154         struct iovec auth_iovec[3];
155         unsigned auth_index;
156         char *auth_buffer;
157         usec_t auth_timeout;
158
159         struct ucred ucred;
160         char label[NAME_MAX];
161
162         int *fds;
163         unsigned n_fds;
164
165         char *exec_path;
166         char **exec_argv;
167
168         uint64_t hello_serial;
169         unsigned iteration_counter;
170
171         void *kdbus_buffer;
172
173         /* We do locking around the memfd cache, since we want to
174          * allow people to process a sd_bus_message in a different
175          * thread then it was generated on and free it there. Since
176          * adding something to the memfd cache might happen when a
177          * message is released, we hence need to protect this bit with
178          * a mutex. */
179         pthread_mutex_t memfd_cache_mutex;
180         struct memfd_cache memfd_cache[MEMFD_CACHE_MAX];
181         unsigned n_memfd_cache;
182
183         pid_t original_pid;
184 };
185
186 static inline void bus_unrefp(sd_bus **b) {
187         sd_bus_unref(*b);
188 }
189
190 #define _cleanup_bus_unref_ __attribute__((cleanup(bus_unrefp)))
191 #define _cleanup_bus_error_free_ __attribute__((cleanup(sd_bus_error_free)))
192
193 #define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
194
195 #define BUS_WQUEUE_MAX 128
196 #define BUS_RQUEUE_MAX 128
197
198 #define BUS_MESSAGE_SIZE_MAX (64*1024*1024)
199 #define BUS_AUTH_SIZE_MAX (64*1024)
200
201 #define BUS_CONTAINER_DEPTH 128
202
203 /* Defined by the specification as maximum size of an array in
204  * bytes */
205 #define BUS_ARRAY_MAX_SIZE 67108864
206
207 #define BUS_FDS_MAX 1024
208
209 #define BUS_EXEC_ARGV_MAX 256
210
211 bool object_path_is_valid(const char *p);
212 bool interface_name_is_valid(const char *p);
213 bool service_name_is_valid(const char *p);
214 bool member_name_is_valid(const char *p);
215
216 bool namespace_complex_pattern(const char *pattern, const char *value);
217 bool path_complex_pattern(const char *pattern, const char *value);
218
219 bool namespace_simple_pattern(const char *pattern, const char *value);
220 bool path_simple_pattern(const char *pattern, const char *value);
221
222 int bus_message_type_from_string(const char *s, uint8_t *u);
223 const char *bus_message_type_to_string(uint8_t u);
224
225 #define error_name_is_valid interface_name_is_valid
226
227 int bus_ensure_running(sd_bus *bus);
228 int bus_start_running(sd_bus *bus);
229 int bus_next_address(sd_bus *bus);
230
231 bool bus_pid_changed(sd_bus *bus);