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