| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-event.h" |
| 4 |
#include "sd-future.h" |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "errno-util.h" |
| 8 |
#include "event-future.h" |
| 9 |
#include "event-util.h" |
| 10 |
#include "fd-util.h" |
| 11 |
|
| 12 |
typedef struct IoFuture { |
| 13 |
sd_event_source *source; |
| 14 |
} IoFuture; |
| 15 |
|
| 16 |
static void* io_future_alloc(void) { |
| 17 |
return new0(IoFuture, 1); |
| 18 |
} |
| 19 |
|
| 20 |
static void io_future_free(sd_future *f) { |
| 21 |
IoFuture *iof = ASSERT_PTR(sd_future_get_private(ASSERT_PTR(f))); |
| 22 |
sd_event_source_unref(iof->source); |
| 23 |
free(iof); |
| 24 |
} |
| 25 |
|
| 26 |
static int io_future_cancel(sd_future *f) { |
| 27 |
IoFuture *iof = ASSERT_PTR(sd_future_get_private(ASSERT_PTR(f))); |
| 28 |
int r = 0; |
| 29 |
|
| 30 |
RET_GATHER(r, sd_event_source_set_enabled(iof->source, SD_EVENT_OFF)); |
| 31 |
RET_GATHER(r, sd_future_resolve(f, -ECANCELED)); |
| 32 |
return r; |
| 33 |
} |
| 34 |
|
| 35 |
static int io_future_set_priority(sd_future *f, int64_t priority) { |
| 36 |
IoFuture *iof = ASSERT_PTR(sd_future_get_private(ASSERT_PTR(f))); |
| 37 |
return sd_event_source_set_priority(iof->source, priority); |
| 38 |
} |
| 39 |
|
| 40 |
static const sd_future_ops io_future_ops = { |
| 41 |
.size = sizeof(sd_future_ops), |
| 42 |
.alloc = io_future_alloc, |
| 43 |
.free = io_future_free, |
| 44 |
.cancel = io_future_cancel, |
| 45 |
.set_priority = io_future_set_priority, |
| 46 |
}; |
| 47 |
|
| 48 |
static int io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 49 |
sd_future *f = ASSERT_PTR(userdata); |
| 50 |
|
| 51 |
/* Resolve with the revents mask on success (matching io_uring poll_add's CQE convention) so |
| 52 |
* callers can read it directly off the future result. EPOLLERR is the one exception: surface |
| 53 |
* the actual socket error via SO_ERROR so callers like sd_fiber_connect() can return -errno |
| 54 |
* directly without re-querying. */ |
| 55 |
if (FLAGS_SET(revents, EPOLLERR)) { |
| 56 |
int error = 0; |
| 57 |
socklen_t len = sizeof(error); |
| 58 |
|
| 59 |
int r = RET_NERRNO(getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len)); |
| 60 |
if (r == -ENOTSOCK) |
| 61 |
return sd_future_resolve(f, (int) revents); |
| 62 |
if (r >= 0 && error != 0) |
| 63 |
return sd_future_resolve(f, -error); |
| 64 |
if (r >= 0) |
| 65 |
/* EPOLLERR was reported but SO_ERROR returned no pending error (e.g. |
| 66 |
* already consumed elsewhere). Surface the revents mask so the caller |
| 67 |
* still sees the error condition rather than mistaking it for success. */ |
| 68 |
return sd_future_resolve(f, (int) revents); |
| 69 |
/* On any other getsockopt() error fall through and resolve the future with that |
| 70 |
* error so the waiting fiber wakes up rather than hanging forever. */ |
| 71 |
return sd_future_resolve(f, r); |
| 72 |
} |
| 73 |
|
| 74 |
return sd_future_resolve(f, (int) revents); |
| 75 |
} |
| 76 |
|
| 77 |
int future_new_io(sd_event *e, int fd, uint32_t events, sd_future **ret) { |
| 78 |
int r; |
| 79 |
|
| 80 |
assert(e); |
| 81 |
assert(fd >= 0); |
| 82 |
assert(ret); |
| 83 |
|
| 84 |
if (IN_SET(sd_event_get_state(e), SD_EVENT_EXITING, SD_EVENT_FINISHED)) |
| 85 |
return -ECANCELED; |
| 86 |
|
| 87 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 88 |
r = sd_future_new(&io_future_ops, &f); |
| 89 |
if (r < 0) |
| 90 |
return r; |
| 91 |
|
| 92 |
IoFuture *iof = sd_future_get_private(f); |
| 93 |
|
| 94 |
/* Duplicate fd to avoid EEXIST from epoll when adding the same fd multiple times */ |
| 95 |
_cleanup_close_ int fd_copy = fcntl(fd, F_DUPFD_CLOEXEC, 3); |
| 96 |
if (fd_copy < 0) |
| 97 |
return -errno; |
| 98 |
|
| 99 |
r = sd_event_add_io(e, &iof->source, fd_copy, events, io_handler, f); |
| 100 |
if (r < 0) |
| 101 |
return r; |
| 102 |
|
| 103 |
r = sd_event_source_set_io_fd_own(iof->source, true); |
| 104 |
if (r < 0) |
| 105 |
return r; |
| 106 |
|
| 107 |
TAKE_FD(fd_copy); |
| 108 |
|
| 109 |
r = sd_event_source_set_enabled(iof->source, SD_EVENT_ONESHOT); |
| 110 |
if (r < 0) |
| 111 |
return r; |
| 112 |
|
| 113 |
if (sd_fiber_is_running()) { |
| 114 |
int64_t priority; |
| 115 |
|
| 116 |
r = sd_fiber_get_priority(&priority); |
| 117 |
if (r < 0) |
| 118 |
return r; |
| 119 |
|
| 120 |
r = sd_event_source_set_priority(iof->source, priority); |
| 121 |
if (r < 0) |
| 122 |
return r; |
| 123 |
} |
| 124 |
|
| 125 |
*ret = TAKE_PTR(f); |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
typedef struct TimeFuture { |
| 130 |
sd_event_source *source; |
| 131 |
|
| 132 |
/* Result the future resolves with on natural expiry (vs. cancellation). 0 for normal sleep, |
| 133 |
* non-zero (e.g. -ETIMEDOUT) lets a fiber waiting on this future resume with that error. */ |
| 134 |
int result; |
| 135 |
} TimeFuture; |
| 136 |
|
| 137 |
static void* time_future_alloc(void) { |
| 138 |
return new0(TimeFuture, 1); |
| 139 |
} |
| 140 |
|
| 141 |
static void time_future_free(sd_future *f) { |
| 142 |
TimeFuture *tf = ASSERT_PTR(sd_future_get_private(ASSERT_PTR(f))); |
| 143 |
sd_event_source_unref(tf->source); |
| 144 |
free(tf); |
| 145 |
} |
| 146 |
|
| 147 |
static int time_future_cancel(sd_future *f) { |
| 148 |
TimeFuture *tf = ASSERT_PTR(sd_future_get_private(ASSERT_PTR(f))); |
| 149 |
int r; |
| 150 |
|
| 151 |
r = sd_event_source_set_enabled(tf->source, SD_EVENT_OFF); |
| 152 |
RET_GATHER(r, sd_future_resolve(f, -ECANCELED)); |
| 153 |
return r; |
| 154 |
} |
| 155 |
|
| 156 |
static int time_future_set_priority(sd_future *f, int64_t priority) { |
| 157 |
TimeFuture *tf = ASSERT_PTR(sd_future_get_private(ASSERT_PTR(f))); |
| 158 |
return sd_event_source_set_priority(tf->source, priority); |
| 159 |
} |
| 160 |
|
| 161 |
static const sd_future_ops time_future_ops = { |
| 162 |
.size = sizeof(sd_future_ops), |
| 163 |
.alloc = time_future_alloc, |
| 164 |
.free = time_future_free, |
| 165 |
.cancel = time_future_cancel, |
| 166 |
.set_priority = time_future_set_priority, |
| 167 |
}; |
| 168 |
|
| 169 |
static int time_handler(sd_event_source *s, usec_t usec, void *userdata) { |
| 170 |
sd_future *f = ASSERT_PTR(userdata); |
| 171 |
TimeFuture *tf = ASSERT_PTR(sd_future_get_private(f)); |
| 172 |
|
| 173 |
return sd_future_resolve(f, tf->result); |
| 174 |
} |
| 175 |
|
| 176 |
typedef int (*event_add_time_func)( |
| 177 |
sd_event *e, |
| 178 |
sd_event_source **ret, |
| 179 |
clockid_t clock, |
| 180 |
uint64_t usec, |
| 181 |
uint64_t accuracy, |
| 182 |
sd_event_time_handler_t callback, |
| 183 |
void *userdata); |
| 184 |
|
| 185 |
static int future_new_time_internal( |
| 186 |
event_add_time_func add_time, |
| 187 |
sd_event *e, |
| 188 |
clockid_t clock, |
| 189 |
uint64_t usec, |
| 190 |
uint64_t accuracy, |
| 191 |
int result, |
| 192 |
sd_future **ret) { |
| 193 |
|
| 194 |
int r; |
| 195 |
|
| 196 |
assert(add_time); |
| 197 |
assert(e); |
| 198 |
assert(ret); |
| 199 |
|
| 200 |
if (IN_SET(sd_event_get_state(e), SD_EVENT_EXITING, SD_EVENT_FINISHED)) |
| 201 |
return -ECANCELED; |
| 202 |
|
| 203 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 204 |
r = sd_future_new(&time_future_ops, &f); |
| 205 |
if (r < 0) |
| 206 |
return r; |
| 207 |
|
| 208 |
TimeFuture *tf = sd_future_get_private(f); |
| 209 |
tf->result = result; |
| 210 |
|
| 211 |
r = add_time(e, &tf->source, clock, usec, accuracy, time_handler, f); |
| 212 |
if (r < 0) |
| 213 |
return r; |
| 214 |
|
| 215 |
if (sd_fiber_is_running()) { |
| 216 |
int64_t priority; |
| 217 |
|
| 218 |
r = sd_fiber_get_priority(&priority); |
| 219 |
if (r < 0) |
| 220 |
return r; |
| 221 |
|
| 222 |
r = sd_event_source_set_priority(tf->source, priority); |
| 223 |
if (r < 0) |
| 224 |
return r; |
| 225 |
} |
| 226 |
|
| 227 |
*ret = TAKE_PTR(f); |
| 228 |
return 0; |
| 229 |
} |
| 230 |
|
| 231 |
int future_new_time(sd_event *e, clockid_t clock, uint64_t usec, uint64_t accuracy, int result, sd_future **ret) { |
| 232 |
return future_new_time_internal(sd_event_add_time, e, clock, usec, accuracy, result, ret); |
| 233 |
} |
| 234 |
|
| 235 |
int future_new_time_relative(sd_event *e, clockid_t clock, uint64_t usec, uint64_t accuracy, int result, sd_future **ret) { |
| 236 |
return future_new_time_internal(sd_event_add_time_relative, e, clock, usec, accuracy, result, ret); |
| 237 |
} |
| 238 |
|
| 239 |
int event_run_suspend(sd_event *e, uint64_t timeout) { |
| 240 |
sd_event *outer = sd_fiber_get_event(); |
| 241 |
int r; |
| 242 |
|
| 243 |
assert(e); |
| 244 |
assert(sd_fiber_is_running()); |
| 245 |
assert(outer); |
| 246 |
assert(e != outer); |
| 247 |
|
| 248 |
/* Make sure that none of the preparation callbacks ends up freeing the event source under our feet */ |
| 249 |
PROTECT_EVENT(e); |
| 250 |
|
| 251 |
r = sd_event_prepare(e); |
| 252 |
if (r < 0) |
| 253 |
return r; |
| 254 |
if (r == 0) { |
| 255 |
r = sd_event_wait(e, 0); |
| 256 |
if (r < 0) |
| 257 |
return r; |
| 258 |
} |
| 259 |
if (r > 0) { |
| 260 |
r = sd_event_dispatch(e); |
| 261 |
if (r < 0) |
| 262 |
return r; |
| 263 |
|
| 264 |
return 1; |
| 265 |
} |
| 266 |
|
| 267 |
if (timeout == 0) |
| 268 |
return 0; |
| 269 |
|
| 270 |
int fd = sd_event_get_fd(e); |
| 271 |
if (fd < 0) |
| 272 |
return fd; |
| 273 |
|
| 274 |
_cleanup_(sd_future_cancel_wait_unrefp) sd_future *io = NULL; |
| 275 |
r = future_new_io(outer, fd, EPOLLIN, &io); |
| 276 |
if (r < 0) |
| 277 |
return r; |
| 278 |
|
| 279 |
_cleanup_(sd_future_cancel_wait_unrefp) sd_future *timer = NULL; |
| 280 |
if (timeout != USEC_INFINITY) { |
| 281 |
r = future_new_time_relative( |
| 282 |
outer, |
| 283 |
CLOCK_MONOTONIC, |
| 284 |
timeout, |
| 285 |
/* accuracy= */ 1, |
| 286 |
/* result= */ 0, |
| 287 |
&timer); |
| 288 |
if (r < 0) |
| 289 |
return r; |
| 290 |
} |
| 291 |
|
| 292 |
r = sd_fiber_suspend(); |
| 293 |
if (r < 0) |
| 294 |
return r; |
| 295 |
|
| 296 |
r = sd_event_prepare(e); |
| 297 |
if (r == 0) |
| 298 |
r = sd_event_wait(e, 0); |
| 299 |
if (r > 0) { |
| 300 |
r = sd_event_dispatch(e); |
| 301 |
if (r < 0) |
| 302 |
return r; |
| 303 |
|
| 304 |
return 1; |
| 305 |
} |
| 306 |
|
| 307 |
return r; |
| 308 |
} |