chiark / gitweb /
0edb09764af2800263a51367adb7c3046dd1bc47
[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
28 #include "hashmap.h"
29 #include "prioq.h"
30 #include "list.h"
31 #include "util.h"
32 #include "refcnt.h"
33
34 #include "sd-bus.h"
35 #include "bus-error.h"
36 #include "bus-match.h"
37 #include "bus-kernel.h"
38
39 struct reply_callback {
40         sd_bus_message_handler_t callback;
41         void *userdata;
42         usec_t timeout;
43         uint64_t serial;
44         unsigned prioq_idx;
45 };
46
47 struct filter_callback {
48         sd_bus_message_handler_t callback;
49         void *userdata;
50
51         unsigned last_iteration;
52
53         LIST_FIELDS(struct filter_callback, callbacks);
54 };
55
56 struct object_callback {
57         sd_bus_message_handler_t callback;
58         void *userdata;
59
60         char *path;
61         bool is_fallback;
62
63         unsigned last_iteration;
64 };
65
66 enum bus_state {
67         BUS_UNSET,
68         BUS_OPENING,
69         BUS_AUTHENTICATING,
70         BUS_HELLO,
71         BUS_RUNNING
72 };
73
74 enum bus_auth {
75         _BUS_AUTH_INVALID,
76         BUS_AUTH_EXTERNAL,
77         BUS_AUTH_ANONYMOUS
78 };
79
80 struct sd_bus {
81         /* We use atomic ref counting here since sd_bus_message
82            objects retain references to their originating sd_bus but
83            we want to allow them to be processed in a different
84            thread. We won't provide full thread safety, but only the
85            bare minimum that makes it possible to use sd_bus and
86            sd_bus_message objects independently and on different
87            threads as long as each object is used only once at the
88            same time. */
89         RefCount n_ref;
90
91         enum bus_state state;
92         int input_fd, output_fd;
93         int message_version;
94
95         bool is_kernel:1;
96         bool negotiate_fds:1;
97         bool can_fds:1;
98         bool bus_client:1;
99         bool ucred_valid:1;
100         bool is_server:1;
101         bool anonymous_auth:1;
102         bool prefer_readv:1;
103         bool prefer_writev:1;
104         bool processing:1;
105         bool match_callbacks_modified:1;
106         bool filter_callbacks_modified:1;
107         bool object_callbacks_modified:1;
108
109         void *rbuffer;
110         size_t rbuffer_size;
111
112         sd_bus_message **rqueue;
113         unsigned rqueue_size;
114
115         sd_bus_message **wqueue;
116         unsigned wqueue_size;
117         size_t windex;
118
119         uint64_t serial;
120
121         char *unique_name;
122
123         struct bus_match_node match_callbacks;
124         Prioq *reply_callbacks_prioq;
125         Hashmap *reply_callbacks;
126         LIST_HEAD(struct filter_callback, filter_callbacks);
127         Hashmap *object_callbacks;
128
129         union {
130                 struct sockaddr sa;
131                 struct sockaddr_un un;
132                 struct sockaddr_in in;
133                 struct sockaddr_in6 in6;
134         } sockaddr;
135         socklen_t sockaddr_size;
136
137         char *kernel;
138
139         sd_id128_t server_id;
140
141         char *address;
142         unsigned address_index;
143
144         int last_connect_error;
145
146         enum bus_auth auth;
147         size_t auth_rbegin;
148         struct iovec auth_iovec[3];
149         unsigned auth_index;
150         char *auth_buffer;
151         usec_t auth_timeout;
152
153         struct ucred ucred;
154         char label[NAME_MAX];
155
156         int *fds;
157         unsigned n_fds;
158
159         char *exec_path;
160         char **exec_argv;
161
162         uint64_t hello_serial;
163         unsigned iteration_counter;
164
165         void *kdbus_buffer;
166
167         struct memfd_cache memfd_cache[MEMFD_CACHE_MAX];
168         unsigned n_memfd_cache;
169 };
170
171 static inline void bus_unrefp(sd_bus **b) {
172         sd_bus_unref(*b);
173 }
174
175 #define _cleanup_bus_unref_ __attribute__((cleanup(bus_unrefp)))
176 #define _cleanup_bus_error_free_ __attribute__((cleanup(sd_bus_error_free)))
177
178 #define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
179
180 #define BUS_WQUEUE_MAX 128
181 #define BUS_RQUEUE_MAX 128
182
183 #define BUS_MESSAGE_SIZE_MAX (64*1024*1024)
184 #define BUS_AUTH_SIZE_MAX (64*1024)
185
186 #define BUS_CONTAINER_DEPTH 128
187
188 /* Defined by the specification as maximum size of an array in
189  * bytes */
190 #define BUS_ARRAY_MAX_SIZE 67108864
191
192 #define BUS_FDS_MAX 1024
193
194 #define BUS_EXEC_ARGV_MAX 256
195
196 bool object_path_is_valid(const char *p);
197 bool interface_name_is_valid(const char *p);
198 bool service_name_is_valid(const char *p);
199 bool member_name_is_valid(const char *p);
200
201 bool namespace_complex_pattern(const char *pattern, const char *value);
202 bool path_complex_pattern(const char *pattern, const char *value);
203
204 bool namespace_simple_pattern(const char *pattern, const char *value);
205 bool path_simple_pattern(const char *pattern, const char *value);
206
207 int bus_message_type_from_string(const char *s, uint8_t *u);
208 const char *bus_message_type_to_string(uint8_t u);
209
210 #define error_name_is_valid interface_name_is_valid
211
212 int bus_ensure_running(sd_bus *bus);
213 int bus_start_running(sd_bus *bus);
214 int bus_next_address(sd_bus *bus);