| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <linux/kd.h> |
| 5 |
#include <sys/inotify.h> |
| 6 |
#include <sys/ioctl.h> |
| 7 |
#include <sys/mount.h> |
| 8 |
#include <sys/reboot.h> |
| 9 |
#include <sys/wait.h> |
| 10 |
#include <unistd.h> |
| 11 |
|
| 12 |
#include "sd-bus.h" |
| 13 |
#include "sd-daemon.h" |
| 14 |
#include "sd-messages.h" |
| 15 |
#include "sd-netlink.h" |
| 16 |
#include "sd-path.h" |
| 17 |
|
| 18 |
#include "all-units.h" |
| 19 |
#include "alloc-util.h" |
| 20 |
#include "architecture.h" |
| 21 |
#include "audit-fd.h" |
| 22 |
#include "boot-timestamps.h" |
| 23 |
#include "bpf-restrict-fs.h" |
| 24 |
#include "bpf-restrict-fsaccess.h" |
| 25 |
#include "build-path.h" |
| 26 |
#include "bus-common-errors.h" |
| 27 |
#include "bus-error.h" |
| 28 |
#include "clean-ipc.h" |
| 29 |
#include "common-signal.h" |
| 30 |
#include "confidential-virt.h" |
| 31 |
#include "constants.h" |
| 32 |
#include "creds-util.h" |
| 33 |
#include "daemon-util.h" |
| 34 |
#include "dbus-job.h" |
| 35 |
#include "dbus-manager.h" |
| 36 |
#include "dbus-unit.h" |
| 37 |
#include "dbus.h" |
| 38 |
#include "dirent-util.h" |
| 39 |
#include "dynamic-user.h" |
| 40 |
#include "env-util.h" |
| 41 |
#include "escape.h" |
| 42 |
#include "event-util.h" |
| 43 |
#include "exec-util.h" |
| 44 |
#include "execute.h" |
| 45 |
#include "exit-status.h" |
| 46 |
#include "fd-util.h" |
| 47 |
#include "fdset.h" |
| 48 |
#include "format-util.h" |
| 49 |
#include "fs-util.h" |
| 50 |
#include "generator-setup.h" |
| 51 |
#include "hashmap.h" |
| 52 |
#include "initrd-util.h" |
| 53 |
#include "inotify-util.h" |
| 54 |
#include "install.h" |
| 55 |
#include "io-util.h" |
| 56 |
#include "iovec-util.h" |
| 57 |
#include "libaudit-util.h" |
| 58 |
#include "locale-setup.h" |
| 59 |
#include "log.h" |
| 60 |
#include "luo.h" |
| 61 |
#include "manager-dump.h" |
| 62 |
#include "manager-serialize.h" |
| 63 |
#include "manager.h" |
| 64 |
#include "mkdir.h" |
| 65 |
#include "mount-util.h" |
| 66 |
#include "notify-recv.h" |
| 67 |
#include "parse-util.h" |
| 68 |
#include "path-lookup.h" |
| 69 |
#include "path-util.h" |
| 70 |
#include "plymouth-util.h" |
| 71 |
#include "pretty-print.h" |
| 72 |
#include "prioq.h" |
| 73 |
#include "process-util.h" |
| 74 |
#include "psi-util.h" |
| 75 |
#include "ratelimit.h" |
| 76 |
#include "rlimit-util.h" |
| 77 |
#include "rm-rf.h" |
| 78 |
#include "selinux-util.h" |
| 79 |
#include "serialize.h" |
| 80 |
#include "service.h" |
| 81 |
#include "set.h" |
| 82 |
#include "signal-util.h" |
| 83 |
#include "socket-util.h" |
| 84 |
#include "special.h" |
| 85 |
#include "stat-util.h" |
| 86 |
#include "string-table.h" |
| 87 |
#include "string-util.h" |
| 88 |
#include "strv.h" |
| 89 |
#include "strxcpyx.h" |
| 90 |
#include "sysctl-util.h" |
| 91 |
#include "syslog-util.h" |
| 92 |
#include "taint.h" |
| 93 |
#include "terminal-util.h" |
| 94 |
#include "time-util.h" |
| 95 |
#include "transaction.h" |
| 96 |
#include "umask-util.h" |
| 97 |
#include "unit-name.h" |
| 98 |
#include "user-util.h" |
| 99 |
#include "varlink-unit.h" |
| 100 |
#include "varlink.h" |
| 101 |
#include "virt.h" |
| 102 |
#include "watchdog.h" |
| 103 |
|
| 104 |
/* Make sure clients notifying us don't block */ |
| 105 |
#define MANAGER_SOCKET_RCVBUF_SIZE (8*U64_MB) |
| 106 |
|
| 107 |
/* Initial delay and the interval for printing status messages about running jobs */ |
| 108 |
#define JOBS_IN_PROGRESS_WAIT_USEC (2*USEC_PER_SEC) |
| 109 |
#define JOBS_IN_PROGRESS_QUIET_WAIT_USEC (25*USEC_PER_SEC) |
| 110 |
#define JOBS_IN_PROGRESS_PERIOD_USEC (USEC_PER_SEC / 3) |
| 111 |
#define JOBS_IN_PROGRESS_PERIOD_DIVISOR 3 |
| 112 |
|
| 113 |
/* If there are more than 1K bus messages queue across our API and direct buses, then let's not add more on top until |
| 114 |
* the queue gets more empty. */ |
| 115 |
#define MANAGER_BUS_BUSY_THRESHOLD 1024LU |
| 116 |
|
| 117 |
/* How many units and jobs to process of the bus queue before returning to the event loop. */ |
| 118 |
#define MANAGER_BUS_MESSAGE_BUDGET 100U |
| 119 |
|
| 120 |
#define DEFAULT_TASKS_MAX ((const CGroupTasksMax) { 15U, 100U }) /* 15% */ |
| 121 |
|
| 122 |
static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
| 123 |
static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
| 124 |
static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
| 125 |
static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
| 126 |
static int manager_dispatch_user_lookup_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
| 127 |
static int manager_dispatch_handoff_timestamp_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
| 128 |
static int manager_dispatch_pidref_transport_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
| 129 |
static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata); |
| 130 |
static int manager_dispatch_run_queue(sd_event_source *source, void *userdata); |
| 131 |
static int manager_dispatch_sigchld(sd_event_source *source, void *userdata); |
| 132 |
static int manager_dispatch_timezone_change(sd_event_source *source, const struct inotify_event *event, void *userdata); |
| 133 |
static int manager_run_environment_generators(Manager *m); |
| 134 |
static int manager_run_generators(Manager *m); |
| 135 |
static void manager_vacuum(Manager *m); |
| 136 |
|
| 137 |
static usec_t manager_watch_jobs_next_time(Manager *m) { |
| 138 |
usec_t timeout; |
| 139 |
|
| 140 |
if (MANAGER_IS_USER(m)) |
| 141 |
/* Let the user manager without a timeout show status quickly, so the system manager can make |
| 142 |
* use of it, if it wants to. */ |
| 143 |
timeout = JOBS_IN_PROGRESS_WAIT_USEC * 2 / 3; |
| 144 |
else if (manager_get_show_status_on(m)) |
| 145 |
/* When status is on, just use the usual timeout. */ |
| 146 |
timeout = JOBS_IN_PROGRESS_WAIT_USEC; |
| 147 |
else |
| 148 |
timeout = JOBS_IN_PROGRESS_QUIET_WAIT_USEC; |
| 149 |
|
| 150 |
return usec_add(now(CLOCK_MONOTONIC), timeout); |
| 151 |
} |
| 152 |
|
| 153 |
static bool manager_is_confirm_spawn_disabled(Manager *m) { |
| 154 |
assert(m); |
| 155 |
|
| 156 |
if (!m->confirm_spawn) |
| 157 |
return true; |
| 158 |
|
| 159 |
return access("/run/systemd/confirm_spawn_disabled", F_OK) >= 0; |
| 160 |
} |
| 161 |
|
| 162 |
static void manager_watch_jobs_in_progress(Manager *m) { |
| 163 |
usec_t next; |
| 164 |
int r; |
| 165 |
|
| 166 |
assert(m); |
| 167 |
|
| 168 |
/* We do not want to show the cylon animation if the user |
| 169 |
* needs to confirm service executions otherwise confirmation |
| 170 |
* messages will be screwed by the cylon animation. */ |
| 171 |
if (!manager_is_confirm_spawn_disabled(m)) |
| 172 |
return; |
| 173 |
|
| 174 |
if (m->jobs_in_progress_event_source) |
| 175 |
return; |
| 176 |
|
| 177 |
next = manager_watch_jobs_next_time(m); |
| 178 |
r = sd_event_add_time( |
| 179 |
m->event, |
| 180 |
&m->jobs_in_progress_event_source, |
| 181 |
CLOCK_MONOTONIC, |
| 182 |
next, 0, |
| 183 |
manager_dispatch_jobs_in_progress, m); |
| 184 |
if (r < 0) |
| 185 |
return; |
| 186 |
|
| 187 |
(void) sd_event_source_set_description(m->jobs_in_progress_event_source, "manager-jobs-in-progress"); |
| 188 |
} |
| 189 |
|
| 190 |
static void manager_flip_auto_status(Manager *m, bool enable, const char *reason) { |
| 191 |
assert(m); |
| 192 |
|
| 193 |
if (enable) { |
| 194 |
if (m->show_status == SHOW_STATUS_AUTO) |
| 195 |
manager_set_show_status(m, SHOW_STATUS_TEMPORARY, reason); |
| 196 |
} else { |
| 197 |
if (m->show_status == SHOW_STATUS_TEMPORARY) |
| 198 |
manager_set_show_status(m, SHOW_STATUS_AUTO, reason); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
static void manager_print_jobs_in_progress(Manager *m) { |
| 203 |
Job *j; |
| 204 |
unsigned counter = 0, print_nr; |
| 205 |
char cylon[6 + CYLON_BUFFER_EXTRA + 1]; |
| 206 |
unsigned cylon_pos; |
| 207 |
uint64_t timeout = 0; |
| 208 |
|
| 209 |
assert(m); |
| 210 |
assert(m->n_running_jobs > 0); |
| 211 |
|
| 212 |
manager_flip_auto_status(m, true, "delay"); |
| 213 |
|
| 214 |
print_nr = (m->jobs_in_progress_iteration / JOBS_IN_PROGRESS_PERIOD_DIVISOR) % m->n_running_jobs; |
| 215 |
|
| 216 |
HASHMAP_FOREACH(j, m->jobs) |
| 217 |
if (j->state == JOB_RUNNING && counter++ == print_nr) |
| 218 |
break; |
| 219 |
|
| 220 |
/* m->n_running_jobs must be consistent with the contents of m->jobs, |
| 221 |
* so the above loop must have succeeded in finding j. */ |
| 222 |
assert(counter == print_nr + 1); |
| 223 |
assert(j); |
| 224 |
|
| 225 |
cylon_pos = m->jobs_in_progress_iteration % 14; |
| 226 |
if (cylon_pos >= 8) |
| 227 |
cylon_pos = 14 - cylon_pos; |
| 228 |
draw_cylon(cylon, sizeof(cylon), 6, cylon_pos); |
| 229 |
|
| 230 |
m->jobs_in_progress_iteration++; |
| 231 |
|
| 232 |
char job_of_n[STRLEN("( of ) ") + DECIMAL_STR_MAX(unsigned)*2] = ""; |
| 233 |
if (m->n_running_jobs > 1) |
| 234 |
xsprintf(job_of_n, "(%u of %u) ", counter, m->n_running_jobs); |
| 235 |
|
| 236 |
(void) job_get_timeout(j, &timeout); |
| 237 |
|
| 238 |
/* We want to use enough information for the user to identify previous lines talking about the same |
| 239 |
* unit, but keep the message as short as possible. So if 'Starting foo.service' or 'Starting |
| 240 |
* foo.service - Description' were used, 'foo.service' is enough here. On the other hand, if we used |
| 241 |
* 'Starting Description' before, then we shall also use 'Description' here. So we pass NULL as the |
| 242 |
* second argument to unit_status_string(). */ |
| 243 |
const char *ident = unit_status_string(j->unit, NULL); |
| 244 |
|
| 245 |
const char *time = FORMAT_TIMESPAN(now(CLOCK_MONOTONIC) - j->begin_usec, 1*USEC_PER_SEC); |
| 246 |
const char *limit = timeout > 0 ? FORMAT_TIMESPAN(timeout - j->begin_usec, 1*USEC_PER_SEC) : "no limit"; |
| 247 |
|
| 248 |
if (m->status_unit_format == STATUS_UNIT_FORMAT_DESCRIPTION) |
| 249 |
/* When using 'Description', we effectively don't have enough space to show the nested status |
| 250 |
* without ellipsization, so let's not even try. */ |
| 251 |
manager_status_printf(m, STATUS_TYPE_EPHEMERAL, cylon, |
| 252 |
"%sA %s job is running for %s (%s / %s)", |
| 253 |
job_of_n, |
| 254 |
job_type_to_string(j->type), |
| 255 |
ident, |
| 256 |
time, limit); |
| 257 |
else { |
| 258 |
const char *status_text = unit_status_text(j->unit); |
| 259 |
|
| 260 |
manager_status_printf(m, STATUS_TYPE_EPHEMERAL, cylon, |
| 261 |
"%sJob %s/%s running (%s / %s)%s%s", |
| 262 |
job_of_n, |
| 263 |
ident, |
| 264 |
job_type_to_string(j->type), |
| 265 |
time, limit, |
| 266 |
status_text ? ": " : "", |
| 267 |
strempty(status_text)); |
| 268 |
} |
| 269 |
|
| 270 |
(void) sd_notifyf(/* unset_environment= */ false, |
| 271 |
"STATUS=%sUser job %s/%s running (%s / %s)...", |
| 272 |
job_of_n, |
| 273 |
ident, job_type_to_string(j->type), |
| 274 |
time, limit); |
| 275 |
m->status_ready = false; |
| 276 |
} |
| 277 |
|
| 278 |
static int have_ask_password(void) { |
| 279 |
_cleanup_closedir_ DIR *dir = NULL; |
| 280 |
|
| 281 |
dir = opendir("/run/systemd/ask-password"); |
| 282 |
if (!dir) { |
| 283 |
if (errno == ENOENT) |
| 284 |
return false; |
| 285 |
|
| 286 |
return -errno; |
| 287 |
} |
| 288 |
|
| 289 |
FOREACH_DIRENT_ALL(de, dir, return -errno) { |
| 290 |
if (!IN_SET(de->d_type, DT_REG, DT_UNKNOWN)) |
| 291 |
continue; |
| 292 |
|
| 293 |
if (startswith(de->d_name, "ask.")) |
| 294 |
return true; |
| 295 |
} |
| 296 |
|
| 297 |
return false; |
| 298 |
} |
| 299 |
|
| 300 |
static int manager_dispatch_ask_password_fd(sd_event_source *source, |
| 301 |
int fd, uint32_t revents, void *userdata) { |
| 302 |
Manager *m = ASSERT_PTR(userdata); |
| 303 |
|
| 304 |
(void) flush_fd(fd); |
| 305 |
|
| 306 |
m->have_ask_password = have_ask_password(); |
| 307 |
if (m->have_ask_password < 0) |
| 308 |
/* Log error but continue. Negative have_ask_password is treated as unknown status. */ |
| 309 |
log_warning_errno(m->have_ask_password, "Failed to list /run/systemd/ask-password/, ignoring: %m"); |
| 310 |
|
| 311 |
return 0; |
| 312 |
} |
| 313 |
|
| 314 |
static void manager_close_ask_password(Manager *m) { |
| 315 |
assert(m); |
| 316 |
|
| 317 |
m->ask_password_event_source = sd_event_source_disable_unref(m->ask_password_event_source); |
| 318 |
m->have_ask_password = -EINVAL; |
| 319 |
} |
| 320 |
|
| 321 |
static int manager_check_ask_password(Manager *m) { |
| 322 |
int r; |
| 323 |
|
| 324 |
assert(m); |
| 325 |
|
| 326 |
/* We only care about passwords prompts when running in system mode (because that's the only time we |
| 327 |
* manage a console) */ |
| 328 |
if (!MANAGER_IS_SYSTEM(m)) |
| 329 |
return 0; |
| 330 |
|
| 331 |
if (!m->ask_password_event_source) { |
| 332 |
_cleanup_close_ int inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); |
| 333 |
if (inotify_fd < 0) |
| 334 |
return log_error_errno(errno, "Failed to create inotify object: %m"); |
| 335 |
|
| 336 |
(void) mkdir_label("/run/systemd/ask-password", 0755); |
| 337 |
r = inotify_add_watch_and_warn(inotify_fd, "/run/systemd/ask-password", IN_CLOSE_WRITE|IN_DELETE|IN_MOVED_TO|IN_ONLYDIR); |
| 338 |
if (r < 0) |
| 339 |
return r; |
| 340 |
|
| 341 |
_cleanup_(sd_event_source_disable_unrefp) sd_event_source *event_source = NULL; |
| 342 |
r = sd_event_add_io( |
| 343 |
m->event, |
| 344 |
&event_source, |
| 345 |
inotify_fd, |
| 346 |
EPOLLIN, |
| 347 |
manager_dispatch_ask_password_fd, |
| 348 |
m); |
| 349 |
if (r < 0) |
| 350 |
return log_error_errno(r, "Failed to add event source for /run/systemd/ask-password/: %m"); |
| 351 |
|
| 352 |
r = sd_event_source_set_io_fd_own(event_source, true); |
| 353 |
if (r < 0) |
| 354 |
return log_error_errno(r, "Failed to pass ownership of /run/systemd/ask-password/ inotify fd to event source: %m"); |
| 355 |
TAKE_FD(inotify_fd); |
| 356 |
|
| 357 |
(void) sd_event_source_set_description(event_source, "manager-ask-password"); |
| 358 |
|
| 359 |
m->ask_password_event_source = TAKE_PTR(event_source); |
| 360 |
|
| 361 |
/* Queries might have been added meanwhile... */ |
| 362 |
(void) manager_dispatch_ask_password_fd(m->ask_password_event_source, sd_event_source_get_io_fd(m->ask_password_event_source), EPOLLIN, m); |
| 363 |
} |
| 364 |
|
| 365 |
return m->have_ask_password; |
| 366 |
} |
| 367 |
|
| 368 |
static int manager_watch_idle_pipe(Manager *m) { |
| 369 |
int r; |
| 370 |
|
| 371 |
assert(m); |
| 372 |
|
| 373 |
if (m->idle_pipe_event_source) |
| 374 |
return 0; |
| 375 |
|
| 376 |
if (m->idle_pipe[2] < 0) |
| 377 |
return 0; |
| 378 |
|
| 379 |
r = sd_event_add_io(m->event, &m->idle_pipe_event_source, m->idle_pipe[2], EPOLLIN, manager_dispatch_idle_pipe_fd, m); |
| 380 |
if (r < 0) |
| 381 |
return log_error_errno(r, "Failed to watch idle pipe: %m"); |
| 382 |
|
| 383 |
(void) sd_event_source_set_description(m->idle_pipe_event_source, "manager-idle-pipe"); |
| 384 |
|
| 385 |
return 0; |
| 386 |
} |
| 387 |
|
| 388 |
static void manager_close_idle_pipe(Manager *m) { |
| 389 |
assert(m); |
| 390 |
|
| 391 |
m->idle_pipe_event_source = sd_event_source_disable_unref(m->idle_pipe_event_source); |
| 392 |
|
| 393 |
safe_close_pair(m->idle_pipe); |
| 394 |
safe_close_pair(m->idle_pipe + 2); |
| 395 |
} |
| 396 |
|
| 397 |
static int manager_setup_time_change(Manager *m) { |
| 398 |
int r; |
| 399 |
|
| 400 |
assert(m); |
| 401 |
|
| 402 |
if (MANAGER_IS_TEST_RUN(m)) |
| 403 |
return 0; |
| 404 |
|
| 405 |
m->time_change_event_source = sd_event_source_disable_unref(m->time_change_event_source); |
| 406 |
|
| 407 |
r = event_add_time_change(m->event, &m->time_change_event_source, manager_dispatch_time_change_fd, m); |
| 408 |
if (r < 0) |
| 409 |
return log_error_errno(r, "Failed to create time change event source: %m"); |
| 410 |
|
| 411 |
/* Schedule this slightly earlier than the .timer event sources */ |
| 412 |
r = sd_event_source_set_priority(m->time_change_event_source, EVENT_PRIORITY_TIME_CHANGE); |
| 413 |
if (r < 0) |
| 414 |
return log_error_errno(r, "Failed to set priority of time change event sources: %m"); |
| 415 |
|
| 416 |
log_debug("Set up TFD_TIMER_CANCEL_ON_SET timerfd."); |
| 417 |
|
| 418 |
return 0; |
| 419 |
} |
| 420 |
|
| 421 |
static int manager_read_timezone_stat(Manager *m) { |
| 422 |
struct stat st; |
| 423 |
bool changed; |
| 424 |
|
| 425 |
assert(m); |
| 426 |
|
| 427 |
/* Read the current stat() data of /etc/localtime so that we detect changes */ |
| 428 |
if (lstat(etc_localtime(), &st) < 0) { |
| 429 |
log_debug_errno(errno, "Failed to stat /etc/localtime, ignoring: %m"); |
| 430 |
changed = m->etc_localtime_accessible; |
| 431 |
m->etc_localtime_accessible = false; |
| 432 |
} else { |
| 433 |
usec_t k; |
| 434 |
|
| 435 |
k = timespec_load(&st.st_mtim); |
| 436 |
changed = !m->etc_localtime_accessible || k != m->etc_localtime_mtime; |
| 437 |
|
| 438 |
m->etc_localtime_mtime = k; |
| 439 |
m->etc_localtime_accessible = true; |
| 440 |
} |
| 441 |
|
| 442 |
return changed; |
| 443 |
} |
| 444 |
|
| 445 |
static int manager_setup_timezone_change(Manager *m) { |
| 446 |
_cleanup_(sd_event_source_unrefp) sd_event_source *new_event = NULL; |
| 447 |
int r; |
| 448 |
|
| 449 |
assert(m); |
| 450 |
|
| 451 |
if (MANAGER_IS_TEST_RUN(m)) |
| 452 |
return 0; |
| 453 |
|
| 454 |
/* We watch /etc/localtime for three events: change of the link count (which might mean removal from /etc even |
| 455 |
* though another link might be kept), renames, and file close operations after writing. Note we don't bother |
| 456 |
* with IN_DELETE_SELF, as that would just report when the inode is removed entirely, i.e. after the link count |
| 457 |
* went to zero and all fds to it are closed. |
| 458 |
* |
| 459 |
* Note that we never follow symlinks here. This is a simplification, but should cover almost all cases |
| 460 |
* correctly. |
| 461 |
* |
| 462 |
* Note that we create the new event source first here, before releasing the old one. This should optimize |
| 463 |
* behaviour as this way sd-event can reuse the old watch in case the inode didn't change. */ |
| 464 |
|
| 465 |
r = sd_event_add_inotify(m->event, &new_event, etc_localtime(), |
| 466 |
IN_ATTRIB|IN_MOVE_SELF|IN_CLOSE_WRITE|IN_DONT_FOLLOW, manager_dispatch_timezone_change, m); |
| 467 |
if (r == -ENOENT) { |
| 468 |
/* If the file doesn't exist yet, subscribe to /etc instead, and wait until it is created either by |
| 469 |
* O_CREATE or by rename() */ |
| 470 |
_cleanup_free_ char *localtime_dir = NULL; |
| 471 |
|
| 472 |
int dir_r = path_extract_directory(etc_localtime(), &localtime_dir); |
| 473 |
if (dir_r < 0) |
| 474 |
return log_error_errno(dir_r, "Failed to extract directory from path '%s': %m", etc_localtime()); |
| 475 |
|
| 476 |
log_debug_errno(r, "%s doesn't exist yet, watching %s instead.", etc_localtime(), localtime_dir); |
| 477 |
|
| 478 |
r = sd_event_add_inotify(m->event, &new_event, localtime_dir, |
| 479 |
IN_CREATE|IN_MOVED_TO|IN_ONLYDIR, manager_dispatch_timezone_change, m); |
| 480 |
} |
| 481 |
if (r < 0) |
| 482 |
return log_error_errno(r, "Failed to create timezone change event source: %m"); |
| 483 |
|
| 484 |
/* Schedule this slightly earlier than the .timer event sources */ |
| 485 |
r = sd_event_source_set_priority(new_event, EVENT_PRIORITY_TIME_ZONE); |
| 486 |
if (r < 0) |
| 487 |
return log_error_errno(r, "Failed to set priority of timezone change event sources: %m"); |
| 488 |
|
| 489 |
sd_event_source_unref(m->timezone_change_event_source); |
| 490 |
m->timezone_change_event_source = TAKE_PTR(new_event); |
| 491 |
|
| 492 |
return 0; |
| 493 |
} |
| 494 |
|
| 495 |
static int manager_enable_special_signals(Manager *m) { |
| 496 |
_cleanup_close_ int fd = -EBADF; |
| 497 |
|
| 498 |
assert(m); |
| 499 |
|
| 500 |
if (!MANAGER_IS_SYSTEM(m) || MANAGER_IS_TEST_RUN(m)) |
| 501 |
return 0; |
| 502 |
|
| 503 |
/* Enable that we get SIGINT on control-alt-del. In containers this will fail with EPERM (older) or |
| 504 |
* EINVAL (newer), so ignore that. */ |
| 505 |
if (reboot(RB_DISABLE_CAD) < 0 && !IN_SET(errno, EPERM, EINVAL)) |
| 506 |
log_warning_errno(errno, "Failed to enable ctrl-alt-del handling, ignoring: %m"); |
| 507 |
|
| 508 |
fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC); |
| 509 |
if (fd < 0) |
| 510 |
/* Support systems without virtual console (ENOENT) gracefully */ |
| 511 |
log_full_errno(fd == -ENOENT ? LOG_DEBUG : LOG_WARNING, fd, "Failed to open %s, ignoring: %m", "/dev/tty0"); |
| 512 |
else { |
| 513 |
/* Enable that we get SIGWINCH on kbrequest */ |
| 514 |
if (ioctl(fd, KDSIGACCEPT, SIGWINCH) < 0) |
| 515 |
log_warning_errno(errno, "Failed to enable kbrequest handling, ignoring: %m"); |
| 516 |
} |
| 517 |
|
| 518 |
return 0; |
| 519 |
} |
| 520 |
|
| 521 |
static int manager_setup_signals(Manager *m) { |
| 522 |
static const struct sigaction sa = { |
| 523 |
.sa_handler = SIG_DFL, |
| 524 |
.sa_flags = SA_NOCLDSTOP|SA_RESTART, |
| 525 |
}; |
| 526 |
sigset_t mask; |
| 527 |
int r; |
| 528 |
|
| 529 |
assert(m); |
| 530 |
|
| 531 |
assert_se(sigaction(SIGCHLD, &sa, NULL) == 0); |
| 532 |
|
| 533 |
/* We make liberal use of realtime signals here. On Linux we have 29 of them, between |
| 534 |
* SIGRTMIN+0 ... SIGRTMIN+29. The glibc has one more (SIGRTMAX is SIGRTMIN+30), |
| 535 |
* but musl does not (SIGRTMAX is SIGRTMIN+29). */ |
| 536 |
|
| 537 |
assert_se(sigemptyset(&mask) == 0); |
| 538 |
sigset_add_many(&mask, |
| 539 |
SIGCHLD, /* Child died */ |
| 540 |
SIGTERM, /* Reexecute daemon */ |
| 541 |
SIGHUP, /* Reload configuration */ |
| 542 |
SIGUSR1, /* systemd: reconnect to D-Bus */ |
| 543 |
SIGUSR2, /* systemd: dump status */ |
| 544 |
SIGINT, /* Kernel sends us this on control-alt-del */ |
| 545 |
SIGWINCH, /* Kernel sends us this on kbrequest (alt-arrowup) */ |
| 546 |
SIGPWR, /* Some kernel drivers and upsd send us this on power failure */ |
| 547 |
|
| 548 |
SIGRTMIN+0, /* systemd: start default.target */ |
| 549 |
SIGRTMIN+1, /* systemd: isolate rescue.target */ |
| 550 |
SIGRTMIN+2, /* systemd: isolate emergency.target */ |
| 551 |
SIGRTMIN+3, /* systemd: start halt.target */ |
| 552 |
SIGRTMIN+4, /* systemd: start poweroff.target */ |
| 553 |
SIGRTMIN+5, /* systemd: start reboot.target */ |
| 554 |
SIGRTMIN+6, /* systemd: start kexec.target */ |
| 555 |
SIGRTMIN+7, /* systemd: start soft-reboot.target */ |
| 556 |
|
| 557 |
/* ... space for more special targets ... */ |
| 558 |
|
| 559 |
SIGRTMIN+13, /* systemd: Immediate halt */ |
| 560 |
SIGRTMIN+14, /* systemd: Immediate poweroff */ |
| 561 |
SIGRTMIN+15, /* systemd: Immediate reboot */ |
| 562 |
SIGRTMIN+16, /* systemd: Immediate kexec */ |
| 563 |
SIGRTMIN+17, /* systemd: Immediate soft-reboot */ |
| 564 |
SIGRTMIN+18, /* systemd: control command */ |
| 565 |
|
| 566 |
/* ... space ... */ |
| 567 |
|
| 568 |
SIGRTMIN+20, /* systemd: enable status messages */ |
| 569 |
SIGRTMIN+21, /* systemd: disable status messages */ |
| 570 |
SIGRTMIN+22, /* systemd: set log level to LOG_DEBUG */ |
| 571 |
SIGRTMIN+23, /* systemd: set log level to LOG_INFO */ |
| 572 |
SIGRTMIN+24, /* systemd: Immediate exit (--user only) */ |
| 573 |
SIGRTMIN+25, /* systemd: reexecute manager */ |
| 574 |
|
| 575 |
SIGRTMIN+26, /* systemd: set log target to journal-or-kmsg */ |
| 576 |
SIGRTMIN+27, /* systemd: set log target to console */ |
| 577 |
SIGRTMIN+28, /* systemd: set log target to kmsg */ |
| 578 |
SIGRTMIN+29, /* systemd: set log target to syslog-or-kmsg (obsolete) */ |
| 579 |
|
| 580 |
/* ... one free signal here SIGRTMIN+30 (glibc only) ... */ |
| 581 |
-1); |
| 582 |
assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0); |
| 583 |
|
| 584 |
m->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); |
| 585 |
if (m->signal_fd < 0) |
| 586 |
return -errno; |
| 587 |
|
| 588 |
r = sd_event_add_io(m->event, &m->signal_event_source, m->signal_fd, EPOLLIN, manager_dispatch_signal_fd, m); |
| 589 |
if (r < 0) |
| 590 |
return r; |
| 591 |
|
| 592 |
(void) sd_event_source_set_description(m->signal_event_source, "manager-signal"); |
| 593 |
|
| 594 |
/* Process signals a bit earlier than the rest of things, but later than notify_fd processing, so that the |
| 595 |
* notify processing can still figure out to which process/service a message belongs, before we reap the |
| 596 |
* process. Also, process this before handling cgroup notifications, so that we always collect child exit |
| 597 |
* status information before detecting that there's no process in a cgroup. */ |
| 598 |
r = sd_event_source_set_priority(m->signal_event_source, EVENT_PRIORITY_SIGNALS); |
| 599 |
if (r < 0) |
| 600 |
return r; |
| 601 |
|
| 602 |
/* Report to supervisor that we now process the above signals. We report this as level "2", to |
| 603 |
* indicate that we support more than sysvinit's signals (of course, sysvinit never sent this |
| 604 |
* message, but conceptually it makes sense to consider level "1" to be equivalent to sysvinit's |
| 605 |
* signal handling). Also, by setting this to "2" people looking for this hopefully won't |
| 606 |
* misunderstand this as a boolean concept. Signal level 2 shall refer to the signals PID 1 |
| 607 |
* understands at the time of release of systemd v256, i.e. including basic SIGRTMIN+18 handling for |
| 608 |
* memory pressure and stuff. When more signals are hooked up (or more SIGRTMIN+18 multiplex |
| 609 |
* operations added, this level should be increased). */ |
| 610 |
(void) sd_notify(/* unset_environment= */ false, |
| 611 |
"X_SYSTEMD_SIGNALS_LEVEL=2"); |
| 612 |
|
| 613 |
return manager_enable_special_signals(m); |
| 614 |
} |
| 615 |
|
| 616 |
static char** sanitize_environment(char **l) { |
| 617 |
|
| 618 |
/* Let's remove some environment variables that we need ourselves to communicate with our clients */ |
| 619 |
strv_env_unset_many( |
| 620 |
l, |
| 621 |
"CACHE_DIRECTORY", |
| 622 |
"CONFIGURATION_DIRECTORY", |
| 623 |
"CPU_PRESSURE_WATCH", |
| 624 |
"CPU_PRESSURE_WRITE", |
| 625 |
"CREDENTIALS_DIRECTORY", |
| 626 |
"EXIT_CODE", |
| 627 |
"EXIT_STATUS", |
| 628 |
"IO_PRESSURE_WATCH", |
| 629 |
"IO_PRESSURE_WRITE", |
| 630 |
"INVOCATION_ID", |
| 631 |
"JOURNAL_STREAM", |
| 632 |
"LISTEN_FDNAMES", |
| 633 |
"LISTEN_FDS", |
| 634 |
"LISTEN_PID", |
| 635 |
"LISTEN_PIDFDID", |
| 636 |
"LOGS_DIRECTORY", |
| 637 |
"LOG_NAMESPACE", |
| 638 |
"MAINPID", |
| 639 |
"MANAGERPID", |
| 640 |
"MEMORY_PRESSURE_WATCH", |
| 641 |
"MEMORY_PRESSURE_WRITE", |
| 642 |
"MONITOR_EXIT_CODE", |
| 643 |
"MONITOR_EXIT_STATUS", |
| 644 |
"MONITOR_INVOCATION_ID", |
| 645 |
"MONITOR_SERVICE_RESULT", |
| 646 |
"MONITOR_UNIT", |
| 647 |
"NOTIFY_SOCKET", |
| 648 |
"PIDFILE", |
| 649 |
"REMOTE_ADDR", |
| 650 |
"REMOTE_PORT", |
| 651 |
"RUNTIME_DIRECTORY", |
| 652 |
"SERVICE_RESULT", |
| 653 |
"STATE_DIRECTORY", |
| 654 |
"SYSTEMD_EXEC_PID", |
| 655 |
"TRIGGER_PATH", |
| 656 |
"TRIGGER_TIMER_MONOTONIC_USEC", |
| 657 |
"TRIGGER_TIMER_REALTIME_USEC", |
| 658 |
"TRIGGER_UNIT", |
| 659 |
"WATCHDOG_PID", |
| 660 |
"WATCHDOG_USEC"); |
| 661 |
|
| 662 |
/* Let's order the environment alphabetically, just to make it pretty */ |
| 663 |
return strv_sort(l); |
| 664 |
} |
| 665 |
|
| 666 |
int manager_default_environment(Manager *m) { |
| 667 |
assert(m); |
| 668 |
|
| 669 |
m->transient_environment = strv_free(m->transient_environment); |
| 670 |
|
| 671 |
if (MANAGER_IS_SYSTEM(m)) { |
| 672 |
/* The system manager always starts with a clean environment for its children. It does not |
| 673 |
* import the kernel's or the parents' exported variables. |
| 674 |
* |
| 675 |
* The initial passed environment is untouched to keep /proc/self/environ valid; it is used |
| 676 |
* for tagging the init process inside containers. */ |
| 677 |
char *path = strjoin("PATH=", default_PATH()); |
| 678 |
if (!path) |
| 679 |
return log_oom(); |
| 680 |
|
| 681 |
if (strv_consume(&m->transient_environment, path) < 0) |
| 682 |
return log_oom(); |
| 683 |
|
| 684 |
/* Import locale variables LC_*= from configuration */ |
| 685 |
(void) locale_setup(&m->transient_environment); |
| 686 |
} else { |
| 687 |
/* The user manager passes its own environment along to its children, except for $PATH and |
| 688 |
* session envs. */ |
| 689 |
|
| 690 |
m->transient_environment = strv_copy(environ); |
| 691 |
if (!m->transient_environment) |
| 692 |
return log_oom(); |
| 693 |
|
| 694 |
char *path = strjoin("PATH=", default_user_PATH()); |
| 695 |
if (!path) |
| 696 |
return log_oom(); |
| 697 |
|
| 698 |
if (strv_env_replace_consume(&m->transient_environment, path) < 0) |
| 699 |
return log_oom(); |
| 700 |
|
| 701 |
/* Envvars set for our 'manager' class session are private and should not be propagated |
| 702 |
* to children. Also it's likely that the graphical session will set these on their own. */ |
| 703 |
strv_env_unset_many(m->transient_environment, |
| 704 |
"XDG_SESSION_ID", |
| 705 |
"XDG_SESSION_CLASS", |
| 706 |
"XDG_SESSION_TYPE", |
| 707 |
"XDG_SESSION_DESKTOP", |
| 708 |
"XDG_SESSION_EXTRA_DEVICE_ACCESS", |
| 709 |
"XDG_SEAT", |
| 710 |
"XDG_VTNR"); |
| 711 |
} |
| 712 |
|
| 713 |
sanitize_environment(m->transient_environment); |
| 714 |
return 0; |
| 715 |
} |
| 716 |
|
| 717 |
static int manager_setup_prefix(Manager *m) { |
| 718 |
struct table_entry { |
| 719 |
uint64_t type; |
| 720 |
const char *suffix; |
| 721 |
}; |
| 722 |
|
| 723 |
static const struct table_entry paths_system[_EXEC_DIRECTORY_TYPE_MAX] = { |
| 724 |
[EXEC_DIRECTORY_RUNTIME] = { SD_PATH_SYSTEM_RUNTIME, NULL }, |
| 725 |
[EXEC_DIRECTORY_STATE] = { SD_PATH_SYSTEM_STATE_PRIVATE, NULL }, |
| 726 |
[EXEC_DIRECTORY_CACHE] = { SD_PATH_SYSTEM_STATE_CACHE, NULL }, |
| 727 |
[EXEC_DIRECTORY_LOGS] = { SD_PATH_SYSTEM_STATE_LOGS, NULL }, |
| 728 |
[EXEC_DIRECTORY_CONFIGURATION] = { SD_PATH_SYSTEM_CONFIGURATION, NULL }, |
| 729 |
}; |
| 730 |
|
| 731 |
static const struct table_entry paths_user[_EXEC_DIRECTORY_TYPE_MAX] = { |
| 732 |
[EXEC_DIRECTORY_RUNTIME] = { SD_PATH_USER_RUNTIME, NULL }, |
| 733 |
[EXEC_DIRECTORY_STATE] = { SD_PATH_USER_STATE_PRIVATE, NULL }, |
| 734 |
[EXEC_DIRECTORY_CACHE] = { SD_PATH_USER_STATE_CACHE, NULL }, |
| 735 |
[EXEC_DIRECTORY_LOGS] = { SD_PATH_USER_STATE_PRIVATE, "log" }, |
| 736 |
[EXEC_DIRECTORY_CONFIGURATION] = { SD_PATH_USER_CONFIGURATION, NULL }, |
| 737 |
}; |
| 738 |
|
| 739 |
assert(m); |
| 740 |
|
| 741 |
const struct table_entry *p = MANAGER_IS_SYSTEM(m) ? paths_system : paths_user; |
| 742 |
int r; |
| 743 |
|
| 744 |
for (ExecDirectoryType i = 0; i < _EXEC_DIRECTORY_TYPE_MAX; i++) { |
| 745 |
r = sd_path_lookup(p[i].type, p[i].suffix, &m->prefix[i]); |
| 746 |
if (r < 0) |
| 747 |
return log_warning_errno(r, "Failed to lookup %s path: %m", |
| 748 |
exec_directory_type_to_string(i)); |
| 749 |
} |
| 750 |
|
| 751 |
return 0; |
| 752 |
} |
| 753 |
|
| 754 |
static void manager_free_unit_name_maps(Manager *m) { |
| 755 |
m->unit_id_map = hashmap_free(m->unit_id_map); |
| 756 |
m->unit_name_map = hashmap_free(m->unit_name_map); |
| 757 |
m->unit_path_cache = set_free(m->unit_path_cache); |
| 758 |
m->unit_cache_timestamp_hash = 0; |
| 759 |
} |
| 760 |
|
| 761 |
static int manager_setup_run_queue(Manager *m) { |
| 762 |
int r; |
| 763 |
|
| 764 |
assert(m); |
| 765 |
assert(!m->run_queue_event_source); |
| 766 |
|
| 767 |
r = sd_event_add_defer(m->event, &m->run_queue_event_source, manager_dispatch_run_queue, m); |
| 768 |
if (r < 0) |
| 769 |
return r; |
| 770 |
|
| 771 |
r = sd_event_source_set_priority(m->run_queue_event_source, EVENT_PRIORITY_RUN_QUEUE); |
| 772 |
if (r < 0) |
| 773 |
return r; |
| 774 |
|
| 775 |
r = sd_event_source_set_enabled(m->run_queue_event_source, SD_EVENT_OFF); |
| 776 |
if (r < 0) |
| 777 |
return r; |
| 778 |
|
| 779 |
(void) sd_event_source_set_description(m->run_queue_event_source, "manager-run-queue"); |
| 780 |
|
| 781 |
return 0; |
| 782 |
} |
| 783 |
|
| 784 |
static int manager_setup_sigchld_event_source(Manager *m) { |
| 785 |
int r; |
| 786 |
|
| 787 |
assert(m); |
| 788 |
assert(!m->sigchld_event_source); |
| 789 |
|
| 790 |
r = sd_event_add_defer(m->event, &m->sigchld_event_source, manager_dispatch_sigchld, m); |
| 791 |
if (r < 0) |
| 792 |
return r; |
| 793 |
|
| 794 |
r = sd_event_source_set_priority(m->sigchld_event_source, EVENT_PRIORITY_SIGCHLD); |
| 795 |
if (r < 0) |
| 796 |
return r; |
| 797 |
|
| 798 |
r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_OFF); |
| 799 |
if (r < 0) |
| 800 |
return r; |
| 801 |
|
| 802 |
(void) sd_event_source_set_description(m->sigchld_event_source, "manager-sigchld"); |
| 803 |
|
| 804 |
return 0; |
| 805 |
} |
| 806 |
|
| 807 |
typedef int (*pressure_add_t)(sd_event *, sd_event_source **, sd_event_handler_t, void *); |
| 808 |
typedef int (*pressure_set_period_t)(sd_event_source *, usec_t, usec_t); |
| 809 |
|
| 810 |
static const struct { |
| 811 |
pressure_add_t add; |
| 812 |
pressure_set_period_t set_period; |
| 813 |
} pressure_dispatch_table[_PRESSURE_RESOURCE_MAX] = { |
| 814 |
[PRESSURE_MEMORY] = { sd_event_add_memory_pressure, sd_event_source_set_memory_pressure_period }, |
| 815 |
[PRESSURE_CPU] = { sd_event_add_cpu_pressure, sd_event_source_set_cpu_pressure_period }, |
| 816 |
[PRESSURE_IO] = { sd_event_add_io_pressure, sd_event_source_set_io_pressure_period }, |
| 817 |
}; |
| 818 |
|
| 819 |
int manager_setup_pressure_event_source(Manager *m, PressureResource t) { |
| 820 |
int r; |
| 821 |
|
| 822 |
assert(m); |
| 823 |
assert(t >= 0 && t < _PRESSURE_RESOURCE_MAX); |
| 824 |
|
| 825 |
m->pressure_event_source[t] = sd_event_source_disable_unref(m->pressure_event_source[t]); |
| 826 |
|
| 827 |
r = pressure_dispatch_table[t].add(m->event, &m->pressure_event_source[t], NULL, NULL); |
| 828 |
if (r < 0) |
| 829 |
log_full_errno(ERRNO_IS_NOT_SUPPORTED(r) || ERRNO_IS_PRIVILEGE(r) || (r == -EHOSTDOWN) ? LOG_DEBUG : LOG_NOTICE, r, |
| 830 |
"Failed to establish %s pressure event source, ignoring: %m", pressure_resource_to_string(t)); |
| 831 |
else if (m->defaults.pressure[t].threshold_usec != USEC_INFINITY) { |
| 832 |
|
| 833 |
r = pressure_dispatch_table[t].set_period( |
| 834 |
m->pressure_event_source[t], |
| 835 |
m->defaults.pressure[t].threshold_usec, |
| 836 |
PRESSURE_DEFAULT_WINDOW_USEC); |
| 837 |
if (r < 0) |
| 838 |
log_warning_errno(r, "Failed to adjust %s pressure threshold, ignoring: %m", pressure_resource_to_string(t)); |
| 839 |
} |
| 840 |
|
| 841 |
return 0; |
| 842 |
} |
| 843 |
|
| 844 |
static int manager_find_credentials_dirs(Manager *m) { |
| 845 |
const char *e; |
| 846 |
int r; |
| 847 |
|
| 848 |
assert(m); |
| 849 |
|
| 850 |
r = get_credentials_dir(&e); |
| 851 |
if (r < 0) { |
| 852 |
if (r != -ENXIO) |
| 853 |
log_debug_errno(r, "Failed to determine credentials directory, ignoring: %m"); |
| 854 |
} else { |
| 855 |
m->received_credentials_directory = strdup(e); |
| 856 |
if (!m->received_credentials_directory) |
| 857 |
return -ENOMEM; |
| 858 |
} |
| 859 |
|
| 860 |
r = get_encrypted_credentials_dir(&e); |
| 861 |
if (r < 0) { |
| 862 |
if (r != -ENXIO) |
| 863 |
log_debug_errno(r, "Failed to determine encrypted credentials directory, ignoring: %m"); |
| 864 |
} else { |
| 865 |
m->received_encrypted_credentials_directory = strdup(e); |
| 866 |
if (!m->received_encrypted_credentials_directory) |
| 867 |
return -ENOMEM; |
| 868 |
} |
| 869 |
|
| 870 |
return 0; |
| 871 |
} |
| 872 |
|
| 873 |
void manager_set_switching_root(Manager *m, bool switching_root) { |
| 874 |
assert(m); |
| 875 |
|
| 876 |
m->switching_root = MANAGER_IS_SYSTEM(m) && switching_root; |
| 877 |
} |
| 878 |
|
| 879 |
double manager_get_progress(Manager *m) { |
| 880 |
assert(m); |
| 881 |
|
| 882 |
if (MANAGER_IS_FINISHED(m) || m->n_installed_jobs == 0) |
| 883 |
return 1.0; |
| 884 |
|
| 885 |
return 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs); |
| 886 |
} |
| 887 |
|
| 888 |
static int compare_job_priority(const void *a, const void *b) { |
| 889 |
const Job *x = a, *y = b; |
| 890 |
|
| 891 |
return unit_compare_priority(x->unit, y->unit); |
| 892 |
} |
| 893 |
|
| 894 |
usec_t manager_default_timeout(RuntimeScope scope) { |
| 895 |
return scope == RUNTIME_SCOPE_SYSTEM ? DEFAULT_TIMEOUT_USEC : DEFAULT_USER_TIMEOUT_USEC; |
| 896 |
} |
| 897 |
|
| 898 |
int manager_new(RuntimeScope runtime_scope, ManagerTestRunFlags test_run_flags, Manager **ret) { |
| 899 |
_cleanup_(manager_freep) Manager *m = NULL; |
| 900 |
int r; |
| 901 |
|
| 902 |
assert(IN_SET(runtime_scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER)); |
| 903 |
assert(ret); |
| 904 |
|
| 905 |
m = new(Manager, 1); |
| 906 |
if (!m) |
| 907 |
return -ENOMEM; |
| 908 |
|
| 909 |
*m = (Manager) { |
| 910 |
.runtime_scope = runtime_scope, |
| 911 |
.objective = _MANAGER_OBJECTIVE_INVALID, |
| 912 |
.previous_objective = _MANAGER_OBJECTIVE_INVALID, |
| 913 |
|
| 914 |
.status_unit_format = STATUS_UNIT_FORMAT_DEFAULT, |
| 915 |
|
| 916 |
.original_log_level = -1, |
| 917 |
.original_log_target = _LOG_TARGET_INVALID, |
| 918 |
|
| 919 |
.watchdog_overridden[WATCHDOG_RUNTIME] = USEC_INFINITY, |
| 920 |
.watchdog_overridden[WATCHDOG_REBOOT] = USEC_INFINITY, |
| 921 |
.watchdog_overridden[WATCHDOG_KEXEC] = USEC_INFINITY, |
| 922 |
.watchdog_overridden[WATCHDOG_PRETIMEOUT] = USEC_INFINITY, |
| 923 |
|
| 924 |
.show_status_overridden = _SHOW_STATUS_INVALID, |
| 925 |
|
| 926 |
.notify_fd = -EBADF, |
| 927 |
.signal_fd = -EBADF, |
| 928 |
.user_lookup_fds = EBADF_PAIR, |
| 929 |
.handoff_timestamp_fds = EBADF_PAIR, |
| 930 |
.pidref_transport_fds = EBADF_PAIR, |
| 931 |
.private_listen_fd = -EBADF, |
| 932 |
.dev_autofs_fd = -EBADF, |
| 933 |
.cgroup_inotify_fd = -EBADF, |
| 934 |
.pin_cgroupfs_fd = -EBADF, |
| 935 |
.idle_pipe = { -EBADF, -EBADF, -EBADF, -EBADF}, |
| 936 |
|
| 937 |
/* start as id #1, so that we can leave #0 around as "null-like" value */ |
| 938 |
.current_job_id = 1, |
| 939 |
|
| 940 |
.have_ask_password = -EINVAL, /* we don't know */ |
| 941 |
.first_boot = -1, |
| 942 |
.test_run_flags = test_run_flags, |
| 943 |
|
| 944 |
.dump_ratelimit = (const RateLimit) { .interval = 10 * USEC_PER_MINUTE, .burst = 10 }, |
| 945 |
|
| 946 |
.executor_fd = -EBADF, |
| 947 |
|
| 948 |
.restrict_fsaccess_bss_map_fd = -EBADF, |
| 949 |
}; |
| 950 |
|
| 951 |
FOREACH_ELEMENT(fd, m->restrict_fsaccess_link_fds) |
| 952 |
*fd = -EBADF; |
| 953 |
|
| 954 |
unit_defaults_init(&m->defaults, runtime_scope); |
| 955 |
|
| 956 |
#if ENABLE_EFI |
| 957 |
if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0) |
| 958 |
boot_timestamps(m->timestamps + MANAGER_TIMESTAMP_USERSPACE, |
| 959 |
m->timestamps + MANAGER_TIMESTAMP_FIRMWARE, |
| 960 |
m->timestamps + MANAGER_TIMESTAMP_LOADER); |
| 961 |
#endif |
| 962 |
|
| 963 |
/* Reboot immediately if the user hits C-A-D more often than 7x per 2s */ |
| 964 |
m->ctrl_alt_del_ratelimit = (const RateLimit) { .interval = 2 * USEC_PER_SEC, .burst = 7 }; |
| 965 |
|
| 966 |
r = manager_default_environment(m); |
| 967 |
if (r < 0) |
| 968 |
return r; |
| 969 |
|
| 970 |
r = hashmap_ensure_allocated(&m->units, &string_hash_ops); |
| 971 |
if (r < 0) |
| 972 |
return r; |
| 973 |
|
| 974 |
r = hashmap_ensure_allocated(&m->cgroup_unit, &path_hash_ops); |
| 975 |
if (r < 0) |
| 976 |
return r; |
| 977 |
|
| 978 |
r = hashmap_ensure_allocated(&m->watch_bus, &string_hash_ops); |
| 979 |
if (r < 0) |
| 980 |
return r; |
| 981 |
|
| 982 |
r = prioq_ensure_allocated(&m->run_queue, compare_job_priority); |
| 983 |
if (r < 0) |
| 984 |
return r; |
| 985 |
|
| 986 |
r = manager_setup_prefix(m); |
| 987 |
if (r < 0) |
| 988 |
return r; |
| 989 |
|
| 990 |
r = manager_find_credentials_dirs(m); |
| 991 |
if (r < 0) |
| 992 |
return r; |
| 993 |
|
| 994 |
r = sd_event_default(&m->event); |
| 995 |
if (r < 0) |
| 996 |
return r; |
| 997 |
|
| 998 |
r = manager_setup_run_queue(m); |
| 999 |
if (r < 0) |
| 1000 |
return r; |
| 1001 |
|
| 1002 |
if (FLAGS_SET(test_run_flags, MANAGER_TEST_RUN_MINIMAL)) { |
| 1003 |
m->cgroup_root = strdup(""); |
| 1004 |
if (!m->cgroup_root) |
| 1005 |
return -ENOMEM; |
| 1006 |
} else { |
| 1007 |
r = manager_setup_signals(m); |
| 1008 |
if (r < 0) |
| 1009 |
return r; |
| 1010 |
|
| 1011 |
r = manager_setup_cgroup(m); |
| 1012 |
if (r < 0) |
| 1013 |
return r; |
| 1014 |
|
| 1015 |
r = manager_setup_time_change(m); |
| 1016 |
if (r < 0) |
| 1017 |
return r; |
| 1018 |
|
| 1019 |
r = manager_read_timezone_stat(m); |
| 1020 |
if (r < 0) |
| 1021 |
return r; |
| 1022 |
|
| 1023 |
(void) manager_setup_timezone_change(m); |
| 1024 |
|
| 1025 |
r = manager_setup_sigchld_event_source(m); |
| 1026 |
if (r < 0) |
| 1027 |
return r; |
| 1028 |
|
| 1029 |
for (PressureResource t = 0; t < _PRESSURE_RESOURCE_MAX; t++) { |
| 1030 |
r = manager_setup_pressure_event_source(m, t); |
| 1031 |
if (r < 0) |
| 1032 |
return r; |
| 1033 |
} |
| 1034 |
|
| 1035 |
#if HAVE_LIBBPF |
| 1036 |
if (MANAGER_IS_SYSTEM(m) && bpf_restrict_fs_supported(/* initialize= */ true)) { |
| 1037 |
r = bpf_restrict_fs_setup(m); |
| 1038 |
if (r < 0) |
| 1039 |
log_warning_errno(r, "Failed to setup LSM BPF, ignoring: %m"); |
| 1040 |
} |
| 1041 |
#endif |
| 1042 |
} |
| 1043 |
|
| 1044 |
if (test_run_flags == 0) { |
| 1045 |
if (MANAGER_IS_SYSTEM(m)) |
| 1046 |
r = mkdir_label("/run/systemd/units", 0755); |
| 1047 |
else { |
| 1048 |
_cleanup_free_ char *units_path = NULL; |
| 1049 |
r = xdg_user_runtime_dir("/systemd/units", &units_path); |
| 1050 |
if (r < 0) |
| 1051 |
return r; |
| 1052 |
|
| 1053 |
r = mkdir_label(units_path, 0755); |
| 1054 |
} |
| 1055 |
if (r < 0 && r != -EEXIST) |
| 1056 |
return r; |
| 1057 |
} |
| 1058 |
|
| 1059 |
if (!FLAGS_SET(test_run_flags, MANAGER_TEST_DONT_OPEN_EXECUTOR)) { |
| 1060 |
m->executor_fd = pin_callout_binary(SYSTEMD_EXECUTOR_BINARY_PATH, &m->executor_path); |
| 1061 |
if (m->executor_fd < 0) |
| 1062 |
return log_debug_errno(m->executor_fd, "Failed to pin executor binary: %m"); |
| 1063 |
|
| 1064 |
log_debug("Using systemd-executor binary from '%s'.", m->executor_path); |
| 1065 |
} |
| 1066 |
|
| 1067 |
/* Note that we do not set up the notify fd here. We do that after deserialization, |
| 1068 |
* since they might have gotten serialized across the reexec. */ |
| 1069 |
|
| 1070 |
*ret = TAKE_PTR(m); |
| 1071 |
|
| 1072 |
return 0; |
| 1073 |
} |
| 1074 |
|
| 1075 |
static int manager_setup_notify(Manager *m) { |
| 1076 |
int r; |
| 1077 |
|
| 1078 |
if (MANAGER_IS_TEST_RUN(m)) |
| 1079 |
return 0; |
| 1080 |
|
| 1081 |
if (m->notify_fd < 0) { |
| 1082 |
_cleanup_close_ int fd = -EBADF; |
| 1083 |
union sockaddr_union sa; |
| 1084 |
socklen_t sa_len; |
| 1085 |
|
| 1086 |
/* First free all secondary fields */ |
| 1087 |
m->notify_socket = mfree(m->notify_socket); |
| 1088 |
m->notify_event_source = sd_event_source_disable_unref(m->notify_event_source); |
| 1089 |
|
| 1090 |
fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
| 1091 |
if (fd < 0) |
| 1092 |
return log_error_errno(errno, "Failed to allocate notification socket: %m"); |
| 1093 |
|
| 1094 |
(void) fd_increase_rxbuf(fd, MANAGER_SOCKET_RCVBUF_SIZE); |
| 1095 |
|
| 1096 |
m->notify_socket = path_join(m->prefix[EXEC_DIRECTORY_RUNTIME], "systemd/notify"); |
| 1097 |
if (!m->notify_socket) |
| 1098 |
return log_oom(); |
| 1099 |
|
| 1100 |
r = sockaddr_un_set_path(&sa.un, m->notify_socket); |
| 1101 |
if (r < 0) |
| 1102 |
return log_error_errno(r, "Notify socket '%s' not valid for AF_UNIX socket address, refusing.", |
| 1103 |
m->notify_socket); |
| 1104 |
sa_len = r; |
| 1105 |
|
| 1106 |
(void) sockaddr_un_unlink(&sa.un); |
| 1107 |
|
| 1108 |
r = mac_selinux_bind(fd, &sa.sa, sa_len); |
| 1109 |
if (r < 0) |
| 1110 |
return log_error_errno(r, "Failed to bind notify fd to '%s': %m", m->notify_socket); |
| 1111 |
|
| 1112 |
r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true); |
| 1113 |
if (r < 0) |
| 1114 |
return log_error_errno(r, "Failed to enable SO_PASSCRED for notify socket: %m"); |
| 1115 |
|
| 1116 |
// TODO: enforce SO_PASSPIDFD when our baseline of the kernel version is bumped to >= 6.5. |
| 1117 |
r = setsockopt_int(fd, SOL_SOCKET, SO_PASSPIDFD, true); |
| 1118 |
if (r < 0 && r != -ENOPROTOOPT) |
| 1119 |
log_warning_errno(r, "Failed to enable SO_PASSPIDFD for notify socket, ignoring: %m"); |
| 1120 |
|
| 1121 |
m->notify_fd = TAKE_FD(fd); |
| 1122 |
|
| 1123 |
log_debug("Using notification socket %s", m->notify_socket); |
| 1124 |
} |
| 1125 |
|
| 1126 |
if (!m->notify_event_source) { |
| 1127 |
r = sd_event_add_io(m->event, &m->notify_event_source, m->notify_fd, EPOLLIN, manager_dispatch_notify_fd, m); |
| 1128 |
if (r < 0) |
| 1129 |
return log_error_errno(r, "Failed to allocate notify event source: %m"); |
| 1130 |
|
| 1131 |
/* Process notification messages a bit earlier than SIGCHLD, so that we can still identify to which |
| 1132 |
* service an exit message belongs. */ |
| 1133 |
r = sd_event_source_set_priority(m->notify_event_source, EVENT_PRIORITY_NOTIFY); |
| 1134 |
if (r < 0) |
| 1135 |
return log_error_errno(r, "Failed to set priority of notify event source: %m"); |
| 1136 |
|
| 1137 |
(void) sd_event_source_set_description(m->notify_event_source, "manager-notify"); |
| 1138 |
} |
| 1139 |
|
| 1140 |
return 0; |
| 1141 |
} |
| 1142 |
|
| 1143 |
static int manager_setup_user_lookup_fd(Manager *m) { |
| 1144 |
int r; |
| 1145 |
|
| 1146 |
assert(m); |
| 1147 |
|
| 1148 |
/* Set up the socket pair used for passing UID/GID resolution results from forked off processes to PID |
| 1149 |
* 1. Background: we can't do name lookups (NSS) from PID 1, since it might involve IPC and thus activation, |
| 1150 |
* and we might hence deadlock on ourselves. Hence we do all user/group lookups asynchronously from the forked |
| 1151 |
* off processes right before executing the binaries to start. In order to be able to clean up any IPC objects |
| 1152 |
* created by a unit (see RemoveIPC=) we need to know in PID 1 the used UID/GID of the executed processes, |
| 1153 |
* hence we establish this communication channel so that forked off processes can pass their UID/GID |
| 1154 |
* information back to PID 1. The forked off processes send their resolved UID/GID to PID 1 in a simple |
| 1155 |
* datagram, along with their unit name, so that we can share one communication socket pair among all units for |
| 1156 |
* this purpose. |
| 1157 |
* |
| 1158 |
* You might wonder why we need a communication channel for this that is independent of the usual notification |
| 1159 |
* socket scheme (i.e. $NOTIFY_SOCKET). The primary difference is about trust: data sent via the $NOTIFY_SOCKET |
| 1160 |
* channel is only accepted if it originates from the right unit and if reception was enabled for it. The user |
| 1161 |
* lookup socket OTOH is only accessible by PID 1 and its children until they exec(), and always available. |
| 1162 |
* |
| 1163 |
* Note that this function is called under two circumstances: when we first initialize (in which case we |
| 1164 |
* allocate both the socket pair and the event source to listen on it), and when we deserialize after a reload |
| 1165 |
* (in which case the socket pair already exists but we still need to allocate the event source for it). */ |
| 1166 |
|
| 1167 |
if (m->user_lookup_fds[0] < 0) { |
| 1168 |
|
| 1169 |
/* Free all secondary fields */ |
| 1170 |
safe_close_pair(m->user_lookup_fds); |
| 1171 |
m->user_lookup_event_source = sd_event_source_disable_unref(m->user_lookup_event_source); |
| 1172 |
|
| 1173 |
if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->user_lookup_fds) < 0) |
| 1174 |
return log_error_errno(errno, "Failed to allocate user lookup socket: %m"); |
| 1175 |
|
| 1176 |
r = setsockopt_int(m->user_lookup_fds[0], SOL_SOCKET, SO_PASSRIGHTS, false); |
| 1177 |
if (r < 0 && !ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 1178 |
log_warning_errno(r, "Failed to turn off SO_PASSRIGHTS on user lookup socket, ignoring: %m"); |
| 1179 |
|
| 1180 |
(void) fd_increase_rxbuf(m->user_lookup_fds[0], MANAGER_SOCKET_RCVBUF_SIZE); |
| 1181 |
} |
| 1182 |
|
| 1183 |
if (!m->user_lookup_event_source) { |
| 1184 |
r = sd_event_add_io(m->event, &m->user_lookup_event_source, m->user_lookup_fds[0], EPOLLIN, manager_dispatch_user_lookup_fd, m); |
| 1185 |
if (r < 0) |
| 1186 |
return log_error_errno(r, "Failed to allocate user lookup event source: %m"); |
| 1187 |
|
| 1188 |
/* Process even earlier than the notify event source, so that we always know first about valid UID/GID |
| 1189 |
* resolutions */ |
| 1190 |
r = sd_event_source_set_priority(m->user_lookup_event_source, EVENT_PRIORITY_USER_LOOKUP); |
| 1191 |
if (r < 0) |
| 1192 |
return log_error_errno(r, "Failed to set priority of user lookup event source: %m"); |
| 1193 |
|
| 1194 |
(void) sd_event_source_set_description(m->user_lookup_event_source, "user-lookup"); |
| 1195 |
} |
| 1196 |
|
| 1197 |
return 0; |
| 1198 |
} |
| 1199 |
|
| 1200 |
static int manager_setup_handoff_timestamp_fd(Manager *m) { |
| 1201 |
int r; |
| 1202 |
|
| 1203 |
assert(m); |
| 1204 |
|
| 1205 |
/* Set up the socket pair used for passing timestamps back when the executor processes we fork |
| 1206 |
* off invokes execve(), i.e. when we hand off control to our payload processes. */ |
| 1207 |
|
| 1208 |
if (m->handoff_timestamp_fds[0] < 0) { |
| 1209 |
m->handoff_timestamp_event_source = sd_event_source_disable_unref(m->handoff_timestamp_event_source); |
| 1210 |
safe_close_pair(m->handoff_timestamp_fds); |
| 1211 |
|
| 1212 |
if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->handoff_timestamp_fds) < 0) |
| 1213 |
return log_error_errno(errno, "Failed to allocate handoff timestamp socket: %m"); |
| 1214 |
|
| 1215 |
/* Make sure children never have to block */ |
| 1216 |
(void) fd_increase_rxbuf(m->handoff_timestamp_fds[0], MANAGER_SOCKET_RCVBUF_SIZE); |
| 1217 |
|
| 1218 |
r = setsockopt_int(m->handoff_timestamp_fds[0], SOL_SOCKET, SO_PASSCRED, true); |
| 1219 |
if (r < 0) |
| 1220 |
return log_error_errno(r, "Failed to enable SO_PASSCRED on handoff timestamp socket: %m"); |
| 1221 |
|
| 1222 |
r = setsockopt_int(m->handoff_timestamp_fds[0], SOL_SOCKET, SO_PASSRIGHTS, false); |
| 1223 |
if (r < 0 && !ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 1224 |
log_warning_errno(r, "Failed to turn off SO_PASSRIGHTS on handoff timestamp socket, ignoring: %m"); |
| 1225 |
|
| 1226 |
/* Mark the receiving socket as O_NONBLOCK (but leave sending side as-is) */ |
| 1227 |
r = fd_nonblock(m->handoff_timestamp_fds[0], true); |
| 1228 |
if (r < 0) |
| 1229 |
return log_error_errno(r, "Failed to make handoff timestamp socket O_NONBLOCK: %m"); |
| 1230 |
} |
| 1231 |
|
| 1232 |
if (!m->handoff_timestamp_event_source) { |
| 1233 |
r = sd_event_add_io(m->event, &m->handoff_timestamp_event_source, m->handoff_timestamp_fds[0], EPOLLIN, manager_dispatch_handoff_timestamp_fd, m); |
| 1234 |
if (r < 0) |
| 1235 |
return log_error_errno(r, "Failed to allocate handoff timestamp event source: %m"); |
| 1236 |
|
| 1237 |
r = sd_event_source_set_priority(m->handoff_timestamp_event_source, EVENT_PRIORITY_HANDOFF_TIMESTAMP); |
| 1238 |
if (r < 0) |
| 1239 |
return log_error_errno(r, "Failed to set priority of handoff timestamp event source: %m"); |
| 1240 |
|
| 1241 |
(void) sd_event_source_set_description(m->handoff_timestamp_event_source, "handoff-timestamp"); |
| 1242 |
} |
| 1243 |
|
| 1244 |
return 0; |
| 1245 |
} |
| 1246 |
|
| 1247 |
static int manager_setup_pidref_transport_fd(Manager *m) { |
| 1248 |
int r; |
| 1249 |
|
| 1250 |
assert(m); |
| 1251 |
|
| 1252 |
/* Set up the socket pair used for passing parent and child pidrefs back when the executor unshares |
| 1253 |
* a PID namespace and forks again when using PrivatePIDs=yes. */ |
| 1254 |
|
| 1255 |
if (m->pidref_transport_fds[0] < 0) { |
| 1256 |
m->pidref_event_source = sd_event_source_disable_unref(m->pidref_event_source); |
| 1257 |
safe_close_pair(m->pidref_transport_fds); |
| 1258 |
|
| 1259 |
if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->pidref_transport_fds) < 0) |
| 1260 |
return log_error_errno(errno, "Failed to allocate pidref socket: %m"); |
| 1261 |
|
| 1262 |
/* Make sure children never have to block */ |
| 1263 |
(void) fd_increase_rxbuf(m->pidref_transport_fds[0], MANAGER_SOCKET_RCVBUF_SIZE); |
| 1264 |
|
| 1265 |
r = setsockopt_int(m->pidref_transport_fds[0], SOL_SOCKET, SO_PASSCRED, true); |
| 1266 |
if (r < 0) |
| 1267 |
return log_error_errno(r, "Failed to enable SO_PASSCRED for pidref socket: %m"); |
| 1268 |
|
| 1269 |
r = setsockopt_int(m->pidref_transport_fds[0], SOL_SOCKET, SO_PASSPIDFD, true); |
| 1270 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 1271 |
log_debug_errno(r, "SO_PASSPIDFD is not supported for pidref socket, ignoring."); |
| 1272 |
else if (r < 0) |
| 1273 |
log_warning_errno(r, "Failed to enable SO_PASSPIDFD for pidref socket, ignoring: %m"); |
| 1274 |
|
| 1275 |
/* Mark the receiving socket as O_NONBLOCK (but leave sending side as-is) */ |
| 1276 |
r = fd_nonblock(m->pidref_transport_fds[0], true); |
| 1277 |
if (r < 0) |
| 1278 |
return log_error_errno(r, "Failed to make pidref socket O_NONBLOCK: %m"); |
| 1279 |
} |
| 1280 |
|
| 1281 |
if (!m->pidref_event_source) { |
| 1282 |
r = sd_event_add_io(m->event, &m->pidref_event_source, m->pidref_transport_fds[0], EPOLLIN, manager_dispatch_pidref_transport_fd, m); |
| 1283 |
if (r < 0) |
| 1284 |
return log_error_errno(r, "Failed to allocate pidref event source: %m"); |
| 1285 |
|
| 1286 |
r = sd_event_source_set_priority(m->pidref_event_source, EVENT_PRIORITY_PIDREF); |
| 1287 |
if (r < 0) |
| 1288 |
return log_error_errno(r, "Failed to set priority of pidref event source: %m"); |
| 1289 |
|
| 1290 |
(void) sd_event_source_set_description(m->pidref_event_source, "pidref"); |
| 1291 |
} |
| 1292 |
|
| 1293 |
return 0; |
| 1294 |
} |
| 1295 |
|
| 1296 |
static unsigned manager_dispatch_cleanup_queue(Manager *m) { |
| 1297 |
Unit *u; |
| 1298 |
unsigned n = 0; |
| 1299 |
|
| 1300 |
assert(m); |
| 1301 |
|
| 1302 |
while ((u = m->cleanup_queue)) { |
| 1303 |
assert(u->in_cleanup_queue); |
| 1304 |
|
| 1305 |
unit_free(u); |
| 1306 |
n++; |
| 1307 |
} |
| 1308 |
|
| 1309 |
return n; |
| 1310 |
} |
| 1311 |
|
| 1312 |
static unsigned manager_dispatch_release_resources_queue(Manager *m) { |
| 1313 |
unsigned n = 0; |
| 1314 |
Unit *u; |
| 1315 |
|
| 1316 |
assert(m); |
| 1317 |
|
| 1318 |
while ((u = LIST_POP(release_resources_queue, m->release_resources_queue))) { |
| 1319 |
assert(u->in_release_resources_queue); |
| 1320 |
u->in_release_resources_queue = false; |
| 1321 |
|
| 1322 |
n++; |
| 1323 |
|
| 1324 |
unit_release_resources(u); |
| 1325 |
} |
| 1326 |
|
| 1327 |
return n; |
| 1328 |
} |
| 1329 |
|
| 1330 |
enum { |
| 1331 |
GC_OFFSET_IN_PATH, /* This one is on the path we were traveling */ |
| 1332 |
GC_OFFSET_UNSURE, /* No clue */ |
| 1333 |
GC_OFFSET_GOOD, /* We still need this unit */ |
| 1334 |
GC_OFFSET_BAD, /* We don't need this unit anymore */ |
| 1335 |
_GC_OFFSET_MAX |
| 1336 |
}; |
| 1337 |
|
| 1338 |
static void unit_gc_mark_good(Unit *u, unsigned gc_marker) { |
| 1339 |
Unit *other; |
| 1340 |
|
| 1341 |
u->gc_marker = gc_marker + GC_OFFSET_GOOD; |
| 1342 |
|
| 1343 |
/* Recursively mark referenced units as GOOD as well */ |
| 1344 |
UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_REFERENCES) |
| 1345 |
if (other->gc_marker == gc_marker + GC_OFFSET_UNSURE) |
| 1346 |
unit_gc_mark_good(other, gc_marker); |
| 1347 |
} |
| 1348 |
|
| 1349 |
static void unit_gc_sweep(Unit *u, unsigned gc_marker) { |
| 1350 |
Unit *other; |
| 1351 |
bool is_bad; |
| 1352 |
|
| 1353 |
assert(u); |
| 1354 |
|
| 1355 |
if (IN_SET(u->gc_marker - gc_marker, |
| 1356 |
GC_OFFSET_GOOD, GC_OFFSET_BAD, GC_OFFSET_UNSURE, GC_OFFSET_IN_PATH)) |
| 1357 |
return; |
| 1358 |
|
| 1359 |
if (u->in_cleanup_queue) |
| 1360 |
goto bad; |
| 1361 |
|
| 1362 |
if (!unit_may_gc(u)) |
| 1363 |
goto good; |
| 1364 |
|
| 1365 |
u->gc_marker = gc_marker + GC_OFFSET_IN_PATH; |
| 1366 |
|
| 1367 |
is_bad = true; |
| 1368 |
|
| 1369 |
UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_REFERENCED_BY) { |
| 1370 |
unit_gc_sweep(other, gc_marker); |
| 1371 |
|
| 1372 |
if (other->gc_marker == gc_marker + GC_OFFSET_GOOD) |
| 1373 |
goto good; |
| 1374 |
|
| 1375 |
if (other->gc_marker != gc_marker + GC_OFFSET_BAD) |
| 1376 |
is_bad = false; |
| 1377 |
} |
| 1378 |
|
| 1379 |
LIST_FOREACH(refs_by_target, ref, u->refs_by_target) { |
| 1380 |
unit_gc_sweep(ref->source, gc_marker); |
| 1381 |
|
| 1382 |
if (ref->source->gc_marker == gc_marker + GC_OFFSET_GOOD) |
| 1383 |
goto good; |
| 1384 |
|
| 1385 |
if (ref->source->gc_marker != gc_marker + GC_OFFSET_BAD) |
| 1386 |
is_bad = false; |
| 1387 |
} |
| 1388 |
|
| 1389 |
if (is_bad) |
| 1390 |
goto bad; |
| 1391 |
|
| 1392 |
/* We were unable to find anything out about this entry, so |
| 1393 |
* let's investigate it later */ |
| 1394 |
u->gc_marker = gc_marker + GC_OFFSET_UNSURE; |
| 1395 |
unit_add_to_gc_queue(u); |
| 1396 |
return; |
| 1397 |
|
| 1398 |
bad: |
| 1399 |
/* We definitely know that this one is not useful anymore, so |
| 1400 |
* let's mark it for deletion */ |
| 1401 |
u->gc_marker = gc_marker + GC_OFFSET_BAD; |
| 1402 |
unit_add_to_cleanup_queue(u); |
| 1403 |
return; |
| 1404 |
|
| 1405 |
good: |
| 1406 |
unit_gc_mark_good(u, gc_marker); |
| 1407 |
} |
| 1408 |
|
| 1409 |
static unsigned manager_dispatch_gc_unit_queue(Manager *m) { |
| 1410 |
unsigned n = 0, gc_marker; |
| 1411 |
|
| 1412 |
assert(m); |
| 1413 |
|
| 1414 |
/* log_debug("Running GC..."); */ |
| 1415 |
|
| 1416 |
m->gc_marker += _GC_OFFSET_MAX; |
| 1417 |
if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX) |
| 1418 |
m->gc_marker = 1; |
| 1419 |
|
| 1420 |
gc_marker = m->gc_marker; |
| 1421 |
|
| 1422 |
Unit *u; |
| 1423 |
while ((u = m->gc_unit_queue)) { |
| 1424 |
assert(u->in_gc_queue); |
| 1425 |
|
| 1426 |
unit_gc_sweep(u, gc_marker); |
| 1427 |
|
| 1428 |
LIST_REMOVE(gc_queue, m->gc_unit_queue, u); |
| 1429 |
u->in_gc_queue = false; |
| 1430 |
|
| 1431 |
n++; |
| 1432 |
|
| 1433 |
if (IN_SET(u->gc_marker - gc_marker, |
| 1434 |
GC_OFFSET_BAD, GC_OFFSET_UNSURE)) { |
| 1435 |
if (u->id) |
| 1436 |
log_unit_debug(u, "Collecting."); |
| 1437 |
u->gc_marker = gc_marker + GC_OFFSET_BAD; |
| 1438 |
unit_add_to_cleanup_queue(u); |
| 1439 |
} |
| 1440 |
} |
| 1441 |
|
| 1442 |
return n; |
| 1443 |
} |
| 1444 |
|
| 1445 |
static unsigned manager_dispatch_gc_job_queue(Manager *m) { |
| 1446 |
unsigned n = 0; |
| 1447 |
Job *j; |
| 1448 |
|
| 1449 |
assert(m); |
| 1450 |
|
| 1451 |
while ((j = LIST_POP(gc_queue, m->gc_job_queue))) { |
| 1452 |
assert(j->in_gc_queue); |
| 1453 |
j->in_gc_queue = false; |
| 1454 |
|
| 1455 |
n++; |
| 1456 |
|
| 1457 |
if (!job_may_gc(j)) |
| 1458 |
continue; |
| 1459 |
|
| 1460 |
log_unit_debug(j->unit, "Collecting job."); |
| 1461 |
(void) job_finish_and_invalidate(j, JOB_COLLECTED, false, false); |
| 1462 |
} |
| 1463 |
|
| 1464 |
return n; |
| 1465 |
} |
| 1466 |
|
| 1467 |
static int manager_ratelimit_requeue(sd_event_source *s, uint64_t usec, void *userdata) { |
| 1468 |
Unit *u = userdata; |
| 1469 |
|
| 1470 |
assert(u); |
| 1471 |
assert(s == u->auto_start_stop_event_source); |
| 1472 |
|
| 1473 |
u->auto_start_stop_event_source = sd_event_source_unref(u->auto_start_stop_event_source); |
| 1474 |
|
| 1475 |
/* Re-queue to all queues, if the rate limit hit we might have been throttled on any of them. */ |
| 1476 |
unit_submit_to_stop_when_unneeded_queue(u); |
| 1477 |
unit_submit_to_start_when_upheld_queue(u); |
| 1478 |
unit_submit_to_stop_when_bound_queue(u); |
| 1479 |
|
| 1480 |
return 0; |
| 1481 |
} |
| 1482 |
|
| 1483 |
static int manager_ratelimit_check_and_queue(Unit *u) { |
| 1484 |
int r; |
| 1485 |
|
| 1486 |
assert(u); |
| 1487 |
|
| 1488 |
if (ratelimit_below(&u->auto_start_stop_ratelimit)) |
| 1489 |
return 1; |
| 1490 |
|
| 1491 |
/* Already queued, no need to requeue */ |
| 1492 |
if (u->auto_start_stop_event_source) |
| 1493 |
return 0; |
| 1494 |
|
| 1495 |
r = sd_event_add_time( |
| 1496 |
u->manager->event, |
| 1497 |
&u->auto_start_stop_event_source, |
| 1498 |
CLOCK_MONOTONIC, |
| 1499 |
ratelimit_end(&u->auto_start_stop_ratelimit), |
| 1500 |
0, |
| 1501 |
manager_ratelimit_requeue, |
| 1502 |
u); |
| 1503 |
if (r < 0) |
| 1504 |
return log_unit_error_errno(u, r, "Failed to queue timer on event loop: %m"); |
| 1505 |
|
| 1506 |
return 0; |
| 1507 |
} |
| 1508 |
|
| 1509 |
static unsigned manager_dispatch_stop_when_unneeded_queue(Manager *m) { |
| 1510 |
unsigned n = 0; |
| 1511 |
Unit *u; |
| 1512 |
int r; |
| 1513 |
|
| 1514 |
assert(m); |
| 1515 |
|
| 1516 |
while ((u = LIST_POP(stop_when_unneeded_queue, m->stop_when_unneeded_queue))) { |
| 1517 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1518 |
|
| 1519 |
assert(u->in_stop_when_unneeded_queue); |
| 1520 |
u->in_stop_when_unneeded_queue = false; |
| 1521 |
|
| 1522 |
n++; |
| 1523 |
|
| 1524 |
if (!unit_is_unneeded(u)) |
| 1525 |
continue; |
| 1526 |
|
| 1527 |
log_unit_debug(u, "Unit is not needed anymore."); |
| 1528 |
|
| 1529 |
/* If stopping a unit fails continuously we might enter a stop loop here, hence stop acting on the |
| 1530 |
* service being unnecessary after a while. */ |
| 1531 |
|
| 1532 |
r = manager_ratelimit_check_and_queue(u); |
| 1533 |
if (r <= 0) { |
| 1534 |
log_unit_warning(u, |
| 1535 |
"Unit not needed anymore, but not stopping since we tried this too often recently.%s", |
| 1536 |
r == 0 ? " Will retry later." : ""); |
| 1537 |
continue; |
| 1538 |
} |
| 1539 |
|
| 1540 |
/* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */ |
| 1541 |
r = manager_add_job(u->manager, JOB_STOP, u, JOB_FAIL, &error, /* ret= */ NULL); |
| 1542 |
if (r < 0) |
| 1543 |
log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %s", bus_error_message(&error, r)); |
| 1544 |
} |
| 1545 |
|
| 1546 |
return n; |
| 1547 |
} |
| 1548 |
|
| 1549 |
static unsigned manager_dispatch_start_when_upheld_queue(Manager *m) { |
| 1550 |
unsigned n = 0; |
| 1551 |
Unit *u; |
| 1552 |
int r; |
| 1553 |
|
| 1554 |
assert(m); |
| 1555 |
|
| 1556 |
while ((u = LIST_POP(start_when_upheld_queue, m->start_when_upheld_queue))) { |
| 1557 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1558 |
Unit *culprit = NULL; |
| 1559 |
|
| 1560 |
assert(u->in_start_when_upheld_queue); |
| 1561 |
u->in_start_when_upheld_queue = false; |
| 1562 |
|
| 1563 |
n++; |
| 1564 |
|
| 1565 |
if (!unit_is_upheld_by_active(u, &culprit)) |
| 1566 |
continue; |
| 1567 |
|
| 1568 |
log_unit_debug(u, "Unit is started because upheld by active unit %s.", culprit->id); |
| 1569 |
|
| 1570 |
/* If stopping a unit fails continuously we might enter a stop loop here, hence stop acting on the |
| 1571 |
* service being unnecessary after a while. */ |
| 1572 |
|
| 1573 |
r = manager_ratelimit_check_and_queue(u); |
| 1574 |
if (r <= 0) { |
| 1575 |
log_unit_warning(u, |
| 1576 |
"Unit needs to be started because active unit %s upholds it, but not starting since we tried this too often recently.%s", |
| 1577 |
culprit->id, |
| 1578 |
r == 0 ? " Will retry later." : ""); |
| 1579 |
continue; |
| 1580 |
} |
| 1581 |
|
| 1582 |
r = manager_add_job(u->manager, JOB_START, u, JOB_FAIL, &error, /* ret= */ NULL); |
| 1583 |
if (r < 0) |
| 1584 |
log_unit_warning_errno(u, r, "Failed to enqueue start job, ignoring: %s", bus_error_message(&error, r)); |
| 1585 |
} |
| 1586 |
|
| 1587 |
return n; |
| 1588 |
} |
| 1589 |
|
| 1590 |
static unsigned manager_dispatch_stop_when_bound_queue(Manager *m) { |
| 1591 |
unsigned n = 0; |
| 1592 |
Unit *u; |
| 1593 |
int r; |
| 1594 |
|
| 1595 |
assert(m); |
| 1596 |
|
| 1597 |
while ((u = LIST_POP(stop_when_bound_queue, m->stop_when_bound_queue))) { |
| 1598 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1599 |
Unit *culprit = NULL; |
| 1600 |
|
| 1601 |
assert(u->in_stop_when_bound_queue); |
| 1602 |
u->in_stop_when_bound_queue = false; |
| 1603 |
|
| 1604 |
n++; |
| 1605 |
|
| 1606 |
if (!unit_is_bound_by_inactive(u, &culprit)) |
| 1607 |
continue; |
| 1608 |
|
| 1609 |
log_unit_debug(u, "Unit is stopped because bound to inactive unit %s.", culprit->id); |
| 1610 |
|
| 1611 |
/* If stopping a unit fails continuously we might enter a stop loop here, hence stop acting on the |
| 1612 |
* service being unnecessary after a while. */ |
| 1613 |
|
| 1614 |
r = manager_ratelimit_check_and_queue(u); |
| 1615 |
if (r <= 0) { |
| 1616 |
log_unit_warning(u, |
| 1617 |
"Unit needs to be stopped because it is bound to inactive unit %s it, but not stopping since we tried this too often recently.%s", |
| 1618 |
culprit->id, |
| 1619 |
r == 0 ? " Will retry later." : ""); |
| 1620 |
continue; |
| 1621 |
} |
| 1622 |
|
| 1623 |
r = manager_add_job(u->manager, JOB_STOP, u, JOB_REPLACE, &error, /* ret= */ NULL); |
| 1624 |
if (r < 0) |
| 1625 |
log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %s", bus_error_message(&error, r)); |
| 1626 |
} |
| 1627 |
|
| 1628 |
return n; |
| 1629 |
} |
| 1630 |
|
| 1631 |
static unsigned manager_dispatch_stop_notify_queue(Manager *m) { |
| 1632 |
unsigned n = 0; |
| 1633 |
|
| 1634 |
assert(m); |
| 1635 |
|
| 1636 |
if (m->may_dispatch_stop_notify_queue < 0) |
| 1637 |
m->may_dispatch_stop_notify_queue = hashmap_isempty(m->jobs); |
| 1638 |
|
| 1639 |
if (!m->may_dispatch_stop_notify_queue) |
| 1640 |
return 0; |
| 1641 |
|
| 1642 |
m->may_dispatch_stop_notify_queue = false; |
| 1643 |
|
| 1644 |
LIST_FOREACH(stop_notify_queue, u, m->stop_notify_queue) { |
| 1645 |
assert(u->in_stop_notify_queue); |
| 1646 |
|
| 1647 |
assert(UNIT_VTABLE(u)->stop_notify); |
| 1648 |
if (UNIT_VTABLE(u)->stop_notify(u)) { |
| 1649 |
assert(!u->in_stop_notify_queue); |
| 1650 |
n++; |
| 1651 |
} |
| 1652 |
} |
| 1653 |
|
| 1654 |
return n; |
| 1655 |
} |
| 1656 |
|
| 1657 |
static void manager_clear_jobs_and_units(Manager *m) { |
| 1658 |
Unit *u; |
| 1659 |
|
| 1660 |
assert(m); |
| 1661 |
|
| 1662 |
while ((u = hashmap_first(m->units))) |
| 1663 |
unit_free(u); |
| 1664 |
|
| 1665 |
manager_dispatch_cleanup_queue(m); |
| 1666 |
|
| 1667 |
assert(!m->load_queue); |
| 1668 |
assert(prioq_isempty(m->run_queue)); |
| 1669 |
assert(!m->dbus_unit_queue); |
| 1670 |
assert(!m->dbus_job_queue); |
| 1671 |
assert(!m->cleanup_queue); |
| 1672 |
assert(!m->gc_unit_queue); |
| 1673 |
assert(!m->gc_job_queue); |
| 1674 |
assert(!m->cgroup_realize_queue); |
| 1675 |
assert(!m->cgroup_empty_queue); |
| 1676 |
assert(!m->cgroup_oom_queue); |
| 1677 |
assert(!m->target_deps_queue); |
| 1678 |
assert(!m->stop_when_unneeded_queue); |
| 1679 |
assert(!m->start_when_upheld_queue); |
| 1680 |
assert(!m->stop_when_bound_queue); |
| 1681 |
assert(!m->release_resources_queue); |
| 1682 |
|
| 1683 |
assert(hashmap_isempty(m->jobs)); |
| 1684 |
assert(hashmap_isempty(m->units)); |
| 1685 |
assert(hashmap_isempty(m->units_by_invocation_id)); |
| 1686 |
|
| 1687 |
m->n_on_console = 0; |
| 1688 |
m->n_running_jobs = 0; |
| 1689 |
m->n_installed_jobs = 0; |
| 1690 |
m->n_failed_jobs = 0; |
| 1691 |
|
| 1692 |
m->transactions_with_cycle = set_free(m->transactions_with_cycle); |
| 1693 |
} |
| 1694 |
|
| 1695 |
Manager* manager_free(Manager *m) { |
| 1696 |
if (!m) |
| 1697 |
return NULL; |
| 1698 |
|
| 1699 |
manager_clear_jobs_and_units(m); |
| 1700 |
|
| 1701 |
for (UnitType c = 0; c < _UNIT_TYPE_MAX; c++) |
| 1702 |
if (unit_vtable[c]->shutdown) |
| 1703 |
unit_vtable[c]->shutdown(m); |
| 1704 |
|
| 1705 |
/* Keep the cgroup hierarchy in place except when we know we are going down for good */ |
| 1706 |
manager_shutdown_cgroup(m, /* delete= */ IN_SET(m->objective, MANAGER_EXIT, MANAGER_REBOOT, MANAGER_POWEROFF, MANAGER_HALT, MANAGER_KEXEC)); |
| 1707 |
|
| 1708 |
lookup_paths_flush_generator(&m->lookup_paths); |
| 1709 |
|
| 1710 |
bus_done(m); |
| 1711 |
manager_varlink_done(m); |
| 1712 |
|
| 1713 |
exec_shared_runtime_vacuum(m); |
| 1714 |
hashmap_free(m->exec_shared_runtime_by_id); |
| 1715 |
|
| 1716 |
dynamic_user_vacuum(m, false); |
| 1717 |
hashmap_free(m->dynamic_users); |
| 1718 |
|
| 1719 |
hashmap_free(m->units); |
| 1720 |
hashmap_free(m->units_by_invocation_id); |
| 1721 |
hashmap_free(m->jobs); |
| 1722 |
hashmap_free(m->watch_pids); |
| 1723 |
hashmap_free(m->watch_pids_more); |
| 1724 |
hashmap_free(m->watch_bus); |
| 1725 |
|
| 1726 |
prioq_free(m->run_queue); |
| 1727 |
|
| 1728 |
set_free(m->startup_units); |
| 1729 |
set_free(m->failed_units); |
| 1730 |
|
| 1731 |
sd_event_source_unref(m->signal_event_source); |
| 1732 |
sd_event_source_unref(m->sigchld_event_source); |
| 1733 |
sd_event_source_unref(m->notify_event_source); |
| 1734 |
sd_event_source_unref(m->time_change_event_source); |
| 1735 |
sd_event_source_unref(m->timezone_change_event_source); |
| 1736 |
sd_event_source_unref(m->jobs_in_progress_event_source); |
| 1737 |
sd_event_source_unref(m->run_queue_event_source); |
| 1738 |
sd_event_source_unref(m->user_lookup_event_source); |
| 1739 |
sd_event_source_unref(m->handoff_timestamp_event_source); |
| 1740 |
sd_event_source_unref(m->pidref_event_source); |
| 1741 |
FOREACH_ARRAY(pressure_event_source, m->pressure_event_source, _PRESSURE_RESOURCE_MAX) |
| 1742 |
sd_event_source_unref(*pressure_event_source); |
| 1743 |
|
| 1744 |
safe_close(m->signal_fd); |
| 1745 |
safe_close(m->notify_fd); |
| 1746 |
safe_close_pair(m->user_lookup_fds); |
| 1747 |
safe_close_pair(m->handoff_timestamp_fds); |
| 1748 |
safe_close_pair(m->pidref_transport_fds); |
| 1749 |
|
| 1750 |
manager_close_ask_password(m); |
| 1751 |
|
| 1752 |
manager_close_idle_pipe(m); |
| 1753 |
|
| 1754 |
sd_event_unref(m->event); |
| 1755 |
|
| 1756 |
free(m->notify_socket); |
| 1757 |
|
| 1758 |
lookup_paths_done(&m->lookup_paths); |
| 1759 |
strv_free(m->transient_environment); |
| 1760 |
strv_free(m->client_environment); |
| 1761 |
|
| 1762 |
hashmap_free(m->cgroup_unit); |
| 1763 |
manager_free_unit_name_maps(m); |
| 1764 |
|
| 1765 |
free(m->switch_root); |
| 1766 |
free(m->switch_root_init); |
| 1767 |
|
| 1768 |
sd_bus_track_unref(m->subscribed); |
| 1769 |
strv_free(m->subscribed_as_strv); |
| 1770 |
|
| 1771 |
unit_defaults_done(&m->defaults); |
| 1772 |
|
| 1773 |
FOREACH_ARRAY(map, m->units_needing_mounts_for, _UNIT_MOUNT_DEPENDENCY_TYPE_MAX) { |
| 1774 |
assert(hashmap_isempty(*map)); |
| 1775 |
hashmap_free(*map); |
| 1776 |
} |
| 1777 |
|
| 1778 |
hashmap_free(m->uid_refs); |
| 1779 |
hashmap_free(m->gid_refs); |
| 1780 |
|
| 1781 |
FOREACH_ARRAY(i, m->prefix, _EXEC_DIRECTORY_TYPE_MAX) |
| 1782 |
free(*i); |
| 1783 |
|
| 1784 |
free(m->received_credentials_directory); |
| 1785 |
free(m->received_encrypted_credentials_directory); |
| 1786 |
|
| 1787 |
free(m->watchdog_pretimeout_governor); |
| 1788 |
free(m->watchdog_pretimeout_governor_overridden); |
| 1789 |
|
| 1790 |
sd_netlink_unref(m->nfnl); |
| 1791 |
|
| 1792 |
#if BPF_FRAMEWORK |
| 1793 |
bpf_restrict_fs_destroy(m->restrict_fs); |
| 1794 |
#endif |
| 1795 |
close_many(m->restrict_fsaccess_link_fds, ELEMENTSOF(m->restrict_fsaccess_link_fds)); |
| 1796 |
safe_close(m->restrict_fsaccess_bss_map_fd); |
| 1797 |
|
| 1798 |
safe_close(m->executor_fd); |
| 1799 |
free(m->executor_path); |
| 1800 |
|
| 1801 |
return mfree(m); |
| 1802 |
} |
| 1803 |
|
| 1804 |
static void manager_enumerate_perpetual(Manager *m) { |
| 1805 |
assert(m); |
| 1806 |
|
| 1807 |
if (FLAGS_SET(m->test_run_flags, MANAGER_TEST_RUN_MINIMAL)) |
| 1808 |
return; |
| 1809 |
|
| 1810 |
/* Let's ask every type to load all units from disk/kernel that it might know */ |
| 1811 |
for (UnitType c = 0; c < _UNIT_TYPE_MAX; c++) { |
| 1812 |
if (!unit_type_supported(c)) { |
| 1813 |
log_debug("Unit type .%s is not supported on this system.", unit_type_to_string(c)); |
| 1814 |
continue; |
| 1815 |
} |
| 1816 |
|
| 1817 |
if (unit_vtable[c]->enumerate_perpetual) |
| 1818 |
unit_vtable[c]->enumerate_perpetual(m); |
| 1819 |
} |
| 1820 |
} |
| 1821 |
|
| 1822 |
static void manager_enumerate(Manager *m) { |
| 1823 |
assert(m); |
| 1824 |
|
| 1825 |
if (FLAGS_SET(m->test_run_flags, MANAGER_TEST_RUN_MINIMAL)) |
| 1826 |
return; |
| 1827 |
|
| 1828 |
/* Let's ask every type to load all units from disk/kernel that it might know */ |
| 1829 |
for (UnitType c = 0; c < _UNIT_TYPE_MAX; c++) { |
| 1830 |
if (!unit_type_supported(c)) { |
| 1831 |
log_debug("Unit type .%s is not supported on this system.", unit_type_to_string(c)); |
| 1832 |
continue; |
| 1833 |
} |
| 1834 |
|
| 1835 |
if (unit_vtable[c]->enumerate) |
| 1836 |
unit_vtable[c]->enumerate(m); |
| 1837 |
} |
| 1838 |
|
| 1839 |
manager_dispatch_load_queue(m); |
| 1840 |
} |
| 1841 |
|
| 1842 |
static void manager_coldplug(Manager *m) { |
| 1843 |
Unit *u; |
| 1844 |
char *k; |
| 1845 |
int r; |
| 1846 |
|
| 1847 |
assert(m); |
| 1848 |
|
| 1849 |
log_debug("Invoking unit coldplug() handlers%s", glyph(GLYPH_ELLIPSIS)); |
| 1850 |
|
| 1851 |
/* Let's place the units back into their deserialized state */ |
| 1852 |
HASHMAP_FOREACH_KEY(u, k, m->units) { |
| 1853 |
|
| 1854 |
/* ignore aliases */ |
| 1855 |
if (u->id != k) |
| 1856 |
continue; |
| 1857 |
|
| 1858 |
r = unit_coldplug(u); |
| 1859 |
if (r < 0) |
| 1860 |
log_warning_errno(r, "We couldn't coldplug %s, proceeding anyway: %m", u->id); |
| 1861 |
} |
| 1862 |
} |
| 1863 |
|
| 1864 |
static void manager_catchup(Manager *m) { |
| 1865 |
Unit *u; |
| 1866 |
char *k; |
| 1867 |
|
| 1868 |
assert(m); |
| 1869 |
|
| 1870 |
log_debug("Invoking unit catchup() handlers%s", glyph(GLYPH_ELLIPSIS)); |
| 1871 |
|
| 1872 |
/* Let's catch up on any state changes that happened while we were reloading/reexecing */ |
| 1873 |
HASHMAP_FOREACH_KEY(u, k, m->units) { |
| 1874 |
|
| 1875 |
/* ignore aliases */ |
| 1876 |
if (u->id != k) |
| 1877 |
continue; |
| 1878 |
|
| 1879 |
unit_catchup(u); |
| 1880 |
} |
| 1881 |
} |
| 1882 |
|
| 1883 |
ListenFDsTag* listen_fds_tag_free(ListenFDsTag *t) { |
| 1884 |
if (!t) |
| 1885 |
return NULL; |
| 1886 |
|
| 1887 |
free(t->unit_id); |
| 1888 |
free(t->fdname); |
| 1889 |
return mfree(t); |
| 1890 |
} |
| 1891 |
|
| 1892 |
DEFINE_HASH_OPS_FULL( |
| 1893 |
fd_to_listen_fds_tag_hash_ops, |
| 1894 |
void, trivial_hash_func, trivial_compare_func, close_fd_ptr, |
| 1895 |
ListenFDsTag, listen_fds_tag_free); |
| 1896 |
|
| 1897 |
int manager_dispatch_external_fd_to_unit( |
| 1898 |
Manager *m, |
| 1899 |
const char *unit_id, |
| 1900 |
const char *fdname, |
| 1901 |
uint64_t index, |
| 1902 |
int fd_in, |
| 1903 |
const char *log_context) { |
| 1904 |
|
| 1905 |
_cleanup_close_ int fd = ASSERT_FD(fd_in); |
| 1906 |
Unit *u = NULL; |
| 1907 |
int r; |
| 1908 |
|
| 1909 |
assert(m); |
| 1910 |
assert(unit_id); |
| 1911 |
assert(fdname); |
| 1912 |
assert(log_context); |
| 1913 |
|
| 1914 |
/* Load the unit eagerly: if the unit file exists this brings it into UNIT_LOADED, otherwise it |
| 1915 |
* lands in UNIT_NOT_FOUND. In both cases we want to attach the fd so it's preserved until the |
| 1916 |
* unit is fully stopped (or its file appears via daemon-reload). */ |
| 1917 |
r = manager_load_unit(m, unit_id, /* path= */ NULL, /* e= */ NULL, &u); |
| 1918 |
if (r < 0) |
| 1919 |
return log_warning_errno(r, "%s: failed to load unit '%s', closing fd '%s': %m", |
| 1920 |
log_context, unit_id, fdname); |
| 1921 |
|
| 1922 |
if (!UNIT_VTABLE(u)->attach_external_fd_to_fdstore) |
| 1923 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1924 |
"%s: unit '%s' does not support fd restoration, closing fd '%s'.", |
| 1925 |
log_context, unit_id, fdname); |
| 1926 |
|
| 1927 |
r = UNIT_VTABLE(u)->attach_external_fd_to_fdstore(u, TAKE_FD(fd), fdname, index); |
| 1928 |
if (r < 0) |
| 1929 |
return log_unit_warning_errno(u, r, "%s: failed to attach fd '%s' to fd store: %m", |
| 1930 |
log_context, fdname); |
| 1931 |
|
| 1932 |
return 1; /* fd consumed */ |
| 1933 |
} |
| 1934 |
|
| 1935 |
static int manager_distribute_listen_fds_named(Manager *m, Hashmap *named_listen_fds) { |
| 1936 |
assert(m); |
| 1937 |
|
| 1938 |
/* Route fds whose LISTEN_FDNAMES name was a numeric index into the matching unit's fd store. |
| 1939 |
* The hashmap is built and owned by main.c's collect_fds(), keyed by fd, with ListenFDsTag* values |
| 1940 |
* that already carry the parsed unit-id, original fdname and index (resolved against the |
| 1941 |
* upstream-pushed fdstore-mapping memfd). We steal entries here so any leftover (skipped) entries |
| 1942 |
* are still cleaned up by the hashmap's destructor on the caller side. */ |
| 1943 |
|
| 1944 |
if (MANAGER_IS_TEST_RUN(m)) |
| 1945 |
return 0; |
| 1946 |
|
| 1947 |
for (;;) { |
| 1948 |
_cleanup_(listen_fds_tag_freep) ListenFDsTag *t = NULL; |
| 1949 |
_cleanup_close_ int fd = -EBADF; |
| 1950 |
void *key; |
| 1951 |
|
| 1952 |
t = hashmap_steal_first_key_and_value(named_listen_fds, &key); |
| 1953 |
if (!t) |
| 1954 |
break; |
| 1955 |
|
| 1956 |
fd = PTR_TO_FD(key); |
| 1957 |
|
| 1958 |
if (!t->unit_id || !t->fdname) |
| 1959 |
continue; |
| 1960 |
|
| 1961 |
if (!unit_name_is_valid(t->unit_id, UNIT_NAME_ANY)) |
| 1962 |
continue; |
| 1963 |
|
| 1964 |
(void) manager_dispatch_external_fd_to_unit(m, t->unit_id, t->fdname, t->index, TAKE_FD(fd), "LISTEN_FDS"); |
| 1965 |
} |
| 1966 |
|
| 1967 |
return 0; |
| 1968 |
} |
| 1969 |
|
| 1970 |
static void manager_distribute_fds(Manager *m, FDSet *fds) { |
| 1971 |
Unit *u; |
| 1972 |
|
| 1973 |
assert(m); |
| 1974 |
|
| 1975 |
HASHMAP_FOREACH(u, m->units) { |
| 1976 |
|
| 1977 |
if (fdset_isempty(fds)) |
| 1978 |
break; |
| 1979 |
|
| 1980 |
if (!UNIT_VTABLE(u)->distribute_fds) |
| 1981 |
continue; |
| 1982 |
|
| 1983 |
UNIT_VTABLE(u)->distribute_fds(u, fds); |
| 1984 |
} |
| 1985 |
} |
| 1986 |
|
| 1987 |
static bool manager_dbus_is_running(Manager *m, bool deserialized) { |
| 1988 |
Unit *u; |
| 1989 |
|
| 1990 |
assert(m); |
| 1991 |
|
| 1992 |
/* This checks whether the dbus instance we are supposed to expose our APIs on is up. We check both the socket |
| 1993 |
* and the service unit. If the 'deserialized' parameter is true we'll check the deserialized state of the unit |
| 1994 |
* rather than the current one. */ |
| 1995 |
|
| 1996 |
if (MANAGER_IS_TEST_RUN(m)) |
| 1997 |
return false; |
| 1998 |
|
| 1999 |
u = manager_get_unit(m, SPECIAL_DBUS_SOCKET); |
| 2000 |
if (!u) |
| 2001 |
return false; |
| 2002 |
if ((deserialized ? SOCKET(u)->deserialized_state : SOCKET(u)->state) != SOCKET_RUNNING) |
| 2003 |
return false; |
| 2004 |
|
| 2005 |
u = manager_get_unit(m, SPECIAL_DBUS_SERVICE); |
| 2006 |
if (!u) |
| 2007 |
return false; |
| 2008 |
if (!IN_SET(deserialized ? SERVICE(u)->deserialized_state : SERVICE(u)->state, |
| 2009 |
SERVICE_RUNNING, |
| 2010 |
SERVICE_REFRESH_EXTENSIONS, |
| 2011 |
SERVICE_REFRESH_CREDENTIALS, |
| 2012 |
SERVICE_RELOAD, |
| 2013 |
SERVICE_RELOAD_SIGNAL, |
| 2014 |
SERVICE_RELOAD_NOTIFY, |
| 2015 |
SERVICE_RELOAD_POST, |
| 2016 |
SERVICE_MOUNTING)) |
| 2017 |
return false; |
| 2018 |
|
| 2019 |
return true; |
| 2020 |
} |
| 2021 |
|
| 2022 |
static void manager_setup_bus(Manager *m) { |
| 2023 |
assert(m); |
| 2024 |
|
| 2025 |
if (MANAGER_IS_TEST_RUN(m)) |
| 2026 |
return; |
| 2027 |
|
| 2028 |
/* Let's set up our private bus connection now, unconditionally */ |
| 2029 |
(void) bus_init_private(m); |
| 2030 |
|
| 2031 |
/* If we are in --user mode also connect to the system bus now */ |
| 2032 |
if (MANAGER_IS_USER(m)) |
| 2033 |
(void) bus_init_system(m); |
| 2034 |
|
| 2035 |
/* Let's connect to the bus now, but only if the unit is supposed to be up */ |
| 2036 |
if (manager_dbus_is_running(m, MANAGER_IS_RELOADING(m))) { |
| 2037 |
(void) bus_init_api(m); |
| 2038 |
|
| 2039 |
if (MANAGER_IS_SYSTEM(m)) |
| 2040 |
(void) bus_init_system(m); |
| 2041 |
} |
| 2042 |
} |
| 2043 |
|
| 2044 |
static void manager_preset_all(Manager *m) { |
| 2045 |
int r; |
| 2046 |
|
| 2047 |
assert(m); |
| 2048 |
|
| 2049 |
if (m->first_boot <= 0) |
| 2050 |
return; |
| 2051 |
|
| 2052 |
if (!MANAGER_IS_SYSTEM(m)) |
| 2053 |
return; |
| 2054 |
|
| 2055 |
if (MANAGER_IS_TEST_RUN(m)) |
| 2056 |
return; |
| 2057 |
|
| 2058 |
/* If this is the first boot, and we are in the host system, then preset everything */ |
| 2059 |
UnitFilePresetMode mode = |
| 2060 |
ENABLE_FIRST_BOOT_FULL_PRESET ? UNIT_FILE_PRESET_FULL : UNIT_FILE_PRESET_ENABLE_ONLY; |
| 2061 |
InstallChange *changes = NULL; |
| 2062 |
size_t n_changes = 0; |
| 2063 |
|
| 2064 |
CLEANUP_ARRAY(changes, n_changes, install_changes_free); |
| 2065 |
|
| 2066 |
log_info("Applying preset policy."); |
| 2067 |
r = unit_file_preset_all(RUNTIME_SCOPE_SYSTEM, /* file_flags= */ 0, |
| 2068 |
/* root_dir= */ NULL, mode, &changes, &n_changes); |
| 2069 |
r = install_changes_dump(r, "preset all", changes, n_changes, /* quiet= */ false); |
| 2070 |
if (r >= 0) |
| 2071 |
log_info("Populated /etc with preset unit settings."); |
| 2072 |
} |
| 2073 |
|
| 2074 |
static void manager_ready(Manager *m) { |
| 2075 |
assert(m); |
| 2076 |
|
| 2077 |
/* After having loaded everything, do the final round of catching up with what might have changed */ |
| 2078 |
|
| 2079 |
m->objective = MANAGER_OK; /* Tell everyone we are up now */ |
| 2080 |
|
| 2081 |
/* It might be safe to log to the journal now and connect to dbus */ |
| 2082 |
manager_recheck_journal(m); |
| 2083 |
manager_recheck_dbus(m); |
| 2084 |
|
| 2085 |
/* Let's finally catch up with any changes that took place while we were reloading/reexecing */ |
| 2086 |
manager_catchup(m); |
| 2087 |
|
| 2088 |
/* Create a file which will indicate when the manager started loading units the last time. */ |
| 2089 |
if (MANAGER_IS_SYSTEM(m)) |
| 2090 |
(void) touch_file("/run/systemd/systemd-units-load", false, |
| 2091 |
m->timestamps[MANAGER_TIMESTAMP_UNITS_LOAD].realtime ?: now(CLOCK_REALTIME), |
| 2092 |
UID_INVALID, GID_INVALID, 0444); |
| 2093 |
} |
| 2094 |
|
| 2095 |
Manager* manager_reloading_start(Manager *m) { |
| 2096 |
m->n_reloading++; |
| 2097 |
dual_timestamp_now(m->timestamps + MANAGER_TIMESTAMP_UNITS_LOAD); |
| 2098 |
return m; |
| 2099 |
} |
| 2100 |
|
| 2101 |
void manager_reloading_stopp(Manager **m) { |
| 2102 |
assert(m); |
| 2103 |
|
| 2104 |
if (*m) { |
| 2105 |
assert((*m)->n_reloading > 0); |
| 2106 |
(*m)->n_reloading--; |
| 2107 |
} |
| 2108 |
} |
| 2109 |
|
| 2110 |
static int manager_make_runtime_dir(Manager *m) { |
| 2111 |
int r; |
| 2112 |
|
| 2113 |
assert(m); |
| 2114 |
|
| 2115 |
_cleanup_free_ char *d = path_join(m->prefix[EXEC_DIRECTORY_RUNTIME], "systemd"); |
| 2116 |
if (!d) |
| 2117 |
return log_oom(); |
| 2118 |
|
| 2119 |
r = mkdir_label(d, 0755); |
| 2120 |
if (r < 0 && r != -EEXIST) |
| 2121 |
return log_error_errno(r, "Failed to create directory '%s/': %m", d); |
| 2122 |
|
| 2123 |
return 0; |
| 2124 |
} |
| 2125 |
|
| 2126 |
int manager_startup(Manager *m, FILE *serialization, FDSet *fds, Hashmap *named_listen_fds, const char *root) { |
| 2127 |
int r; |
| 2128 |
|
| 2129 |
assert(m); |
| 2130 |
|
| 2131 |
r = manager_make_runtime_dir(m); |
| 2132 |
if (r < 0) |
| 2133 |
return r; |
| 2134 |
|
| 2135 |
/* If we are running in test mode, we still want to run the generators, |
| 2136 |
* but we should not touch the real generator directories. */ |
| 2137 |
r = lookup_paths_init_or_warn(&m->lookup_paths, m->runtime_scope, |
| 2138 |
MANAGER_IS_TEST_RUN(m) ? LOOKUP_PATHS_TEMPORARY_GENERATED : 0, |
| 2139 |
root); |
| 2140 |
if (r < 0) |
| 2141 |
return r; |
| 2142 |
|
| 2143 |
dual_timestamp_now(m->timestamps + manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_GENERATORS_START)); |
| 2144 |
r = manager_run_environment_generators(m); |
| 2145 |
if (r >= 0) |
| 2146 |
r = manager_run_generators(m); |
| 2147 |
dual_timestamp_now(m->timestamps + manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_GENERATORS_FINISH)); |
| 2148 |
if (r < 0) |
| 2149 |
return r; |
| 2150 |
|
| 2151 |
manager_preset_all(m); |
| 2152 |
|
| 2153 |
lookup_paths_log(&m->lookup_paths); |
| 2154 |
|
| 2155 |
{ |
| 2156 |
/* This block is (optionally) done with the reloading counter bumped */ |
| 2157 |
_unused_ _cleanup_(manager_reloading_stopp) Manager *reloading = NULL; |
| 2158 |
|
| 2159 |
/* Make sure we don't have a left-over from a previous run */ |
| 2160 |
if (!serialization) |
| 2161 |
(void) rm_rf(m->lookup_paths.transient, 0); |
| 2162 |
|
| 2163 |
/* If we will deserialize make sure that during enumeration this is already known, so we increase the |
| 2164 |
* counter here already */ |
| 2165 |
if (serialization) |
| 2166 |
reloading = manager_reloading_start(m); |
| 2167 |
|
| 2168 |
/* First, enumerate what we can from all config files */ |
| 2169 |
dual_timestamp_now(m->timestamps + manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_UNITS_LOAD_START)); |
| 2170 |
manager_enumerate_perpetual(m); |
| 2171 |
manager_enumerate(m); |
| 2172 |
dual_timestamp_now(m->timestamps + manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_UNITS_LOAD_FINISH)); |
| 2173 |
|
| 2174 |
/* Second, deserialize if there is something to deserialize */ |
| 2175 |
if (serialization) { |
| 2176 |
r = manager_deserialize(m, serialization, fds); |
| 2177 |
if (r < 0) |
| 2178 |
return log_error_errno(r, "Deserialization failed: %m"); |
| 2179 |
} |
| 2180 |
|
| 2181 |
if (m->previous_objective >= 0) { |
| 2182 |
if (IN_SET(m->previous_objective, MANAGER_REEXECUTE, MANAGER_SOFT_REBOOT, MANAGER_SWITCH_ROOT)) |
| 2183 |
log_debug("Launching as effect of a '%s' operation.", |
| 2184 |
manager_objective_to_string(m->previous_objective)); |
| 2185 |
else |
| 2186 |
log_warning("Got unexpected previous objective '%s', ignoring.", |
| 2187 |
manager_objective_to_string(m->previous_objective)); |
| 2188 |
} |
| 2189 |
|
| 2190 |
/* If we are in a new soft-reboot iteration bump the counter now before starting units, so |
| 2191 |
* that they can reliably read it. We get the previous objective from serialized state. */ |
| 2192 |
if (m->previous_objective == MANAGER_SOFT_REBOOT) |
| 2193 |
m->soft_reboots_count++; |
| 2194 |
|
| 2195 |
/* If a LUO (Live Update Orchestrator) session from a previous kexec is available, restore |
| 2196 |
* preserved file descriptors into the appropriate service fd stores now, before coldplug. */ |
| 2197 |
(void) manager_luo_restore_fd_stores(m); |
| 2198 |
|
| 2199 |
/* Pick up fds passed via the LISTEN_FDS=/LISTEN_FDNAMES= protocol that are tagged with a |
| 2200 |
* unit id ("unit-id|fdname"), and route them into the matching unit's fd store. Untagged |
| 2201 |
* fds remain in 'fds' and are handed to socket units below as before. */ |
| 2202 |
(void) manager_distribute_listen_fds_named(m, named_listen_fds); |
| 2203 |
|
| 2204 |
/* Any fds left? Find some unit which wants them. This is useful to allow container managers to pass |
| 2205 |
* some file descriptors to us pre-initialized. This enables socket-based activation of entire |
| 2206 |
* containers. */ |
| 2207 |
manager_distribute_fds(m, fds); |
| 2208 |
|
| 2209 |
/* We might have deserialized the notify fd, but if we didn't then let's create it now */ |
| 2210 |
r = manager_setup_notify(m); |
| 2211 |
if (r < 0) |
| 2212 |
/* No sense to continue without notifications, our children would fail anyway. */ |
| 2213 |
return r; |
| 2214 |
|
| 2215 |
r = manager_setup_user_lookup_fd(m); |
| 2216 |
if (r < 0) |
| 2217 |
/* This shouldn't fail, except if things are really broken. */ |
| 2218 |
return r; |
| 2219 |
|
| 2220 |
r = manager_setup_handoff_timestamp_fd(m); |
| 2221 |
if (r < 0) |
| 2222 |
/* This shouldn't fail, except if things are really broken. */ |
| 2223 |
return r; |
| 2224 |
|
| 2225 |
r = manager_setup_pidref_transport_fd(m); |
| 2226 |
if (r < 0) |
| 2227 |
/* This shouldn't fail, except if things are really broken. */ |
| 2228 |
return r; |
| 2229 |
|
| 2230 |
/* Connect to the bus if we are good for it */ |
| 2231 |
manager_setup_bus(m); |
| 2232 |
|
| 2233 |
r = manager_varlink_init(m); |
| 2234 |
if (r < 0) |
| 2235 |
log_warning_errno(r, "Failed to set up Varlink, ignoring: %m"); |
| 2236 |
|
| 2237 |
/* Third, fire things up! */ |
| 2238 |
manager_coldplug(m); |
| 2239 |
|
| 2240 |
/* Clean up runtime objects */ |
| 2241 |
manager_vacuum(m); |
| 2242 |
|
| 2243 |
/* After deserialization, refresh the upstream JSON mapping memfd so the supervisor's |
| 2244 |
* view of our fd store stays consistent with the indices we just restored. */ |
| 2245 |
(void) service_propagate_fd_store_mapping_upstream(m); |
| 2246 |
|
| 2247 |
if (serialization) |
| 2248 |
/* Let's wait for the UnitNew/JobNew messages being sent, before we notify that the |
| 2249 |
* reload is finished */ |
| 2250 |
m->send_reloading_done = true; |
| 2251 |
} |
| 2252 |
|
| 2253 |
/* Set up RestrictFileSystemAccess= BPF LSM after deserialization (so we can detect deserialized link FDs) |
| 2254 |
* and before clearing switching_root (so we can close the initramfs trust window). This must |
| 2255 |
* run after set_manager_settings() has set m->restrict_filesystem_access. */ |
| 2256 |
r = bpf_restrict_fsaccess_setup(m); |
| 2257 |
if (r < 0) |
| 2258 |
return r; |
| 2259 |
|
| 2260 |
manager_ready(m); |
| 2261 |
|
| 2262 |
manager_set_switching_root(m, false); |
| 2263 |
|
| 2264 |
return 0; |
| 2265 |
} |
| 2266 |
|
| 2267 |
int manager_add_job_full( |
| 2268 |
Manager *m, |
| 2269 |
JobType type, |
| 2270 |
Unit *unit, |
| 2271 |
JobMode mode, |
| 2272 |
TransactionAddFlags extra_flags, |
| 2273 |
Set *affected_jobs, |
| 2274 |
sd_bus_error *reterr_error, |
| 2275 |
Job **ret) { |
| 2276 |
|
| 2277 |
_cleanup_(transaction_abort_and_freep) Transaction *tr = NULL; |
| 2278 |
int r; |
| 2279 |
|
| 2280 |
assert(m); |
| 2281 |
assert(type >= 0 && type < _JOB_TYPE_MAX); |
| 2282 |
assert(unit); |
| 2283 |
assert(mode >= 0 && mode < _JOB_MODE_MAX); |
| 2284 |
assert((extra_flags & ~_TRANSACTION_FLAGS_MASK_PUBLIC) == 0); |
| 2285 |
|
| 2286 |
if (mode == JOB_ISOLATE && type != JOB_START) |
| 2287 |
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Isolate is only valid for start."); |
| 2288 |
|
| 2289 |
if (mode == JOB_ISOLATE && !unit->allow_isolate) |
| 2290 |
return sd_bus_error_set(reterr_error, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated."); |
| 2291 |
|
| 2292 |
if (mode == JOB_TRIGGERING && type != JOB_STOP) |
| 2293 |
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "--job-mode=triggering is only valid for stop."); |
| 2294 |
|
| 2295 |
if (mode == JOB_RESTART_DEPENDENCIES && type != JOB_START) |
| 2296 |
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "--job-mode=restart-dependencies is only valid for start."); |
| 2297 |
|
| 2298 |
tr = transaction_new(mode == JOB_REPLACE_IRREVERSIBLY, ++m->last_transaction_id); |
| 2299 |
if (!tr) |
| 2300 |
return -ENOMEM; |
| 2301 |
|
| 2302 |
LOG_CONTEXT_PUSHF("TRANSACTION_ID=%" PRIu64, tr->id); |
| 2303 |
|
| 2304 |
log_unit_debug(unit, "Trying to enqueue job %s/%s/%s", unit->id, job_type_to_string(type), job_mode_to_string(mode)); |
| 2305 |
|
| 2306 |
type = job_type_collapse(type, unit); |
| 2307 |
|
| 2308 |
r = transaction_add_job_and_dependencies( |
| 2309 |
tr, |
| 2310 |
type, |
| 2311 |
unit, |
| 2312 |
/* by= */ NULL, |
| 2313 |
TRANSACTION_MATTERS | |
| 2314 |
(IN_SET(mode, JOB_IGNORE_DEPENDENCIES, JOB_IGNORE_REQUIREMENTS) ? TRANSACTION_IGNORE_REQUIREMENTS : 0) | |
| 2315 |
(mode == JOB_IGNORE_DEPENDENCIES ? TRANSACTION_IGNORE_ORDER : 0) | |
| 2316 |
(mode == JOB_RESTART_DEPENDENCIES ? TRANSACTION_PROPAGATE_START_AS_RESTART : 0) | |
| 2317 |
extra_flags, |
| 2318 |
reterr_error); |
| 2319 |
if (r < 0) |
| 2320 |
return r; |
| 2321 |
|
| 2322 |
if (mode == JOB_ISOLATE) { |
| 2323 |
r = transaction_add_isolate_jobs(tr, m); |
| 2324 |
if (r < 0) |
| 2325 |
return r; |
| 2326 |
} |
| 2327 |
|
| 2328 |
if (mode == JOB_TRIGGERING) { |
| 2329 |
r = transaction_add_triggering_jobs(tr, unit); |
| 2330 |
if (r < 0) |
| 2331 |
return r; |
| 2332 |
} |
| 2333 |
|
| 2334 |
r = transaction_activate(tr, m, mode, affected_jobs, reterr_error); |
| 2335 |
if (r < 0) |
| 2336 |
return r; |
| 2337 |
|
| 2338 |
log_unit_debug(unit, |
| 2339 |
"Enqueued job %s/%s as %u", unit->id, |
| 2340 |
job_type_to_string(type), (unsigned) tr->anchor_job->id); |
| 2341 |
|
| 2342 |
if (ret) |
| 2343 |
*ret = tr->anchor_job; |
| 2344 |
|
| 2345 |
tr = transaction_free(tr); |
| 2346 |
return 0; |
| 2347 |
} |
| 2348 |
|
| 2349 |
int manager_add_job( |
| 2350 |
Manager *m, |
| 2351 |
JobType type, |
| 2352 |
Unit *unit, |
| 2353 |
JobMode mode, |
| 2354 |
sd_bus_error *reterr_error, |
| 2355 |
Job **ret) { |
| 2356 |
|
| 2357 |
return manager_add_job_full(m, type, unit, mode, 0, NULL, reterr_error, ret); |
| 2358 |
} |
| 2359 |
|
| 2360 |
int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, Set *affected_jobs, sd_bus_error *e, Job **ret) { |
| 2361 |
Unit *unit = NULL; /* just to appease gcc, initialization is not really necessary */ |
| 2362 |
int r; |
| 2363 |
|
| 2364 |
assert(m); |
| 2365 |
assert(type < _JOB_TYPE_MAX); |
| 2366 |
assert(name); |
| 2367 |
assert(mode < _JOB_MODE_MAX); |
| 2368 |
|
| 2369 |
r = manager_load_unit(m, name, NULL, NULL, &unit); |
| 2370 |
if (r < 0) |
| 2371 |
return r; |
| 2372 |
assert(unit); |
| 2373 |
|
| 2374 |
return manager_add_job_full(m, type, unit, mode, /* extra_flags= */ 0, affected_jobs, e, ret); |
| 2375 |
} |
| 2376 |
|
| 2377 |
int manager_add_job_by_name_and_warn(Manager *m, JobType type, const char *name, JobMode mode, Set *affected_jobs, Job **ret) { |
| 2378 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 2379 |
int r; |
| 2380 |
|
| 2381 |
assert(m); |
| 2382 |
assert(type < _JOB_TYPE_MAX); |
| 2383 |
assert(name); |
| 2384 |
assert(mode < _JOB_MODE_MAX); |
| 2385 |
|
| 2386 |
r = manager_add_job_by_name(m, type, name, mode, affected_jobs, &error, ret); |
| 2387 |
if (r < 0) |
| 2388 |
return log_warning_errno(r, "Failed to enqueue %s job for %s: %s", job_mode_to_string(mode), name, bus_error_message(&error, r)); |
| 2389 |
|
| 2390 |
return r; |
| 2391 |
} |
| 2392 |
|
| 2393 |
int manager_propagate_reload(Manager *m, Unit *unit, JobMode mode, sd_bus_error *e) { |
| 2394 |
_cleanup_(transaction_abort_and_freep) Transaction *tr = NULL; |
| 2395 |
int r; |
| 2396 |
|
| 2397 |
assert(m); |
| 2398 |
assert(unit); |
| 2399 |
assert(mode < _JOB_MODE_MAX); |
| 2400 |
assert(mode != JOB_ISOLATE); /* Isolate is only valid for start */ |
| 2401 |
|
| 2402 |
tr = transaction_new(mode == JOB_REPLACE_IRREVERSIBLY, ++m->last_transaction_id); |
| 2403 |
if (!tr) |
| 2404 |
return -ENOMEM; |
| 2405 |
|
| 2406 |
LOG_CONTEXT_PUSHF("TRANSACTION_ID=%" PRIu64, tr->id); |
| 2407 |
|
| 2408 |
/* We need an anchor job */ |
| 2409 |
r = transaction_add_job_and_dependencies(tr, JOB_NOP, unit, NULL, TRANSACTION_IGNORE_REQUIREMENTS|TRANSACTION_IGNORE_ORDER, e); |
| 2410 |
if (r < 0) |
| 2411 |
return r; |
| 2412 |
|
| 2413 |
/* Failure in adding individual dependencies is ignored, so this always succeeds. */ |
| 2414 |
transaction_add_propagate_reload_jobs( |
| 2415 |
tr, |
| 2416 |
unit, |
| 2417 |
tr->anchor_job, |
| 2418 |
mode == JOB_IGNORE_DEPENDENCIES ? TRANSACTION_IGNORE_ORDER : 0); |
| 2419 |
|
| 2420 |
/* Only activate the transaction if it contains jobs other than NOP anchor. |
| 2421 |
* Short-circuiting here avoids unnecessary processing, such as emitting D-Bus signals. */ |
| 2422 |
if (hashmap_size(tr->jobs) <= 1) |
| 2423 |
return 0; |
| 2424 |
|
| 2425 |
r = transaction_activate(tr, m, mode, NULL, e); |
| 2426 |
if (r < 0) |
| 2427 |
return r; |
| 2428 |
|
| 2429 |
tr = transaction_free(tr); |
| 2430 |
return 0; |
| 2431 |
} |
| 2432 |
|
| 2433 |
Job *manager_get_job(Manager *m, uint32_t id) { |
| 2434 |
assert(m); |
| 2435 |
|
| 2436 |
return hashmap_get(m->jobs, UINT32_TO_PTR(id)); |
| 2437 |
} |
| 2438 |
|
| 2439 |
Unit *manager_get_unit(Manager *m, const char *name) { |
| 2440 |
assert(m); |
| 2441 |
assert(name); |
| 2442 |
|
| 2443 |
return hashmap_get(m->units, name); |
| 2444 |
} |
| 2445 |
|
| 2446 |
static int manager_dispatch_target_deps_queue(Manager *m) { |
| 2447 |
Unit *u; |
| 2448 |
int r = 0; |
| 2449 |
|
| 2450 |
assert(m); |
| 2451 |
|
| 2452 |
while ((u = LIST_POP(target_deps_queue, m->target_deps_queue))) { |
| 2453 |
_cleanup_free_ Unit **targets = NULL; |
| 2454 |
int n_targets; |
| 2455 |
|
| 2456 |
assert(u->in_target_deps_queue); |
| 2457 |
|
| 2458 |
u->in_target_deps_queue = false; |
| 2459 |
|
| 2460 |
/* Take an "atomic" snapshot of dependencies here, as the call below will likely modify the |
| 2461 |
* dependencies, and we can't have it that hash tables we iterate through are modified while |
| 2462 |
* we are iterating through them. */ |
| 2463 |
n_targets = unit_get_dependency_array(u, UNIT_ATOM_DEFAULT_TARGET_DEPENDENCIES, &targets); |
| 2464 |
if (n_targets < 0) |
| 2465 |
return n_targets; |
| 2466 |
|
| 2467 |
FOREACH_ARRAY(i, targets, n_targets) { |
| 2468 |
r = unit_add_default_target_dependency(u, *i); |
| 2469 |
if (r < 0) |
| 2470 |
return r; |
| 2471 |
} |
| 2472 |
} |
| 2473 |
|
| 2474 |
return r; |
| 2475 |
} |
| 2476 |
|
| 2477 |
unsigned manager_dispatch_load_queue(Manager *m) { |
| 2478 |
Unit *u; |
| 2479 |
unsigned n = 0; |
| 2480 |
|
| 2481 |
assert(m); |
| 2482 |
|
| 2483 |
/* Make sure we are not run recursively */ |
| 2484 |
if (m->dispatching_load_queue) |
| 2485 |
return 0; |
| 2486 |
|
| 2487 |
m->dispatching_load_queue = true; |
| 2488 |
|
| 2489 |
/* Dispatches the load queue. Takes a unit from the queue and |
| 2490 |
* tries to load its data until the queue is empty */ |
| 2491 |
|
| 2492 |
while ((u = m->load_queue)) { |
| 2493 |
assert(u->in_load_queue); |
| 2494 |
|
| 2495 |
unit_load(u); |
| 2496 |
n++; |
| 2497 |
} |
| 2498 |
|
| 2499 |
m->dispatching_load_queue = false; |
| 2500 |
|
| 2501 |
/* Dispatch the units waiting for their target dependencies to be added now, as all targets that we know about |
| 2502 |
* should be loaded and have aliases resolved */ |
| 2503 |
(void) manager_dispatch_target_deps_queue(m); |
| 2504 |
|
| 2505 |
return n; |
| 2506 |
} |
| 2507 |
|
| 2508 |
bool manager_unit_cache_should_retry_load(Unit *u) { |
| 2509 |
assert(u); |
| 2510 |
|
| 2511 |
/* Automatic reloading from disk only applies to units which were not found sometime in the past, and |
| 2512 |
* the not-found stub is kept pinned in the unit graph by dependencies. For units that were |
| 2513 |
* previously loaded, we don't do automatic reloading, and daemon-reload is necessary to update. */ |
| 2514 |
if (u->load_state != UNIT_NOT_FOUND) |
| 2515 |
return false; |
| 2516 |
|
| 2517 |
/* The cache has been updated since the last time we tried to load the unit. There might be new |
| 2518 |
* fragment paths to read. */ |
| 2519 |
if (u->manager->unit_cache_timestamp_hash != u->fragment_not_found_timestamp_hash) |
| 2520 |
return true; |
| 2521 |
|
| 2522 |
/* The cache needs to be updated because there are modifications on disk. */ |
| 2523 |
return !lookup_paths_timestamp_hash_same(&u->manager->lookup_paths, u->manager->unit_cache_timestamp_hash, NULL); |
| 2524 |
} |
| 2525 |
|
| 2526 |
int manager_load_unit_prepare( |
| 2527 |
Manager *m, |
| 2528 |
const char *name, |
| 2529 |
const char *path, |
| 2530 |
sd_bus_error *e, |
| 2531 |
Unit **ret) { |
| 2532 |
|
| 2533 |
_cleanup_(unit_freep) Unit *cleanup_unit = NULL; |
| 2534 |
_cleanup_free_ char *nbuf = NULL; |
| 2535 |
int r; |
| 2536 |
|
| 2537 |
assert(m); |
| 2538 |
assert(ret); |
| 2539 |
assert(name || path); |
| 2540 |
|
| 2541 |
/* This will prepare the unit for loading, but not actually load anything from disk. */ |
| 2542 |
|
| 2543 |
if (path && !path_is_absolute(path)) |
| 2544 |
return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path); |
| 2545 |
|
| 2546 |
if (!name) { |
| 2547 |
r = path_extract_filename(path, &nbuf); |
| 2548 |
if (r < 0) |
| 2549 |
return r; |
| 2550 |
if (r == O_DIRECTORY) |
| 2551 |
return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Path '%s' refers to directory, refusing.", path); |
| 2552 |
|
| 2553 |
name = nbuf; |
| 2554 |
} |
| 2555 |
|
| 2556 |
UnitType t = unit_name_to_type(name); |
| 2557 |
|
| 2558 |
if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) { |
| 2559 |
if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) |
| 2560 |
return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is missing the instance name.", name); |
| 2561 |
|
| 2562 |
return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is not valid.", name); |
| 2563 |
} |
| 2564 |
|
| 2565 |
Unit *unit = manager_get_unit(m, name); |
| 2566 |
if (unit) { |
| 2567 |
/* The time-based cache allows new units to be started without daemon-reload, |
| 2568 |
* but if they are already referenced (because of dependencies or ordering) |
| 2569 |
* then we have to force a load of the fragment. As an optimization, check |
| 2570 |
* first if anything in the usual paths was modified since the last time |
| 2571 |
* the cache was loaded. Also check if the last time an attempt to load the |
| 2572 |
* unit was made was before the most recent cache refresh, so that we know |
| 2573 |
* we need to try again — even if the cache is current, it might have been |
| 2574 |
* updated in a different context before we had a chance to retry loading |
| 2575 |
* this particular unit. */ |
| 2576 |
if (manager_unit_cache_should_retry_load(unit)) |
| 2577 |
unit->load_state = UNIT_STUB; |
| 2578 |
else { |
| 2579 |
*ret = unit; |
| 2580 |
return 0; /* The unit was already loaded */ |
| 2581 |
} |
| 2582 |
} else { |
| 2583 |
unit = cleanup_unit = unit_new(m, unit_vtable[t]->object_size); |
| 2584 |
if (!unit) |
| 2585 |
return -ENOMEM; |
| 2586 |
} |
| 2587 |
|
| 2588 |
if (path) { |
| 2589 |
r = free_and_strdup(&unit->fragment_path, path); |
| 2590 |
if (r < 0) |
| 2591 |
return r; |
| 2592 |
} |
| 2593 |
|
| 2594 |
r = unit_add_name(unit, name); |
| 2595 |
if (r < 0) |
| 2596 |
return r; |
| 2597 |
|
| 2598 |
unit_add_to_load_queue(unit); |
| 2599 |
unit_add_to_dbus_queue(unit); |
| 2600 |
unit_add_to_gc_queue(unit); |
| 2601 |
|
| 2602 |
*ret = unit; |
| 2603 |
TAKE_PTR(cleanup_unit); |
| 2604 |
|
| 2605 |
return 1; /* The unit was added the load queue */ |
| 2606 |
} |
| 2607 |
|
| 2608 |
int manager_load_unit( |
| 2609 |
Manager *m, |
| 2610 |
const char *name, |
| 2611 |
const char *path, |
| 2612 |
sd_bus_error *e, |
| 2613 |
Unit **ret) { |
| 2614 |
int r; |
| 2615 |
|
| 2616 |
assert(m); |
| 2617 |
assert(ret); |
| 2618 |
|
| 2619 |
/* This will load the unit config, but not actually start any services or anything. */ |
| 2620 |
|
| 2621 |
r = manager_load_unit_prepare(m, name, path, e, ret); |
| 2622 |
if (r <= 0) |
| 2623 |
return r; |
| 2624 |
|
| 2625 |
/* Unit was newly loaded */ |
| 2626 |
manager_dispatch_load_queue(m); |
| 2627 |
*ret = unit_follow_merge(*ret); |
| 2628 |
return 0; |
| 2629 |
} |
| 2630 |
|
| 2631 |
int manager_load_startable_unit_or_warn( |
| 2632 |
Manager *m, |
| 2633 |
const char *name, |
| 2634 |
const char *path, |
| 2635 |
Unit **ret) { |
| 2636 |
|
| 2637 |
/* Load a unit, make sure it loaded fully and is not masked. */ |
| 2638 |
|
| 2639 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 2640 |
Unit *unit; |
| 2641 |
int r; |
| 2642 |
|
| 2643 |
assert(ret); |
| 2644 |
|
| 2645 |
r = manager_load_unit(m, name, path, &error, &unit); |
| 2646 |
if (r < 0) |
| 2647 |
return log_error_errno(r, "Failed to load %s %s: %s", |
| 2648 |
name ? "unit" : "unit file", name ?: path, |
| 2649 |
bus_error_message(&error, r)); |
| 2650 |
|
| 2651 |
r = bus_unit_validate_load_state(unit, &error); |
| 2652 |
if (r < 0) |
| 2653 |
return log_error_errno(r, "%s", bus_error_message(&error, r)); |
| 2654 |
|
| 2655 |
*ret = unit; |
| 2656 |
return 0; |
| 2657 |
} |
| 2658 |
|
| 2659 |
void manager_clear_jobs(Manager *m) { |
| 2660 |
Job *j; |
| 2661 |
|
| 2662 |
assert(m); |
| 2663 |
|
| 2664 |
while ((j = hashmap_first(m->jobs))) |
| 2665 |
/* No need to recurse. We're cancelling all jobs. */ |
| 2666 |
job_finish_and_invalidate(j, JOB_CANCELED, false, false); |
| 2667 |
} |
| 2668 |
|
| 2669 |
void manager_unwatch_pidref(Manager *m, const PidRef *pid) { |
| 2670 |
assert(m); |
| 2671 |
|
| 2672 |
for (;;) { |
| 2673 |
Unit *u; |
| 2674 |
|
| 2675 |
u = manager_get_unit_by_pidref_watching(m, pid); |
| 2676 |
if (!u) |
| 2677 |
break; |
| 2678 |
|
| 2679 |
unit_unwatch_pidref(u, pid); |
| 2680 |
} |
| 2681 |
} |
| 2682 |
|
| 2683 |
static int manager_dispatch_run_queue(sd_event_source *source, void *userdata) { |
| 2684 |
Manager *m = ASSERT_PTR(userdata); |
| 2685 |
Job *j; |
| 2686 |
|
| 2687 |
assert(source); |
| 2688 |
|
| 2689 |
while ((j = prioq_peek(m->run_queue))) { |
| 2690 |
assert(j->installed); |
| 2691 |
assert(j->in_run_queue); |
| 2692 |
|
| 2693 |
(void) job_run_and_invalidate(j); |
| 2694 |
} |
| 2695 |
|
| 2696 |
if (m->n_running_jobs > 0) |
| 2697 |
manager_watch_jobs_in_progress(m); |
| 2698 |
|
| 2699 |
if (m->n_on_console > 0) |
| 2700 |
manager_watch_idle_pipe(m); |
| 2701 |
|
| 2702 |
return 1; |
| 2703 |
} |
| 2704 |
|
| 2705 |
void manager_trigger_run_queue(Manager *m) { |
| 2706 |
int r; |
| 2707 |
|
| 2708 |
assert(m); |
| 2709 |
|
| 2710 |
r = sd_event_source_set_enabled( |
| 2711 |
m->run_queue_event_source, |
| 2712 |
prioq_isempty(m->run_queue) ? SD_EVENT_OFF : SD_EVENT_ONESHOT); |
| 2713 |
if (r < 0) |
| 2714 |
log_warning_errno(r, "Failed to enable job run queue event source, ignoring: %m"); |
| 2715 |
} |
| 2716 |
|
| 2717 |
static unsigned manager_dispatch_dbus_queue(Manager *m) { |
| 2718 |
unsigned n = 0, budget; |
| 2719 |
Unit *u; |
| 2720 |
Job *j; |
| 2721 |
|
| 2722 |
assert(m); |
| 2723 |
|
| 2724 |
/* When we are reloading, let's not wait with generating signals, since we need to exit the manager as quickly |
| 2725 |
* as we can. There's no point in throttling generation of signals in that case. */ |
| 2726 |
if (MANAGER_IS_RELOADING(m) || m->send_reloading_done || m->pending_reload_message_dbus || m->pending_reload_message_vl) |
| 2727 |
budget = UINT_MAX; /* infinite budget in this case */ |
| 2728 |
else { |
| 2729 |
/* Anything to do at all? */ |
| 2730 |
if (!m->dbus_unit_queue && !m->dbus_job_queue) |
| 2731 |
return 0; |
| 2732 |
|
| 2733 |
/* Do we have overly many messages queued at the moment? If so, let's not enqueue more on top, let's |
| 2734 |
* sit this cycle out, and process things in a later cycle when the queues got a bit emptier. */ |
| 2735 |
if (manager_bus_n_queued_write(m) > MANAGER_BUS_BUSY_THRESHOLD) |
| 2736 |
return 0; |
| 2737 |
|
| 2738 |
/* Only process a certain number of units/jobs per event loop iteration. Even if the bus queue wasn't |
| 2739 |
* overly full before this call we shouldn't increase it in size too wildly in one step, and we |
| 2740 |
* shouldn't monopolize CPU time with generating these messages. Note the difference in counting of |
| 2741 |
* this "budget" and the "threshold" above: the "budget" is decreased only once per generated message, |
| 2742 |
* regardless how many buses/direct connections it is enqueued on, while the "threshold" is applied to |
| 2743 |
* each queued instance of bus message, i.e. if the same message is enqueued to five buses/direct |
| 2744 |
* connections it will be counted five times. This difference in counting ("references" |
| 2745 |
* vs. "instances") is primarily a result of the fact that it's easier to implement it this way, |
| 2746 |
* however it also reflects the thinking that the "threshold" should put a limit on used queue memory, |
| 2747 |
* i.e. space, while the "budget" should put a limit on time. Also note that the "threshold" is |
| 2748 |
* currently chosen much higher than the "budget". */ |
| 2749 |
budget = MANAGER_BUS_MESSAGE_BUDGET; |
| 2750 |
} |
| 2751 |
|
| 2752 |
while (budget != 0 && (u = m->dbus_unit_queue)) { |
| 2753 |
|
| 2754 |
assert(u->in_dbus_queue); |
| 2755 |
|
| 2756 |
bus_unit_send_change_signal(u); |
| 2757 |
varlink_unit_send_change_signal(u); |
| 2758 |
n++; |
| 2759 |
|
| 2760 |
if (budget != UINT_MAX) |
| 2761 |
budget--; |
| 2762 |
} |
| 2763 |
|
| 2764 |
while (budget != 0 && (j = m->dbus_job_queue)) { |
| 2765 |
assert(j->in_dbus_queue); |
| 2766 |
|
| 2767 |
bus_job_send_change_signal(j); |
| 2768 |
varlink_job_send_change_signal(j); |
| 2769 |
n++; |
| 2770 |
|
| 2771 |
if (budget != UINT_MAX) |
| 2772 |
budget--; |
| 2773 |
} |
| 2774 |
|
| 2775 |
if (m->send_reloading_done) { |
| 2776 |
m->send_reloading_done = false; |
| 2777 |
bus_manager_send_reloading(m, false); |
| 2778 |
n++; |
| 2779 |
} |
| 2780 |
|
| 2781 |
if (m->pending_reload_message_dbus) { |
| 2782 |
bus_send_pending_reload_message(m); |
| 2783 |
n++; |
| 2784 |
} |
| 2785 |
|
| 2786 |
if (m->pending_reload_message_vl) { |
| 2787 |
manager_varlink_send_pending_reload_message(m); |
| 2788 |
n++; |
| 2789 |
} |
| 2790 |
|
| 2791 |
return n; |
| 2792 |
} |
| 2793 |
|
| 2794 |
static bool manager_process_barrier_fd(char * const *tags, FDSet *fds) { |
| 2795 |
|
| 2796 |
/* nothing else must be sent when using BARRIER=1 */ |
| 2797 |
if (strv_contains(tags, "BARRIER=1")) { |
| 2798 |
if (strv_length(tags) != 1) |
| 2799 |
log_warning("Extra notification messages sent with BARRIER=1, ignoring everything."); |
| 2800 |
else if (fdset_size(fds) != 1) |
| 2801 |
log_warning("Got incorrect number of fds with BARRIER=1, closing them."); |
| 2802 |
|
| 2803 |
/* Drop the message if BARRIER=1 was found */ |
| 2804 |
return true; |
| 2805 |
} |
| 2806 |
|
| 2807 |
return false; |
| 2808 |
} |
| 2809 |
|
| 2810 |
static void manager_invoke_notify_message( |
| 2811 |
Manager *m, |
| 2812 |
Unit *u, |
| 2813 |
PidRef *pidref, |
| 2814 |
const struct ucred *ucred, |
| 2815 |
char * const *tags, |
| 2816 |
FDSet *fds) { |
| 2817 |
|
| 2818 |
assert(m); |
| 2819 |
assert(u); |
| 2820 |
assert(pidref_is_set(pidref)); |
| 2821 |
assert(ucred); |
| 2822 |
assert(pidref->pid == ucred->pid); |
| 2823 |
assert(tags); |
| 2824 |
|
| 2825 |
if (u->notifygen == m->notifygen) /* Already invoked on this same unit in this same iteration? */ |
| 2826 |
return; |
| 2827 |
u->notifygen = m->notifygen; |
| 2828 |
|
| 2829 |
if (UNIT_VTABLE(u)->notify_message) |
| 2830 |
UNIT_VTABLE(u)->notify_message(u, pidref, ucred, tags, fds); |
| 2831 |
|
| 2832 |
else if (DEBUG_LOGGING) { |
| 2833 |
_cleanup_free_ char *joined = strv_join(tags, ", "); |
| 2834 |
char buf[CELLESCAPE_DEFAULT_LENGTH]; |
| 2835 |
|
| 2836 |
log_unit_debug(u, "Got notification message from unexpected unit type, ignoring: %s", |
| 2837 |
joined ? cellescape(buf, sizeof(buf), joined) : "(null)"); |
| 2838 |
} |
| 2839 |
} |
| 2840 |
|
| 2841 |
static int manager_get_units_for_pidref(Manager *m, const PidRef *pidref, Unit ***ret_units) { |
| 2842 |
/* Determine array of every unit that is interested in the specified process */ |
| 2843 |
|
| 2844 |
assert(m); |
| 2845 |
assert(pidref_is_set(pidref)); |
| 2846 |
|
| 2847 |
Unit *u1, *u2, **array; |
| 2848 |
u1 = manager_get_unit_by_pidref_cgroup(m, pidref); |
| 2849 |
u2 = hashmap_get(m->watch_pids, pidref); |
| 2850 |
array = hashmap_get(m->watch_pids_more, pidref); |
| 2851 |
|
| 2852 |
size_t n = 0; |
| 2853 |
if (u1) |
| 2854 |
n++; |
| 2855 |
if (u2) |
| 2856 |
n++; |
| 2857 |
if (array) |
| 2858 |
for (size_t j = 0; array[j]; j++) |
| 2859 |
n++; |
| 2860 |
|
| 2861 |
assert(n <= INT_MAX); /* Make sure we can reasonably return the counter as "int" */ |
| 2862 |
|
| 2863 |
if (ret_units) { |
| 2864 |
_cleanup_free_ Unit **units = NULL; |
| 2865 |
|
| 2866 |
if (n > 0) { |
| 2867 |
units = new(Unit*, n + 1); |
| 2868 |
if (!units) |
| 2869 |
return -ENOMEM; |
| 2870 |
|
| 2871 |
/* We return a dense array, and put the "main" unit first, i.e. unit in whose cgroup |
| 2872 |
* the process currently is. Note that we do not bother with filtering duplicates |
| 2873 |
* here. */ |
| 2874 |
|
| 2875 |
size_t i = 0; |
| 2876 |
if (u1) |
| 2877 |
units[i++] = u1; |
| 2878 |
if (u2) |
| 2879 |
units[i++] = u2; |
| 2880 |
if (array) |
| 2881 |
for (size_t j = 0; array[j]; j++) |
| 2882 |
units[i++] = array[j]; |
| 2883 |
assert(i == n); |
| 2884 |
|
| 2885 |
units[i] = NULL; /* end array in an extra NULL */ |
| 2886 |
} |
| 2887 |
|
| 2888 |
*ret_units = TAKE_PTR(units); |
| 2889 |
} |
| 2890 |
|
| 2891 |
return (int) n; |
| 2892 |
} |
| 2893 |
|
| 2894 |
static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
| 2895 |
Manager *m = ASSERT_PTR(userdata); |
| 2896 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 2897 |
struct ucred ucred; |
| 2898 |
_cleanup_(fdset_free_asyncp) FDSet *fds = NULL; |
| 2899 |
int r; |
| 2900 |
|
| 2901 |
assert(m->notify_fd == fd); |
| 2902 |
|
| 2903 |
if (revents != EPOLLIN) { |
| 2904 |
log_warning("Got unexpected poll event for notify fd."); |
| 2905 |
return 0; |
| 2906 |
} |
| 2907 |
|
| 2908 |
_cleanup_strv_free_ char **tags = NULL; |
| 2909 |
r = notify_recv_with_fds_strv(m->notify_fd, &tags, &ucred, &pidref, &fds); |
| 2910 |
if (r == -EAGAIN) |
| 2911 |
return 0; |
| 2912 |
if (r < 0) |
| 2913 |
/* If this is any other, real error, then stop processing this socket. This of course means |
| 2914 |
* we won't take notification messages anymore, but that's still better than busy looping: |
| 2915 |
* being woken up over and over again, but being unable to actually read the message from the |
| 2916 |
* socket. */ |
| 2917 |
return r; |
| 2918 |
|
| 2919 |
/* Possibly a barrier fd, let's see. */ |
| 2920 |
if (manager_process_barrier_fd(tags, fds)) { |
| 2921 |
log_debug("Received barrier notification message from PID " PID_FMT ".", pidref.pid); |
| 2922 |
return 0; |
| 2923 |
} |
| 2924 |
|
| 2925 |
/* Increase the generation counter used for filtering out duplicate unit invocations. */ |
| 2926 |
m->notifygen++; |
| 2927 |
|
| 2928 |
/* Notify every unit that might be interested, which might be multiple. */ |
| 2929 |
_cleanup_free_ Unit **array = NULL; |
| 2930 |
|
| 2931 |
int n_array = manager_get_units_for_pidref(m, &pidref, &array); |
| 2932 |
if (n_array < 0) { |
| 2933 |
log_warning_errno(n_array, "Failed to determine units for PID " PID_FMT ", ignoring: %m", pidref.pid); |
| 2934 |
return 0; |
| 2935 |
} |
| 2936 |
if (n_array == 0) |
| 2937 |
log_debug("Cannot find unit for notify message of PID "PID_FMT", ignoring.", pidref.pid); |
| 2938 |
else |
| 2939 |
/* And now invoke the per-unit callbacks. Note that manager_invoke_notify_message() will handle |
| 2940 |
* duplicate units – making sure we only invoke each unit's handler once. */ |
| 2941 |
FOREACH_ARRAY(u, array, n_array) |
| 2942 |
manager_invoke_notify_message(m, *u, &pidref, &ucred, tags, fds); |
| 2943 |
|
| 2944 |
if (!fdset_isempty(fds)) |
| 2945 |
log_warning("Got extra auxiliary fds with notification message, closing them."); |
| 2946 |
|
| 2947 |
return 0; |
| 2948 |
} |
| 2949 |
|
| 2950 |
static void manager_invoke_sigchld_event( |
| 2951 |
Manager *m, |
| 2952 |
Unit *u, |
| 2953 |
const siginfo_t *si) { |
| 2954 |
|
| 2955 |
assert(m); |
| 2956 |
assert(u); |
| 2957 |
assert(si); |
| 2958 |
|
| 2959 |
/* Already invoked the handler of this unit in this iteration? Then don't process this again */ |
| 2960 |
if (u->sigchldgen == m->sigchldgen) |
| 2961 |
return; |
| 2962 |
u->sigchldgen = m->sigchldgen; |
| 2963 |
|
| 2964 |
log_unit_debug(u, "Child "PID_FMT" belongs to %s.", si->si_pid, u->id); |
| 2965 |
unit_unwatch_pidref(u, &PIDREF_MAKE_FROM_PID(si->si_pid)); |
| 2966 |
|
| 2967 |
if (UNIT_VTABLE(u)->sigchld_event) |
| 2968 |
UNIT_VTABLE(u)->sigchld_event(u, si->si_pid, si->si_code, si->si_status); |
| 2969 |
} |
| 2970 |
|
| 2971 |
static int manager_dispatch_sigchld(sd_event_source *source, void *userdata) { |
| 2972 |
Manager *m = ASSERT_PTR(userdata); |
| 2973 |
siginfo_t si = {}; |
| 2974 |
int r; |
| 2975 |
|
| 2976 |
assert(source); |
| 2977 |
|
| 2978 |
/* First we call waitid() for a PID and do not reap the zombie. That way we can still access |
| 2979 |
* /proc/$PID for it while it is a zombie. */ |
| 2980 |
|
| 2981 |
if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) { |
| 2982 |
|
| 2983 |
if (errno != ECHILD) |
| 2984 |
log_error_errno(errno, "Failed to peek for child with waitid(), ignoring: %m"); |
| 2985 |
|
| 2986 |
goto turn_off; |
| 2987 |
} |
| 2988 |
|
| 2989 |
if (si.si_pid <= 0) |
| 2990 |
goto turn_off; |
| 2991 |
|
| 2992 |
if (SIGINFO_CODE_IS_DEAD(si.si_code)) { |
| 2993 |
_cleanup_free_ char *name = NULL; |
| 2994 |
(void) pid_get_comm(si.si_pid, &name); |
| 2995 |
|
| 2996 |
log_debug("Child "PID_FMT" (%s) died (code=%s, status=%i/%s)", |
| 2997 |
si.si_pid, strna(name), |
| 2998 |
sigchld_code_to_string(si.si_code), |
| 2999 |
si.si_status, |
| 3000 |
strna(si.si_code == CLD_EXITED |
| 3001 |
? exit_status_to_string(si.si_status, EXIT_STATUS_FULL) |
| 3002 |
: signal_to_string(si.si_status))); |
| 3003 |
|
| 3004 |
/* Increase the generation counter used for filtering out duplicate unit invocations */ |
| 3005 |
m->sigchldgen++; |
| 3006 |
|
| 3007 |
/* We look this up by a PidRef that only consists of the PID. After all we couldn't create a |
| 3008 |
* pidfd here any more even if we wanted (since the process just exited). */ |
| 3009 |
PidRef pidref = PIDREF_MAKE_FROM_PID(si.si_pid); |
| 3010 |
|
| 3011 |
/* And now figure out the units this belongs to, there might be multiple... */ |
| 3012 |
_cleanup_free_ Unit **array = NULL; |
| 3013 |
int n_array = manager_get_units_for_pidref(m, &pidref, &array); |
| 3014 |
if (n_array < 0) |
| 3015 |
log_warning_errno(n_array, "Failed to get units for process " PID_FMT ", ignoring: %m", si.si_pid); |
| 3016 |
else if (n_array == 0) |
| 3017 |
log_debug("Got SIGCHLD for process " PID_FMT " we weren't interested in, ignoring.", si.si_pid); |
| 3018 |
else { |
| 3019 |
/* We check for an OOM condition, in case we got SIGCHLD before the OOM notification. |
| 3020 |
* We only do this for the cgroup the PID belonged to, which is the f */ |
| 3021 |
(void) unit_check_oom(array[0]); |
| 3022 |
|
| 3023 |
/* We check if systemd-oomd performed a kill so that we log and notify appropriately */ |
| 3024 |
(void) unit_check_oomd_kill(array[0]); |
| 3025 |
|
| 3026 |
/* Finally, execute them all. Note that the array might contain duplicates, but that's fine, |
| 3027 |
* manager_invoke_sigchld_event() will ensure we only invoke the handlers once for each |
| 3028 |
* iteration. */ |
| 3029 |
FOREACH_ARRAY(u, array, n_array) |
| 3030 |
manager_invoke_sigchld_event(m, *u, &si); |
| 3031 |
} |
| 3032 |
} |
| 3033 |
|
| 3034 |
/* And now, we actually reap the zombie. */ |
| 3035 |
if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) { |
| 3036 |
log_error_errno(errno, "Failed to dequeue child, ignoring: %m"); |
| 3037 |
return 0; |
| 3038 |
} |
| 3039 |
|
| 3040 |
return 0; |
| 3041 |
|
| 3042 |
turn_off: |
| 3043 |
/* All children processed for now, turn off event source */ |
| 3044 |
|
| 3045 |
r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_OFF); |
| 3046 |
if (r < 0) |
| 3047 |
return log_error_errno(r, "Failed to disable SIGCHLD event source: %m"); |
| 3048 |
|
| 3049 |
return 0; |
| 3050 |
} |
| 3051 |
|
| 3052 |
static void manager_start_special(Manager *m, const char *name, JobMode mode) { |
| 3053 |
Job *job; |
| 3054 |
|
| 3055 |
if (manager_add_job_by_name_and_warn(m, JOB_START, name, mode, NULL, &job) < 0) |
| 3056 |
return; |
| 3057 |
|
| 3058 |
const char *s = unit_status_string(job->unit, NULL); |
| 3059 |
|
| 3060 |
log_info("Activating special unit %s...", s); |
| 3061 |
|
| 3062 |
(void) sd_notifyf(/* unset_environment= */ false, |
| 3063 |
"STATUS=Activating special unit %s...", s); |
| 3064 |
m->status_ready = false; |
| 3065 |
} |
| 3066 |
|
| 3067 |
static void manager_handle_ctrl_alt_del(Manager *m) { |
| 3068 |
assert(m); |
| 3069 |
|
| 3070 |
/* If the user presses C-A-D more than 7 times within 2s, we reboot/shutdown immediately, |
| 3071 |
* unless it was disabled in system.conf. */ |
| 3072 |
|
| 3073 |
if (ratelimit_below(&m->ctrl_alt_del_ratelimit) || m->cad_burst_action == EMERGENCY_ACTION_NONE) |
| 3074 |
manager_start_special(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE_IRREVERSIBLY); |
| 3075 |
else |
| 3076 |
emergency_action( |
| 3077 |
m, |
| 3078 |
m->cad_burst_action, |
| 3079 |
EMERGENCY_ACTION_WARN, |
| 3080 |
/* reboot_arg= */ NULL, |
| 3081 |
/* exit_status= */ -1, |
| 3082 |
"Ctrl-Alt-Del was pressed more than 7 times within 2s"); |
| 3083 |
} |
| 3084 |
|
| 3085 |
static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
| 3086 |
Manager *m = ASSERT_PTR(userdata); |
| 3087 |
ssize_t n; |
| 3088 |
struct signalfd_siginfo sfsi; |
| 3089 |
int r; |
| 3090 |
|
| 3091 |
assert(m->signal_fd == fd); |
| 3092 |
|
| 3093 |
if (revents != EPOLLIN) { |
| 3094 |
log_warning("Got unexpected events from signal file descriptor."); |
| 3095 |
return 0; |
| 3096 |
} |
| 3097 |
|
| 3098 |
n = read(m->signal_fd, &sfsi, sizeof(sfsi)); |
| 3099 |
if (n < 0) { |
| 3100 |
if (ERRNO_IS_TRANSIENT(errno)) |
| 3101 |
return 0; |
| 3102 |
|
| 3103 |
/* We return an error here, which will kill this handler, |
| 3104 |
* to avoid a busy loop on read error. */ |
| 3105 |
return log_error_errno(errno, "Reading from signal fd failed: %m"); |
| 3106 |
} |
| 3107 |
if (n != sizeof(sfsi)) { |
| 3108 |
log_warning("Truncated read from signal fd (%zi bytes), ignoring!", n); |
| 3109 |
return 0; |
| 3110 |
} |
| 3111 |
|
| 3112 |
log_received_signal(sfsi.ssi_signo == SIGCHLD || |
| 3113 |
(sfsi.ssi_signo == SIGTERM && MANAGER_IS_USER(m)) |
| 3114 |
? LOG_DEBUG : LOG_INFO, |
| 3115 |
&sfsi); |
| 3116 |
|
| 3117 |
switch (sfsi.ssi_signo) { |
| 3118 |
|
| 3119 |
case SIGCHLD: |
| 3120 |
r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_ON); |
| 3121 |
if (r < 0) |
| 3122 |
log_warning_errno(r, "Failed to enable SIGCHLD event source, ignoring: %m"); |
| 3123 |
|
| 3124 |
break; |
| 3125 |
|
| 3126 |
case SIGTERM: |
| 3127 |
if (MANAGER_IS_SYSTEM(m)) { |
| 3128 |
/* This is for compatibility with the original sysvinit */ |
| 3129 |
m->objective = MANAGER_REEXECUTE; |
| 3130 |
break; |
| 3131 |
} |
| 3132 |
|
| 3133 |
_fallthrough_; |
| 3134 |
case SIGINT: |
| 3135 |
if (MANAGER_IS_SYSTEM(m)) |
| 3136 |
manager_handle_ctrl_alt_del(m); |
| 3137 |
else |
| 3138 |
manager_start_special(m, SPECIAL_EXIT_TARGET, JOB_REPLACE_IRREVERSIBLY); |
| 3139 |
break; |
| 3140 |
|
| 3141 |
case SIGWINCH: |
| 3142 |
/* This is a nop on non-init */ |
| 3143 |
if (MANAGER_IS_SYSTEM(m)) |
| 3144 |
manager_start_special(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE); |
| 3145 |
|
| 3146 |
break; |
| 3147 |
|
| 3148 |
case SIGPWR: |
| 3149 |
/* This is a nop on non-init */ |
| 3150 |
if (MANAGER_IS_SYSTEM(m)) |
| 3151 |
manager_start_special(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE); |
| 3152 |
|
| 3153 |
break; |
| 3154 |
|
| 3155 |
case SIGUSR1: |
| 3156 |
if (manager_dbus_is_running(m, false)) { |
| 3157 |
log_info("Trying to reconnect to bus..."); |
| 3158 |
|
| 3159 |
(void) bus_init_api(m); |
| 3160 |
|
| 3161 |
if (MANAGER_IS_SYSTEM(m)) |
| 3162 |
(void) bus_init_system(m); |
| 3163 |
} else |
| 3164 |
manager_start_special(m, SPECIAL_DBUS_SERVICE, JOB_REPLACE); |
| 3165 |
|
| 3166 |
break; |
| 3167 |
|
| 3168 |
case SIGUSR2: { |
| 3169 |
_cleanup_free_ char *dump = NULL; |
| 3170 |
|
| 3171 |
r = manager_get_dump_string(m, /* patterns= */ NULL, &dump); |
| 3172 |
if (r < 0) { |
| 3173 |
log_warning_errno(r, "Failed to acquire manager dump: %m"); |
| 3174 |
break; |
| 3175 |
} |
| 3176 |
|
| 3177 |
log_dump(LOG_INFO, dump); |
| 3178 |
break; |
| 3179 |
} |
| 3180 |
|
| 3181 |
case SIGHUP: |
| 3182 |
m->objective = MANAGER_RELOAD; |
| 3183 |
break; |
| 3184 |
|
| 3185 |
default: { |
| 3186 |
|
| 3187 |
if (MANAGER_IS_SYSTEM(m)) { |
| 3188 |
/* Starting SIGRTMIN+0 */ |
| 3189 |
static const struct { |
| 3190 |
const char *target; |
| 3191 |
JobMode mode; |
| 3192 |
} target_table[] = { |
| 3193 |
[0] = { SPECIAL_DEFAULT_TARGET, JOB_ISOLATE }, |
| 3194 |
[1] = { SPECIAL_RESCUE_TARGET, JOB_ISOLATE }, |
| 3195 |
[2] = { SPECIAL_EMERGENCY_TARGET, JOB_ISOLATE }, |
| 3196 |
[3] = { SPECIAL_HALT_TARGET, JOB_REPLACE_IRREVERSIBLY }, |
| 3197 |
[4] = { SPECIAL_POWEROFF_TARGET, JOB_REPLACE_IRREVERSIBLY }, |
| 3198 |
[5] = { SPECIAL_REBOOT_TARGET, JOB_REPLACE_IRREVERSIBLY }, |
| 3199 |
[6] = { SPECIAL_KEXEC_TARGET, JOB_REPLACE_IRREVERSIBLY }, |
| 3200 |
[7] = { SPECIAL_SOFT_REBOOT_TARGET, JOB_REPLACE_IRREVERSIBLY }, |
| 3201 |
}; |
| 3202 |
|
| 3203 |
/* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */ |
| 3204 |
static const ManagerObjective objective_table[] = { |
| 3205 |
[0] = MANAGER_HALT, |
| 3206 |
[1] = MANAGER_POWEROFF, |
| 3207 |
[2] = MANAGER_REBOOT, |
| 3208 |
[3] = MANAGER_KEXEC, |
| 3209 |
[4] = MANAGER_SOFT_REBOOT, |
| 3210 |
}; |
| 3211 |
|
| 3212 |
if ((int) sfsi.ssi_signo >= SIGRTMIN+0 && |
| 3213 |
(int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(target_table)) { |
| 3214 |
int idx = (int) sfsi.ssi_signo - SIGRTMIN; |
| 3215 |
manager_start_special(m, target_table[idx].target, target_table[idx].mode); |
| 3216 |
break; |
| 3217 |
} |
| 3218 |
|
| 3219 |
if ((int) sfsi.ssi_signo >= SIGRTMIN+13 && |
| 3220 |
(int) sfsi.ssi_signo < SIGRTMIN+13+(int) ELEMENTSOF(objective_table)) { |
| 3221 |
m->objective = objective_table[sfsi.ssi_signo - SIGRTMIN - 13]; |
| 3222 |
break; |
| 3223 |
} |
| 3224 |
} |
| 3225 |
|
| 3226 |
switch (sfsi.ssi_signo - SIGRTMIN) { |
| 3227 |
|
| 3228 |
case 18: { |
| 3229 |
bool generic = false; |
| 3230 |
|
| 3231 |
if (sfsi.ssi_code != SI_QUEUE) |
| 3232 |
generic = true; |
| 3233 |
else { |
| 3234 |
/* Override a few select commands by our own PID1-specific logic */ |
| 3235 |
|
| 3236 |
switch (sfsi.ssi_int) { |
| 3237 |
|
| 3238 |
case _COMMON_SIGNAL_COMMAND_LOG_LEVEL_BASE..._COMMON_SIGNAL_COMMAND_LOG_LEVEL_END: |
| 3239 |
manager_override_log_level(m, sfsi.ssi_int - _COMMON_SIGNAL_COMMAND_LOG_LEVEL_BASE); |
| 3240 |
break; |
| 3241 |
|
| 3242 |
case COMMON_SIGNAL_COMMAND_CONSOLE: |
| 3243 |
manager_override_log_target(m, LOG_TARGET_CONSOLE); |
| 3244 |
break; |
| 3245 |
|
| 3246 |
case COMMON_SIGNAL_COMMAND_JOURNAL: |
| 3247 |
manager_override_log_target(m, LOG_TARGET_JOURNAL); |
| 3248 |
break; |
| 3249 |
|
| 3250 |
case COMMON_SIGNAL_COMMAND_KMSG: |
| 3251 |
manager_override_log_target(m, LOG_TARGET_KMSG); |
| 3252 |
break; |
| 3253 |
|
| 3254 |
case COMMON_SIGNAL_COMMAND_NULL: |
| 3255 |
manager_override_log_target(m, LOG_TARGET_NULL); |
| 3256 |
break; |
| 3257 |
|
| 3258 |
case MANAGER_SIGNAL_COMMAND_DUMP_JOBS: { |
| 3259 |
_cleanup_free_ char *dump_jobs = NULL; |
| 3260 |
|
| 3261 |
r = manager_get_dump_jobs_string(m, /* patterns= */ NULL, " ", &dump_jobs); |
| 3262 |
if (r < 0) { |
| 3263 |
log_warning_errno(r, "Failed to acquire manager jobs dump: %m"); |
| 3264 |
break; |
| 3265 |
} |
| 3266 |
|
| 3267 |
log_dump(LOG_INFO, dump_jobs); |
| 3268 |
break; |
| 3269 |
} |
| 3270 |
|
| 3271 |
default: |
| 3272 |
generic = true; |
| 3273 |
} |
| 3274 |
} |
| 3275 |
|
| 3276 |
if (generic) |
| 3277 |
return sigrtmin18_handler(source, &sfsi, NULL); |
| 3278 |
|
| 3279 |
break; |
| 3280 |
} |
| 3281 |
|
| 3282 |
case 20: |
| 3283 |
manager_override_show_status(m, SHOW_STATUS_YES, "signal"); |
| 3284 |
break; |
| 3285 |
|
| 3286 |
case 21: |
| 3287 |
manager_override_show_status(m, SHOW_STATUS_NO, "signal"); |
| 3288 |
break; |
| 3289 |
|
| 3290 |
case 22: |
| 3291 |
manager_override_log_level(m, LOG_DEBUG); |
| 3292 |
break; |
| 3293 |
|
| 3294 |
case 23: |
| 3295 |
manager_restore_original_log_level(m); |
| 3296 |
break; |
| 3297 |
|
| 3298 |
case 24: |
| 3299 |
if (MANAGER_IS_USER(m)) { |
| 3300 |
m->objective = MANAGER_EXIT; |
| 3301 |
return 0; |
| 3302 |
} |
| 3303 |
|
| 3304 |
/* This is a nop on init */ |
| 3305 |
break; |
| 3306 |
|
| 3307 |
case 25: |
| 3308 |
m->objective = MANAGER_REEXECUTE; |
| 3309 |
break; |
| 3310 |
|
| 3311 |
case 26: |
| 3312 |
case 29: /* compatibility: used to be mapped to LOG_TARGET_SYSLOG_OR_KMSG */ |
| 3313 |
manager_restore_original_log_target(m); |
| 3314 |
break; |
| 3315 |
|
| 3316 |
case 27: |
| 3317 |
manager_override_log_target(m, LOG_TARGET_CONSOLE); |
| 3318 |
break; |
| 3319 |
|
| 3320 |
case 28: |
| 3321 |
manager_override_log_target(m, LOG_TARGET_KMSG); |
| 3322 |
break; |
| 3323 |
|
| 3324 |
default: |
| 3325 |
log_warning("Got unhandled signal <%s>.", signal_to_string(sfsi.ssi_signo)); |
| 3326 |
} |
| 3327 |
}} |
| 3328 |
|
| 3329 |
return 0; |
| 3330 |
} |
| 3331 |
|
| 3332 |
static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
| 3333 |
Manager *m = ASSERT_PTR(userdata); |
| 3334 |
Unit *u; |
| 3335 |
|
| 3336 |
log_struct(LOG_DEBUG, |
| 3337 |
LOG_MESSAGE_ID(SD_MESSAGE_TIME_CHANGE_STR), |
| 3338 |
LOG_MESSAGE("Time has been changed")); |
| 3339 |
|
| 3340 |
/* Restart the watch */ |
| 3341 |
(void) manager_setup_time_change(m); |
| 3342 |
|
| 3343 |
HASHMAP_FOREACH(u, m->units) |
| 3344 |
if (UNIT_VTABLE(u)->time_change) |
| 3345 |
UNIT_VTABLE(u)->time_change(u); |
| 3346 |
|
| 3347 |
return 0; |
| 3348 |
} |
| 3349 |
|
| 3350 |
static int manager_dispatch_timezone_change( |
| 3351 |
sd_event_source *source, |
| 3352 |
const struct inotify_event *e, |
| 3353 |
void *userdata) { |
| 3354 |
|
| 3355 |
Manager *m = ASSERT_PTR(userdata); |
| 3356 |
int changed; |
| 3357 |
Unit *u; |
| 3358 |
|
| 3359 |
log_debug("inotify event for /etc/localtime"); |
| 3360 |
|
| 3361 |
changed = manager_read_timezone_stat(m); |
| 3362 |
if (changed <= 0) |
| 3363 |
return changed; |
| 3364 |
|
| 3365 |
/* Something changed, restart the watch, to ensure we watch the new /etc/localtime if it changed */ |
| 3366 |
(void) manager_setup_timezone_change(m); |
| 3367 |
|
| 3368 |
/* Read the new timezone */ |
| 3369 |
tzset(); |
| 3370 |
|
| 3371 |
log_debug("Timezone has been changed (now: %s).", get_tzname(daylight)); |
| 3372 |
|
| 3373 |
HASHMAP_FOREACH(u, m->units) |
| 3374 |
if (UNIT_VTABLE(u)->timezone_change) |
| 3375 |
UNIT_VTABLE(u)->timezone_change(u); |
| 3376 |
|
| 3377 |
return 0; |
| 3378 |
} |
| 3379 |
|
| 3380 |
static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
| 3381 |
Manager *m = ASSERT_PTR(userdata); |
| 3382 |
|
| 3383 |
assert(m->idle_pipe[2] == fd); |
| 3384 |
|
| 3385 |
/* There's at least one Type=idle child that just gave up on us waiting for the boot process to |
| 3386 |
* complete. Let's now turn off any further console output if there's at least one service that needs |
| 3387 |
* console access, so that from now on our own output should not spill into that service's output |
| 3388 |
* anymore. After all, we support Type=idle only to beautify console output and it generally is set |
| 3389 |
* on services that want to own the console exclusively without our interference. */ |
| 3390 |
m->no_console_output = m->n_on_console > 0; |
| 3391 |
|
| 3392 |
/* Acknowledge the child's request, and let all other children know too that they shouldn't wait |
| 3393 |
* any longer by closing the pipes towards them, which is what they are waiting for. */ |
| 3394 |
manager_close_idle_pipe(m); |
| 3395 |
|
| 3396 |
return 0; |
| 3397 |
} |
| 3398 |
|
| 3399 |
static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata) { |
| 3400 |
Manager *m = ASSERT_PTR(userdata); |
| 3401 |
int r; |
| 3402 |
|
| 3403 |
assert(source); |
| 3404 |
|
| 3405 |
manager_print_jobs_in_progress(m); |
| 3406 |
|
| 3407 |
r = sd_event_source_set_time_relative(source, JOBS_IN_PROGRESS_PERIOD_USEC); |
| 3408 |
if (r < 0) |
| 3409 |
return r; |
| 3410 |
|
| 3411 |
return sd_event_source_set_enabled(source, SD_EVENT_ONESHOT); |
| 3412 |
} |
| 3413 |
|
| 3414 |
int manager_loop(Manager *m) { |
| 3415 |
int r; |
| 3416 |
|
| 3417 |
assert(m); |
| 3418 |
assert(m->objective == MANAGER_OK); /* Ensure manager_startup() has been called */ |
| 3419 |
|
| 3420 |
manager_check_finished(m); |
| 3421 |
|
| 3422 |
/* There might still be some zombies hanging around from before we were exec()'ed. Let's reap them. */ |
| 3423 |
r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_ON); |
| 3424 |
if (r < 0) |
| 3425 |
return log_error_errno(r, "Failed to enable SIGCHLD event source: %m"); |
| 3426 |
|
| 3427 |
while (m->objective == MANAGER_OK) { |
| 3428 |
|
| 3429 |
if (!ratelimit_below(&m->event_loop_ratelimit)) { |
| 3430 |
/* Yay, something is going seriously wrong, pause a little */ |
| 3431 |
log_warning("Looping too fast. Throttling execution a little."); |
| 3432 |
sleep(1); |
| 3433 |
} |
| 3434 |
|
| 3435 |
(void) watchdog_ping(); |
| 3436 |
|
| 3437 |
if (manager_dispatch_load_queue(m) > 0) |
| 3438 |
continue; |
| 3439 |
|
| 3440 |
if (manager_dispatch_gc_job_queue(m) > 0) |
| 3441 |
continue; |
| 3442 |
|
| 3443 |
if (manager_dispatch_gc_unit_queue(m) > 0) |
| 3444 |
continue; |
| 3445 |
|
| 3446 |
if (manager_dispatch_cleanup_queue(m) > 0) |
| 3447 |
continue; |
| 3448 |
|
| 3449 |
if (manager_dispatch_cgroup_realize_queue(m) > 0) |
| 3450 |
continue; |
| 3451 |
|
| 3452 |
if (manager_dispatch_start_when_upheld_queue(m) > 0) |
| 3453 |
continue; |
| 3454 |
|
| 3455 |
if (manager_dispatch_stop_when_bound_queue(m) > 0) |
| 3456 |
continue; |
| 3457 |
|
| 3458 |
if (manager_dispatch_stop_when_unneeded_queue(m) > 0) |
| 3459 |
continue; |
| 3460 |
|
| 3461 |
if (manager_dispatch_release_resources_queue(m) > 0) |
| 3462 |
continue; |
| 3463 |
|
| 3464 |
if (manager_dispatch_stop_notify_queue(m) > 0) |
| 3465 |
continue; |
| 3466 |
|
| 3467 |
if (manager_dispatch_dbus_queue(m) > 0) |
| 3468 |
continue; |
| 3469 |
|
| 3470 |
/* Sleep for watchdog runtime wait time */ |
| 3471 |
r = sd_event_run(m->event, watchdog_runtime_wait(/* divisor= */ 2)); |
| 3472 |
if (r < 0) |
| 3473 |
return log_error_errno(r, "Failed to run event loop: %m"); |
| 3474 |
} |
| 3475 |
|
| 3476 |
return m->objective; |
| 3477 |
} |
| 3478 |
|
| 3479 |
int manager_load_unit_from_dbus_path(Manager *m, const char *s, sd_bus_error *e, Unit **_u) { |
| 3480 |
_cleanup_free_ char *n = NULL; |
| 3481 |
sd_id128_t invocation_id; |
| 3482 |
Unit *u; |
| 3483 |
int r; |
| 3484 |
|
| 3485 |
assert(m); |
| 3486 |
assert(s); |
| 3487 |
assert(_u); |
| 3488 |
|
| 3489 |
r = unit_name_from_dbus_path(s, &n); |
| 3490 |
if (r < 0) |
| 3491 |
return r; |
| 3492 |
|
| 3493 |
/* Permit addressing units by invocation ID: if the passed bus path is suffixed by a 128-bit ID then |
| 3494 |
* we use it as invocation ID. */ |
| 3495 |
r = sd_id128_from_string(n, &invocation_id); |
| 3496 |
if (r >= 0) { |
| 3497 |
u = hashmap_get(m->units_by_invocation_id, &invocation_id); |
| 3498 |
if (u) { |
| 3499 |
*_u = u; |
| 3500 |
return 0; |
| 3501 |
} |
| 3502 |
|
| 3503 |
return sd_bus_error_setf(e, BUS_ERROR_NO_UNIT_FOR_INVOCATION_ID, |
| 3504 |
"No unit with the specified invocation ID " SD_ID128_FORMAT_STR " known.", |
| 3505 |
SD_ID128_FORMAT_VAL(invocation_id)); |
| 3506 |
} |
| 3507 |
|
| 3508 |
/* If this didn't work, we check if this is a unit name */ |
| 3509 |
if (!unit_name_is_valid(n, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) { |
| 3510 |
_cleanup_free_ char *nn = NULL; |
| 3511 |
|
| 3512 |
nn = cescape(n); |
| 3513 |
return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, |
| 3514 |
"Unit name %s is neither a valid invocation ID nor unit name.", strnull(nn)); |
| 3515 |
} |
| 3516 |
|
| 3517 |
r = manager_load_unit(m, n, NULL, e, &u); |
| 3518 |
if (r < 0) |
| 3519 |
return r; |
| 3520 |
|
| 3521 |
*_u = u; |
| 3522 |
return 0; |
| 3523 |
} |
| 3524 |
|
| 3525 |
int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) { |
| 3526 |
const char *p; |
| 3527 |
unsigned id; |
| 3528 |
Job *j; |
| 3529 |
int r; |
| 3530 |
|
| 3531 |
assert(m); |
| 3532 |
assert(s); |
| 3533 |
assert(_j); |
| 3534 |
|
| 3535 |
p = startswith(s, "/org/freedesktop/systemd1/job/"); |
| 3536 |
if (!p) |
| 3537 |
return -EINVAL; |
| 3538 |
|
| 3539 |
r = safe_atou(p, &id); |
| 3540 |
if (r < 0) |
| 3541 |
return r; |
| 3542 |
|
| 3543 |
j = manager_get_job(m, id); |
| 3544 |
if (!j) |
| 3545 |
return -ENOENT; |
| 3546 |
|
| 3547 |
*_j = j; |
| 3548 |
|
| 3549 |
return 0; |
| 3550 |
} |
| 3551 |
|
| 3552 |
void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) { |
| 3553 |
|
| 3554 |
#if HAVE_AUDIT |
| 3555 |
_cleanup_free_ char *p = NULL; |
| 3556 |
const char *msg; |
| 3557 |
int audit_fd, r; |
| 3558 |
|
| 3559 |
assert(m); |
| 3560 |
assert(u); |
| 3561 |
|
| 3562 |
if (!MANAGER_IS_SYSTEM(m)) |
| 3563 |
return; |
| 3564 |
|
| 3565 |
/* Don't generate audit events if the service was already started and we're just deserializing */ |
| 3566 |
if (MANAGER_IS_RELOADING(m)) |
| 3567 |
return; |
| 3568 |
|
| 3569 |
audit_fd = get_core_audit_fd(); |
| 3570 |
if (audit_fd < 0) |
| 3571 |
return; |
| 3572 |
|
| 3573 |
r = unit_name_to_prefix_and_instance(u->id, &p); |
| 3574 |
if (r < 0) { |
| 3575 |
log_warning_errno(r, "Failed to extract prefix and instance of unit name, ignoring: %m"); |
| 3576 |
return; |
| 3577 |
} |
| 3578 |
|
| 3579 |
msg = strjoina("unit=", p); |
| 3580 |
if (sym_audit_log_user_comm_message(audit_fd, type, msg, "systemd", NULL, NULL, NULL, success) < 0) { |
| 3581 |
if (ERRNO_IS_PRIVILEGE(errno)) { |
| 3582 |
/* We aren't allowed to send audit messages? Then let's not retry again. */ |
| 3583 |
log_debug_errno(errno, "Failed to send audit message, closing audit socket: %m"); |
| 3584 |
close_core_audit_fd(); |
| 3585 |
} else |
| 3586 |
log_warning_errno(errno, "Failed to send audit message, ignoring: %m"); |
| 3587 |
} |
| 3588 |
#endif |
| 3589 |
} |
| 3590 |
|
| 3591 |
void manager_send_unit_plymouth(Manager *m, Unit *u) { |
| 3592 |
_cleanup_free_ char *message = NULL; |
| 3593 |
int c, r; |
| 3594 |
|
| 3595 |
assert(m); |
| 3596 |
assert(u); |
| 3597 |
|
| 3598 |
if (!MANAGER_IS_SYSTEM(m)) |
| 3599 |
return; |
| 3600 |
|
| 3601 |
/* Don't generate plymouth events if the service was already started and we're just deserializing */ |
| 3602 |
if (MANAGER_IS_RELOADING(m)) |
| 3603 |
return; |
| 3604 |
|
| 3605 |
if (detect_container() > 0) |
| 3606 |
return; |
| 3607 |
|
| 3608 |
if (!UNIT_VTABLE(u)->notify_plymouth) |
| 3609 |
return; |
| 3610 |
|
| 3611 |
c = asprintf(&message, "U\x02%c%s%c", (int) (strlen(u->id) + 1), u->id, '\x00'); |
| 3612 |
if (c < 0) |
| 3613 |
return (void) log_oom(); |
| 3614 |
|
| 3615 |
/* We set SOCK_NONBLOCK here so that we rather drop the message then wait for plymouth */ |
| 3616 |
r = plymouth_send_raw(message, c, SOCK_NONBLOCK); |
| 3617 |
if (r < 0) |
| 3618 |
log_full_errno(ERRNO_IS_NO_PLYMOUTH(r) ? LOG_DEBUG : LOG_WARNING, r, |
| 3619 |
"Failed to communicate with plymouth: %m"); |
| 3620 |
} |
| 3621 |
|
| 3622 |
void manager_send_unit_supervisor(Manager *m, Unit *u, bool active) { |
| 3623 |
assert(m); |
| 3624 |
assert(u); |
| 3625 |
|
| 3626 |
/* Notify a "supervisor" process about our progress, i.e. a container manager, hypervisor, or |
| 3627 |
* surrounding service manager. */ |
| 3628 |
|
| 3629 |
if (MANAGER_IS_RELOADING(m)) |
| 3630 |
return; |
| 3631 |
|
| 3632 |
if (!UNIT_VTABLE(u)->notify_supervisor) |
| 3633 |
return; |
| 3634 |
|
| 3635 |
if (in_initrd()) /* Only send these once we left the initrd */ |
| 3636 |
return; |
| 3637 |
|
| 3638 |
(void) sd_notifyf(/* unset_environment= */ false, |
| 3639 |
active ? "X_SYSTEMD_UNIT_ACTIVE=%s" : "X_SYSTEMD_UNIT_INACTIVE=%s", |
| 3640 |
u->id); |
| 3641 |
} |
| 3642 |
|
| 3643 |
usec_t manager_get_watchdog(Manager *m, WatchdogType t) { |
| 3644 |
assert(m); |
| 3645 |
|
| 3646 |
if (MANAGER_IS_USER(m)) |
| 3647 |
return USEC_INFINITY; |
| 3648 |
|
| 3649 |
if (m->watchdog_overridden[t] != USEC_INFINITY) |
| 3650 |
return m->watchdog_overridden[t]; |
| 3651 |
|
| 3652 |
return m->watchdog[t]; |
| 3653 |
} |
| 3654 |
|
| 3655 |
void manager_set_watchdog(Manager *m, WatchdogType t, usec_t timeout) { |
| 3656 |
|
| 3657 |
assert(m); |
| 3658 |
|
| 3659 |
if (MANAGER_IS_USER(m)) |
| 3660 |
return; |
| 3661 |
|
| 3662 |
if (m->watchdog_overridden[t] == USEC_INFINITY) { |
| 3663 |
if (t == WATCHDOG_RUNTIME) |
| 3664 |
(void) watchdog_setup(timeout); |
| 3665 |
else if (t == WATCHDOG_PRETIMEOUT) |
| 3666 |
(void) watchdog_setup_pretimeout(timeout); |
| 3667 |
} |
| 3668 |
|
| 3669 |
m->watchdog[t] = timeout; |
| 3670 |
} |
| 3671 |
|
| 3672 |
void manager_override_watchdog(Manager *m, WatchdogType t, usec_t timeout) { |
| 3673 |
usec_t usec; |
| 3674 |
|
| 3675 |
assert(m); |
| 3676 |
|
| 3677 |
if (MANAGER_IS_USER(m)) |
| 3678 |
return; |
| 3679 |
|
| 3680 |
usec = timeout == USEC_INFINITY ? m->watchdog[t] : timeout; |
| 3681 |
if (t == WATCHDOG_RUNTIME) |
| 3682 |
(void) watchdog_setup(usec); |
| 3683 |
else if (t == WATCHDOG_PRETIMEOUT) |
| 3684 |
(void) watchdog_setup_pretimeout(usec); |
| 3685 |
|
| 3686 |
m->watchdog_overridden[t] = timeout; |
| 3687 |
} |
| 3688 |
|
| 3689 |
int manager_set_watchdog_pretimeout_governor(Manager *m, const char *governor) { |
| 3690 |
_cleanup_free_ char *p = NULL; |
| 3691 |
int r; |
| 3692 |
|
| 3693 |
assert(m); |
| 3694 |
|
| 3695 |
if (MANAGER_IS_USER(m)) |
| 3696 |
return 0; |
| 3697 |
|
| 3698 |
governor = empty_to_null(governor); |
| 3699 |
|
| 3700 |
if (streq_ptr(m->watchdog_pretimeout_governor, governor)) |
| 3701 |
return 0; |
| 3702 |
|
| 3703 |
r = strdup_to(&p, governor); |
| 3704 |
if (r < 0) |
| 3705 |
return r; |
| 3706 |
|
| 3707 |
r = watchdog_setup_pretimeout_governor(governor); |
| 3708 |
if (r < 0) |
| 3709 |
return r; |
| 3710 |
|
| 3711 |
return free_and_replace(m->watchdog_pretimeout_governor, p); |
| 3712 |
} |
| 3713 |
|
| 3714 |
int manager_override_watchdog_pretimeout_governor(Manager *m, const char *governor) { |
| 3715 |
_cleanup_free_ char *p = NULL; |
| 3716 |
int r; |
| 3717 |
|
| 3718 |
assert(m); |
| 3719 |
|
| 3720 |
if (MANAGER_IS_USER(m)) |
| 3721 |
return 0; |
| 3722 |
|
| 3723 |
governor = empty_to_null(governor); |
| 3724 |
|
| 3725 |
if (streq_ptr(m->watchdog_pretimeout_governor_overridden, governor)) |
| 3726 |
return 0; |
| 3727 |
|
| 3728 |
r = strdup_to(&p, governor); |
| 3729 |
if (r < 0) |
| 3730 |
return r; |
| 3731 |
|
| 3732 |
r = watchdog_setup_pretimeout_governor(governor); |
| 3733 |
if (r < 0) |
| 3734 |
return r; |
| 3735 |
|
| 3736 |
return free_and_replace(m->watchdog_pretimeout_governor_overridden, p); |
| 3737 |
} |
| 3738 |
|
| 3739 |
int manager_reload(Manager *m) { |
| 3740 |
_unused_ _cleanup_(manager_reloading_stopp) Manager *reloading = NULL; |
| 3741 |
_cleanup_fdset_free_ FDSet *fds = NULL; |
| 3742 |
_cleanup_fclose_ FILE *f = NULL; |
| 3743 |
int r; |
| 3744 |
|
| 3745 |
assert(m); |
| 3746 |
|
| 3747 |
r = manager_open_serialization(m, &f); |
| 3748 |
if (r < 0) |
| 3749 |
return log_error_errno(r, "Failed to create serialization file: %m"); |
| 3750 |
|
| 3751 |
fds = fdset_new(); |
| 3752 |
if (!fds) |
| 3753 |
return log_oom(); |
| 3754 |
|
| 3755 |
/* We are officially in reload mode from here on. */ |
| 3756 |
reloading = manager_reloading_start(m); |
| 3757 |
|
| 3758 |
r = manager_serialize(m, f, fds, false); |
| 3759 |
if (r < 0) |
| 3760 |
return r; |
| 3761 |
|
| 3762 |
r = finish_serialization_file(f); |
| 3763 |
if (r < 0) |
| 3764 |
return log_error_errno(r, "Failed to finish serialization: %m"); |
| 3765 |
|
| 3766 |
/* 💀 This is the point of no return, from here on there is no way back. 💀 */ |
| 3767 |
reloading = NULL; |
| 3768 |
|
| 3769 |
/* Bump before sending the Reloading signal, so any client that reads |
| 3770 |
* ReloadCount in response to that signal observes the new value. */ |
| 3771 |
m->reload_count = saturate_add(m->reload_count, 1, UINT64_MAX); |
| 3772 |
|
| 3773 |
bus_manager_send_reloading(m, true); |
| 3774 |
|
| 3775 |
/* Start by flushing out all jobs and units, all generated units, all runtime environments, all dynamic users |
| 3776 |
* and everything else that is worth flushing out. We'll get it all back from the serialization — if we need |
| 3777 |
* it. */ |
| 3778 |
|
| 3779 |
manager_clear_jobs_and_units(m); |
| 3780 |
lookup_paths_flush_generator(&m->lookup_paths); |
| 3781 |
exec_shared_runtime_vacuum(m); |
| 3782 |
dynamic_user_vacuum(m, false); |
| 3783 |
m->uid_refs = hashmap_free(m->uid_refs); |
| 3784 |
m->gid_refs = hashmap_free(m->gid_refs); |
| 3785 |
|
| 3786 |
(void) manager_run_environment_generators(m); |
| 3787 |
(void) manager_run_generators(m); |
| 3788 |
|
| 3789 |
/* We flushed out generated files, for which we don't watch mtime, so we should flush the old map. */ |
| 3790 |
manager_free_unit_name_maps(m); |
| 3791 |
m->unit_file_state_outdated = false; |
| 3792 |
|
| 3793 |
/* First, enumerate what we can from kernel and suchlike */ |
| 3794 |
manager_enumerate_perpetual(m); |
| 3795 |
manager_enumerate(m); |
| 3796 |
|
| 3797 |
/* Second, deserialize our stored data */ |
| 3798 |
r = manager_deserialize(m, f, fds); |
| 3799 |
if (r < 0) |
| 3800 |
log_warning_errno(r, "Deserialization failed, proceeding anyway: %m"); |
| 3801 |
|
| 3802 |
/* We don't need the serialization anymore */ |
| 3803 |
f = safe_fclose(f); |
| 3804 |
|
| 3805 |
/* Re-register notify_fd as event source, and set up other sockets/communication channels we might need */ |
| 3806 |
(void) manager_setup_notify(m); |
| 3807 |
(void) manager_setup_user_lookup_fd(m); |
| 3808 |
(void) manager_setup_handoff_timestamp_fd(m); |
| 3809 |
(void) manager_setup_pidref_transport_fd(m); |
| 3810 |
|
| 3811 |
/* Clean up deserialized bus track information. They're never consumed during reload (as opposed to |
| 3812 |
* reexec) since we do not disconnect from the bus. */ |
| 3813 |
m->subscribed_as_strv = strv_free(m->subscribed_as_strv); |
| 3814 |
m->deserialized_bus_id = SD_ID128_NULL; |
| 3815 |
|
| 3816 |
/* Third, fire things up! */ |
| 3817 |
manager_coldplug(m); |
| 3818 |
|
| 3819 |
/* Clean up runtime objects no longer referenced */ |
| 3820 |
manager_vacuum(m); |
| 3821 |
|
| 3822 |
/* Consider the reload process complete now. */ |
| 3823 |
assert(m->n_reloading > 0); |
| 3824 |
m->n_reloading--; |
| 3825 |
|
| 3826 |
manager_ready(m); |
| 3827 |
|
| 3828 |
m->send_reloading_done = true; |
| 3829 |
return 0; |
| 3830 |
} |
| 3831 |
|
| 3832 |
void manager_reset_failed(Manager *m) { |
| 3833 |
Unit *u; |
| 3834 |
|
| 3835 |
assert(m); |
| 3836 |
|
| 3837 |
HASHMAP_FOREACH(u, m->units) |
| 3838 |
unit_reset_failed(u); |
| 3839 |
|
| 3840 |
m->transactions_with_cycle = set_free(m->transactions_with_cycle); |
| 3841 |
} |
| 3842 |
|
| 3843 |
bool manager_unit_inactive_or_pending(Manager *m, const char *name) { |
| 3844 |
Unit *u; |
| 3845 |
|
| 3846 |
assert(m); |
| 3847 |
assert(name); |
| 3848 |
|
| 3849 |
/* Returns true if the unit is inactive or going down */ |
| 3850 |
u = manager_get_unit(m, name); |
| 3851 |
if (!u) |
| 3852 |
return true; |
| 3853 |
|
| 3854 |
return unit_inactive_or_pending(u); |
| 3855 |
} |
| 3856 |
|
| 3857 |
static void log_taint_string(Manager *m) { |
| 3858 |
assert(m); |
| 3859 |
|
| 3860 |
if (MANAGER_IS_USER(m) || m->taint_logged) |
| 3861 |
return; |
| 3862 |
|
| 3863 |
m->taint_logged = true; /* only check for taint once */ |
| 3864 |
|
| 3865 |
_cleanup_free_ char *taint = taint_string(); |
| 3866 |
if (isempty(taint)) |
| 3867 |
return; |
| 3868 |
|
| 3869 |
log_struct(LOG_NOTICE, |
| 3870 |
LOG_MESSAGE("System is tainted: %s", taint), |
| 3871 |
LOG_ITEM("TAINT=%s", taint), |
| 3872 |
LOG_MESSAGE_ID(SD_MESSAGE_TAINTED_STR)); |
| 3873 |
} |
| 3874 |
|
| 3875 |
static void manager_notify_finished(Manager *m) { |
| 3876 |
usec_t firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec; |
| 3877 |
|
| 3878 |
if (MANAGER_IS_TEST_RUN(m)) |
| 3879 |
return; |
| 3880 |
|
| 3881 |
if (MANAGER_IS_SYSTEM(m) && m->soft_reboots_count > 0) { |
| 3882 |
/* The soft-reboot case, where we only report data for the last reboot */ |
| 3883 |
firmware_usec = loader_usec = initrd_usec = kernel_usec = 0; |
| 3884 |
total_usec = userspace_usec = usec_sub_unsigned(m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic, |
| 3885 |
m->timestamps[MANAGER_TIMESTAMP_SHUTDOWN_START].monotonic); |
| 3886 |
|
| 3887 |
log_struct(LOG_INFO, |
| 3888 |
LOG_MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED_STR), |
| 3889 |
LOG_ITEM("USERSPACE_USEC="USEC_FMT, userspace_usec), |
| 3890 |
LOG_MESSAGE("Soft-reboot finished in %s, counter is now at %u.", |
| 3891 |
FORMAT_TIMESPAN(total_usec, USEC_PER_MSEC), |
| 3892 |
m->soft_reboots_count)); |
| 3893 |
} else if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0) { |
| 3894 |
char buf[FORMAT_TIMESPAN_MAX + STRLEN(" (firmware) + ") + FORMAT_TIMESPAN_MAX + STRLEN(" (loader) + ")] |
| 3895 |
= {}; |
| 3896 |
char *p = buf; |
| 3897 |
size_t size = sizeof buf; |
| 3898 |
|
| 3899 |
/* Note that MANAGER_TIMESTAMP_KERNEL's monotonic value is always at 0, and |
| 3900 |
* MANAGER_TIMESTAMP_FIRMWARE's and MANAGER_TIMESTAMP_LOADER's monotonic value should be considered |
| 3901 |
* negative values. */ |
| 3902 |
|
| 3903 |
firmware_usec = m->timestamps[MANAGER_TIMESTAMP_FIRMWARE].monotonic - m->timestamps[MANAGER_TIMESTAMP_LOADER].monotonic; |
| 3904 |
loader_usec = m->timestamps[MANAGER_TIMESTAMP_LOADER].monotonic - m->timestamps[MANAGER_TIMESTAMP_KERNEL].monotonic; |
| 3905 |
userspace_usec = m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic - m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic; |
| 3906 |
total_usec = m->timestamps[MANAGER_TIMESTAMP_FIRMWARE].monotonic + m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic; |
| 3907 |
|
| 3908 |
if (firmware_usec > 0) |
| 3909 |
size = strpcpyf(&p, size, "%s (firmware) + ", FORMAT_TIMESPAN(firmware_usec, USEC_PER_MSEC)); |
| 3910 |
if (loader_usec > 0) |
| 3911 |
size = strpcpyf(&p, size, "%s (loader) + ", FORMAT_TIMESPAN(loader_usec, USEC_PER_MSEC)); |
| 3912 |
|
| 3913 |
if (dual_timestamp_is_set(&m->timestamps[MANAGER_TIMESTAMP_INITRD])) { |
| 3914 |
|
| 3915 |
/* The initrd case on bare-metal */ |
| 3916 |
kernel_usec = m->timestamps[MANAGER_TIMESTAMP_INITRD].monotonic - m->timestamps[MANAGER_TIMESTAMP_KERNEL].monotonic; |
| 3917 |
initrd_usec = m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic - m->timestamps[MANAGER_TIMESTAMP_INITRD].monotonic; |
| 3918 |
|
| 3919 |
log_struct(LOG_INFO, |
| 3920 |
LOG_MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED_STR), |
| 3921 |
LOG_ITEM("KERNEL_USEC="USEC_FMT, kernel_usec), |
| 3922 |
LOG_ITEM("INITRD_USEC="USEC_FMT, initrd_usec), |
| 3923 |
LOG_ITEM("USERSPACE_USEC="USEC_FMT, userspace_usec), |
| 3924 |
LOG_MESSAGE("Startup finished in %s%s (kernel) + %s (initrd) + %s (userspace) = %s.", |
| 3925 |
buf, |
| 3926 |
FORMAT_TIMESPAN(kernel_usec, USEC_PER_MSEC), |
| 3927 |
FORMAT_TIMESPAN(initrd_usec, USEC_PER_MSEC), |
| 3928 |
FORMAT_TIMESPAN(userspace_usec, USEC_PER_MSEC), |
| 3929 |
FORMAT_TIMESPAN(total_usec, USEC_PER_MSEC))); |
| 3930 |
} else { |
| 3931 |
/* The initrd-less case on bare-metal */ |
| 3932 |
|
| 3933 |
kernel_usec = m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic - m->timestamps[MANAGER_TIMESTAMP_KERNEL].monotonic; |
| 3934 |
initrd_usec = 0; |
| 3935 |
|
| 3936 |
log_struct(LOG_INFO, |
| 3937 |
LOG_MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED_STR), |
| 3938 |
LOG_ITEM("KERNEL_USEC="USEC_FMT, kernel_usec), |
| 3939 |
LOG_ITEM("USERSPACE_USEC="USEC_FMT, userspace_usec), |
| 3940 |
LOG_MESSAGE("Startup finished in %s%s (kernel) + %s (userspace) = %s.", |
| 3941 |
buf, |
| 3942 |
FORMAT_TIMESPAN(kernel_usec, USEC_PER_MSEC), |
| 3943 |
FORMAT_TIMESPAN(userspace_usec, USEC_PER_MSEC), |
| 3944 |
FORMAT_TIMESPAN(total_usec, USEC_PER_MSEC))); |
| 3945 |
} |
| 3946 |
} else { |
| 3947 |
/* The container and --user case */ |
| 3948 |
firmware_usec = loader_usec = initrd_usec = kernel_usec = 0; |
| 3949 |
total_usec = userspace_usec = m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic - m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic; |
| 3950 |
|
| 3951 |
log_struct(LOG_INFO, |
| 3952 |
LOG_MESSAGE_ID(SD_MESSAGE_USER_STARTUP_FINISHED_STR), |
| 3953 |
LOG_ITEM("USERSPACE_USEC="USEC_FMT, userspace_usec), |
| 3954 |
LOG_MESSAGE("Startup finished in %s.", |
| 3955 |
FORMAT_TIMESPAN(total_usec, USEC_PER_MSEC))); |
| 3956 |
} |
| 3957 |
|
| 3958 |
bus_manager_send_finished(m, firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec); |
| 3959 |
|
| 3960 |
if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0) |
| 3961 |
watchdog_report_if_missing(); |
| 3962 |
|
| 3963 |
log_taint_string(m); |
| 3964 |
} |
| 3965 |
|
| 3966 |
static void manager_send_ready_on_basic_target(Manager *m) { |
| 3967 |
int r; |
| 3968 |
|
| 3969 |
assert(m); |
| 3970 |
|
| 3971 |
/* We send READY=1 on reaching basic.target only when running in --user mode. */ |
| 3972 |
if (!MANAGER_IS_USER(m) || m->ready_sent) |
| 3973 |
return; |
| 3974 |
|
| 3975 |
r = sd_notify(/* unset_environment= */ false, |
| 3976 |
"READY=1\n" |
| 3977 |
"STATUS=Reached " SPECIAL_BASIC_TARGET "."); |
| 3978 |
if (r < 0) |
| 3979 |
log_warning_errno(r, "Failed to send readiness notification, ignoring: %m"); |
| 3980 |
|
| 3981 |
m->ready_sent = true; |
| 3982 |
m->status_ready = false; |
| 3983 |
} |
| 3984 |
|
| 3985 |
static void manager_send_ready_on_idle(Manager *m) { |
| 3986 |
int r; |
| 3987 |
|
| 3988 |
assert(m); |
| 3989 |
|
| 3990 |
/* Skip the notification if nothing changed. */ |
| 3991 |
if (m->ready_sent && m->status_ready) |
| 3992 |
return; |
| 3993 |
|
| 3994 |
/* Note that for user managers, we might have already sent READY=1 in manager_send_ready_user_scope(). |
| 3995 |
* But we still need to flush STATUS=. The second READY=1 will be treated as a noop so it doesn't |
| 3996 |
* hurt to send it twice. */ |
| 3997 |
r = sd_notify(/* unset_environment= */ false, |
| 3998 |
"READY=1\n" |
| 3999 |
"STATUS=Ready."); |
| 4000 |
if (r < 0) |
| 4001 |
log_full_errno(m->ready_sent ? LOG_DEBUG : LOG_WARNING, r, |
| 4002 |
"Failed to send readiness notification, ignoring: %m"); |
| 4003 |
|
| 4004 |
m->ready_sent = m->status_ready = true; |
| 4005 |
} |
| 4006 |
|
| 4007 |
static void manager_check_basic_target(Manager *m) { |
| 4008 |
Unit *u; |
| 4009 |
|
| 4010 |
assert(m); |
| 4011 |
|
| 4012 |
/* Small shortcut */ |
| 4013 |
if (m->ready_sent && m->taint_logged) |
| 4014 |
return; |
| 4015 |
|
| 4016 |
u = manager_get_unit(m, SPECIAL_BASIC_TARGET); |
| 4017 |
if (!u || !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) |
| 4018 |
return; |
| 4019 |
|
| 4020 |
/* For user managers, send out READY=1 as soon as we reach basic.target */ |
| 4021 |
manager_send_ready_on_basic_target(m); |
| 4022 |
|
| 4023 |
/* Log the taint string as soon as we reach basic.target */ |
| 4024 |
log_taint_string(m); |
| 4025 |
} |
| 4026 |
|
| 4027 |
void manager_check_finished(Manager *m) { |
| 4028 |
assert(m); |
| 4029 |
|
| 4030 |
if (MANAGER_IS_RELOADING(m)) |
| 4031 |
return; |
| 4032 |
|
| 4033 |
/* Verify that we have entered the event loop already, and not left it again. */ |
| 4034 |
if (!MANAGER_IS_RUNNING(m)) |
| 4035 |
return; |
| 4036 |
|
| 4037 |
manager_check_basic_target(m); |
| 4038 |
|
| 4039 |
if (!hashmap_isempty(m->jobs)) { |
| 4040 |
if (m->jobs_in_progress_event_source) |
| 4041 |
/* Ignore any failure, this is only for feedback */ |
| 4042 |
(void) sd_event_source_set_time(m->jobs_in_progress_event_source, |
| 4043 |
manager_watch_jobs_next_time(m)); |
| 4044 |
return; |
| 4045 |
} |
| 4046 |
|
| 4047 |
/* The jobs hashmap tends to grow a lot during boot, and then it's not reused until shutdown. Let's |
| 4048 |
kill the hashmap if it is relatively large. */ |
| 4049 |
if (hashmap_buckets(m->jobs) > hashmap_size(m->units) / 10) |
| 4050 |
m->jobs = hashmap_free(m->jobs); |
| 4051 |
|
| 4052 |
manager_send_ready_on_idle(m); |
| 4053 |
|
| 4054 |
/* Notify Type=idle units that we are done now */ |
| 4055 |
manager_close_idle_pipe(m); |
| 4056 |
|
| 4057 |
if (MANAGER_IS_FINISHED(m)) |
| 4058 |
return; |
| 4059 |
|
| 4060 |
manager_flip_auto_status(m, false, "boot finished"); |
| 4061 |
|
| 4062 |
/* Turn off confirm spawn now */ |
| 4063 |
m->confirm_spawn = NULL; |
| 4064 |
|
| 4065 |
/* No need to update ask password status when we're going non-interactive */ |
| 4066 |
manager_close_ask_password(m); |
| 4067 |
|
| 4068 |
/* This is no longer the first boot */ |
| 4069 |
manager_set_first_boot(m, false); |
| 4070 |
|
| 4071 |
dual_timestamp_now(m->timestamps + MANAGER_TIMESTAMP_FINISH); |
| 4072 |
|
| 4073 |
manager_notify_finished(m); |
| 4074 |
|
| 4075 |
manager_invalidate_startup_units(m); |
| 4076 |
} |
| 4077 |
|
| 4078 |
void manager_send_reloading(Manager *m) { |
| 4079 |
assert(m); |
| 4080 |
|
| 4081 |
/* Let whoever invoked us know that we are now reloading */ |
| 4082 |
(void) notify_reloading_full(/* status= */ NULL); |
| 4083 |
|
| 4084 |
/* And ensure that we'll send READY=1 again as soon as we are ready again */ |
| 4085 |
m->ready_sent = false; |
| 4086 |
} |
| 4087 |
|
| 4088 |
static bool generator_path_any(char * const *paths) { |
| 4089 |
|
| 4090 |
/* Optimize by skipping the whole process by not creating output directories if no generators are found. */ |
| 4091 |
|
| 4092 |
STRV_FOREACH(i, paths) { |
| 4093 |
if (access(*i, F_OK) >= 0) |
| 4094 |
return true; |
| 4095 |
if (errno != ENOENT) |
| 4096 |
log_warning_errno(errno, "Failed to check if generator dir '%s' exists, assuming not: %m", *i); |
| 4097 |
} |
| 4098 |
|
| 4099 |
return false; |
| 4100 |
} |
| 4101 |
|
| 4102 |
static int manager_run_environment_generators(Manager *m) { |
| 4103 |
_cleanup_strv_free_ char **paths = NULL; |
| 4104 |
int r; |
| 4105 |
|
| 4106 |
assert(m); |
| 4107 |
|
| 4108 |
if (MANAGER_IS_TEST_RUN(m) && !(m->test_run_flags & MANAGER_TEST_RUN_ENV_GENERATORS)) |
| 4109 |
return 0; |
| 4110 |
|
| 4111 |
paths = env_generator_binary_paths(m->runtime_scope); |
| 4112 |
if (!paths) |
| 4113 |
return log_oom(); |
| 4114 |
|
| 4115 |
if (!generator_path_any(paths)) |
| 4116 |
return 0; |
| 4117 |
|
| 4118 |
char **tmp = NULL; /* this is only used in the forked process, no cleanup here */ |
| 4119 |
void *args[_STDOUT_CONSUME_MAX] = { |
| 4120 |
[STDOUT_GENERATE] = &tmp, |
| 4121 |
[STDOUT_COLLECT] = &tmp, |
| 4122 |
[STDOUT_CONSUME] = &m->transient_environment, |
| 4123 |
}; |
| 4124 |
|
| 4125 |
WITH_UMASK(0022) |
| 4126 |
r = execute_directories( |
| 4127 |
"environment-generators", |
| 4128 |
(const char* const*) paths, |
| 4129 |
DEFAULT_TIMEOUT_USEC, |
| 4130 |
gather_environment, |
| 4131 |
args, |
| 4132 |
/* argv[]= */ NULL, |
| 4133 |
m->transient_environment, |
| 4134 |
EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS | EXEC_DIR_SET_SYSTEMD_EXEC_PID); |
| 4135 |
return r; |
| 4136 |
} |
| 4137 |
|
| 4138 |
static int build_generator_environment(Manager *m, char ***ret) { |
| 4139 |
_cleanup_strv_free_ char **nl = NULL; |
| 4140 |
Virtualization v; |
| 4141 |
ConfidentialVirtualization cv; |
| 4142 |
int r; |
| 4143 |
|
| 4144 |
assert(m); |
| 4145 |
assert(ret); |
| 4146 |
|
| 4147 |
/* Generators oftentimes want to know some basic facts about the environment they run in, in order to |
| 4148 |
* adjust generated units to that. Let's pass down some bits of information that are easy for us to |
| 4149 |
* determine (but a bit harder for generator scripts to determine), as environment variables. */ |
| 4150 |
|
| 4151 |
nl = strv_copy(m->transient_environment); |
| 4152 |
if (!nl) |
| 4153 |
return -ENOMEM; |
| 4154 |
|
| 4155 |
r = strv_env_assign(&nl, "SYSTEMD_SCOPE", runtime_scope_to_string(m->runtime_scope)); |
| 4156 |
if (r < 0) |
| 4157 |
return r; |
| 4158 |
|
| 4159 |
if (MANAGER_IS_SYSTEM(m)) { |
| 4160 |
/* Note that $SYSTEMD_IN_INITRD may be used to override the initrd detection in much of our |
| 4161 |
* codebase. This is hence more than purely informational. It will shortcut detection of the |
| 4162 |
* initrd state if generators invoke our own tools. But that's OK, as it would come to the |
| 4163 |
* same results (hopefully). */ |
| 4164 |
r = strv_env_assign(&nl, "SYSTEMD_IN_INITRD", one_zero(in_initrd())); |
| 4165 |
if (r < 0) |
| 4166 |
return r; |
| 4167 |
|
| 4168 |
if (m->soft_reboots_count > 0) { |
| 4169 |
r = strv_env_assignf(&nl, "SYSTEMD_SOFT_REBOOTS_COUNT", "%u", m->soft_reboots_count); |
| 4170 |
if (r < 0) |
| 4171 |
return r; |
| 4172 |
} |
| 4173 |
|
| 4174 |
if (m->first_boot >= 0) { |
| 4175 |
r = strv_env_assign(&nl, "SYSTEMD_FIRST_BOOT", one_zero(m->first_boot)); |
| 4176 |
if (r < 0) |
| 4177 |
return r; |
| 4178 |
} |
| 4179 |
} |
| 4180 |
|
| 4181 |
v = detect_virtualization(); |
| 4182 |
if (v < 0) |
| 4183 |
log_debug_errno(v, "Failed to detect virtualization, ignoring: %m"); |
| 4184 |
else if (v > 0) { |
| 4185 |
const char *s; |
| 4186 |
|
| 4187 |
s = strjoina(VIRTUALIZATION_IS_VM(v) ? "vm:" : |
| 4188 |
VIRTUALIZATION_IS_CONTAINER(v) ? "container:" : ":", |
| 4189 |
virtualization_to_string(v)); |
| 4190 |
|
| 4191 |
r = strv_env_assign(&nl, "SYSTEMD_VIRTUALIZATION", s); |
| 4192 |
if (r < 0) |
| 4193 |
return r; |
| 4194 |
} |
| 4195 |
|
| 4196 |
cv = detect_confidential_virtualization(); |
| 4197 |
if (cv < 0) |
| 4198 |
log_debug_errno(cv, "Failed to detect confidential virtualization, ignoring: %m"); |
| 4199 |
else if (cv > 0) { |
| 4200 |
r = strv_env_assign(&nl, "SYSTEMD_CONFIDENTIAL_VIRTUALIZATION", confidential_virtualization_to_string(cv)); |
| 4201 |
if (r < 0) |
| 4202 |
return r; |
| 4203 |
} |
| 4204 |
|
| 4205 |
r = strv_env_assign(&nl, "SYSTEMD_ARCHITECTURE", architecture_to_string(uname_architecture())); |
| 4206 |
if (r < 0) |
| 4207 |
return r; |
| 4208 |
|
| 4209 |
*ret = TAKE_PTR(nl); |
| 4210 |
return 0; |
| 4211 |
} |
| 4212 |
|
| 4213 |
static int manager_execute_generators(Manager *m, char * const *paths, bool remount_ro) { |
| 4214 |
_cleanup_strv_free_ char **ge = NULL; |
| 4215 |
int r; |
| 4216 |
|
| 4217 |
assert(m); |
| 4218 |
|
| 4219 |
r = build_generator_environment(m, &ge); |
| 4220 |
if (r < 0) |
| 4221 |
return log_error_errno(r, "Failed to build generator environment: %m"); |
| 4222 |
|
| 4223 |
if (remount_ro) { |
| 4224 |
/* Remount most of the filesystem tree read-only. We leave /sys/ as-is, because our code |
| 4225 |
* checks whether it is read-only to detect containerized execution environments. We leave |
| 4226 |
* /run/ as-is too, because that's where our output goes. We also leave /proc/ and /dev/shm/ |
| 4227 |
* because they're API, and /tmp/ that safe_fork() mounted for us. |
| 4228 |
*/ |
| 4229 |
r = bind_remount_recursive("/", MS_RDONLY, MS_RDONLY, |
| 4230 |
STRV_MAKE("/sys", "/run", "/proc", "/dev/shm", "/tmp")); |
| 4231 |
if (r < 0) |
| 4232 |
log_warning_errno(r, "Read-only bind remount failed, ignoring: %m"); |
| 4233 |
} |
| 4234 |
|
| 4235 |
const char *argv[] = { |
| 4236 |
NULL, /* Leave this empty, execute_directory() will fill something in */ |
| 4237 |
m->lookup_paths.generator, |
| 4238 |
m->lookup_paths.generator_early, |
| 4239 |
m->lookup_paths.generator_late, |
| 4240 |
NULL, |
| 4241 |
}; |
| 4242 |
|
| 4243 |
BLOCK_WITH_UMASK(0022); |
| 4244 |
return execute_directories( |
| 4245 |
"generators", |
| 4246 |
(const char* const*) paths, |
| 4247 |
DEFAULT_TIMEOUT_USEC, |
| 4248 |
/* callbacks= */ NULL, /* callback_args= */ NULL, |
| 4249 |
(char**) argv, |
| 4250 |
ge, |
| 4251 |
EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS | EXEC_DIR_SET_SYSTEMD_EXEC_PID | EXEC_DIR_WARN_WORLD_WRITABLE); |
| 4252 |
} |
| 4253 |
|
| 4254 |
static int manager_run_generators(Manager *m) { |
| 4255 |
ForkFlags flags = FORK_RESET_SIGNALS | FORK_WAIT | FORK_NEW_MOUNTNS | FORK_MOUNTNS_SLAVE; |
| 4256 |
_cleanup_strv_free_ char **paths = NULL; |
| 4257 |
int r; |
| 4258 |
|
| 4259 |
assert(m); |
| 4260 |
|
| 4261 |
if (MANAGER_IS_TEST_RUN(m) && !(m->test_run_flags & MANAGER_TEST_RUN_GENERATORS)) |
| 4262 |
return 0; |
| 4263 |
|
| 4264 |
paths = generator_binary_paths(m->runtime_scope); |
| 4265 |
if (!paths) |
| 4266 |
return log_oom(); |
| 4267 |
|
| 4268 |
if (!generator_path_any(paths)) |
| 4269 |
return 0; |
| 4270 |
|
| 4271 |
r = lookup_paths_mkdir_generator(&m->lookup_paths); |
| 4272 |
if (r < 0) { |
| 4273 |
log_error_errno(r, "Failed to create generator directories: %m"); |
| 4274 |
goto finish; |
| 4275 |
} |
| 4276 |
|
| 4277 |
/* If we are the system manager, we fork and invoke the generators in a sanitized mount namespace. If |
| 4278 |
* we are the user manager, let's just execute the generators directly. We might not have the |
| 4279 |
* necessary privileges, and the system manager has already mounted /tmp/ and everything else for us. |
| 4280 |
*/ |
| 4281 |
if (MANAGER_IS_USER(m)) { |
| 4282 |
r = manager_execute_generators(m, paths, /* remount_ro= */ false); |
| 4283 |
goto finish; |
| 4284 |
} |
| 4285 |
|
| 4286 |
/* On some systems /tmp/ doesn't exist, and on some other systems we cannot create it at all. Avoid |
| 4287 |
* trying to mount a private tmpfs on it as there's no one size fits all. */ |
| 4288 |
if (is_dir("/tmp", /* follow= */ false) > 0 && !MANAGER_IS_TEST_RUN(m)) |
| 4289 |
flags |= FORK_PRIVATE_TMP; |
| 4290 |
|
| 4291 |
r = pidref_safe_fork("(sd-gens)", flags, /* ret= */ NULL); |
| 4292 |
if (r == 0) { |
| 4293 |
r = manager_execute_generators(m, paths, /* remount_ro= */ true); |
| 4294 |
_exit(r >= 0 ? EXIT_SUCCESS : EXIT_FAILURE); |
| 4295 |
} |
| 4296 |
if (r < 0) { |
| 4297 |
if (!ERRNO_IS_PRIVILEGE(r) && r != -EINVAL) { |
| 4298 |
log_error_errno(r, "Failed to fork off sandboxing environment for executing generators: %m"); |
| 4299 |
goto finish; |
| 4300 |
} |
| 4301 |
|
| 4302 |
/* Failed to fork with new mount namespace? Maybe, running in a container environment with |
| 4303 |
* seccomp or without capability. |
| 4304 |
* |
| 4305 |
* We also allow -EINVAL to allow running without CLONE_NEWNS. |
| 4306 |
* |
| 4307 |
* Also, when running on non-native userland architecture via systemd-nspawn and |
| 4308 |
* qemu-user-static QEMU-emulator, clone() with CLONE_NEWNS fails with EINVAL, see |
| 4309 |
* https://github.com/systemd/systemd/issues/28901. |
| 4310 |
*/ |
| 4311 |
log_debug_errno(r, |
| 4312 |
"Failed to fork off sandboxing environment for executing generators. " |
| 4313 |
"Falling back to execute generators without sandboxing: %m"); |
| 4314 |
r = manager_execute_generators(m, paths, /* remount_ro= */ false); |
| 4315 |
} |
| 4316 |
|
| 4317 |
finish: |
| 4318 |
lookup_paths_trim_generator(&m->lookup_paths); |
| 4319 |
return r; |
| 4320 |
} |
| 4321 |
|
| 4322 |
int manager_transient_environment_add(Manager *m, char **plus) { |
| 4323 |
char **a; |
| 4324 |
|
| 4325 |
assert(m); |
| 4326 |
|
| 4327 |
if (strv_isempty(plus)) |
| 4328 |
return 0; |
| 4329 |
|
| 4330 |
a = strv_env_merge(m->transient_environment, plus); |
| 4331 |
if (!a) |
| 4332 |
return log_oom(); |
| 4333 |
|
| 4334 |
sanitize_environment(a); |
| 4335 |
|
| 4336 |
return strv_free_and_replace(m->transient_environment, a); |
| 4337 |
} |
| 4338 |
|
| 4339 |
int manager_client_environment_modify( |
| 4340 |
Manager *m, |
| 4341 |
char **minus, |
| 4342 |
char **plus) { |
| 4343 |
|
| 4344 |
char **a = NULL, **b = NULL, **l; |
| 4345 |
|
| 4346 |
assert(m); |
| 4347 |
|
| 4348 |
if (strv_isempty(minus) && strv_isempty(plus)) |
| 4349 |
return 0; |
| 4350 |
|
| 4351 |
l = m->client_environment; |
| 4352 |
|
| 4353 |
if (!strv_isempty(minus)) { |
| 4354 |
a = strv_env_delete(l, 1, minus); |
| 4355 |
if (!a) |
| 4356 |
return -ENOMEM; |
| 4357 |
|
| 4358 |
l = a; |
| 4359 |
} |
| 4360 |
|
| 4361 |
if (!strv_isempty(plus)) { |
| 4362 |
b = strv_env_merge(l, plus); |
| 4363 |
if (!b) { |
| 4364 |
strv_free(a); |
| 4365 |
return -ENOMEM; |
| 4366 |
} |
| 4367 |
|
| 4368 |
l = b; |
| 4369 |
} |
| 4370 |
|
| 4371 |
if (m->client_environment != l) |
| 4372 |
strv_free(m->client_environment); |
| 4373 |
|
| 4374 |
if (a != l) |
| 4375 |
strv_free(a); |
| 4376 |
if (b != l) |
| 4377 |
strv_free(b); |
| 4378 |
|
| 4379 |
m->client_environment = sanitize_environment(l); |
| 4380 |
return 0; |
| 4381 |
} |
| 4382 |
|
| 4383 |
int manager_get_effective_environment(Manager *m, char ***ret) { |
| 4384 |
char **l; |
| 4385 |
|
| 4386 |
assert(m); |
| 4387 |
assert(ret); |
| 4388 |
|
| 4389 |
l = strv_env_merge(m->transient_environment, m->client_environment); |
| 4390 |
if (!l) |
| 4391 |
return -ENOMEM; |
| 4392 |
|
| 4393 |
*ret = l; |
| 4394 |
return 0; |
| 4395 |
} |
| 4396 |
|
| 4397 |
int manager_set_unit_defaults(Manager *m, const UnitDefaults *defaults) { |
| 4398 |
_cleanup_free_ char *label = NULL; |
| 4399 |
struct rlimit *rlimit[_RLIMIT_MAX]; |
| 4400 |
int r; |
| 4401 |
|
| 4402 |
assert(m); |
| 4403 |
assert(defaults); |
| 4404 |
|
| 4405 |
if (streq_ptr(defaults->smack_process_label, "/")) |
| 4406 |
label = NULL; |
| 4407 |
else { |
| 4408 |
const char *l = defaults->smack_process_label; |
| 4409 |
#ifdef SMACK_DEFAULT_PROCESS_LABEL |
| 4410 |
if (!l) |
| 4411 |
l = SMACK_DEFAULT_PROCESS_LABEL; |
| 4412 |
#endif |
| 4413 |
if (l) { |
| 4414 |
label = strdup(l); |
| 4415 |
if (!label) |
| 4416 |
return -ENOMEM; |
| 4417 |
} else |
| 4418 |
label = NULL; |
| 4419 |
} |
| 4420 |
|
| 4421 |
r = rlimit_copy_all(rlimit, defaults->rlimit); |
| 4422 |
if (r < 0) |
| 4423 |
return r; |
| 4424 |
|
| 4425 |
m->defaults.std_output = defaults->std_output; |
| 4426 |
m->defaults.std_error = defaults->std_error; |
| 4427 |
|
| 4428 |
m->defaults.restart_usec = defaults->restart_usec; |
| 4429 |
m->defaults.timeout_start_usec = defaults->timeout_start_usec; |
| 4430 |
m->defaults.timeout_stop_usec = defaults->timeout_stop_usec; |
| 4431 |
m->defaults.timeout_abort_usec = defaults->timeout_abort_usec; |
| 4432 |
m->defaults.timeout_abort_set = defaults->timeout_abort_set; |
| 4433 |
m->defaults.device_timeout_usec = defaults->device_timeout_usec; |
| 4434 |
|
| 4435 |
m->defaults.restrict_suid_sgid = defaults->restrict_suid_sgid; |
| 4436 |
|
| 4437 |
m->defaults.start_limit = defaults->start_limit; |
| 4438 |
|
| 4439 |
m->defaults.memory_accounting = defaults->memory_accounting; |
| 4440 |
m->defaults.io_accounting = defaults->io_accounting; |
| 4441 |
m->defaults.tasks_accounting = defaults->tasks_accounting; |
| 4442 |
m->defaults.ip_accounting = defaults->ip_accounting; |
| 4443 |
|
| 4444 |
m->defaults.tasks_max = defaults->tasks_max; |
| 4445 |
m->defaults.timer_accuracy_usec = defaults->timer_accuracy_usec; |
| 4446 |
|
| 4447 |
m->defaults.oom_policy = defaults->oom_policy; |
| 4448 |
m->defaults.oom_score_adjust = defaults->oom_score_adjust; |
| 4449 |
m->defaults.oom_score_adjust_set = defaults->oom_score_adjust_set; |
| 4450 |
|
| 4451 |
memcpy(m->defaults.pressure, defaults->pressure, sizeof(m->defaults.pressure)); |
| 4452 |
|
| 4453 |
m->defaults.memory_zswap_writeback = defaults->memory_zswap_writeback; |
| 4454 |
|
| 4455 |
free_and_replace(m->defaults.smack_process_label, label); |
| 4456 |
rlimit_free_all(m->defaults.rlimit); |
| 4457 |
memcpy(m->defaults.rlimit, rlimit, sizeof(struct rlimit*) * _RLIMIT_MAX); |
| 4458 |
|
| 4459 |
return 0; |
| 4460 |
} |
| 4461 |
|
| 4462 |
void manager_recheck_dbus(Manager *m) { |
| 4463 |
assert(m); |
| 4464 |
|
| 4465 |
/* Connects to the bus if the dbus service and socket are running. If we are running in user mode |
| 4466 |
* this is all it does. In system mode we'll also connect to the system bus (which will most likely |
| 4467 |
* just reuse the connection of the API bus). That's because the system bus after all runs as service |
| 4468 |
* of the system instance, while in the user instance we can assume it's already there. */ |
| 4469 |
|
| 4470 |
if (MANAGER_IS_RELOADING(m)) |
| 4471 |
return; /* don't check while we are reloading… */ |
| 4472 |
|
| 4473 |
if (manager_dbus_is_running(m, false)) { |
| 4474 |
(void) bus_init_api(m); |
| 4475 |
|
| 4476 |
if (MANAGER_IS_SYSTEM(m)) |
| 4477 |
(void) bus_init_system(m); |
| 4478 |
} else { |
| 4479 |
bus_done_api(m); |
| 4480 |
|
| 4481 |
if (MANAGER_IS_SYSTEM(m)) |
| 4482 |
bus_done_system(m); |
| 4483 |
} |
| 4484 |
} |
| 4485 |
|
| 4486 |
static bool manager_journal_is_running(Manager *m) { |
| 4487 |
Unit *u; |
| 4488 |
|
| 4489 |
assert(m); |
| 4490 |
|
| 4491 |
if (MANAGER_IS_TEST_RUN(m)) |
| 4492 |
return false; |
| 4493 |
|
| 4494 |
/* If we are the user manager we can safely assume that the journal is up */ |
| 4495 |
if (!MANAGER_IS_SYSTEM(m)) |
| 4496 |
return true; |
| 4497 |
|
| 4498 |
/* Check that the socket is not only up, but in RUNNING state */ |
| 4499 |
u = manager_get_unit(m, SPECIAL_JOURNALD_SOCKET); |
| 4500 |
if (!u) |
| 4501 |
return false; |
| 4502 |
if (SOCKET(u)->state != SOCKET_RUNNING) |
| 4503 |
return false; |
| 4504 |
|
| 4505 |
/* Similar, check if the daemon itself is fully up, too */ |
| 4506 |
u = manager_get_unit(m, SPECIAL_JOURNALD_SERVICE); |
| 4507 |
if (!u) |
| 4508 |
return false; |
| 4509 |
if (!UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u)) || SERVICE(u)->state == SERVICE_EXITED) |
| 4510 |
return false; |
| 4511 |
|
| 4512 |
return true; |
| 4513 |
} |
| 4514 |
|
| 4515 |
void disable_printk_ratelimit(void) { |
| 4516 |
/* Disable kernel's printk ratelimit. |
| 4517 |
* |
| 4518 |
* Logging to /dev/kmsg is most useful during early boot and shutdown, where normal logging |
| 4519 |
* mechanisms are not available. The semantics of this sysctl are such that any kernel command-line |
| 4520 |
* setting takes precedence. */ |
| 4521 |
int r; |
| 4522 |
|
| 4523 |
r = sysctl_write("kernel/printk_devkmsg", "on"); |
| 4524 |
if (r < 0) |
| 4525 |
log_debug_errno(r, "Failed to set sysctl kernel.printk_devkmsg=on: %m"); |
| 4526 |
} |
| 4527 |
|
| 4528 |
void manager_recheck_journal(Manager *m) { |
| 4529 |
|
| 4530 |
assert(m); |
| 4531 |
|
| 4532 |
/* Don't bother with this unless we are in the special situation of being PID 1 */ |
| 4533 |
if (getpid_cached() != 1) |
| 4534 |
return; |
| 4535 |
|
| 4536 |
/* Don't check this while we are reloading, things might still change */ |
| 4537 |
if (MANAGER_IS_RELOADING(m)) |
| 4538 |
return; |
| 4539 |
|
| 4540 |
/* The journal is fully and entirely up? If so, let's permit logging to it, if that's configured. If |
| 4541 |
* the journal is down, don't ever log to it, otherwise we might end up deadlocking ourselves as we |
| 4542 |
* might trigger an activation ourselves we can't fulfill. */ |
| 4543 |
log_set_prohibit_ipc(!manager_journal_is_running(m)); |
| 4544 |
log_open(); |
| 4545 |
} |
| 4546 |
|
| 4547 |
static ShowStatus manager_get_show_status(Manager *m) { |
| 4548 |
assert(m); |
| 4549 |
|
| 4550 |
if (MANAGER_IS_USER(m)) |
| 4551 |
return _SHOW_STATUS_INVALID; |
| 4552 |
|
| 4553 |
if (m->show_status_overridden != _SHOW_STATUS_INVALID) |
| 4554 |
return m->show_status_overridden; |
| 4555 |
|
| 4556 |
return m->show_status; |
| 4557 |
} |
| 4558 |
|
| 4559 |
bool manager_get_show_status_on(Manager *m) { |
| 4560 |
assert(m); |
| 4561 |
|
| 4562 |
return show_status_on(manager_get_show_status(m)); |
| 4563 |
} |
| 4564 |
|
| 4565 |
static void set_show_status_marker(bool b) { |
| 4566 |
if (b) |
| 4567 |
(void) touch("/run/systemd/show-status"); |
| 4568 |
else |
| 4569 |
(void) unlink("/run/systemd/show-status"); |
| 4570 |
} |
| 4571 |
|
| 4572 |
void manager_set_show_status(Manager *m, ShowStatus mode, const char *reason) { |
| 4573 |
assert(m); |
| 4574 |
assert(reason); |
| 4575 |
assert(mode >= 0 && mode < _SHOW_STATUS_MAX); |
| 4576 |
|
| 4577 |
if (MANAGER_IS_USER(m)) |
| 4578 |
return; |
| 4579 |
|
| 4580 |
if (mode == m->show_status) |
| 4581 |
return; |
| 4582 |
|
| 4583 |
if (m->show_status_overridden == _SHOW_STATUS_INVALID) { |
| 4584 |
bool enabled; |
| 4585 |
|
| 4586 |
enabled = show_status_on(mode); |
| 4587 |
log_debug("%s (%s) showing of status (%s).", |
| 4588 |
enabled ? "Enabling" : "Disabling", |
| 4589 |
strna(show_status_to_string(mode)), |
| 4590 |
reason); |
| 4591 |
|
| 4592 |
set_show_status_marker(enabled); |
| 4593 |
} |
| 4594 |
|
| 4595 |
m->show_status = mode; |
| 4596 |
} |
| 4597 |
|
| 4598 |
void manager_override_show_status(Manager *m, ShowStatus mode, const char *reason) { |
| 4599 |
assert(m); |
| 4600 |
assert(mode < _SHOW_STATUS_MAX); |
| 4601 |
|
| 4602 |
if (MANAGER_IS_USER(m)) |
| 4603 |
return; |
| 4604 |
|
| 4605 |
if (mode == m->show_status_overridden) |
| 4606 |
return; |
| 4607 |
|
| 4608 |
m->show_status_overridden = mode; |
| 4609 |
|
| 4610 |
if (mode == _SHOW_STATUS_INVALID) |
| 4611 |
mode = m->show_status; |
| 4612 |
|
| 4613 |
log_debug("%s (%s) showing of status (%s).", |
| 4614 |
m->show_status_overridden != _SHOW_STATUS_INVALID ? "Overriding" : "Restoring", |
| 4615 |
strna(show_status_to_string(mode)), |
| 4616 |
reason); |
| 4617 |
|
| 4618 |
set_show_status_marker(show_status_on(mode)); |
| 4619 |
} |
| 4620 |
|
| 4621 |
const char* manager_get_confirm_spawn(Manager *m) { |
| 4622 |
static int last_errno = 0; |
| 4623 |
struct stat st; |
| 4624 |
int r; |
| 4625 |
|
| 4626 |
assert(m); |
| 4627 |
|
| 4628 |
/* Here's the deal: we want to test the validity of the console but don't want |
| 4629 |
* PID1 to go through the whole console process which might block. But we also |
| 4630 |
* want to warn the user only once if something is wrong with the console so we |
| 4631 |
* cannot do the sanity checks after spawning our children. So here we simply do |
| 4632 |
* really basic tests to hopefully trap common errors. |
| 4633 |
* |
| 4634 |
* If the console suddenly disappear at the time our children will really it |
| 4635 |
* then they will simply fail to acquire it and a positive answer will be |
| 4636 |
* assumed. New children will fall back to /dev/console though. |
| 4637 |
* |
| 4638 |
* Note: TTYs are devices that can come and go any time, and frequently aren't |
| 4639 |
* available yet during early boot (consider a USB rs232 dongle...). If for any |
| 4640 |
* reason the configured console is not ready, we fall back to the default |
| 4641 |
* console. */ |
| 4642 |
|
| 4643 |
if (!m->confirm_spawn || path_equal(m->confirm_spawn, "/dev/console")) |
| 4644 |
return m->confirm_spawn; |
| 4645 |
|
| 4646 |
if (stat(m->confirm_spawn, &st) < 0) { |
| 4647 |
r = -errno; |
| 4648 |
goto fail; |
| 4649 |
} |
| 4650 |
|
| 4651 |
r = stat_verify_char(&st); |
| 4652 |
if (r < 0) |
| 4653 |
goto fail; |
| 4654 |
|
| 4655 |
last_errno = 0; |
| 4656 |
return m->confirm_spawn; |
| 4657 |
|
| 4658 |
fail: |
| 4659 |
if (last_errno != r) |
| 4660 |
last_errno = log_warning_errno(r, "Failed to open %s, using default console: %m", m->confirm_spawn); |
| 4661 |
|
| 4662 |
return "/dev/console"; |
| 4663 |
} |
| 4664 |
|
| 4665 |
void manager_set_first_boot(Manager *m, bool b) { |
| 4666 |
assert(m); |
| 4667 |
|
| 4668 |
if (!MANAGER_IS_SYSTEM(m)) |
| 4669 |
return; |
| 4670 |
|
| 4671 |
if (m->first_boot != (int) b) { |
| 4672 |
if (b) |
| 4673 |
(void) touch("/run/systemd/first-boot"); |
| 4674 |
else |
| 4675 |
(void) unlink("/run/systemd/first-boot"); |
| 4676 |
} |
| 4677 |
|
| 4678 |
m->first_boot = b; |
| 4679 |
} |
| 4680 |
|
| 4681 |
void manager_disable_confirm_spawn(void) { |
| 4682 |
(void) touch("/run/systemd/confirm_spawn_disabled"); |
| 4683 |
} |
| 4684 |
|
| 4685 |
static bool manager_should_show_status(Manager *m, StatusType type) { |
| 4686 |
assert(m); |
| 4687 |
|
| 4688 |
if (!MANAGER_IS_SYSTEM(m)) |
| 4689 |
return false; |
| 4690 |
|
| 4691 |
if (m->no_console_output) |
| 4692 |
return false; |
| 4693 |
|
| 4694 |
if (!IN_SET(manager_state(m), MANAGER_INITIALIZING, MANAGER_STARTING, MANAGER_STOPPING)) |
| 4695 |
return false; |
| 4696 |
|
| 4697 |
/* If we cannot find out the status properly, just proceed. */ |
| 4698 |
if (type < STATUS_TYPE_EMERGENCY && manager_check_ask_password(m) > 0) |
| 4699 |
return false; |
| 4700 |
|
| 4701 |
if (type >= STATUS_TYPE_NOTICE && manager_get_show_status(m) != SHOW_STATUS_NO) |
| 4702 |
return true; |
| 4703 |
|
| 4704 |
return manager_get_show_status_on(m); |
| 4705 |
} |
| 4706 |
|
| 4707 |
void manager_status_printf(Manager *m, StatusType type, const char *status, const char *format, ...) { |
| 4708 |
va_list ap; |
| 4709 |
|
| 4710 |
/* If m is NULL, assume we're after shutdown and let the messages through. */ |
| 4711 |
|
| 4712 |
if (m && !manager_should_show_status(m, type)) |
| 4713 |
return; |
| 4714 |
|
| 4715 |
/* XXX We should totally drop the check for ephemeral here |
| 4716 |
* and thus effectively make 'Type=idle' pointless. */ |
| 4717 |
if (type == STATUS_TYPE_EPHEMERAL && m && m->n_on_console > 0) |
| 4718 |
return; |
| 4719 |
|
| 4720 |
va_start(ap, format); |
| 4721 |
status_vprintf(status, SHOW_STATUS_ELLIPSIZE|(type == STATUS_TYPE_EPHEMERAL ? SHOW_STATUS_EPHEMERAL : 0), format, ap); |
| 4722 |
va_end(ap); |
| 4723 |
} |
| 4724 |
|
| 4725 |
Set* manager_get_units_needing_mounts_for(Manager *m, const char *path, UnitMountDependencyType t) { |
| 4726 |
assert(m); |
| 4727 |
assert(path); |
| 4728 |
assert(t >= 0 && t < _UNIT_MOUNT_DEPENDENCY_TYPE_MAX); |
| 4729 |
|
| 4730 |
if (path_equal(path, "/")) |
| 4731 |
path = ""; |
| 4732 |
|
| 4733 |
return hashmap_get(m->units_needing_mounts_for[t], path); |
| 4734 |
} |
| 4735 |
|
| 4736 |
int manager_update_failed_units(Manager *m, Unit *u, bool failed) { |
| 4737 |
unsigned size; |
| 4738 |
int r; |
| 4739 |
|
| 4740 |
assert(m); |
| 4741 |
assert(u->manager == m); |
| 4742 |
|
| 4743 |
size = set_size(m->failed_units); |
| 4744 |
|
| 4745 |
if (failed) { |
| 4746 |
r = set_ensure_put(&m->failed_units, NULL, u); |
| 4747 |
if (r < 0) |
| 4748 |
return log_oom(); |
| 4749 |
} else |
| 4750 |
(void) set_remove(m->failed_units, u); |
| 4751 |
|
| 4752 |
if (set_size(m->failed_units) != size) |
| 4753 |
bus_manager_send_change_signal(m); |
| 4754 |
|
| 4755 |
return 0; |
| 4756 |
} |
| 4757 |
|
| 4758 |
ManagerState manager_state(Manager *m) { |
| 4759 |
Unit *u; |
| 4760 |
|
| 4761 |
assert(m); |
| 4762 |
|
| 4763 |
/* Is the special shutdown target active or queued? If so, we are in shutdown state */ |
| 4764 |
u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET); |
| 4765 |
if (u && unit_active_or_pending(u)) |
| 4766 |
return MANAGER_STOPPING; |
| 4767 |
|
| 4768 |
/* Did we ever finish booting? If not then we are still starting up */ |
| 4769 |
if (!MANAGER_IS_FINISHED(m)) { |
| 4770 |
|
| 4771 |
u = manager_get_unit(m, SPECIAL_BASIC_TARGET); |
| 4772 |
if (!u || !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) |
| 4773 |
return MANAGER_INITIALIZING; |
| 4774 |
|
| 4775 |
return MANAGER_STARTING; |
| 4776 |
} |
| 4777 |
|
| 4778 |
if (MANAGER_IS_SYSTEM(m)) { |
| 4779 |
/* Are the rescue or emergency targets active or queued? If so we are in maintenance state */ |
| 4780 |
u = manager_get_unit(m, SPECIAL_RESCUE_TARGET); |
| 4781 |
if (u && unit_active_or_pending(u)) |
| 4782 |
return MANAGER_MAINTENANCE; |
| 4783 |
|
| 4784 |
u = manager_get_unit(m, SPECIAL_EMERGENCY_TARGET); |
| 4785 |
if (u && unit_active_or_pending(u)) |
| 4786 |
return MANAGER_MAINTENANCE; |
| 4787 |
} |
| 4788 |
|
| 4789 |
/* Are there any failed units or ordering cycles? If so, we are in degraded mode */ |
| 4790 |
if (!set_isempty(m->failed_units) || !set_isempty(m->transactions_with_cycle)) |
| 4791 |
return MANAGER_DEGRADED; |
| 4792 |
|
| 4793 |
return MANAGER_RUNNING; |
| 4794 |
} |
| 4795 |
|
| 4796 |
static void manager_unref_uid_internal( |
| 4797 |
Hashmap *uid_refs, |
| 4798 |
uid_t uid, |
| 4799 |
bool destroy_now, |
| 4800 |
int (*_clean_ipc)(uid_t uid)) { |
| 4801 |
|
| 4802 |
uint32_t c, n; |
| 4803 |
|
| 4804 |
assert(uid_is_valid(uid)); |
| 4805 |
assert(_clean_ipc); |
| 4806 |
|
| 4807 |
/* A generic implementation, covering both manager_unref_uid() and manager_unref_gid(), under the |
| 4808 |
* assumption that uid_t and gid_t are actually defined the same way, with the same validity rules. |
| 4809 |
* |
| 4810 |
* We store a hashmap where the key is the UID/GID and the value is a 32-bit reference counter, whose |
| 4811 |
* highest bit is used as flag for marking UIDs/GIDs whose IPC objects to remove when the last |
| 4812 |
* reference to the UID/GID is dropped. The flag is set to on, once at least one reference from a |
| 4813 |
* unit where RemoveIPC= is set is added on a UID/GID. It is reset when the UID's/GID's reference |
| 4814 |
* counter drops to 0 again. */ |
| 4815 |
|
| 4816 |
assert_cc(sizeof(uid_t) == sizeof(gid_t)); |
| 4817 |
assert_cc(UID_INVALID == (uid_t) GID_INVALID); |
| 4818 |
|
| 4819 |
if (uid == 0) /* We don't keep track of root, and will never destroy it */ |
| 4820 |
return; |
| 4821 |
|
| 4822 |
c = PTR_TO_UINT32(hashmap_get(uid_refs, UID_TO_PTR(uid))); |
| 4823 |
|
| 4824 |
n = c & ~DESTROY_IPC_FLAG; |
| 4825 |
assert(n > 0); |
| 4826 |
n--; |
| 4827 |
|
| 4828 |
if (destroy_now && n == 0) { |
| 4829 |
hashmap_remove(uid_refs, UID_TO_PTR(uid)); |
| 4830 |
|
| 4831 |
if (c & DESTROY_IPC_FLAG) { |
| 4832 |
log_debug("%s " UID_FMT " is no longer referenced, cleaning up its IPC.", |
| 4833 |
_clean_ipc == clean_ipc_by_uid ? "UID" : "GID", |
| 4834 |
uid); |
| 4835 |
(void) _clean_ipc(uid); |
| 4836 |
} |
| 4837 |
} else { |
| 4838 |
c = n | (c & DESTROY_IPC_FLAG); |
| 4839 |
assert_se(hashmap_update(uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c)) >= 0); |
| 4840 |
} |
| 4841 |
} |
| 4842 |
|
| 4843 |
void manager_unref_uid(Manager *m, uid_t uid, bool destroy_now) { |
| 4844 |
manager_unref_uid_internal(m->uid_refs, uid, destroy_now, clean_ipc_by_uid); |
| 4845 |
} |
| 4846 |
|
| 4847 |
void manager_unref_gid(Manager *m, gid_t gid, bool destroy_now) { |
| 4848 |
manager_unref_uid_internal(m->gid_refs, (uid_t) gid, destroy_now, clean_ipc_by_gid); |
| 4849 |
} |
| 4850 |
|
| 4851 |
static int manager_ref_uid_internal( |
| 4852 |
Hashmap **uid_refs, |
| 4853 |
uid_t uid, |
| 4854 |
bool clean_ipc) { |
| 4855 |
|
| 4856 |
uint32_t c, n; |
| 4857 |
int r; |
| 4858 |
|
| 4859 |
assert(uid_refs); |
| 4860 |
assert(uid_is_valid(uid)); |
| 4861 |
|
| 4862 |
/* A generic implementation, covering both manager_ref_uid() and manager_ref_gid(), under the |
| 4863 |
* assumption that uid_t and gid_t are actually defined the same way, with the same validity |
| 4864 |
* rules. */ |
| 4865 |
|
| 4866 |
assert_cc(sizeof(uid_t) == sizeof(gid_t)); |
| 4867 |
assert_cc(UID_INVALID == (uid_t) GID_INVALID); |
| 4868 |
|
| 4869 |
if (uid == 0) /* We don't keep track of root, and will never destroy it */ |
| 4870 |
return 0; |
| 4871 |
|
| 4872 |
r = hashmap_ensure_allocated(uid_refs, &trivial_hash_ops); |
| 4873 |
if (r < 0) |
| 4874 |
return r; |
| 4875 |
|
| 4876 |
c = PTR_TO_UINT32(hashmap_get(*uid_refs, UID_TO_PTR(uid))); |
| 4877 |
|
| 4878 |
n = c & ~DESTROY_IPC_FLAG; |
| 4879 |
n++; |
| 4880 |
|
| 4881 |
if (n & DESTROY_IPC_FLAG) /* check for overflow */ |
| 4882 |
return -EOVERFLOW; |
| 4883 |
|
| 4884 |
c = n | (c & DESTROY_IPC_FLAG) | (clean_ipc ? DESTROY_IPC_FLAG : 0); |
| 4885 |
|
| 4886 |
return hashmap_replace(*uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c)); |
| 4887 |
} |
| 4888 |
|
| 4889 |
int manager_ref_uid(Manager *m, uid_t uid, bool clean_ipc) { |
| 4890 |
return manager_ref_uid_internal(&m->uid_refs, uid, clean_ipc); |
| 4891 |
} |
| 4892 |
|
| 4893 |
int manager_ref_gid(Manager *m, gid_t gid, bool clean_ipc) { |
| 4894 |
return manager_ref_uid_internal(&m->gid_refs, (uid_t) gid, clean_ipc); |
| 4895 |
} |
| 4896 |
|
| 4897 |
static void manager_vacuum_uid_refs_internal( |
| 4898 |
Hashmap *uid_refs, |
| 4899 |
int (*_clean_ipc)(uid_t uid)) { |
| 4900 |
|
| 4901 |
void *p, *k; |
| 4902 |
|
| 4903 |
assert(_clean_ipc); |
| 4904 |
|
| 4905 |
HASHMAP_FOREACH_KEY(p, k, uid_refs) { |
| 4906 |
uint32_t c, n; |
| 4907 |
uid_t uid; |
| 4908 |
|
| 4909 |
uid = PTR_TO_UID(k); |
| 4910 |
c = PTR_TO_UINT32(p); |
| 4911 |
|
| 4912 |
n = c & ~DESTROY_IPC_FLAG; |
| 4913 |
if (n > 0) |
| 4914 |
continue; |
| 4915 |
|
| 4916 |
if (c & DESTROY_IPC_FLAG) { |
| 4917 |
log_debug("Found unreferenced %s " UID_FMT " after reload/reexec. Cleaning up.", |
| 4918 |
_clean_ipc == clean_ipc_by_uid ? "UID" : "GID", |
| 4919 |
uid); |
| 4920 |
(void) _clean_ipc(uid); |
| 4921 |
} |
| 4922 |
|
| 4923 |
assert_se(hashmap_remove(uid_refs, k) == p); |
| 4924 |
} |
| 4925 |
} |
| 4926 |
|
| 4927 |
static void manager_vacuum_uid_refs(Manager *m) { |
| 4928 |
manager_vacuum_uid_refs_internal(m->uid_refs, clean_ipc_by_uid); |
| 4929 |
} |
| 4930 |
|
| 4931 |
static void manager_vacuum_gid_refs(Manager *m) { |
| 4932 |
manager_vacuum_uid_refs_internal(m->gid_refs, clean_ipc_by_gid); |
| 4933 |
} |
| 4934 |
|
| 4935 |
static void manager_vacuum(Manager *m) { |
| 4936 |
assert(m); |
| 4937 |
|
| 4938 |
/* Release any dynamic users no longer referenced */ |
| 4939 |
dynamic_user_vacuum(m, true); |
| 4940 |
|
| 4941 |
/* Release any references to UIDs/GIDs no longer referenced, and destroy any IPC owned by them */ |
| 4942 |
manager_vacuum_uid_refs(m); |
| 4943 |
manager_vacuum_gid_refs(m); |
| 4944 |
|
| 4945 |
/* Release any runtimes no longer referenced */ |
| 4946 |
exec_shared_runtime_vacuum(m); |
| 4947 |
} |
| 4948 |
|
| 4949 |
static int manager_dispatch_user_lookup_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
| 4950 |
struct buffer { |
| 4951 |
uid_t uid; |
| 4952 |
gid_t gid; |
| 4953 |
char unit_name[UNIT_NAME_MAX+1]; |
| 4954 |
} _packed_ buffer; |
| 4955 |
|
| 4956 |
Manager *m = ASSERT_PTR(userdata); |
| 4957 |
ssize_t l; |
| 4958 |
size_t n; |
| 4959 |
Unit *u; |
| 4960 |
|
| 4961 |
assert(source); |
| 4962 |
|
| 4963 |
/* Invoked whenever a child process succeeded resolving its user/group to use and sent us the |
| 4964 |
* resulting UID/GID in a datagram. We parse the datagram here and pass it off to the unit, so that |
| 4965 |
* it can add a reference to the UID/GID so that it can destroy the UID/GID's IPC objects when the |
| 4966 |
* reference counter drops to 0. */ |
| 4967 |
|
| 4968 |
l = recv(fd, &buffer, sizeof(buffer), MSG_DONTWAIT); |
| 4969 |
if (l < 0) { |
| 4970 |
if (ERRNO_IS_TRANSIENT(errno)) |
| 4971 |
return 0; |
| 4972 |
|
| 4973 |
return log_error_errno(errno, "Failed to read from user lookup fd: %m"); |
| 4974 |
} |
| 4975 |
|
| 4976 |
if ((size_t) l <= offsetof(struct buffer, unit_name)) { |
| 4977 |
log_warning("Received too short user lookup message, ignoring."); |
| 4978 |
return 0; |
| 4979 |
} |
| 4980 |
|
| 4981 |
if ((size_t) l > offsetof(struct buffer, unit_name) + UNIT_NAME_MAX) { |
| 4982 |
log_warning("Received too long user lookup message, ignoring."); |
| 4983 |
return 0; |
| 4984 |
} |
| 4985 |
|
| 4986 |
if (!uid_is_valid(buffer.uid) && !gid_is_valid(buffer.gid)) { |
| 4987 |
log_warning("Got user lookup message with invalid UID/GID pair, ignoring."); |
| 4988 |
return 0; |
| 4989 |
} |
| 4990 |
|
| 4991 |
n = (size_t) l - offsetof(struct buffer, unit_name); |
| 4992 |
if (memchr(buffer.unit_name, 0, n)) { |
| 4993 |
log_warning("Received lookup message with embedded NUL character, ignoring."); |
| 4994 |
return 0; |
| 4995 |
} |
| 4996 |
|
| 4997 |
buffer.unit_name[n] = 0; |
| 4998 |
u = manager_get_unit(m, buffer.unit_name); |
| 4999 |
if (!u) { |
| 5000 |
log_debug("Got user lookup message but unit doesn't exist, ignoring."); |
| 5001 |
return 0; |
| 5002 |
} |
| 5003 |
|
| 5004 |
log_unit_debug(u, "User lookup succeeded: uid=" UID_FMT " gid=" GID_FMT, buffer.uid, buffer.gid); |
| 5005 |
|
| 5006 |
unit_notify_user_lookup(u, buffer.uid, buffer.gid); |
| 5007 |
return 0; |
| 5008 |
} |
| 5009 |
|
| 5010 |
static int manager_dispatch_handoff_timestamp_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
| 5011 |
Manager *m = ASSERT_PTR(userdata); |
| 5012 |
usec_t ts[2] = {}; |
| 5013 |
CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control; |
| 5014 |
struct msghdr msghdr = { |
| 5015 |
.msg_iov = &IOVEC_MAKE(ts, sizeof(ts)), |
| 5016 |
.msg_iovlen = 1, |
| 5017 |
.msg_control = &control, |
| 5018 |
.msg_controllen = sizeof(control), |
| 5019 |
}; |
| 5020 |
ssize_t n; |
| 5021 |
|
| 5022 |
assert(source); |
| 5023 |
|
| 5024 |
n = recvmsg_safe(m->handoff_timestamp_fds[0], &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC); |
| 5025 |
if (ERRNO_IS_NEG_TRANSIENT(n)) |
| 5026 |
return 0; /* Spurious wakeup, try again */ |
| 5027 |
if (n == -ECHRNG) { |
| 5028 |
log_warning_errno(n, "Got message with truncated control data (unexpected fds sent?), ignoring."); |
| 5029 |
return 0; |
| 5030 |
} |
| 5031 |
if (n == -EXFULL) { |
| 5032 |
log_warning_errno(n, "Got message with truncated payload data, ignoring."); |
| 5033 |
return 0; |
| 5034 |
} |
| 5035 |
if (n < 0) |
| 5036 |
return log_error_errno(n, "Failed to receive handoff timestamp message: %m"); |
| 5037 |
|
| 5038 |
cmsg_close_all(&msghdr); |
| 5039 |
|
| 5040 |
if (n != sizeof(ts)) { |
| 5041 |
log_warning("Got handoff timestamp message of unexpected size %zi (expected %zu), ignoring.", n, sizeof(ts)); |
| 5042 |
return 0; |
| 5043 |
} |
| 5044 |
|
| 5045 |
struct ucred *ucred = CMSG_FIND_DATA(&msghdr, SOL_SOCKET, SCM_CREDENTIALS, struct ucred); |
| 5046 |
if (!ucred || !pid_is_valid(ucred->pid)) { |
| 5047 |
log_warning("Received handoff timestamp message without valid credentials. Ignoring."); |
| 5048 |
return 0; |
| 5049 |
} |
| 5050 |
|
| 5051 |
log_debug("Got handoff timestamp event for PID " PID_FMT ".", ucred->pid); |
| 5052 |
|
| 5053 |
_cleanup_free_ Unit **units = NULL; |
| 5054 |
int n_units = manager_get_units_for_pidref(m, &PIDREF_MAKE_FROM_PID(ucred->pid), &units); |
| 5055 |
if (n_units < 0) { |
| 5056 |
log_warning_errno(n_units, "Unable to determine units for PID " PID_FMT ", ignoring: %m", ucred->pid); |
| 5057 |
return 0; |
| 5058 |
} |
| 5059 |
if (n_units == 0) { |
| 5060 |
log_debug("Got handoff timestamp for process " PID_FMT " we are not interested in, ignoring.", ucred->pid); |
| 5061 |
return 0; |
| 5062 |
} |
| 5063 |
|
| 5064 |
dual_timestamp dt = { |
| 5065 |
.realtime = ts[0], |
| 5066 |
.monotonic = ts[1], |
| 5067 |
}; |
| 5068 |
|
| 5069 |
FOREACH_ARRAY(u, units, n_units) { |
| 5070 |
if (!UNIT_VTABLE(*u)->notify_handoff_timestamp) |
| 5071 |
continue; |
| 5072 |
|
| 5073 |
UNIT_VTABLE(*u)->notify_handoff_timestamp(*u, ucred, &dt); |
| 5074 |
} |
| 5075 |
|
| 5076 |
return 0; |
| 5077 |
} |
| 5078 |
|
| 5079 |
static int manager_dispatch_pidref_transport_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
| 5080 |
Manager *m = ASSERT_PTR(userdata); |
| 5081 |
_cleanup_(pidref_done) PidRef child_pidref = PIDREF_NULL, parent_pidref = PIDREF_NULL; |
| 5082 |
_cleanup_close_ int child_pidfd = -EBADF, parent_pidfd = -EBADF; |
| 5083 |
struct ucred *ucred = NULL; |
| 5084 |
CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred)) + CMSG_SPACE(sizeof(int)) * 2) control; |
| 5085 |
pid_t child_pid = 0; /* silence false-positive warning by coverity */ |
| 5086 |
struct msghdr msghdr = { |
| 5087 |
.msg_iov = &IOVEC_MAKE(&child_pid, sizeof(child_pid)), |
| 5088 |
.msg_iovlen = 1, |
| 5089 |
.msg_control = &control, |
| 5090 |
.msg_controllen = sizeof(control), |
| 5091 |
}; |
| 5092 |
struct cmsghdr *cmsg; |
| 5093 |
ssize_t n; |
| 5094 |
int r; |
| 5095 |
|
| 5096 |
assert(source); |
| 5097 |
|
| 5098 |
/* Server expects: |
| 5099 |
* - Parent PID in ucreds enabled via SO_PASSCRED |
| 5100 |
* - Parent PIDFD in SCM_PIDFD message enabled via SO_PASSPIDFD |
| 5101 |
* - Child PIDFD in SCM_RIGHTS in message body |
| 5102 |
* - Child PID in message IOV |
| 5103 |
* |
| 5104 |
* SO_PASSPIDFD may not be supported by the kernel (it is supported since v6.5) so we fall back to |
| 5105 |
* using parent PID from ucreds and accept some raciness. */ |
| 5106 |
n = recvmsg_safe(m->pidref_transport_fds[0], &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC|MSG_TRUNC); |
| 5107 |
if (ERRNO_IS_NEG_TRANSIENT(n)) |
| 5108 |
return 0; /* Spurious wakeup, try again */ |
| 5109 |
if (n == -ECHRNG) { |
| 5110 |
log_warning_errno(n, "Got message with truncated control data (unexpected fds sent?), ignoring."); |
| 5111 |
return 0; |
| 5112 |
} |
| 5113 |
if (n == -EXFULL) { |
| 5114 |
log_warning_errno(n, "Got message with truncated payload data, ignoring."); |
| 5115 |
return 0; |
| 5116 |
} |
| 5117 |
if (n < 0) |
| 5118 |
return log_error_errno(n, "Failed to receive pidref message: %m"); |
| 5119 |
|
| 5120 |
if (n != sizeof(child_pid)) { |
| 5121 |
log_warning("Got pidref message of unexpected size %zi (expected %zu), ignoring.", n, sizeof(child_pid)); |
| 5122 |
cmsg_close_all(&msghdr); |
| 5123 |
return 0; |
| 5124 |
} |
| 5125 |
|
| 5126 |
CMSG_FOREACH(cmsg, &msghdr) { |
| 5127 |
if (cmsg->cmsg_level != SOL_SOCKET) |
| 5128 |
continue; |
| 5129 |
|
| 5130 |
if (cmsg->cmsg_type == SCM_CREDENTIALS && cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) { |
| 5131 |
assert(!ucred); |
| 5132 |
ucred = CMSG_TYPED_DATA(cmsg, struct ucred); |
| 5133 |
} else if (cmsg->cmsg_type == SCM_PIDFD) { |
| 5134 |
assert(parent_pidfd < 0); |
| 5135 |
parent_pidfd = *CMSG_TYPED_DATA(cmsg, int); |
| 5136 |
} else if (cmsg->cmsg_type == SCM_RIGHTS) { |
| 5137 |
assert(child_pidfd < 0); |
| 5138 |
child_pidfd = *CMSG_TYPED_DATA(cmsg, int); |
| 5139 |
} |
| 5140 |
} |
| 5141 |
|
| 5142 |
/* From this point on, the fds are owned by our local variables. Call cmsg_close_all no more. */ |
| 5143 |
|
| 5144 |
/* Verify and set parent pidref. */ |
| 5145 |
if (!ucred || !pid_is_valid(ucred->pid)) { |
| 5146 |
log_warning("Received pidref message without valid credentials. Ignoring."); |
| 5147 |
return 0; |
| 5148 |
} |
| 5149 |
|
| 5150 |
/* Need to handle kernels without SO_PASSPIDFD where SCM_PIDFD will not be set. */ |
| 5151 |
if (parent_pidfd >= 0) |
| 5152 |
r = pidref_set_pidfd_consume(&parent_pidref, TAKE_FD(parent_pidfd)); |
| 5153 |
else |
| 5154 |
r = pidref_set_pid(&parent_pidref, ucred->pid); |
| 5155 |
if (r < 0) { |
| 5156 |
if (r == -ESRCH) |
| 5157 |
log_debug_errno(r, "PidRef child process died before message is processed. Ignoring."); |
| 5158 |
else |
| 5159 |
log_warning_errno(r, "Failed to pin pidref child process, ignoring message: %m"); |
| 5160 |
return 0; |
| 5161 |
} |
| 5162 |
|
| 5163 |
if (parent_pidref.pid != ucred->pid) { |
| 5164 |
assert(parent_pidref.fd >= 0); |
| 5165 |
log_warning("Got SCM_PIDFD for parent process " PID_FMT " but got SCM_CREDENTIALS for parent process " PID_FMT ". Ignoring.", |
| 5166 |
parent_pidref.pid, ucred->pid); |
| 5167 |
return 0; |
| 5168 |
} |
| 5169 |
|
| 5170 |
/* Verify and set child pidref. */ |
| 5171 |
if (!pid_is_valid(child_pid)) { |
| 5172 |
log_warning("Received pidref message without valid child PID. Ignoring."); |
| 5173 |
return 0; |
| 5174 |
} |
| 5175 |
|
| 5176 |
/* Need to handle kernels without PIDFD support. */ |
| 5177 |
if (child_pidfd >= 0) |
| 5178 |
r = pidref_set_pidfd_consume(&child_pidref, TAKE_FD(child_pidfd)); |
| 5179 |
else |
| 5180 |
r = pidref_set_pid(&child_pidref, child_pid); |
| 5181 |
if (r < 0) { |
| 5182 |
if (r == -ESRCH) |
| 5183 |
log_debug_errno(r, "PidRef child process died before message is processed. Ignoring."); |
| 5184 |
else |
| 5185 |
log_warning_errno(r, "Failed to pin pidref child process, ignoring message: %m"); |
| 5186 |
return 0; |
| 5187 |
} |
| 5188 |
|
| 5189 |
if (child_pidref.pid != child_pid) { |
| 5190 |
assert(child_pidref.fd >= 0); |
| 5191 |
log_warning("Got SCM_RIGHTS for child process " PID_FMT " but PID in IOV message is " PID_FMT ". Ignoring.", |
| 5192 |
child_pidref.pid, child_pid); |
| 5193 |
return 0; |
| 5194 |
} |
| 5195 |
|
| 5196 |
log_debug("Got pidref event with parent PID " PID_FMT " and child PID " PID_FMT ".", parent_pidref.pid, child_pidref.pid); |
| 5197 |
|
| 5198 |
/* Try finding cgroup of parent process. But if parent process exited and we're not using PIDFD, this could return NULL. |
| 5199 |
* Then fall back to finding cgroup of the child process. */ |
| 5200 |
Unit *u = manager_get_unit_by_pidref_cgroup(m, &parent_pidref); |
| 5201 |
if (!u) |
| 5202 |
u = manager_get_unit_by_pidref_cgroup(m, &child_pidref); |
| 5203 |
if (!u) { |
| 5204 |
log_debug("Got pidref for parent process " PID_FMT " and child process " PID_FMT " we are not interested in, ignoring.", parent_pidref.pid, child_pidref.pid); |
| 5205 |
return 0; |
| 5206 |
} |
| 5207 |
|
| 5208 |
if (!UNIT_VTABLE(u)->notify_pidref) { |
| 5209 |
log_unit_warning(u, "Received pidref event from unexpected unit type '%s'.", unit_type_to_string(u->type)); |
| 5210 |
return 0; |
| 5211 |
} |
| 5212 |
|
| 5213 |
UNIT_VTABLE(u)->notify_pidref(u, &parent_pidref, &child_pidref); |
| 5214 |
|
| 5215 |
return 0; |
| 5216 |
} |
| 5217 |
|
| 5218 |
void manager_ref_console(Manager *m) { |
| 5219 |
assert(m); |
| 5220 |
|
| 5221 |
m->n_on_console++; |
| 5222 |
} |
| 5223 |
|
| 5224 |
void manager_unref_console(Manager *m) { |
| 5225 |
|
| 5226 |
assert(m->n_on_console > 0); |
| 5227 |
m->n_on_console--; |
| 5228 |
|
| 5229 |
if (m->n_on_console == 0) |
| 5230 |
m->no_console_output = false; /* unset no_console_output flag, since the console is definitely free now */ |
| 5231 |
} |
| 5232 |
|
| 5233 |
void manager_override_log_level(Manager *m, int level) { |
| 5234 |
_cleanup_free_ char *s = NULL; |
| 5235 |
assert(m); |
| 5236 |
|
| 5237 |
if (!m->log_level_overridden) { |
| 5238 |
m->original_log_level = log_get_max_level(); |
| 5239 |
m->log_level_overridden = true; |
| 5240 |
} |
| 5241 |
|
| 5242 |
(void) log_level_to_string_alloc(level, &s); |
| 5243 |
log_info("Setting log level to %s.", strna(s)); |
| 5244 |
|
| 5245 |
log_set_max_level(level); |
| 5246 |
} |
| 5247 |
|
| 5248 |
void manager_restore_original_log_level(Manager *m) { |
| 5249 |
_cleanup_free_ char *s = NULL; |
| 5250 |
assert(m); |
| 5251 |
|
| 5252 |
if (!m->log_level_overridden) |
| 5253 |
return; |
| 5254 |
|
| 5255 |
(void) log_level_to_string_alloc(m->original_log_level, &s); |
| 5256 |
log_info("Restoring log level to original (%s).", strna(s)); |
| 5257 |
|
| 5258 |
log_set_max_level(m->original_log_level); |
| 5259 |
m->log_level_overridden = false; |
| 5260 |
} |
| 5261 |
|
| 5262 |
void manager_override_log_target(Manager *m, LogTarget target) { |
| 5263 |
assert(m); |
| 5264 |
|
| 5265 |
if (!m->log_target_overridden) { |
| 5266 |
m->original_log_target = log_get_target(); |
| 5267 |
m->log_target_overridden = true; |
| 5268 |
} |
| 5269 |
|
| 5270 |
log_info("Setting log target to %s.", log_target_to_string(target)); |
| 5271 |
log_set_target(target); |
| 5272 |
} |
| 5273 |
|
| 5274 |
void manager_restore_original_log_target(Manager *m) { |
| 5275 |
assert(m); |
| 5276 |
|
| 5277 |
if (!m->log_target_overridden) |
| 5278 |
return; |
| 5279 |
|
| 5280 |
log_info("Restoring log target to original %s.", log_target_to_string(m->original_log_target)); |
| 5281 |
|
| 5282 |
log_set_target(m->original_log_target); |
| 5283 |
m->log_target_overridden = false; |
| 5284 |
} |
| 5285 |
|
| 5286 |
ManagerTimestamp manager_timestamp_initrd_mangle(ManagerTimestamp s) { |
| 5287 |
if (in_initrd() && |
| 5288 |
s >= MANAGER_TIMESTAMP_SECURITY_START && |
| 5289 |
s <= MANAGER_TIMESTAMP_UNITS_LOAD_FINISH) |
| 5290 |
return s - MANAGER_TIMESTAMP_SECURITY_START + MANAGER_TIMESTAMP_INITRD_SECURITY_START; |
| 5291 |
return s; |
| 5292 |
} |
| 5293 |
|
| 5294 |
int manager_allocate_idle_pipe(Manager *m) { |
| 5295 |
int r; |
| 5296 |
|
| 5297 |
assert(m); |
| 5298 |
|
| 5299 |
if (m->idle_pipe[0] >= 0) { |
| 5300 |
assert(m->idle_pipe[1] >= 0); |
| 5301 |
assert(m->idle_pipe[2] >= 0); |
| 5302 |
assert(m->idle_pipe[3] >= 0); |
| 5303 |
return 0; |
| 5304 |
} |
| 5305 |
|
| 5306 |
assert(m->idle_pipe[1] < 0); |
| 5307 |
assert(m->idle_pipe[2] < 0); |
| 5308 |
assert(m->idle_pipe[3] < 0); |
| 5309 |
|
| 5310 |
r = RET_NERRNO(pipe2(m->idle_pipe + 0, O_NONBLOCK|O_CLOEXEC)); |
| 5311 |
if (r < 0) |
| 5312 |
return r; |
| 5313 |
|
| 5314 |
r = RET_NERRNO(pipe2(m->idle_pipe + 2, O_NONBLOCK|O_CLOEXEC)); |
| 5315 |
if (r < 0) { |
| 5316 |
safe_close_pair(m->idle_pipe + 0); |
| 5317 |
return r; |
| 5318 |
} |
| 5319 |
|
| 5320 |
return 1; |
| 5321 |
} |
| 5322 |
|
| 5323 |
void unit_defaults_init(UnitDefaults *defaults, RuntimeScope scope) { |
| 5324 |
assert(defaults); |
| 5325 |
assert(scope >= 0); |
| 5326 |
assert(scope < _RUNTIME_SCOPE_MAX); |
| 5327 |
|
| 5328 |
*defaults = (UnitDefaults) { |
| 5329 |
.std_output = EXEC_OUTPUT_JOURNAL, |
| 5330 |
.std_error = EXEC_OUTPUT_INHERIT, |
| 5331 |
.restart_usec = DEFAULT_RESTART_USEC, |
| 5332 |
.timeout_start_usec = manager_default_timeout(scope), |
| 5333 |
.timeout_stop_usec = manager_default_timeout(scope), |
| 5334 |
.timeout_abort_usec = manager_default_timeout(scope), |
| 5335 |
.timeout_abort_set = false, |
| 5336 |
.device_timeout_usec = manager_default_timeout(scope), |
| 5337 |
.start_limit = { DEFAULT_START_LIMIT_INTERVAL, DEFAULT_START_LIMIT_BURST }, |
| 5338 |
|
| 5339 |
.memory_accounting = MEMORY_ACCOUNTING_DEFAULT, |
| 5340 |
.io_accounting = false, |
| 5341 |
.tasks_accounting = true, |
| 5342 |
.ip_accounting = false, |
| 5343 |
|
| 5344 |
.tasks_max = DEFAULT_TASKS_MAX, |
| 5345 |
.timer_accuracy_usec = 1 * USEC_PER_MINUTE, |
| 5346 |
|
| 5347 |
.pressure = { |
| 5348 |
[PRESSURE_MEMORY] = { .watch = CGROUP_PRESSURE_WATCH_AUTO, .threshold_usec = PRESSURE_DEFAULT_THRESHOLD_USEC }, |
| 5349 |
[PRESSURE_CPU] = { .watch = CGROUP_PRESSURE_WATCH_AUTO, .threshold_usec = PRESSURE_DEFAULT_THRESHOLD_USEC }, |
| 5350 |
[PRESSURE_IO] = { .watch = CGROUP_PRESSURE_WATCH_AUTO, .threshold_usec = PRESSURE_DEFAULT_THRESHOLD_USEC }, |
| 5351 |
}, |
| 5352 |
|
| 5353 |
.oom_policy = OOM_STOP, |
| 5354 |
.oom_score_adjust_set = false, |
| 5355 |
|
| 5356 |
.memory_zswap_writeback = true, |
| 5357 |
}; |
| 5358 |
} |
| 5359 |
|
| 5360 |
void unit_defaults_done(UnitDefaults *defaults) { |
| 5361 |
assert(defaults); |
| 5362 |
|
| 5363 |
defaults->smack_process_label = mfree(defaults->smack_process_label); |
| 5364 |
rlimit_free_all(defaults->rlimit); |
| 5365 |
} |
| 5366 |
|
| 5367 |
LogTarget manager_get_executor_log_target(Manager *m) { |
| 5368 |
assert(m); |
| 5369 |
|
| 5370 |
/* If journald is not available tell sd-executor to go to kmsg, as it might be starting journald */ |
| 5371 |
if (!MANAGER_IS_TEST_RUN(m) && !manager_journal_is_running(m)) |
| 5372 |
return LOG_TARGET_KMSG; |
| 5373 |
|
| 5374 |
return log_get_target(); |
| 5375 |
} |
| 5376 |
|
| 5377 |
void manager_log_caller(Manager *manager, PidRef *caller, const char *method) { |
| 5378 |
_cleanup_free_ char *comm = NULL; |
| 5379 |
|
| 5380 |
assert(manager); |
| 5381 |
assert(method); |
| 5382 |
|
| 5383 |
if (!pidref_is_set(caller)) { |
| 5384 |
log_notice("%s requested from unknown client PID...", method); |
| 5385 |
return; |
| 5386 |
} |
| 5387 |
|
| 5388 |
(void) pidref_get_comm(caller, &comm); |
| 5389 |
Unit *caller_unit = manager_get_unit_by_pidref(manager, caller); |
| 5390 |
|
| 5391 |
log_notice("%s requested from client PID " PID_FMT "%s%s%s%s%s%s...", |
| 5392 |
method, caller->pid, |
| 5393 |
comm ? " ('" : "", strempty(comm), comm ? "')" : "", |
| 5394 |
caller_unit ? " (unit " : "", caller_unit ? caller_unit->id : "", caller_unit ? ")" : ""); |
| 5395 |
} |
| 5396 |
|
| 5397 |
static const char* const manager_state_table[_MANAGER_STATE_MAX] = { |
| 5398 |
[MANAGER_INITIALIZING] = "initializing", |
| 5399 |
[MANAGER_STARTING] = "starting", |
| 5400 |
[MANAGER_RUNNING] = "running", |
| 5401 |
[MANAGER_DEGRADED] = "degraded", |
| 5402 |
[MANAGER_MAINTENANCE] = "maintenance", |
| 5403 |
[MANAGER_STOPPING] = "stopping", |
| 5404 |
}; |
| 5405 |
|
| 5406 |
DEFINE_STRING_TABLE_LOOKUP(manager_state, ManagerState); |
| 5407 |
|
| 5408 |
static const char* const manager_objective_table[_MANAGER_OBJECTIVE_MAX] = { |
| 5409 |
[MANAGER_OK] = "ok", |
| 5410 |
[MANAGER_EXIT] = "exit", |
| 5411 |
[MANAGER_RELOAD] = "reload", |
| 5412 |
[MANAGER_REEXECUTE] = "reexecute", |
| 5413 |
[MANAGER_REBOOT] = "reboot", |
| 5414 |
[MANAGER_SOFT_REBOOT] = "soft-reboot", |
| 5415 |
[MANAGER_POWEROFF] = "poweroff", |
| 5416 |
[MANAGER_HALT] = "halt", |
| 5417 |
[MANAGER_KEXEC] = "kexec", |
| 5418 |
[MANAGER_SWITCH_ROOT] = "switch-root", |
| 5419 |
}; |
| 5420 |
|
| 5421 |
DEFINE_STRING_TABLE_LOOKUP(manager_objective, ManagerObjective); |
| 5422 |
|
| 5423 |
static const char* const manager_timestamp_table[_MANAGER_TIMESTAMP_MAX] = { |
| 5424 |
[MANAGER_TIMESTAMP_FIRMWARE] = "firmware", |
| 5425 |
[MANAGER_TIMESTAMP_LOADER] = "loader", |
| 5426 |
[MANAGER_TIMESTAMP_KERNEL] = "kernel", |
| 5427 |
[MANAGER_TIMESTAMP_INITRD] = "initrd", |
| 5428 |
[MANAGER_TIMESTAMP_USERSPACE] = "userspace", |
| 5429 |
[MANAGER_TIMESTAMP_FINISH] = "finish", |
| 5430 |
[MANAGER_TIMESTAMP_SECURITY_START] = "security-start", |
| 5431 |
[MANAGER_TIMESTAMP_SECURITY_FINISH] = "security-finish", |
| 5432 |
[MANAGER_TIMESTAMP_GENERATORS_START] = "generators-start", |
| 5433 |
[MANAGER_TIMESTAMP_GENERATORS_FINISH] = "generators-finish", |
| 5434 |
[MANAGER_TIMESTAMP_UNITS_LOAD_START] = "units-load-start", |
| 5435 |
[MANAGER_TIMESTAMP_UNITS_LOAD_FINISH] = "units-load-finish", |
| 5436 |
[MANAGER_TIMESTAMP_UNITS_LOAD] = "units-load", |
| 5437 |
[MANAGER_TIMESTAMP_INITRD_SECURITY_START] = "initrd-security-start", |
| 5438 |
[MANAGER_TIMESTAMP_INITRD_SECURITY_FINISH] = "initrd-security-finish", |
| 5439 |
[MANAGER_TIMESTAMP_INITRD_GENERATORS_START] = "initrd-generators-start", |
| 5440 |
[MANAGER_TIMESTAMP_INITRD_GENERATORS_FINISH] = "initrd-generators-finish", |
| 5441 |
[MANAGER_TIMESTAMP_INITRD_UNITS_LOAD_START] = "initrd-units-load-start", |
| 5442 |
[MANAGER_TIMESTAMP_INITRD_UNITS_LOAD_FINISH] = "initrd-units-load-finish", |
| 5443 |
[MANAGER_TIMESTAMP_SHUTDOWN_START] = "shutdown-start", |
| 5444 |
}; |
| 5445 |
|
| 5446 |
DEFINE_STRING_TABLE_LOOKUP(manager_timestamp, ManagerTimestamp); |