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