| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "sd-daemon.h" |
| 8 |
#include "sd-event.h" |
| 9 |
|
| 10 |
#include "alloc-util.h" |
| 11 |
#include "build.h" |
| 12 |
#include "env-util.h" |
| 13 |
#include "escape.h" |
| 14 |
#include "event-util.h" |
| 15 |
#include "exit-status.h" |
| 16 |
#include "fd-util.h" |
| 17 |
#include "fdset.h" |
| 18 |
#include "format-table.h" |
| 19 |
#include "format-util.h" |
| 20 |
#include "help-util.h" |
| 21 |
#include "log.h" |
| 22 |
#include "main-func.h" |
| 23 |
#include "notify-recv.h" |
| 24 |
#include "options.h" |
| 25 |
#include "parse-util.h" |
| 26 |
#include "pidref.h" |
| 27 |
#include "process-util.h" |
| 28 |
#include "signal-util.h" |
| 29 |
#include "string-util.h" |
| 30 |
#include "strv.h" |
| 31 |
#include "time-util.h" |
| 32 |
#include "user-util.h" |
| 33 |
|
| 34 |
static enum { |
| 35 |
ACTION_NOTIFY, |
| 36 |
ACTION_BOOTED, |
| 37 |
ACTION_FORK, |
| 38 |
} arg_action = ACTION_NOTIFY; |
| 39 |
static bool arg_ready = false; |
| 40 |
static bool arg_reloading = false; |
| 41 |
static bool arg_stopping = false; |
| 42 |
static PidRef arg_pid = PIDREF_NULL; |
| 43 |
static const char *arg_status = NULL; |
| 44 |
static uid_t arg_uid = UID_INVALID; |
| 45 |
static gid_t arg_gid = GID_INVALID; |
| 46 |
static bool arg_no_block = false; |
| 47 |
static char **arg_env = NULL; |
| 48 |
static char **arg_exec = NULL; |
| 49 |
static FDSet *arg_fds = NULL; |
| 50 |
static char *arg_fdname = NULL; |
| 51 |
static bool arg_quiet = false; |
| 52 |
|
| 53 |
STATIC_DESTRUCTOR_REGISTER(arg_pid, pidref_done); |
| 54 |
STATIC_DESTRUCTOR_REGISTER(arg_env, strv_freep); |
| 55 |
STATIC_DESTRUCTOR_REGISTER(arg_exec, strv_freep); |
| 56 |
STATIC_DESTRUCTOR_REGISTER(arg_fds, fdset_freep); |
| 57 |
STATIC_DESTRUCTOR_REGISTER(arg_fdname, freep); |
| 58 |
|
| 59 |
static int help(void) { |
| 60 |
int r; |
| 61 |
|
| 62 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 63 |
r = option_parser_get_help_table(&options); |
| 64 |
if (r < 0) |
| 65 |
return r; |
| 66 |
|
| 67 |
help_cmdline("[OPTIONS...] [VARIABLE=VALUE...]"); |
| 68 |
help_cmdline("[OPTIONS...] --exec [VARIABLE=VALUE...] ; -- CMDLINE..."); |
| 69 |
help_cmdline("[OPTIONS...] --fork -- CMDLINE..."); |
| 70 |
help_abstract("Notify the service manager about service status updates."); |
| 71 |
|
| 72 |
help_section("Options"); |
| 73 |
r = table_print_or_warn(options); |
| 74 |
if (r < 0) |
| 75 |
return r; |
| 76 |
|
| 77 |
help_man_page_reference("systemd-notify", "1"); |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
static int get_manager_pid(PidRef *ret) { |
| 82 |
int r; |
| 83 |
|
| 84 |
assert(ret); |
| 85 |
|
| 86 |
/* If we run as a service managed by systemd --user the $MANAGERPID environment variable points to |
| 87 |
* the service manager's PID. */ |
| 88 |
const char *e = getenv("MANAGERPID"); |
| 89 |
if (!e) { |
| 90 |
*ret = PIDREF_NULL; |
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
_cleanup_(pidref_done) PidRef manager = PIDREF_NULL; |
| 95 |
r = pidref_set_pidstr(&manager, e); |
| 96 |
if (r < 0) |
| 97 |
return log_warning_errno(r, "$MANAGERPID is set to an invalid PID, ignoring: %s", e); |
| 98 |
|
| 99 |
e = getenv("MANAGERPIDFDID"); |
| 100 |
if (e) { |
| 101 |
uint64_t manager_pidfd_id; |
| 102 |
|
| 103 |
r = safe_atou64(e, &manager_pidfd_id); |
| 104 |
if (r < 0) |
| 105 |
return log_warning_errno(r, "$MANAGERPIDFDID is not set to a valid inode number, ignoring: %s", e); |
| 106 |
|
| 107 |
r = pidref_acquire_pidfd_id(&manager); |
| 108 |
if (r < 0) |
| 109 |
return log_warning_errno(r, "Unable to acquire pidfd ID for manager: %m"); |
| 110 |
|
| 111 |
if (manager_pidfd_id != manager.fd_id) { |
| 112 |
log_debug("$MANAGERPIDFDID doesn't match process currently referenced by $MANAGERPID, suppressing."); |
| 113 |
*ret = PIDREF_NULL; |
| 114 |
return 0; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
*ret = TAKE_PIDREF(manager); |
| 119 |
return 1; |
| 120 |
} |
| 121 |
|
| 122 |
static int pidref_parent_if_applicable(PidRef *ret) { |
| 123 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL, manager = PIDREF_NULL; |
| 124 |
int r; |
| 125 |
|
| 126 |
assert(ret); |
| 127 |
|
| 128 |
r = pidref_set_parent(&pidref); |
| 129 |
if (r < 0) |
| 130 |
return log_debug_errno(r, "Failed to create reference to our parent process: %m"); |
| 131 |
|
| 132 |
/* Don't send from PID 1 or the service manager's PID (which might be distinct from 1, if we are a |
| 133 |
* --user service). That'd just be confusing for the service manager. */ |
| 134 |
if (pidref.pid == 1) |
| 135 |
goto from_self; |
| 136 |
|
| 137 |
r = get_manager_pid(&manager); |
| 138 |
if (r > 0 && pidref_equal(&pidref, &manager)) |
| 139 |
goto from_self; |
| 140 |
|
| 141 |
*ret = TAKE_PIDREF(pidref); |
| 142 |
return 0; |
| 143 |
|
| 144 |
from_self: |
| 145 |
return pidref_set_self(ret); |
| 146 |
} |
| 147 |
|
| 148 |
static int parse_argv(int argc, char *argv[], char ***ret_args) { |
| 149 |
_cleanup_fdset_free_ FDSet *passed = NULL; |
| 150 |
bool do_exec = false; |
| 151 |
int r; |
| 152 |
|
| 153 |
assert(argc >= 0); |
| 154 |
assert(argv); |
| 155 |
|
| 156 |
OptionParser opts = { argc, argv }; |
| 157 |
|
| 158 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 159 |
switch (c) { |
| 160 |
|
| 161 |
OPTION_COMMON_HELP: |
| 162 |
return help(); |
| 163 |
|
| 164 |
OPTION_COMMON_VERSION: |
| 165 |
return version(); |
| 166 |
|
| 167 |
OPTION_LONG("ready", NULL, |
| 168 |
"Inform the service manager about service start-up/reload completion"): |
| 169 |
arg_ready = true; |
| 170 |
break; |
| 171 |
|
| 172 |
OPTION_LONG("reloading", NULL, |
| 173 |
"Inform the service manager about configuration reloading"): |
| 174 |
arg_reloading = true; |
| 175 |
break; |
| 176 |
|
| 177 |
OPTION_LONG("stopping", NULL, |
| 178 |
"Inform the service manager about service shutdown"): |
| 179 |
arg_stopping = true; |
| 180 |
break; |
| 181 |
|
| 182 |
OPTION_FULL(OPTION_OPTIONAL_ARG, /* sc= */ 0, "pid", "PID", |
| 183 |
"Set main PID of daemon"): |
| 184 |
pidref_done(&arg_pid); |
| 185 |
|
| 186 |
if (isempty(opts.arg) || streq(opts.arg, "auto")) |
| 187 |
r = pidref_parent_if_applicable(&arg_pid); |
| 188 |
else if (streq(opts.arg, "parent")) |
| 189 |
r = pidref_set_parent(&arg_pid); |
| 190 |
else if (streq(opts.arg, "self")) |
| 191 |
r = pidref_set_self(&arg_pid); |
| 192 |
else |
| 193 |
r = pidref_set_pidstr(&arg_pid, opts.arg); |
| 194 |
if (r < 0) |
| 195 |
return log_error_errno(r, "Failed to refer to --pid='%s': %m", opts.arg); |
| 196 |
break; |
| 197 |
|
| 198 |
OPTION_LONG("uid", "USER", "Set user to send from"): |
| 199 |
r = get_user_creds(opts.arg, /* flags= */ 0, NULL, &arg_uid, &arg_gid, NULL, NULL); |
| 200 |
if (r == -ESRCH) /* If the user doesn't exist, then accept it anyway as numeric */ |
| 201 |
r = parse_uid(opts.arg, &arg_uid); |
| 202 |
if (r < 0) |
| 203 |
return log_error_errno(r, "Can't resolve user %s: %m", opts.arg); |
| 204 |
break; |
| 205 |
|
| 206 |
OPTION_LONG("status", "TEXT", "Set status text"): |
| 207 |
arg_status = opts.arg; |
| 208 |
break; |
| 209 |
|
| 210 |
OPTION_LONG("booted", NULL, "Check if the system was booted up with systemd"): |
| 211 |
arg_action = ACTION_BOOTED; |
| 212 |
break; |
| 213 |
|
| 214 |
OPTION_LONG("no-block", NULL, "Do not wait until operation finished"): |
| 215 |
arg_no_block = true; |
| 216 |
break; |
| 217 |
|
| 218 |
OPTION_LONG("exec", NULL, "Execute command line separated by ';' once done"): |
| 219 |
do_exec = true; |
| 220 |
break; |
| 221 |
|
| 222 |
OPTION_LONG("fd", "FD", "Pass specified file descriptor along with the message"): { |
| 223 |
_cleanup_close_ int owned_fd = -EBADF; |
| 224 |
|
| 225 |
int fdnr = parse_fd(opts.arg); |
| 226 |
if (fdnr < 0) |
| 227 |
return log_error_errno(fdnr, "Failed to parse file descriptor: %s", opts.arg); |
| 228 |
|
| 229 |
if (!passed) { |
| 230 |
/* Take possession of all passed fds */ |
| 231 |
r = fdset_new_fill(/* filter_cloexec= */ 0, &passed); |
| 232 |
if (r < 0) |
| 233 |
return log_error_errno(r, "Failed to take possession of passed file descriptors: %m"); |
| 234 |
} |
| 235 |
|
| 236 |
if (fdnr < 3) { |
| 237 |
/* For stdin/stdout/stderr we want to keep the fd, too, hence make a copy */ |
| 238 |
owned_fd = fcntl(fdnr, F_DUPFD_CLOEXEC, 3); |
| 239 |
if (owned_fd < 0) |
| 240 |
return log_error_errno(errno, "Failed to duplicate file descriptor: %m"); |
| 241 |
} else { |
| 242 |
/* Otherwise, move the fd over */ |
| 243 |
owned_fd = fdset_remove(passed, fdnr); |
| 244 |
if (owned_fd < 0) |
| 245 |
return log_error_errno(owned_fd, "Specified file descriptor '%i' not passed or specified more than once: %m", fdnr); |
| 246 |
} |
| 247 |
|
| 248 |
if (!arg_fds) { |
| 249 |
arg_fds = fdset_new(); |
| 250 |
if (!arg_fds) |
| 251 |
return log_oom(); |
| 252 |
} |
| 253 |
|
| 254 |
r = fdset_consume(arg_fds, TAKE_FD(owned_fd)); |
| 255 |
if (r < 0) |
| 256 |
return log_error_errno(r, "Failed to add file descriptor to set: %m"); |
| 257 |
break; |
| 258 |
} |
| 259 |
|
| 260 |
OPTION_LONG("fdname", "NAME", "Name to assign to passed file descriptors"): |
| 261 |
if (!fdname_is_valid(opts.arg)) |
| 262 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File descriptor name invalid: %s", opts.arg); |
| 263 |
|
| 264 |
if (free_and_strdup(&arg_fdname, opts.arg) < 0) |
| 265 |
return log_oom(); |
| 266 |
|
| 267 |
break; |
| 268 |
|
| 269 |
OPTION_LONG("fork", NULL, "Receive notifications from child rather than sending them"): |
| 270 |
arg_action = ACTION_FORK; |
| 271 |
break; |
| 272 |
|
| 273 |
OPTION('q', "quiet", NULL, "Do not show PID of child when forking"): |
| 274 |
arg_quiet = true; |
| 275 |
break; |
| 276 |
} |
| 277 |
|
| 278 |
bool have_env = arg_ready || arg_stopping || arg_reloading || arg_status || pidref_is_set(&arg_pid) || !fdset_isempty(arg_fds); |
| 279 |
|
| 280 |
char **args = option_parser_get_args(&opts); |
| 281 |
|
| 282 |
switch (arg_action) { |
| 283 |
|
| 284 |
case ACTION_NOTIFY: { |
| 285 |
if (arg_fdname && fdset_isempty(arg_fds)) |
| 286 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No file descriptors passed, but --fdname= set, refusing."); |
| 287 |
|
| 288 |
size_t n_arg_env; |
| 289 |
|
| 290 |
if (do_exec) { |
| 291 |
int i; |
| 292 |
|
| 293 |
for (i = 0; args[i]; i++) |
| 294 |
if (streq(args[i], ";")) |
| 295 |
break; |
| 296 |
|
| 297 |
if (!args[i]) |
| 298 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "If --exec is used, argument list must contain ';' separator, refusing."); |
| 299 |
if (!args[i + 1]) |
| 300 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Empty command line specified after ';' separator, refusing."); |
| 301 |
|
| 302 |
arg_exec = strv_copy(args + i + 1); |
| 303 |
if (!arg_exec) |
| 304 |
return log_oom(); |
| 305 |
|
| 306 |
n_arg_env = i; |
| 307 |
} else |
| 308 |
n_arg_env = strv_length(args); |
| 309 |
|
| 310 |
have_env = have_env || n_arg_env > 0; |
| 311 |
if (!have_env) { |
| 312 |
if (do_exec) |
| 313 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No notify message specified while --exec, refusing."); |
| 314 |
|
| 315 |
/* No argument at all? */ |
| 316 |
help(); |
| 317 |
return -EINVAL; |
| 318 |
} |
| 319 |
|
| 320 |
if (n_arg_env > 0) { |
| 321 |
arg_env = strv_copy_n(args, n_arg_env); |
| 322 |
if (!arg_env) |
| 323 |
return log_oom(); |
| 324 |
} |
| 325 |
|
| 326 |
if (!fdset_isempty(passed)) |
| 327 |
log_warning("Warning: %u more file descriptors passed than referenced with --fd=.", fdset_size(passed)); |
| 328 |
|
| 329 |
break; |
| 330 |
} |
| 331 |
|
| 332 |
case ACTION_BOOTED: |
| 333 |
if (!strv_isempty(args)) |
| 334 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--booted takes no parameters, refusing."); |
| 335 |
|
| 336 |
break; |
| 337 |
|
| 338 |
case ACTION_FORK: |
| 339 |
if (strv_isempty(args)) |
| 340 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--fork requires a command to be specified, refusing."); |
| 341 |
|
| 342 |
break; |
| 343 |
|
| 344 |
default: |
| 345 |
assert_not_reached(); |
| 346 |
} |
| 347 |
|
| 348 |
if (have_env && arg_action != ACTION_NOTIFY) |
| 349 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 350 |
"--ready, --reloading, --stopping, --pid=, --status=, --fd= may not be combined with --fork or --booted, refusing."); |
| 351 |
|
| 352 |
*ret_args = args; |
| 353 |
|
| 354 |
return 1; |
| 355 |
} |
| 356 |
|
| 357 |
static int on_notify_socket(sd_event_source *s, int fd, unsigned event, void *userdata) { |
| 358 |
PidRef *child = ASSERT_PTR(userdata); |
| 359 |
int r; |
| 360 |
|
| 361 |
assert(s); |
| 362 |
assert(fd >= 0); |
| 363 |
|
| 364 |
_cleanup_free_ char *text = NULL; |
| 365 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 366 |
r = notify_recv(fd, &text, /* ret_ucred= */ NULL, &pidref); |
| 367 |
if (r == -EAGAIN) |
| 368 |
return 0; |
| 369 |
if (r < 0) |
| 370 |
return r; |
| 371 |
|
| 372 |
if (!pidref_equal(child, &pidref)) { |
| 373 |
log_warning("Received notification message from unexpected process " PID_FMT " (expected " PID_FMT "), ignoring.", |
| 374 |
pidref.pid, child->pid); |
| 375 |
return 0; |
| 376 |
} |
| 377 |
|
| 378 |
const char *p = find_line_startswith(text, "READY=1"); |
| 379 |
if (!p || !IN_SET(*p, '\n', 0)) { |
| 380 |
if (!DEBUG_LOGGING) |
| 381 |
return 0; |
| 382 |
|
| 383 |
_cleanup_free_ char *escaped = cescape(text); |
| 384 |
log_debug("Received notification message without READY=1, ignoring: %s", strna(escaped)); |
| 385 |
return 0; |
| 386 |
} |
| 387 |
|
| 388 |
log_debug("Received READY=1, exiting."); |
| 389 |
return sd_event_exit(sd_event_source_get_event(s), EXIT_SUCCESS); |
| 390 |
} |
| 391 |
|
| 392 |
static int on_child(sd_event_source *s, const siginfo_t *si, void *userdata) { |
| 393 |
assert(s); |
| 394 |
assert(si); |
| 395 |
|
| 396 |
int ret; |
| 397 |
if (si->si_code == CLD_EXITED) { |
| 398 |
if (si->si_status != EXIT_SUCCESS) |
| 399 |
log_debug("Child failed with exit status %i.", si->si_status); |
| 400 |
else |
| 401 |
log_debug("Child exited successfully. (But no READY=1 message was sent!)"); |
| 402 |
|
| 403 |
/* NB: we propagate success here if the child exited cleanly but never sent us READY=1. We |
| 404 |
* are not a service manager after all, where this would be a protocol violation. We are just |
| 405 |
* a shell tool to fork off stuff in the background, where I think it makes sense to allow |
| 406 |
* clean early exit of forked off processes. */ |
| 407 |
ret = si->si_status; |
| 408 |
|
| 409 |
} else if (IN_SET(si->si_code, CLD_KILLED, CLD_DUMPED)) |
| 410 |
ret = log_debug_errno(SYNTHETIC_ERRNO(EPROTO), |
| 411 |
"Child terminated by signal %s.", signal_to_string(si->si_status)); |
| 412 |
else |
| 413 |
ret = log_debug_errno(SYNTHETIC_ERRNO(EPROTO), |
| 414 |
"Child terminated due to unknown reason."); |
| 415 |
|
| 416 |
return sd_event_exit(sd_event_source_get_event(s), ret); |
| 417 |
} |
| 418 |
|
| 419 |
static int action_fork(char *const *_command) { |
| 420 |
|
| 421 |
static const int forward_signals[] = { |
| 422 |
SIGHUP, |
| 423 |
SIGTERM, |
| 424 |
SIGINT, |
| 425 |
SIGQUIT, |
| 426 |
SIGTSTP, |
| 427 |
SIGCONT, |
| 428 |
SIGUSR1, |
| 429 |
SIGUSR2, |
| 430 |
}; |
| 431 |
|
| 432 |
int r; |
| 433 |
|
| 434 |
assert(!strv_isempty(_command)); |
| 435 |
|
| 436 |
/* Make a copy, since pidref_safe_fork_full() will change argv[] further down. */ |
| 437 |
_cleanup_strv_free_ char **command = strv_copy(_command); |
| 438 |
if (!command) |
| 439 |
return log_oom(); |
| 440 |
|
| 441 |
_cleanup_free_ char *c = strv_join(command, " "); |
| 442 |
if (!c) |
| 443 |
return log_oom(); |
| 444 |
|
| 445 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 446 |
r = sd_event_new(&event); |
| 447 |
if (r < 0) |
| 448 |
return log_error_errno(r, "Failed to allocate event loop: %m"); |
| 449 |
|
| 450 |
_cleanup_(pidref_done) PidRef child = PIDREF_NULL; |
| 451 |
_cleanup_free_ char *addr_string = NULL; |
| 452 |
r = notify_socket_prepare( |
| 453 |
event, |
| 454 |
SD_EVENT_PRIORITY_NORMAL - 10, /* If we receive both the sd_notify() message and |
| 455 |
* child exit notification, always process sd_notify() |
| 456 |
* first, it's the more interesting, "positive" |
| 457 |
* information. */ |
| 458 |
on_notify_socket, |
| 459 |
&child, |
| 460 |
&addr_string); |
| 461 |
if (r < 0) |
| 462 |
return log_error_errno(r, "Failed to prepare notify socket: %m"); |
| 463 |
|
| 464 |
r = pidref_safe_fork_full( |
| 465 |
"(notify)", |
| 466 |
/* stdio_fds= */ (const int[]) { -EBADF, -EBADF, STDERR_FILENO }, |
| 467 |
/* except_fds= */ NULL, |
| 468 |
/* n_except_fds= */ 0, |
| 469 |
/* flags= */ FORK_REARRANGE_STDIO|FORK_LOG, |
| 470 |
&child); |
| 471 |
if (r < 0) |
| 472 |
return log_error_errno(r, "Failed to fork child in order to execute '%s': %m", c); |
| 473 |
if (r == 0) { |
| 474 |
if (setenv("NOTIFY_SOCKET", addr_string, /* overwrite= */ true) < 0) { |
| 475 |
log_error_errno(errno, "Failed to set $NOTIFY_SOCKET: %m"); |
| 476 |
_exit(EXIT_MEMORY); |
| 477 |
} |
| 478 |
|
| 479 |
log_debug("Executing: %s", c); |
| 480 |
execvp(command[0], command); |
| 481 |
log_error_errno(errno, "Failed to execute '%s': %m", c); |
| 482 |
_exit(EXIT_EXEC); |
| 483 |
} |
| 484 |
|
| 485 |
if (!arg_quiet) { |
| 486 |
printf(PID_FMT "\n", child.pid); |
| 487 |
fflush(stdout); |
| 488 |
} |
| 489 |
|
| 490 |
_cleanup_(sd_event_source_disable_unrefp) sd_event_source *child_event_source = NULL; |
| 491 |
r = event_add_child_pidref(event, &child_event_source, &child, WEXITED, on_child, /* userdata= */ NULL); |
| 492 |
if (r < 0) |
| 493 |
return log_error_errno(r, "Failed to allocate child source: %m"); |
| 494 |
|
| 495 |
/* Handle child exit before propagating the other signals below */ |
| 496 |
r = sd_event_source_set_priority(child_event_source, SD_EVENT_PRIORITY_NORMAL - 5); |
| 497 |
if (r < 0) |
| 498 |
return log_error_errno(r, "Failed to change child event source priority: %m"); |
| 499 |
|
| 500 |
sd_event_source **forward_signal_sources = NULL; |
| 501 |
size_t n_forward_signal_sources = 0; |
| 502 |
CLEANUP_ARRAY(forward_signal_sources, n_forward_signal_sources, event_source_unref_many); |
| 503 |
|
| 504 |
r = event_forward_signals( |
| 505 |
event, |
| 506 |
child_event_source, |
| 507 |
forward_signals, ELEMENTSOF(forward_signals), |
| 508 |
&forward_signal_sources, &n_forward_signal_sources); |
| 509 |
if (r < 0) |
| 510 |
return log_error_errno(r, "Failed to set up signal forwarding: %m"); |
| 511 |
|
| 512 |
r = sd_event_loop(event); |
| 513 |
if (r < 0) |
| 514 |
return log_error_errno(r, "Failed to run event loop: %m"); |
| 515 |
|
| 516 |
return r; |
| 517 |
} |
| 518 |
|
| 519 |
static int run(int argc, char* argv[]) { |
| 520 |
_cleanup_free_ char *status = NULL, *main_pid = NULL, *main_pidfd_id = NULL, *msg = NULL, |
| 521 |
*monotonic_usec = NULL, *fdn = NULL; |
| 522 |
_cleanup_strv_free_ char **final_env = NULL; |
| 523 |
const char *our_env[10]; |
| 524 |
size_t i = 0; |
| 525 |
char **args = NULL; /* unnecessary initialization to appease gcc */ |
| 526 |
int r; |
| 527 |
|
| 528 |
log_setup(); |
| 529 |
|
| 530 |
r = parse_argv(argc, argv, &args); |
| 531 |
if (r <= 0) |
| 532 |
return r; |
| 533 |
assert(args); |
| 534 |
|
| 535 |
if (arg_action == ACTION_FORK) |
| 536 |
return action_fork(args); |
| 537 |
|
| 538 |
if (arg_action == ACTION_BOOTED) { |
| 539 |
r = sd_booted(); |
| 540 |
if (r < 0) |
| 541 |
log_debug_errno(r, "Failed to determine whether we are booted with systemd, assuming we aren't: %m"); |
| 542 |
else |
| 543 |
log_debug("The system %s booted with systemd.", r ? "was" : "was not"); |
| 544 |
|
| 545 |
return r <= 0; |
| 546 |
} |
| 547 |
|
| 548 |
if (arg_reloading) { |
| 549 |
our_env[i++] = "RELOADING=1"; |
| 550 |
|
| 551 |
if (asprintf(&monotonic_usec, "MONOTONIC_USEC=" USEC_FMT, now(CLOCK_MONOTONIC)) < 0) |
| 552 |
return log_oom(); |
| 553 |
|
| 554 |
our_env[i++] = monotonic_usec; |
| 555 |
} |
| 556 |
|
| 557 |
if (arg_ready) |
| 558 |
our_env[i++] = "READY=1"; |
| 559 |
|
| 560 |
if (arg_stopping) |
| 561 |
our_env[i++] = "STOPPING=1"; |
| 562 |
|
| 563 |
if (arg_status) { |
| 564 |
status = strjoin("STATUS=", arg_status); |
| 565 |
if (!status) |
| 566 |
return log_oom(); |
| 567 |
|
| 568 |
our_env[i++] = status; |
| 569 |
} |
| 570 |
|
| 571 |
if (pidref_is_set(&arg_pid)) { |
| 572 |
if (asprintf(&main_pid, "MAINPID="PID_FMT, arg_pid.pid) < 0) |
| 573 |
return log_oom(); |
| 574 |
|
| 575 |
our_env[i++] = main_pid; |
| 576 |
|
| 577 |
r = pidref_acquire_pidfd_id(&arg_pid); |
| 578 |
if (r < 0) |
| 579 |
log_debug_errno(r, "Unable to acquire pidfd id of new main pid " PID_FMT ", ignoring: %m", |
| 580 |
arg_pid.pid); |
| 581 |
else { |
| 582 |
if (asprintf(&main_pidfd_id, "MAINPIDFDID=%" PRIu64, arg_pid.fd_id) < 0) |
| 583 |
return log_oom(); |
| 584 |
|
| 585 |
our_env[i++] = main_pidfd_id; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
if (!fdset_isempty(arg_fds)) { |
| 590 |
our_env[i++] = "FDSTORE=1"; |
| 591 |
|
| 592 |
if (arg_fdname) { |
| 593 |
fdn = strjoin("FDNAME=", arg_fdname); |
| 594 |
if (!fdn) |
| 595 |
return log_oom(); |
| 596 |
|
| 597 |
our_env[i++] = fdn; |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
our_env[i++] = NULL; |
| 602 |
assert(i <= ELEMENTSOF(our_env)); |
| 603 |
|
| 604 |
final_env = strv_env_merge((char**) our_env, arg_env); |
| 605 |
if (!final_env) |
| 606 |
return log_oom(); |
| 607 |
assert(!strv_isempty(final_env)); |
| 608 |
|
| 609 |
msg = strv_join(final_env, "\n"); |
| 610 |
if (!msg) |
| 611 |
return log_oom(); |
| 612 |
|
| 613 |
/* If this is requested change to the requested UID/GID. Note that we only change the real UID here, and leave |
| 614 |
the effective UID in effect (which is 0 for this to work). That's because we want the privileges to fake the |
| 615 |
ucred data, and sd_pid_notify() uses the real UID for filling in ucred. */ |
| 616 |
|
| 617 |
if (arg_gid != GID_INVALID && |
| 618 |
setregid(arg_gid, GID_INVALID) < 0) |
| 619 |
return log_error_errno(errno, "Failed to change GID: %m"); |
| 620 |
|
| 621 |
if (arg_uid != UID_INVALID && |
| 622 |
setreuid(arg_uid, UID_INVALID) < 0) |
| 623 |
return log_error_errno(errno, "Failed to change UID: %m"); |
| 624 |
|
| 625 |
/* If --pid= is explicitly specified, use it as source pid. Otherwise, pretend the message originates |
| 626 |
* from our parent, i.e. --pid=auto */ |
| 627 |
if (!pidref_is_set(&arg_pid)) |
| 628 |
(void) pidref_parent_if_applicable(&arg_pid); |
| 629 |
|
| 630 |
if (fdset_isempty(arg_fds)) |
| 631 |
r = sd_pid_notify(arg_pid.pid, /* unset_environment= */ false, msg); |
| 632 |
else { |
| 633 |
_cleanup_free_ int *a = NULL; |
| 634 |
int k; |
| 635 |
|
| 636 |
k = fdset_to_array(arg_fds, &a); |
| 637 |
if (k < 0) |
| 638 |
return log_error_errno(k, "Failed to convert file descriptor set to array: %m"); |
| 639 |
|
| 640 |
r = sd_pid_notify_with_fds(arg_pid.pid, /* unset_environment= */ false, msg, a, k); |
| 641 |
|
| 642 |
} |
| 643 |
if (r == -E2BIG) |
| 644 |
return log_error_errno(r, "Too many file descriptors passed."); |
| 645 |
if (r < 0) |
| 646 |
return log_error_errno(r, "Failed to notify service manager: %m"); |
| 647 |
if (r == 0) |
| 648 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 649 |
"No status data could be sent: $NOTIFY_SOCKET was not set"); |
| 650 |
|
| 651 |
arg_fds = fdset_free(arg_fds); /* Close before we execute anything */ |
| 652 |
|
| 653 |
if (!arg_no_block) { |
| 654 |
r = sd_pid_notify_barrier(arg_pid.pid, /* unset_environment= */ false, 5 * USEC_PER_SEC); |
| 655 |
if (r < 0) |
| 656 |
return log_error_errno(r, "Failed to invoke barrier: %m"); |
| 657 |
if (r == 0) |
| 658 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 659 |
"No status data could be sent: $NOTIFY_SOCKET was not set"); |
| 660 |
} |
| 661 |
|
| 662 |
if (arg_exec) { |
| 663 |
execvp(arg_exec[0], arg_exec); |
| 664 |
|
| 665 |
_cleanup_free_ char *cmdline = strv_join(arg_exec, " "); |
| 666 |
return log_error_errno(errno, "Failed to execute command line: %s", strnull(cmdline)); |
| 667 |
} |
| 668 |
|
| 669 |
/* The DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE() boilerplate will send the exit status via |
| 670 |
* sd_notify(). Which is normally fine, but very confusing in systemd-notify, whose purpose is to |
| 671 |
* send user-controllable notification messages, and not implicit ones. Let's turn if off, by |
| 672 |
* unsetting the $NOTIFY_SOCKET environment variable. */ |
| 673 |
(void) unsetenv("NOTIFY_SOCKET"); |
| 674 |
return 0; |
| 675 |
} |
| 676 |
|
| 677 |
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |