1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <fcntl.h>
#include <signal.h>
#include <stddef.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
#include "time-util.h"
#include "util.h"
#define TIMEOUT_NSEC UINT64_C(30000000000)
static sig_atomic_t quit = false;
static void exit_signal_handler(int signo) {
quit = true;
};
static void sigchld_signal_handler(int signo) {
/* Nothing */
};
int main(int argc, char *argv[]) {
int fd = -1;
pid_t pid = 0;
union {
struct sockaddr sa;
struct sockaddr_un un;
} sa = {
.un.sun_family = AF_UNIX,
};
socklen_t salen = sizeof(sa);
char *buffer = NULL, *e;
size_t buffer_size = 0, allocate_at_least = strlen("READY=1\n");
sigset_t ss, ss_poll;
struct sigaction exit_sigaction = {
.sa_handler = exit_signal_handler,
.sa_flags = SA_RESTART,
};
struct sigaction sigchld_sigaction = {
.sa_handler = sigchld_signal_handler,
.sa_flags = SA_RESTART,
};
uint64_t timeout_at;
int r;
if (argc < 2) {
r = -EINVAL;
log_error("Command line to execute required as argument.");
goto finish;
}
fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
if (fd < 0) {
r = -errno;
log_error("Failed to create notification socket: %m");
goto finish;
}
/* Bind the socket using the kernel's autobind functionality. */
if (bind(fd, &sa.sa, sizeof(sa_family_t)) < 0) {
r = -errno;
log_error("Failed to bind notification socket: %m");
goto finish;
}
if (getsockname(fd, &sa.sa, &salen) < 0) {
r = -errno;
log_error("Failed to determine bound socket address: %m");
goto finish;
}
assert_se(salen > offsetof(struct sockaddr_un, sun_path) + 1);
assert_se(sa.un.sun_family == AF_UNIX);
assert_se(sa.un.sun_path[0] == 0);
e = newa(char, salen - offsetof(struct sockaddr_un, sun_path) + 1);
e[0] = '@';
memcpy(e + 1, sa.un.sun_path + 1, salen - offsetof(struct sockaddr_un, sun_path) - 1);
e[salen - offsetof(struct sockaddr_un, sun_path)] = 0;
assert_se(sigemptyset(&ss) >= 0);
assert_se(sigaddset(&ss, SIGINT) >= 0);
assert_se(sigaddset(&ss, SIGTERM) >= 0);
assert_se(sigaddset(&ss, SIGCHLD) >= 0);
assert_se(sigprocmask(SIG_BLOCK, &ss, &ss_poll) >= 0);
assert_se(sigdelset(&ss_poll, SIGINT) >= 0);
assert_se(sigdelset(&ss_poll, SIGTERM) >= 0);
assert_se(sigdelset(&ss_poll, SIGCHLD) >= 0);
assert_se(sigaction(SIGTERM, &exit_sigaction, NULL) >= 0);
assert_se(sigaction(SIGINT, &exit_sigaction, NULL) >= 0);
assert_se(sigaction(SIGCHLD, &sigchld_sigaction, NULL) >= 0);
timeout_at = now(CLOCK_MONOTONIC) + TIMEOUT_NSEC;
pid = fork();
if (pid < 0) {
r = -errno;
log_error("Failed to to fork(): %m");
goto finish;
}
if (pid == 0) { /* Child? */
int new_stdout;
assert_se(sigprocmask(SIG_SETMASK, &ss_poll, NULL) >= 0);
fd = safe_close(fd);
/* Let's run the child with a new stdout, independent from the parent, so that the PID of we print below is followed by an EOF */
new_stdout = open("/dev/tty", O_WRONLY);
if (new_stdout < 0) {
r = -errno;
/* If that didn't work (which it won't if we are being run from "ninja test"), then let's use /dev/null */
new_stdout = open("/dev/null", O_WRONLY);
if (new_stdout < 0) {
log_error("Failed to open new STDOUT: %m");
goto inner_fail;
}
}
if (new_stdout != STDOUT_FILENO) {
if (dup2(new_stdout, STDOUT_FILENO) < 0) {
r = -errno;
log_error("Failed to duplicate STDOUT: %m");
goto inner_fail;
}
new_stdout = safe_close(new_stdout);
}
if (setenv("NOTIFY_SOCKET", e, 1) < 0) {
r = -errno;
log_error("Failed to set $NOTIFY_SOCKET: %m");
goto inner_fail;
}
execv(argv[1], argv + 1);
r = -errno;
log_error("Failed to execute child process: %m");
inner_fail:
_exit(EXIT_FAILURE);
}
for (;;) {
siginfo_t si = {};
const char *p;
ssize_t n;
struct pollfd pollfd = {
.fd = fd,
.events = POLLIN,
};
struct timespec ts;
uint64_t nw;
if (waitid(P_PID, pid, &si, WEXITED|WNOHANG) < 0) {
r = -errno;
log_error("Failed to wait for children: %m");
goto finish;
}
if (si.si_pid > 0) {
switch (si.si_code) {
case CLD_EXITED:
log_error("Process exited prematurely with status %i.", si.si_status);
r = -EPIPE;
pid = 0;
goto finish;
case CLD_KILLED:
case CLD_DUMPED:
log_error("Process killed with signal %i (%s).", si.si_status, strsignal(si.si_status));
r = -EPIPE;
pid = 0;
goto finish;
default:
assert_se(false);
}
}
for (;;) {
ssize_t k;
if (buffer_size < allocate_at_least) {
free(buffer);
buffer = new(char, allocate_at_least);
if (!buffer) {
r = log_oom();
goto finish;
}
buffer_size = allocate_at_least;
}
n = recv(fd, buffer, buffer_size, MSG_DONTWAIT|MSG_PEEK|MSG_TRUNC);
if (n < 0) {
if (errno == EAGAIN)
goto wait_for_event;
r = log_error_errno(errno,
"Failed to read notification datagram: %m");
goto finish;
}
if ((size_t) n > buffer_size) {
allocate_at_least = n;
continue;
}
/* Now we know the message fit in the buffer, now read it properly. */
k = recv(fd, buffer, buffer_size, 0);
if (k < 0) {
r = log_error_errno(errno,
"Failed to consume notification datagram: %m");
goto finish;
}
if (k != n) {
r = log_error_errno(EIO,
"Consumed notification datagram has different size than original: %m");
goto finish;
}
/* Successfully acquired a message! */
break;
}
/* Too short for what we are looking for... */
if ((size_t) n < strlen("READY=1"))
goto wait_for_event;
p = startswith(buffer, "READY=1");
if (!p) {
p = memmem(buffer, n, "\nREADY=1", strlen("\nREADY=1"));
if (!p)
goto wait_for_event; /* Doesn't contain a READY= stanza */
p += strlen("\nREADY=1");
}
if (p == buffer + n || *p == '\n') {
/* Got it! */
printf(PID_FMT "\n", pid);
pid = 0; /* don't kill on exit */
break;
}
wait_for_event:
if (quit) {
/* Got SIGINT or SIGTERM */
log_error("Exiting due to signal.");
break;
}
nw = now(CLOCK_MONOTONIC);
ts = nsec_to_timespec(timeout_at > nw ? timeout_at - nw : 0);
r = ppoll(&pollfd, 1, &ts, &ss_poll);
if (r < 0) {
if (errno == EINTR) /* got a SIGCHLD or a SIGTERM/SIGINT? */
continue;
r = log_error_errno(errno, "Failed to ppoll(): %m");
goto finish;
}
if (r == 0) { /* timeout */
log_error("Timeout.");
r = -ETIMEDOUT;
goto finish;
}
}
r = 0;
finish:
if (pid > 1)
(void) kill(pid, SIGTERM);
free(buffer);
safe_close(fd);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
|