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