| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
/* In 'store' mode pushes a couple of memfds with known content into the supervisor's fd store via FDSTORE=1 |
| 4 |
* sd_notify() messages. In 'check' mode reads back the fds passed via LISTEN_FDS and verifies the content |
| 5 |
* matches what was pushed. |
| 6 |
* |
| 7 |
* This binary is intentionally linked against libsystemd only so that it can go in the minimal image. */ |
| 8 |
|
| 9 |
#include <signal.h> |
| 10 |
#include <stdbool.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <string.h> |
| 14 |
#include <sys/mman.h> |
| 15 |
#include <unistd.h> |
| 16 |
|
| 17 |
#include "sd-daemon.h" |
| 18 |
|
| 19 |
#define DATA_A "fdstore-data-a" |
| 20 |
#define DATA_B "fdstore-data-b" |
| 21 |
|
| 22 |
#define _cleanup_(f) __attribute__((cleanup(f))) |
| 23 |
|
| 24 |
static void closep(int *fd) { |
| 25 |
if (!fd || *fd < 0) |
| 26 |
return; |
| 27 |
|
| 28 |
close(*fd); |
| 29 |
*fd = -EBADF; |
| 30 |
} |
| 31 |
|
| 32 |
static _Noreturn void exit_handler(int sig) { |
| 33 |
_exit(EXIT_SUCCESS); |
| 34 |
} |
| 35 |
|
| 36 |
static int push_one(const char *fdname, const char *content) { |
| 37 |
_cleanup_(closep) int fd = -EBADF; |
| 38 |
int r; |
| 39 |
|
| 40 |
assert(fdname); |
| 41 |
assert(content); |
| 42 |
|
| 43 |
fd = memfd_create(fdname, MFD_CLOEXEC | MFD_ALLOW_SEALING); |
| 44 |
if (fd < 0) { |
| 45 |
fprintf(stderr, "memfd_create(%s) failed: %m\n", fdname); |
| 46 |
return -errno; |
| 47 |
} |
| 48 |
|
| 49 |
size_t len = strlen(content); |
| 50 |
if (write(fd, content, len) != (ssize_t) len) { |
| 51 |
fprintf(stderr, "write(%s) failed: %m\n", fdname); |
| 52 |
return -errno; |
| 53 |
} |
| 54 |
|
| 55 |
char msg[256]; |
| 56 |
r = snprintf(msg, sizeof(msg), "FDSTORE=1\nFDNAME=%s", fdname); |
| 57 |
if (r < 0 || (size_t) r >= sizeof(msg)) { |
| 58 |
if (r >= 0) |
| 59 |
errno = ENOBUFS; |
| 60 |
fprintf(stderr, "FDSTORE message for fdname=%s did not fit in buffer\n", fdname); |
| 61 |
return -errno; |
| 62 |
} |
| 63 |
|
| 64 |
r = sd_pid_notify_with_fds(0, /* unset_environment= */ 0, msg, &fd, 1); |
| 65 |
if (r < 0) { |
| 66 |
errno = -r; |
| 67 |
fprintf(stderr, "sd_pid_notify_with_fds(%s) failed: %m\n", fdname); |
| 68 |
return r; |
| 69 |
} |
| 70 |
if (r == 0) { |
| 71 |
fprintf(stderr, "NOTIFY_SOCKET not set\n"); |
| 72 |
return -ENOENT; |
| 73 |
} |
| 74 |
|
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
static int do_store(void) { |
| 79 |
int r; |
| 80 |
|
| 81 |
if (push_one("test-fd-a", DATA_A) < 0) |
| 82 |
return EXIT_FAILURE; |
| 83 |
|
| 84 |
if (push_one("test-fd-b", DATA_B) < 0) |
| 85 |
return EXIT_FAILURE; |
| 86 |
|
| 87 |
/* Wait for our supervisor to actually process the FDSTORE messages before we exit, otherwise |
| 88 |
* the cgroup-based pidref to unit lookup may fail once we're gone. */ |
| 89 |
r = sd_notify_barrier(0, 5 * 1000 * 1000); |
| 90 |
if (r < 0) { |
| 91 |
errno = -r; |
| 92 |
fprintf(stderr, "sd_notify_barrier failed: %m\n"); |
| 93 |
return EXIT_FAILURE; |
| 94 |
} |
| 95 |
|
| 96 |
return EXIT_SUCCESS; |
| 97 |
} |
| 98 |
|
| 99 |
static int do_check(void) { |
| 100 |
bool seen_a = false, seen_b = false; |
| 101 |
int n; |
| 102 |
|
| 103 |
n = sd_listen_fds(/* unset_environment= */ 0); |
| 104 |
if (n < 0) { |
| 105 |
errno = -n; |
| 106 |
fprintf(stderr, "sd_listen_fds failed: %m\n"); |
| 107 |
return EXIT_FAILURE; |
| 108 |
} |
| 109 |
if (n < 2) { |
| 110 |
fprintf(stderr, "Expected at least 2 fds via LISTEN_FDS, got %d\n", n); |
| 111 |
return EXIT_FAILURE; |
| 112 |
} |
| 113 |
|
| 114 |
for (int i = 0; i < n; i++) { |
| 115 |
int fd = SD_LISTEN_FDS_START + i; |
| 116 |
char buf[256] = {}; |
| 117 |
ssize_t k; |
| 118 |
|
| 119 |
if (lseek(fd, 0, SEEK_SET) < 0) { |
| 120 |
fprintf(stderr, "lseek(fd=%d) failed: %m\n", fd); |
| 121 |
return EXIT_FAILURE; |
| 122 |
} |
| 123 |
k = read(fd, buf, sizeof(buf) - 1); |
| 124 |
if (k < 0) { |
| 125 |
fprintf(stderr, "read(fd=%d) failed: %m\n", fd); |
| 126 |
return EXIT_FAILURE; |
| 127 |
} |
| 128 |
buf[k] = 0; |
| 129 |
|
| 130 |
if (strcmp(buf, DATA_A) == 0) |
| 131 |
seen_a = true; |
| 132 |
else if (strcmp(buf, DATA_B) == 0) |
| 133 |
seen_b = true; |
| 134 |
else |
| 135 |
fprintf(stderr, "Unexpected fd content: '%s'\n", buf); |
| 136 |
} |
| 137 |
|
| 138 |
if (!seen_a || !seen_b) { |
| 139 |
fprintf(stderr, "Missing expected fds: seen_a=%d seen_b=%d\n", seen_a, seen_b); |
| 140 |
return EXIT_FAILURE; |
| 141 |
} |
| 142 |
|
| 143 |
printf("Payload received both preserved fds with matching content.\n"); |
| 144 |
return EXIT_SUCCESS; |
| 145 |
} |
| 146 |
|
| 147 |
int main(int argc, char *argv[]) { |
| 148 |
int r; |
| 149 |
|
| 150 |
if (argc < 2) { |
| 151 |
fprintf(stderr, "Usage: %s store|check\n", argv[0]); |
| 152 |
return EXIT_FAILURE; |
| 153 |
} |
| 154 |
|
| 155 |
if (strcmp(argv[1], "store") == 0) |
| 156 |
r = do_store(); |
| 157 |
else if (strcmp(argv[1], "check") == 0) |
| 158 |
r = do_check(); |
| 159 |
else { |
| 160 |
fprintf(stderr, "Unknown verb: %s\n", argv[1]); |
| 161 |
return EXIT_FAILURE; |
| 162 |
} |
| 163 |
|
| 164 |
if (r != EXIT_SUCCESS) |
| 165 |
return r; |
| 166 |
|
| 167 |
/* On success, stay alive so if we are a container payload we keep it running. Install handlers |
| 168 |
* for the signals an outer supervisor may use to terminate us, so we exit cleanly (with status 0) |
| 169 |
* and the container service ends up in 'inactive' rather than 'failed'. */ |
| 170 |
struct sigaction sa = { .sa_handler = exit_handler }; |
| 171 |
if (sigaction(SIGTERM, &sa, /* __oact= */ NULL) < 0 || |
| 172 |
sigaction(SIGINT, &sa, /* __oact= */ NULL) < 0) { |
| 173 |
fprintf(stderr, "Failed to install signal handlers: %m\n"); |
| 174 |
return EXIT_FAILURE; |
| 175 |
} |
| 176 |
|
| 177 |
for (;;) |
| 178 |
pause(); |
| 179 |
} |