| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-event.h" |
| 6 |
|
| 7 |
#include "build-path.h" |
| 8 |
#include "device-private.h" |
| 9 |
#include "device-util.h" |
| 10 |
#include "errno-util.h" |
| 11 |
#include "event-util.h" |
| 12 |
#include "exec-util.h" |
| 13 |
#include "extract-word.h" |
| 14 |
#include "fd-util.h" |
| 15 |
#include "format-util.h" |
| 16 |
#include "hashmap.h" |
| 17 |
#include "path-util.h" |
| 18 |
#include "pidref.h" |
| 19 |
#include "process-util.h" |
| 20 |
#include "signal-util.h" |
| 21 |
#include "string-util.h" |
| 22 |
#include "strv.h" |
| 23 |
#include "time-util.h" |
| 24 |
#include "udev-builtin.h" |
| 25 |
#include "udev-event.h" |
| 26 |
#include "udev-spawn.h" |
| 27 |
#include "udev-trace.h" |
| 28 |
#include "udev-worker.h" |
| 29 |
|
| 30 |
typedef struct Spawn { |
| 31 |
sd_device *device; |
| 32 |
const char *cmd; |
| 33 |
PidRef pidref; |
| 34 |
usec_t timeout_warn_usec; |
| 35 |
usec_t timeout_usec; |
| 36 |
int timeout_signal; |
| 37 |
usec_t event_birth_usec; |
| 38 |
usec_t cmd_birth_usec; |
| 39 |
bool accept_failure; |
| 40 |
int fd_stdout; |
| 41 |
int fd_stderr; |
| 42 |
char *result; |
| 43 |
size_t result_size; |
| 44 |
size_t result_len; |
| 45 |
bool truncated; |
| 46 |
} Spawn; |
| 47 |
|
| 48 |
static int on_spawn_io(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 49 |
Spawn *spawn = ASSERT_PTR(userdata); |
| 50 |
bool read_to_result; |
| 51 |
char buf[4096], *p; |
| 52 |
size_t size; |
| 53 |
ssize_t l; |
| 54 |
int r; |
| 55 |
|
| 56 |
assert(fd == spawn->fd_stdout || fd == spawn->fd_stderr); |
| 57 |
assert(!spawn->result || spawn->result_len < spawn->result_size); |
| 58 |
|
| 59 |
if (fd == spawn->fd_stdout && spawn->result && !spawn->truncated) { |
| 60 |
/* When reading to the result buffer, use the maximum available size, to detect truncation. */ |
| 61 |
read_to_result = true; |
| 62 |
p = spawn->result + spawn->result_len; |
| 63 |
size = spawn->result_size - spawn->result_len; |
| 64 |
assert(size > 0); |
| 65 |
} else { |
| 66 |
/* When reading to the local buffer, keep the space for the trailing NUL. */ |
| 67 |
read_to_result = false; |
| 68 |
p = buf; |
| 69 |
size = sizeof(buf) - 1; |
| 70 |
} |
| 71 |
|
| 72 |
l = read(fd, p, size); |
| 73 |
if (l < 0) { |
| 74 |
log_device_full_errno(spawn->device, |
| 75 |
ERRNO_IS_TRANSIENT(errno) ? LOG_DEBUG : LOG_WARNING, |
| 76 |
errno, |
| 77 |
"Failed to read %s of '%s', ignoring: %m", |
| 78 |
fd == spawn->fd_stdout ? "stdout" : "stderr", |
| 79 |
spawn->cmd); |
| 80 |
return 0; |
| 81 |
} |
| 82 |
if (l == 0) { /* EOF */ |
| 83 |
r = sd_event_source_set_enabled(s, SD_EVENT_OFF); |
| 84 |
if (r < 0) { |
| 85 |
log_device_warning_errno(spawn->device, r, |
| 86 |
"Failed to disable %s event source of '%s': %m", |
| 87 |
fd == spawn->fd_stdout ? "stdout" : "stderr", |
| 88 |
spawn->cmd); |
| 89 |
(void) sd_event_exit(sd_event_source_get_event(s), r); /* propagate negative errno */ |
| 90 |
return r; |
| 91 |
} |
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
if (read_to_result) { |
| 96 |
if ((size_t) l == size) { |
| 97 |
log_device_warning(spawn->device, "Truncating stdout of '%s' up to %zu byte.", |
| 98 |
spawn->cmd, spawn->result_size - 1); |
| 99 |
l--; |
| 100 |
spawn->truncated = true; |
| 101 |
} |
| 102 |
|
| 103 |
spawn->result_len += l; |
| 104 |
} |
| 105 |
|
| 106 |
p[l] = '\0'; |
| 107 |
|
| 108 |
/* Log output only if we watch stderr. */ |
| 109 |
if (l > 0 && spawn->fd_stderr >= 0) { |
| 110 |
_cleanup_strv_free_ char **v = NULL; |
| 111 |
|
| 112 |
r = strv_split_newlines_full(&v, p, EXTRACT_RETAIN_ESCAPE); |
| 113 |
if (r < 0) |
| 114 |
log_device_debug(spawn->device, |
| 115 |
"Failed to split output from '%s'(%s), ignoring: %m", |
| 116 |
spawn->cmd, fd == spawn->fd_stdout ? "out" : "err"); |
| 117 |
|
| 118 |
STRV_FOREACH(q, v) |
| 119 |
log_device_debug(spawn->device, "'%s'(%s) '%s'", spawn->cmd, |
| 120 |
fd == spawn->fd_stdout ? "out" : "err", *q); |
| 121 |
} |
| 122 |
|
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
static int on_spawn_timeout(sd_event_source *s, uint64_t usec, void *userdata) { |
| 127 |
Spawn *spawn = ASSERT_PTR(userdata); |
| 128 |
|
| 129 |
DEVICE_TRACE_POINT(spawn_timeout, spawn->device, spawn->cmd); |
| 130 |
|
| 131 |
log_device_error(spawn->device, "Spawned process '%s' ["PID_FMT"] timed out after %s, killing.", |
| 132 |
spawn->cmd, spawn->pidref.pid, |
| 133 |
FORMAT_TIMESPAN(spawn->timeout_usec, USEC_PER_SEC)); |
| 134 |
|
| 135 |
(void) pidref_kill_and_sigcont(&spawn->pidref, spawn->timeout_signal); |
| 136 |
return 1; |
| 137 |
} |
| 138 |
|
| 139 |
static int on_spawn_timeout_warning(sd_event_source *s, uint64_t usec, void *userdata) { |
| 140 |
Spawn *spawn = ASSERT_PTR(userdata); |
| 141 |
|
| 142 |
log_device_warning(spawn->device, "Spawned process '%s' ["PID_FMT"] is taking longer than %s to complete.", |
| 143 |
spawn->cmd, spawn->pidref.pid, |
| 144 |
FORMAT_TIMESPAN(spawn->timeout_warn_usec, USEC_PER_SEC)); |
| 145 |
|
| 146 |
return 1; |
| 147 |
} |
| 148 |
|
| 149 |
static int on_spawn_exit(sd_event_source *s, const siginfo_t *si, void *userdata) { |
| 150 |
Spawn *spawn = ASSERT_PTR(userdata); |
| 151 |
int ret = -EIO; |
| 152 |
|
| 153 |
switch (si->si_code) { |
| 154 |
case CLD_EXITED: |
| 155 |
if (si->si_status == 0) |
| 156 |
log_device_debug(spawn->device, "Process '%s' succeeded.", spawn->cmd); |
| 157 |
else |
| 158 |
log_device_full(spawn->device, spawn->accept_failure ? LOG_DEBUG : LOG_WARNING, |
| 159 |
"Process '%s' failed with exit code %i.", spawn->cmd, si->si_status); |
| 160 |
ret = si->si_status; |
| 161 |
break; |
| 162 |
case CLD_KILLED: |
| 163 |
case CLD_DUMPED: |
| 164 |
log_device_error(spawn->device, "Process '%s' terminated by signal %s.", spawn->cmd, signal_to_string(si->si_status)); |
| 165 |
break; |
| 166 |
default: |
| 167 |
log_device_error(spawn->device, "Process '%s' failed due to unknown reason.", spawn->cmd); |
| 168 |
} |
| 169 |
|
| 170 |
DEVICE_TRACE_POINT(spawn_exit, spawn->device, spawn->cmd); |
| 171 |
|
| 172 |
sd_event_exit(sd_event_source_get_event(s), ret); |
| 173 |
return 1; |
| 174 |
} |
| 175 |
|
| 176 |
static int spawn_wait(Spawn *spawn) { |
| 177 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 178 |
_cleanup_(sd_event_source_disable_unrefp) sd_event_source *child_source = NULL; |
| 179 |
_cleanup_(sd_event_source_disable_unrefp) sd_event_source *stdout_source = NULL; |
| 180 |
_cleanup_(sd_event_source_disable_unrefp) sd_event_source *stderr_source = NULL; |
| 181 |
int r; |
| 182 |
|
| 183 |
assert(spawn); |
| 184 |
|
| 185 |
r = sd_event_new(&e); |
| 186 |
if (r < 0) |
| 187 |
return log_device_debug_errno(spawn->device, r, "Failed to allocate sd-event object: %m"); |
| 188 |
|
| 189 |
if (spawn->timeout_usec != USEC_INFINITY) { |
| 190 |
if (spawn->timeout_warn_usec < spawn->timeout_usec) { |
| 191 |
r = sd_event_add_time(e, NULL, CLOCK_MONOTONIC, |
| 192 |
usec_add(spawn->cmd_birth_usec, spawn->timeout_warn_usec), USEC_PER_SEC, |
| 193 |
on_spawn_timeout_warning, spawn); |
| 194 |
if (r < 0) |
| 195 |
return log_device_debug_errno(spawn->device, r, "Failed to create timeout warning event source: %m"); |
| 196 |
} |
| 197 |
|
| 198 |
r = sd_event_add_time(e, NULL, CLOCK_MONOTONIC, |
| 199 |
usec_add(spawn->cmd_birth_usec, spawn->timeout_usec), USEC_PER_SEC, |
| 200 |
on_spawn_timeout, spawn); |
| 201 |
if (r < 0) |
| 202 |
return log_device_debug_errno(spawn->device, r, "Failed to create timeout event source: %m"); |
| 203 |
} |
| 204 |
|
| 205 |
if (spawn->fd_stdout >= 0) { |
| 206 |
r = sd_event_add_io(e, &stdout_source, spawn->fd_stdout, EPOLLIN, on_spawn_io, spawn); |
| 207 |
if (r < 0) |
| 208 |
return log_device_debug_errno(spawn->device, r, "Failed to create stdio event source: %m"); |
| 209 |
} |
| 210 |
|
| 211 |
if (spawn->fd_stderr >= 0) { |
| 212 |
r = sd_event_add_io(e, &stderr_source, spawn->fd_stderr, EPOLLIN, on_spawn_io, spawn); |
| 213 |
if (r < 0) |
| 214 |
return log_device_debug_errno(spawn->device, r, "Failed to create stderr event source: %m"); |
| 215 |
} |
| 216 |
|
| 217 |
r = event_add_child_pidref(e, &child_source, &spawn->pidref, WEXITED, on_spawn_exit, spawn); |
| 218 |
if (r < 0) |
| 219 |
return log_device_debug_errno(spawn->device, r, "Failed to create sigchild event source: %m"); |
| 220 |
|
| 221 |
/* Child exit should be processed after IO is complete */ |
| 222 |
r = sd_event_source_set_priority(child_source, SD_EVENT_PRIORITY_NORMAL + 1); |
| 223 |
if (r < 0) |
| 224 |
return log_device_debug_errno(spawn->device, r, "Failed to set priority to sigchild event source: %m"); |
| 225 |
|
| 226 |
return sd_event_loop(e); |
| 227 |
} |
| 228 |
|
| 229 |
int udev_event_spawn( |
| 230 |
UdevEvent *event, |
| 231 |
bool accept_failure, |
| 232 |
const char *cmd, |
| 233 |
char *result, |
| 234 |
size_t result_size, |
| 235 |
bool *ret_truncated) { |
| 236 |
|
| 237 |
int r; |
| 238 |
|
| 239 |
assert(event); |
| 240 |
assert(IN_SET(event->event_mode, EVENT_UDEV_WORKER, EVENT_UDEVADM_TEST, EVENT_TEST_RULE_RUNNER, EVENT_TEST_SPAWN)); |
| 241 |
assert(event->dev); |
| 242 |
assert(cmd); |
| 243 |
assert(result || result_size == 0); |
| 244 |
|
| 245 |
if (event->event_mode == EVENT_UDEVADM_TEST && |
| 246 |
!STARTSWITH_SET(cmd, "ata_id", "cdrom_id", "dmi_memory_id", "fido_id", "mtd_probe", "scsi_id")) { |
| 247 |
log_device_debug(event->dev, "Running in test mode, skipping execution of '%s'.", cmd); |
| 248 |
result[0] = '\0'; |
| 249 |
if (ret_truncated) |
| 250 |
*ret_truncated = false; |
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
int timeout_signal = event->worker ? event->worker->config.timeout_signal : SIGKILL; |
| 255 |
usec_t timeout_usec = event->worker ? event->worker->config.timeout_usec : DEFAULT_WORKER_TIMEOUT_USEC; |
| 256 |
usec_t now_usec = now(CLOCK_MONOTONIC); |
| 257 |
usec_t age_usec = usec_sub_unsigned(now_usec, event->birth_usec); |
| 258 |
usec_t cmd_timeout_usec = usec_sub_unsigned(timeout_usec, age_usec); |
| 259 |
if (cmd_timeout_usec <= 0) |
| 260 |
return log_device_warning_errno(event->dev, SYNTHETIC_ERRNO(ETIME), |
| 261 |
"The event already takes longer (%s) than the timeout (%s), skipping execution of '%s'.", |
| 262 |
FORMAT_TIMESPAN(age_usec, 1), FORMAT_TIMESPAN(timeout_usec, 1), cmd); |
| 263 |
|
| 264 |
/* pipes from child to parent */ |
| 265 |
_cleanup_close_pair_ int outpipe[2] = EBADF_PAIR; |
| 266 |
if (result || log_get_max_level() >= LOG_INFO) |
| 267 |
if (pipe2(outpipe, O_NONBLOCK|O_CLOEXEC) != 0) |
| 268 |
return log_device_error_errno(event->dev, errno, |
| 269 |
"Failed to create pipe for command '%s': %m", cmd); |
| 270 |
|
| 271 |
_cleanup_close_pair_ int errpipe[2] = EBADF_PAIR; |
| 272 |
if (log_get_max_level() >= LOG_INFO) |
| 273 |
if (pipe2(errpipe, O_NONBLOCK|O_CLOEXEC) != 0) |
| 274 |
return log_device_error_errno(event->dev, errno, |
| 275 |
"Failed to create pipe for command '%s': %m", cmd); |
| 276 |
|
| 277 |
_cleanup_strv_free_ char **argv = NULL; |
| 278 |
r = strv_split_full(&argv, cmd, NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX | EXTRACT_RETAIN_ESCAPE); |
| 279 |
if (r < 0) |
| 280 |
return log_device_error_errno(event->dev, r, "Failed to split command: %m"); |
| 281 |
|
| 282 |
if (isempty(argv[0])) |
| 283 |
return log_device_error_errno(event->dev, SYNTHETIC_ERRNO(EINVAL), |
| 284 |
"Invalid command '%s'", cmd); |
| 285 |
|
| 286 |
/* allow programs in /usr/lib/udev/ to be called without the path */ |
| 287 |
if (!path_is_absolute(argv[0])) { |
| 288 |
char *program; |
| 289 |
|
| 290 |
program = path_join(UDEVLIBEXECDIR, argv[0]); |
| 291 |
if (!program) |
| 292 |
return log_oom(); |
| 293 |
|
| 294 |
free_and_replace(argv[0], program); |
| 295 |
} |
| 296 |
|
| 297 |
char *found; |
| 298 |
_cleanup_close_ int fd_executable = r = pin_callout_binary(argv[0], &found); |
| 299 |
if (r < 0) |
| 300 |
return log_device_error_errno(event->dev, r, "Failed to find and pin callout binary \"%s\": %m", argv[0]); |
| 301 |
|
| 302 |
log_device_debug(event->dev, "Found callout binary: \"%s\".", found); |
| 303 |
free_and_replace(argv[0], found); |
| 304 |
|
| 305 |
char **envp; |
| 306 |
r = device_get_properties_strv(event->dev, &envp); |
| 307 |
if (r < 0) |
| 308 |
return log_device_error_errno(event->dev, r, "Failed to get device properties"); |
| 309 |
|
| 310 |
log_device_debug(event->dev, "Starting '%s'", cmd); |
| 311 |
|
| 312 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 313 |
r = pidref_safe_fork_full( |
| 314 |
"(spawn)", |
| 315 |
(int[]) { -EBADF, outpipe[WRITE_END], errpipe[WRITE_END] }, |
| 316 |
&fd_executable, 1, |
| 317 |
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REARRANGE_STDIO|FORK_LOG|FORK_RLIMIT_NOFILE_SAFE, |
| 318 |
&pidref); |
| 319 |
if (r < 0) |
| 320 |
return log_device_error_errno(event->dev, r, |
| 321 |
"Failed to fork() to execute command '%s': %m", cmd); |
| 322 |
if (r == 0) { |
| 323 |
DEVICE_TRACE_POINT(spawn_exec, event->dev, cmd); |
| 324 |
(void) fexecve_or_execve(fd_executable, argv[0], argv, envp); |
| 325 |
_exit(EXIT_FAILURE); |
| 326 |
} |
| 327 |
|
| 328 |
/* parent closed child's ends of pipes */ |
| 329 |
outpipe[WRITE_END] = safe_close(outpipe[WRITE_END]); |
| 330 |
errpipe[WRITE_END] = safe_close(errpipe[WRITE_END]); |
| 331 |
|
| 332 |
Spawn spawn = { |
| 333 |
.device = event->dev, |
| 334 |
.cmd = cmd, |
| 335 |
.pidref = pidref, /* Do not take ownership */ |
| 336 |
.accept_failure = accept_failure, |
| 337 |
.timeout_warn_usec = udev_warn_timeout(cmd_timeout_usec), |
| 338 |
.timeout_usec = cmd_timeout_usec, |
| 339 |
.timeout_signal = timeout_signal, |
| 340 |
.event_birth_usec = event->birth_usec, |
| 341 |
.cmd_birth_usec = now_usec, |
| 342 |
.fd_stdout = outpipe[READ_END], |
| 343 |
.fd_stderr = errpipe[READ_END], |
| 344 |
.result = result, |
| 345 |
.result_size = result_size, |
| 346 |
}; |
| 347 |
r = spawn_wait(&spawn); |
| 348 |
if (r < 0) |
| 349 |
return log_device_error_errno(event->dev, r, |
| 350 |
"Failed to wait for spawned command '%s': %m", cmd); |
| 351 |
|
| 352 |
if (result) |
| 353 |
result[spawn.result_len] = '\0'; |
| 354 |
|
| 355 |
if (ret_truncated) |
| 356 |
*ret_truncated = spawn.truncated; |
| 357 |
|
| 358 |
return r; /* 0 for success, and positive if the program failed */ |
| 359 |
} |
| 360 |
|
| 361 |
void udev_event_execute_run(UdevEvent *event) { |
| 362 |
const char *command; |
| 363 |
void *val; |
| 364 |
int r; |
| 365 |
|
| 366 |
assert(event); |
| 367 |
|
| 368 |
ORDERED_HASHMAP_FOREACH_KEY(val, command, event->run_list) { |
| 369 |
UdevBuiltinCommand builtin_cmd = PTR_TO_UDEV_BUILTIN_CMD(val); |
| 370 |
|
| 371 |
if (builtin_cmd >= 0) { |
| 372 |
log_device_debug(event->dev, "Running built-in command \"%s\"", command); |
| 373 |
r = udev_builtin_run(event, builtin_cmd, command); |
| 374 |
if (r < 0) |
| 375 |
log_device_debug_errno(event->dev, r, "Failed to run built-in command \"%s\", ignoring: %m", command); |
| 376 |
} else { |
| 377 |
if (event->worker && event->worker->config.exec_delay_usec > 0) { |
| 378 |
usec_t now_usec = now(CLOCK_MONOTONIC); |
| 379 |
usec_t age_usec = usec_sub_unsigned(now_usec, event->birth_usec); |
| 380 |
|
| 381 |
if (event->worker->config.exec_delay_usec >= usec_sub_unsigned(event->worker->config.timeout_usec, age_usec)) { |
| 382 |
log_device_warning(event->dev, |
| 383 |
"Cannot delay execution of \"%s\" for %s, skipping.", |
| 384 |
command, FORMAT_TIMESPAN(event->worker->config.exec_delay_usec, USEC_PER_SEC)); |
| 385 |
continue; |
| 386 |
} |
| 387 |
|
| 388 |
log_device_debug(event->dev, "Delaying execution of \"%s\" for %s.", |
| 389 |
command, FORMAT_TIMESPAN(event->worker->config.exec_delay_usec, USEC_PER_SEC)); |
| 390 |
(void) usleep_safe(event->worker->config.exec_delay_usec); |
| 391 |
} |
| 392 |
|
| 393 |
log_device_debug(event->dev, "Running command \"%s\"", command); |
| 394 |
|
| 395 |
r = udev_event_spawn(event, /* accept_failure= */ false, command, NULL, 0, NULL); |
| 396 |
if (r < 0) |
| 397 |
log_device_warning_errno(event->dev, r, "Failed to execute '%s', ignoring: %m", command); |
| 398 |
else if (r > 0) /* returned value is positive when program fails */ |
| 399 |
log_device_debug(event->dev, "Command \"%s\" returned %d (error), ignoring.", command, r); |
| 400 |
} |
| 401 |
} |
| 402 |
} |