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