| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <poll.h> |
| 4 |
#include <sys/socket.h> |
| 5 |
#include <sys/stat.h> |
| 6 |
#include <unistd.h> |
| 7 |
|
| 8 |
#include "sd-event.h" |
| 9 |
#include "sd-json.h" |
| 10 |
|
| 11 |
#include "alloc-util.h" |
| 12 |
#include "errno-util.h" |
| 13 |
#include "fd-util.h" |
| 14 |
#include "io-util.h" |
| 15 |
#include "iovec-util.h" |
| 16 |
#include "json-stream.h" |
| 17 |
#include "list.h" |
| 18 |
#include "log.h" |
| 19 |
#include "memory-util.h" |
| 20 |
#include "process-util.h" |
| 21 |
#include "socket-util.h" |
| 22 |
#include "string-util.h" |
| 23 |
#include "time-util.h" |
| 24 |
#include "user-util.h" |
| 25 |
|
| 26 |
#define JSON_STREAM_BUFFER_MAX_DEFAULT (16U * 1024U * 1024U) |
| 27 |
#define JSON_STREAM_READ_SIZE_DEFAULT (64U * 1024U) |
| 28 |
#define JSON_STREAM_QUEUE_MAX_DEFAULT (64U * 1024U) |
| 29 |
#define JSON_STREAM_FDS_MAX (16U * 1024U) |
| 30 |
|
| 31 |
struct JsonStreamQueueItem { |
| 32 |
LIST_FIELDS(JsonStreamQueueItem, queue); |
| 33 |
sd_json_variant *data; |
| 34 |
size_t n_fds; |
| 35 |
int fds[]; |
| 36 |
}; |
| 37 |
|
| 38 |
/* Returns the size of the framing delimiter in bytes: strlen(delimiter) for multi-char |
| 39 |
* delimiters (e.g. "\r\n"), or 1 for the default NUL-byte delimiter (delimiter == NULL). */ |
| 40 |
static size_t json_stream_delimiter_size(const JsonStream *s) { |
| 41 |
return strlen_ptr(s->delimiter) ?: 1; |
| 42 |
} |
| 43 |
|
| 44 |
static usec_t json_stream_now(const JsonStream *s) { |
| 45 |
usec_t t; |
| 46 |
|
| 47 |
if (s->event && sd_event_now(s->event, CLOCK_MONOTONIC, &t) >= 0) |
| 48 |
return t; |
| 49 |
|
| 50 |
return now(CLOCK_MONOTONIC); |
| 51 |
} |
| 52 |
|
| 53 |
static JsonStreamQueueItem* json_stream_queue_item_free(JsonStreamQueueItem *q) { |
| 54 |
if (!q) |
| 55 |
return NULL; |
| 56 |
|
| 57 |
sd_json_variant_unref(q->data); |
| 58 |
close_many(q->fds, q->n_fds); |
| 59 |
|
| 60 |
return mfree(q); |
| 61 |
} |
| 62 |
|
| 63 |
static JsonStreamQueueItem* json_stream_queue_item_new(sd_json_variant *m, const int fds[], size_t n_fds) { |
| 64 |
JsonStreamQueueItem *q; |
| 65 |
|
| 66 |
assert(m); |
| 67 |
assert(fds || n_fds == 0); |
| 68 |
|
| 69 |
size_t sz = sizeof(int); |
| 70 |
if (!MUL_SAFE(&sz, sz, n_fds) || |
| 71 |
!INC_SAFE(&sz, offsetof(JsonStreamQueueItem, fds))) |
| 72 |
return NULL; |
| 73 |
|
| 74 |
q = malloc(sz); |
| 75 |
if (!q) |
| 76 |
return NULL; |
| 77 |
|
| 78 |
*q = (JsonStreamQueueItem) { |
| 79 |
.data = sd_json_variant_ref(m), |
| 80 |
.n_fds = n_fds, |
| 81 |
}; |
| 82 |
|
| 83 |
memcpy_safe(q->fds, fds, n_fds * sizeof(int)); |
| 84 |
|
| 85 |
return TAKE_PTR(q); |
| 86 |
} |
| 87 |
|
| 88 |
int json_stream_init(JsonStream *s, const JsonStreamParams *params) { |
| 89 |
assert(s); |
| 90 |
assert(params); |
| 91 |
assert(params->phase); |
| 92 |
assert(params->dispatch); |
| 93 |
|
| 94 |
char *delimiter = NULL; |
| 95 |
if (params->delimiter) { |
| 96 |
delimiter = strdup(params->delimiter); |
| 97 |
if (!delimiter) |
| 98 |
return -ENOMEM; |
| 99 |
} |
| 100 |
|
| 101 |
*s = (JsonStream) { |
| 102 |
.delimiter = delimiter, |
| 103 |
.buffer_max = params->buffer_max > 0 ? params->buffer_max : JSON_STREAM_BUFFER_MAX_DEFAULT, |
| 104 |
.read_chunk = params->read_chunk > 0 ? params->read_chunk : JSON_STREAM_READ_SIZE_DEFAULT, |
| 105 |
.queue_max = params->queue_max > 0 ? params->queue_max : JSON_STREAM_QUEUE_MAX_DEFAULT, |
| 106 |
.phase_cb = params->phase, |
| 107 |
.dispatch_cb = params->dispatch, |
| 108 |
.userdata = params->userdata, |
| 109 |
.input_fd = -EBADF, |
| 110 |
.output_fd = -EBADF, |
| 111 |
.timeout = USEC_INFINITY, |
| 112 |
.last_activity = USEC_INFINITY, |
| 113 |
.ucred = UCRED_INVALID, |
| 114 |
.peer_pidfd = -EBADF, |
| 115 |
.af = -1, |
| 116 |
}; |
| 117 |
|
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
static void json_stream_clear(JsonStream *s) { |
| 122 |
if (!s) |
| 123 |
return; |
| 124 |
|
| 125 |
json_stream_detach_event(s); |
| 126 |
|
| 127 |
s->delimiter = mfree(s->delimiter); |
| 128 |
s->description = mfree(s->description); |
| 129 |
|
| 130 |
if (s->input_fd != s->output_fd) { |
| 131 |
s->input_fd = safe_close(s->input_fd); |
| 132 |
s->output_fd = safe_close(s->output_fd); |
| 133 |
} else |
| 134 |
s->output_fd = s->input_fd = safe_close(s->input_fd); |
| 135 |
|
| 136 |
s->peer_pidfd = safe_close(s->peer_pidfd); |
| 137 |
s->ucred_acquired = false; |
| 138 |
s->af = -1; |
| 139 |
|
| 140 |
close_many(s->input_fds, s->n_input_fds); |
| 141 |
s->input_fds = mfree(s->input_fds); |
| 142 |
s->n_input_fds = 0; |
| 143 |
|
| 144 |
s->input_buffer = FLAGS_SET(s->flags, JSON_STREAM_INPUT_SENSITIVE) ? erase_and_free(s->input_buffer) : mfree(s->input_buffer); |
| 145 |
s->input_buffer_index = s->input_buffer_size = s->input_buffer_unscanned = 0; |
| 146 |
|
| 147 |
s->output_buffer = FLAGS_SET(s->flags, JSON_STREAM_OUTPUT_BUFFER_SENSITIVE) ? erase_and_free(s->output_buffer) : mfree(s->output_buffer); |
| 148 |
s->output_buffer_index = s->output_buffer_size = 0; |
| 149 |
s->flags &= ~JSON_STREAM_OUTPUT_BUFFER_SENSITIVE; |
| 150 |
|
| 151 |
s->input_control_buffer = mfree(s->input_control_buffer); |
| 152 |
s->input_control_buffer_size = 0; |
| 153 |
|
| 154 |
close_many(s->output_fds, s->n_output_fds); |
| 155 |
s->output_fds = mfree(s->output_fds); |
| 156 |
s->n_output_fds = 0; |
| 157 |
|
| 158 |
LIST_CLEAR(queue, s->output_queue, json_stream_queue_item_free); |
| 159 |
s->output_queue_tail = NULL; |
| 160 |
s->n_output_queue = 0; |
| 161 |
} |
| 162 |
|
| 163 |
void json_stream_done(JsonStream *s) { |
| 164 |
if (!s) |
| 165 |
return; |
| 166 |
|
| 167 |
json_stream_clear(s); |
| 168 |
} |
| 169 |
|
| 170 |
int json_stream_set_description(JsonStream *s, const char *description) { |
| 171 |
assert(s); |
| 172 |
return free_and_strdup(&s->description, description); |
| 173 |
} |
| 174 |
|
| 175 |
const char* json_stream_get_description(const JsonStream *s) { |
| 176 |
assert(s); |
| 177 |
return s->description; |
| 178 |
} |
| 179 |
|
| 180 |
int json_stream_connect_address(JsonStream *s, const char *address) { |
| 181 |
union sockaddr_union sockaddr; |
| 182 |
int r; |
| 183 |
|
| 184 |
assert(s); |
| 185 |
assert(address); |
| 186 |
|
| 187 |
_cleanup_close_ int sock_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
| 188 |
if (sock_fd < 0) |
| 189 |
return json_stream_log_errno(s, errno, "Failed to create AF_UNIX socket: %m"); |
| 190 |
|
| 191 |
sock_fd = fd_move_above_stdio(sock_fd); |
| 192 |
|
| 193 |
r = sockaddr_un_set_path(&sockaddr.un, address); |
| 194 |
if (r < 0) { |
| 195 |
if (r != -ENAMETOOLONG) |
| 196 |
return json_stream_log_errno(s, r, "Failed to set socket address '%s': %m", address); |
| 197 |
|
| 198 |
/* Path too long to fit into sockaddr_un, connect via O_PATH instead. */ |
| 199 |
r = connect_unix_path(sock_fd, AT_FDCWD, address); |
| 200 |
} else |
| 201 |
r = RET_NERRNO(connect(sock_fd, &sockaddr.sa, r)); |
| 202 |
|
| 203 |
if (r < 0) { |
| 204 |
if (!IN_SET(r, -EAGAIN, -EINPROGRESS)) |
| 205 |
return json_stream_log_errno(s, r, "Failed to connect to %s: %m", address); |
| 206 |
|
| 207 |
/* The connect() is being processed in the background. As long as that's the |
| 208 |
* case the socket is in a special state: we can poll it for POLLOUT, but |
| 209 |
* write()s before POLLOUT will fail with ENOTCONN (rather than EAGAIN). Since |
| 210 |
* ENOTCONN can mean two different things (not yet connected vs. already |
| 211 |
* disconnected), we track this as a separate flag. */ |
| 212 |
s->flags |= JSON_STREAM_CONNECTING; |
| 213 |
} |
| 214 |
|
| 215 |
int fd = TAKE_FD(sock_fd); |
| 216 |
return json_stream_attach_fds(s, fd, fd); |
| 217 |
} |
| 218 |
|
| 219 |
int json_stream_attach_fds(JsonStream *s, int input_fd, int output_fd) { |
| 220 |
struct stat st; |
| 221 |
|
| 222 |
assert(s); |
| 223 |
|
| 224 |
/* NB: input_fd and output_fd are donated to the JsonStream instance! */ |
| 225 |
|
| 226 |
if (s->input_fd != s->output_fd) { |
| 227 |
safe_close(s->input_fd); |
| 228 |
safe_close(s->output_fd); |
| 229 |
} else |
| 230 |
safe_close(s->input_fd); |
| 231 |
|
| 232 |
s->input_fd = input_fd; |
| 233 |
s->output_fd = output_fd; |
| 234 |
s->flags &= ~(JSON_STREAM_PREFER_READ|JSON_STREAM_PREFER_WRITE); |
| 235 |
|
| 236 |
/* Detect non-socket fds up front so the read/write paths use read()/write() for |
| 237 |
* non-socket fds and send()/recv() for sockets (mostly for MSG_NOSIGNAL). */ |
| 238 |
if (input_fd >= 0) { |
| 239 |
if (fstat(input_fd, &st) < 0) |
| 240 |
return -errno; |
| 241 |
if (!S_ISSOCK(st.st_mode)) |
| 242 |
s->flags |= JSON_STREAM_PREFER_READ; |
| 243 |
} |
| 244 |
|
| 245 |
if (output_fd >= 0 && output_fd != input_fd) { |
| 246 |
if (fstat(output_fd, &st) < 0) |
| 247 |
return -errno; |
| 248 |
if (!S_ISSOCK(st.st_mode)) |
| 249 |
s->flags |= JSON_STREAM_PREFER_WRITE; |
| 250 |
} else if (FLAGS_SET(s->flags, JSON_STREAM_PREFER_READ)) |
| 251 |
s->flags |= JSON_STREAM_PREFER_WRITE; |
| 252 |
|
| 253 |
return 0; |
| 254 |
} |
| 255 |
|
| 256 |
int json_stream_connect_fd_pair(JsonStream *s, int input_fd, int output_fd) { |
| 257 |
int r; |
| 258 |
|
| 259 |
assert(s); |
| 260 |
assert(input_fd >= 0); |
| 261 |
assert(output_fd >= 0); |
| 262 |
|
| 263 |
r = fd_nonblock(input_fd, true); |
| 264 |
if (r < 0) |
| 265 |
return json_stream_log_errno(s, r, "Failed to make input fd %d nonblocking: %m", input_fd); |
| 266 |
|
| 267 |
if (input_fd != output_fd) { |
| 268 |
r = fd_nonblock(output_fd, true); |
| 269 |
if (r < 0) |
| 270 |
return json_stream_log_errno(s, r, "Failed to make output fd %d nonblocking: %m", output_fd); |
| 271 |
} |
| 272 |
|
| 273 |
return json_stream_attach_fds(s, input_fd, output_fd); |
| 274 |
} |
| 275 |
|
| 276 |
bool json_stream_flags_set(const JsonStream *s, JsonStreamFlags flags) { |
| 277 |
assert(s); |
| 278 |
assert((flags & ~(JSON_STREAM_BOUNDED_READS|JSON_STREAM_INPUT_SENSITIVE|JSON_STREAM_ALLOW_FD_PASSING_INPUT|JSON_STREAM_ALLOW_FD_PASSING_OUTPUT)) == 0); |
| 279 |
|
| 280 |
return FLAGS_SET(s->flags, flags); |
| 281 |
} |
| 282 |
|
| 283 |
/* Multiple flags may be passed — all are set or cleared together. */ |
| 284 |
void json_stream_set_flags(JsonStream *s, JsonStreamFlags flags, bool b) { |
| 285 |
assert(s); |
| 286 |
assert((flags & ~(JSON_STREAM_BOUNDED_READS|JSON_STREAM_INPUT_SENSITIVE)) == 0); |
| 287 |
|
| 288 |
SET_FLAG(s->flags, flags, b); |
| 289 |
} |
| 290 |
|
| 291 |
bool json_stream_has_buffered_input(const JsonStream *s) { |
| 292 |
assert(s); |
| 293 |
return s->input_buffer_size > 0; |
| 294 |
} |
| 295 |
|
| 296 |
/* Query the consumer's current phase. The callback is mandatory (asserted at construction |
| 297 |
* time), so we can call it unconditionally. */ |
| 298 |
static JsonStreamPhase json_stream_current_phase(const JsonStream *s) { |
| 299 |
assert(s); |
| 300 |
return s->phase_cb(s->userdata); |
| 301 |
} |
| 302 |
|
| 303 |
/* Both READING and AWAITING_REPLY mean "we want POLLIN and would lose if the read side |
| 304 |
* died" — they only differ in whether the idle timeout is in force. */ |
| 305 |
static bool phase_is_reading(JsonStreamPhase p) { |
| 306 |
return IN_SET(p, JSON_STREAM_PHASE_READING, JSON_STREAM_PHASE_AWAITING_REPLY); |
| 307 |
} |
| 308 |
|
| 309 |
bool json_stream_should_disconnect(const JsonStream *s) { |
| 310 |
assert(s); |
| 311 |
|
| 312 |
/* Carefully decide when the consumer should initiate a teardown. We err on the side |
| 313 |
* of staying around so half-open connections can flush remaining data and reads can |
| 314 |
* surface buffered messages before we tear everything down. */ |
| 315 |
|
| 316 |
/* Wait until any in-flight async connect() completes — there's nothing reasonable |
| 317 |
* to do until we know whether the socket is connected or not. */ |
| 318 |
if (FLAGS_SET(s->flags, JSON_STREAM_CONNECTING)) |
| 319 |
return false; |
| 320 |
|
| 321 |
/* Still bytes to write and we can write? Stay around so the flush can complete. */ |
| 322 |
if (s->output_buffer_size > 0 && !FLAGS_SET(s->flags, JSON_STREAM_WRITE_DISCONNECTED)) |
| 323 |
return false; |
| 324 |
|
| 325 |
/* Both sides gone already? Then there's no point in lingering. */ |
| 326 |
if (FLAGS_SET(s->flags, JSON_STREAM_READ_DISCONNECTED|JSON_STREAM_WRITE_DISCONNECTED)) |
| 327 |
return true; |
| 328 |
|
| 329 |
JsonStreamPhase phase = json_stream_current_phase(s); |
| 330 |
|
| 331 |
/* Caller is waiting for input but the read side is shut down — we'll never see |
| 332 |
* another message. */ |
| 333 |
if (phase_is_reading(phase) && FLAGS_SET(s->flags, JSON_STREAM_READ_DISCONNECTED)) |
| 334 |
return true; |
| 335 |
|
| 336 |
/* Idle client whose write side has died, or we saw POLLHUP. We explicitly check for |
| 337 |
* POLLHUP because we likely won't notice the write side being down via send() if we |
| 338 |
* never wrote anything in the first place. */ |
| 339 |
if (phase == JSON_STREAM_PHASE_IDLE_CLIENT && |
| 340 |
(s->flags & (JSON_STREAM_WRITE_DISCONNECTED|JSON_STREAM_GOT_POLLHUP))) |
| 341 |
return true; |
| 342 |
|
| 343 |
/* Caller has more output to send but the peer hung up, and we're either out of |
| 344 |
* bytes or already saw a write error. Nothing left to do. */ |
| 345 |
if (phase == JSON_STREAM_PHASE_PENDING_OUTPUT && |
| 346 |
(FLAGS_SET(s->flags, JSON_STREAM_WRITE_DISCONNECTED) || s->output_buffer_size == 0) && |
| 347 |
FLAGS_SET(s->flags, JSON_STREAM_GOT_POLLHUP)) |
| 348 |
return true; |
| 349 |
|
| 350 |
return false; |
| 351 |
} |
| 352 |
|
| 353 |
int json_stream_get_events(const JsonStream *s) { |
| 354 |
int ret = 0; |
| 355 |
|
| 356 |
assert(s); |
| 357 |
|
| 358 |
/* While an asynchronous connect() is still in flight we only ask for POLLOUT, which |
| 359 |
* tells us once the connection is fully established. We must not read or write before |
| 360 |
* that. */ |
| 361 |
if (FLAGS_SET(s->flags, JSON_STREAM_CONNECTING)) |
| 362 |
return POLLOUT; |
| 363 |
|
| 364 |
if (phase_is_reading(json_stream_current_phase(s)) && |
| 365 |
!FLAGS_SET(s->flags, JSON_STREAM_READ_DISCONNECTED) && |
| 366 |
s->input_buffer_unscanned == 0) |
| 367 |
ret |= POLLIN; |
| 368 |
|
| 369 |
if (!FLAGS_SET(s->flags, JSON_STREAM_WRITE_DISCONNECTED) && (s->output_queue || s->output_buffer_size > 0)) |
| 370 |
ret |= POLLOUT; |
| 371 |
|
| 372 |
return ret; |
| 373 |
} |
| 374 |
|
| 375 |
static void json_stream_handle_revents(JsonStream *s, int revents) { |
| 376 |
assert(s); |
| 377 |
|
| 378 |
if (FLAGS_SET(s->flags, JSON_STREAM_CONNECTING)) { |
| 379 |
/* If we have seen POLLOUT or POLLHUP on a socket we are asynchronously waiting a |
| 380 |
* connect() to complete on, we know we are ready. We don't read the connection |
| 381 |
* error here though — we'll get it on the next read() or write(). */ |
| 382 |
if ((revents & (POLLOUT|POLLHUP)) == 0) |
| 383 |
return; |
| 384 |
|
| 385 |
json_stream_log(s, "Asynchronous connection completed."); |
| 386 |
s->flags &= ~JSON_STREAM_CONNECTING; |
| 387 |
return; |
| 388 |
} |
| 389 |
|
| 390 |
/* Note that we don't care much about POLLIN/POLLOUT here, we'll just try reading and |
| 391 |
* writing what we can. However, we do care about POLLHUP to detect connection |
| 392 |
* termination even if we momentarily don't want to read nor write anything. */ |
| 393 |
if (FLAGS_SET(revents, POLLHUP)) { |
| 394 |
json_stream_log(s, "Got POLLHUP from socket."); |
| 395 |
s->flags |= JSON_STREAM_GOT_POLLHUP; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
int json_stream_wait(JsonStream *s, usec_t timeout) { |
| 400 |
int events, r; |
| 401 |
|
| 402 |
assert(s); |
| 403 |
|
| 404 |
events = json_stream_get_events(s); |
| 405 |
if (events < 0) |
| 406 |
return events; |
| 407 |
|
| 408 |
/* MIN the caller's timeout with our own deadline (if any) so that we wake up to |
| 409 |
* fire the idle timeout. */ |
| 410 |
usec_t deadline = json_stream_get_timeout(s); |
| 411 |
if (deadline != USEC_INFINITY) |
| 412 |
timeout = MIN(timeout, usec_sub_unsigned(deadline, now(CLOCK_MONOTONIC))); |
| 413 |
|
| 414 |
struct pollfd pollfd[2]; |
| 415 |
size_t n_poll_fd = 0; |
| 416 |
|
| 417 |
if (s->input_fd == s->output_fd) { |
| 418 |
pollfd[n_poll_fd++] = (struct pollfd) { |
| 419 |
.fd = s->input_fd, |
| 420 |
.events = events, |
| 421 |
}; |
| 422 |
} else { |
| 423 |
pollfd[n_poll_fd++] = (struct pollfd) { |
| 424 |
.fd = s->input_fd, |
| 425 |
.events = events & POLLIN, |
| 426 |
}; |
| 427 |
pollfd[n_poll_fd++] = (struct pollfd) { |
| 428 |
.fd = s->output_fd, |
| 429 |
.events = events & POLLOUT, |
| 430 |
}; |
| 431 |
} |
| 432 |
|
| 433 |
r = ppoll_usec(pollfd, n_poll_fd, timeout); |
| 434 |
if (ERRNO_IS_NEG_TRANSIENT(r)) |
| 435 |
/* Treat EINTR as not a timeout, but also nothing happened, and the caller gets |
| 436 |
* a chance to call back into us. */ |
| 437 |
return 1; |
| 438 |
if (r <= 0) |
| 439 |
return r; |
| 440 |
|
| 441 |
int revents = 0; |
| 442 |
FOREACH_ARRAY(p, pollfd, n_poll_fd) |
| 443 |
revents |= p->revents; |
| 444 |
|
| 445 |
json_stream_handle_revents(s, revents); |
| 446 |
return 1; |
| 447 |
} |
| 448 |
|
| 449 |
/* ===== Timeout management ===== */ |
| 450 |
|
| 451 |
static usec_t json_stream_get_deadline(const JsonStream *s) { |
| 452 |
assert(s); |
| 453 |
|
| 454 |
return usec_add(s->last_activity, s->timeout); |
| 455 |
} |
| 456 |
|
| 457 |
usec_t json_stream_get_timeout(const JsonStream *s) { |
| 458 |
assert(s); |
| 459 |
|
| 460 |
/* The deadline is in force only when the consumer is in PHASE_AWAITING_REPLY. In |
| 461 |
* other phases (idle server, between operations) we ignore the cached deadline even |
| 462 |
* if it's still set from a previous operation. */ |
| 463 |
if (json_stream_current_phase(s) != JSON_STREAM_PHASE_AWAITING_REPLY) |
| 464 |
return USEC_INFINITY; |
| 465 |
|
| 466 |
return json_stream_get_deadline(s); |
| 467 |
} |
| 468 |
|
| 469 |
static void json_stream_rearm_time_source(JsonStream *s) { |
| 470 |
int r; |
| 471 |
|
| 472 |
assert(s); |
| 473 |
|
| 474 |
if (!s->time_event_source) |
| 475 |
return; |
| 476 |
|
| 477 |
usec_t deadline = json_stream_get_timeout(s); |
| 478 |
if (deadline == USEC_INFINITY) { |
| 479 |
(void) sd_event_source_set_enabled(s->time_event_source, SD_EVENT_OFF); |
| 480 |
return; |
| 481 |
} |
| 482 |
|
| 483 |
r = sd_event_source_set_time(s->time_event_source, deadline); |
| 484 |
if (r < 0) { |
| 485 |
json_stream_log_errno(s, r, "Failed to set time source deadline: %m"); |
| 486 |
return; |
| 487 |
} |
| 488 |
|
| 489 |
(void) sd_event_source_set_enabled(s->time_event_source, SD_EVENT_ON); |
| 490 |
} |
| 491 |
|
| 492 |
void json_stream_set_timeout(JsonStream *s, usec_t timeout) { |
| 493 |
assert(s); |
| 494 |
|
| 495 |
s->timeout = timeout; |
| 496 |
|
| 497 |
/* If the configured timeout changes mid-flight, rearm the time source so the new |
| 498 |
* deadline takes effect immediately rather than waiting for the next mark_activity |
| 499 |
* or successful write. */ |
| 500 |
json_stream_rearm_time_source(s); |
| 501 |
} |
| 502 |
|
| 503 |
void json_stream_mark_activity(JsonStream *s) { |
| 504 |
assert(s); |
| 505 |
|
| 506 |
s->last_activity = json_stream_now(s); |
| 507 |
json_stream_rearm_time_source(s); |
| 508 |
} |
| 509 |
|
| 510 |
static int json_stream_acquire_peer_ucred(JsonStream *s, struct ucred *ret) { |
| 511 |
int r; |
| 512 |
|
| 513 |
assert(s); |
| 514 |
assert(ret); |
| 515 |
|
| 516 |
if (!s->ucred_acquired) { |
| 517 |
/* Peer credentials only make sense for a bidirectional socket. */ |
| 518 |
if (s->input_fd != s->output_fd) |
| 519 |
return -EBADF; |
| 520 |
|
| 521 |
r = getpeercred(s->input_fd, &s->ucred); |
| 522 |
if (r < 0) |
| 523 |
return r; |
| 524 |
|
| 525 |
s->ucred_acquired = true; |
| 526 |
} |
| 527 |
|
| 528 |
*ret = s->ucred; |
| 529 |
return 0; |
| 530 |
} |
| 531 |
|
| 532 |
int json_stream_acquire_peer_uid(JsonStream *s, uid_t *ret) { |
| 533 |
struct ucred ucred; |
| 534 |
int r; |
| 535 |
|
| 536 |
assert(s); |
| 537 |
assert(ret); |
| 538 |
|
| 539 |
r = json_stream_acquire_peer_ucred(s, &ucred); |
| 540 |
if (r < 0) |
| 541 |
return json_stream_log_errno(s, r, "Failed to acquire credentials: %m"); |
| 542 |
|
| 543 |
if (!uid_is_valid(ucred.uid)) |
| 544 |
return json_stream_log_errno(s, SYNTHETIC_ERRNO(ENODATA), "Peer UID is invalid."); |
| 545 |
|
| 546 |
*ret = ucred.uid; |
| 547 |
return 0; |
| 548 |
} |
| 549 |
|
| 550 |
int json_stream_acquire_peer_gid(JsonStream *s, gid_t *ret) { |
| 551 |
struct ucred ucred; |
| 552 |
int r; |
| 553 |
|
| 554 |
assert(s); |
| 555 |
assert(ret); |
| 556 |
|
| 557 |
r = json_stream_acquire_peer_ucred(s, &ucred); |
| 558 |
if (r < 0) |
| 559 |
return json_stream_log_errno(s, r, "Failed to acquire credentials: %m"); |
| 560 |
|
| 561 |
if (!gid_is_valid(ucred.gid)) |
| 562 |
return json_stream_log_errno(s, SYNTHETIC_ERRNO(ENODATA), "Peer GID is invalid."); |
| 563 |
|
| 564 |
*ret = ucred.gid; |
| 565 |
return 0; |
| 566 |
} |
| 567 |
|
| 568 |
int json_stream_acquire_peer_pid(JsonStream *s, pid_t *ret) { |
| 569 |
struct ucred ucred; |
| 570 |
int r; |
| 571 |
|
| 572 |
assert(s); |
| 573 |
assert(ret); |
| 574 |
|
| 575 |
r = json_stream_acquire_peer_ucred(s, &ucred); |
| 576 |
if (r < 0) |
| 577 |
return json_stream_log_errno(s, r, "Failed to acquire credentials: %m"); |
| 578 |
|
| 579 |
if (!pid_is_valid(ucred.pid)) |
| 580 |
return json_stream_log_errno(s, SYNTHETIC_ERRNO(ENODATA), "Peer PID is invalid."); |
| 581 |
|
| 582 |
*ret = ucred.pid; |
| 583 |
return 0; |
| 584 |
} |
| 585 |
|
| 586 |
int json_stream_get_peer_ucred(const JsonStream *s, struct ucred *ret) { |
| 587 |
assert(s); |
| 588 |
assert(ret); |
| 589 |
|
| 590 |
if (!s->ucred_acquired) |
| 591 |
return -ENODATA; |
| 592 |
|
| 593 |
*ret = s->ucred; |
| 594 |
return 0; |
| 595 |
} |
| 596 |
|
| 597 |
void json_stream_set_peer_ucred(JsonStream *s, const struct ucred *ucred) { |
| 598 |
assert(s); |
| 599 |
assert(ucred); |
| 600 |
|
| 601 |
s->ucred = *ucred; |
| 602 |
s->ucred_acquired = true; |
| 603 |
} |
| 604 |
|
| 605 |
int json_stream_acquire_peer_pidfd(JsonStream *s) { |
| 606 |
assert(s); |
| 607 |
|
| 608 |
if (s->peer_pidfd >= 0) |
| 609 |
return s->peer_pidfd; |
| 610 |
|
| 611 |
if (s->input_fd != s->output_fd) |
| 612 |
return json_stream_log_errno(s, SYNTHETIC_ERRNO(EBADF), "Failed to acquire pidfd of peer: separate input/output fds"); |
| 613 |
|
| 614 |
s->peer_pidfd = getpeerpidfd(s->input_fd); |
| 615 |
if (s->peer_pidfd < 0) |
| 616 |
return json_stream_log_errno(s, s->peer_pidfd, "Failed to acquire pidfd of peer: %m"); |
| 617 |
|
| 618 |
return s->peer_pidfd; |
| 619 |
} |
| 620 |
|
| 621 |
static int json_stream_verify_unix_socket(JsonStream *s) { |
| 622 |
assert(s); |
| 623 |
|
| 624 |
/* Returns: |
| 625 |
* • 0 if this is an AF_UNIX socket |
| 626 |
* • -ENOTSOCK if this is not a socket at all |
| 627 |
* • -ENOMEDIUM if this is a socket, but not an AF_UNIX socket |
| 628 |
* |
| 629 |
* The result is cached after the first call. af < 0 = unchecked, af == AF_UNSPEC = |
| 630 |
* checked but not a socket, otherwise af is the resolved address family. */ |
| 631 |
|
| 632 |
if (s->af < 0) { |
| 633 |
/* If we have distinct input + output fds, we don't consider ourselves to be |
| 634 |
* connected via a regular AF_UNIX socket. */ |
| 635 |
if (s->input_fd != s->output_fd) { |
| 636 |
s->af = AF_UNSPEC; |
| 637 |
return -ENOTSOCK; |
| 638 |
} |
| 639 |
|
| 640 |
struct stat st; |
| 641 |
|
| 642 |
if (fstat(s->input_fd, &st) < 0) |
| 643 |
return -errno; |
| 644 |
if (!S_ISSOCK(st.st_mode)) { |
| 645 |
s->af = AF_UNSPEC; |
| 646 |
return -ENOTSOCK; |
| 647 |
} |
| 648 |
|
| 649 |
s->af = socket_get_family(s->input_fd); |
| 650 |
if (s->af < 0) |
| 651 |
return s->af; |
| 652 |
} |
| 653 |
|
| 654 |
if (s->af == AF_UNIX) |
| 655 |
return 0; |
| 656 |
if (s->af == AF_UNSPEC) |
| 657 |
return -ENOTSOCK; |
| 658 |
|
| 659 |
return -ENOMEDIUM; |
| 660 |
} |
| 661 |
|
| 662 |
int json_stream_set_allow_fd_passing_input(JsonStream *s, bool enabled, bool with_sockopt) { |
| 663 |
int r; |
| 664 |
|
| 665 |
assert(s); |
| 666 |
|
| 667 |
if (FLAGS_SET(s->flags, JSON_STREAM_ALLOW_FD_PASSING_INPUT) == enabled) |
| 668 |
return 0; |
| 669 |
|
| 670 |
r = json_stream_verify_unix_socket(s); |
| 671 |
if (r < 0) { |
| 672 |
/* If the caller is disabling, accept the verify failure silently — we just |
| 673 |
* leave the flag as it was (or set it to false if currently true). */ |
| 674 |
if (!enabled) { |
| 675 |
s->flags &= ~JSON_STREAM_ALLOW_FD_PASSING_INPUT; |
| 676 |
return 0; |
| 677 |
} |
| 678 |
return r; |
| 679 |
} |
| 680 |
|
| 681 |
if (with_sockopt) { |
| 682 |
r = setsockopt_int(s->input_fd, SOL_SOCKET, SO_PASSRIGHTS, enabled); |
| 683 |
if (r < 0 && !ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 684 |
json_stream_log_errno(s, r, "Failed to set SO_PASSRIGHTS socket option: %m"); |
| 685 |
} |
| 686 |
|
| 687 |
SET_FLAG(s->flags, JSON_STREAM_ALLOW_FD_PASSING_INPUT, enabled); |
| 688 |
return 1; |
| 689 |
} |
| 690 |
|
| 691 |
int json_stream_set_allow_fd_passing_output(JsonStream *s, bool enabled) { |
| 692 |
int r; |
| 693 |
|
| 694 |
assert(s); |
| 695 |
|
| 696 |
if (FLAGS_SET(s->flags, JSON_STREAM_ALLOW_FD_PASSING_OUTPUT) == enabled) |
| 697 |
return 0; |
| 698 |
|
| 699 |
r = json_stream_verify_unix_socket(s); |
| 700 |
if (r < 0) |
| 701 |
return r; |
| 702 |
|
| 703 |
SET_FLAG(s->flags, JSON_STREAM_ALLOW_FD_PASSING_OUTPUT, enabled); |
| 704 |
return 1; |
| 705 |
} |
| 706 |
|
| 707 |
/* ===== sd-event integration ===== */ |
| 708 |
|
| 709 |
static int json_stream_io_callback(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
| 710 |
JsonStream *s = ASSERT_PTR(userdata); |
| 711 |
int r; |
| 712 |
|
| 713 |
json_stream_handle_revents(s, revents); |
| 714 |
|
| 715 |
r = s->dispatch_cb(s->userdata); |
| 716 |
if (r < 0) |
| 717 |
json_stream_log_errno(s, r, "Dispatch callback failed, ignoring: %m"); |
| 718 |
|
| 719 |
return 1; |
| 720 |
} |
| 721 |
|
| 722 |
static int json_stream_time_callback(sd_event_source *source, uint64_t usec, void *userdata) { |
| 723 |
JsonStream *s = ASSERT_PTR(userdata); |
| 724 |
int r; |
| 725 |
|
| 726 |
/* Disable the source: it must not fire again until activity is marked. The consumer |
| 727 |
* notices the timeout by comparing now() to json_stream_get_timeout() in its dispatch |
| 728 |
* callback. */ |
| 729 |
(void) sd_event_source_set_enabled(s->time_event_source, SD_EVENT_OFF); |
| 730 |
|
| 731 |
r = s->dispatch_cb(s->userdata); |
| 732 |
if (r < 0) |
| 733 |
json_stream_log_errno(s, r, "Dispatch callback failed, ignoring: %m"); |
| 734 |
|
| 735 |
return 1; |
| 736 |
} |
| 737 |
|
| 738 |
static int json_stream_prepare_callback(sd_event_source *source, void *userdata) { |
| 739 |
JsonStream *s = ASSERT_PTR(userdata); |
| 740 |
int r, e; |
| 741 |
|
| 742 |
e = json_stream_get_events(s); |
| 743 |
if (e < 0) |
| 744 |
return e; |
| 745 |
|
| 746 |
if (s->input_event_source == s->output_event_source) |
| 747 |
/* Same fd for input + output */ |
| 748 |
r = sd_event_source_set_io_events(s->input_event_source, e); |
| 749 |
else { |
| 750 |
r = sd_event_source_set_io_events(s->input_event_source, e & POLLIN); |
| 751 |
if (r >= 0) |
| 752 |
r = sd_event_source_set_io_events(s->output_event_source, e & POLLOUT); |
| 753 |
} |
| 754 |
if (r < 0) |
| 755 |
return json_stream_log_errno(s, r, "Failed to set io events: %m"); |
| 756 |
|
| 757 |
/* Rearm the timeout on every prepare cycle so that phase transitions (e.g. entering |
| 758 |
* AWAITING_REPLY) are picked up without requiring the consumer to explicitly call |
| 759 |
* mark_activity at every state change. */ |
| 760 |
json_stream_rearm_time_source(s); |
| 761 |
|
| 762 |
return 1; |
| 763 |
} |
| 764 |
|
| 765 |
void json_stream_detach_event(JsonStream *s) { |
| 766 |
if (!s) |
| 767 |
return; |
| 768 |
|
| 769 |
s->input_event_source = sd_event_source_disable_unref(s->input_event_source); |
| 770 |
s->output_event_source = sd_event_source_disable_unref(s->output_event_source); |
| 771 |
s->time_event_source = sd_event_source_disable_unref(s->time_event_source); |
| 772 |
s->event = sd_event_unref(s->event); |
| 773 |
} |
| 774 |
|
| 775 |
sd_event* json_stream_get_event(const JsonStream *s) { |
| 776 |
assert(s); |
| 777 |
return s->event; |
| 778 |
} |
| 779 |
|
| 780 |
int json_stream_attach_event(JsonStream *s, sd_event *event, int64_t priority) { |
| 781 |
int r; |
| 782 |
|
| 783 |
assert(s); |
| 784 |
assert(!s->event); |
| 785 |
assert(s->input_fd >= 0); |
| 786 |
assert(s->output_fd >= 0); |
| 787 |
|
| 788 |
if (event) |
| 789 |
s->event = sd_event_ref(event); |
| 790 |
else { |
| 791 |
r = sd_event_default(&s->event); |
| 792 |
if (r < 0) |
| 793 |
return json_stream_log_errno(s, r, "Failed to acquire default event loop: %m"); |
| 794 |
} |
| 795 |
|
| 796 |
r = sd_event_add_io(s->event, &s->input_event_source, s->input_fd, 0, json_stream_io_callback, s); |
| 797 |
if (r < 0) |
| 798 |
goto fail; |
| 799 |
|
| 800 |
r = sd_event_source_set_prepare(s->input_event_source, json_stream_prepare_callback); |
| 801 |
if (r < 0) |
| 802 |
goto fail; |
| 803 |
|
| 804 |
r = sd_event_source_set_priority(s->input_event_source, priority); |
| 805 |
if (r < 0) |
| 806 |
goto fail; |
| 807 |
|
| 808 |
(void) sd_event_source_set_description(s->input_event_source, "json-stream-input"); |
| 809 |
|
| 810 |
if (s->input_fd == s->output_fd) |
| 811 |
s->output_event_source = sd_event_source_ref(s->input_event_source); |
| 812 |
else { |
| 813 |
r = sd_event_add_io(s->event, &s->output_event_source, s->output_fd, 0, json_stream_io_callback, s); |
| 814 |
if (r < 0) |
| 815 |
goto fail; |
| 816 |
|
| 817 |
r = sd_event_source_set_priority(s->output_event_source, priority); |
| 818 |
if (r < 0) |
| 819 |
goto fail; |
| 820 |
|
| 821 |
(void) sd_event_source_set_description(s->output_event_source, "json-stream-output"); |
| 822 |
} |
| 823 |
|
| 824 |
r = sd_event_add_time(s->event, &s->time_event_source, CLOCK_MONOTONIC, /* usec= */ 0, /* accuracy= */ 0, |
| 825 |
json_stream_time_callback, s); |
| 826 |
if (r < 0) |
| 827 |
goto fail; |
| 828 |
|
| 829 |
r = sd_event_source_set_priority(s->time_event_source, priority); |
| 830 |
if (r < 0) |
| 831 |
goto fail; |
| 832 |
|
| 833 |
(void) sd_event_source_set_description(s->time_event_source, "json-stream-time"); |
| 834 |
|
| 835 |
/* Initially disabled — only enabled by mark_activity once a timeout is configured. */ |
| 836 |
(void) sd_event_source_set_enabled(s->time_event_source, SD_EVENT_OFF); |
| 837 |
json_stream_rearm_time_source(s); |
| 838 |
|
| 839 |
return 0; |
| 840 |
|
| 841 |
fail: |
| 842 |
json_stream_log_errno(s, r, "Failed to attach event source: %m"); |
| 843 |
json_stream_detach_event(s); |
| 844 |
return r; |
| 845 |
} |
| 846 |
|
| 847 |
int json_stream_flush(JsonStream *s) { |
| 848 |
int ret = 0, r; |
| 849 |
|
| 850 |
assert(s); |
| 851 |
|
| 852 |
for (;;) { |
| 853 |
if (s->output_buffer_size == 0 && !s->output_queue) |
| 854 |
break; |
| 855 |
if (FLAGS_SET(s->flags, JSON_STREAM_WRITE_DISCONNECTED)) |
| 856 |
return -ECONNRESET; |
| 857 |
|
| 858 |
r = json_stream_write(s); |
| 859 |
if (r < 0) |
| 860 |
return r; |
| 861 |
if (r > 0) { |
| 862 |
ret = 1; |
| 863 |
continue; |
| 864 |
} |
| 865 |
|
| 866 |
r = json_stream_wait(s, USEC_INFINITY); |
| 867 |
if (ERRNO_IS_NEG_TRANSIENT(r)) |
| 868 |
continue; |
| 869 |
if (r < 0) |
| 870 |
return json_stream_log_errno(s, r, "Poll failed on fd: %m"); |
| 871 |
assert(r > 0); |
| 872 |
} |
| 873 |
|
| 874 |
return ret; |
| 875 |
} |
| 876 |
|
| 877 |
int json_stream_peek_input_fd(const JsonStream *s, size_t i) { |
| 878 |
assert(s); |
| 879 |
|
| 880 |
if (i >= s->n_input_fds) |
| 881 |
return -ENXIO; |
| 882 |
|
| 883 |
return s->input_fds[i]; |
| 884 |
} |
| 885 |
|
| 886 |
int json_stream_take_input_fd(JsonStream *s, size_t i) { |
| 887 |
assert(s); |
| 888 |
|
| 889 |
if (i >= s->n_input_fds) |
| 890 |
return -ENXIO; |
| 891 |
|
| 892 |
return TAKE_FD(s->input_fds[i]); |
| 893 |
} |
| 894 |
|
| 895 |
size_t json_stream_get_n_input_fds(const JsonStream *s) { |
| 896 |
assert(s); |
| 897 |
return s->n_input_fds; |
| 898 |
} |
| 899 |
|
| 900 |
void json_stream_close_input_fds(JsonStream *s) { |
| 901 |
assert(s); |
| 902 |
|
| 903 |
close_many(s->input_fds, s->n_input_fds); |
| 904 |
s->input_fds = mfree(s->input_fds); |
| 905 |
s->n_input_fds = 0; |
| 906 |
} |
| 907 |
|
| 908 |
/* ===== Output formatting ===== */ |
| 909 |
|
| 910 |
static int json_stream_format_json(JsonStream *s, sd_json_variant *m) { |
| 911 |
_cleanup_(erase_and_freep) char *text = NULL; |
| 912 |
ssize_t sz, r; |
| 913 |
|
| 914 |
assert(s); |
| 915 |
assert(m); |
| 916 |
|
| 917 |
sz = sd_json_variant_format(m, /* flags= */ 0, &text); |
| 918 |
if (sz < 0) |
| 919 |
return sz; |
| 920 |
assert(text[sz] == '\0'); |
| 921 |
|
| 922 |
size_t dsz = json_stream_delimiter_size(s); |
| 923 |
|
| 924 |
/* Append the framing delimiter after the formatted JSON. For varlink (delimiter == |
| 925 |
* NULL) this keeps the trailing NUL already placed by sd_json_variant_format(); for |
| 926 |
* multi-char delimiters (e.g. "\r\n") we grow the buffer and copy them in. */ |
| 927 |
if (s->delimiter) { |
| 928 |
if (!GREEDY_REALLOC(text, sz + dsz)) |
| 929 |
return -ENOMEM; |
| 930 |
memcpy(text + sz, s->delimiter, dsz); |
| 931 |
} |
| 932 |
|
| 933 |
if (s->output_buffer_size + sz + dsz > s->buffer_max) |
| 934 |
return -ENOBUFS; |
| 935 |
|
| 936 |
if (DEBUG_LOGGING) { |
| 937 |
_cleanup_(erase_and_freep) char *censored_text = NULL; |
| 938 |
|
| 939 |
/* Suppress sensitive fields in the debug output */ |
| 940 |
r = sd_json_variant_format(m, SD_JSON_FORMAT_CENSOR_SENSITIVE, &censored_text); |
| 941 |
if (r >= 0) |
| 942 |
json_stream_log(s, "Sending message: %s", censored_text); |
| 943 |
} |
| 944 |
|
| 945 |
if (s->output_buffer_size == 0) { |
| 946 |
if (FLAGS_SET(s->flags, JSON_STREAM_OUTPUT_BUFFER_SENSITIVE)) { |
| 947 |
s->output_buffer = erase_and_free(s->output_buffer); |
| 948 |
s->flags &= ~JSON_STREAM_OUTPUT_BUFFER_SENSITIVE; |
| 949 |
} |
| 950 |
|
| 951 |
free_and_replace(s->output_buffer, text); |
| 952 |
|
| 953 |
s->output_buffer_size = sz + dsz; |
| 954 |
s->output_buffer_index = 0; |
| 955 |
|
| 956 |
} else if (!FLAGS_SET(s->flags, JSON_STREAM_OUTPUT_BUFFER_SENSITIVE) && s->output_buffer_index == 0) { |
| 957 |
if (!GREEDY_REALLOC(s->output_buffer, s->output_buffer_size + sz + dsz)) |
| 958 |
return -ENOMEM; |
| 959 |
|
| 960 |
memcpy(s->output_buffer + s->output_buffer_size, text, sz + dsz); |
| 961 |
s->output_buffer_size += sz + dsz; |
| 962 |
} else { |
| 963 |
const size_t new_size = s->output_buffer_size + sz + dsz; |
| 964 |
|
| 965 |
char *n = new(char, new_size); |
| 966 |
if (!n) |
| 967 |
return -ENOMEM; |
| 968 |
|
| 969 |
memcpy(mempcpy(n, s->output_buffer + s->output_buffer_index, s->output_buffer_size), text, sz + dsz); |
| 970 |
|
| 971 |
if (FLAGS_SET(s->flags, JSON_STREAM_OUTPUT_BUFFER_SENSITIVE)) |
| 972 |
s->output_buffer = erase_and_free(s->output_buffer); |
| 973 |
else |
| 974 |
free(s->output_buffer); |
| 975 |
s->output_buffer = n; |
| 976 |
s->output_buffer_size = new_size; |
| 977 |
s->output_buffer_index = 0; |
| 978 |
} |
| 979 |
|
| 980 |
if (sd_json_variant_is_sensitive_recursive(m)) |
| 981 |
s->flags |= JSON_STREAM_OUTPUT_BUFFER_SENSITIVE; |
| 982 |
else |
| 983 |
text = mfree(text); /* Skip the erase_and_free() destructor declared above */ |
| 984 |
|
| 985 |
return 0; |
| 986 |
} |
| 987 |
|
| 988 |
static int json_stream_format_queue(JsonStream *s) { |
| 989 |
int r; |
| 990 |
|
| 991 |
assert(s); |
| 992 |
|
| 993 |
/* Drain entries out of the output queue and format them into the output buffer. |
| 994 |
* Stop if there are unwritten output_fds or if the next item carries fds but |
| 995 |
* the output buffer is non-empty, since adding more would corrupt the fd boundary. */ |
| 996 |
|
| 997 |
while (s->output_queue) { |
| 998 |
assert(s->n_output_queue > 0); |
| 999 |
|
| 1000 |
if (s->n_output_fds > 0) |
| 1001 |
return 0; |
| 1002 |
|
| 1003 |
JsonStreamQueueItem *q = s->output_queue; |
| 1004 |
|
| 1005 |
/* If the next item carries fds but the output buffer still holds bytes from |
| 1006 |
* a prior fast-path enqueue or a partial write, we must not concatenate its |
| 1007 |
* JSON into that same buffer: the subsequent sendmsg() in json_stream_write() |
| 1008 |
* would attach the fds to the combined bytes and break the message-to-fd boundary. |
| 1009 |
* Stop here and let json_stream_write() drain the buffer first; the next write() |
| 1010 |
* call will pull this item into a clean buffer. |
| 1011 |
* |
| 1012 |
* Note: this only produces a difference on SOCK_SEQPACKET / SOCK_DGRAM, where |
| 1013 |
* each sendmsg() is its own datagram with its own SCM_RIGHTS cmsg. On AF_UNIX |
| 1014 |
* SOCK_STREAM the kernel absorbs a preceding non-scm skb forward into the |
| 1015 |
* next scm-bearing skb's recv, so per-sendmsg separation is invisible to the |
| 1016 |
* receiver anyway. Kept as cheap defensive sender hygiene that's necessary |
| 1017 |
* the moment a SEQPACKET/DGRAM consumer wires JsonStream up. */ |
| 1018 |
if (q->n_fds > 0 && s->output_buffer_size > 0) |
| 1019 |
return 0; |
| 1020 |
|
| 1021 |
_cleanup_free_ int *array = NULL; |
| 1022 |
if (q->n_fds > 0) { |
| 1023 |
array = newdup(int, q->fds, q->n_fds); |
| 1024 |
if (!array) |
| 1025 |
return -ENOMEM; |
| 1026 |
} |
| 1027 |
|
| 1028 |
r = json_stream_format_json(s, q->data); |
| 1029 |
if (r < 0) |
| 1030 |
return r; |
| 1031 |
|
| 1032 |
free_and_replace(s->output_fds, array); |
| 1033 |
s->n_output_fds = q->n_fds; |
| 1034 |
q->n_fds = 0; |
| 1035 |
|
| 1036 |
LIST_REMOVE(queue, s->output_queue, q); |
| 1037 |
if (!s->output_queue) |
| 1038 |
s->output_queue_tail = NULL; |
| 1039 |
s->n_output_queue--; |
| 1040 |
|
| 1041 |
json_stream_queue_item_free(q); |
| 1042 |
} |
| 1043 |
|
| 1044 |
return 0; |
| 1045 |
} |
| 1046 |
|
| 1047 |
int json_stream_enqueue_full(JsonStream *s, sd_json_variant *m, const int fds[], size_t n_fds) { |
| 1048 |
assert(s); |
| 1049 |
assert(m); |
| 1050 |
assert(fds || n_fds == 0); |
| 1051 |
|
| 1052 |
/* Fast path: no fds and no items currently queued — append directly into the |
| 1053 |
* output buffer to avoid the queue allocation. */ |
| 1054 |
if (n_fds == 0 && !s->output_queue) |
| 1055 |
return json_stream_format_json(s, m); |
| 1056 |
|
| 1057 |
if (s->n_output_queue >= s->queue_max) |
| 1058 |
return -ENOBUFS; |
| 1059 |
|
| 1060 |
JsonStreamQueueItem *q = json_stream_queue_item_new(m, fds, n_fds); |
| 1061 |
if (!q) |
| 1062 |
return -ENOMEM; |
| 1063 |
|
| 1064 |
LIST_INSERT_AFTER(queue, s->output_queue, s->output_queue_tail, q); |
| 1065 |
s->output_queue_tail = q; |
| 1066 |
s->n_output_queue++; |
| 1067 |
return 0; |
| 1068 |
} |
| 1069 |
|
| 1070 |
/* ===== Write side ===== */ |
| 1071 |
|
| 1072 |
int json_stream_write(JsonStream *s) { |
| 1073 |
ssize_t n; |
| 1074 |
int r; |
| 1075 |
|
| 1076 |
assert(s); |
| 1077 |
|
| 1078 |
if (FLAGS_SET(s->flags, JSON_STREAM_CONNECTING)) |
| 1079 |
return 0; |
| 1080 |
if (FLAGS_SET(s->flags, JSON_STREAM_WRITE_DISCONNECTED)) |
| 1081 |
return 0; |
| 1082 |
|
| 1083 |
/* Drain the deferred queue into the output buffer if possible */ |
| 1084 |
r = json_stream_format_queue(s); |
| 1085 |
if (r < 0) |
| 1086 |
return r; |
| 1087 |
|
| 1088 |
if (s->output_buffer_size == 0) |
| 1089 |
return 0; |
| 1090 |
|
| 1091 |
assert(s->output_fd >= 0); |
| 1092 |
|
| 1093 |
if (s->n_output_fds > 0) { |
| 1094 |
struct iovec iov = { |
| 1095 |
.iov_base = s->output_buffer + s->output_buffer_index, |
| 1096 |
.iov_len = s->output_buffer_size, |
| 1097 |
}; |
| 1098 |
struct msghdr mh = { |
| 1099 |
.msg_iov = &iov, |
| 1100 |
.msg_iovlen = 1, |
| 1101 |
.msg_controllen = CMSG_SPACE(sizeof(int) * s->n_output_fds), |
| 1102 |
}; |
| 1103 |
|
| 1104 |
mh.msg_control = alloca0(mh.msg_controllen); |
| 1105 |
|
| 1106 |
struct cmsghdr *control = CMSG_FIRSTHDR(&mh); |
| 1107 |
control->cmsg_len = CMSG_LEN(sizeof(int) * s->n_output_fds); |
| 1108 |
control->cmsg_level = SOL_SOCKET; |
| 1109 |
control->cmsg_type = SCM_RIGHTS; |
| 1110 |
memcpy(CMSG_DATA(control), s->output_fds, sizeof(int) * s->n_output_fds); |
| 1111 |
|
| 1112 |
n = sendmsg(s->output_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL); |
| 1113 |
} else if (FLAGS_SET(s->flags, JSON_STREAM_PREFER_WRITE)) |
| 1114 |
n = write(s->output_fd, s->output_buffer + s->output_buffer_index, s->output_buffer_size); |
| 1115 |
else |
| 1116 |
n = send(s->output_fd, s->output_buffer + s->output_buffer_index, s->output_buffer_size, MSG_DONTWAIT|MSG_NOSIGNAL); |
| 1117 |
if (n < 0) { |
| 1118 |
if (ERRNO_IS_TRANSIENT(errno)) |
| 1119 |
return 0; |
| 1120 |
|
| 1121 |
if (ERRNO_IS_DISCONNECT(errno)) { |
| 1122 |
s->flags |= JSON_STREAM_WRITE_DISCONNECTED; |
| 1123 |
return 1; |
| 1124 |
} |
| 1125 |
|
| 1126 |
return -errno; |
| 1127 |
} |
| 1128 |
|
| 1129 |
if (FLAGS_SET(s->flags, JSON_STREAM_OUTPUT_BUFFER_SENSITIVE)) |
| 1130 |
explicit_bzero_safe(s->output_buffer + s->output_buffer_index, n); |
| 1131 |
|
| 1132 |
s->output_buffer_size -= n; |
| 1133 |
|
| 1134 |
if (s->output_buffer_size == 0) { |
| 1135 |
s->output_buffer_index = 0; |
| 1136 |
s->flags &= ~JSON_STREAM_OUTPUT_BUFFER_SENSITIVE; |
| 1137 |
} else |
| 1138 |
s->output_buffer_index += n; |
| 1139 |
|
| 1140 |
close_many(s->output_fds, s->n_output_fds); |
| 1141 |
s->n_output_fds = 0; |
| 1142 |
|
| 1143 |
/* Refresh activity timestamp on real progress (and rearm the time source if attached |
| 1144 |
* to an event loop). */ |
| 1145 |
s->last_activity = json_stream_now(s); |
| 1146 |
json_stream_rearm_time_source(s); |
| 1147 |
|
| 1148 |
return 1; |
| 1149 |
} |
| 1150 |
|
| 1151 |
/* ===== Read side ===== */ |
| 1152 |
|
| 1153 |
/* In bounded-reads mode, peek at the socket data to find the delimiter and return a read |
| 1154 |
* size that won't consume past it. This prevents over-reading data that belongs to whatever |
| 1155 |
* protocol the socket is being handed off to. Falls back to byte-by-byte for non-socket fds |
| 1156 |
* where MSG_PEEK is not available. */ |
| 1157 |
static ssize_t json_stream_peek_message_boundary(JsonStream *s, void *p, size_t rs) { |
| 1158 |
assert(s); |
| 1159 |
|
| 1160 |
if (!FLAGS_SET(s->flags, JSON_STREAM_BOUNDED_READS)) |
| 1161 |
return rs; |
| 1162 |
|
| 1163 |
if (FLAGS_SET(s->flags, JSON_STREAM_PREFER_READ)) |
| 1164 |
return 1; |
| 1165 |
|
| 1166 |
ssize_t peeked = recv(s->input_fd, p, rs, MSG_PEEK|MSG_DONTWAIT); |
| 1167 |
if (peeked < 0) { |
| 1168 |
if (!ERRNO_IS_TRANSIENT(errno)) |
| 1169 |
return -errno; |
| 1170 |
|
| 1171 |
/* Transient error: shouldn't happen but fall back to byte-by-byte */ |
| 1172 |
return 1; |
| 1173 |
} |
| 1174 |
/* EOF: the real recv() will also see it; what we return here doesn't matter */ |
| 1175 |
if (peeked == 0) |
| 1176 |
return rs; |
| 1177 |
|
| 1178 |
size_t dsz = json_stream_delimiter_size(s); |
| 1179 |
void *delim = memmem_safe(p, peeked, s->delimiter ?: "\0", dsz); |
| 1180 |
if (delim) |
| 1181 |
return (ssize_t) ((char*) delim - (char*) p) + dsz; |
| 1182 |
|
| 1183 |
return peeked; |
| 1184 |
} |
| 1185 |
|
| 1186 |
int json_stream_read(JsonStream *s) { |
| 1187 |
struct iovec iov; |
| 1188 |
struct msghdr mh; |
| 1189 |
ssize_t rs; |
| 1190 |
ssize_t n; |
| 1191 |
void *p; |
| 1192 |
|
| 1193 |
assert(s); |
| 1194 |
|
| 1195 |
if (FLAGS_SET(s->flags, JSON_STREAM_CONNECTING)) |
| 1196 |
return 0; |
| 1197 |
if (s->input_buffer_unscanned > 0) |
| 1198 |
return 0; |
| 1199 |
if (FLAGS_SET(s->flags, JSON_STREAM_READ_DISCONNECTED)) |
| 1200 |
return 0; |
| 1201 |
|
| 1202 |
if (s->input_buffer_size >= s->buffer_max) |
| 1203 |
return -ENOBUFS; |
| 1204 |
|
| 1205 |
assert(s->input_fd >= 0); |
| 1206 |
|
| 1207 |
if (MALLOC_SIZEOF_SAFE(s->input_buffer) <= s->input_buffer_index + s->input_buffer_size) { |
| 1208 |
size_t add; |
| 1209 |
|
| 1210 |
add = MIN(s->buffer_max - s->input_buffer_size, s->read_chunk); |
| 1211 |
|
| 1212 |
if (s->input_buffer_index == 0 && |
| 1213 |
(!FLAGS_SET(s->flags, JSON_STREAM_INPUT_SENSITIVE) || s->input_buffer_size == 0)) { |
| 1214 |
if (!GREEDY_REALLOC(s->input_buffer, s->input_buffer_size + add)) |
| 1215 |
return -ENOMEM; |
| 1216 |
} else { |
| 1217 |
char *b; |
| 1218 |
|
| 1219 |
b = new(char, s->input_buffer_size + add); |
| 1220 |
if (!b) |
| 1221 |
return -ENOMEM; |
| 1222 |
|
| 1223 |
memcpy(b, s->input_buffer + s->input_buffer_index, s->input_buffer_size); |
| 1224 |
|
| 1225 |
if (FLAGS_SET(s->flags, JSON_STREAM_INPUT_SENSITIVE)) |
| 1226 |
s->input_buffer = erase_and_free(s->input_buffer); |
| 1227 |
else |
| 1228 |
free(s->input_buffer); |
| 1229 |
s->input_buffer = b; |
| 1230 |
s->input_buffer_index = 0; |
| 1231 |
} |
| 1232 |
} |
| 1233 |
|
| 1234 |
p = s->input_buffer + s->input_buffer_index + s->input_buffer_size; |
| 1235 |
|
| 1236 |
rs = MALLOC_SIZEOF_SAFE(s->input_buffer) - (s->input_buffer_index + s->input_buffer_size); |
| 1237 |
|
| 1238 |
/* If a protocol upgrade may follow, ensure we don't consume any post-upgrade bytes by |
| 1239 |
* limiting the read to the next delimiter. Uses MSG_PEEK on sockets, single-byte reads |
| 1240 |
* otherwise. */ |
| 1241 |
rs = json_stream_peek_message_boundary(s, p, rs); |
| 1242 |
if (rs < 0) |
| 1243 |
return json_stream_log_errno(s, (int) rs, "Failed to peek message boundary: %m"); |
| 1244 |
|
| 1245 |
if (FLAGS_SET(s->flags, JSON_STREAM_ALLOW_FD_PASSING_INPUT)) { |
| 1246 |
iov = IOVEC_MAKE(p, rs); |
| 1247 |
|
| 1248 |
if (!s->input_control_buffer) { |
| 1249 |
s->input_control_buffer_size = CMSG_SPACE(sizeof(int) * JSON_STREAM_FDS_MAX); |
| 1250 |
s->input_control_buffer = malloc(s->input_control_buffer_size); |
| 1251 |
if (!s->input_control_buffer) |
| 1252 |
return -ENOMEM; |
| 1253 |
} |
| 1254 |
|
| 1255 |
mh = (struct msghdr) { |
| 1256 |
.msg_iov = &iov, |
| 1257 |
.msg_iovlen = 1, |
| 1258 |
.msg_control = s->input_control_buffer, |
| 1259 |
.msg_controllen = s->input_control_buffer_size, |
| 1260 |
}; |
| 1261 |
|
| 1262 |
n = RET_NERRNO(recvmsg(s->input_fd, &mh, MSG_DONTWAIT|MSG_CMSG_CLOEXEC)); |
| 1263 |
if (n >= 0 && FLAGS_SET(mh.msg_flags, MSG_TRUNC)) { |
| 1264 |
cmsg_close_all(&mh); |
| 1265 |
return -EXFULL; |
| 1266 |
} |
| 1267 |
if (n >= 0 && FLAGS_SET(mh.msg_flags, MSG_CTRUNC)) { |
| 1268 |
/* SCM_RIGHTS got truncated — typically because an LSM (e.g. SELinux) |
| 1269 |
* denied the fd transfer. Drop the partial fds and continue with the |
| 1270 |
* data bytes: the request handler will surface a clean error to the |
| 1271 |
* peer when it tries to peek the missing fd, instead of us tearing |
| 1272 |
* the connection down silently and leaving the caller waiting. */ |
| 1273 |
json_stream_log(s, "SCM_RIGHTS truncated on inbound message, dropping received file descriptors."); |
| 1274 |
cmsg_close_all(&mh); |
| 1275 |
mh.msg_controllen = 0; |
| 1276 |
} |
| 1277 |
} else if (FLAGS_SET(s->flags, JSON_STREAM_PREFER_READ)) |
| 1278 |
n = RET_NERRNO(read(s->input_fd, p, rs)); |
| 1279 |
else |
| 1280 |
n = RET_NERRNO(recv(s->input_fd, p, rs, MSG_DONTWAIT)); |
| 1281 |
if (ERRNO_IS_NEG_TRANSIENT(n)) |
| 1282 |
return 0; |
| 1283 |
if (ERRNO_IS_NEG_DISCONNECT(n)) { |
| 1284 |
s->flags |= JSON_STREAM_READ_DISCONNECTED; |
| 1285 |
return 1; |
| 1286 |
} |
| 1287 |
if (n < 0) |
| 1288 |
return n; |
| 1289 |
if (n == 0) { /* EOF */ |
| 1290 |
if (FLAGS_SET(s->flags, JSON_STREAM_ALLOW_FD_PASSING_INPUT)) |
| 1291 |
cmsg_close_all(&mh); |
| 1292 |
|
| 1293 |
s->flags |= JSON_STREAM_READ_DISCONNECTED; |
| 1294 |
return 1; |
| 1295 |
} |
| 1296 |
|
| 1297 |
if (FLAGS_SET(s->flags, JSON_STREAM_ALLOW_FD_PASSING_INPUT)) { |
| 1298 |
struct cmsghdr *cmsg; |
| 1299 |
|
| 1300 |
cmsg = cmsg_find(&mh, SOL_SOCKET, SCM_RIGHTS, (socklen_t) -1); |
| 1301 |
if (cmsg) { |
| 1302 |
size_t add; |
| 1303 |
|
| 1304 |
/* fds are only allowed with the first byte of a message; receiving them |
| 1305 |
* mid-stream is a protocol violation. */ |
| 1306 |
if (s->input_buffer_size != 0) { |
| 1307 |
cmsg_close_all(&mh); |
| 1308 |
return -EPROTO; |
| 1309 |
} |
| 1310 |
|
| 1311 |
add = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); |
| 1312 |
if (add > INT_MAX - s->n_input_fds) { |
| 1313 |
cmsg_close_all(&mh); |
| 1314 |
return -EBADF; |
| 1315 |
} |
| 1316 |
|
| 1317 |
if (!GREEDY_REALLOC(s->input_fds, s->n_input_fds + add)) { |
| 1318 |
cmsg_close_all(&mh); |
| 1319 |
return -ENOMEM; |
| 1320 |
} |
| 1321 |
|
| 1322 |
memcpy_safe(s->input_fds + s->n_input_fds, CMSG_TYPED_DATA(cmsg, int), add * sizeof(int)); |
| 1323 |
s->n_input_fds += add; |
| 1324 |
} |
| 1325 |
} |
| 1326 |
|
| 1327 |
s->input_buffer_size += n; |
| 1328 |
s->input_buffer_unscanned += n; |
| 1329 |
|
| 1330 |
return 1; |
| 1331 |
} |
| 1332 |
|
| 1333 |
/* ===== Parse ===== */ |
| 1334 |
|
| 1335 |
int json_stream_parse(JsonStream *s, sd_json_variant **ret) { |
| 1336 |
char *begin, *e; |
| 1337 |
size_t sz; |
| 1338 |
int r; |
| 1339 |
|
| 1340 |
assert(s); |
| 1341 |
assert(ret); |
| 1342 |
|
| 1343 |
if (s->input_buffer_unscanned == 0) { |
| 1344 |
*ret = NULL; |
| 1345 |
return 0; |
| 1346 |
} |
| 1347 |
|
| 1348 |
assert(s->input_buffer_unscanned <= s->input_buffer_size); |
| 1349 |
assert(s->input_buffer_index + s->input_buffer_size <= MALLOC_SIZEOF_SAFE(s->input_buffer)); |
| 1350 |
|
| 1351 |
begin = s->input_buffer + s->input_buffer_index; |
| 1352 |
|
| 1353 |
size_t dsz = json_stream_delimiter_size(s); |
| 1354 |
e = memmem_safe(begin + s->input_buffer_size - s->input_buffer_unscanned, s->input_buffer_unscanned, s->delimiter ?: "\0", dsz); |
| 1355 |
if (!e) { |
| 1356 |
s->input_buffer_unscanned = 0; |
| 1357 |
*ret = NULL; |
| 1358 |
return 0; |
| 1359 |
} |
| 1360 |
|
| 1361 |
sz = e - begin + dsz; |
| 1362 |
|
| 1363 |
/* For non-NUL delimiters (e.g. "\r\n" for QMP) sd_json_parse() needs a NUL-terminated |
| 1364 |
* string; overwrite the first delimiter byte with NUL in place. For NUL delimiters |
| 1365 |
* this is a no-op since the byte is already '\0'. */ |
| 1366 |
if (s->delimiter) |
| 1367 |
*e = '\0'; |
| 1368 |
|
| 1369 |
r = sd_json_parse(begin, SD_JSON_PARSE_MUST_BE_OBJECT, ret, /* reterr_line= */ NULL, /* reterr_column= */ NULL); |
| 1370 |
if (FLAGS_SET(s->flags, JSON_STREAM_INPUT_SENSITIVE)) |
| 1371 |
explicit_bzero_safe(begin, sz); |
| 1372 |
if (r < 0) { |
| 1373 |
/* Unrecoverable parse failure: drop all buffered data. */ |
| 1374 |
s->input_buffer_index = s->input_buffer_size = s->input_buffer_unscanned = 0; |
| 1375 |
return json_stream_log_errno(s, r, "Failed to parse JSON object: %m"); |
| 1376 |
} |
| 1377 |
|
| 1378 |
if (DEBUG_LOGGING) { |
| 1379 |
_cleanup_(erase_and_freep) char *censored_text = NULL; |
| 1380 |
|
| 1381 |
/* Suppress sensitive fields in the debug output */ |
| 1382 |
r = sd_json_variant_format(*ret, /* flags= */ SD_JSON_FORMAT_CENSOR_SENSITIVE, &censored_text); |
| 1383 |
if (r >= 0) |
| 1384 |
json_stream_log(s, "Received message: %s", censored_text); |
| 1385 |
} |
| 1386 |
|
| 1387 |
s->input_buffer_size -= sz; |
| 1388 |
|
| 1389 |
if (s->input_buffer_size == 0) |
| 1390 |
s->input_buffer_index = 0; |
| 1391 |
else |
| 1392 |
s->input_buffer_index += sz; |
| 1393 |
|
| 1394 |
s->input_buffer_unscanned = s->input_buffer_size; |
| 1395 |
return 1; |
| 1396 |
} |