| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/sockios.h> |
| 4 |
#include <poll.h> |
| 5 |
#include <sys/ioctl.h> |
| 6 |
|
| 7 |
#include "sd-varlink.h" |
| 8 |
|
| 9 |
#include "alloc-util.h" |
| 10 |
#include "io-util.h" |
| 11 |
#include "journald-manager.h" |
| 12 |
#include "journald-stream.h" |
| 13 |
#include "journald-sync.h" |
| 14 |
#include "journald-varlink.h" |
| 15 |
#include "log.h" |
| 16 |
#include "prioq.h" |
| 17 |
#include "socket-netlink.h" |
| 18 |
#include "time-util.h" |
| 19 |
|
| 20 |
StreamSyncReq *stream_sync_req_free(StreamSyncReq *ssr) { |
| 21 |
if (!ssr) |
| 22 |
return NULL; |
| 23 |
|
| 24 |
if (ssr->req) |
| 25 |
LIST_REMOVE(by_sync_req, ssr->req->stream_sync_reqs, ssr); |
| 26 |
if (ssr->stream) |
| 27 |
LIST_REMOVE(by_stdout_stream, ssr->stream->stream_sync_reqs, ssr); |
| 28 |
|
| 29 |
return mfree(ssr); |
| 30 |
} |
| 31 |
|
| 32 |
void stream_sync_req_advance_revalidate(StreamSyncReq *ssr, size_t p) { |
| 33 |
assert(ssr); |
| 34 |
|
| 35 |
/* Subtract the specified number of bytes from the byte counter. And when we hit zero we consider |
| 36 |
* this stream processed for the synchronization request */ |
| 37 |
|
| 38 |
/* NB: This might invalidate the 'ssr' object! */ |
| 39 |
|
| 40 |
if (p < ssr->pending_siocinq) { |
| 41 |
ssr->pending_siocinq -= p; |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
SyncReq *req = ASSERT_PTR(ssr->req); |
| 46 |
stream_sync_req_free(TAKE_PTR(ssr)); |
| 47 |
|
| 48 |
/* Maybe we are done now? */ |
| 49 |
sync_req_revalidate(TAKE_PTR(req)); |
| 50 |
} |
| 51 |
|
| 52 |
static bool sync_req_is_complete(SyncReq *req) { |
| 53 |
int r; |
| 54 |
|
| 55 |
assert(req); |
| 56 |
assert(req->manager); |
| 57 |
|
| 58 |
/* In case the clock jumped backwards, let's adjust the timestamp, to guarantee reasonably quick |
| 59 |
* termination */ |
| 60 |
usec_t n = now(CLOCK_REALTIME); |
| 61 |
if (n < req->realtime) |
| 62 |
req->realtime = n; |
| 63 |
|
| 64 |
if (req->realtime_prioq_idx != PRIOQ_IDX_NULL) { |
| 65 |
/* If this sync request is still in the priority queue it means we still need to check if |
| 66 |
* incoming message timestamps are now newer than then sync request timestamp. */ |
| 67 |
|
| 68 |
if (req->manager->native_fd >= 0 && |
| 69 |
req->manager->native_timestamp < req->realtime) { |
| 70 |
r = fd_wait_for_event(req->manager->native_fd, POLLIN, /* timeout= */ 0); |
| 71 |
if (r < 0) |
| 72 |
log_debug_errno(r, "Failed to determine pending IO events of native socket, ignoring: %m"); |
| 73 |
else if (r != 0) /* if there's more queued we need to wait for the timestamp to pass. If it's idle though we are done here. */ |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
if (req->manager->syslog_fd >= 0&& |
| 78 |
req->manager->syslog_timestamp < req->realtime) { |
| 79 |
r = fd_wait_for_event(req->manager->syslog_fd, POLLIN, /* timeout= */ 0); |
| 80 |
if (r < 0) |
| 81 |
log_debug_errno(r, "Failed to determine pending IO events of syslog socket, ignoring: %m"); |
| 82 |
else if (r != 0) |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
/* This sync request is fulfilled for the native + syslog datagram streams? Then, let's |
| 87 |
* remove this sync request from the priority queue, so that we dont need to consider it |
| 88 |
* anymore. */ |
| 89 |
assert(prioq_remove(req->manager->sync_req_realtime_prioq, req, &req->realtime_prioq_idx) > 0); |
| 90 |
} |
| 91 |
|
| 92 |
if (req->boottime_prioq_idx != PRIOQ_IDX_NULL) { |
| 93 |
/* Very similar to the above, but for /dev/kmsg we operate on the CLOCK_BOOTTIME clock */ |
| 94 |
|
| 95 |
if (req->manager->dev_kmsg_fd >= 0 && |
| 96 |
req->manager->dev_kmsg_timestamp < req->boottime) { |
| 97 |
r = fd_wait_for_event(req->manager->dev_kmsg_fd, POLLIN, /* timeout= */ 0); |
| 98 |
if (r < 0) |
| 99 |
log_debug_errno(r, "Failed to determine pending IO events of /dev/kmsg file descriptor, ignoring: %m"); |
| 100 |
else if (r != 0) |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
assert(prioq_remove(req->manager->sync_req_boottime_prioq, req, &req->boottime_prioq_idx) > 0); |
| 105 |
} |
| 106 |
|
| 107 |
/* If there are still streams with pending counters, we still need to look into things */ |
| 108 |
if (req->stream_sync_reqs) |
| 109 |
return false; |
| 110 |
|
| 111 |
/* If there are still pending connections from before the sync started, we still need to look into things */ |
| 112 |
if (req->pending_rqlen > 0) |
| 113 |
return false; |
| 114 |
|
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
static int on_idle(sd_event_source *s, void *userdata) { |
| 119 |
SyncReq *req = ASSERT_PTR(userdata); |
| 120 |
|
| 121 |
req->idle_event_source = sd_event_source_disable_unref(req->idle_event_source); |
| 122 |
|
| 123 |
/* When this idle event triggers, then we definitely are done with the synchronization request. This |
| 124 |
* is a safety net of a kind, to ensure we'll definitely put an end to any synchronization request, |
| 125 |
* even if we are confused by CLOCK_REALTIME jumps or similar. */ |
| 126 |
sync_req_varlink_reply(TAKE_PTR(req)); |
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
SyncReq* sync_req_free(SyncReq *req) { |
| 131 |
if (!req) |
| 132 |
return NULL; |
| 133 |
|
| 134 |
if (req->manager) { |
| 135 |
if (req->realtime_prioq_idx != PRIOQ_IDX_NULL) |
| 136 |
assert_se(prioq_remove(req->manager->sync_req_realtime_prioq, req, &req->realtime_prioq_idx) > 0); |
| 137 |
|
| 138 |
if (req->boottime_prioq_idx != PRIOQ_IDX_NULL) |
| 139 |
assert_se(prioq_remove(req->manager->sync_req_boottime_prioq, req, &req->boottime_prioq_idx) > 0); |
| 140 |
|
| 141 |
if (req->pending_rqlen > 0) |
| 142 |
LIST_REMOVE(pending_rqlen, req->manager->sync_req_pending_rqlen, req); |
| 143 |
} |
| 144 |
|
| 145 |
sd_event_source_disable_unref(req->idle_event_source); |
| 146 |
|
| 147 |
sd_varlink_unref(req->link); |
| 148 |
|
| 149 |
while (req->stream_sync_reqs) |
| 150 |
stream_sync_req_free(req->stream_sync_reqs); |
| 151 |
|
| 152 |
return mfree(req); |
| 153 |
} |
| 154 |
|
| 155 |
static int sync_req_realtime_compare(const SyncReq *x, const SyncReq *y) { |
| 156 |
return CMP(ASSERT_PTR(x)->realtime, ASSERT_PTR(y)->realtime); |
| 157 |
} |
| 158 |
|
| 159 |
static int sync_req_boottime_compare(const SyncReq *x, const SyncReq *y) { |
| 160 |
return CMP(ASSERT_PTR(x)->boottime, ASSERT_PTR(y)->boottime); |
| 161 |
} |
| 162 |
|
| 163 |
static int sync_req_add_stream(SyncReq *req, StdoutStream *ss) { |
| 164 |
assert(req); |
| 165 |
assert(ss); |
| 166 |
|
| 167 |
int v = 0; |
| 168 |
if (ioctl(ss->fd, SIOCINQ, &v) < 0) |
| 169 |
log_debug_errno(errno, "Failed to issue SIOCINQ on stream socket, ignoring: %m"); |
| 170 |
if (v <= 0) |
| 171 |
return 0; /* Pending messages are zero anyway? then there's nothing to track */ |
| 172 |
|
| 173 |
_cleanup_(stream_sync_req_freep) StreamSyncReq *ssr = new(StreamSyncReq, 1); |
| 174 |
if (!ssr) |
| 175 |
return -ENOMEM; |
| 176 |
|
| 177 |
*ssr = (StreamSyncReq) { |
| 178 |
.stream = ss, |
| 179 |
.pending_siocinq = v, |
| 180 |
.req = req, |
| 181 |
}; |
| 182 |
|
| 183 |
LIST_PREPEND(by_sync_req, req->stream_sync_reqs, ssr); |
| 184 |
LIST_PREPEND(by_stdout_stream, ss->stream_sync_reqs, ssr); |
| 185 |
|
| 186 |
TAKE_PTR(ssr); |
| 187 |
return 1; |
| 188 |
} |
| 189 |
|
| 190 |
int sync_req_new(Manager *m, sd_varlink *link, SyncReq **ret) { |
| 191 |
int r; |
| 192 |
|
| 193 |
assert(m); |
| 194 |
assert(link); |
| 195 |
assert(ret); |
| 196 |
|
| 197 |
_cleanup_(sync_req_freep) SyncReq *req = new(SyncReq, 1); |
| 198 |
if (!req) |
| 199 |
return -ENOMEM; |
| 200 |
|
| 201 |
*req = (SyncReq) { |
| 202 |
.manager = m, |
| 203 |
.link = sd_varlink_ref(link), |
| 204 |
.realtime_prioq_idx = PRIOQ_IDX_NULL, |
| 205 |
.boottime_prioq_idx = PRIOQ_IDX_NULL, |
| 206 |
}; |
| 207 |
|
| 208 |
/* We use five distinct mechanisms to determine when the synchronization request is complete: |
| 209 |
* |
| 210 |
* 1. For the syslog/native AF_UNIX/SOCK_DGRAM sockets we look at the datagram timestamps: once the |
| 211 |
* most recently seen datagram on the socket is newer than the timestamp when we initiated the |
| 212 |
* sync request we know that all previously enqueued messages have been processed by us. |
| 213 |
* |
| 214 |
* 2. For established stream AF_UNIX/SOCK_STREAM sockets we have no timestamps. For them we take the |
| 215 |
* SIOCINQ counter at the moment the synchronization request was enqueued. And once we processed |
| 216 |
* the indicated number of input bytes we know that anything further was enqueued later than the |
| 217 |
* original synchronization request we started from. |
| 218 |
* |
| 219 |
* 3. For pending new, un-accept()ed stream AF_UNIX/SOCK_STREAM sockets we have no timestamps either, |
| 220 |
* but we can query the number of pending connections via the sockdiag netlink protocol (I so wish |
| 221 |
* there was an easier, quicker way!). Once we accept()ed that many connections we know all |
| 222 |
* further connections are definitely more recent than the sync request. |
| 223 |
* |
| 224 |
* 4. For /dev/kmsg we look at the log message timestamps, similar to the AF_UNIX/SOCK_DGRAM case, |
| 225 |
* and they are in CLOCK_BOOTTIME clock. |
| 226 |
* |
| 227 |
* 5. Finally, as safety net we install an idle handler with a very low priority (lower than the |
| 228 |
* syslog/native/stream IO handlers). If this handler is called we know that there's no pending |
| 229 |
* IO, hence everything so far queued is definitely processed. |
| 230 |
* |
| 231 |
* Note the asymmetry: for AF_UNIX/SOCK_DGRAM + /dev/kmsg we go by timestamp, for established |
| 232 |
* AF_UNIX/SOCK_STREAM we count bytes. That's because for SOCK_STREAM we have no timestamps, and for |
| 233 |
* SOCK_DGRAM we have no API to query all pending bytes (as SIOCINQ on SOCK_DGRAM reports size of |
| 234 |
* next datagram, not size of all pending datagrams). Ideally, we'd actually use neither of this, and |
| 235 |
* the kernel would provide us CLOCK_MONOTONIC timestamps... |
| 236 |
* |
| 237 |
* Note that CLOCK_REALTIME is not necessarily monotonic (that's the whole point of it after all). If |
| 238 |
* the clock jumps then we know the algorithm will eventually terminate, because of the idle handler |
| 239 |
* that is our safety net. (Also, whenever we see poll() return an empty revents for some source we |
| 240 |
* know everything is processed by now regardless of any timestamps or pending byte or connection |
| 241 |
* counts.) */ |
| 242 |
|
| 243 |
req->realtime = now(CLOCK_REALTIME); |
| 244 |
req->boottime = now(CLOCK_BOOTTIME); |
| 245 |
|
| 246 |
if (m->native_event_source || m->syslog_event_source) { |
| 247 |
r = prioq_ensure_put(&m->sync_req_realtime_prioq, sync_req_realtime_compare, req, &req->realtime_prioq_idx); |
| 248 |
if (r < 0) |
| 249 |
return r; |
| 250 |
} |
| 251 |
|
| 252 |
if (m->dev_kmsg_event_source) { |
| 253 |
r = prioq_ensure_put(&m->sync_req_boottime_prioq, sync_req_boottime_compare, req, &req->boottime_prioq_idx); |
| 254 |
if (r < 0) |
| 255 |
return r; |
| 256 |
} |
| 257 |
|
| 258 |
r = sd_event_add_defer(m->event, &req->idle_event_source, on_idle, req); |
| 259 |
if (r < 0) |
| 260 |
return r; |
| 261 |
|
| 262 |
r = sd_event_source_set_priority(req->idle_event_source, SD_EVENT_PRIORITY_NORMAL+15); |
| 263 |
if (r < 0) |
| 264 |
return r; |
| 265 |
|
| 266 |
(void) sd_event_source_set_description(req->idle_event_source, "deferred-sync"); |
| 267 |
|
| 268 |
/* Now determine the pending byte counter for each stdout stream. If non-zero allocate a |
| 269 |
* StreamSyncReq for the stream to keep track of it */ |
| 270 |
LIST_FOREACH(stdout_stream, ss, m->stdout_streams) { |
| 271 |
r = sync_req_add_stream(req, ss); |
| 272 |
if (r < 0) |
| 273 |
return r; |
| 274 |
} |
| 275 |
|
| 276 |
/* Also track how many pending, incoming stream sockets there are currently, so that we process them |
| 277 |
* too */ |
| 278 |
r = af_unix_get_qlen(m->stdout_fd, &req->pending_rqlen); |
| 279 |
if (r < 0) |
| 280 |
log_warning_errno(r, "Failed to determine current incoming queue length, ignoring: %m"); |
| 281 |
if (req->pending_rqlen > 0) |
| 282 |
LIST_PREPEND(pending_rqlen, m->sync_req_pending_rqlen, req); |
| 283 |
|
| 284 |
*ret = TAKE_PTR(req); |
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
static void sync_req_advance_rqlen_revalidate(SyncReq *req, uint32_t current_rqlen, StdoutStream *ss) { |
| 289 |
int r; |
| 290 |
|
| 291 |
assert(req); |
| 292 |
|
| 293 |
/* Invoked whenever a new connection was accept()ed, i.e. dropped off the queue of pending incoming |
| 294 |
* connections. We decrease the qlen counter by one here, except if the new overall counter is |
| 295 |
* already below our target. */ |
| 296 |
|
| 297 |
uint32_t n; |
| 298 |
if (req->pending_rqlen <= 0) |
| 299 |
n = 0; |
| 300 |
else if (req->pending_rqlen > current_rqlen) |
| 301 |
n = current_rqlen; |
| 302 |
else |
| 303 |
n = req->pending_rqlen - 1; |
| 304 |
|
| 305 |
if (req->pending_rqlen > 0) { |
| 306 |
/* if this synchronization request is supposed to process a non-zero number of connections we |
| 307 |
* need to also track what's inside those stream connections */ |
| 308 |
if (ss) { |
| 309 |
r = sync_req_add_stream(req, ss); |
| 310 |
if (r < 0) |
| 311 |
log_warning_errno(r, "Failed to track stream queue size, ignoring: %m"); |
| 312 |
} |
| 313 |
|
| 314 |
/* If there are no more connections to wait for, remove us from the list of synchronization |
| 315 |
* requests with non-zero pending connection counters */ |
| 316 |
if (n == 0) |
| 317 |
LIST_REMOVE(pending_rqlen, req->manager->sync_req_pending_rqlen, req); |
| 318 |
} |
| 319 |
|
| 320 |
req->pending_rqlen = n; |
| 321 |
|
| 322 |
sync_req_revalidate(req); |
| 323 |
} |
| 324 |
|
| 325 |
void manager_notify_stream(Manager *m, StdoutStream *stream) { |
| 326 |
int r; |
| 327 |
|
| 328 |
assert(m); |
| 329 |
|
| 330 |
/* Invoked whenever a new connection was accept()ed, i.e. dropped off the queue of pending incoming |
| 331 |
* connections. */ |
| 332 |
|
| 333 |
if (!m->sync_req_pending_rqlen) |
| 334 |
return; |
| 335 |
|
| 336 |
uint32_t current_qlen; |
| 337 |
|
| 338 |
r = af_unix_get_qlen(m->stdout_fd, ¤t_qlen); |
| 339 |
if (r < 0) { |
| 340 |
log_warning_errno(r, "Failed to determine current AF_UNIX stream socket pending connections, ignoring: %m"); |
| 341 |
current_qlen = UINT32_MAX; |
| 342 |
} |
| 343 |
|
| 344 |
LIST_FOREACH(pending_rqlen, sr, m->sync_req_pending_rqlen) |
| 345 |
/* NB: this might invalidate the SyncReq object! */ |
| 346 |
sync_req_advance_rqlen_revalidate(sr, current_qlen, stream); |
| 347 |
} |
| 348 |
|
| 349 |
bool sync_req_revalidate(SyncReq *req) { |
| 350 |
assert(req); |
| 351 |
|
| 352 |
/* Check if the synchronization request is complete now. If so, answer the Varlink client. NB: this |
| 353 |
* might invalidate the SyncReq object */ |
| 354 |
|
| 355 |
if (!sync_req_is_complete(req)) |
| 356 |
return false; |
| 357 |
|
| 358 |
sync_req_varlink_reply(TAKE_PTR(req)); |
| 359 |
return true; |
| 360 |
} |
| 361 |
|
| 362 |
void sync_req_revalidate_by_timestamp(Manager *m) { |
| 363 |
assert(m); |
| 364 |
|
| 365 |
/* Go through the pending sync requests by timestamp, and complete those for which a sync is now |
| 366 |
* complete. */ |
| 367 |
|
| 368 |
SyncReq *req; |
| 369 |
while ((req = prioq_peek(m->sync_req_realtime_prioq))) |
| 370 |
if (!sync_req_revalidate(req)) |
| 371 |
break; |
| 372 |
|
| 373 |
while ((req = prioq_peek(m->sync_req_boottime_prioq))) |
| 374 |
if (!sync_req_revalidate(req)) |
| 375 |
break; |
| 376 |
} |