bus-internal.h revision 8f155917bf5c11c8f156d7f25f242257d6086cb9
d657c51f14601d0235434ffb78cf6ac0f27cc83cLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering#pragma once
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering/***
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering This file is part of systemd.
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering Copyright 2013 Lennart Poettering
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering systemd is free software; you can redistribute it and/or modify it
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering under the terms of the GNU Lesser General Public License as published by
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering (at your option) any later version.
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering systemd is distributed in the hope that it will be useful, but
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering Lesser General Public License for more details.
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering You should have received a copy of the GNU Lesser General Public License
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering***/
204fa33c82588c47ebeef3f8c4c0b7da750e37f7Lennart Poettering
e0d25329b23a43332ea340f9907721873a316f4eKay Sievers#include <sys/socket.h>
e0d25329b23a43332ea340f9907721873a316f4eKay Sievers#include <sys/un.h>
e0d25329b23a43332ea340f9907721873a316f4eKay Sievers#include <netinet/in.h>
e0d25329b23a43332ea340f9907721873a316f4eKay Sievers#include <pthread.h>
e0d25329b23a43332ea340f9907721873a316f4eKay Sievers
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poettering#include "hashmap.h"
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poettering#include "prioq.h"
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poettering#include "list.h"
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poettering#include "util.h"
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poettering#include "refcnt.h"
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poettering
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poettering#include "sd-bus.h"
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poettering#include "bus-error.h"
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poettering#include "bus-match.h"
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poettering#include "bus-kernel.h"
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poettering
ccd07a083e8040a5bb091c5036ab1b4493ff8363Lennart Poetteringstruct reply_callback {
ccd07a083e8040a5bb091c5036ab1b4493ff8363Lennart Poettering sd_bus_message_handler_t callback;
353e12c2f4a9e96a47eb80b80d2ffb7bc1d44a1bLennart Poettering void *userdata;
353e12c2f4a9e96a47eb80b80d2ffb7bc1d44a1bLennart Poettering usec_t timeout;
353e12c2f4a9e96a47eb80b80d2ffb7bc1d44a1bLennart Poettering uint64_t serial;
353e12c2f4a9e96a47eb80b80d2ffb7bc1d44a1bLennart Poettering unsigned prioq_idx;
353e12c2f4a9e96a47eb80b80d2ffb7bc1d44a1bLennart Poettering};
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poettering
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poetteringstruct filter_callback {
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poettering sd_bus_message_handler_t callback;
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poettering void *userdata;
d26e4270409506cd398875216413b651d6ee7de6Lennart Poettering
d26e4270409506cd398875216413b651d6ee7de6Lennart Poettering unsigned last_iteration;
d26e4270409506cd398875216413b651d6ee7de6Lennart Poettering
d26e4270409506cd398875216413b651d6ee7de6Lennart Poettering LIST_FIELDS(struct filter_callback, callbacks);
d26e4270409506cd398875216413b651d6ee7de6Lennart Poettering};
d26e4270409506cd398875216413b651d6ee7de6Lennart Poettering
d26e4270409506cd398875216413b651d6ee7de6Lennart Poetteringstruct object_callback {
d26e4270409506cd398875216413b651d6ee7de6Lennart Poettering sd_bus_message_handler_t callback;
d26e4270409506cd398875216413b651d6ee7de6Lennart Poettering void *userdata;
d26e4270409506cd398875216413b651d6ee7de6Lennart Poettering
d26e4270409506cd398875216413b651d6ee7de6Lennart Poettering char *path;
d26e4270409506cd398875216413b651d6ee7de6Lennart Poettering bool is_fallback;
d26e4270409506cd398875216413b651d6ee7de6Lennart Poettering
d26e4270409506cd398875216413b651d6ee7de6Lennart Poettering unsigned last_iteration;
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poettering};
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poettering
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poetteringenum bus_state {
b13df9644bc6d4823b5a84e8a6bbf3bbb2c207c2Lennart Poettering BUS_UNSET,
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering BUS_OPENING,
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering BUS_AUTHENTICATING,
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering BUS_HELLO,
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering BUS_RUNNING,
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering BUS_CLOSED
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering};
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poetteringstatic inline bool BUS_IS_OPEN(enum bus_state state) {
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering return state > BUS_UNSET && state < BUS_CLOSED;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering}
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poetteringenum bus_auth {
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering _BUS_AUTH_INVALID,
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering BUS_AUTH_EXTERNAL,
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering BUS_AUTH_ANONYMOUS
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering};
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poetteringstruct sd_bus {
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering /* We use atomic ref counting here since sd_bus_message
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering objects retain references to their originating sd_bus but
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering we want to allow them to be processed in a different
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering thread. We won't provide full thread safety, but only the
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering bare minimum that makes it possible to use sd_bus and
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering sd_bus_message objects independently and on different
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering threads as long as each object is used only once at the
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering same time. */
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering RefCount n_ref;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering enum bus_state state;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering int input_fd, output_fd;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering int message_version;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering bool is_kernel:1;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering bool can_fds:1;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering bool bus_client:1;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering bool ucred_valid:1;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering bool is_server:1;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering bool anonymous_auth:1;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering bool prefer_readv:1;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering bool prefer_writev:1;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering bool processing:1;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering bool match_callbacks_modified:1;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering bool filter_callbacks_modified:1;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering bool object_callbacks_modified:1;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering int use_memfd;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering void *rbuffer;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering size_t rbuffer_size;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering sd_bus_message **rqueue;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering unsigned rqueue_size;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering sd_bus_message **wqueue;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering unsigned wqueue_size;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering size_t windex;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering uint64_t serial;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering char *unique_name;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering struct bus_match_node match_callbacks;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering Prioq *reply_callbacks_prioq;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering Hashmap *reply_callbacks;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering LIST_HEAD(struct filter_callback, filter_callbacks);
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering Hashmap *object_callbacks;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering union {
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering struct sockaddr sa;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering struct sockaddr_un un;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering struct sockaddr_in in;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering struct sockaddr_in6 in6;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering } sockaddr;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering socklen_t sockaddr_size;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering char *kernel;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering sd_id128_t server_id;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering char *address;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering unsigned address_index;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering int last_connect_error;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering enum bus_auth auth;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering size_t auth_rbegin;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering struct iovec auth_iovec[3];
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering unsigned auth_index;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering char *auth_buffer;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering usec_t auth_timeout;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering struct ucred ucred;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering char label[NAME_MAX];
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering int *fds;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering unsigned n_fds;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering char *exec_path;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering char **exec_argv;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering uint64_t hello_serial;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering unsigned iteration_counter;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering void *kdbus_buffer;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering /* We do locking around the memfd cache, since we want to
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering * allow people to process a sd_bus_message in a different
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering * thread then it was generated on and free it there. Since
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering * adding something to the memfd cache might happen when a
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering * message is released, we hence need to protect this bit with
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering * a mutex. */
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering pthread_mutex_t memfd_cache_mutex;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering struct memfd_cache memfd_cache[MEMFD_CACHE_MAX];
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering unsigned n_memfd_cache;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering pid_t original_pid;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering uint64_t hello_flags;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering uint64_t match_cookie;
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering};
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poetteringstatic inline void bus_unrefp(sd_bus **b) {
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering sd_bus_unref(*b);
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering}
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering#define _cleanup_bus_unref_ __attribute__((cleanup(bus_unrefp)))
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering#define _cleanup_bus_error_free_ __attribute__((cleanup(sd_bus_error_free)))
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering#define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering#define BUS_WQUEUE_MAX 128
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering#define BUS_RQUEUE_MAX 128
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering#define BUS_MESSAGE_SIZE_MAX (64*1024*1024)
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering#define BUS_AUTH_SIZE_MAX (64*1024)
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering#define BUS_CONTAINER_DEPTH 128
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering/* Defined by the specification as maximum size of an array in
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering * bytes */
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering#define BUS_ARRAY_MAX_SIZE 67108864
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering#define BUS_FDS_MAX 1024
#define BUS_EXEC_ARGV_MAX 256
bool object_path_is_valid(const char *p);
bool interface_name_is_valid(const char *p);
bool service_name_is_valid(const char *p);
bool member_name_is_valid(const char *p);
bool namespace_complex_pattern(const char *pattern, const char *value);
bool path_complex_pattern(const char *pattern, const char *value);
bool namespace_simple_pattern(const char *pattern, const char *value);
bool path_simple_pattern(const char *pattern, const char *value);
int bus_message_type_from_string(const char *s, uint8_t *u);
const char *bus_message_type_to_string(uint8_t u);
#define error_name_is_valid interface_name_is_valid
int bus_ensure_running(sd_bus *bus);
int bus_start_running(sd_bus *bus);
int bus_next_address(sd_bus *bus);
bool bus_pid_changed(sd_bus *bus);