bus-internal.h revision c1b9d935725103e95901f347b8981647ce4dd546
337eebb936be1da1215535e866965ce54c82d755Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4b2d99d9f4258a29f0bf8b1a78d17836e75bc378Lennart Poettering
d42d27ead91e470cb12986d928441e56c0f543caLennart Poettering#pragma once
91f9dcaf9270fe465525638cc08bd94590273349Lennart Poettering
f401e48c2db22ff9d1a05885b5599bebf19c2707Lennart Poettering/***
f401e48c2db22ff9d1a05885b5599bebf19c2707Lennart Poettering This file is part of systemd.
7640a5de1b3ffe6547200ad204d14e4f067caf4fLennart Poettering
1063dc3a525a87c0285e071794317f71724492feLennart Poettering Copyright 2013 Lennart Poettering
8e1bd70d4ce6d3881c1df6a6482643a2b3a69bb1Lennart Poettering
8cf3a8a982661c0bb9b04ff27f6d486b38b1b35eLennart Poettering systemd is free software; you can redistribute it and/or modify it
de47ca9b50e8c05c9fc116ff37794e526bddf92eLennart Poettering under the terms of the GNU Lesser General Public License as published by
e0e1580aae5913870933518f3cb7055730ce3a49Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
306a7fd82e790b3c00ba5cf806ccd6c0108061b5Lennart Poettering (at your option) any later version.
335aa753fa60ba0bb3c9fe679c761d5f1f3b1588Lennart Poettering
335aa753fa60ba0bb3c9fe679c761d5f1f3b1588Lennart Poettering systemd is distributed in the hope that it will be useful, but
335aa753fa60ba0bb3c9fe679c761d5f1f3b1588Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
3b63d2d31d0850bd7a81ab9b468218d2c4c461e8Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3b63d2d31d0850bd7a81ab9b468218d2c4c461e8Lennart Poettering Lesser General Public License for more details.
46574a5b4afeac0c3f69f15ce47c460309cb9becLennart Poettering
46574a5b4afeac0c3f69f15ce47c460309cb9becLennart Poettering You should have received a copy of the GNU Lesser General Public License
46574a5b4afeac0c3f69f15ce47c460309cb9becLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
85ed27f699939f75b8422ae67e016bdf9f439da9Lennart Poettering***/
447be1550523114f96c7f9eacb9d6a1ff6ca4197Lennart Poettering
c7b508592b28ee1e62350f0d249856811371f631Lennart Poettering#include <sys/socket.h>
afbf835326b0cc05c282b43f14ed501977de2004Lennart Poettering#include <sys/un.h>
4bb2357f77c875976de5e238a5783e4e136b37b5Lennart Poettering#include <netinet/in.h>
4bb2357f77c875976de5e238a5783e4e136b37b5Lennart Poettering#include <pthread.h>
93a45c562a1989dfbb2dd08c65f8a21b02959934Lennart Poettering
addab137cd8d318e4f543ca56018ee23d51aaca9Lennart Poettering#include "hashmap.h"
b2423f1f436f847d9fc96a63679be2b5552b6bafLennart Poettering#include "prioq.h"
449ddb2d23a63ca4c8cd70d13a070fba87c1fb30Lennart Poettering#include "list.h"
97c4a07df982ee967705022feaba9be33947abf0Lennart Poettering#include "util.h"
b574fa098d2827359c31a2d922b729725a8c6c8cLennart Poettering#include "refcnt.h"
f61448083198dc0e4e0d19a916bcd478336cc85dLennart Poettering
6e200d55ae538fc29360cdaa9863f30cdddf58f3Lennart Poettering#include "sd-bus.h"
6e200d55ae538fc29360cdaa9863f30cdddf58f3Lennart Poettering#include "bus-error.h"
dfac97b21e00cd3617ba817227db7b621841b5ccLennart Poettering#include "bus-match.h"
346bce1f4cff0096177c613987cdc80fa4ec134eLennart Poettering#include "bus-kernel.h"
5e6afdd3d359fc42de7ac432243e98673577e81fLennart Poettering
cd6d0a456bc9c45fa79316fc5896e4a3ae75a30bLennart Poetteringstruct reply_callback {
cd6d0a456bc9c45fa79316fc5896e4a3ae75a30bLennart Poettering sd_bus_message_handler_t callback;
c24eb49e6aecd6de2ad450083e826d4c9d9c75b6Lennart Poettering void *userdata;
151b190e79e64824552e01849352ca8f6ac7dedbLennart Poettering usec_t timeout;
9cf2578683de834026f501c24a0f5d3d0991d0a6Dave Reisner uint64_t serial;
f13b388f97bc3ba8db844bd3413d510e2466a0b6Kay Sievers unsigned prioq_idx;
f13b388f97bc3ba8db844bd3413d510e2466a0b6Kay Sievers};
f13b388f97bc3ba8db844bd3413d510e2466a0b6Kay Sievers
struct filter_callback {
sd_bus_message_handler_t callback;
void *userdata;
unsigned last_iteration;
LIST_FIELDS(struct filter_callback, callbacks);
};
struct node {
char *path;
struct node *parent;
LIST_HEAD(struct node, child);
LIST_FIELDS(struct node, siblings);
LIST_HEAD(struct node_callback, callbacks);
LIST_HEAD(struct node_vtable, vtables);
LIST_HEAD(struct node_enumerator, enumerators);
bool object_manager;
};
struct node_callback {
struct node *node;
bool is_fallback;
sd_bus_message_handler_t callback;
void *userdata;
unsigned last_iteration;
LIST_FIELDS(struct node_callback, callbacks);
};
struct node_enumerator {
struct node *node;
sd_bus_node_enumerator_t callback;
void *userdata;
unsigned last_iteration;
LIST_FIELDS(struct node_enumerator, enumerators);
};
struct node_vtable {
struct node *node;
char *interface;
bool is_fallback;
const sd_bus_vtable *vtable;
void *userdata;
sd_bus_object_find_t find;
unsigned last_iteration;
LIST_FIELDS(struct node_vtable, vtables);
};
struct vtable_member {
const char *path;
const char *interface;
const char *member;
struct node_vtable *parent;
unsigned last_iteration;
const sd_bus_vtable *vtable;
};
enum bus_state {
BUS_UNSET,
BUS_OPENING,
BUS_AUTHENTICATING,
BUS_HELLO,
BUS_RUNNING,
BUS_CLOSING,
BUS_CLOSED
};
static inline bool BUS_IS_OPEN(enum bus_state state) {
return state > BUS_UNSET && state < BUS_CLOSING;
}
enum bus_auth {
_BUS_AUTH_INVALID,
BUS_AUTH_EXTERNAL,
BUS_AUTH_ANONYMOUS
};
struct sd_bus {
/* We use atomic ref counting here since sd_bus_message
objects retain references to their originating sd_bus but
we want to allow them to be processed in a different
thread. We won't provide full thread safety, but only the
bare minimum that makes it possible to use sd_bus and
sd_bus_message objects independently and on different
threads as long as each object is used only once at the
same time. */
RefCount n_ref;
enum bus_state state;
int input_fd, output_fd;
int message_version;
bool is_kernel:1;
bool can_fds:1;
bool bus_client:1;
bool ucred_valid:1;
bool is_server:1;
bool anonymous_auth:1;
bool prefer_readv:1;
bool prefer_writev:1;
bool match_callbacks_modified:1;
bool filter_callbacks_modified:1;
bool nodes_modified:1;
bool use_gvariant:1;
int use_memfd;
void *rbuffer;
size_t rbuffer_size;
sd_bus_message **rqueue;
unsigned rqueue_size, rqueue_allocated;
sd_bus_message **wqueue;
unsigned wqueue_size;
size_t windex;
uint64_t serial;
char *unique_name;
uint64_t unique_id;
struct bus_match_node match_callbacks;
Prioq *reply_callbacks_prioq;
Hashmap *reply_callbacks;
LIST_HEAD(struct filter_callback, filter_callbacks);
Hashmap *nodes;
Hashmap *vtable_methods;
Hashmap *vtable_properties;
union {
struct sockaddr sa;
struct sockaddr_un un;
struct sockaddr_in in;
struct sockaddr_in6 in6;
} sockaddr;
socklen_t sockaddr_size;
char *kernel;
char *machine;
sd_id128_t server_id;
char *address;
unsigned address_index;
int last_connect_error;
enum bus_auth auth;
size_t auth_rbegin;
struct iovec auth_iovec[3];
unsigned auth_index;
char *auth_buffer;
usec_t auth_timeout;
struct ucred ucred;
char label[NAME_MAX];
uint64_t creds_mask;
int *fds;
unsigned n_fds;
char *exec_path;
char **exec_argv;
uint64_t hello_serial;
unsigned iteration_counter;
void *kdbus_buffer;
/* We do locking around the memfd cache, since we want to
* allow people to process a sd_bus_message in a different
* thread then it was generated on and free it there. Since
* adding something to the memfd cache might happen when a
* message is released, we hence need to protect this bit with
* a mutex. */
pthread_mutex_t memfd_cache_mutex;
struct memfd_cache memfd_cache[MEMFD_CACHE_MAX];
unsigned n_memfd_cache;
pid_t original_pid;
uint64_t hello_flags;
uint64_t attach_flags;
uint64_t match_cookie;
sd_event_source *input_io_event_source;
sd_event_source *output_io_event_source;
sd_event_source *time_event_source;
sd_event_source *quit_event_source;
sd_event *event;
sd_bus_message *current;
sd_bus **default_bus_ptr;
pid_t tid;
};
#define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
#define BUS_WQUEUE_MAX 1024
#define BUS_RQUEUE_MAX 64*1024
#define BUS_MESSAGE_SIZE_MAX (64*1024*1024)
#define BUS_AUTH_SIZE_MAX (64*1024)
#define BUS_CONTAINER_DEPTH 128
/* Defined by the specification as maximum size of an array in
* bytes */
#define BUS_ARRAY_MAX_SIZE 67108864
#define BUS_FDS_MAX 1024
#define BUS_EXEC_ARGV_MAX 256
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 object_path_is_valid(const char *p);
char *object_path_startswith(const char *a, const char *b);
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);
int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m);
int bus_rqueue_make_room(sd_bus *bus);
bool bus_pid_changed(sd_bus *bus);
char *bus_address_escape(const char *v);
#define OBJECT_PATH_FOREACH_PREFIX(prefix, path) \
for (char *_slash = ({ strcpy((prefix), (path)); streq((prefix), "/") ? NULL : strrchr((prefix), '/'); }) ; \
_slash && !(_slash[(_slash) == (prefix)] = 0); \
_slash = streq((prefix), "/") ? NULL : strrchr((prefix), '/'))
/* If we are invoking callbacks of a bus object, ensure unreffing the
* bus from the callback doesn't destroy the object we are working
* on */
#define BUS_DONT_DESTROY(bus) \
_cleanup_bus_unref_ _unused_ sd_bus *_dont_destroy_##bus = sd_bus_ref(bus)