| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include <sys/socket.h> |
| 5 |
|
| 6 |
#include "sd-varlink.h" |
| 7 |
|
| 8 |
#include "json-stream.h" |
| 9 |
#include "list.h" |
| 10 |
#include "pidref.h" |
| 11 |
#include "sd-forward.h" |
| 12 |
|
| 13 |
typedef enum VarlinkState { |
| 14 |
/* Client side states */ |
| 15 |
VARLINK_IDLE_CLIENT, |
| 16 |
VARLINK_AWAITING_REPLY, |
| 17 |
VARLINK_AWAITING_REPLY_MORE, |
| 18 |
VARLINK_CALLING, |
| 19 |
VARLINK_CALLED, |
| 20 |
VARLINK_COLLECTING, |
| 21 |
VARLINK_COLLECTING_REPLY, |
| 22 |
VARLINK_PROCESSING_REPLY, |
| 23 |
|
| 24 |
/* Server side states */ |
| 25 |
VARLINK_IDLE_SERVER, |
| 26 |
VARLINK_PROCESSING_METHOD, |
| 27 |
VARLINK_PROCESSING_METHOD_MORE, |
| 28 |
VARLINK_PROCESSING_METHOD_ONEWAY, |
| 29 |
VARLINK_PROCESSED_METHOD, |
| 30 |
VARLINK_PENDING_METHOD, |
| 31 |
VARLINK_PENDING_METHOD_MORE, |
| 32 |
|
| 33 |
/* Common states (only during shutdown) */ |
| 34 |
VARLINK_PENDING_DISCONNECT, |
| 35 |
VARLINK_PENDING_TIMEOUT, |
| 36 |
VARLINK_PROCESSING_DISCONNECT, |
| 37 |
VARLINK_PROCESSING_TIMEOUT, |
| 38 |
VARLINK_PROCESSING_FAILURE, |
| 39 |
VARLINK_DISCONNECTED, |
| 40 |
|
| 41 |
_VARLINK_STATE_MAX, |
| 42 |
_VARLINK_STATE_INVALID = -EINVAL, |
| 43 |
} VarlinkState; |
| 44 |
|
| 45 |
/* Tests whether we are not yet disconnected. Note that this is true during all states where the connection |
| 46 |
* is still good for something, and false only when it's dead for good. This means: when we are |
| 47 |
* asynchronously connecting to a peer and the connect() is still pending, then this will return 'true', as |
| 48 |
* the connection is still good, and we are likely to be able to properly operate on it soon. */ |
| 49 |
#define VARLINK_STATE_IS_ALIVE(state) \ |
| 50 |
IN_SET(state, \ |
| 51 |
VARLINK_IDLE_CLIENT, \ |
| 52 |
VARLINK_AWAITING_REPLY, \ |
| 53 |
VARLINK_AWAITING_REPLY_MORE, \ |
| 54 |
VARLINK_CALLING, \ |
| 55 |
VARLINK_CALLED, \ |
| 56 |
VARLINK_COLLECTING, \ |
| 57 |
VARLINK_COLLECTING_REPLY, \ |
| 58 |
VARLINK_PROCESSING_REPLY, \ |
| 59 |
VARLINK_IDLE_SERVER, \ |
| 60 |
VARLINK_PROCESSING_METHOD, \ |
| 61 |
VARLINK_PROCESSING_METHOD_MORE, \ |
| 62 |
VARLINK_PROCESSING_METHOD_ONEWAY, \ |
| 63 |
VARLINK_PROCESSED_METHOD, \ |
| 64 |
VARLINK_PENDING_METHOD, \ |
| 65 |
VARLINK_PENDING_METHOD_MORE) |
| 66 |
|
| 67 |
/* Tests whether we are expected to generate a method call reply, i.e. are processing a method call, except |
| 68 |
* one with the ONEWAY flag set. */ |
| 69 |
#define VARLINK_STATE_WANTS_REPLY(state) \ |
| 70 |
IN_SET(state, \ |
| 71 |
VARLINK_PROCESSING_METHOD, \ |
| 72 |
VARLINK_PROCESSING_METHOD_MORE) |
| 73 |
|
| 74 |
typedef struct sd_varlink { |
| 75 |
unsigned n_ref; |
| 76 |
|
| 77 |
VarlinkState state; |
| 78 |
sd_varlink_server *server; |
| 79 |
|
| 80 |
/* Transport layer: input/output buffers, fd passing, output queue, read/write/parse |
| 81 |
* step functions, sd-event integration (input/output/time event sources, idle |
| 82 |
* timeout, description, peer credentials). The varlink-level state machine and |
| 83 |
* dispatch logic live in sd-varlink.c; everything else about moving bytes is |
| 84 |
* delegated. */ |
| 85 |
JsonStream stream; |
| 86 |
|
| 87 |
unsigned n_pending; |
| 88 |
|
| 89 |
/* Per-call protocol-upgrade marker: set when the *current* method call carries the |
| 90 |
* SD_VARLINK_METHOD_UPGRADE flag. Validated by sd_varlink_reply_and_upgrade() to |
| 91 |
* ensure the caller's contract is honored. The transport-layer "stop reading at the |
| 92 |
* next message boundary" behavior is governed independently by the JsonStream's |
| 93 |
* bounded_reads flag. */ |
| 94 |
bool protocol_upgrade; |
| 95 |
|
| 96 |
sd_varlink_reply_t reply_callback; |
| 97 |
|
| 98 |
sd_json_variant *current; |
| 99 |
sd_json_variant *current_collected; |
| 100 |
sd_varlink_reply_flags_t current_reply_flags; |
| 101 |
sd_varlink_symbol *current_method; |
| 102 |
|
| 103 |
int *pushed_fds; |
| 104 |
size_t n_pushed_fds; |
| 105 |
|
| 106 |
sd_json_variant *previous; |
| 107 |
int *previous_fds; |
| 108 |
size_t n_previous_fds; |
| 109 |
char *sentinel; |
| 110 |
|
| 111 |
void *userdata; |
| 112 |
|
| 113 |
sd_event_source *quit_event_source; |
| 114 |
sd_event_source *defer_event_source; |
| 115 |
|
| 116 |
PidRef exec_pidref; |
| 117 |
} sd_varlink; |
| 118 |
|
| 119 |
typedef struct VarlinkServerSocket VarlinkServerSocket; |
| 120 |
|
| 121 |
struct VarlinkServerSocket { |
| 122 |
sd_varlink_server *server; |
| 123 |
|
| 124 |
int fd; |
| 125 |
char *address; |
| 126 |
|
| 127 |
sd_event_source *event_source; |
| 128 |
|
| 129 |
LIST_FIELDS(VarlinkServerSocket, sockets); |
| 130 |
}; |
| 131 |
|
| 132 |
typedef struct sd_varlink_server { |
| 133 |
unsigned n_ref; |
| 134 |
sd_varlink_server_flags_t flags; |
| 135 |
|
| 136 |
LIST_HEAD(VarlinkServerSocket, sockets); |
| 137 |
|
| 138 |
Hashmap *methods; /* Fully qualified symbol name of a method → sd_varlink_method_t */ |
| 139 |
Hashmap *fiber_methods; /* Fully qualified symbol name of a fiber method → sd_varlink_method_t */ |
| 140 |
Hashmap *interfaces; /* Fully qualified interface name → VarlinkInterface* */ |
| 141 |
Hashmap *symbols; /* Fully qualified symbol name of method/error → VarlinkSymbol* */ |
| 142 |
sd_varlink_connect_t connect_callback; |
| 143 |
sd_varlink_disconnect_t disconnect_callback; |
| 144 |
|
| 145 |
sd_event *event; |
| 146 |
int64_t event_priority; |
| 147 |
|
| 148 |
Hashmap *by_uid; /* UID_TO_PTR(uid) → UINT_TO_PTR(n_connections) */ |
| 149 |
unsigned n_connections; |
| 150 |
unsigned connections_max; |
| 151 |
unsigned connections_per_uid_max; |
| 152 |
|
| 153 |
bool exit_on_idle; |
| 154 |
|
| 155 |
void *userdata; |
| 156 |
|
| 157 |
char *description; |
| 158 |
char *vendor; |
| 159 |
char *product; |
| 160 |
char *version; |
| 161 |
char *url; |
| 162 |
} sd_varlink_server; |
| 163 |
|
| 164 |
#define varlink_log_errno(v, error, fmt, ...) \ |
| 165 |
log_debug_errno(error, "%s: " fmt, varlink_description(v), ##__VA_ARGS__) |
| 166 |
|
| 167 |
#define varlink_log(v, fmt, ...) \ |
| 168 |
log_debug("%s: " fmt, varlink_description(v), ##__VA_ARGS__) |
| 169 |
|
| 170 |
#define varlink_server_log_errno(s, error, fmt, ...) \ |
| 171 |
log_debug_errno(error, "%s: " fmt, varlink_server_description(s), ##__VA_ARGS__) |
| 172 |
|
| 173 |
#define varlink_server_log(s, fmt, ...) \ |
| 174 |
log_debug("%s: " fmt, varlink_server_description(s), ##__VA_ARGS__) |
| 175 |
|
| 176 |
static inline const char* varlink_description(sd_varlink *v) { |
| 177 |
return (v ? json_stream_get_description(&v->stream) : NULL) ?: "varlink"; |
| 178 |
} |
| 179 |
|
| 180 |
static inline const char* varlink_server_description(sd_varlink_server *s) { |
| 181 |
return (s ? s->description : NULL) ?: "varlink"; |
| 182 |
} |
| 183 |
|
| 184 |
VarlinkServerSocket* varlink_server_socket_free(VarlinkServerSocket *ss); |
| 185 |
DEFINE_TRIVIAL_CLEANUP_FUNC(VarlinkServerSocket *, varlink_server_socket_free); |
| 186 |
|
| 187 |
int varlink_server_add_socket_event_source(sd_varlink_server *s, VarlinkServerSocket *ss); |