2013-03-19 20:03:16 +01:00
|
|
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2013 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <netinet/in.h>
|
2013-05-17 03:13:58 +02:00
|
|
|
#include <pthread.h>
|
2013-03-19 20:03:16 +01:00
|
|
|
|
|
|
|
#include "hashmap.h"
|
2013-03-21 22:50:25 +01:00
|
|
|
#include "prioq.h"
|
2013-03-19 20:03:16 +01:00
|
|
|
#include "list.h"
|
|
|
|
#include "util.h"
|
2013-05-16 21:52:35 +02:00
|
|
|
#include "refcnt.h"
|
2013-03-19 20:03:16 +01:00
|
|
|
|
|
|
|
#include "sd-bus.h"
|
|
|
|
#include "bus-error.h"
|
2013-03-31 16:16:37 +02:00
|
|
|
#include "bus-match.h"
|
2013-05-14 22:24:26 +02:00
|
|
|
#include "bus-kernel.h"
|
2013-12-24 15:03:32 +01:00
|
|
|
#include "kdbus.h"
|
2013-03-19 20:03:16 +01:00
|
|
|
|
|
|
|
struct reply_callback {
|
2013-03-31 16:26:14 +02:00
|
|
|
sd_bus_message_handler_t callback;
|
2013-03-19 20:03:16 +01:00
|
|
|
usec_t timeout;
|
2013-12-25 17:46:45 +01:00
|
|
|
uint64_t cookie;
|
2013-03-21 22:50:25 +01:00
|
|
|
unsigned prioq_idx;
|
2013-03-19 20:03:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct filter_callback {
|
2013-03-31 16:26:14 +02:00
|
|
|
sd_bus_message_handler_t callback;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-04-05 03:55:58 +02:00
|
|
|
unsigned last_iteration;
|
|
|
|
|
2013-03-19 20:03:16 +01:00
|
|
|
LIST_FIELDS(struct filter_callback, callbacks);
|
|
|
|
};
|
|
|
|
|
2014-05-15 01:15:30 +02:00
|
|
|
struct match_callback {
|
|
|
|
sd_bus_message_handler_t callback;
|
|
|
|
|
|
|
|
uint64_t cookie;
|
|
|
|
unsigned last_iteration;
|
|
|
|
|
|
|
|
char *match_string;
|
|
|
|
|
|
|
|
struct bus_match_node *match_node;
|
|
|
|
};
|
|
|
|
|
2013-10-09 02:37:10 +02:00
|
|
|
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);
|
2014-05-15 01:15:30 +02:00
|
|
|
LIST_HEAD(struct node_object_manager, object_managers);
|
2013-10-09 02:37:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct node_callback {
|
|
|
|
struct node *node;
|
|
|
|
|
|
|
|
bool is_fallback;
|
2013-03-31 16:26:14 +02:00
|
|
|
sd_bus_message_handler_t callback;
|
2013-03-22 05:58:47 +01:00
|
|
|
|
2013-10-09 02:37:10 +02:00
|
|
|
unsigned last_iteration;
|
|
|
|
|
|
|
|
LIST_FIELDS(struct node_callback, callbacks);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct node_enumerator {
|
|
|
|
struct node *node;
|
|
|
|
|
|
|
|
sd_bus_node_enumerator_t callback;
|
|
|
|
|
|
|
|
unsigned last_iteration;
|
|
|
|
|
|
|
|
LIST_FIELDS(struct node_enumerator, enumerators);
|
|
|
|
};
|
|
|
|
|
2014-05-15 01:15:30 +02:00
|
|
|
struct node_object_manager {
|
|
|
|
struct node *node;
|
|
|
|
|
|
|
|
LIST_FIELDS(struct node_object_manager, object_managers);
|
|
|
|
};
|
|
|
|
|
2013-10-09 02:37:10 +02:00
|
|
|
struct node_vtable {
|
|
|
|
struct node *node;
|
|
|
|
|
|
|
|
char *interface;
|
2013-03-22 05:58:47 +01:00
|
|
|
bool is_fallback;
|
2013-10-09 02:37:10 +02:00
|
|
|
const sd_bus_vtable *vtable;
|
|
|
|
sd_bus_object_find_t find;
|
2013-04-05 03:55:58 +02:00
|
|
|
|
|
|
|
unsigned last_iteration;
|
2013-10-09 02:37:10 +02:00
|
|
|
|
|
|
|
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;
|
2013-03-22 05:58:47 +01:00
|
|
|
};
|
|
|
|
|
2014-05-15 01:15:30 +02:00
|
|
|
typedef enum BusSlotType {
|
|
|
|
BUS_REPLY_CALLBACK,
|
|
|
|
BUS_FILTER_CALLBACK,
|
|
|
|
BUS_MATCH_CALLBACK,
|
|
|
|
BUS_NODE_CALLBACK,
|
|
|
|
BUS_NODE_ENUMERATOR,
|
|
|
|
BUS_NODE_VTABLE,
|
|
|
|
BUS_NODE_OBJECT_MANAGER,
|
2014-05-15 01:55:18 +02:00
|
|
|
_BUS_SLOT_INVALID = -1,
|
2014-05-15 01:15:30 +02:00
|
|
|
} BusSlotType;
|
|
|
|
|
|
|
|
struct sd_bus_slot {
|
|
|
|
unsigned n_ref;
|
|
|
|
sd_bus *bus;
|
|
|
|
void *userdata;
|
2014-05-15 01:55:18 +02:00
|
|
|
BusSlotType type:5;
|
|
|
|
bool floating:1;
|
2014-11-04 16:57:38 +01:00
|
|
|
char *description;
|
2014-05-15 01:15:30 +02:00
|
|
|
|
|
|
|
LIST_FIELDS(sd_bus_slot, slots);
|
|
|
|
|
|
|
|
union {
|
|
|
|
struct reply_callback reply_callback;
|
|
|
|
struct filter_callback filter_callback;
|
|
|
|
struct match_callback match_callback;
|
|
|
|
struct node_callback node_callback;
|
|
|
|
struct node_enumerator node_enumerator;
|
|
|
|
struct node_object_manager node_object_manager;
|
|
|
|
struct node_vtable node_vtable;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-03-19 20:03:16 +01:00
|
|
|
enum bus_state {
|
2013-03-24 23:55:03 +01:00
|
|
|
BUS_UNSET,
|
2013-03-19 20:03:16 +01:00
|
|
|
BUS_OPENING,
|
|
|
|
BUS_AUTHENTICATING,
|
|
|
|
BUS_HELLO,
|
2013-05-17 02:22:37 +02:00
|
|
|
BUS_RUNNING,
|
2013-11-19 21:12:59 +01:00
|
|
|
BUS_CLOSING,
|
2013-05-17 02:22:37 +02:00
|
|
|
BUS_CLOSED
|
2013-03-19 20:03:16 +01:00
|
|
|
};
|
|
|
|
|
2013-05-17 02:22:37 +02:00
|
|
|
static inline bool BUS_IS_OPEN(enum bus_state state) {
|
2013-11-19 21:12:59 +01:00
|
|
|
return state > BUS_UNSET && state < BUS_CLOSING;
|
2013-05-17 02:22:37 +02:00
|
|
|
}
|
|
|
|
|
2013-03-30 15:21:06 +01:00
|
|
|
enum bus_auth {
|
|
|
|
_BUS_AUTH_INVALID,
|
|
|
|
BUS_AUTH_EXTERNAL,
|
|
|
|
BUS_AUTH_ANONYMOUS
|
|
|
|
};
|
|
|
|
|
2013-03-19 20:03:16 +01:00
|
|
|
struct sd_bus {
|
2013-05-16 21:52:35 +02:00
|
|
|
/* 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;
|
|
|
|
|
2013-03-19 20:03:16 +01:00
|
|
|
enum bus_state state;
|
2013-03-31 19:10:49 +02:00
|
|
|
int input_fd, output_fd;
|
2013-03-19 20:03:16 +01:00
|
|
|
int message_version;
|
2013-12-12 19:58:46 +01:00
|
|
|
int message_endian;
|
2013-03-24 23:55:03 +01:00
|
|
|
|
2013-04-11 23:09:29 +02:00
|
|
|
bool is_kernel:1;
|
2013-03-19 20:03:16 +01:00
|
|
|
bool can_fds:1;
|
2013-03-26 00:36:43 +01:00
|
|
|
bool bus_client:1;
|
2013-03-22 03:36:58 +01:00
|
|
|
bool ucred_valid:1;
|
2013-03-30 15:21:06 +01:00
|
|
|
bool is_server:1;
|
|
|
|
bool anonymous_auth:1;
|
2013-03-31 20:19:18 +02:00
|
|
|
bool prefer_readv:1;
|
|
|
|
bool prefer_writev:1;
|
2013-04-05 03:55:58 +02:00
|
|
|
bool match_callbacks_modified:1;
|
|
|
|
bool filter_callbacks_modified:1;
|
2013-10-09 02:37:10 +02:00
|
|
|
bool nodes_modified:1;
|
2013-12-10 17:41:39 +01:00
|
|
|
bool trusted:1;
|
2013-12-24 15:03:32 +01:00
|
|
|
bool fake_creds_valid:1;
|
2013-12-26 03:08:52 +01:00
|
|
|
bool manual_peer_interface:1;
|
2014-01-22 16:09:59 +01:00
|
|
|
bool is_system:1;
|
|
|
|
bool is_user:1;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-05-22 16:02:21 +02:00
|
|
|
int use_memfd;
|
|
|
|
|
2013-03-19 20:03:16 +01:00
|
|
|
void *rbuffer;
|
|
|
|
size_t rbuffer_size;
|
|
|
|
|
|
|
|
sd_bus_message **rqueue;
|
2013-12-16 20:32:37 +01:00
|
|
|
unsigned rqueue_size;
|
|
|
|
size_t rqueue_allocated;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
|
|
|
sd_bus_message **wqueue;
|
|
|
|
unsigned wqueue_size;
|
|
|
|
size_t windex;
|
2013-12-16 20:32:37 +01:00
|
|
|
size_t wqueue_allocated;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-12-25 17:46:45 +01:00
|
|
|
uint64_t cookie;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
|
|
|
char *unique_name;
|
2013-11-29 22:02:43 +01:00
|
|
|
uint64_t unique_id;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-03-31 16:16:37 +02:00
|
|
|
struct bus_match_node match_callbacks;
|
2013-03-21 22:50:25 +01:00
|
|
|
Prioq *reply_callbacks_prioq;
|
2014-10-14 18:27:55 +02:00
|
|
|
OrderedHashmap *reply_callbacks;
|
2013-03-19 20:03:16 +01:00
|
|
|
LIST_HEAD(struct filter_callback, filter_callbacks);
|
2013-10-09 02:37:10 +02:00
|
|
|
|
|
|
|
Hashmap *nodes;
|
|
|
|
Hashmap *vtable_methods;
|
|
|
|
Hashmap *vtable_properties;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2014-07-10 21:16:40 +02:00
|
|
|
union sockaddr_union sockaddr;
|
2013-03-19 20:03:16 +01:00
|
|
|
socklen_t sockaddr_size;
|
|
|
|
|
2013-04-11 23:09:29 +02:00
|
|
|
char *kernel;
|
2013-10-30 15:34:50 +01:00
|
|
|
char *machine;
|
2013-04-11 23:09:29 +02:00
|
|
|
|
2013-03-31 16:46:21 +02:00
|
|
|
sd_id128_t server_id;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
|
|
|
char *address;
|
|
|
|
unsigned address_index;
|
|
|
|
|
|
|
|
int last_connect_error;
|
|
|
|
|
2013-03-30 15:21:06 +01:00
|
|
|
enum bus_auth auth;
|
|
|
|
size_t auth_rbegin;
|
2013-03-19 20:03:16 +01:00
|
|
|
struct iovec auth_iovec[3];
|
|
|
|
unsigned auth_index;
|
2013-03-30 15:21:06 +01:00
|
|
|
char *auth_buffer;
|
2013-03-21 22:50:25 +01:00
|
|
|
usec_t auth_timeout;
|
2013-03-22 03:36:58 +01:00
|
|
|
|
|
|
|
struct ucred ucred;
|
|
|
|
char label[NAME_MAX];
|
2013-03-24 22:02:05 +01:00
|
|
|
|
2013-11-28 17:50:02 +01:00
|
|
|
uint64_t creds_mask;
|
|
|
|
|
2013-03-24 22:02:05 +01:00
|
|
|
int *fds;
|
|
|
|
unsigned n_fds;
|
2013-03-25 02:30:32 +01:00
|
|
|
|
|
|
|
char *exec_path;
|
|
|
|
char **exec_argv;
|
2013-03-29 23:44:11 +01:00
|
|
|
|
2013-04-05 03:55:58 +02:00
|
|
|
unsigned iteration_counter;
|
2013-05-10 03:36:55 +02:00
|
|
|
|
|
|
|
void *kdbus_buffer;
|
2013-05-14 22:24:26 +02:00
|
|
|
|
2013-05-17 03:13:58 +02:00
|
|
|
/* 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;
|
2013-05-14 22:24:26 +02:00
|
|
|
struct memfd_cache memfd_cache[MEMFD_CACHE_MAX];
|
|
|
|
unsigned n_memfd_cache;
|
2013-05-17 02:50:00 +02:00
|
|
|
|
|
|
|
pid_t original_pid;
|
2013-05-17 04:25:56 +02:00
|
|
|
|
|
|
|
uint64_t hello_flags;
|
2013-11-29 15:36:37 +01:00
|
|
|
uint64_t attach_flags;
|
2013-05-19 18:39:08 +02:00
|
|
|
|
|
|
|
uint64_t match_cookie;
|
2013-03-20 22:56:25 +01:00
|
|
|
|
2013-10-16 06:10:04 +02:00
|
|
|
sd_event_source *input_io_event_source;
|
|
|
|
sd_event_source *output_io_event_source;
|
|
|
|
sd_event_source *time_event_source;
|
2013-10-18 01:43:11 +02:00
|
|
|
sd_event_source *quit_event_source;
|
2013-10-16 06:10:04 +02:00
|
|
|
sd_event *event;
|
2013-12-13 05:12:46 +01:00
|
|
|
int event_priority;
|
2013-11-05 20:51:49 +01:00
|
|
|
|
2014-05-15 01:15:30 +02:00
|
|
|
sd_bus_message *current_message;
|
|
|
|
sd_bus_slot *current_slot;
|
2014-08-18 17:41:56 +02:00
|
|
|
sd_bus_message_handler_t current_handler;
|
|
|
|
void *current_userdata;
|
2013-11-11 22:00:48 +01:00
|
|
|
|
|
|
|
sd_bus **default_bus_ptr;
|
|
|
|
pid_t tid;
|
2013-12-24 15:03:32 +01:00
|
|
|
|
|
|
|
struct kdbus_creds fake_creds;
|
|
|
|
char *fake_label;
|
2013-12-24 19:31:44 +01:00
|
|
|
|
|
|
|
char *cgroup_root;
|
2014-01-22 16:09:59 +01:00
|
|
|
|
2014-11-04 16:13:49 +01:00
|
|
|
char *description;
|
2014-01-28 00:57:38 +01:00
|
|
|
|
|
|
|
size_t bloom_size;
|
|
|
|
unsigned bloom_n_hash;
|
2014-03-03 01:33:45 +01:00
|
|
|
|
|
|
|
sd_bus_track *track_queue;
|
2014-05-15 01:15:30 +02:00
|
|
|
|
|
|
|
LIST_HEAD(sd_bus_slot, slots);
|
2013-10-16 06:10:04 +02:00
|
|
|
};
|
2013-03-21 22:50:25 +01:00
|
|
|
|
|
|
|
#define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
|
2013-03-21 23:20:25 +01:00
|
|
|
|
2013-11-08 18:23:47 +01:00
|
|
|
#define BUS_WQUEUE_MAX 1024
|
|
|
|
#define BUS_RQUEUE_MAX 64*1024
|
2013-03-21 23:20:25 +01:00
|
|
|
|
|
|
|
#define BUS_MESSAGE_SIZE_MAX (64*1024*1024)
|
|
|
|
#define BUS_AUTH_SIZE_MAX (64*1024)
|
2013-03-22 00:42:53 +01:00
|
|
|
|
2013-03-22 02:32:34 +01:00
|
|
|
#define BUS_CONTAINER_DEPTH 128
|
|
|
|
|
2013-03-22 00:42:53 +01:00
|
|
|
/* Defined by the specification as maximum size of an array in
|
|
|
|
* bytes */
|
|
|
|
#define BUS_ARRAY_MAX_SIZE 67108864
|
|
|
|
|
2013-03-24 22:02:05 +01:00
|
|
|
#define BUS_FDS_MAX 1024
|
|
|
|
|
2013-03-25 02:30:32 +01:00
|
|
|
#define BUS_EXEC_ARGV_MAX 256
|
|
|
|
|
2013-12-25 18:26:51 +01:00
|
|
|
bool interface_name_is_valid(const char *p) _pure_;
|
|
|
|
bool service_name_is_valid(const char *p) _pure_;
|
|
|
|
bool member_name_is_valid(const char *p) _pure_;
|
|
|
|
bool object_path_is_valid(const char *p) _pure_;
|
|
|
|
char *object_path_startswith(const char *a, const char *b) _pure_;
|
2013-03-22 01:15:20 +01:00
|
|
|
|
2013-12-25 18:26:51 +01:00
|
|
|
bool namespace_complex_pattern(const char *pattern, const char *value) _pure_;
|
|
|
|
bool path_complex_pattern(const char *pattern, const char *value) _pure_;
|
2013-03-31 16:16:37 +02:00
|
|
|
|
2013-12-25 18:26:51 +01:00
|
|
|
bool namespace_simple_pattern(const char *pattern, const char *value) _pure_;
|
|
|
|
bool path_simple_pattern(const char *pattern, const char *value) _pure_;
|
2013-03-31 16:16:37 +02:00
|
|
|
|
2013-12-25 18:26:51 +01:00
|
|
|
int bus_message_type_from_string(const char *s, uint8_t *u) _pure_;
|
|
|
|
const char *bus_message_type_to_string(uint8_t u) _pure_;
|
2013-03-31 16:16:37 +02:00
|
|
|
|
2013-03-22 01:15:20 +01:00
|
|
|
#define error_name_is_valid interface_name_is_valid
|
2013-03-22 03:34:29 +01:00
|
|
|
|
|
|
|
int bus_ensure_running(sd_bus *bus);
|
2013-03-25 03:04:03 +01:00
|
|
|
int bus_start_running(sd_bus *bus);
|
|
|
|
int bus_next_address(sd_bus *bus);
|
2013-05-17 02:50:00 +02:00
|
|
|
|
2013-11-30 00:19:01 +01:00
|
|
|
int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m);
|
2013-11-29 20:07:56 +01:00
|
|
|
|
2013-11-30 00:19:01 +01:00
|
|
|
int bus_rqueue_make_room(sd_bus *bus);
|
2013-11-29 21:29:16 +01:00
|
|
|
|
2013-05-17 02:50:00 +02:00
|
|
|
bool bus_pid_changed(sd_bus *bus);
|
2013-10-11 23:20:54 +02:00
|
|
|
|
2013-10-30 13:52:40 +01:00
|
|
|
char *bus_address_escape(const char *v);
|
|
|
|
|
2013-10-11 23:20:54 +02:00
|
|
|
#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), '/'))
|
2013-10-14 19:53:56 +02:00
|
|
|
|
|
|
|
/* 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) \
|
2013-11-19 21:12:38 +01:00
|
|
|
_cleanup_bus_unref_ _unused_ sd_bus *_dont_destroy_##bus = sd_bus_ref(bus)
|
2014-03-19 04:17:00 +01:00
|
|
|
|
|
|
|
int bus_set_address_system(sd_bus *bus);
|
|
|
|
int bus_set_address_user(sd_bus *bus);
|
|
|
|
int bus_set_address_system_remote(sd_bus *b, const char *host);
|
|
|
|
int bus_set_address_system_container(sd_bus *b, const char *machine);
|
2014-05-15 01:15:30 +02:00
|
|
|
|
|
|
|
int bus_remove_match_by_string(sd_bus *bus, const char *match, sd_bus_message_handler_t callback, void *userdata);
|
2014-08-18 20:21:55 +02:00
|
|
|
|
|
|
|
int bus_get_root_path(sd_bus *bus);
|