| File: | build-scan/../src/core/manager.c |
| Warning: | line 3404, column 21 1st function call argument is an uninitialized value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |||
| 2 | ||||
| 3 | #include <errno(*__errno_location ()).h> | |||
| 4 | #include <fcntl.h> | |||
| 5 | #include <linux1/kd.h> | |||
| 6 | #include <signal.h> | |||
| 7 | #include <stdio_ext.h> | |||
| 8 | #include <string.h> | |||
| 9 | #include <sys/epoll.h> | |||
| 10 | #include <sys/inotify.h> | |||
| 11 | #include <sys/ioctl.h> | |||
| 12 | #include <sys/reboot.h> | |||
| 13 | #include <sys/timerfd.h> | |||
| 14 | #include <sys/wait.h> | |||
| 15 | #include <unistd.h> | |||
| 16 | ||||
| 17 | #if HAVE_AUDIT1 | |||
| 18 | #include <libaudit.h> | |||
| 19 | #endif | |||
| 20 | ||||
| 21 | #include "sd-daemon.h" | |||
| 22 | #include "sd-messages.h" | |||
| 23 | #include "sd-path.h" | |||
| 24 | ||||
| 25 | #include "alloc-util.h" | |||
| 26 | #include "all-units.h" | |||
| 27 | #include "audit-fd.h" | |||
| 28 | #include "boot-timestamps.h" | |||
| 29 | #include "bus-common-errors.h" | |||
| 30 | #include "bus-error.h" | |||
| 31 | #include "bus-kernel.h" | |||
| 32 | #include "bus-util.h" | |||
| 33 | #include "clean-ipc.h" | |||
| 34 | #include "clock-util.h" | |||
| 35 | #include "dbus-job.h" | |||
| 36 | #include "dbus-manager.h" | |||
| 37 | #include "dbus-unit.h" | |||
| 38 | #include "dbus.h" | |||
| 39 | #include "dirent-util.h" | |||
| 40 | #include "env-util.h" | |||
| 41 | #include "escape.h" | |||
| 42 | #include "exec-util.h" | |||
| 43 | #include "execute.h" | |||
| 44 | #include "exit-status.h" | |||
| 45 | #include "fd-util.h" | |||
| 46 | #include "fileio.h" | |||
| 47 | #include "fs-util.h" | |||
| 48 | #include "hashmap.h" | |||
| 49 | #include "io-util.h" | |||
| 50 | #include "label.h" | |||
| 51 | #include "locale-setup.h" | |||
| 52 | #include "log.h" | |||
| 53 | #include "macro.h" | |||
| 54 | #include "manager.h" | |||
| 55 | #include "missing.h" | |||
| 56 | #include "mkdir.h" | |||
| 57 | #include "parse-util.h" | |||
| 58 | #include "path-lookup.h" | |||
| 59 | #include "path-util.h" | |||
| 60 | #include "process-util.h" | |||
| 61 | #include "ratelimit.h" | |||
| 62 | #include "rlimit-util.h" | |||
| 63 | #include "rm-rf.h" | |||
| 64 | #include "signal-util.h" | |||
| 65 | #include "socket-util.h" | |||
| 66 | #include "special.h" | |||
| 67 | #include "stat-util.h" | |||
| 68 | #include "string-table.h" | |||
| 69 | #include "string-util.h" | |||
| 70 | #include "strv.h" | |||
| 71 | #include "strxcpyx.h" | |||
| 72 | #include "syslog-util.h" | |||
| 73 | #include "terminal-util.h" | |||
| 74 | #include "time-util.h" | |||
| 75 | #include "transaction.h" | |||
| 76 | #include "umask-util.h" | |||
| 77 | #include "unit-name.h" | |||
| 78 | #include "user-util.h" | |||
| 79 | #include "util.h" | |||
| 80 | #include "virt.h" | |||
| 81 | #include "watchdog.h" | |||
| 82 | ||||
| 83 | #define NOTIFY_RCVBUF_SIZE(8*1024*1024) (8*1024*1024) | |||
| 84 | #define CGROUPS_AGENT_RCVBUF_SIZE(8*1024*1024) (8*1024*1024) | |||
| 85 | ||||
| 86 | /* Initial delay and the interval for printing status messages about running jobs */ | |||
| 87 | #define JOBS_IN_PROGRESS_WAIT_USEC(5*((usec_t) 1000000ULL)) (5*USEC_PER_SEC((usec_t) 1000000ULL)) | |||
| 88 | #define JOBS_IN_PROGRESS_PERIOD_USEC(((usec_t) 1000000ULL) / 3) (USEC_PER_SEC((usec_t) 1000000ULL) / 3) | |||
| 89 | #define JOBS_IN_PROGRESS_PERIOD_DIVISOR3 3 | |||
| 90 | ||||
| 91 | /* If there are more than 1K bus messages queue across our API and direct busses, then let's not add more on top until | |||
| 92 | * the queue gets more empty. */ | |||
| 93 | #define MANAGER_BUS_BUSY_THRESHOLD1024LU 1024LU | |||
| 94 | ||||
| 95 | /* How many units and jobs to process of the bus queue before returning to the event loop. */ | |||
| 96 | #define MANAGER_BUS_MESSAGE_BUDGET100U 100U | |||
| 97 | ||||
| 98 | static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |||
| 99 | static int manager_dispatch_cgroups_agent_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |||
| 100 | static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |||
| 101 | static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |||
| 102 | static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |||
| 103 | static int manager_dispatch_user_lookup_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |||
| 104 | static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata); | |||
| 105 | static int manager_dispatch_run_queue(sd_event_source *source, void *userdata); | |||
| 106 | static int manager_dispatch_sigchld(sd_event_source *source, void *userdata); | |||
| 107 | static int manager_dispatch_timezone_change(sd_event_source *source, const struct inotify_event *event, void *userdata); | |||
| 108 | static int manager_run_environment_generators(Manager *m); | |||
| 109 | static int manager_run_generators(Manager *m); | |||
| 110 | ||||
| 111 | static void manager_watch_jobs_in_progress(Manager *m) { | |||
| 112 | usec_t next; | |||
| 113 | int r; | |||
| 114 | ||||
| 115 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 115, __PRETTY_FUNCTION__ ); } while (0); | |||
| 116 | ||||
| 117 | /* We do not want to show the cylon animation if the user | |||
| 118 | * needs to confirm service executions otherwise confirmation | |||
| 119 | * messages will be screwed by the cylon animation. */ | |||
| 120 | if (!manager_is_confirm_spawn_disabled(m)) | |||
| 121 | return; | |||
| 122 | ||||
| 123 | if (m->jobs_in_progress_event_source) | |||
| 124 | return; | |||
| 125 | ||||
| 126 | next = now(CLOCK_MONOTONIC1) + JOBS_IN_PROGRESS_WAIT_USEC(5*((usec_t) 1000000ULL)); | |||
| 127 | r = sd_event_add_time( | |||
| 128 | m->event, | |||
| 129 | &m->jobs_in_progress_event_source, | |||
| 130 | CLOCK_MONOTONIC1, | |||
| 131 | next, 0, | |||
| 132 | manager_dispatch_jobs_in_progress, m); | |||
| 133 | if (r < 0) | |||
| 134 | return; | |||
| 135 | ||||
| 136 | (void) sd_event_source_set_description(m->jobs_in_progress_event_source, "manager-jobs-in-progress"); | |||
| 137 | } | |||
| 138 | ||||
| 139 | #define CYLON_BUFFER_EXTRA(2*(sizeof("""\x1B[0;31m""") - 1) + (sizeof("""\x1B[0;1;31m""" ) - 1) + 2*(sizeof("""\x1B[0m""") - 1)) (2*STRLEN(ANSI_RED)(sizeof("""\x1B[0;31m""") - 1) + STRLEN(ANSI_HIGHLIGHT_RED)(sizeof("""\x1B[0;1;31m""") - 1) + 2*STRLEN(ANSI_NORMAL)(sizeof("""\x1B[0m""") - 1)) | |||
| 140 | ||||
| 141 | static void draw_cylon(char buffer[], size_t buflen, unsigned width, unsigned pos) { | |||
| 142 | char *p = buffer; | |||
| 143 | ||||
| 144 | assert(buflen >= CYLON_BUFFER_EXTRA + width + 1)do { if ((__builtin_expect(!!(!(buflen >= (2*(sizeof("""\x1B[0;31m" "") - 1) + (sizeof("""\x1B[0;1;31m""") - 1) + 2*(sizeof("""\x1B[0m" "") - 1)) + width + 1)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("buflen >= CYLON_BUFFER_EXTRA + width + 1"), "../src/core/manager.c" , 144, __PRETTY_FUNCTION__); } while (0); | |||
| 145 | assert(pos <= width+1)do { if ((__builtin_expect(!!(!(pos <= width+1)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("pos <= width+1"), "../src/core/manager.c" , 145, __PRETTY_FUNCTION__); } while (0); /* 0 or width+1 mean that the center light is behind the corner */ | |||
| 146 | ||||
| 147 | if (pos > 1) { | |||
| 148 | if (pos > 2) | |||
| 149 | p = mempset(p, ' ', pos-2); | |||
| 150 | if (log_get_show_color()) | |||
| 151 | p = stpcpy(p, ANSI_RED"\x1B[0;31m"); | |||
| 152 | *p++ = '*'; | |||
| 153 | } | |||
| 154 | ||||
| 155 | if (pos > 0 && pos <= width) { | |||
| 156 | if (log_get_show_color()) | |||
| 157 | p = stpcpy(p, ANSI_HIGHLIGHT_RED"\x1B[0;1;31m"); | |||
| 158 | *p++ = '*'; | |||
| 159 | } | |||
| 160 | ||||
| 161 | if (log_get_show_color()) | |||
| 162 | p = stpcpy(p, ANSI_NORMAL"\x1B[0m"); | |||
| 163 | ||||
| 164 | if (pos < width) { | |||
| 165 | if (log_get_show_color()) | |||
| 166 | p = stpcpy(p, ANSI_RED"\x1B[0;31m"); | |||
| 167 | *p++ = '*'; | |||
| 168 | if (pos < width-1) | |||
| 169 | p = mempset(p, ' ', width-1-pos); | |||
| 170 | if (log_get_show_color()) | |||
| 171 | strcpy(p, ANSI_NORMAL"\x1B[0m"); | |||
| 172 | } | |||
| 173 | } | |||
| 174 | ||||
| 175 | void manager_flip_auto_status(Manager *m, bool_Bool enable) { | |||
| 176 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 176, __PRETTY_FUNCTION__ ); } while (0); | |||
| 177 | ||||
| 178 | if (enable) { | |||
| 179 | if (m->show_status == SHOW_STATUS_AUTO) | |||
| 180 | manager_set_show_status(m, SHOW_STATUS_TEMPORARY); | |||
| 181 | } else { | |||
| 182 | if (m->show_status == SHOW_STATUS_TEMPORARY) | |||
| 183 | manager_set_show_status(m, SHOW_STATUS_AUTO); | |||
| 184 | } | |||
| 185 | } | |||
| 186 | ||||
| 187 | static void manager_print_jobs_in_progress(Manager *m) { | |||
| 188 | _cleanup_free___attribute__((cleanup(freep))) char *job_of_n = NULL((void*)0); | |||
| 189 | Iterator i; | |||
| 190 | Job *j; | |||
| 191 | unsigned counter = 0, print_nr; | |||
| 192 | char cylon[6 + CYLON_BUFFER_EXTRA(2*(sizeof("""\x1B[0;31m""") - 1) + (sizeof("""\x1B[0;1;31m""" ) - 1) + 2*(sizeof("""\x1B[0m""") - 1)) + 1]; | |||
| 193 | unsigned cylon_pos; | |||
| 194 | char time[FORMAT_TIMESPAN_MAX64], limit[FORMAT_TIMESPAN_MAX64] = "no limit"; | |||
| 195 | uint64_t x; | |||
| 196 | ||||
| 197 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 197, __PRETTY_FUNCTION__ ); } while (0); | |||
| 198 | assert(m->n_running_jobs > 0)do { if ((__builtin_expect(!!(!(m->n_running_jobs > 0)) ,0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("m->n_running_jobs > 0" ), "../src/core/manager.c", 198, __PRETTY_FUNCTION__); } while (0); | |||
| 199 | ||||
| 200 | manager_flip_auto_status(m, true1); | |||
| 201 | ||||
| 202 | print_nr = (m->jobs_in_progress_iteration / JOBS_IN_PROGRESS_PERIOD_DIVISOR3) % m->n_running_jobs; | |||
| 203 | ||||
| 204 | HASHMAP_FOREACH(j, m->jobs, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((m->jobs), & (i), (void**)&(j), ((void*)0)); ) | |||
| 205 | if (j->state == JOB_RUNNING && counter++ == print_nr) | |||
| 206 | break; | |||
| 207 | ||||
| 208 | /* m->n_running_jobs must be consistent with the contents of m->jobs, | |||
| 209 | * so the above loop must have succeeded in finding j. */ | |||
| 210 | assert(counter == print_nr + 1)do { if ((__builtin_expect(!!(!(counter == print_nr + 1)),0)) ) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("counter == print_nr + 1" ), "../src/core/manager.c", 210, __PRETTY_FUNCTION__); } while (0); | |||
| 211 | assert(j)do { if ((__builtin_expect(!!(!(j)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("j"), "../src/core/manager.c", 211, __PRETTY_FUNCTION__ ); } while (0); | |||
| 212 | ||||
| 213 | cylon_pos = m->jobs_in_progress_iteration % 14; | |||
| 214 | if (cylon_pos >= 8) | |||
| 215 | cylon_pos = 14 - cylon_pos; | |||
| 216 | draw_cylon(cylon, sizeof(cylon), 6, cylon_pos); | |||
| 217 | ||||
| 218 | m->jobs_in_progress_iteration++; | |||
| 219 | ||||
| 220 | if (m->n_running_jobs > 1) { | |||
| 221 | if (asprintf(&job_of_n, "(%u of %u) ", counter, m->n_running_jobs) < 0) | |||
| 222 | job_of_n = NULL((void*)0); | |||
| 223 | } | |||
| 224 | ||||
| 225 | format_timespan(time, sizeof(time), now(CLOCK_MONOTONIC1) - j->begin_usec, 1*USEC_PER_SEC((usec_t) 1000000ULL)); | |||
| 226 | if (job_get_timeout(j, &x) > 0) | |||
| 227 | format_timespan(limit, sizeof(limit), x - j->begin_usec, 1*USEC_PER_SEC((usec_t) 1000000ULL)); | |||
| 228 | ||||
| 229 | manager_status_printf(m, STATUS_TYPE_EPHEMERAL, cylon, | |||
| 230 | "%sA %s job is running for %s (%s / %s)", | |||
| 231 | strempty(job_of_n), | |||
| 232 | job_type_to_string(j->type), | |||
| 233 | unit_description(j->unit), | |||
| 234 | time, limit); | |||
| 235 | } | |||
| 236 | ||||
| 237 | static int have_ask_password(void) { | |||
| 238 | _cleanup_closedir___attribute__((cleanup(closedirp))) DIR *dir; | |||
| 239 | struct dirent *de; | |||
| 240 | ||||
| 241 | dir = opendir("/run/systemd/ask-password"); | |||
| 242 | if (!dir) { | |||
| 243 | if (errno(*__errno_location ()) == ENOENT2) | |||
| 244 | return false0; | |||
| 245 | else | |||
| 246 | return -errno(*__errno_location ()); | |||
| 247 | } | |||
| 248 | ||||
| 249 | FOREACH_DIRENT_ALL(de, dir, return -errno)for ((*__errno_location ()) = 0, de = readdir(dir);; (*__errno_location ()) = 0, de = readdir(dir)) if (!de) { if ((*__errno_location ()) > 0) { return -(*__errno_location ()); } break; } else { | |||
| 250 | if (startswith(de->d_name, "ask.")) | |||
| 251 | return true1; | |||
| 252 | } | |||
| 253 | return false0; | |||
| 254 | } | |||
| 255 | ||||
| 256 | static int manager_dispatch_ask_password_fd(sd_event_source *source, | |||
| 257 | int fd, uint32_t revents, void *userdata) { | |||
| 258 | Manager *m = userdata; | |||
| 259 | ||||
| 260 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 260, __PRETTY_FUNCTION__ ); } while (0); | |||
| 261 | ||||
| 262 | (void) flush_fd(fd); | |||
| 263 | ||||
| 264 | m->have_ask_password = have_ask_password(); | |||
| 265 | if (m->have_ask_password < 0) | |||
| 266 | /* Log error but continue. Negative have_ask_password | |||
| 267 | * is treated as unknown status. */ | |||
| 268 | log_error_errno(m->have_ask_password, "Failed to list /run/systemd/ask-password: %m")({ int _level = ((3)), _e = ((m->have_ask_password)), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 268, __func__, "Failed to list /run/systemd/ask-password: %m" ) : -abs(_e); }); | |||
| 269 | ||||
| 270 | return 0; | |||
| 271 | } | |||
| 272 | ||||
| 273 | static void manager_close_ask_password(Manager *m) { | |||
| 274 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 274, __PRETTY_FUNCTION__ ); } while (0); | |||
| 275 | ||||
| 276 | m->ask_password_event_source = sd_event_source_unref(m->ask_password_event_source); | |||
| 277 | m->ask_password_inotify_fd = safe_close(m->ask_password_inotify_fd); | |||
| 278 | m->have_ask_password = -EINVAL22; | |||
| 279 | } | |||
| 280 | ||||
| 281 | static int manager_check_ask_password(Manager *m) { | |||
| 282 | int r; | |||
| 283 | ||||
| 284 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 284, __PRETTY_FUNCTION__ ); } while (0); | |||
| 285 | ||||
| 286 | if (!m->ask_password_event_source) { | |||
| 287 | assert(m->ask_password_inotify_fd < 0)do { if ((__builtin_expect(!!(!(m->ask_password_inotify_fd < 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("m->ask_password_inotify_fd < 0" ), "../src/core/manager.c", 287, __PRETTY_FUNCTION__); } while (0); | |||
| 288 | ||||
| 289 | mkdir_p_label("/run/systemd/ask-password", 0755); | |||
| 290 | ||||
| 291 | m->ask_password_inotify_fd = inotify_init1(IN_NONBLOCKIN_NONBLOCK|IN_CLOEXECIN_CLOEXEC); | |||
| 292 | if (m->ask_password_inotify_fd < 0) | |||
| 293 | return log_error_errno(errno, "inotify_init1() failed: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 293, __func__, "inotify_init1() failed: %m" ) : -abs(_e); }); | |||
| 294 | ||||
| 295 | if (inotify_add_watch(m->ask_password_inotify_fd, "/run/systemd/ask-password", IN_CREATE0x00000100|IN_DELETE0x00000200|IN_MOVE(0x00000040 | 0x00000080)) < 0) { | |||
| 296 | log_error_errno(errno, "Failed to add watch on /run/systemd/ask-password: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 296, __func__, "Failed to add watch on /run/systemd/ask-password: %m" ) : -abs(_e); }); | |||
| 297 | manager_close_ask_password(m); | |||
| 298 | return -errno(*__errno_location ()); | |||
| 299 | } | |||
| 300 | ||||
| 301 | r = sd_event_add_io(m->event, &m->ask_password_event_source, | |||
| 302 | m->ask_password_inotify_fd, EPOLLINEPOLLIN, | |||
| 303 | manager_dispatch_ask_password_fd, m); | |||
| 304 | if (r < 0) { | |||
| 305 | log_error_errno(errno, "Failed to add event source for /run/systemd/ask-password: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 305, __func__, "Failed to add event source for /run/systemd/ask-password: %m" ) : -abs(_e); }); | |||
| 306 | manager_close_ask_password(m); | |||
| 307 | return -errno(*__errno_location ()); | |||
| 308 | } | |||
| 309 | ||||
| 310 | (void) sd_event_source_set_description(m->ask_password_event_source, "manager-ask-password"); | |||
| 311 | ||||
| 312 | /* Queries might have been added meanwhile... */ | |||
| 313 | manager_dispatch_ask_password_fd(m->ask_password_event_source, | |||
| 314 | m->ask_password_inotify_fd, EPOLLINEPOLLIN, m); | |||
| 315 | } | |||
| 316 | ||||
| 317 | return m->have_ask_password; | |||
| 318 | } | |||
| 319 | ||||
| 320 | static int manager_watch_idle_pipe(Manager *m) { | |||
| 321 | int r; | |||
| 322 | ||||
| 323 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 323, __PRETTY_FUNCTION__ ); } while (0); | |||
| 324 | ||||
| 325 | if (m->idle_pipe_event_source) | |||
| 326 | return 0; | |||
| 327 | ||||
| 328 | if (m->idle_pipe[2] < 0) | |||
| 329 | return 0; | |||
| 330 | ||||
| 331 | r = sd_event_add_io(m->event, &m->idle_pipe_event_source, m->idle_pipe[2], EPOLLINEPOLLIN, manager_dispatch_idle_pipe_fd, m); | |||
| 332 | if (r < 0) | |||
| 333 | return log_error_errno(r, "Failed to watch idle pipe: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 333, __func__, "Failed to watch idle pipe: %m" ) : -abs(_e); }); | |||
| 334 | ||||
| 335 | (void) sd_event_source_set_description(m->idle_pipe_event_source, "manager-idle-pipe"); | |||
| 336 | ||||
| 337 | return 0; | |||
| 338 | } | |||
| 339 | ||||
| 340 | static void manager_close_idle_pipe(Manager *m) { | |||
| 341 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 341, __PRETTY_FUNCTION__ ); } while (0); | |||
| 342 | ||||
| 343 | m->idle_pipe_event_source = sd_event_source_unref(m->idle_pipe_event_source); | |||
| 344 | ||||
| 345 | safe_close_pair(m->idle_pipe); | |||
| 346 | safe_close_pair(m->idle_pipe + 2); | |||
| 347 | } | |||
| 348 | ||||
| 349 | static int manager_setup_time_change(Manager *m) { | |||
| 350 | int r; | |||
| 351 | ||||
| 352 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 352, __PRETTY_FUNCTION__ ); } while (0); | |||
| 353 | ||||
| 354 | if (m->test_run_flags) | |||
| 355 | return 0; | |||
| 356 | ||||
| 357 | m->time_change_event_source = sd_event_source_unref(m->time_change_event_source); | |||
| 358 | m->time_change_fd = safe_close(m->time_change_fd); | |||
| 359 | ||||
| 360 | m->time_change_fd = time_change_fd(); | |||
| 361 | if (m->time_change_fd < 0) | |||
| 362 | return log_error_errno(m->time_change_fd, "Failed to create timer change timer fd: %m")({ int _level = ((3)), _e = ((m->time_change_fd)), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ( (_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 362, __func__, "Failed to create timer change timer fd: %m" ) : -abs(_e); }); | |||
| 363 | ||||
| 364 | r = sd_event_add_io(m->event, &m->time_change_event_source, m->time_change_fd, EPOLLINEPOLLIN, manager_dispatch_time_change_fd, m); | |||
| 365 | if (r < 0) | |||
| 366 | return log_error_errno(r, "Failed to create time change event source: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 366, __func__, "Failed to create time change event source: %m" ) : -abs(_e); }); | |||
| 367 | ||||
| 368 | /* Schedule this slightly earlier than the .timer event sources */ | |||
| 369 | r = sd_event_source_set_priority(m->time_change_event_source, SD_EVENT_PRIORITY_NORMAL-1); | |||
| 370 | if (r < 0) | |||
| 371 | return log_error_errno(r, "Failed to set priority of time change event sources: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 371, __func__, "Failed to set priority of time change event sources: %m" ) : -abs(_e); }); | |||
| 372 | ||||
| 373 | (void) sd_event_source_set_description(m->time_change_event_source, "manager-time-change"); | |||
| 374 | ||||
| 375 | log_debug("Set up TFD_TIMER_CANCEL_ON_SET timerfd.")({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 375, __func__, "Set up TFD_TIMER_CANCEL_ON_SET timerfd." ) : -abs(_e); }); | |||
| 376 | ||||
| 377 | return 0; | |||
| 378 | } | |||
| 379 | ||||
| 380 | static int manager_read_timezone_stat(Manager *m) { | |||
| 381 | struct stat st; | |||
| 382 | bool_Bool changed; | |||
| 383 | ||||
| 384 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 384, __PRETTY_FUNCTION__ ); } while (0); | |||
| 385 | ||||
| 386 | /* Read the current stat() data of /etc/localtime so that we detect changes */ | |||
| 387 | if (lstat("/etc/localtime", &st) < 0) { | |||
| 388 | log_debug_errno(errno, "Failed to stat /etc/localtime, ignoring: %m")({ int _level = ((7)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 388, __func__, "Failed to stat /etc/localtime, ignoring: %m" ) : -abs(_e); }); | |||
| 389 | changed = m->etc_localtime_accessible; | |||
| 390 | m->etc_localtime_accessible = false0; | |||
| 391 | } else { | |||
| 392 | usec_t k; | |||
| 393 | ||||
| 394 | k = timespec_load(&st.st_mtim); | |||
| 395 | changed = !m->etc_localtime_accessible || k != m->etc_localtime_mtime; | |||
| 396 | ||||
| 397 | m->etc_localtime_mtime = k; | |||
| 398 | m->etc_localtime_accessible = true1; | |||
| 399 | } | |||
| 400 | ||||
| 401 | return changed; | |||
| 402 | } | |||
| 403 | ||||
| 404 | static int manager_setup_timezone_change(Manager *m) { | |||
| 405 | _cleanup_(sd_event_source_unrefp)__attribute__((cleanup(sd_event_source_unrefp))) sd_event_source *new_event = NULL((void*)0); | |||
| 406 | int r; | |||
| 407 | ||||
| 408 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 408, __PRETTY_FUNCTION__ ); } while (0); | |||
| 409 | ||||
| 410 | if (m->test_run_flags != 0) | |||
| 411 | return 0; | |||
| 412 | ||||
| 413 | /* We watch /etc/localtime for three events: change of the link count (which might mean removal from /etc even | |||
| 414 | * though another link might be kept), renames, and file close operations after writing. Note we don't bother | |||
| 415 | * with IN_DELETE_SELF, as that would just report when the inode is removed entirely, i.e. after the link count | |||
| 416 | * went to zero and all fds to it are closed. | |||
| 417 | * | |||
| 418 | * Note that we never follow symlinks here. This is a simplification, but should cover almost all cases | |||
| 419 | * correctly. | |||
| 420 | * | |||
| 421 | * Note that we create the new event source first here, before releasing the old one. This should optimize | |||
| 422 | * behaviour as this way sd-event can reuse the old watch in case the inode didn't change. */ | |||
| 423 | ||||
| 424 | r = sd_event_add_inotify(m->event, &new_event, "/etc/localtime", | |||
| 425 | IN_ATTRIB0x00000004|IN_MOVE_SELF0x00000800|IN_CLOSE_WRITE0x00000008|IN_DONT_FOLLOW0x02000000, manager_dispatch_timezone_change, m); | |||
| 426 | if (r == -ENOENT2) /* If the file doesn't exist yet, subscribe to /etc instead, and wait until it is created | |||
| 427 | * either by O_CREATE or by rename() */ | |||
| 428 | r = sd_event_add_inotify(m->event, &new_event, "/etc", | |||
| 429 | IN_CREATE0x00000100|IN_MOVED_TO0x00000080|IN_ONLYDIR0x01000000, manager_dispatch_timezone_change, m); | |||
| 430 | if (r < 0) | |||
| 431 | return log_error_errno(r, "Failed to create timezone change event source: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 431, __func__, "Failed to create timezone change event source: %m" ) : -abs(_e); }); | |||
| 432 | ||||
| 433 | /* Schedule this slightly earlier than the .timer event sources */ | |||
| 434 | r = sd_event_source_set_priority(new_event, SD_EVENT_PRIORITY_NORMAL-1); | |||
| 435 | if (r < 0) | |||
| 436 | return log_error_errno(r, "Failed to set priority of timezone change event sources: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 436, __func__, "Failed to set priority of timezone change event sources: %m" ) : -abs(_e); }); | |||
| 437 | ||||
| 438 | sd_event_source_unref(m->timezone_change_event_source); | |||
| 439 | m->timezone_change_event_source = TAKE_PTR(new_event)({ typeof(new_event) _ptr_ = (new_event); (new_event) = ((void *)0); _ptr_; }); | |||
| 440 | ||||
| 441 | return 0; | |||
| 442 | } | |||
| 443 | ||||
| 444 | static int enable_special_signals(Manager *m) { | |||
| 445 | _cleanup_close___attribute__((cleanup(closep))) int fd = -1; | |||
| 446 | ||||
| 447 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 447, __PRETTY_FUNCTION__ ); } while (0); | |||
| 448 | ||||
| 449 | if (m->test_run_flags) | |||
| 450 | return 0; | |||
| 451 | ||||
| 452 | /* Enable that we get SIGINT on control-alt-del. In containers | |||
| 453 | * this will fail with EPERM (older) or EINVAL (newer), so | |||
| 454 | * ignore that. */ | |||
| 455 | if (reboot(RB_DISABLE_CAD0) < 0 && !IN_SET(errno, EPERM, EINVAL)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){1, 22})/sizeof(int)]; switch((*__errno_location ())) { case 1: case 22: _found = 1; break; default: break; } _found; })) | |||
| 456 | log_warning_errno(errno, "Failed to enable ctrl-alt-del handling: %m")({ int _level = ((4)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 456, __func__, "Failed to enable ctrl-alt-del handling: %m" ) : -abs(_e); }); | |||
| 457 | ||||
| 458 | fd = open_terminal("/dev/tty0", O_RDWR02|O_NOCTTY0400|O_CLOEXEC02000000); | |||
| 459 | if (fd < 0) { | |||
| 460 | /* Support systems without virtual console */ | |||
| 461 | if (fd != -ENOENT2) | |||
| 462 | log_warning_errno(errno, "Failed to open /dev/tty0: %m")({ int _level = ((4)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 462, __func__, "Failed to open /dev/tty0: %m" ) : -abs(_e); }); | |||
| 463 | } else { | |||
| 464 | /* Enable that we get SIGWINCH on kbrequest */ | |||
| 465 | if (ioctl(fd, KDSIGACCEPT0x4B4E, SIGWINCH28) < 0) | |||
| 466 | log_warning_errno(errno, "Failed to enable kbrequest handling: %m")({ int _level = ((4)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 466, __func__, "Failed to enable kbrequest handling: %m" ) : -abs(_e); }); | |||
| 467 | } | |||
| 468 | ||||
| 469 | return 0; | |||
| 470 | } | |||
| 471 | ||||
| 472 | #define RTSIG_IF_AVAILABLE(signum)(signum <= (__libc_current_sigrtmax ()) ? signum : -1) (signum <= SIGRTMAX(__libc_current_sigrtmax ()) ? signum : -1) | |||
| 473 | ||||
| 474 | static int manager_setup_signals(Manager *m) { | |||
| 475 | struct sigaction sa = { | |||
| 476 | .sa_handler__sigaction_handler.sa_handler = SIG_DFL((__sighandler_t) 0), | |||
| 477 | .sa_flags = SA_NOCLDSTOP1|SA_RESTART0x10000000, | |||
| 478 | }; | |||
| 479 | sigset_t mask; | |||
| 480 | int r; | |||
| 481 | ||||
| 482 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 482, __PRETTY_FUNCTION__ ); } while (0); | |||
| 483 | ||||
| 484 | assert_se(sigaction(SIGCHLD, &sa, NULL) == 0)do { if ((__builtin_expect(!!(!(sigaction(17, &sa, ((void *)0)) == 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ( "sigaction(SIGCHLD, &sa, NULL) == 0"), "../src/core/manager.c" , 484, __PRETTY_FUNCTION__); } while (0); | |||
| 485 | ||||
| 486 | /* We make liberal use of realtime signals here. On | |||
| 487 | * Linux/glibc we have 30 of them (with the exception of Linux | |||
| 488 | * on hppa, see below), between SIGRTMIN+0 ... SIGRTMIN+30 | |||
| 489 | * (aka SIGRTMAX). */ | |||
| 490 | ||||
| 491 | assert_se(sigemptyset(&mask) == 0)do { if ((__builtin_expect(!!(!(sigemptyset(&mask) == 0)) ,0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("sigemptyset(&mask) == 0" ), "../src/core/manager.c", 491, __PRETTY_FUNCTION__); } while (0); | |||
| 492 | sigset_add_many(&mask, | |||
| 493 | SIGCHLD17, /* Child died */ | |||
| 494 | SIGTERM15, /* Reexecute daemon */ | |||
| 495 | SIGHUP1, /* Reload configuration */ | |||
| 496 | SIGUSR110, /* systemd/upstart: reconnect to D-Bus */ | |||
| 497 | SIGUSR212, /* systemd: dump status */ | |||
| 498 | SIGINT2, /* Kernel sends us this on control-alt-del */ | |||
| 499 | SIGWINCH28, /* Kernel sends us this on kbrequest (alt-arrowup) */ | |||
| 500 | SIGPWR30, /* Some kernel drivers and upsd send us this on power failure */ | |||
| 501 | ||||
| 502 | SIGRTMIN(__libc_current_sigrtmin ())+0, /* systemd: start default.target */ | |||
| 503 | SIGRTMIN(__libc_current_sigrtmin ())+1, /* systemd: isolate rescue.target */ | |||
| 504 | SIGRTMIN(__libc_current_sigrtmin ())+2, /* systemd: isolate emergency.target */ | |||
| 505 | SIGRTMIN(__libc_current_sigrtmin ())+3, /* systemd: start halt.target */ | |||
| 506 | SIGRTMIN(__libc_current_sigrtmin ())+4, /* systemd: start poweroff.target */ | |||
| 507 | SIGRTMIN(__libc_current_sigrtmin ())+5, /* systemd: start reboot.target */ | |||
| 508 | SIGRTMIN(__libc_current_sigrtmin ())+6, /* systemd: start kexec.target */ | |||
| 509 | ||||
| 510 | /* ... space for more special targets ... */ | |||
| 511 | ||||
| 512 | SIGRTMIN(__libc_current_sigrtmin ())+13, /* systemd: Immediate halt */ | |||
| 513 | SIGRTMIN(__libc_current_sigrtmin ())+14, /* systemd: Immediate poweroff */ | |||
| 514 | SIGRTMIN(__libc_current_sigrtmin ())+15, /* systemd: Immediate reboot */ | |||
| 515 | SIGRTMIN(__libc_current_sigrtmin ())+16, /* systemd: Immediate kexec */ | |||
| 516 | ||||
| 517 | /* ... space for more immediate system state changes ... */ | |||
| 518 | ||||
| 519 | SIGRTMIN(__libc_current_sigrtmin ())+20, /* systemd: enable status messages */ | |||
| 520 | SIGRTMIN(__libc_current_sigrtmin ())+21, /* systemd: disable status messages */ | |||
| 521 | SIGRTMIN(__libc_current_sigrtmin ())+22, /* systemd: set log level to LOG_DEBUG */ | |||
| 522 | SIGRTMIN(__libc_current_sigrtmin ())+23, /* systemd: set log level to LOG_INFO */ | |||
| 523 | SIGRTMIN(__libc_current_sigrtmin ())+24, /* systemd: Immediate exit (--user only) */ | |||
| 524 | ||||
| 525 | /* .. one free signal here ... */ | |||
| 526 | ||||
| 527 | /* Apparently Linux on hppa had fewer RT signals until v3.18, | |||
| 528 | * SIGRTMAX was SIGRTMIN+25, and then SIGRTMIN was lowered, | |||
| 529 | * see commit v3.17-7614-g1f25df2eff. | |||
| 530 | * | |||
| 531 | * We cannot unconditionally make use of those signals here, | |||
| 532 | * so let's use a runtime check. Since these commands are | |||
| 533 | * accessible by different means and only really a safety | |||
| 534 | * net, the missing functionality on hppa shouldn't matter. | |||
| 535 | */ | |||
| 536 | ||||
| 537 | RTSIG_IF_AVAILABLE(SIGRTMIN+26)((__libc_current_sigrtmin ())+26 <= (__libc_current_sigrtmax ()) ? (__libc_current_sigrtmin ())+26 : -1), /* systemd: set log target to journal-or-kmsg */ | |||
| 538 | RTSIG_IF_AVAILABLE(SIGRTMIN+27)((__libc_current_sigrtmin ())+27 <= (__libc_current_sigrtmax ()) ? (__libc_current_sigrtmin ())+27 : -1), /* systemd: set log target to console */ | |||
| 539 | RTSIG_IF_AVAILABLE(SIGRTMIN+28)((__libc_current_sigrtmin ())+28 <= (__libc_current_sigrtmax ()) ? (__libc_current_sigrtmin ())+28 : -1), /* systemd: set log target to kmsg */ | |||
| 540 | RTSIG_IF_AVAILABLE(SIGRTMIN+29)((__libc_current_sigrtmin ())+29 <= (__libc_current_sigrtmax ()) ? (__libc_current_sigrtmin ())+29 : -1), /* systemd: set log target to syslog-or-kmsg (obsolete) */ | |||
| 541 | ||||
| 542 | /* ... one free signal here SIGRTMIN+30 ... */ | |||
| 543 | -1); | |||
| 544 | assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0)do { if ((__builtin_expect(!!(!(sigprocmask(2, &mask, ((void *)0)) == 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ( "sigprocmask(SIG_SETMASK, &mask, NULL) == 0"), "../src/core/manager.c" , 544, __PRETTY_FUNCTION__); } while (0); | |||
| 545 | ||||
| 546 | m->signal_fd = signalfd(-1, &mask, SFD_NONBLOCKSFD_NONBLOCK|SFD_CLOEXECSFD_CLOEXEC); | |||
| 547 | if (m->signal_fd < 0) | |||
| 548 | return -errno(*__errno_location ()); | |||
| 549 | ||||
| 550 | r = sd_event_add_io(m->event, &m->signal_event_source, m->signal_fd, EPOLLINEPOLLIN, manager_dispatch_signal_fd, m); | |||
| 551 | if (r < 0) | |||
| 552 | return r; | |||
| 553 | ||||
| 554 | (void) sd_event_source_set_description(m->signal_event_source, "manager-signal"); | |||
| 555 | ||||
| 556 | /* Process signals a bit earlier than the rest of things, but later than notify_fd processing, so that the | |||
| 557 | * notify processing can still figure out to which process/service a message belongs, before we reap the | |||
| 558 | * process. Also, process this before handling cgroup notifications, so that we always collect child exit | |||
| 559 | * status information before detecting that there's no process in a cgroup. */ | |||
| 560 | r = sd_event_source_set_priority(m->signal_event_source, SD_EVENT_PRIORITY_NORMAL-6); | |||
| 561 | if (r < 0) | |||
| 562 | return r; | |||
| 563 | ||||
| 564 | if (MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) | |||
| 565 | return enable_special_signals(m); | |||
| 566 | ||||
| 567 | return 0; | |||
| 568 | } | |||
| 569 | ||||
| 570 | static void manager_sanitize_environment(Manager *m) { | |||
| 571 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 571, __PRETTY_FUNCTION__ ); } while (0); | |||
| 572 | ||||
| 573 | /* Let's remove some environment variables that we need ourselves to communicate with our clients */ | |||
| 574 | strv_env_unset_many( | |||
| 575 | m->environment, | |||
| 576 | "EXIT_CODE", | |||
| 577 | "EXIT_STATUS", | |||
| 578 | "INVOCATION_ID", | |||
| 579 | "JOURNAL_STREAM", | |||
| 580 | "LISTEN_FDNAMES", | |||
| 581 | "LISTEN_FDS", | |||
| 582 | "LISTEN_PID", | |||
| 583 | "MAINPID", | |||
| 584 | "MANAGERPID", | |||
| 585 | "NOTIFY_SOCKET", | |||
| 586 | "REMOTE_ADDR", | |||
| 587 | "REMOTE_PORT", | |||
| 588 | "SERVICE_RESULT", | |||
| 589 | "WATCHDOG_PID", | |||
| 590 | "WATCHDOG_USEC", | |||
| 591 | NULL((void*)0)); | |||
| 592 | ||||
| 593 | /* Let's order the environment alphabetically, just to make it pretty */ | |||
| 594 | strv_sort(m->environment); | |||
| 595 | } | |||
| 596 | ||||
| 597 | static int manager_default_environment(Manager *m) { | |||
| 598 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 598, __PRETTY_FUNCTION__ ); } while (0); | |||
| 599 | ||||
| 600 | if (MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) { | |||
| 601 | /* The system manager always starts with a clean | |||
| 602 | * environment for its children. It does not import | |||
| 603 | * the kernel's or the parents' exported variables. | |||
| 604 | * | |||
| 605 | * The initial passed environment is untouched to keep | |||
| 606 | * /proc/self/environ valid; it is used for tagging | |||
| 607 | * the init process inside containers. */ | |||
| 608 | m->environment = strv_new("PATH=" DEFAULT_PATH"/usr/local/" "sbin:" "/usr/local/" "bin" ":" "/usr/" "sbin:" "/usr/" "bin", | |||
| 609 | NULL((void*)0)); | |||
| 610 | ||||
| 611 | /* Import locale variables LC_*= from configuration */ | |||
| 612 | locale_setup(&m->environment); | |||
| 613 | } else | |||
| 614 | /* The user manager passes its own environment | |||
| 615 | * along to its children. */ | |||
| 616 | m->environment = strv_copy(environ); | |||
| 617 | ||||
| 618 | if (!m->environment) | |||
| 619 | return -ENOMEM12; | |||
| 620 | ||||
| 621 | manager_sanitize_environment(m); | |||
| 622 | ||||
| 623 | return 0; | |||
| 624 | } | |||
| 625 | ||||
| 626 | static int manager_setup_prefix(Manager *m) { | |||
| 627 | struct table_entry { | |||
| 628 | uint64_t type; | |||
| 629 | const char *suffix; | |||
| 630 | }; | |||
| 631 | ||||
| 632 | static const struct table_entry paths_system[_EXEC_DIRECTORY_TYPE_MAX] = { | |||
| 633 | [EXEC_DIRECTORY_RUNTIME] = { SD_PATH_SYSTEM_RUNTIME, NULL((void*)0) }, | |||
| 634 | [EXEC_DIRECTORY_STATE] = { SD_PATH_SYSTEM_STATE_PRIVATE, NULL((void*)0) }, | |||
| 635 | [EXEC_DIRECTORY_CACHE] = { SD_PATH_SYSTEM_STATE_CACHE, NULL((void*)0) }, | |||
| 636 | [EXEC_DIRECTORY_LOGS] = { SD_PATH_SYSTEM_STATE_LOGS, NULL((void*)0) }, | |||
| 637 | [EXEC_DIRECTORY_CONFIGURATION] = { SD_PATH_SYSTEM_CONFIGURATION, NULL((void*)0) }, | |||
| 638 | }; | |||
| 639 | ||||
| 640 | static const struct table_entry paths_user[_EXEC_DIRECTORY_TYPE_MAX] = { | |||
| 641 | [EXEC_DIRECTORY_RUNTIME] = { SD_PATH_USER_RUNTIME, NULL((void*)0) }, | |||
| 642 | [EXEC_DIRECTORY_STATE] = { SD_PATH_USER_CONFIGURATION, NULL((void*)0) }, | |||
| 643 | [EXEC_DIRECTORY_CACHE] = { SD_PATH_USER_STATE_CACHE, NULL((void*)0) }, | |||
| 644 | [EXEC_DIRECTORY_LOGS] = { SD_PATH_USER_CONFIGURATION, "log" }, | |||
| 645 | [EXEC_DIRECTORY_CONFIGURATION] = { SD_PATH_USER_CONFIGURATION, NULL((void*)0) }, | |||
| 646 | }; | |||
| 647 | ||||
| 648 | const struct table_entry *p; | |||
| 649 | ExecDirectoryType i; | |||
| 650 | int r; | |||
| 651 | ||||
| 652 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 652, __PRETTY_FUNCTION__ ); } while (0); | |||
| 653 | ||||
| 654 | if (MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) | |||
| 655 | p = paths_system; | |||
| 656 | else | |||
| 657 | p = paths_user; | |||
| 658 | ||||
| 659 | for (i = 0; i < _EXEC_DIRECTORY_TYPE_MAX; i++) { | |||
| 660 | r = sd_path_home(p[i].type, p[i].suffix, &m->prefix[i]); | |||
| 661 | if (r < 0) | |||
| 662 | return r; | |||
| 663 | } | |||
| 664 | ||||
| 665 | return 0; | |||
| 666 | } | |||
| 667 | ||||
| 668 | static int manager_setup_run_queue(Manager *m) { | |||
| 669 | int r; | |||
| 670 | ||||
| 671 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 671, __PRETTY_FUNCTION__ ); } while (0); | |||
| 672 | assert(!m->run_queue_event_source)do { if ((__builtin_expect(!!(!(!m->run_queue_event_source )),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!m->run_queue_event_source" ), "../src/core/manager.c", 672, __PRETTY_FUNCTION__); } while (0); | |||
| 673 | ||||
| 674 | r = sd_event_add_defer(m->event, &m->run_queue_event_source, manager_dispatch_run_queue, m); | |||
| 675 | if (r < 0) | |||
| 676 | return r; | |||
| 677 | ||||
| 678 | r = sd_event_source_set_priority(m->run_queue_event_source, SD_EVENT_PRIORITY_IDLE); | |||
| 679 | if (r < 0) | |||
| 680 | return r; | |||
| 681 | ||||
| 682 | r = sd_event_source_set_enabled(m->run_queue_event_source, SD_EVENT_OFF); | |||
| 683 | if (r < 0) | |||
| 684 | return r; | |||
| 685 | ||||
| 686 | (void) sd_event_source_set_description(m->run_queue_event_source, "manager-run-queue"); | |||
| 687 | ||||
| 688 | return 0; | |||
| 689 | } | |||
| 690 | ||||
| 691 | static int manager_setup_sigchld_event_source(Manager *m) { | |||
| 692 | int r; | |||
| 693 | ||||
| 694 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 694, __PRETTY_FUNCTION__ ); } while (0); | |||
| 695 | assert(!m->sigchld_event_source)do { if ((__builtin_expect(!!(!(!m->sigchld_event_source)) ,0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!m->sigchld_event_source" ), "../src/core/manager.c", 695, __PRETTY_FUNCTION__); } while (0); | |||
| 696 | ||||
| 697 | r = sd_event_add_defer(m->event, &m->sigchld_event_source, manager_dispatch_sigchld, m); | |||
| 698 | if (r < 0) | |||
| 699 | return r; | |||
| 700 | ||||
| 701 | r = sd_event_source_set_priority(m->sigchld_event_source, SD_EVENT_PRIORITY_NORMAL-7); | |||
| 702 | if (r < 0) | |||
| 703 | return r; | |||
| 704 | ||||
| 705 | r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_OFF); | |||
| 706 | if (r < 0) | |||
| 707 | return r; | |||
| 708 | ||||
| 709 | (void) sd_event_source_set_description(m->sigchld_event_source, "manager-sigchld"); | |||
| 710 | ||||
| 711 | return 0; | |||
| 712 | } | |||
| 713 | ||||
| 714 | int manager_new(UnitFileScope scope, unsigned test_run_flags, Manager **_m) { | |||
| 715 | _cleanup_(manager_freep)__attribute__((cleanup(manager_freep))) Manager *m = NULL((void*)0); | |||
| 716 | int r; | |||
| 717 | ||||
| 718 | assert(_m)do { if ((__builtin_expect(!!(!(_m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("_m"), "../src/core/manager.c", 718, __PRETTY_FUNCTION__ ); } while (0); | |||
| 719 | assert(IN_SET(scope, UNIT_FILE_SYSTEM, UNIT_FILE_USER))do { if ((__builtin_expect(!!(!(({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended[20 - sizeof((int[]){UNIT_FILE_SYSTEM, UNIT_FILE_USER})/sizeof(int )]; switch(scope) { case UNIT_FILE_SYSTEM: case UNIT_FILE_USER : _found = 1; break; default: break; } _found; }))),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("IN_SET(scope, UNIT_FILE_SYSTEM, UNIT_FILE_USER)" ), "../src/core/manager.c", 719, __PRETTY_FUNCTION__); } while (0); | |||
| 720 | ||||
| 721 | m = new0(Manager, 1)((Manager*) calloc((1), sizeof(Manager))); | |||
| 722 | if (!m) | |||
| 723 | return -ENOMEM12; | |||
| 724 | ||||
| 725 | m->unit_file_scope = scope; | |||
| 726 | m->exit_code = _MANAGER_EXIT_CODE_INVALID; | |||
| 727 | m->default_timer_accuracy_usec = USEC_PER_MINUTE((usec_t) (60ULL*((usec_t) 1000000ULL))); | |||
| 728 | m->default_memory_accounting = MEMORY_ACCOUNTING_DEFAULT1; | |||
| 729 | m->default_tasks_accounting = true1; | |||
| 730 | m->default_tasks_max = UINT64_MAX(18446744073709551615UL); | |||
| 731 | m->default_timeout_start_usec = DEFAULT_TIMEOUT_USEC(90*((usec_t) 1000000ULL)); | |||
| 732 | m->default_timeout_stop_usec = DEFAULT_TIMEOUT_USEC(90*((usec_t) 1000000ULL)); | |||
| 733 | m->default_restart_usec = DEFAULT_RESTART_USEC(100*((usec_t) 1000ULL)); | |||
| 734 | m->original_log_level = -1; | |||
| 735 | m->original_log_target = _LOG_TARGET_INVALID; | |||
| 736 | ||||
| 737 | #if ENABLE_EFI1 | |||
| 738 | if (MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM) && detect_container() <= 0) | |||
| 739 | boot_timestamps(m->timestamps + MANAGER_TIMESTAMP_USERSPACE, | |||
| 740 | m->timestamps + MANAGER_TIMESTAMP_FIRMWARE, | |||
| 741 | m->timestamps + MANAGER_TIMESTAMP_LOADER); | |||
| 742 | #endif | |||
| 743 | ||||
| 744 | /* Prepare log fields we can use for structured logging */ | |||
| 745 | if (MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) { | |||
| 746 | m->unit_log_field = "UNIT="; | |||
| 747 | m->unit_log_format_string = "UNIT=%s"; | |||
| 748 | ||||
| 749 | m->invocation_log_field = "INVOCATION_ID="; | |||
| 750 | m->invocation_log_format_string = "INVOCATION_ID=%s"; | |||
| 751 | } else { | |||
| 752 | m->unit_log_field = "USER_UNIT="; | |||
| 753 | m->unit_log_format_string = "USER_UNIT=%s"; | |||
| 754 | ||||
| 755 | m->invocation_log_field = "USER_INVOCATION_ID="; | |||
| 756 | m->invocation_log_format_string = "USER_INVOCATION_ID=%s"; | |||
| 757 | } | |||
| 758 | ||||
| 759 | m->idle_pipe[0] = m->idle_pipe[1] = m->idle_pipe[2] = m->idle_pipe[3] = -1; | |||
| 760 | ||||
| 761 | m->pin_cgroupfs_fd = m->notify_fd = m->cgroups_agent_fd = m->signal_fd = m->time_change_fd = | |||
| 762 | m->dev_autofs_fd = m->private_listen_fd = m->cgroup_inotify_fd = | |||
| 763 | m->ask_password_inotify_fd = -1; | |||
| 764 | ||||
| 765 | m->user_lookup_fds[0] = m->user_lookup_fds[1] = -1; | |||
| 766 | ||||
| 767 | m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */ | |||
| 768 | ||||
| 769 | m->have_ask_password = -EINVAL22; /* we don't know */ | |||
| 770 | m->first_boot = -1; | |||
| 771 | ||||
| 772 | m->test_run_flags = test_run_flags; | |||
| 773 | ||||
| 774 | /* Reboot immediately if the user hits C-A-D more often than 7x per 2s */ | |||
| 775 | RATELIMIT_INIT(m->ctrl_alt_del_ratelimit, 2 * USEC_PER_SEC, 7)do { RateLimit *_r = &(m->ctrl_alt_del_ratelimit); _r-> interval = (2 * ((usec_t) 1000000ULL)); _r->burst = (7); _r ->num = 0; _r->begin = 0; } while (0); | |||
| 776 | ||||
| 777 | r = manager_default_environment(m); | |||
| 778 | if (r < 0) | |||
| 779 | return r; | |||
| 780 | ||||
| 781 | r = hashmap_ensure_allocated(&m->units, &string_hash_ops)internal_hashmap_ensure_allocated(&m->units, &string_hash_ops ); | |||
| 782 | if (r < 0) | |||
| 783 | return r; | |||
| 784 | ||||
| 785 | r = hashmap_ensure_allocated(&m->jobs, NULL)internal_hashmap_ensure_allocated(&m->jobs, ((void*)0) ); | |||
| 786 | if (r < 0) | |||
| 787 | return r; | |||
| 788 | ||||
| 789 | r = hashmap_ensure_allocated(&m->cgroup_unit, &path_hash_ops)internal_hashmap_ensure_allocated(&m->cgroup_unit, & path_hash_ops ); | |||
| 790 | if (r < 0) | |||
| 791 | return r; | |||
| 792 | ||||
| 793 | r = hashmap_ensure_allocated(&m->watch_bus, &string_hash_ops)internal_hashmap_ensure_allocated(&m->watch_bus, & string_hash_ops ); | |||
| 794 | if (r < 0) | |||
| 795 | return r; | |||
| 796 | ||||
| 797 | r = manager_setup_prefix(m); | |||
| 798 | if (r < 0) | |||
| 799 | return r; | |||
| 800 | ||||
| 801 | m->udev = udev_new(); | |||
| 802 | if (!m->udev) | |||
| 803 | return -ENOMEM12; | |||
| 804 | ||||
| 805 | r = sd_event_default(&m->event); | |||
| 806 | if (r < 0) | |||
| 807 | return r; | |||
| 808 | ||||
| 809 | r = manager_setup_run_queue(m); | |||
| 810 | if (r < 0) | |||
| 811 | return r; | |||
| 812 | ||||
| 813 | if (test_run_flags == MANAGER_TEST_RUN_MINIMAL) { | |||
| 814 | m->cgroup_root = strdup(""); | |||
| 815 | if (!m->cgroup_root) | |||
| 816 | return -ENOMEM12; | |||
| 817 | } else { | |||
| 818 | r = manager_setup_signals(m); | |||
| 819 | if (r < 0) | |||
| 820 | return r; | |||
| 821 | ||||
| 822 | r = manager_setup_cgroup(m); | |||
| 823 | if (r < 0) | |||
| 824 | return r; | |||
| 825 | ||||
| 826 | r = manager_setup_time_change(m); | |||
| 827 | if (r < 0) | |||
| 828 | return r; | |||
| 829 | ||||
| 830 | r = manager_read_timezone_stat(m); | |||
| 831 | if (r < 0) | |||
| 832 | return r; | |||
| 833 | ||||
| 834 | r = manager_setup_timezone_change(m); | |||
| 835 | if (r < 0) | |||
| 836 | return r; | |||
| 837 | ||||
| 838 | r = manager_setup_sigchld_event_source(m); | |||
| 839 | if (r < 0) | |||
| 840 | return r; | |||
| 841 | } | |||
| 842 | ||||
| 843 | if (MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM) && test_run_flags == 0) { | |||
| 844 | r = mkdir_label("/run/systemd/units", 0755); | |||
| 845 | if (r < 0 && r != -EEXIST17) | |||
| 846 | return r; | |||
| 847 | } | |||
| 848 | ||||
| 849 | m->taint_usr = | |||
| 850 | !in_initrd() && | |||
| 851 | dir_is_empty("/usr") > 0; | |||
| 852 | ||||
| 853 | /* Note that we do not set up the notify fd here. We do that after deserialization, | |||
| 854 | * since they might have gotten serialized across the reexec. */ | |||
| 855 | ||||
| 856 | *_m = TAKE_PTR(m)({ typeof(m) _ptr_ = (m); (m) = ((void*)0); _ptr_; }); | |||
| 857 | ||||
| 858 | return 0; | |||
| 859 | } | |||
| 860 | ||||
| 861 | static int manager_setup_notify(Manager *m) { | |||
| 862 | int r; | |||
| 863 | ||||
| 864 | if (m->test_run_flags) | |||
| 865 | return 0; | |||
| 866 | ||||
| 867 | if (m->notify_fd < 0) { | |||
| 868 | _cleanup_close___attribute__((cleanup(closep))) int fd = -1; | |||
| 869 | union sockaddr_union sa = { | |||
| 870 | .sa.sa_family = AF_UNIX1, | |||
| 871 | }; | |||
| 872 | static const int one = 1; | |||
| 873 | ||||
| 874 | /* First free all secondary fields */ | |||
| 875 | m->notify_socket = mfree(m->notify_socket); | |||
| 876 | m->notify_event_source = sd_event_source_unref(m->notify_event_source); | |||
| 877 | ||||
| 878 | fd = socket(AF_UNIX1, SOCK_DGRAMSOCK_DGRAM|SOCK_CLOEXECSOCK_CLOEXEC|SOCK_NONBLOCKSOCK_NONBLOCK, 0); | |||
| 879 | if (fd < 0) | |||
| 880 | return log_error_errno(errno, "Failed to allocate notification socket: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 880, __func__, "Failed to allocate notification socket: %m" ) : -abs(_e); }); | |||
| 881 | ||||
| 882 | fd_inc_rcvbuf(fd, NOTIFY_RCVBUF_SIZE(8*1024*1024)); | |||
| 883 | ||||
| 884 | m->notify_socket = strappend(m->prefix[EXEC_DIRECTORY_RUNTIME], "/systemd/notify"); | |||
| 885 | if (!m->notify_socket) | |||
| 886 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/core/manager.c", 886 , __func__); | |||
| 887 | ||||
| 888 | (void) mkdir_parents_label(m->notify_socket, 0755); | |||
| 889 | (void) unlink(m->notify_socket); | |||
| 890 | ||||
| 891 | strncpy(sa.un.sun_path, m->notify_socket, sizeof(sa.un.sun_path)-1); | |||
| 892 | r = bind(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)({ const struct sockaddr_un *_sa = &(sa.un); do { if ((__builtin_expect (!!(!(_sa->sun_family == 1)),0))) log_assert_failed_realm( LOG_REALM_SYSTEMD, ("_sa->sun_family == AF_UNIX"), "../src/core/manager.c" , 892, __PRETTY_FUNCTION__); } while (0); __builtin_offsetof( struct sockaddr_un, sun_path) + (_sa->sun_path[0] == 0 ? 1 + strnlen(_sa->sun_path+1, sizeof(_sa->sun_path)-1) : strnlen (_sa->sun_path, sizeof(_sa->sun_path))); })); | |||
| 893 | if (r < 0) | |||
| 894 | return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path)({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 894, __func__, "bind(%s) failed: %m" , sa.un.sun_path) : -abs(_e); }); | |||
| 895 | ||||
| 896 | r = setsockopt(fd, SOL_SOCKET1, SO_PASSCRED16, &one, sizeof(one)); | |||
| 897 | if (r < 0) | |||
| 898 | return log_error_errno(errno, "SO_PASSCRED failed: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 898, __func__, "SO_PASSCRED failed: %m" ) : -abs(_e); }); | |||
| 899 | ||||
| 900 | m->notify_fd = TAKE_FD(fd)({ int _fd_ = (fd); (fd) = -1; _fd_; }); | |||
| 901 | ||||
| 902 | log_debug("Using notification socket %s", m->notify_socket)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 902, __func__, "Using notification socket %s" , m->notify_socket) : -abs(_e); }); | |||
| 903 | } | |||
| 904 | ||||
| 905 | if (!m->notify_event_source) { | |||
| 906 | r = sd_event_add_io(m->event, &m->notify_event_source, m->notify_fd, EPOLLINEPOLLIN, manager_dispatch_notify_fd, m); | |||
| 907 | if (r < 0) | |||
| 908 | return log_error_errno(r, "Failed to allocate notify event source: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 908, __func__, "Failed to allocate notify event source: %m" ) : -abs(_e); }); | |||
| 909 | ||||
| 910 | /* Process notification messages a bit earlier than SIGCHLD, so that we can still identify to which | |||
| 911 | * service an exit message belongs. */ | |||
| 912 | r = sd_event_source_set_priority(m->notify_event_source, SD_EVENT_PRIORITY_NORMAL-8); | |||
| 913 | if (r < 0) | |||
| 914 | return log_error_errno(r, "Failed to set priority of notify event source: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 914, __func__, "Failed to set priority of notify event source: %m" ) : -abs(_e); }); | |||
| 915 | ||||
| 916 | (void) sd_event_source_set_description(m->notify_event_source, "manager-notify"); | |||
| 917 | } | |||
| 918 | ||||
| 919 | return 0; | |||
| 920 | } | |||
| 921 | ||||
| 922 | static int manager_setup_cgroups_agent(Manager *m) { | |||
| 923 | ||||
| 924 | static const union sockaddr_union sa = { | |||
| 925 | .un.sun_family = AF_UNIX1, | |||
| 926 | .un.sun_path = "/run/systemd/cgroups-agent", | |||
| 927 | }; | |||
| 928 | int r; | |||
| 929 | ||||
| 930 | /* This creates a listening socket we receive cgroups agent messages on. We do not use D-Bus for delivering | |||
| 931 | * these messages from the cgroups agent binary to PID 1, as the cgroups agent binary is very short-living, and | |||
| 932 | * each instance of it needs a new D-Bus connection. Since D-Bus connections are SOCK_STREAM/AF_UNIX, on | |||
| 933 | * overloaded systems the backlog of the D-Bus socket becomes relevant, as not more than the configured number | |||
| 934 | * of D-Bus connections may be queued until the kernel will start dropping further incoming connections, | |||
| 935 | * possibly resulting in lost cgroups agent messages. To avoid this, we'll use a private SOCK_DGRAM/AF_UNIX | |||
| 936 | * socket, where no backlog is relevant as communication may take place without an actual connect() cycle, and | |||
| 937 | * we thus won't lose messages. | |||
| 938 | * | |||
| 939 | * Note that PID 1 will forward the agent message to system bus, so that the user systemd instance may listen | |||
| 940 | * to it. The system instance hence listens on this special socket, but the user instances listen on the system | |||
| 941 | * bus for these messages. */ | |||
| 942 | ||||
| 943 | if (m->test_run_flags) | |||
| 944 | return 0; | |||
| 945 | ||||
| 946 | if (!MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) | |||
| 947 | return 0; | |||
| 948 | ||||
| 949 | r = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER"_systemd"); | |||
| 950 | if (r < 0) | |||
| 951 | return log_error_errno(r, "Failed to determine whether unified cgroups hierarchy is used: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 951, __func__, "Failed to determine whether unified cgroups hierarchy is used: %m" ) : -abs(_e); }); | |||
| 952 | if (r > 0) /* We don't need this anymore on the unified hierarchy */ | |||
| 953 | return 0; | |||
| 954 | ||||
| 955 | if (m->cgroups_agent_fd < 0) { | |||
| 956 | _cleanup_close___attribute__((cleanup(closep))) int fd = -1; | |||
| 957 | ||||
| 958 | /* First free all secondary fields */ | |||
| 959 | m->cgroups_agent_event_source = sd_event_source_unref(m->cgroups_agent_event_source); | |||
| 960 | ||||
| 961 | fd = socket(AF_UNIX1, SOCK_DGRAMSOCK_DGRAM|SOCK_CLOEXECSOCK_CLOEXEC|SOCK_NONBLOCKSOCK_NONBLOCK, 0); | |||
| 962 | if (fd < 0) | |||
| 963 | return log_error_errno(errno, "Failed to allocate cgroups agent socket: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 963, __func__, "Failed to allocate cgroups agent socket: %m" ) : -abs(_e); }); | |||
| 964 | ||||
| 965 | fd_inc_rcvbuf(fd, CGROUPS_AGENT_RCVBUF_SIZE(8*1024*1024)); | |||
| 966 | ||||
| 967 | (void) unlink(sa.un.sun_path); | |||
| 968 | ||||
| 969 | /* Only allow root to connect to this socket */ | |||
| 970 | RUN_WITH_UMASK(0077)for (__attribute__((cleanup(_reset_umask_))) struct _umask_struct_ _saved_umask_ = { umask(0077), 0 }; !_saved_umask_.quit ; _saved_umask_ .quit = 1) | |||
| 971 | r = bind(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)({ const struct sockaddr_un *_sa = &(sa.un); do { if ((__builtin_expect (!!(!(_sa->sun_family == 1)),0))) log_assert_failed_realm( LOG_REALM_SYSTEMD, ("_sa->sun_family == AF_UNIX"), "../src/core/manager.c" , 971, __PRETTY_FUNCTION__); } while (0); __builtin_offsetof( struct sockaddr_un, sun_path) + (_sa->sun_path[0] == 0 ? 1 + strnlen(_sa->sun_path+1, sizeof(_sa->sun_path)-1) : strnlen (_sa->sun_path, sizeof(_sa->sun_path))); })); | |||
| 972 | if (r < 0) | |||
| 973 | return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path)({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 973, __func__, "bind(%s) failed: %m" , sa.un.sun_path) : -abs(_e); }); | |||
| 974 | ||||
| 975 | m->cgroups_agent_fd = fd; | |||
| 976 | fd = -1; | |||
| 977 | } | |||
| 978 | ||||
| 979 | if (!m->cgroups_agent_event_source) { | |||
| 980 | r = sd_event_add_io(m->event, &m->cgroups_agent_event_source, m->cgroups_agent_fd, EPOLLINEPOLLIN, manager_dispatch_cgroups_agent_fd, m); | |||
| 981 | if (r < 0) | |||
| 982 | return log_error_errno(r, "Failed to allocate cgroups agent event source: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 982, __func__, "Failed to allocate cgroups agent event source: %m" ) : -abs(_e); }); | |||
| 983 | ||||
| 984 | /* Process cgroups notifications early, but after having processed service notification messages or | |||
| 985 | * SIGCHLD signals, so that a cgroup running empty is always just the last safety net of notification, | |||
| 986 | * and we collected the metadata the notification and SIGCHLD stuff offers first. Also see handling of | |||
| 987 | * cgroup inotify for the unified cgroup stuff. */ | |||
| 988 | r = sd_event_source_set_priority(m->cgroups_agent_event_source, SD_EVENT_PRIORITY_NORMAL-4); | |||
| 989 | if (r < 0) | |||
| 990 | return log_error_errno(r, "Failed to set priority of cgroups agent event source: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 990, __func__, "Failed to set priority of cgroups agent event source: %m" ) : -abs(_e); }); | |||
| 991 | ||||
| 992 | (void) sd_event_source_set_description(m->cgroups_agent_event_source, "manager-cgroups-agent"); | |||
| 993 | } | |||
| 994 | ||||
| 995 | return 0; | |||
| 996 | } | |||
| 997 | ||||
| 998 | static int manager_setup_user_lookup_fd(Manager *m) { | |||
| 999 | int r; | |||
| 1000 | ||||
| 1001 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1001, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1002 | ||||
| 1003 | /* Set up the socket pair used for passing UID/GID resolution results from forked off processes to PID | |||
| 1004 | * 1. Background: we can't do name lookups (NSS) from PID 1, since it might involve IPC and thus activation, | |||
| 1005 | * and we might hence deadlock on ourselves. Hence we do all user/group lookups asynchronously from the forked | |||
| 1006 | * off processes right before executing the binaries to start. In order to be able to clean up any IPC objects | |||
| 1007 | * created by a unit (see RemoveIPC=) we need to know in PID 1 the used UID/GID of the executed processes, | |||
| 1008 | * hence we establish this communication channel so that forked off processes can pass their UID/GID | |||
| 1009 | * information back to PID 1. The forked off processes send their resolved UID/GID to PID 1 in a simple | |||
| 1010 | * datagram, along with their unit name, so that we can share one communication socket pair among all units for | |||
| 1011 | * this purpose. | |||
| 1012 | * | |||
| 1013 | * You might wonder why we need a communication channel for this that is independent of the usual notification | |||
| 1014 | * socket scheme (i.e. $NOTIFY_SOCKET). The primary difference is about trust: data sent via the $NOTIFY_SOCKET | |||
| 1015 | * channel is only accepted if it originates from the right unit and if reception was enabled for it. The user | |||
| 1016 | * lookup socket OTOH is only accessible by PID 1 and its children until they exec(), and always available. | |||
| 1017 | * | |||
| 1018 | * Note that this function is called under two circumstances: when we first initialize (in which case we | |||
| 1019 | * allocate both the socket pair and the event source to listen on it), and when we deserialize after a reload | |||
| 1020 | * (in which case the socket pair already exists but we still need to allocate the event source for it). */ | |||
| 1021 | ||||
| 1022 | if (m->user_lookup_fds[0] < 0) { | |||
| 1023 | ||||
| 1024 | /* Free all secondary fields */ | |||
| 1025 | safe_close_pair(m->user_lookup_fds); | |||
| 1026 | m->user_lookup_event_source = sd_event_source_unref(m->user_lookup_event_source); | |||
| 1027 | ||||
| 1028 | if (socketpair(AF_UNIX1, SOCK_DGRAMSOCK_DGRAM|SOCK_CLOEXECSOCK_CLOEXEC, 0, m->user_lookup_fds) < 0) | |||
| 1029 | return log_error_errno(errno, "Failed to allocate user lookup socket: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 1029, __func__, "Failed to allocate user lookup socket: %m") : -abs(_e); }); | |||
| 1030 | ||||
| 1031 | (void) fd_inc_rcvbuf(m->user_lookup_fds[0], NOTIFY_RCVBUF_SIZE(8*1024*1024)); | |||
| 1032 | } | |||
| 1033 | ||||
| 1034 | if (!m->user_lookup_event_source) { | |||
| 1035 | r = sd_event_add_io(m->event, &m->user_lookup_event_source, m->user_lookup_fds[0], EPOLLINEPOLLIN, manager_dispatch_user_lookup_fd, m); | |||
| 1036 | if (r < 0) | |||
| 1037 | return log_error_errno(errno, "Failed to allocate user lookup event source: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 1037, __func__, "Failed to allocate user lookup event source: %m") : -abs(_e ); }); | |||
| 1038 | ||||
| 1039 | /* Process even earlier than the notify event source, so that we always know first about valid UID/GID | |||
| 1040 | * resolutions */ | |||
| 1041 | r = sd_event_source_set_priority(m->user_lookup_event_source, SD_EVENT_PRIORITY_NORMAL-11); | |||
| 1042 | if (r < 0) | |||
| 1043 | return log_error_errno(errno, "Failed to set priority ot user lookup event source: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 1043, __func__, "Failed to set priority ot user lookup event source: %m") : - abs(_e); }); | |||
| 1044 | ||||
| 1045 | (void) sd_event_source_set_description(m->user_lookup_event_source, "user-lookup"); | |||
| 1046 | } | |||
| 1047 | ||||
| 1048 | return 0; | |||
| 1049 | } | |||
| 1050 | ||||
| 1051 | static unsigned manager_dispatch_cleanup_queue(Manager *m) { | |||
| 1052 | Unit *u; | |||
| 1053 | unsigned n = 0; | |||
| 1054 | ||||
| 1055 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1055, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1056 | ||||
| 1057 | while ((u = m->cleanup_queue)) { | |||
| 1058 | assert(u->in_cleanup_queue)do { if ((__builtin_expect(!!(!(u->in_cleanup_queue)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("u->in_cleanup_queue" ), "../src/core/manager.c", 1058, __PRETTY_FUNCTION__); } while (0); | |||
| 1059 | ||||
| 1060 | unit_free(u); | |||
| 1061 | n++; | |||
| 1062 | } | |||
| 1063 | ||||
| 1064 | return n; | |||
| 1065 | } | |||
| 1066 | ||||
| 1067 | enum { | |||
| 1068 | GC_OFFSET_IN_PATH, /* This one is on the path we were traveling */ | |||
| 1069 | GC_OFFSET_UNSURE, /* No clue */ | |||
| 1070 | GC_OFFSET_GOOD, /* We still need this unit */ | |||
| 1071 | GC_OFFSET_BAD, /* We don't need this unit anymore */ | |||
| 1072 | _GC_OFFSET_MAX | |||
| 1073 | }; | |||
| 1074 | ||||
| 1075 | static void unit_gc_mark_good(Unit *u, unsigned gc_marker) { | |||
| 1076 | Unit *other; | |||
| 1077 | Iterator i; | |||
| 1078 | void *v; | |||
| 1079 | ||||
| 1080 | u->gc_marker = gc_marker + GC_OFFSET_GOOD; | |||
| 1081 | ||||
| 1082 | /* Recursively mark referenced units as GOOD as well */ | |||
| 1083 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_REFERENCES], i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((u->dependencies [UNIT_REFERENCES]), &(i), (void**)&(v), (const void** ) &(other)); ) | |||
| 1084 | if (other->gc_marker == gc_marker + GC_OFFSET_UNSURE) | |||
| 1085 | unit_gc_mark_good(other, gc_marker); | |||
| 1086 | } | |||
| 1087 | ||||
| 1088 | static void unit_gc_sweep(Unit *u, unsigned gc_marker) { | |||
| 1089 | Unit *other; | |||
| 1090 | bool_Bool is_bad; | |||
| 1091 | Iterator i; | |||
| 1092 | void *v; | |||
| 1093 | ||||
| 1094 | assert(u)do { if ((__builtin_expect(!!(!(u)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u"), "../src/core/manager.c", 1094, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1095 | ||||
| 1096 | if (IN_SET(u->gc_marker - gc_marker,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){GC_OFFSET_GOOD, GC_OFFSET_BAD, GC_OFFSET_UNSURE , GC_OFFSET_IN_PATH})/sizeof(int)]; switch(u->gc_marker - gc_marker ) { case GC_OFFSET_GOOD: case GC_OFFSET_BAD: case GC_OFFSET_UNSURE : case GC_OFFSET_IN_PATH: _found = 1; break; default: break; } _found; }) | |||
| 1097 | GC_OFFSET_GOOD, GC_OFFSET_BAD, GC_OFFSET_UNSURE, GC_OFFSET_IN_PATH)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){GC_OFFSET_GOOD, GC_OFFSET_BAD, GC_OFFSET_UNSURE , GC_OFFSET_IN_PATH})/sizeof(int)]; switch(u->gc_marker - gc_marker ) { case GC_OFFSET_GOOD: case GC_OFFSET_BAD: case GC_OFFSET_UNSURE : case GC_OFFSET_IN_PATH: _found = 1; break; default: break; } _found; })) | |||
| 1098 | return; | |||
| 1099 | ||||
| 1100 | if (u->in_cleanup_queue) | |||
| 1101 | goto bad; | |||
| 1102 | ||||
| 1103 | if (!unit_may_gc(u)) | |||
| 1104 | goto good; | |||
| 1105 | ||||
| 1106 | u->gc_marker = gc_marker + GC_OFFSET_IN_PATH; | |||
| 1107 | ||||
| 1108 | is_bad = true1; | |||
| 1109 | ||||
| 1110 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_REFERENCED_BY], i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((u->dependencies [UNIT_REFERENCED_BY]), &(i), (void**)&(v), (const void **) &(other)); ) { | |||
| 1111 | unit_gc_sweep(other, gc_marker); | |||
| 1112 | ||||
| 1113 | if (other->gc_marker == gc_marker + GC_OFFSET_GOOD) | |||
| 1114 | goto good; | |||
| 1115 | ||||
| 1116 | if (other->gc_marker != gc_marker + GC_OFFSET_BAD) | |||
| 1117 | is_bad = false0; | |||
| 1118 | } | |||
| 1119 | ||||
| 1120 | if (u->refs_by_target) { | |||
| 1121 | const UnitRef *ref; | |||
| 1122 | ||||
| 1123 | LIST_FOREACH(refs_by_target, ref, u->refs_by_target)for ((ref) = (u->refs_by_target); (ref); (ref) = (ref)-> refs_by_target_next) { | |||
| 1124 | unit_gc_sweep(ref->source, gc_marker); | |||
| 1125 | ||||
| 1126 | if (ref->source->gc_marker == gc_marker + GC_OFFSET_GOOD) | |||
| 1127 | goto good; | |||
| 1128 | ||||
| 1129 | if (ref->source->gc_marker != gc_marker + GC_OFFSET_BAD) | |||
| 1130 | is_bad = false0; | |||
| 1131 | } | |||
| 1132 | } | |||
| 1133 | ||||
| 1134 | if (is_bad) | |||
| 1135 | goto bad; | |||
| 1136 | ||||
| 1137 | /* We were unable to find anything out about this entry, so | |||
| 1138 | * let's investigate it later */ | |||
| 1139 | u->gc_marker = gc_marker + GC_OFFSET_UNSURE; | |||
| 1140 | unit_add_to_gc_queue(u); | |||
| 1141 | return; | |||
| 1142 | ||||
| 1143 | bad: | |||
| 1144 | /* We definitely know that this one is not useful anymore, so | |||
| 1145 | * let's mark it for deletion */ | |||
| 1146 | u->gc_marker = gc_marker + GC_OFFSET_BAD; | |||
| 1147 | unit_add_to_cleanup_queue(u); | |||
| 1148 | return; | |||
| 1149 | ||||
| 1150 | good: | |||
| 1151 | unit_gc_mark_good(u, gc_marker); | |||
| 1152 | } | |||
| 1153 | ||||
| 1154 | static unsigned manager_dispatch_gc_unit_queue(Manager *m) { | |||
| 1155 | unsigned n = 0, gc_marker; | |||
| 1156 | Unit *u; | |||
| 1157 | ||||
| 1158 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1158, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1159 | ||||
| 1160 | /* log_debug("Running GC..."); */ | |||
| 1161 | ||||
| 1162 | m->gc_marker += _GC_OFFSET_MAX; | |||
| 1163 | if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX) | |||
| 1164 | m->gc_marker = 1; | |||
| 1165 | ||||
| 1166 | gc_marker = m->gc_marker; | |||
| 1167 | ||||
| 1168 | while ((u = m->gc_unit_queue)) { | |||
| 1169 | assert(u->in_gc_queue)do { if ((__builtin_expect(!!(!(u->in_gc_queue)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u->in_gc_queue"), "../src/core/manager.c" , 1169, __PRETTY_FUNCTION__); } while (0); | |||
| 1170 | ||||
| 1171 | unit_gc_sweep(u, gc_marker); | |||
| 1172 | ||||
| 1173 | LIST_REMOVE(gc_queue, m->gc_unit_queue, u)do { typeof(*(m->gc_unit_queue)) **_head = &(m->gc_unit_queue ), *_item = (u); do { if ((__builtin_expect(!!(!(_item)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("_item"), "../src/core/manager.c" , 1173, __PRETTY_FUNCTION__); } while (0); if (_item->gc_queue_next ) _item->gc_queue_next->gc_queue_prev = _item->gc_queue_prev ; if (_item->gc_queue_prev) _item->gc_queue_prev->gc_queue_next = _item->gc_queue_next; else { do { if ((__builtin_expect (!!(!(*_head == _item)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("*_head == _item"), "../src/core/manager.c", 1173, __PRETTY_FUNCTION__ ); } while (0); *_head = _item->gc_queue_next; } _item-> gc_queue_next = _item->gc_queue_prev = ((void*)0); } while (0); | |||
| 1174 | u->in_gc_queue = false0; | |||
| 1175 | ||||
| 1176 | n++; | |||
| 1177 | ||||
| 1178 | if (IN_SET(u->gc_marker - gc_marker,({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){GC_OFFSET_BAD, GC_OFFSET_UNSURE})/sizeof (int)]; switch(u->gc_marker - gc_marker) { case GC_OFFSET_BAD : case GC_OFFSET_UNSURE: _found = 1; break; default: break; } _found; }) | |||
| 1179 | GC_OFFSET_BAD, GC_OFFSET_UNSURE)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){GC_OFFSET_BAD, GC_OFFSET_UNSURE})/sizeof (int)]; switch(u->gc_marker - gc_marker) { case GC_OFFSET_BAD : case GC_OFFSET_UNSURE: _found = 1; break; default: break; } _found; })) { | |||
| 1180 | if (u->id) | |||
| 1181 | log_unit_debug(u, "Collecting.")({ const Unit *_u = (u); _u ? log_object_internal(7, 0, "../src/core/manager.c" , 1181, __func__, _u->manager->unit_log_field, _u->id , _u->manager->invocation_log_field, _u->invocation_id_string , "Collecting.") : log_internal_realm(((LOG_REALM_SYSTEMD) << 10 | ((7))), 0, "../src/core/manager.c", 1181, __func__, "Collecting." ); }); | |||
| 1182 | u->gc_marker = gc_marker + GC_OFFSET_BAD; | |||
| 1183 | unit_add_to_cleanup_queue(u); | |||
| 1184 | } | |||
| 1185 | } | |||
| 1186 | ||||
| 1187 | return n; | |||
| 1188 | } | |||
| 1189 | ||||
| 1190 | static unsigned manager_dispatch_gc_job_queue(Manager *m) { | |||
| 1191 | unsigned n = 0; | |||
| 1192 | Job *j; | |||
| 1193 | ||||
| 1194 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1194, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1195 | ||||
| 1196 | while ((j = m->gc_job_queue)) { | |||
| 1197 | assert(j->in_gc_queue)do { if ((__builtin_expect(!!(!(j->in_gc_queue)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("j->in_gc_queue"), "../src/core/manager.c" , 1197, __PRETTY_FUNCTION__); } while (0); | |||
| 1198 | ||||
| 1199 | LIST_REMOVE(gc_queue, m->gc_job_queue, j)do { typeof(*(m->gc_job_queue)) **_head = &(m->gc_job_queue ), *_item = (j); do { if ((__builtin_expect(!!(!(_item)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("_item"), "../src/core/manager.c" , 1199, __PRETTY_FUNCTION__); } while (0); if (_item->gc_queue_next ) _item->gc_queue_next->gc_queue_prev = _item->gc_queue_prev ; if (_item->gc_queue_prev) _item->gc_queue_prev->gc_queue_next = _item->gc_queue_next; else { do { if ((__builtin_expect (!!(!(*_head == _item)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("*_head == _item"), "../src/core/manager.c", 1199, __PRETTY_FUNCTION__ ); } while (0); *_head = _item->gc_queue_next; } _item-> gc_queue_next = _item->gc_queue_prev = ((void*)0); } while (0); | |||
| 1200 | j->in_gc_queue = false0; | |||
| 1201 | ||||
| 1202 | n++; | |||
| 1203 | ||||
| 1204 | if (!job_may_gc(j)) | |||
| 1205 | continue; | |||
| 1206 | ||||
| 1207 | log_unit_debug(j->unit, "Collecting job.")({ const Unit *_u = (j->unit); _u ? log_object_internal(7, 0, "../src/core/manager.c", 1207, __func__, _u->manager-> unit_log_field, _u->id, _u->manager->invocation_log_field , _u->invocation_id_string, "Collecting job.") : log_internal_realm (((LOG_REALM_SYSTEMD) << 10 | ((7))), 0, "../src/core/manager.c" , 1207, __func__, "Collecting job."); }); | |||
| 1208 | (void) job_finish_and_invalidate(j, JOB_COLLECTED, false0, false0); | |||
| 1209 | } | |||
| 1210 | ||||
| 1211 | return n; | |||
| 1212 | } | |||
| 1213 | ||||
| 1214 | static unsigned manager_dispatch_stop_when_unneeded_queue(Manager *m) { | |||
| 1215 | unsigned n = 0; | |||
| 1216 | Unit *u; | |||
| 1217 | int r; | |||
| 1218 | ||||
| 1219 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1219, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1220 | ||||
| 1221 | while ((u = m->stop_when_unneeded_queue)) { | |||
| 1222 | _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0}); | |||
| 1223 | assert(m->stop_when_unneeded_queue)do { if ((__builtin_expect(!!(!(m->stop_when_unneeded_queue )),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("m->stop_when_unneeded_queue" ), "../src/core/manager.c", 1223, __PRETTY_FUNCTION__); } while (0); | |||
| 1224 | ||||
| 1225 | assert(u->in_stop_when_unneeded_queue)do { if ((__builtin_expect(!!(!(u->in_stop_when_unneeded_queue )),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("u->in_stop_when_unneeded_queue" ), "../src/core/manager.c", 1225, __PRETTY_FUNCTION__); } while (0); | |||
| 1226 | LIST_REMOVE(stop_when_unneeded_queue, m->stop_when_unneeded_queue, u)do { typeof(*(m->stop_when_unneeded_queue)) **_head = & (m->stop_when_unneeded_queue), *_item = (u); do { if ((__builtin_expect (!!(!(_item)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("_item"), "../src/core/manager.c", 1226, __PRETTY_FUNCTION__ ); } while (0); if (_item->stop_when_unneeded_queue_next) _item ->stop_when_unneeded_queue_next->stop_when_unneeded_queue_prev = _item->stop_when_unneeded_queue_prev; if (_item->stop_when_unneeded_queue_prev ) _item->stop_when_unneeded_queue_prev->stop_when_unneeded_queue_next = _item->stop_when_unneeded_queue_next; else { do { if (( __builtin_expect(!!(!(*_head == _item)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("*_head == _item"), "../src/core/manager.c" , 1226, __PRETTY_FUNCTION__); } while (0); *_head = _item-> stop_when_unneeded_queue_next; } _item->stop_when_unneeded_queue_next = _item->stop_when_unneeded_queue_prev = ((void*)0); } while (0); | |||
| 1227 | u->in_stop_when_unneeded_queue = false0; | |||
| 1228 | ||||
| 1229 | n++; | |||
| 1230 | ||||
| 1231 | if (!unit_is_unneeded(u)) | |||
| 1232 | continue; | |||
| 1233 | ||||
| 1234 | log_unit_debug(u, "Unit is not needed anymore.")({ const Unit *_u = (u); _u ? log_object_internal(7, 0, "../src/core/manager.c" , 1234, __func__, _u->manager->unit_log_field, _u->id , _u->manager->invocation_log_field, _u->invocation_id_string , "Unit is not needed anymore.") : log_internal_realm(((LOG_REALM_SYSTEMD ) << 10 | ((7))), 0, "../src/core/manager.c", 1234, __func__ , "Unit is not needed anymore."); }); | |||
| 1235 | ||||
| 1236 | /* If stopping a unit fails continuously we might enter a stop loop here, hence stop acting on the | |||
| 1237 | * service being unnecessary after a while. */ | |||
| 1238 | ||||
| 1239 | if (!ratelimit_below(&u->auto_stop_ratelimit)) { | |||
| 1240 | log_unit_warning(u, "Unit not needed anymore, but not stopping since we tried this too often recently.")({ const Unit *_u = (u); _u ? log_object_internal(4, 0, "../src/core/manager.c" , 1240, __func__, _u->manager->unit_log_field, _u->id , _u->manager->invocation_log_field, _u->invocation_id_string , "Unit not needed anymore, but not stopping since we tried this too often recently." ) : log_internal_realm(((LOG_REALM_SYSTEMD) << 10 | ((4 ))), 0, "../src/core/manager.c", 1240, __func__, "Unit not needed anymore, but not stopping since we tried this too often recently." ); }); | |||
| 1241 | continue; | |||
| 1242 | } | |||
| 1243 | ||||
| 1244 | /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */ | |||
| 1245 | r = manager_add_job(u->manager, JOB_STOP, u, JOB_FAIL, NULL((void*)0), &error, NULL((void*)0)); | |||
| 1246 | if (r < 0) | |||
| 1247 | log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %s", bus_error_message(&error, r))({ const Unit *_u = (u); _u ? log_object_internal(4, r, "../src/core/manager.c" , 1247, __func__, _u->manager->unit_log_field, _u->id , _u->manager->invocation_log_field, _u->invocation_id_string , "Failed to enqueue stop job, ignoring: %s", bus_error_message (&error, r)) : log_internal_realm(((LOG_REALM_SYSTEMD) << 10 | ((4))), r, "../src/core/manager.c", 1247, __func__, "Failed to enqueue stop job, ignoring: %s" , bus_error_message(&error, r)); }); | |||
| 1248 | } | |||
| 1249 | ||||
| 1250 | return n; | |||
| 1251 | } | |||
| 1252 | ||||
| 1253 | static void manager_clear_jobs_and_units(Manager *m) { | |||
| 1254 | Unit *u; | |||
| 1255 | ||||
| 1256 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1256, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1257 | ||||
| 1258 | while ((u = hashmap_first(m->units))) | |||
| 1259 | unit_free(u); | |||
| 1260 | ||||
| 1261 | manager_dispatch_cleanup_queue(m); | |||
| 1262 | ||||
| 1263 | assert(!m->load_queue)do { if ((__builtin_expect(!!(!(!m->load_queue)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("!m->load_queue"), "../src/core/manager.c" , 1263, __PRETTY_FUNCTION__); } while (0); | |||
| 1264 | assert(!m->run_queue)do { if ((__builtin_expect(!!(!(!m->run_queue)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("!m->run_queue"), "../src/core/manager.c" , 1264, __PRETTY_FUNCTION__); } while (0); | |||
| 1265 | assert(!m->dbus_unit_queue)do { if ((__builtin_expect(!!(!(!m->dbus_unit_queue)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!m->dbus_unit_queue" ), "../src/core/manager.c", 1265, __PRETTY_FUNCTION__); } while (0); | |||
| 1266 | assert(!m->dbus_job_queue)do { if ((__builtin_expect(!!(!(!m->dbus_job_queue)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("!m->dbus_job_queue"), "../src/core/manager.c" , 1266, __PRETTY_FUNCTION__); } while (0); | |||
| 1267 | assert(!m->cleanup_queue)do { if ((__builtin_expect(!!(!(!m->cleanup_queue)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("!m->cleanup_queue"), "../src/core/manager.c" , 1267, __PRETTY_FUNCTION__); } while (0); | |||
| 1268 | assert(!m->gc_unit_queue)do { if ((__builtin_expect(!!(!(!m->gc_unit_queue)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("!m->gc_unit_queue"), "../src/core/manager.c" , 1268, __PRETTY_FUNCTION__); } while (0); | |||
| 1269 | assert(!m->gc_job_queue)do { if ((__builtin_expect(!!(!(!m->gc_job_queue)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("!m->gc_job_queue"), "../src/core/manager.c" , 1269, __PRETTY_FUNCTION__); } while (0); | |||
| 1270 | assert(!m->stop_when_unneeded_queue)do { if ((__builtin_expect(!!(!(!m->stop_when_unneeded_queue )),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!m->stop_when_unneeded_queue" ), "../src/core/manager.c", 1270, __PRETTY_FUNCTION__); } while (0); | |||
| 1271 | ||||
| 1272 | assert(hashmap_isempty(m->jobs))do { if ((__builtin_expect(!!(!(hashmap_isempty(m->jobs))) ,0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("hashmap_isempty(m->jobs)" ), "../src/core/manager.c", 1272, __PRETTY_FUNCTION__); } while (0); | |||
| 1273 | assert(hashmap_isempty(m->units))do { if ((__builtin_expect(!!(!(hashmap_isempty(m->units)) ),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("hashmap_isempty(m->units)" ), "../src/core/manager.c", 1273, __PRETTY_FUNCTION__); } while (0); | |||
| 1274 | ||||
| 1275 | m->n_on_console = 0; | |||
| 1276 | m->n_running_jobs = 0; | |||
| 1277 | } | |||
| 1278 | ||||
| 1279 | Manager* manager_free(Manager *m) { | |||
| 1280 | UnitType c; | |||
| 1281 | ExecDirectoryType dt; | |||
| 1282 | ||||
| 1283 | if (!m) | |||
| 1284 | return NULL((void*)0); | |||
| 1285 | ||||
| 1286 | manager_clear_jobs_and_units(m); | |||
| 1287 | ||||
| 1288 | for (c = 0; c < _UNIT_TYPE_MAX; c++) | |||
| 1289 | if (unit_vtable[c]->shutdown) | |||
| 1290 | unit_vtable[c]->shutdown(m); | |||
| 1291 | ||||
| 1292 | /* If we reexecute ourselves, we keep the root cgroup around */ | |||
| 1293 | manager_shutdown_cgroup(m, m->exit_code != MANAGER_REEXECUTE); | |||
| 1294 | ||||
| 1295 | lookup_paths_flush_generator(&m->lookup_paths); | |||
| 1296 | ||||
| 1297 | bus_done(m); | |||
| 1298 | ||||
| 1299 | exec_runtime_vacuum(m); | |||
| 1300 | hashmap_free(m->exec_runtime_by_id); | |||
| 1301 | ||||
| 1302 | dynamic_user_vacuum(m, false0); | |||
| 1303 | hashmap_free(m->dynamic_users); | |||
| 1304 | ||||
| 1305 | hashmap_free(m->units); | |||
| 1306 | hashmap_free(m->units_by_invocation_id); | |||
| 1307 | hashmap_free(m->jobs); | |||
| 1308 | hashmap_free(m->watch_pids); | |||
| 1309 | hashmap_free(m->watch_bus); | |||
| 1310 | ||||
| 1311 | set_free(m->startup_units); | |||
| 1312 | set_free(m->failed_units); | |||
| 1313 | ||||
| 1314 | sd_event_source_unref(m->signal_event_source); | |||
| 1315 | sd_event_source_unref(m->sigchld_event_source); | |||
| 1316 | sd_event_source_unref(m->notify_event_source); | |||
| 1317 | sd_event_source_unref(m->cgroups_agent_event_source); | |||
| 1318 | sd_event_source_unref(m->time_change_event_source); | |||
| 1319 | sd_event_source_unref(m->timezone_change_event_source); | |||
| 1320 | sd_event_source_unref(m->jobs_in_progress_event_source); | |||
| 1321 | sd_event_source_unref(m->run_queue_event_source); | |||
| 1322 | sd_event_source_unref(m->user_lookup_event_source); | |||
| 1323 | sd_event_source_unref(m->sync_bus_names_event_source); | |||
| 1324 | ||||
| 1325 | safe_close(m->signal_fd); | |||
| 1326 | safe_close(m->notify_fd); | |||
| 1327 | safe_close(m->cgroups_agent_fd); | |||
| 1328 | safe_close(m->time_change_fd); | |||
| 1329 | safe_close_pair(m->user_lookup_fds); | |||
| 1330 | ||||
| 1331 | manager_close_ask_password(m); | |||
| 1332 | ||||
| 1333 | manager_close_idle_pipe(m); | |||
| 1334 | ||||
| 1335 | udev_unref(m->udev); | |||
| 1336 | sd_event_unref(m->event); | |||
| 1337 | ||||
| 1338 | free(m->notify_socket); | |||
| 1339 | ||||
| 1340 | lookup_paths_free(&m->lookup_paths); | |||
| 1341 | strv_free(m->environment); | |||
| 1342 | ||||
| 1343 | hashmap_free(m->cgroup_unit); | |||
| 1344 | set_free_free(m->unit_path_cache); | |||
| 1345 | ||||
| 1346 | free(m->switch_root); | |||
| 1347 | free(m->switch_root_init); | |||
| 1348 | ||||
| 1349 | rlimit_free_all(m->rlimit); | |||
| 1350 | ||||
| 1351 | assert(hashmap_isempty(m->units_requiring_mounts_for))do { if ((__builtin_expect(!!(!(hashmap_isempty(m->units_requiring_mounts_for ))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("hashmap_isempty(m->units_requiring_mounts_for)" ), "../src/core/manager.c", 1351, __PRETTY_FUNCTION__); } while (0); | |||
| 1352 | hashmap_free(m->units_requiring_mounts_for); | |||
| 1353 | ||||
| 1354 | hashmap_free(m->uid_refs); | |||
| 1355 | hashmap_free(m->gid_refs); | |||
| 1356 | ||||
| 1357 | for (dt = 0; dt < _EXEC_DIRECTORY_TYPE_MAX; dt++) | |||
| 1358 | m->prefix[dt] = mfree(m->prefix[dt]); | |||
| 1359 | ||||
| 1360 | return mfree(m); | |||
| 1361 | } | |||
| 1362 | ||||
| 1363 | static void manager_enumerate_perpetual(Manager *m) { | |||
| 1364 | UnitType c; | |||
| 1365 | ||||
| 1366 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1366, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1367 | ||||
| 1368 | /* Let's ask every type to load all units from disk/kernel that it might know */ | |||
| 1369 | for (c = 0; c < _UNIT_TYPE_MAX; c++) { | |||
| 1370 | if (!unit_type_supported(c)) { | |||
| 1371 | log_debug("Unit type .%s is not supported on this system.", unit_type_to_string(c))({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 1371, __func__, "Unit type .%s is not supported on this system." , unit_type_to_string(c)) : -abs(_e); }); | |||
| 1372 | continue; | |||
| 1373 | } | |||
| 1374 | ||||
| 1375 | if (unit_vtable[c]->enumerate_perpetual) | |||
| 1376 | unit_vtable[c]->enumerate_perpetual(m); | |||
| 1377 | } | |||
| 1378 | } | |||
| 1379 | ||||
| 1380 | static void manager_enumerate(Manager *m) { | |||
| 1381 | UnitType c; | |||
| 1382 | ||||
| 1383 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1383, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1384 | ||||
| 1385 | /* Let's ask every type to load all units from disk/kernel that it might know */ | |||
| 1386 | for (c = 0; c < _UNIT_TYPE_MAX; c++) { | |||
| 1387 | if (!unit_type_supported(c)) { | |||
| 1388 | log_debug("Unit type .%s is not supported on this system.", unit_type_to_string(c))({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 1388, __func__, "Unit type .%s is not supported on this system." , unit_type_to_string(c)) : -abs(_e); }); | |||
| 1389 | continue; | |||
| 1390 | } | |||
| 1391 | ||||
| 1392 | if (unit_vtable[c]->enumerate) | |||
| 1393 | unit_vtable[c]->enumerate(m); | |||
| 1394 | } | |||
| 1395 | ||||
| 1396 | manager_dispatch_load_queue(m); | |||
| 1397 | } | |||
| 1398 | ||||
| 1399 | static void manager_coldplug(Manager *m) { | |||
| 1400 | Iterator i; | |||
| 1401 | Unit *u; | |||
| 1402 | char *k; | |||
| 1403 | int r; | |||
| 1404 | ||||
| 1405 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1405, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1406 | ||||
| 1407 | log_debug("Invoking unit coldplug() handlers…")({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 1407, __func__, "Invoking unit coldplug() handlers…" ) : -abs(_e); }); | |||
| 1408 | ||||
| 1409 | /* Let's place the units back into their deserialized state */ | |||
| 1410 | HASHMAP_FOREACH_KEY(u, k, m->units, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((m->units), & (i), (void**)&(u), (const void**) &(k)); ) { | |||
| 1411 | ||||
| 1412 | /* ignore aliases */ | |||
| 1413 | if (u->id != k) | |||
| 1414 | continue; | |||
| 1415 | ||||
| 1416 | r = unit_coldplug(u); | |||
| 1417 | if (r < 0) | |||
| 1418 | log_warning_errno(r, "We couldn't coldplug %s, proceeding anyway: %m", u->id)({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 1418, __func__, "We couldn't coldplug %s, proceeding anyway: %m" , u->id) : -abs(_e); }); | |||
| 1419 | } | |||
| 1420 | } | |||
| 1421 | ||||
| 1422 | static void manager_catchup(Manager *m) { | |||
| 1423 | Iterator i; | |||
| 1424 | Unit *u; | |||
| 1425 | char *k; | |||
| 1426 | ||||
| 1427 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1427, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1428 | ||||
| 1429 | log_debug("Invoking unit catchup() handlers…")({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 1429, __func__, "Invoking unit catchup() handlers…" ) : -abs(_e); }); | |||
| 1430 | ||||
| 1431 | /* Let's catch up on any state changes that happened while we were reloading/reexecing */ | |||
| 1432 | HASHMAP_FOREACH_KEY(u, k, m->units, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((m->units), & (i), (void**)&(u), (const void**) &(k)); ) { | |||
| 1433 | ||||
| 1434 | /* ignore aliases */ | |||
| 1435 | if (u->id != k) | |||
| 1436 | continue; | |||
| 1437 | ||||
| 1438 | unit_catchup(u); | |||
| 1439 | } | |||
| 1440 | } | |||
| 1441 | ||||
| 1442 | static void manager_build_unit_path_cache(Manager *m) { | |||
| 1443 | char **i; | |||
| 1444 | int r; | |||
| 1445 | ||||
| 1446 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1446, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1447 | ||||
| 1448 | set_free_free(m->unit_path_cache); | |||
| 1449 | ||||
| 1450 | m->unit_path_cache = set_new(&path_hash_ops)internal_set_new(&path_hash_ops ); | |||
| 1451 | if (!m->unit_path_cache) { | |||
| 1452 | r = -ENOMEM12; | |||
| 1453 | goto fail; | |||
| 1454 | } | |||
| 1455 | ||||
| 1456 | /* This simply builds a list of files we know exist, so that | |||
| 1457 | * we don't always have to go to disk */ | |||
| 1458 | ||||
| 1459 | STRV_FOREACH(i, m->lookup_paths.search_path)for ((i) = (m->lookup_paths.search_path); (i) && * (i); (i)++) { | |||
| 1460 | _cleanup_closedir___attribute__((cleanup(closedirp))) DIR *d = NULL((void*)0); | |||
| 1461 | struct dirent *de; | |||
| 1462 | ||||
| 1463 | d = opendir(*i); | |||
| 1464 | if (!d) { | |||
| 1465 | if (errno(*__errno_location ()) != ENOENT2) | |||
| 1466 | log_warning_errno(errno, "Failed to open directory %s, ignoring: %m", *i)({ int _level = ((4)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 1466, __func__, "Failed to open directory %s, ignoring: %m", *i) : -abs(_e); }); | |||
| 1467 | continue; | |||
| 1468 | } | |||
| 1469 | ||||
| 1470 | FOREACH_DIRENT(de, d, r = -errno; goto fail)for ((*__errno_location ()) = 0, de = readdir(d);; (*__errno_location ()) = 0, de = readdir(d)) if (!de) { if ((*__errno_location ( )) > 0) { r = -(*__errno_location ()); goto fail; } break; } else if (hidden_or_backup_file((de)->d_name)) continue; else { | |||
| 1471 | char *p; | |||
| 1472 | ||||
| 1473 | p = strjoin(streq(*i, "/") ? "" : *i, "/", de->d_name)strjoin_real(((strcmp((*i),("/")) == 0) ? "" : *i), "/", de-> d_name, ((void*)0)); | |||
| 1474 | if (!p) { | |||
| 1475 | r = -ENOMEM12; | |||
| 1476 | goto fail; | |||
| 1477 | } | |||
| 1478 | ||||
| 1479 | r = set_consume(m->unit_path_cache, p); | |||
| 1480 | if (r < 0) | |||
| 1481 | goto fail; | |||
| 1482 | } | |||
| 1483 | } | |||
| 1484 | ||||
| 1485 | return; | |||
| 1486 | ||||
| 1487 | fail: | |||
| 1488 | log_warning_errno(r, "Failed to build unit path cache, proceeding without: %m")({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 1488, __func__, "Failed to build unit path cache, proceeding without: %m" ) : -abs(_e); }); | |||
| 1489 | m->unit_path_cache = set_free_free(m->unit_path_cache); | |||
| 1490 | } | |||
| 1491 | ||||
| 1492 | static void manager_distribute_fds(Manager *m, FDSet *fds) { | |||
| 1493 | Iterator i; | |||
| 1494 | Unit *u; | |||
| 1495 | ||||
| 1496 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1496, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1497 | ||||
| 1498 | HASHMAP_FOREACH(u, m->units, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((m->units), & (i), (void**)&(u), ((void*)0)); ) { | |||
| 1499 | ||||
| 1500 | if (fdset_size(fds) <= 0) | |||
| 1501 | break; | |||
| 1502 | ||||
| 1503 | if (!UNIT_VTABLE(u)unit_vtable[(u)->type]->distribute_fds) | |||
| 1504 | continue; | |||
| 1505 | ||||
| 1506 | UNIT_VTABLE(u)unit_vtable[(u)->type]->distribute_fds(u, fds); | |||
| 1507 | } | |||
| 1508 | } | |||
| 1509 | ||||
| 1510 | static bool_Bool manager_dbus_is_running(Manager *m, bool_Bool deserialized) { | |||
| 1511 | Unit *u; | |||
| 1512 | ||||
| 1513 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1513, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1514 | ||||
| 1515 | /* This checks whether the dbus instance we are supposed to expose our APIs on is up. We check both the socket | |||
| 1516 | * and the service unit. If the 'deserialized' parameter is true we'll check the deserialized state of the unit | |||
| 1517 | * rather than the current one. */ | |||
| 1518 | ||||
| 1519 | if (m->test_run_flags != 0) | |||
| 1520 | return false0; | |||
| 1521 | ||||
| 1522 | u = manager_get_unit(m, SPECIAL_DBUS_SOCKET"dbus.socket"); | |||
| 1523 | if (!u) | |||
| 1524 | return false0; | |||
| 1525 | if ((deserialized ? SOCKET(u)->deserialized_state : SOCKET(u)->state) != SOCKET_RUNNING) | |||
| 1526 | return false0; | |||
| 1527 | ||||
| 1528 | u = manager_get_unit(m, SPECIAL_DBUS_SERVICE"dbus.service"); | |||
| 1529 | if (!u) | |||
| 1530 | return false0; | |||
| 1531 | if (!IN_SET((deserialized ? SERVICE(u)->deserialized_state : SERVICE(u)->state), SERVICE_RUNNING, SERVICE_RELOAD)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){SERVICE_RUNNING, SERVICE_RELOAD})/sizeof (int)]; switch((deserialized ? SERVICE(u)->deserialized_state : SERVICE(u)->state)) { case SERVICE_RUNNING: case SERVICE_RELOAD : _found = 1; break; default: break; } _found; })) | |||
| 1532 | return false0; | |||
| 1533 | ||||
| 1534 | return true1; | |||
| 1535 | } | |||
| 1536 | ||||
| 1537 | static void manager_setup_bus(Manager *m) { | |||
| 1538 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1538, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1539 | ||||
| 1540 | /* Let's set up our private bus connection now, unconditionally */ | |||
| 1541 | (void) bus_init_private(m); | |||
| 1542 | ||||
| 1543 | /* If we are in --user mode also connect to the system bus now */ | |||
| 1544 | if (MANAGER_IS_USER(m)((m)->unit_file_scope != UNIT_FILE_SYSTEM)) | |||
| 1545 | (void) bus_init_system(m); | |||
| 1546 | ||||
| 1547 | /* Let's connect to the bus now, but only if the unit is supposed to be up */ | |||
| 1548 | if (manager_dbus_is_running(m, MANAGER_IS_RELOADING(m)((m)->n_reloading > 0))) { | |||
| 1549 | (void) bus_init_api(m); | |||
| 1550 | ||||
| 1551 | if (MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) | |||
| 1552 | (void) bus_init_system(m); | |||
| 1553 | } | |||
| 1554 | } | |||
| 1555 | ||||
| 1556 | static void manager_preset_all(Manager *m) { | |||
| 1557 | int r; | |||
| 1558 | ||||
| 1559 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1559, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1560 | ||||
| 1561 | if (m->first_boot <= 0) | |||
| 1562 | return; | |||
| 1563 | ||||
| 1564 | if (!MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) | |||
| 1565 | return; | |||
| 1566 | ||||
| 1567 | if (m->test_run_flags != 0) | |||
| 1568 | return; | |||
| 1569 | ||||
| 1570 | /* If this is the first boot, and we are in the host system, then preset everything */ | |||
| 1571 | r = unit_file_preset_all(UNIT_FILE_SYSTEM, 0, NULL((void*)0), UNIT_FILE_PRESET_ENABLE_ONLY, NULL((void*)0), 0); | |||
| 1572 | if (r < 0) | |||
| 1573 | log_full_errno(r == -EEXIST ? LOG_NOTICE : LOG_WARNING, r,({ int _level = ((r == -17 ? 5 : 4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 1574, __func__, "Failed to populate /etc with preset unit settings, ignoring: %m" ) : -abs(_e); }) | |||
| 1574 | "Failed to populate /etc with preset unit settings, ignoring: %m")({ int _level = ((r == -17 ? 5 : 4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 1574, __func__, "Failed to populate /etc with preset unit settings, ignoring: %m" ) : -abs(_e); }); | |||
| 1575 | else | |||
| 1576 | log_info("Populated /etc with preset unit settings.")({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 1576, __func__, "Populated /etc with preset unit settings." ) : -abs(_e); }); | |||
| 1577 | } | |||
| 1578 | ||||
| 1579 | int manager_startup(Manager *m, FILE *serialization, FDSet *fds) { | |||
| 1580 | int r; | |||
| 1581 | ||||
| 1582 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1582, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1583 | ||||
| 1584 | /* If we are running in test mode, we still want to run the generators, | |||
| 1585 | * but we should not touch the real generator directories. */ | |||
| 1586 | r = lookup_paths_init(&m->lookup_paths, m->unit_file_scope, | |||
| 1587 | m->test_run_flags ? LOOKUP_PATHS_TEMPORARY_GENERATED : 0, | |||
| 1588 | NULL((void*)0)); | |||
| 1589 | if (r < 0) | |||
| 1590 | return r; | |||
| 1591 | ||||
| 1592 | r = manager_run_environment_generators(m); | |||
| 1593 | if (r < 0) | |||
| 1594 | return r; | |||
| 1595 | ||||
| 1596 | dual_timestamp_get(m->timestamps + MANAGER_TIMESTAMP_GENERATORS_START); | |||
| 1597 | r = manager_run_generators(m); | |||
| 1598 | dual_timestamp_get(m->timestamps + MANAGER_TIMESTAMP_GENERATORS_FINISH); | |||
| 1599 | if (r < 0) | |||
| 1600 | return r; | |||
| 1601 | ||||
| 1602 | manager_preset_all(m); | |||
| 1603 | lookup_paths_reduce(&m->lookup_paths); | |||
| 1604 | manager_build_unit_path_cache(m); | |||
| 1605 | ||||
| 1606 | /* If we will deserialize make sure that during enumeration | |||
| 1607 | * this is already known, so we increase the counter here | |||
| 1608 | * already */ | |||
| 1609 | if (serialization) | |||
| 1610 | m->n_reloading++; | |||
| 1611 | ||||
| 1612 | /* First, enumerate what we can from all config files */ | |||
| 1613 | dual_timestamp_get(m->timestamps + MANAGER_TIMESTAMP_UNITS_LOAD_START); | |||
| 1614 | manager_enumerate_perpetual(m); | |||
| 1615 | manager_enumerate(m); | |||
| 1616 | dual_timestamp_get(m->timestamps + MANAGER_TIMESTAMP_UNITS_LOAD_FINISH); | |||
| 1617 | ||||
| 1618 | /* Second, deserialize if there is something to deserialize */ | |||
| 1619 | if (serialization) { | |||
| 1620 | r = manager_deserialize(m, serialization, fds); | |||
| 1621 | if (r < 0) | |||
| 1622 | return log_error_errno(r, "Deserialization failed: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 1622, __func__, "Deserialization failed: %m" ) : -abs(_e); }); | |||
| 1623 | } | |||
| 1624 | ||||
| 1625 | /* Any fds left? Find some unit which wants them. This is | |||
| 1626 | * useful to allow container managers to pass some file | |||
| 1627 | * descriptors to us pre-initialized. This enables | |||
| 1628 | * socket-based activation of entire containers. */ | |||
| 1629 | manager_distribute_fds(m, fds); | |||
| 1630 | ||||
| 1631 | /* We might have deserialized the notify fd, but if we didn't | |||
| 1632 | * then let's create the bus now */ | |||
| 1633 | r = manager_setup_notify(m); | |||
| 1634 | if (r < 0) | |||
| 1635 | /* No sense to continue without notifications, our children would fail anyway. */ | |||
| 1636 | return r; | |||
| 1637 | ||||
| 1638 | r = manager_setup_cgroups_agent(m); | |||
| 1639 | if (r < 0) | |||
| 1640 | /* Likewise, no sense to continue without empty cgroup notifications. */ | |||
| 1641 | return r; | |||
| 1642 | ||||
| 1643 | r = manager_setup_user_lookup_fd(m); | |||
| 1644 | if (r < 0) | |||
| 1645 | /* This shouldn't fail, except if things are really broken. */ | |||
| 1646 | return r; | |||
| 1647 | ||||
| 1648 | /* Connect to the bus if we are good for it */ | |||
| 1649 | manager_setup_bus(m); | |||
| 1650 | ||||
| 1651 | /* Now that we are connected to all possible busses, let's deserialize who is tracking us. */ | |||
| 1652 | (void) bus_track_coldplug(m, &m->subscribed, false0, m->deserialized_subscribed); | |||
| 1653 | m->deserialized_subscribed = strv_free(m->deserialized_subscribed); | |||
| 1654 | ||||
| 1655 | /* Third, fire things up! */ | |||
| 1656 | manager_coldplug(m); | |||
| 1657 | ||||
| 1658 | /* Release any dynamic users no longer referenced */ | |||
| 1659 | dynamic_user_vacuum(m, true1); | |||
| 1660 | ||||
| 1661 | exec_runtime_vacuum(m); | |||
| 1662 | ||||
| 1663 | /* Release any references to UIDs/GIDs no longer referenced, and destroy any IPC owned by them */ | |||
| 1664 | manager_vacuum_uid_refs(m); | |||
| 1665 | manager_vacuum_gid_refs(m); | |||
| 1666 | ||||
| 1667 | if (serialization) { | |||
| 1668 | assert(m->n_reloading > 0)do { if ((__builtin_expect(!!(!(m->n_reloading > 0)),0) )) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("m->n_reloading > 0" ), "../src/core/manager.c", 1668, __PRETTY_FUNCTION__); } while (0); | |||
| 1669 | m->n_reloading--; | |||
| 1670 | ||||
| 1671 | /* Let's wait for the UnitNew/JobNew messages being | |||
| 1672 | * sent, before we notify that the reload is | |||
| 1673 | * finished */ | |||
| 1674 | m->send_reloading_done = true1; | |||
| 1675 | } | |||
| 1676 | ||||
| 1677 | /* Let's finally catch up with any changes that took place while we were reloading/reexecing */ | |||
| 1678 | manager_catchup(m); | |||
| 1679 | ||||
| 1680 | return 0; | |||
| 1681 | } | |||
| 1682 | ||||
| 1683 | int manager_add_job( | |||
| 1684 | Manager *m, | |||
| 1685 | JobType type, | |||
| 1686 | Unit *unit, | |||
| 1687 | JobMode mode, | |||
| 1688 | Set *affected_jobs, | |||
| 1689 | sd_bus_error *error, | |||
| 1690 | Job **ret) { | |||
| 1691 | ||||
| 1692 | Transaction *tr; | |||
| 1693 | int r; | |||
| 1694 | ||||
| 1695 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1695, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1696 | assert(type < _JOB_TYPE_MAX)do { if ((__builtin_expect(!!(!(type < _JOB_TYPE_MAX)),0)) ) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("type < _JOB_TYPE_MAX" ), "../src/core/manager.c", 1696, __PRETTY_FUNCTION__); } while (0); | |||
| 1697 | assert(unit)do { if ((__builtin_expect(!!(!(unit)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("unit"), "../src/core/manager.c", 1697, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1698 | assert(mode < _JOB_MODE_MAX)do { if ((__builtin_expect(!!(!(mode < _JOB_MODE_MAX)),0)) ) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("mode < _JOB_MODE_MAX" ), "../src/core/manager.c", 1698, __PRETTY_FUNCTION__); } while (0); | |||
| 1699 | ||||
| 1700 | if (mode == JOB_ISOLATE && type != JOB_START) | |||
| 1701 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Isolate is only valid for start."); | |||
| 1702 | ||||
| 1703 | if (mode == JOB_ISOLATE && !unit->allow_isolate) | |||
| 1704 | return sd_bus_error_setf(error, BUS_ERROR_NO_ISOLATION"org.freedesktop.systemd1.NoIsolation", "Operation refused, unit may not be isolated."); | |||
| 1705 | ||||
| 1706 | log_unit_debug(unit, "Trying to enqueue job %s/%s/%s", unit->id, job_type_to_string(type), job_mode_to_string(mode))({ const Unit *_u = (unit); _u ? log_object_internal(7, 0, "../src/core/manager.c" , 1706, __func__, _u->manager->unit_log_field, _u->id , _u->manager->invocation_log_field, _u->invocation_id_string , "Trying to enqueue job %s/%s/%s", unit->id, job_type_to_string (type), job_mode_to_string(mode)) : log_internal_realm(((LOG_REALM_SYSTEMD ) << 10 | ((7))), 0, "../src/core/manager.c", 1706, __func__ , "Trying to enqueue job %s/%s/%s", unit->id, job_type_to_string (type), job_mode_to_string(mode)); }); | |||
| 1707 | ||||
| 1708 | type = job_type_collapse(type, unit); | |||
| 1709 | ||||
| 1710 | tr = transaction_new(mode == JOB_REPLACE_IRREVERSIBLY); | |||
| 1711 | if (!tr) | |||
| 1712 | return -ENOMEM12; | |||
| 1713 | ||||
| 1714 | r = transaction_add_job_and_dependencies(tr, type, unit, NULL((void*)0), true1, false0, | |||
| 1715 | IN_SET(mode, JOB_IGNORE_DEPENDENCIES, JOB_IGNORE_REQUIREMENTS)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){JOB_IGNORE_DEPENDENCIES, JOB_IGNORE_REQUIREMENTS })/sizeof(int)]; switch(mode) { case JOB_IGNORE_DEPENDENCIES: case JOB_IGNORE_REQUIREMENTS: _found = 1; break; default: break ; } _found; }), | |||
| 1716 | mode == JOB_IGNORE_DEPENDENCIES, error); | |||
| 1717 | if (r < 0) | |||
| 1718 | goto tr_abort; | |||
| 1719 | ||||
| 1720 | if (mode == JOB_ISOLATE) { | |||
| 1721 | r = transaction_add_isolate_jobs(tr, m); | |||
| 1722 | if (r < 0) | |||
| 1723 | goto tr_abort; | |||
| 1724 | } | |||
| 1725 | ||||
| 1726 | r = transaction_activate(tr, m, mode, affected_jobs, error); | |||
| 1727 | if (r < 0) | |||
| 1728 | goto tr_abort; | |||
| 1729 | ||||
| 1730 | log_unit_debug(unit,({ const Unit *_u = (unit); _u ? log_object_internal(7, 0, "../src/core/manager.c" , 1732, __func__, _u->manager->unit_log_field, _u->id , _u->manager->invocation_log_field, _u->invocation_id_string , "Enqueued job %s/%s as %u", unit->id, job_type_to_string (type), (unsigned) tr->anchor_job->id) : log_internal_realm (((LOG_REALM_SYSTEMD) << 10 | ((7))), 0, "../src/core/manager.c" , 1732, __func__, "Enqueued job %s/%s as %u", unit->id, job_type_to_string (type), (unsigned) tr->anchor_job->id); }) | |||
| 1731 | "Enqueued job %s/%s as %u", unit->id,({ const Unit *_u = (unit); _u ? log_object_internal(7, 0, "../src/core/manager.c" , 1732, __func__, _u->manager->unit_log_field, _u->id , _u->manager->invocation_log_field, _u->invocation_id_string , "Enqueued job %s/%s as %u", unit->id, job_type_to_string (type), (unsigned) tr->anchor_job->id) : log_internal_realm (((LOG_REALM_SYSTEMD) << 10 | ((7))), 0, "../src/core/manager.c" , 1732, __func__, "Enqueued job %s/%s as %u", unit->id, job_type_to_string (type), (unsigned) tr->anchor_job->id); }) | |||
| 1732 | job_type_to_string(type), (unsigned) tr->anchor_job->id)({ const Unit *_u = (unit); _u ? log_object_internal(7, 0, "../src/core/manager.c" , 1732, __func__, _u->manager->unit_log_field, _u->id , _u->manager->invocation_log_field, _u->invocation_id_string , "Enqueued job %s/%s as %u", unit->id, job_type_to_string (type), (unsigned) tr->anchor_job->id) : log_internal_realm (((LOG_REALM_SYSTEMD) << 10 | ((7))), 0, "../src/core/manager.c" , 1732, __func__, "Enqueued job %s/%s as %u", unit->id, job_type_to_string (type), (unsigned) tr->anchor_job->id); }); | |||
| 1733 | ||||
| 1734 | if (ret) | |||
| 1735 | *ret = tr->anchor_job; | |||
| 1736 | ||||
| 1737 | transaction_free(tr); | |||
| 1738 | return 0; | |||
| 1739 | ||||
| 1740 | tr_abort: | |||
| 1741 | transaction_abort(tr); | |||
| 1742 | transaction_free(tr); | |||
| 1743 | return r; | |||
| 1744 | } | |||
| 1745 | ||||
| 1746 | int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, Set *affected_jobs, sd_bus_error *e, Job **ret) { | |||
| 1747 | Unit *unit = NULL((void*)0); /* just to appease gcc, initialization is not really necessary */ | |||
| 1748 | int r; | |||
| 1749 | ||||
| 1750 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1750, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1751 | assert(type < _JOB_TYPE_MAX)do { if ((__builtin_expect(!!(!(type < _JOB_TYPE_MAX)),0)) ) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("type < _JOB_TYPE_MAX" ), "../src/core/manager.c", 1751, __PRETTY_FUNCTION__); } while (0); | |||
| 1752 | assert(name)do { if ((__builtin_expect(!!(!(name)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("name"), "../src/core/manager.c", 1752, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1753 | assert(mode < _JOB_MODE_MAX)do { if ((__builtin_expect(!!(!(mode < _JOB_MODE_MAX)),0)) ) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("mode < _JOB_MODE_MAX" ), "../src/core/manager.c", 1753, __PRETTY_FUNCTION__); } while (0); | |||
| 1754 | ||||
| 1755 | r = manager_load_unit(m, name, NULL((void*)0), NULL((void*)0), &unit); | |||
| 1756 | if (r < 0) | |||
| 1757 | return r; | |||
| 1758 | assert(unit)do { if ((__builtin_expect(!!(!(unit)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("unit"), "../src/core/manager.c", 1758, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1759 | ||||
| 1760 | return manager_add_job(m, type, unit, mode, affected_jobs, e, ret); | |||
| 1761 | } | |||
| 1762 | ||||
| 1763 | int manager_add_job_by_name_and_warn(Manager *m, JobType type, const char *name, JobMode mode, Set *affected_jobs, Job **ret) { | |||
| 1764 | _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0}); | |||
| 1765 | int r; | |||
| 1766 | ||||
| 1767 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1767, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1768 | assert(type < _JOB_TYPE_MAX)do { if ((__builtin_expect(!!(!(type < _JOB_TYPE_MAX)),0)) ) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("type < _JOB_TYPE_MAX" ), "../src/core/manager.c", 1768, __PRETTY_FUNCTION__); } while (0); | |||
| 1769 | assert(name)do { if ((__builtin_expect(!!(!(name)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("name"), "../src/core/manager.c", 1769, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1770 | assert(mode < _JOB_MODE_MAX)do { if ((__builtin_expect(!!(!(mode < _JOB_MODE_MAX)),0)) ) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("mode < _JOB_MODE_MAX" ), "../src/core/manager.c", 1770, __PRETTY_FUNCTION__); } while (0); | |||
| 1771 | ||||
| 1772 | r = manager_add_job_by_name(m, type, name, mode, affected_jobs, &error, ret); | |||
| 1773 | if (r < 0) | |||
| 1774 | return log_warning_errno(r, "Failed to enqueue %s job for %s: %s", job_mode_to_string(mode), name, bus_error_message(&error, r))({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 1774, __func__, "Failed to enqueue %s job for %s: %s" , job_mode_to_string(mode), name, bus_error_message(&error , r)) : -abs(_e); }); | |||
| 1775 | ||||
| 1776 | return r; | |||
| 1777 | } | |||
| 1778 | ||||
| 1779 | int manager_propagate_reload(Manager *m, Unit *unit, JobMode mode, sd_bus_error *e) { | |||
| 1780 | int r; | |||
| 1781 | Transaction *tr; | |||
| 1782 | ||||
| 1783 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1783, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1784 | assert(unit)do { if ((__builtin_expect(!!(!(unit)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("unit"), "../src/core/manager.c", 1784, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1785 | assert(mode < _JOB_MODE_MAX)do { if ((__builtin_expect(!!(!(mode < _JOB_MODE_MAX)),0)) ) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("mode < _JOB_MODE_MAX" ), "../src/core/manager.c", 1785, __PRETTY_FUNCTION__); } while (0); | |||
| 1786 | assert(mode != JOB_ISOLATE)do { if ((__builtin_expect(!!(!(mode != JOB_ISOLATE)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("mode != JOB_ISOLATE"), "../src/core/manager.c" , 1786, __PRETTY_FUNCTION__); } while (0); /* Isolate is only valid for start */ | |||
| 1787 | ||||
| 1788 | tr = transaction_new(mode == JOB_REPLACE_IRREVERSIBLY); | |||
| 1789 | if (!tr) | |||
| 1790 | return -ENOMEM12; | |||
| 1791 | ||||
| 1792 | /* We need an anchor job */ | |||
| 1793 | r = transaction_add_job_and_dependencies(tr, JOB_NOP, unit, NULL((void*)0), false0, false0, true1, true1, e); | |||
| 1794 | if (r < 0) | |||
| 1795 | goto tr_abort; | |||
| 1796 | ||||
| 1797 | /* Failure in adding individual dependencies is ignored, so this always succeeds. */ | |||
| 1798 | transaction_add_propagate_reload_jobs(tr, unit, tr->anchor_job, mode == JOB_IGNORE_DEPENDENCIES, e); | |||
| 1799 | ||||
| 1800 | r = transaction_activate(tr, m, mode, NULL((void*)0), e); | |||
| 1801 | if (r < 0) | |||
| 1802 | goto tr_abort; | |||
| 1803 | ||||
| 1804 | transaction_free(tr); | |||
| 1805 | return 0; | |||
| 1806 | ||||
| 1807 | tr_abort: | |||
| 1808 | transaction_abort(tr); | |||
| 1809 | transaction_free(tr); | |||
| 1810 | return r; | |||
| 1811 | } | |||
| 1812 | ||||
| 1813 | Job *manager_get_job(Manager *m, uint32_t id) { | |||
| 1814 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1814, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1815 | ||||
| 1816 | return hashmap_get(m->jobs, UINT32_TO_PTR(id)((void *) ((uintptr_t) (id)))); | |||
| 1817 | } | |||
| 1818 | ||||
| 1819 | Unit *manager_get_unit(Manager *m, const char *name) { | |||
| 1820 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1820, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1821 | assert(name)do { if ((__builtin_expect(!!(!(name)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("name"), "../src/core/manager.c", 1821, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1822 | ||||
| 1823 | return hashmap_get(m->units, name); | |||
| 1824 | } | |||
| 1825 | ||||
| 1826 | static int manager_dispatch_target_deps_queue(Manager *m) { | |||
| 1827 | Unit *u; | |||
| 1828 | unsigned k; | |||
| 1829 | int r = 0; | |||
| 1830 | ||||
| 1831 | static const UnitDependency deps[] = { | |||
| 1832 | UNIT_REQUIRED_BY, | |||
| 1833 | UNIT_REQUISITE_OF, | |||
| 1834 | UNIT_WANTED_BY, | |||
| 1835 | UNIT_BOUND_BY | |||
| 1836 | }; | |||
| 1837 | ||||
| 1838 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1838, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1839 | ||||
| 1840 | while ((u = m->target_deps_queue)) { | |||
| 1841 | assert(u->in_target_deps_queue)do { if ((__builtin_expect(!!(!(u->in_target_deps_queue)), 0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("u->in_target_deps_queue" ), "../src/core/manager.c", 1841, __PRETTY_FUNCTION__); } while (0); | |||
| 1842 | ||||
| 1843 | LIST_REMOVE(target_deps_queue, u->manager->target_deps_queue, u)do { typeof(*(u->manager->target_deps_queue)) **_head = &(u->manager->target_deps_queue), *_item = (u); do { if ((__builtin_expect(!!(!(_item)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("_item"), "../src/core/manager.c", 1843, __PRETTY_FUNCTION__); } while (0); if (_item->target_deps_queue_next ) _item->target_deps_queue_next->target_deps_queue_prev = _item->target_deps_queue_prev; if (_item->target_deps_queue_prev ) _item->target_deps_queue_prev->target_deps_queue_next = _item->target_deps_queue_next; else { do { if ((__builtin_expect (!!(!(*_head == _item)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("*_head == _item"), "../src/core/manager.c", 1843, __PRETTY_FUNCTION__ ); } while (0); *_head = _item->target_deps_queue_next; } _item ->target_deps_queue_next = _item->target_deps_queue_prev = ((void*)0); } while (0); | |||
| 1844 | u->in_target_deps_queue = false0; | |||
| 1845 | ||||
| 1846 | for (k = 0; k < ELEMENTSOF(deps)__extension__ (__builtin_choose_expr( !__builtin_types_compatible_p (typeof(deps), typeof(&*(deps))), sizeof(deps)/sizeof((deps )[0]), ((void)0))); k++) { | |||
| 1847 | Unit *target; | |||
| 1848 | Iterator i; | |||
| 1849 | void *v; | |||
| 1850 | ||||
| 1851 | HASHMAP_FOREACH_KEY(v, target, u->dependencies[deps[k]], i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((u->dependencies [deps[k]]), &(i), (void**)&(v), (const void**) &( target)); ) { | |||
| 1852 | r = unit_add_default_target_dependency(u, target); | |||
| 1853 | if (r < 0) | |||
| 1854 | return r; | |||
| 1855 | } | |||
| 1856 | } | |||
| 1857 | } | |||
| 1858 | ||||
| 1859 | return r; | |||
| 1860 | } | |||
| 1861 | ||||
| 1862 | unsigned manager_dispatch_load_queue(Manager *m) { | |||
| 1863 | Unit *u; | |||
| 1864 | unsigned n = 0; | |||
| 1865 | ||||
| 1866 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1866, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1867 | ||||
| 1868 | /* Make sure we are not run recursively */ | |||
| 1869 | if (m->dispatching_load_queue) | |||
| 1870 | return 0; | |||
| 1871 | ||||
| 1872 | m->dispatching_load_queue = true1; | |||
| 1873 | ||||
| 1874 | /* Dispatches the load queue. Takes a unit from the queue and | |||
| 1875 | * tries to load its data until the queue is empty */ | |||
| 1876 | ||||
| 1877 | while ((u = m->load_queue)) { | |||
| 1878 | assert(u->in_load_queue)do { if ((__builtin_expect(!!(!(u->in_load_queue)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u->in_load_queue"), "../src/core/manager.c" , 1878, __PRETTY_FUNCTION__); } while (0); | |||
| 1879 | ||||
| 1880 | unit_load(u); | |||
| 1881 | n++; | |||
| 1882 | } | |||
| 1883 | ||||
| 1884 | m->dispatching_load_queue = false0; | |||
| 1885 | ||||
| 1886 | /* Dispatch the units waiting for their target dependencies to be added now, as all targets that we know about | |||
| 1887 | * should be loaded and have aliases resolved */ | |||
| 1888 | (void) manager_dispatch_target_deps_queue(m); | |||
| 1889 | ||||
| 1890 | return n; | |||
| 1891 | } | |||
| 1892 | ||||
| 1893 | int manager_load_unit_prepare( | |||
| 1894 | Manager *m, | |||
| 1895 | const char *name, | |||
| 1896 | const char *path, | |||
| 1897 | sd_bus_error *e, | |||
| 1898 | Unit **_ret) { | |||
| 1899 | ||||
| 1900 | _cleanup_(unit_freep)__attribute__((cleanup(unit_freep))) Unit *cleanup_ret = NULL((void*)0); | |||
| 1901 | Unit *ret; | |||
| 1902 | UnitType t; | |||
| 1903 | int r; | |||
| 1904 | ||||
| 1905 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1905, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1906 | assert(name || path)do { if ((__builtin_expect(!!(!(name || path)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("name || path"), "../src/core/manager.c" , 1906, __PRETTY_FUNCTION__); } while (0); | |||
| 1907 | assert(_ret)do { if ((__builtin_expect(!!(!(_ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("_ret"), "../src/core/manager.c", 1907, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1908 | ||||
| 1909 | /* This will prepare the unit for loading, but not actually | |||
| 1910 | * load anything from disk. */ | |||
| 1911 | ||||
| 1912 | if (path
| |||
| 1913 | return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Path %s is not absolute.", path); | |||
| 1914 | ||||
| 1915 | if (!name
| |||
| 1916 | name = basename(path); | |||
| 1917 | ||||
| 1918 | t = unit_name_to_type(name); | |||
| 1919 | ||||
| 1920 | if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) { | |||
| 1921 | if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) | |||
| 1922 | return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Unit name %s is missing the instance name.", name); | |||
| 1923 | ||||
| 1924 | return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Unit name %s is not valid.", name); | |||
| 1925 | } | |||
| 1926 | ||||
| 1927 | ret = manager_get_unit(m, name); | |||
| 1928 | if (ret) { | |||
| 1929 | *_ret = ret; | |||
| 1930 | return 1; | |||
| 1931 | } | |||
| 1932 | ||||
| 1933 | ret = cleanup_ret = unit_new(m, unit_vtable[t]->object_size); | |||
| 1934 | if (!ret) | |||
| 1935 | return -ENOMEM12; | |||
| 1936 | ||||
| 1937 | if (path) { | |||
| 1938 | ret->fragment_path = strdup(path); | |||
| 1939 | if (!ret->fragment_path) | |||
| 1940 | return -ENOMEM12; | |||
| 1941 | } | |||
| 1942 | ||||
| 1943 | r = unit_add_name(ret, name); | |||
| 1944 | if (r < 0) | |||
| 1945 | return r; | |||
| 1946 | ||||
| 1947 | unit_add_to_load_queue(ret); | |||
| 1948 | unit_add_to_dbus_queue(ret); | |||
| 1949 | unit_add_to_gc_queue(ret); | |||
| 1950 | ||||
| 1951 | *_ret = ret; | |||
| 1952 | cleanup_ret = NULL((void*)0); | |||
| 1953 | ||||
| 1954 | return 0; | |||
| 1955 | } | |||
| 1956 | ||||
| 1957 | int manager_load_unit( | |||
| 1958 | Manager *m, | |||
| 1959 | const char *name, | |||
| 1960 | const char *path, | |||
| 1961 | sd_bus_error *e, | |||
| 1962 | Unit **_ret) { | |||
| 1963 | ||||
| 1964 | int r; | |||
| 1965 | ||||
| 1966 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 1966, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1967 | assert(_ret)do { if ((__builtin_expect(!!(!(_ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("_ret"), "../src/core/manager.c", 1967, __PRETTY_FUNCTION__ ); } while (0); | |||
| 1968 | ||||
| 1969 | /* This will load the service information files, but not actually | |||
| 1970 | * start any services or anything. */ | |||
| 1971 | ||||
| 1972 | r = manager_load_unit_prepare(m, name, path, e, _ret); | |||
| 1973 | if (r != 0) | |||
| 1974 | return r; | |||
| 1975 | ||||
| 1976 | manager_dispatch_load_queue(m); | |||
| 1977 | ||||
| 1978 | *_ret = unit_follow_merge(*_ret); | |||
| 1979 | return 0; | |||
| 1980 | } | |||
| 1981 | ||||
| 1982 | int manager_load_startable_unit_or_warn( | |||
| 1983 | Manager *m, | |||
| 1984 | const char *name, | |||
| 1985 | const char *path, | |||
| 1986 | Unit **ret) { | |||
| 1987 | ||||
| 1988 | /* Load a unit, make sure it loaded fully and is not masked. */ | |||
| 1989 | ||||
| 1990 | _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0}); | |||
| 1991 | Unit *unit; | |||
| 1992 | int r; | |||
| 1993 | ||||
| 1994 | r = manager_load_unit(m, name, path, &error, &unit); | |||
| 1995 | if (r < 0) | |||
| 1996 | return log_error_errno(r, "Failed to load %s %s: %s",({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 1998, __func__, "Failed to load %s %s: %s" , name ? "unit" : "unit file", name ?: path, bus_error_message (&error, r)) : -abs(_e); }) | |||
| 1997 | name ? "unit" : "unit file", name ?: path,({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 1998, __func__, "Failed to load %s %s: %s" , name ? "unit" : "unit file", name ?: path, bus_error_message (&error, r)) : -abs(_e); }) | |||
| 1998 | bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 1998, __func__, "Failed to load %s %s: %s" , name ? "unit" : "unit file", name ?: path, bus_error_message (&error, r)) : -abs(_e); }); | |||
| 1999 | ||||
| 2000 | r = bus_unit_validate_load_state(unit, &error); | |||
| 2001 | if (r < 0) | |||
| 2002 | return log_error_errno(r, "%s", bus_error_message(&error, r))({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2002, __func__, "%s", bus_error_message (&error, r)) : -abs(_e); }); | |||
| 2003 | ||||
| 2004 | *ret = unit; | |||
| 2005 | return 0; | |||
| 2006 | } | |||
| 2007 | ||||
| 2008 | void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) { | |||
| 2009 | Iterator i; | |||
| 2010 | Job *j; | |||
| 2011 | ||||
| 2012 | assert(s)do { if ((__builtin_expect(!!(!(s)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("s"), "../src/core/manager.c", 2012, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2013 | assert(f)do { if ((__builtin_expect(!!(!(f)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("f"), "../src/core/manager.c", 2013, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2014 | ||||
| 2015 | HASHMAP_FOREACH(j, s->jobs, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((s->jobs), & (i), (void**)&(j), ((void*)0)); ) | |||
| 2016 | job_dump(j, f, prefix); | |||
| 2017 | } | |||
| 2018 | ||||
| 2019 | void manager_dump_units(Manager *s, FILE *f, const char *prefix) { | |||
| 2020 | Iterator i; | |||
| 2021 | Unit *u; | |||
| 2022 | const char *t; | |||
| 2023 | ||||
| 2024 | assert(s)do { if ((__builtin_expect(!!(!(s)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("s"), "../src/core/manager.c", 2024, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2025 | assert(f)do { if ((__builtin_expect(!!(!(f)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("f"), "../src/core/manager.c", 2025, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2026 | ||||
| 2027 | HASHMAP_FOREACH_KEY(u, t, s->units, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((s->units), & (i), (void**)&(u), (const void**) &(t)); ) | |||
| 2028 | if (u->id == t) | |||
| 2029 | unit_dump(u, f, prefix); | |||
| 2030 | } | |||
| 2031 | ||||
| 2032 | void manager_dump(Manager *m, FILE *f, const char *prefix) { | |||
| 2033 | ManagerTimestamp q; | |||
| 2034 | ||||
| 2035 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 2035, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2036 | assert(f)do { if ((__builtin_expect(!!(!(f)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("f"), "../src/core/manager.c", 2036, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2037 | ||||
| 2038 | for (q = 0; q < _MANAGER_TIMESTAMP_MAX; q++) { | |||
| 2039 | char buf[FORMAT_TIMESTAMP_MAX(3+1+10+1+8+1+6+1+6+1)]; | |||
| 2040 | ||||
| 2041 | if (dual_timestamp_is_set(m->timestamps + q)) | |||
| 2042 | fprintf(f, "%sTimestamp %s: %s\n", | |||
| 2043 | strempty(prefix), | |||
| 2044 | manager_timestamp_to_string(q), | |||
| 2045 | format_timestamp(buf, sizeof(buf), m->timestamps[q].realtime)); | |||
| 2046 | } | |||
| 2047 | ||||
| 2048 | manager_dump_units(m, f, prefix); | |||
| 2049 | manager_dump_jobs(m, f, prefix); | |||
| 2050 | } | |||
| 2051 | ||||
| 2052 | int manager_get_dump_string(Manager *m, char **ret) { | |||
| 2053 | _cleanup_free___attribute__((cleanup(freep))) char *dump = NULL((void*)0); | |||
| 2054 | _cleanup_fclose___attribute__((cleanup(fclosep))) FILE *f = NULL((void*)0); | |||
| 2055 | size_t size; | |||
| 2056 | int r; | |||
| 2057 | ||||
| 2058 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 2058, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2059 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/core/manager.c", 2059, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2060 | ||||
| 2061 | f = open_memstream(&dump, &size); | |||
| 2062 | if (!f) | |||
| 2063 | return -errno(*__errno_location ()); | |||
| 2064 | ||||
| 2065 | (void) __fsetlocking(f, FSETLOCKING_BYCALLERFSETLOCKING_BYCALLER); | |||
| 2066 | ||||
| 2067 | manager_dump(m, f, NULL((void*)0)); | |||
| 2068 | ||||
| 2069 | r = fflush_and_check(f); | |||
| 2070 | if (r < 0) | |||
| 2071 | return r; | |||
| 2072 | ||||
| 2073 | f = safe_fclose(f); | |||
| 2074 | ||||
| 2075 | *ret = TAKE_PTR(dump)({ typeof(dump) _ptr_ = (dump); (dump) = ((void*)0); _ptr_; } ); | |||
| 2076 | ||||
| 2077 | return 0; | |||
| 2078 | } | |||
| 2079 | ||||
| 2080 | void manager_clear_jobs(Manager *m) { | |||
| 2081 | Job *j; | |||
| 2082 | ||||
| 2083 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 2083, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2084 | ||||
| 2085 | while ((j = hashmap_first(m->jobs))) | |||
| 2086 | /* No need to recurse. We're cancelling all jobs. */ | |||
| 2087 | job_finish_and_invalidate(j, JOB_CANCELED, false0, false0); | |||
| 2088 | } | |||
| 2089 | ||||
| 2090 | void manager_unwatch_pid(Manager *m, pid_t pid) { | |||
| 2091 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 2091, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2092 | ||||
| 2093 | /* First let's drop the unit keyed as "pid". */ | |||
| 2094 | (void) hashmap_remove(m->watch_pids, PID_TO_PTR(pid)); | |||
| 2095 | ||||
| 2096 | /* Then, let's also drop the array keyed by -pid. */ | |||
| 2097 | free(hashmap_remove(m->watch_pids, PID_TO_PTR(-pid))); | |||
| 2098 | } | |||
| 2099 | ||||
| 2100 | static int manager_dispatch_run_queue(sd_event_source *source, void *userdata) { | |||
| 2101 | Manager *m = userdata; | |||
| 2102 | Job *j; | |||
| 2103 | ||||
| 2104 | assert(source)do { if ((__builtin_expect(!!(!(source)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("source"), "../src/core/manager.c", 2104 , __PRETTY_FUNCTION__); } while (0); | |||
| 2105 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 2105, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2106 | ||||
| 2107 | while ((j = m->run_queue)) { | |||
| 2108 | assert(j->installed)do { if ((__builtin_expect(!!(!(j->installed)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("j->installed"), "../src/core/manager.c" , 2108, __PRETTY_FUNCTION__); } while (0); | |||
| 2109 | assert(j->in_run_queue)do { if ((__builtin_expect(!!(!(j->in_run_queue)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("j->in_run_queue"), "../src/core/manager.c" , 2109, __PRETTY_FUNCTION__); } while (0); | |||
| 2110 | ||||
| 2111 | job_run_and_invalidate(j); | |||
| 2112 | } | |||
| 2113 | ||||
| 2114 | if (m->n_running_jobs > 0) | |||
| 2115 | manager_watch_jobs_in_progress(m); | |||
| 2116 | ||||
| 2117 | if (m->n_on_console > 0) | |||
| 2118 | manager_watch_idle_pipe(m); | |||
| 2119 | ||||
| 2120 | return 1; | |||
| 2121 | } | |||
| 2122 | ||||
| 2123 | static unsigned manager_dispatch_dbus_queue(Manager *m) { | |||
| 2124 | unsigned n = 0, budget; | |||
| 2125 | Unit *u; | |||
| 2126 | Job *j; | |||
| 2127 | ||||
| 2128 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 2128, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2129 | ||||
| 2130 | /* Avoid recursion */ | |||
| 2131 | if (m->dispatching_dbus_queue) | |||
| 2132 | return 0; | |||
| 2133 | ||||
| 2134 | /* When we are reloading, let's not wait with generating signals, since we need to exit the manager as quickly | |||
| 2135 | * as we can. There's no point in throttling generation of signals in that case. */ | |||
| 2136 | if (MANAGER_IS_RELOADING(m)((m)->n_reloading > 0) || m->send_reloading_done || m->pending_reload_message) | |||
| 2137 | budget = (unsigned) -1; /* infinite budget in this case */ | |||
| 2138 | else { | |||
| 2139 | /* Anything to do at all? */ | |||
| 2140 | if (!m->dbus_unit_queue && !m->dbus_job_queue) | |||
| 2141 | return 0; | |||
| 2142 | ||||
| 2143 | /* Do we have overly many messages queued at the moment? If so, let's not enqueue more on top, let's | |||
| 2144 | * sit this cycle out, and process things in a later cycle when the queues got a bit emptier. */ | |||
| 2145 | if (manager_bus_n_queued_write(m) > MANAGER_BUS_BUSY_THRESHOLD1024LU) | |||
| 2146 | return 0; | |||
| 2147 | ||||
| 2148 | /* Only process a certain number of units/jobs per event loop iteration. Even if the bus queue wasn't | |||
| 2149 | * overly full before this call we shouldn't increase it in size too wildly in one step, and we | |||
| 2150 | * shouldn't monopolize CPU time with generating these messages. Note the difference in counting of | |||
| 2151 | * this "budget" and the "threshold" above: the "budget" is decreased only once per generated message, | |||
| 2152 | * regardless how many busses/direct connections it is enqueued on, while the "threshold" is applied to | |||
| 2153 | * each queued instance of bus message, i.e. if the same message is enqueued to five busses/direct | |||
| 2154 | * connections it will be counted five times. This difference in counting ("references" | |||
| 2155 | * vs. "instances") is primarily a result of the fact that it's easier to implement it this way, | |||
| 2156 | * however it also reflects the thinking that the "threshold" should put a limit on used queue memory, | |||
| 2157 | * i.e. space, while the "budget" should put a limit on time. Also note that the "threshold" is | |||
| 2158 | * currently chosen much higher than the "budget". */ | |||
| 2159 | budget = MANAGER_BUS_MESSAGE_BUDGET100U; | |||
| 2160 | } | |||
| 2161 | ||||
| 2162 | m->dispatching_dbus_queue = true1; | |||
| 2163 | ||||
| 2164 | while (budget != 0 && (u = m->dbus_unit_queue)) { | |||
| 2165 | ||||
| 2166 | assert(u->in_dbus_queue)do { if ((__builtin_expect(!!(!(u->in_dbus_queue)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u->in_dbus_queue"), "../src/core/manager.c" , 2166, __PRETTY_FUNCTION__); } while (0); | |||
| 2167 | ||||
| 2168 | bus_unit_send_change_signal(u); | |||
| 2169 | n++; | |||
| 2170 | ||||
| 2171 | if (budget != (unsigned) -1) | |||
| 2172 | budget--; | |||
| 2173 | } | |||
| 2174 | ||||
| 2175 | while (budget != 0 && (j = m->dbus_job_queue)) { | |||
| 2176 | assert(j->in_dbus_queue)do { if ((__builtin_expect(!!(!(j->in_dbus_queue)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("j->in_dbus_queue"), "../src/core/manager.c" , 2176, __PRETTY_FUNCTION__); } while (0); | |||
| 2177 | ||||
| 2178 | bus_job_send_change_signal(j); | |||
| 2179 | n++; | |||
| 2180 | ||||
| 2181 | if (budget != (unsigned) -1) | |||
| 2182 | budget--; | |||
| 2183 | } | |||
| 2184 | ||||
| 2185 | m->dispatching_dbus_queue = false0; | |||
| 2186 | ||||
| 2187 | if (m->send_reloading_done) { | |||
| 2188 | m->send_reloading_done = false0; | |||
| 2189 | bus_manager_send_reloading(m, false0); | |||
| 2190 | n++; | |||
| 2191 | } | |||
| 2192 | ||||
| 2193 | if (m->pending_reload_message) { | |||
| 2194 | bus_send_pending_reload_message(m); | |||
| 2195 | n++; | |||
| 2196 | } | |||
| 2197 | ||||
| 2198 | return n; | |||
| 2199 | } | |||
| 2200 | ||||
| 2201 | static int manager_dispatch_cgroups_agent_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { | |||
| 2202 | Manager *m = userdata; | |||
| 2203 | char buf[PATH_MAX4096+1]; | |||
| 2204 | ssize_t n; | |||
| 2205 | ||||
| 2206 | n = recv(fd, buf, sizeof(buf), 0); | |||
| 2207 | if (n < 0) | |||
| 2208 | return log_error_errno(errno, "Failed to read cgroups agent message: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 2208, __func__, "Failed to read cgroups agent message: %m") : -abs(_e); }); | |||
| 2209 | if (n == 0) { | |||
| 2210 | log_error("Got zero-length cgroups agent message, ignoring.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2210, __func__, "Got zero-length cgroups agent message, ignoring." ) : -abs(_e); }); | |||
| 2211 | return 0; | |||
| 2212 | } | |||
| 2213 | if ((size_t) n >= sizeof(buf)) { | |||
| 2214 | log_error("Got overly long cgroups agent message, ignoring.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2214, __func__, "Got overly long cgroups agent message, ignoring." ) : -abs(_e); }); | |||
| 2215 | return 0; | |||
| 2216 | } | |||
| 2217 | ||||
| 2218 | if (memchr(buf, 0, n)) { | |||
| 2219 | log_error("Got cgroups agent message with embedded NUL byte, ignoring.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2219, __func__, "Got cgroups agent message with embedded NUL byte, ignoring." ) : -abs(_e); }); | |||
| 2220 | return 0; | |||
| 2221 | } | |||
| 2222 | buf[n] = 0; | |||
| 2223 | ||||
| 2224 | manager_notify_cgroup_empty(m, buf); | |||
| 2225 | (void) bus_forward_agent_released(m, buf); | |||
| 2226 | ||||
| 2227 | return 0; | |||
| 2228 | } | |||
| 2229 | ||||
| 2230 | static void manager_invoke_notify_message( | |||
| 2231 | Manager *m, | |||
| 2232 | Unit *u, | |||
| 2233 | const struct ucred *ucred, | |||
| 2234 | const char *buf, | |||
| 2235 | FDSet *fds) { | |||
| 2236 | ||||
| 2237 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 2237, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2238 | assert(u)do { if ((__builtin_expect(!!(!(u)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u"), "../src/core/manager.c", 2238, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2239 | assert(ucred)do { if ((__builtin_expect(!!(!(ucred)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ucred"), "../src/core/manager.c", 2239, __PRETTY_FUNCTION__); } while (0); | |||
| 2240 | assert(buf)do { if ((__builtin_expect(!!(!(buf)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("buf"), "../src/core/manager.c", 2240, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2241 | ||||
| 2242 | if (u->notifygen == m->notifygen) /* Already invoked on this same unit in this same iteration? */ | |||
| 2243 | return; | |||
| 2244 | u->notifygen = m->notifygen; | |||
| 2245 | ||||
| 2246 | if (UNIT_VTABLE(u)unit_vtable[(u)->type]->notify_message) { | |||
| 2247 | _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **tags = NULL((void*)0); | |||
| 2248 | ||||
| 2249 | tags = strv_split(buf, NEWLINE"\n\r"); | |||
| 2250 | if (!tags) { | |||
| 2251 | log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/core/manager.c", 2251 , __func__); | |||
| 2252 | return; | |||
| 2253 | } | |||
| 2254 | ||||
| 2255 | UNIT_VTABLE(u)unit_vtable[(u)->type]->notify_message(u, ucred, tags, fds); | |||
| 2256 | ||||
| 2257 | } else if (DEBUG_LOGGING(__builtin_expect(!!(log_get_max_level_realm(LOG_REALM_SYSTEMD ) >= 7),0))) { | |||
| 2258 | _cleanup_free___attribute__((cleanup(freep))) char *x = NULL((void*)0), *y = NULL((void*)0); | |||
| 2259 | ||||
| 2260 | x = ellipsize(buf, 20, 90); | |||
| 2261 | if (x) | |||
| 2262 | y = cescape(x); | |||
| 2263 | ||||
| 2264 | log_unit_debug(u, "Got notification message \"%s\", ignoring.", strnull(y))({ const Unit *_u = (u); _u ? log_object_internal(7, 0, "../src/core/manager.c" , 2264, __func__, _u->manager->unit_log_field, _u->id , _u->manager->invocation_log_field, _u->invocation_id_string , "Got notification message \"%s\", ignoring.", strnull(y)) : log_internal_realm(((LOG_REALM_SYSTEMD) << 10 | ((7))) , 0, "../src/core/manager.c", 2264, __func__, "Got notification message \"%s\", ignoring." , strnull(y)); }); | |||
| 2265 | } | |||
| 2266 | } | |||
| 2267 | ||||
| 2268 | static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { | |||
| 2269 | ||||
| 2270 | _cleanup_fdset_free___attribute__((cleanup(fdset_freep))) FDSet *fds = NULL((void*)0); | |||
| 2271 | Manager *m = userdata; | |||
| 2272 | char buf[NOTIFY_BUFFER_MAX4096+1]; | |||
| 2273 | struct iovec iovec = { | |||
| 2274 | .iov_base = buf, | |||
| 2275 | .iov_len = sizeof(buf)-1, | |||
| 2276 | }; | |||
| 2277 | union { | |||
| 2278 | struct cmsghdr cmsghdr; | |||
| 2279 | uint8_t buf[CMSG_SPACE(sizeof(struct ucred))((((sizeof(struct ucred)) + sizeof (size_t) - 1) & (size_t ) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1))) + | |||
| 2280 | CMSG_SPACE(sizeof(int) * NOTIFY_FD_MAX)((((sizeof(int) * 768) + sizeof (size_t) - 1) & (size_t) ~ (sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)))]; | |||
| 2281 | } control = {}; | |||
| 2282 | struct msghdr msghdr = { | |||
| 2283 | .msg_iov = &iovec, | |||
| 2284 | .msg_iovlen = 1, | |||
| 2285 | .msg_control = &control, | |||
| 2286 | .msg_controllen = sizeof(control), | |||
| 2287 | }; | |||
| 2288 | ||||
| 2289 | struct cmsghdr *cmsg; | |||
| 2290 | struct ucred *ucred = NULL((void*)0); | |||
| 2291 | _cleanup_free___attribute__((cleanup(freep))) Unit **array_copy = NULL((void*)0); | |||
| 2292 | Unit *u1, *u2, **array; | |||
| 2293 | int r, *fd_array = NULL((void*)0); | |||
| 2294 | size_t n_fds = 0; | |||
| 2295 | bool_Bool found = false0; | |||
| 2296 | ssize_t n; | |||
| 2297 | ||||
| 2298 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 2298, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2299 | assert(m->notify_fd == fd)do { if ((__builtin_expect(!!(!(m->notify_fd == fd)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m->notify_fd == fd"), "../src/core/manager.c" , 2299, __PRETTY_FUNCTION__); } while (0); | |||
| 2300 | ||||
| 2301 | if (revents != EPOLLINEPOLLIN) { | |||
| 2302 | log_warning("Got unexpected poll event for notify fd.")({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2302, __func__, "Got unexpected poll event for notify fd." ) : -abs(_e); }); | |||
| 2303 | return 0; | |||
| 2304 | } | |||
| 2305 | ||||
| 2306 | n = recvmsg(m->notify_fd, &msghdr, MSG_DONTWAITMSG_DONTWAIT|MSG_CMSG_CLOEXECMSG_CMSG_CLOEXEC|MSG_TRUNCMSG_TRUNC); | |||
| 2307 | if (n < 0) { | |||
| 2308 | if (IN_SET(errno, EAGAIN, EINTR)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){11, 4})/sizeof(int)]; switch((*__errno_location ())) { case 11: case 4: _found = 1; break; default: break; } _found; })) | |||
| 2309 | return 0; /* Spurious wakeup, try again */ | |||
| 2310 | ||||
| 2311 | /* If this is any other, real error, then let's stop processing this socket. This of course means we | |||
| 2312 | * won't take notification messages anymore, but that's still better than busy looping around this: | |||
| 2313 | * being woken up over and over again but being unable to actually read the message off the socket. */ | |||
| 2314 | return log_error_errno(errno, "Failed to receive notification message: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 2314, __func__, "Failed to receive notification message: %m") : -abs(_e); }); | |||
| 2315 | } | |||
| 2316 | ||||
| 2317 | CMSG_FOREACH(cmsg, &msghdr)for ((cmsg) = ((size_t) (&msghdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&msghdr)-> msg_control : (struct cmsghdr *) 0); (cmsg); (cmsg) = __cmsg_nxthdr ((&msghdr), (cmsg))) { | |||
| 2318 | if (cmsg->cmsg_level == SOL_SOCKET1 && cmsg->cmsg_type == SCM_RIGHTSSCM_RIGHTS) { | |||
| 2319 | ||||
| 2320 | fd_array = (int*) CMSG_DATA(cmsg)((cmsg)->__cmsg_data); | |||
| 2321 | n_fds = (cmsg->cmsg_len - CMSG_LEN(0)((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t ) ~(sizeof (size_t) - 1)) + (0))) / sizeof(int); | |||
| 2322 | ||||
| 2323 | } else if (cmsg->cmsg_level == SOL_SOCKET1 && | |||
| 2324 | cmsg->cmsg_type == SCM_CREDENTIALSSCM_CREDENTIALS && | |||
| 2325 | cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t ) ~(sizeof (size_t) - 1)) + (sizeof(struct ucred)))) { | |||
| 2326 | ||||
| 2327 | ucred = (struct ucred*) CMSG_DATA(cmsg)((cmsg)->__cmsg_data); | |||
| 2328 | } | |||
| 2329 | } | |||
| 2330 | ||||
| 2331 | if (n_fds > 0) { | |||
| 2332 | assert(fd_array)do { if ((__builtin_expect(!!(!(fd_array)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("fd_array"), "../src/core/manager.c", 2332 , __PRETTY_FUNCTION__); } while (0); | |||
| 2333 | ||||
| 2334 | r = fdset_new_array(&fds, fd_array, n_fds); | |||
| 2335 | if (r < 0) { | |||
| 2336 | close_many(fd_array, n_fds); | |||
| 2337 | log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/core/manager.c", 2337 , __func__); | |||
| 2338 | return 0; | |||
| 2339 | } | |||
| 2340 | } | |||
| 2341 | ||||
| 2342 | if (!ucred || !pid_is_valid(ucred->pid)) { | |||
| 2343 | log_warning("Received notify message without valid credentials. Ignoring.")({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2343, __func__, "Received notify message without valid credentials. Ignoring." ) : -abs(_e); }); | |||
| 2344 | return 0; | |||
| 2345 | } | |||
| 2346 | ||||
| 2347 | if ((size_t) n >= sizeof(buf) || (msghdr.msg_flags & MSG_TRUNCMSG_TRUNC)) { | |||
| 2348 | log_warning("Received notify message exceeded maximum size. Ignoring.")({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2348, __func__, "Received notify message exceeded maximum size. Ignoring." ) : -abs(_e); }); | |||
| 2349 | return 0; | |||
| 2350 | } | |||
| 2351 | ||||
| 2352 | /* As extra safety check, let's make sure the string we get doesn't contain embedded NUL bytes. We permit one | |||
| 2353 | * trailing NUL byte in the message, but don't expect it. */ | |||
| 2354 | if (n > 1 && memchr(buf, 0, n-1)) { | |||
| 2355 | log_warning("Received notify message with embedded NUL bytes. Ignoring.")({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2355, __func__, "Received notify message with embedded NUL bytes. Ignoring." ) : -abs(_e); }); | |||
| 2356 | return 0; | |||
| 2357 | } | |||
| 2358 | ||||
| 2359 | /* Make sure it's NUL-terminated. */ | |||
| 2360 | buf[n] = 0; | |||
| 2361 | ||||
| 2362 | /* Increase the generation counter used for filtering out duplicate unit invocations. */ | |||
| 2363 | m->notifygen++; | |||
| 2364 | ||||
| 2365 | /* Notify every unit that might be interested, which might be multiple. */ | |||
| 2366 | u1 = manager_get_unit_by_pid_cgroup(m, ucred->pid); | |||
| 2367 | u2 = hashmap_get(m->watch_pids, PID_TO_PTR(ucred->pid)); | |||
| 2368 | array = hashmap_get(m->watch_pids, PID_TO_PTR(-ucred->pid)); | |||
| 2369 | if (array) { | |||
| 2370 | size_t k = 0; | |||
| 2371 | ||||
| 2372 | while (array[k]) | |||
| 2373 | k++; | |||
| 2374 | ||||
| 2375 | array_copy = newdup(Unit*, array, k+1)((Unit**) memdup_multiply(array, sizeof(Unit*), (k+1))); | |||
| 2376 | if (!array_copy) | |||
| 2377 | log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/core/manager.c", 2377 , __func__); | |||
| 2378 | } | |||
| 2379 | /* And now invoke the per-unit callbacks. Note that manager_invoke_notify_message() will handle duplicate units | |||
| 2380 | * make sure we only invoke each unit's handler once. */ | |||
| 2381 | if (u1) { | |||
| 2382 | manager_invoke_notify_message(m, u1, ucred, buf, fds); | |||
| 2383 | found = true1; | |||
| 2384 | } | |||
| 2385 | if (u2) { | |||
| 2386 | manager_invoke_notify_message(m, u2, ucred, buf, fds); | |||
| 2387 | found = true1; | |||
| 2388 | } | |||
| 2389 | if (array_copy) | |||
| 2390 | for (size_t i = 0; array_copy[i]; i++) { | |||
| 2391 | manager_invoke_notify_message(m, array_copy[i], ucred, buf, fds); | |||
| 2392 | found = true1; | |||
| 2393 | } | |||
| 2394 | ||||
| 2395 | if (!found) | |||
| 2396 | log_warning("Cannot find unit for notify message of PID "PID_FMT", ignoring.", ucred->pid)({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2396, __func__, "Cannot find unit for notify message of PID " "%" "i"", ignoring.", ucred->pid) : -abs(_e); }); | |||
| 2397 | ||||
| 2398 | if (fdset_size(fds) > 0) | |||
| 2399 | log_warning("Got extra auxiliary fds with notification message, closing them.")({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2399, __func__, "Got extra auxiliary fds with notification message, closing them." ) : -abs(_e); }); | |||
| 2400 | ||||
| 2401 | return 0; | |||
| 2402 | } | |||
| 2403 | ||||
| 2404 | static void manager_invoke_sigchld_event( | |||
| 2405 | Manager *m, | |||
| 2406 | Unit *u, | |||
| 2407 | const siginfo_t *si) { | |||
| 2408 | ||||
| 2409 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 2409, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2410 | assert(u)do { if ((__builtin_expect(!!(!(u)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u"), "../src/core/manager.c", 2410, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2411 | assert(si)do { if ((__builtin_expect(!!(!(si)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("si"), "../src/core/manager.c", 2411, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2412 | ||||
| 2413 | /* Already invoked the handler of this unit in this iteration? Then don't process this again */ | |||
| 2414 | if (u->sigchldgen == m->sigchldgen) | |||
| 2415 | return; | |||
| 2416 | u->sigchldgen = m->sigchldgen; | |||
| 2417 | ||||
| 2418 | log_unit_debug(u, "Child "PID_FMT" belongs to %s.", si->si_pid, u->id)({ const Unit *_u = (u); _u ? log_object_internal(7, 0, "../src/core/manager.c" , 2418, __func__, _u->manager->unit_log_field, _u->id , _u->manager->invocation_log_field, _u->invocation_id_string , "Child ""%" "i"" belongs to %s.", si->_sifields._kill.si_pid , u->id) : log_internal_realm(((LOG_REALM_SYSTEMD) << 10 | ((7))), 0, "../src/core/manager.c", 2418, __func__, "Child " "%" "i"" belongs to %s.", si->_sifields._kill.si_pid, u-> id); }); | |||
| 2419 | unit_unwatch_pid(u, si->si_pid_sifields._kill.si_pid); | |||
| 2420 | ||||
| 2421 | if (UNIT_VTABLE(u)unit_vtable[(u)->type]->sigchld_event) | |||
| 2422 | UNIT_VTABLE(u)unit_vtable[(u)->type]->sigchld_event(u, si->si_pid_sifields._kill.si_pid, si->si_code, si->si_status_sifields._sigchld.si_status); | |||
| 2423 | } | |||
| 2424 | ||||
| 2425 | static int manager_dispatch_sigchld(sd_event_source *source, void *userdata) { | |||
| 2426 | Manager *m = userdata; | |||
| 2427 | siginfo_t si = {}; | |||
| 2428 | int r; | |||
| 2429 | ||||
| 2430 | assert(source)do { if ((__builtin_expect(!!(!(source)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("source"), "../src/core/manager.c", 2430 , __PRETTY_FUNCTION__); } while (0); | |||
| 2431 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 2431, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2432 | ||||
| 2433 | /* First we call waitd() for a PID and do not reap the zombie. That way we can still access /proc/$PID for it | |||
| 2434 | * while it is a zombie. */ | |||
| 2435 | ||||
| 2436 | if (waitid(P_ALL, 0, &si, WEXITED4|WNOHANG1|WNOWAIT0x01000000) < 0) { | |||
| 2437 | ||||
| 2438 | if (errno(*__errno_location ()) != ECHILD10) | |||
| 2439 | log_error_errno(errno, "Failed to peek for child with waitid(), ignoring: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 2439, __func__, "Failed to peek for child with waitid(), ignoring: %m") : -abs (_e); }); | |||
| 2440 | ||||
| 2441 | goto turn_off; | |||
| 2442 | } | |||
| 2443 | ||||
| 2444 | if (si.si_pid_sifields._kill.si_pid <= 0) | |||
| 2445 | goto turn_off; | |||
| 2446 | ||||
| 2447 | if (IN_SET(si.si_code, CLD_EXITED, CLD_KILLED, CLD_DUMPED)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){CLD_EXITED, CLD_KILLED, CLD_DUMPED})/sizeof (int)]; switch(si.si_code) { case CLD_EXITED: case CLD_KILLED : case CLD_DUMPED: _found = 1; break; default: break; } _found ; })) { | |||
| 2448 | _cleanup_free___attribute__((cleanup(freep))) Unit **array_copy = NULL((void*)0); | |||
| 2449 | _cleanup_free___attribute__((cleanup(freep))) char *name = NULL((void*)0); | |||
| 2450 | Unit *u1, *u2, **array; | |||
| 2451 | ||||
| 2452 | (void) get_process_comm(si.si_pid_sifields._kill.si_pid, &name); | |||
| 2453 | ||||
| 2454 | log_debug("Child "PID_FMT" (%s) died (code=%s, status=%i/%s)",({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2460, __func__, "Child ""%" "i"" (%s) died (code=%s, status=%i/%s)" , si._sifields._kill.si_pid, strna(name), sigchld_code_to_string (si.si_code), si._sifields._sigchld.si_status, strna(si.si_code == CLD_EXITED ? exit_status_to_string(si._sifields._sigchld. si_status, EXIT_STATUS_FULL) : signal_to_string(si._sifields. _sigchld.si_status))) : -abs(_e); }) | |||
| 2455 | si.si_pid, strna(name),({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2460, __func__, "Child ""%" "i"" (%s) died (code=%s, status=%i/%s)" , si._sifields._kill.si_pid, strna(name), sigchld_code_to_string (si.si_code), si._sifields._sigchld.si_status, strna(si.si_code == CLD_EXITED ? exit_status_to_string(si._sifields._sigchld. si_status, EXIT_STATUS_FULL) : signal_to_string(si._sifields. _sigchld.si_status))) : -abs(_e); }) | |||
| 2456 | sigchld_code_to_string(si.si_code),({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2460, __func__, "Child ""%" "i"" (%s) died (code=%s, status=%i/%s)" , si._sifields._kill.si_pid, strna(name), sigchld_code_to_string (si.si_code), si._sifields._sigchld.si_status, strna(si.si_code == CLD_EXITED ? exit_status_to_string(si._sifields._sigchld. si_status, EXIT_STATUS_FULL) : signal_to_string(si._sifields. _sigchld.si_status))) : -abs(_e); }) | |||
| 2457 | si.si_status,({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2460, __func__, "Child ""%" "i"" (%s) died (code=%s, status=%i/%s)" , si._sifields._kill.si_pid, strna(name), sigchld_code_to_string (si.si_code), si._sifields._sigchld.si_status, strna(si.si_code == CLD_EXITED ? exit_status_to_string(si._sifields._sigchld. si_status, EXIT_STATUS_FULL) : signal_to_string(si._sifields. _sigchld.si_status))) : -abs(_e); }) | |||
| 2458 | strna(si.si_code == CLD_EXITED({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2460, __func__, "Child ""%" "i"" (%s) died (code=%s, status=%i/%s)" , si._sifields._kill.si_pid, strna(name), sigchld_code_to_string (si.si_code), si._sifields._sigchld.si_status, strna(si.si_code == CLD_EXITED ? exit_status_to_string(si._sifields._sigchld. si_status, EXIT_STATUS_FULL) : signal_to_string(si._sifields. _sigchld.si_status))) : -abs(_e); }) | |||
| 2459 | ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2460, __func__, "Child ""%" "i"" (%s) died (code=%s, status=%i/%s)" , si._sifields._kill.si_pid, strna(name), sigchld_code_to_string (si.si_code), si._sifields._sigchld.si_status, strna(si.si_code == CLD_EXITED ? exit_status_to_string(si._sifields._sigchld. si_status, EXIT_STATUS_FULL) : signal_to_string(si._sifields. _sigchld.si_status))) : -abs(_e); }) | |||
| 2460 | : signal_to_string(si.si_status)))({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2460, __func__, "Child ""%" "i"" (%s) died (code=%s, status=%i/%s)" , si._sifields._kill.si_pid, strna(name), sigchld_code_to_string (si.si_code), si._sifields._sigchld.si_status, strna(si.si_code == CLD_EXITED ? exit_status_to_string(si._sifields._sigchld. si_status, EXIT_STATUS_FULL) : signal_to_string(si._sifields. _sigchld.si_status))) : -abs(_e); }); | |||
| 2461 | ||||
| 2462 | /* Increase the generation counter used for filtering out duplicate unit invocations */ | |||
| 2463 | m->sigchldgen++; | |||
| 2464 | ||||
| 2465 | /* And now figure out the unit this belongs to, it might be multiple... */ | |||
| 2466 | u1 = manager_get_unit_by_pid_cgroup(m, si.si_pid_sifields._kill.si_pid); | |||
| 2467 | u2 = hashmap_get(m->watch_pids, PID_TO_PTR(si.si_pid_sifields._kill.si_pid)); | |||
| 2468 | array = hashmap_get(m->watch_pids, PID_TO_PTR(-si.si_pid_sifields._kill.si_pid)); | |||
| 2469 | if (array) { | |||
| 2470 | size_t n = 0; | |||
| 2471 | ||||
| 2472 | /* Cound how many entries the array has */ | |||
| 2473 | while (array[n]) | |||
| 2474 | n++; | |||
| 2475 | ||||
| 2476 | /* Make a copy of the array so that we don't trip up on the array changing beneath us */ | |||
| 2477 | array_copy = newdup(Unit*, array, n+1)((Unit**) memdup_multiply(array, sizeof(Unit*), (n+1))); | |||
| 2478 | if (!array_copy) | |||
| 2479 | log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/core/manager.c", 2479 , __func__); | |||
| 2480 | } | |||
| 2481 | ||||
| 2482 | /* Finally, execute them all. Note that u1, u2 and the array might contain duplicates, but | |||
| 2483 | * that's fine, manager_invoke_sigchld_event() will ensure we only invoke the handlers once for | |||
| 2484 | * each iteration. */ | |||
| 2485 | if (u1) | |||
| 2486 | manager_invoke_sigchld_event(m, u1, &si); | |||
| 2487 | if (u2) | |||
| 2488 | manager_invoke_sigchld_event(m, u2, &si); | |||
| 2489 | if (array_copy) | |||
| 2490 | for (size_t i = 0; array_copy[i]; i++) | |||
| 2491 | manager_invoke_sigchld_event(m, array_copy[i], &si); | |||
| 2492 | } | |||
| 2493 | ||||
| 2494 | /* And now, we actually reap the zombie. */ | |||
| 2495 | if (waitid(P_PID, si.si_pid_sifields._kill.si_pid, &si, WEXITED4) < 0) { | |||
| 2496 | log_error_errno(errno, "Failed to dequeue child, ignoring: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 2496, __func__, "Failed to dequeue child, ignoring: %m") : -abs(_e); }); | |||
| 2497 | return 0; | |||
| 2498 | } | |||
| 2499 | ||||
| 2500 | return 0; | |||
| 2501 | ||||
| 2502 | turn_off: | |||
| 2503 | /* All children processed for now, turn off event source */ | |||
| 2504 | ||||
| 2505 | r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_OFF); | |||
| 2506 | if (r < 0) | |||
| 2507 | return log_error_errno(r, "Failed to disable SIGCHLD event source: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2507, __func__, "Failed to disable SIGCHLD event source: %m" ) : -abs(_e); }); | |||
| 2508 | ||||
| 2509 | return 0; | |||
| 2510 | } | |||
| 2511 | ||||
| 2512 | static void manager_start_target(Manager *m, const char *name, JobMode mode) { | |||
| 2513 | _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error error = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0}); | |||
| 2514 | int r; | |||
| 2515 | ||||
| 2516 | log_debug("Activating special unit %s", name)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2516, __func__, "Activating special unit %s" , name) : -abs(_e); }); | |||
| 2517 | ||||
| 2518 | r = manager_add_job_by_name(m, JOB_START, name, mode, NULL((void*)0), &error, NULL((void*)0)); | |||
| 2519 | if (r < 0) | |||
| 2520 | log_error("Failed to enqueue %s job: %s", name, bus_error_message(&error, r))({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2520, __func__, "Failed to enqueue %s job: %s" , name, bus_error_message(&error, r)) : -abs(_e); }); | |||
| 2521 | } | |||
| 2522 | ||||
| 2523 | static void manager_handle_ctrl_alt_del(Manager *m) { | |||
| 2524 | /* If the user presses C-A-D more than | |||
| 2525 | * 7 times within 2s, we reboot/shutdown immediately, | |||
| 2526 | * unless it was disabled in system.conf */ | |||
| 2527 | ||||
| 2528 | if (ratelimit_below(&m->ctrl_alt_del_ratelimit) || m->cad_burst_action == EMERGENCY_ACTION_NONE) | |||
| 2529 | manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET"ctrl-alt-del.target", JOB_REPLACE_IRREVERSIBLY); | |||
| 2530 | else | |||
| 2531 | emergency_action(m, m->cad_burst_action, NULL((void*)0), | |||
| 2532 | "Ctrl-Alt-Del was pressed more than 7 times within 2s"); | |||
| 2533 | } | |||
| 2534 | ||||
| 2535 | static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { | |||
| 2536 | Manager *m = userdata; | |||
| 2537 | ssize_t n; | |||
| 2538 | struct signalfd_siginfo sfsi; | |||
| 2539 | int r; | |||
| 2540 | ||||
| 2541 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 2541, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2542 | assert(m->signal_fd == fd)do { if ((__builtin_expect(!!(!(m->signal_fd == fd)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m->signal_fd == fd"), "../src/core/manager.c" , 2542, __PRETTY_FUNCTION__); } while (0); | |||
| 2543 | ||||
| 2544 | if (revents != EPOLLINEPOLLIN) { | |||
| 2545 | log_warning("Got unexpected events from signal file descriptor.")({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2545, __func__, "Got unexpected events from signal file descriptor." ) : -abs(_e); }); | |||
| 2546 | return 0; | |||
| 2547 | } | |||
| 2548 | ||||
| 2549 | n = read(m->signal_fd, &sfsi, sizeof(sfsi)); | |||
| 2550 | if (n != sizeof(sfsi)) { | |||
| 2551 | if (n >= 0) { | |||
| 2552 | log_warning("Truncated read from signal fd (%zu bytes), ignoring!", n)({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2552, __func__, "Truncated read from signal fd (%zu bytes), ignoring!" , n) : -abs(_e); }); | |||
| 2553 | return 0; | |||
| 2554 | } | |||
| 2555 | ||||
| 2556 | if (IN_SET(errno, EINTR, EAGAIN)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){4, 11})/sizeof(int)]; switch((*__errno_location ())) { case 4: case 11: _found = 1; break; default: break; } _found; })) | |||
| 2557 | return 0; | |||
| 2558 | ||||
| 2559 | /* We return an error here, which will kill this handler, | |||
| 2560 | * to avoid a busy loop on read error. */ | |||
| 2561 | return log_error_errno(errno, "Reading from signal fd failed: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 2561, __func__, "Reading from signal fd failed: %m") : -abs(_e); }); | |||
| 2562 | } | |||
| 2563 | ||||
| 2564 | log_received_signal(sfsi.ssi_signo == SIGCHLD17 || | |||
| 2565 | (sfsi.ssi_signo == SIGTERM15 && MANAGER_IS_USER(m)((m)->unit_file_scope != UNIT_FILE_SYSTEM)) | |||
| 2566 | ? LOG_DEBUG7 : LOG_INFO6, | |||
| 2567 | &sfsi); | |||
| 2568 | ||||
| 2569 | switch (sfsi.ssi_signo) { | |||
| 2570 | ||||
| 2571 | case SIGCHLD17: | |||
| 2572 | r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_ON); | |||
| 2573 | if (r < 0) | |||
| 2574 | log_warning_errno(r, "Failed to enable SIGCHLD event source, ignoring: %m")({ int _level = ((4)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2574, __func__, "Failed to enable SIGCHLD event source, ignoring: %m" ) : -abs(_e); }); | |||
| 2575 | ||||
| 2576 | break; | |||
| 2577 | ||||
| 2578 | case SIGTERM15: | |||
| 2579 | if (MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) { | |||
| 2580 | /* This is for compatibility with the original sysvinit */ | |||
| 2581 | r = verify_run_space_and_log("Refusing to reexecute"); | |||
| 2582 | if (r >= 0) | |||
| 2583 | m->exit_code = MANAGER_REEXECUTE; | |||
| 2584 | break; | |||
| 2585 | } | |||
| 2586 | ||||
| 2587 | _fallthrough_; | |||
| 2588 | case SIGINT2: | |||
| 2589 | if (MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) | |||
| 2590 | manager_handle_ctrl_alt_del(m); | |||
| 2591 | else | |||
| 2592 | manager_start_target(m, SPECIAL_EXIT_TARGET"exit.target", | |||
| 2593 | JOB_REPLACE_IRREVERSIBLY); | |||
| 2594 | break; | |||
| 2595 | ||||
| 2596 | case SIGWINCH28: | |||
| 2597 | /* This is a nop on non-init */ | |||
| 2598 | if (MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) | |||
| 2599 | manager_start_target(m, SPECIAL_KBREQUEST_TARGET"kbrequest.target", JOB_REPLACE); | |||
| 2600 | ||||
| 2601 | break; | |||
| 2602 | ||||
| 2603 | case SIGPWR30: | |||
| 2604 | /* This is a nop on non-init */ | |||
| 2605 | if (MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) | |||
| 2606 | manager_start_target(m, SPECIAL_SIGPWR_TARGET"sigpwr.target", JOB_REPLACE); | |||
| 2607 | ||||
| 2608 | break; | |||
| 2609 | ||||
| 2610 | case SIGUSR110: | |||
| 2611 | if (manager_dbus_is_running(m, false0)) { | |||
| 2612 | log_info("Trying to reconnect to bus...")({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2612, __func__, "Trying to reconnect to bus..." ) : -abs(_e); }); | |||
| 2613 | ||||
| 2614 | (void) bus_init_api(m); | |||
| 2615 | ||||
| 2616 | if (MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) | |||
| 2617 | (void) bus_init_system(m); | |||
| 2618 | } else { | |||
| 2619 | log_info("Starting D-Bus service...")({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2619, __func__, "Starting D-Bus service..." ) : -abs(_e); }); | |||
| 2620 | manager_start_target(m, SPECIAL_DBUS_SERVICE"dbus.service", JOB_REPLACE); | |||
| 2621 | } | |||
| 2622 | ||||
| 2623 | break; | |||
| 2624 | ||||
| 2625 | case SIGUSR212: { | |||
| 2626 | _cleanup_free___attribute__((cleanup(freep))) char *dump = NULL((void*)0); | |||
| 2627 | ||||
| 2628 | r = manager_get_dump_string(m, &dump); | |||
| 2629 | if (r < 0) { | |||
| 2630 | log_warning_errno(errno, "Failed to acquire manager dump: %m")({ int _level = ((4)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 2630, __func__, "Failed to acquire manager dump: %m") : -abs(_e); }); | |||
| 2631 | break; | |||
| 2632 | } | |||
| 2633 | ||||
| 2634 | log_dump(LOG_INFO, dump)log_dump_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0, "../src/core/manager.c", 2634, __func__, dump); | |||
| 2635 | break; | |||
| 2636 | } | |||
| 2637 | ||||
| 2638 | case SIGHUP1: | |||
| 2639 | r = verify_run_space_and_log("Refusing to reload"); | |||
| 2640 | if (r >= 0) | |||
| 2641 | m->exit_code = MANAGER_RELOAD; | |||
| 2642 | break; | |||
| 2643 | ||||
| 2644 | default: { | |||
| 2645 | ||||
| 2646 | /* Starting SIGRTMIN+0 */ | |||
| 2647 | static const struct { | |||
| 2648 | const char *target; | |||
| 2649 | JobMode mode; | |||
| 2650 | } target_table[] = { | |||
| 2651 | [0] = { SPECIAL_DEFAULT_TARGET"default.target", JOB_ISOLATE }, | |||
| 2652 | [1] = { SPECIAL_RESCUE_TARGET"rescue.target", JOB_ISOLATE }, | |||
| 2653 | [2] = { SPECIAL_EMERGENCY_TARGET"emergency.target", JOB_ISOLATE }, | |||
| 2654 | [3] = { SPECIAL_HALT_TARGET"halt.target", JOB_REPLACE_IRREVERSIBLY }, | |||
| 2655 | [4] = { SPECIAL_POWEROFF_TARGET"poweroff.target", JOB_REPLACE_IRREVERSIBLY }, | |||
| 2656 | [5] = { SPECIAL_REBOOT_TARGET"reboot.target", JOB_REPLACE_IRREVERSIBLY }, | |||
| 2657 | [6] = { SPECIAL_KEXEC_TARGET"kexec.target", JOB_REPLACE_IRREVERSIBLY }, | |||
| 2658 | }; | |||
| 2659 | ||||
| 2660 | /* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */ | |||
| 2661 | static const ManagerExitCode code_table[] = { | |||
| 2662 | [0] = MANAGER_HALT, | |||
| 2663 | [1] = MANAGER_POWEROFF, | |||
| 2664 | [2] = MANAGER_REBOOT, | |||
| 2665 | [3] = MANAGER_KEXEC, | |||
| 2666 | }; | |||
| 2667 | ||||
| 2668 | if ((int) sfsi.ssi_signo >= SIGRTMIN(__libc_current_sigrtmin ())+0 && | |||
| 2669 | (int) sfsi.ssi_signo < SIGRTMIN(__libc_current_sigrtmin ())+(int) ELEMENTSOF(target_table)__extension__ (__builtin_choose_expr( !__builtin_types_compatible_p (typeof(target_table), typeof(&*(target_table))), sizeof( target_table)/sizeof((target_table)[0]), ((void)0)))) { | |||
| 2670 | int idx = (int) sfsi.ssi_signo - SIGRTMIN(__libc_current_sigrtmin ()); | |||
| 2671 | manager_start_target(m, target_table[idx].target, | |||
| 2672 | target_table[idx].mode); | |||
| 2673 | break; | |||
| 2674 | } | |||
| 2675 | ||||
| 2676 | if ((int) sfsi.ssi_signo >= SIGRTMIN(__libc_current_sigrtmin ())+13 && | |||
| 2677 | (int) sfsi.ssi_signo < SIGRTMIN(__libc_current_sigrtmin ())+13+(int) ELEMENTSOF(code_table)__extension__ (__builtin_choose_expr( !__builtin_types_compatible_p (typeof(code_table), typeof(&*(code_table))), sizeof(code_table )/sizeof((code_table)[0]), ((void)0)))) { | |||
| 2678 | m->exit_code = code_table[sfsi.ssi_signo - SIGRTMIN(__libc_current_sigrtmin ()) - 13]; | |||
| 2679 | break; | |||
| 2680 | } | |||
| 2681 | ||||
| 2682 | switch (sfsi.ssi_signo - SIGRTMIN(__libc_current_sigrtmin ())) { | |||
| 2683 | ||||
| 2684 | case 20: | |||
| 2685 | manager_set_show_status(m, SHOW_STATUS_YES); | |||
| 2686 | break; | |||
| 2687 | ||||
| 2688 | case 21: | |||
| 2689 | manager_set_show_status(m, SHOW_STATUS_NO); | |||
| 2690 | break; | |||
| 2691 | ||||
| 2692 | case 22: | |||
| 2693 | manager_override_log_level(m, LOG_DEBUG7); | |||
| 2694 | break; | |||
| 2695 | ||||
| 2696 | case 23: | |||
| 2697 | manager_restore_original_log_level(m); | |||
| 2698 | break; | |||
| 2699 | ||||
| 2700 | case 24: | |||
| 2701 | if (MANAGER_IS_USER(m)((m)->unit_file_scope != UNIT_FILE_SYSTEM)) { | |||
| 2702 | m->exit_code = MANAGER_EXIT; | |||
| 2703 | return 0; | |||
| 2704 | } | |||
| 2705 | ||||
| 2706 | /* This is a nop on init */ | |||
| 2707 | break; | |||
| 2708 | ||||
| 2709 | case 26: | |||
| 2710 | case 29: /* compatibility: used to be mapped to LOG_TARGET_SYSLOG_OR_KMSG */ | |||
| 2711 | manager_restore_original_log_target(m); | |||
| 2712 | break; | |||
| 2713 | ||||
| 2714 | case 27: | |||
| 2715 | manager_override_log_target(m, LOG_TARGET_CONSOLE); | |||
| 2716 | break; | |||
| 2717 | ||||
| 2718 | case 28: | |||
| 2719 | manager_override_log_target(m, LOG_TARGET_KMSG); | |||
| 2720 | break; | |||
| 2721 | ||||
| 2722 | default: | |||
| 2723 | log_warning("Got unhandled signal <%s>.", signal_to_string(sfsi.ssi_signo))({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2723, __func__, "Got unhandled signal <%s>." , signal_to_string(sfsi.ssi_signo)) : -abs(_e); }); | |||
| 2724 | } | |||
| 2725 | }} | |||
| 2726 | ||||
| 2727 | return 0; | |||
| 2728 | } | |||
| 2729 | ||||
| 2730 | static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { | |||
| 2731 | Manager *m = userdata; | |||
| 2732 | Iterator i; | |||
| 2733 | Unit *u; | |||
| 2734 | ||||
| 2735 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 2735, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2736 | assert(m->time_change_fd == fd)do { if ((__builtin_expect(!!(!(m->time_change_fd == fd)), 0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("m->time_change_fd == fd" ), "../src/core/manager.c", 2736, __PRETTY_FUNCTION__); } while (0); | |||
| 2737 | ||||
| 2738 | log_struct(LOG_DEBUG,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (7)), 0 , "../src/core/manager.c", 2740, __func__, "MESSAGE_ID=" "c7" "a7" "87" "07" "9b" "35" "4e" "aa" "a9" "e7" "7b" "37" "18" "93" "cd" "27", "MESSAGE=" "Time has been changed", ((void*)0)) | |||
| 2739 | "MESSAGE_ID=" SD_MESSAGE_TIME_CHANGE_STR,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (7)), 0 , "../src/core/manager.c", 2740, __func__, "MESSAGE_ID=" "c7" "a7" "87" "07" "9b" "35" "4e" "aa" "a9" "e7" "7b" "37" "18" "93" "cd" "27", "MESSAGE=" "Time has been changed", ((void*)0)) | |||
| 2740 | LOG_MESSAGE("Time has been changed"))log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (7)), 0 , "../src/core/manager.c", 2740, __func__, "MESSAGE_ID=" "c7" "a7" "87" "07" "9b" "35" "4e" "aa" "a9" "e7" "7b" "37" "18" "93" "cd" "27", "MESSAGE=" "Time has been changed", ((void*)0)); | |||
| 2741 | ||||
| 2742 | /* Restart the watch */ | |||
| 2743 | (void) manager_setup_time_change(m); | |||
| 2744 | ||||
| 2745 | HASHMAP_FOREACH(u, m->units, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((m->units), & (i), (void**)&(u), ((void*)0)); ) | |||
| 2746 | if (UNIT_VTABLE(u)unit_vtable[(u)->type]->time_change) | |||
| 2747 | UNIT_VTABLE(u)unit_vtable[(u)->type]->time_change(u); | |||
| 2748 | ||||
| 2749 | return 0; | |||
| 2750 | } | |||
| 2751 | ||||
| 2752 | static int manager_dispatch_timezone_change( | |||
| 2753 | sd_event_source *source, | |||
| 2754 | const struct inotify_event *e, | |||
| 2755 | void *userdata) { | |||
| 2756 | ||||
| 2757 | Manager *m = userdata; | |||
| 2758 | int changed; | |||
| 2759 | Iterator i; | |||
| 2760 | Unit *u; | |||
| 2761 | ||||
| 2762 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 2762, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2763 | ||||
| 2764 | log_debug("inotify event for /etc/localtime")({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2764, __func__, "inotify event for /etc/localtime" ) : -abs(_e); }); | |||
| 2765 | ||||
| 2766 | changed = manager_read_timezone_stat(m); | |||
| 2767 | if (changed < 0) | |||
| 2768 | return changed; | |||
| 2769 | if (!changed) | |||
| 2770 | return 0; | |||
| 2771 | ||||
| 2772 | /* Something changed, restart the watch, to ensure we watch the new /etc/localtime if it changed */ | |||
| 2773 | (void) manager_setup_timezone_change(m); | |||
| 2774 | ||||
| 2775 | /* Read the new timezone */ | |||
| 2776 | tzset(); | |||
| 2777 | ||||
| 2778 | log_debug("Timezone has been changed (now: %s).", tzname[daylight])({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2778, __func__, "Timezone has been changed (now: %s)." , tzname[daylight]) : -abs(_e); }); | |||
| 2779 | ||||
| 2780 | HASHMAP_FOREACH(u, m->units, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((m->units), & (i), (void**)&(u), ((void*)0)); ) | |||
| 2781 | if (UNIT_VTABLE(u)unit_vtable[(u)->type]->timezone_change) | |||
| 2782 | UNIT_VTABLE(u)unit_vtable[(u)->type]->timezone_change(u); | |||
| 2783 | ||||
| 2784 | return 0; | |||
| 2785 | } | |||
| 2786 | ||||
| 2787 | static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { | |||
| 2788 | Manager *m = userdata; | |||
| 2789 | ||||
| 2790 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 2790, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2791 | assert(m->idle_pipe[2] == fd)do { if ((__builtin_expect(!!(!(m->idle_pipe[2] == fd)),0) )) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("m->idle_pipe[2] == fd" ), "../src/core/manager.c", 2791, __PRETTY_FUNCTION__); } while (0); | |||
| 2792 | ||||
| 2793 | /* There's at least one Type=idle child that just gave up on us waiting for the boot process to complete. Let's | |||
| 2794 | * now turn off any further console output if there's at least one service that needs console access, so that | |||
| 2795 | * from now on our own output should not spill into that service's output anymore. After all, we support | |||
| 2796 | * Type=idle only to beautify console output and it generally is set on services that want to own the console | |||
| 2797 | * exclusively without our interference. */ | |||
| 2798 | m->no_console_output = m->n_on_console > 0; | |||
| 2799 | ||||
| 2800 | /* Acknowledge the child's request, and let all all other children know too that they shouldn't wait any longer | |||
| 2801 | * by closing the pipes towards them, which is what they are waiting for. */ | |||
| 2802 | manager_close_idle_pipe(m); | |||
| 2803 | ||||
| 2804 | return 0; | |||
| 2805 | } | |||
| 2806 | ||||
| 2807 | static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata) { | |||
| 2808 | Manager *m = userdata; | |||
| 2809 | int r; | |||
| 2810 | uint64_t next; | |||
| 2811 | ||||
| 2812 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 2812, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2813 | assert(source)do { if ((__builtin_expect(!!(!(source)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("source"), "../src/core/manager.c", 2813 , __PRETTY_FUNCTION__); } while (0); | |||
| 2814 | ||||
| 2815 | manager_print_jobs_in_progress(m); | |||
| 2816 | ||||
| 2817 | next = now(CLOCK_MONOTONIC1) + JOBS_IN_PROGRESS_PERIOD_USEC(((usec_t) 1000000ULL) / 3); | |||
| 2818 | r = sd_event_source_set_time(source, next); | |||
| 2819 | if (r < 0) | |||
| 2820 | return r; | |||
| 2821 | ||||
| 2822 | return sd_event_source_set_enabled(source, SD_EVENT_ONESHOT); | |||
| 2823 | } | |||
| 2824 | ||||
| 2825 | int manager_loop(Manager *m) { | |||
| 2826 | int r; | |||
| 2827 | ||||
| 2828 | RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 50000)RateLimit rl = { .interval = (1*((usec_t) 1000000ULL)), .burst = (50000), .num = 0, .begin = 0 }; | |||
| 2829 | ||||
| 2830 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 2830, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2831 | m->exit_code = MANAGER_OK; | |||
| 2832 | ||||
| 2833 | /* Release the path cache */ | |||
| 2834 | m->unit_path_cache = set_free_free(m->unit_path_cache); | |||
| 2835 | ||||
| 2836 | manager_check_finished(m); | |||
| 2837 | ||||
| 2838 | /* There might still be some zombies hanging around from before we were exec()'ed. Let's reap them. */ | |||
| 2839 | r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_ON); | |||
| 2840 | if (r < 0) | |||
| 2841 | return log_error_errno(r, "Failed to enable SIGCHLD event source: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2841, __func__, "Failed to enable SIGCHLD event source: %m" ) : -abs(_e); }); | |||
| 2842 | ||||
| 2843 | while (m->exit_code == MANAGER_OK) { | |||
| 2844 | usec_t wait_usec; | |||
| 2845 | ||||
| 2846 | if (m->runtime_watchdog > 0 && m->runtime_watchdog != USEC_INFINITY((usec_t) -1) && MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) | |||
| 2847 | watchdog_ping(); | |||
| 2848 | ||||
| 2849 | if (!ratelimit_below(&rl)) { | |||
| 2850 | /* Yay, something is going seriously wrong, pause a little */ | |||
| 2851 | log_warning("Looping too fast. Throttling execution a little.")({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2851, __func__, "Looping too fast. Throttling execution a little." ) : -abs(_e); }); | |||
| 2852 | sleep(1); | |||
| 2853 | } | |||
| 2854 | ||||
| 2855 | if (manager_dispatch_load_queue(m) > 0) | |||
| 2856 | continue; | |||
| 2857 | ||||
| 2858 | if (manager_dispatch_gc_job_queue(m) > 0) | |||
| 2859 | continue; | |||
| 2860 | ||||
| 2861 | if (manager_dispatch_gc_unit_queue(m) > 0) | |||
| 2862 | continue; | |||
| 2863 | ||||
| 2864 | if (manager_dispatch_cleanup_queue(m) > 0) | |||
| 2865 | continue; | |||
| 2866 | ||||
| 2867 | if (manager_dispatch_cgroup_realize_queue(m) > 0) | |||
| 2868 | continue; | |||
| 2869 | ||||
| 2870 | if (manager_dispatch_stop_when_unneeded_queue(m) > 0) | |||
| 2871 | continue; | |||
| 2872 | ||||
| 2873 | if (manager_dispatch_dbus_queue(m) > 0) | |||
| 2874 | continue; | |||
| 2875 | ||||
| 2876 | /* Sleep for half the watchdog time */ | |||
| 2877 | if (m->runtime_watchdog > 0 && m->runtime_watchdog != USEC_INFINITY((usec_t) -1) && MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) { | |||
| 2878 | wait_usec = m->runtime_watchdog / 2; | |||
| 2879 | if (wait_usec <= 0) | |||
| 2880 | wait_usec = 1; | |||
| 2881 | } else | |||
| 2882 | wait_usec = USEC_INFINITY((usec_t) -1); | |||
| 2883 | ||||
| 2884 | r = sd_event_run(m->event, wait_usec); | |||
| 2885 | if (r < 0) | |||
| 2886 | return log_error_errno(r, "Failed to run event loop: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2886, __func__, "Failed to run event loop: %m" ) : -abs(_e); }); | |||
| 2887 | } | |||
| 2888 | ||||
| 2889 | return m->exit_code; | |||
| 2890 | } | |||
| 2891 | ||||
| 2892 | int manager_load_unit_from_dbus_path(Manager *m, const char *s, sd_bus_error *e, Unit **_u) { | |||
| 2893 | _cleanup_free___attribute__((cleanup(freep))) char *n = NULL((void*)0); | |||
| 2894 | sd_id128_t invocation_id; | |||
| 2895 | Unit *u; | |||
| 2896 | int r; | |||
| 2897 | ||||
| 2898 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 2898, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2899 | assert(s)do { if ((__builtin_expect(!!(!(s)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("s"), "../src/core/manager.c", 2899, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2900 | assert(_u)do { if ((__builtin_expect(!!(!(_u)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("_u"), "../src/core/manager.c", 2900, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2901 | ||||
| 2902 | r = unit_name_from_dbus_path(s, &n); | |||
| 2903 | if (r < 0) | |||
| 2904 | return r; | |||
| 2905 | ||||
| 2906 | /* Permit addressing units by invocation ID: if the passed bus path is suffixed by a 128bit ID then we use it | |||
| 2907 | * as invocation ID. */ | |||
| 2908 | r = sd_id128_from_string(n, &invocation_id); | |||
| 2909 | if (r >= 0) { | |||
| 2910 | u = hashmap_get(m->units_by_invocation_id, &invocation_id); | |||
| 2911 | if (u) { | |||
| 2912 | *_u = u; | |||
| 2913 | return 0; | |||
| 2914 | } | |||
| 2915 | ||||
| 2916 | return sd_bus_error_setf(e, BUS_ERROR_NO_UNIT_FOR_INVOCATION_ID"org.freedesktop.systemd1.NoUnitForInvocationID", | |||
| 2917 | "No unit with the specified invocation ID " SD_ID128_FORMAT_STR"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" " known.", | |||
| 2918 | SD_ID128_FORMAT_VAL(invocation_id)(invocation_id).bytes[0], (invocation_id).bytes[1], (invocation_id ).bytes[2], (invocation_id).bytes[3], (invocation_id).bytes[4 ], (invocation_id).bytes[5], (invocation_id).bytes[6], (invocation_id ).bytes[7], (invocation_id).bytes[8], (invocation_id).bytes[9 ], (invocation_id).bytes[10], (invocation_id).bytes[11], (invocation_id ).bytes[12], (invocation_id).bytes[13], (invocation_id).bytes [14], (invocation_id).bytes[15]); | |||
| 2919 | } | |||
| 2920 | ||||
| 2921 | /* If this didn't work, we check if this is a unit name */ | |||
| 2922 | if (!unit_name_is_valid(n, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) { | |||
| 2923 | _cleanup_free___attribute__((cleanup(freep))) char *nn = NULL((void*)0); | |||
| 2924 | ||||
| 2925 | nn = cescape(n); | |||
| 2926 | return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", | |||
| 2927 | "Unit name %s is neither a valid invocation ID nor unit name.", strnull(nn)); | |||
| 2928 | } | |||
| 2929 | ||||
| 2930 | r = manager_load_unit(m, n, NULL((void*)0), e, &u); | |||
| 2931 | if (r < 0) | |||
| 2932 | return r; | |||
| 2933 | ||||
| 2934 | *_u = u; | |||
| 2935 | return 0; | |||
| 2936 | } | |||
| 2937 | ||||
| 2938 | int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) { | |||
| 2939 | const char *p; | |||
| 2940 | unsigned id; | |||
| 2941 | Job *j; | |||
| 2942 | int r; | |||
| 2943 | ||||
| 2944 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 2944, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2945 | assert(s)do { if ((__builtin_expect(!!(!(s)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("s"), "../src/core/manager.c", 2945, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2946 | assert(_j)do { if ((__builtin_expect(!!(!(_j)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("_j"), "../src/core/manager.c", 2946, __PRETTY_FUNCTION__ ); } while (0); | |||
| 2947 | ||||
| 2948 | p = startswith(s, "/org/freedesktop/systemd1/job/"); | |||
| 2949 | if (!p) | |||
| 2950 | return -EINVAL22; | |||
| 2951 | ||||
| 2952 | r = safe_atou(p, &id); | |||
| 2953 | if (r < 0) | |||
| 2954 | return r; | |||
| 2955 | ||||
| 2956 | j = manager_get_job(m, id); | |||
| 2957 | if (!j) | |||
| 2958 | return -ENOENT2; | |||
| 2959 | ||||
| 2960 | *_j = j; | |||
| 2961 | ||||
| 2962 | return 0; | |||
| 2963 | } | |||
| 2964 | ||||
| 2965 | void manager_send_unit_audit(Manager *m, Unit *u, int type, bool_Bool success) { | |||
| 2966 | ||||
| 2967 | #if HAVE_AUDIT1 | |||
| 2968 | _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0); | |||
| 2969 | const char *msg; | |||
| 2970 | int audit_fd, r; | |||
| 2971 | ||||
| 2972 | if (!MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) | |||
| 2973 | return; | |||
| 2974 | ||||
| 2975 | audit_fd = get_audit_fd(); | |||
| 2976 | if (audit_fd < 0) | |||
| 2977 | return; | |||
| 2978 | ||||
| 2979 | /* Don't generate audit events if the service was already | |||
| 2980 | * started and we're just deserializing */ | |||
| 2981 | if (MANAGER_IS_RELOADING(m)((m)->n_reloading > 0)) | |||
| 2982 | return; | |||
| 2983 | ||||
| 2984 | if (u->type != UNIT_SERVICE) | |||
| 2985 | return; | |||
| 2986 | ||||
| 2987 | r = unit_name_to_prefix_and_instance(u->id, &p); | |||
| 2988 | if (r < 0) { | |||
| 2989 | log_error_errno(r, "Failed to extract prefix and instance of unit name: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 2989, __func__, "Failed to extract prefix and instance of unit name: %m" ) : -abs(_e); }); | |||
| 2990 | return; | |||
| 2991 | } | |||
| 2992 | ||||
| 2993 | msg = strjoina("unit=", p)({ const char *_appendees_[] = { "unit=", p }; char *_d_, *_p_ ; size_t _len_ = 0; size_t _i_; for (_i_ = 0; _i_ < __extension__ (__builtin_choose_expr( !__builtin_types_compatible_p(typeof (_appendees_), typeof(&*(_appendees_))), sizeof(_appendees_ )/sizeof((_appendees_)[0]), ((void)0))) && _appendees_ [_i_]; _i_++) _len_ += strlen(_appendees_[_i_]); _p_ = _d_ = __builtin_alloca (_len_ + 1); for (_i_ = 0; _i_ < __extension__ (__builtin_choose_expr ( !__builtin_types_compatible_p(typeof(_appendees_), typeof(& *(_appendees_))), sizeof(_appendees_)/sizeof((_appendees_)[0] ), ((void)0))) && _appendees_[_i_]; _i_++) _p_ = stpcpy (_p_, _appendees_[_i_]); *_p_ = 0; _d_; }); | |||
| 2994 | if (audit_log_user_comm_message(audit_fd, type, msg, "systemd", NULL((void*)0), NULL((void*)0), NULL((void*)0), success) < 0) { | |||
| 2995 | if (errno(*__errno_location ()) == EPERM1) | |||
| 2996 | /* We aren't allowed to send audit messages? | |||
| 2997 | * Then let's not retry again. */ | |||
| 2998 | close_audit_fd(); | |||
| 2999 | else | |||
| 3000 | log_warning_errno(errno, "Failed to send audit message: %m")({ int _level = ((4)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 3000, __func__, "Failed to send audit message: %m") : -abs(_e); }); | |||
| 3001 | } | |||
| 3002 | #endif | |||
| 3003 | ||||
| 3004 | } | |||
| 3005 | ||||
| 3006 | void manager_send_unit_plymouth(Manager *m, Unit *u) { | |||
| 3007 | static const union sockaddr_union sa = PLYMOUTH_SOCKET{ .un.sun_family = 1, .un.sun_path = "\0/org/freedesktop/plymouthd" , }; | |||
| 3008 | _cleanup_free___attribute__((cleanup(freep))) char *message = NULL((void*)0); | |||
| 3009 | _cleanup_close___attribute__((cleanup(closep))) int fd = -1; | |||
| 3010 | int n = 0; | |||
| 3011 | ||||
| 3012 | /* Don't generate plymouth events if the service was already | |||
| 3013 | * started and we're just deserializing */ | |||
| 3014 | if (MANAGER_IS_RELOADING(m)((m)->n_reloading > 0)) | |||
| 3015 | return; | |||
| 3016 | ||||
| 3017 | if (!MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) | |||
| 3018 | return; | |||
| 3019 | ||||
| 3020 | if (detect_container() > 0) | |||
| 3021 | return; | |||
| 3022 | ||||
| 3023 | if (!IN_SET(u->type, UNIT_SERVICE, UNIT_MOUNT, UNIT_SWAP)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){UNIT_SERVICE, UNIT_MOUNT, UNIT_SWAP})/sizeof (int)]; switch(u->type) { case UNIT_SERVICE: case UNIT_MOUNT : case UNIT_SWAP: _found = 1; break; default: break; } _found ; })) | |||
| 3024 | return; | |||
| 3025 | ||||
| 3026 | /* We set SOCK_NONBLOCK here so that we rather drop the | |||
| 3027 | * message then wait for plymouth */ | |||
| 3028 | fd = socket(AF_UNIX1, SOCK_STREAMSOCK_STREAM|SOCK_CLOEXECSOCK_CLOEXEC|SOCK_NONBLOCKSOCK_NONBLOCK, 0); | |||
| 3029 | if (fd < 0) { | |||
| 3030 | log_error_errno(errno, "socket() failed: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 3030, __func__, "socket() failed: %m") : -abs(_e); }); | |||
| 3031 | return; | |||
| 3032 | } | |||
| 3033 | ||||
| 3034 | if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)({ const struct sockaddr_un *_sa = &(sa.un); do { if ((__builtin_expect (!!(!(_sa->sun_family == 1)),0))) log_assert_failed_realm( LOG_REALM_SYSTEMD, ("_sa->sun_family == AF_UNIX"), "../src/core/manager.c" , 3034, __PRETTY_FUNCTION__); } while (0); __builtin_offsetof (struct sockaddr_un, sun_path) + (_sa->sun_path[0] == 0 ? 1 + strnlen(_sa->sun_path+1, sizeof(_sa->sun_path)-1) : strnlen (_sa->sun_path, sizeof(_sa->sun_path))); })) < 0) { | |||
| 3035 | if (!IN_SET(errno, EPIPE, EAGAIN, ENOENT, ECONNREFUSED, ECONNRESET, ECONNABORTED)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){32, 11, 2, 111, 104, 103})/sizeof(int)]; switch((*__errno_location ())) { case 32: case 11: case 2: case 111: case 104: case 103: _found = 1; break; default: break; } _found; })) | |||
| 3036 | log_error_errno(errno, "connect() failed: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 3036, __func__, "connect() failed: %m") : -abs(_e); }); | |||
| 3037 | return; | |||
| 3038 | } | |||
| 3039 | ||||
| 3040 | if (asprintf(&message, "U\002%c%s%n", (int) (strlen(u->id) + 1), u->id, &n) < 0) { | |||
| 3041 | log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/core/manager.c", 3041 , __func__); | |||
| 3042 | return; | |||
| 3043 | } | |||
| 3044 | ||||
| 3045 | errno(*__errno_location ()) = 0; | |||
| 3046 | if (write(fd, message, n + 1) != n + 1) | |||
| 3047 | if (!IN_SET(errno, EPIPE, EAGAIN, ENOENT, ECONNREFUSED, ECONNRESET, ECONNABORTED)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){32, 11, 2, 111, 104, 103})/sizeof(int)]; switch((*__errno_location ())) { case 32: case 11: case 2: case 111: case 104: case 103: _found = 1; break; default: break; } _found; })) | |||
| 3048 | log_error_errno(errno, "Failed to write Plymouth message: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 3048, __func__, "Failed to write Plymouth message: %m") : -abs(_e); }); | |||
| 3049 | } | |||
| 3050 | ||||
| 3051 | int manager_open_serialization(Manager *m, FILE **_f) { | |||
| 3052 | int fd; | |||
| 3053 | FILE *f; | |||
| 3054 | ||||
| 3055 | assert(_f)do { if ((__builtin_expect(!!(!(_f)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("_f"), "../src/core/manager.c", 3055, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3056 | ||||
| 3057 | fd = open_serialization_fd("systemd-state"); | |||
| 3058 | if (fd < 0) | |||
| 3059 | return fd; | |||
| 3060 | ||||
| 3061 | f = fdopen(fd, "w+"); | |||
| 3062 | if (!f) { | |||
| 3063 | safe_close(fd); | |||
| 3064 | return -errno(*__errno_location ()); | |||
| 3065 | } | |||
| 3066 | ||||
| 3067 | *_f = f; | |||
| 3068 | return 0; | |||
| 3069 | } | |||
| 3070 | ||||
| 3071 | int manager_serialize(Manager *m, FILE *f, FDSet *fds, bool_Bool switching_root) { | |||
| 3072 | ManagerTimestamp q; | |||
| 3073 | const char *t; | |||
| 3074 | Iterator i; | |||
| 3075 | Unit *u; | |||
| 3076 | int r; | |||
| 3077 | ||||
| 3078 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 3078, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3079 | assert(f)do { if ((__builtin_expect(!!(!(f)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("f"), "../src/core/manager.c", 3079, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3080 | assert(fds)do { if ((__builtin_expect(!!(!(fds)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("fds"), "../src/core/manager.c", 3080, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3081 | ||||
| 3082 | m->n_reloading++; | |||
| 3083 | ||||
| 3084 | fprintf(f, "current-job-id=%"PRIu32"u""\n", m->current_job_id); | |||
| 3085 | fprintf(f, "n-installed-jobs=%u\n", m->n_installed_jobs); | |||
| 3086 | fprintf(f, "n-failed-jobs=%u\n", m->n_failed_jobs); | |||
| 3087 | fprintf(f, "taint-usr=%s\n", yes_no(m->taint_usr)); | |||
| 3088 | fprintf(f, "ready-sent=%s\n", yes_no(m->ready_sent)); | |||
| 3089 | fprintf(f, "taint-logged=%s\n", yes_no(m->taint_logged)); | |||
| 3090 | fprintf(f, "service-watchdogs=%s\n", yes_no(m->service_watchdogs)); | |||
| 3091 | ||||
| 3092 | if (m->log_level_overridden) | |||
| 3093 | fprintf(f, "log-level-override=%i\n", log_get_max_level()log_get_max_level_realm(LOG_REALM_SYSTEMD)); | |||
| 3094 | if (m->log_target_overridden) | |||
| 3095 | fprintf(f, "log-target-override=%s\n", log_target_to_string(log_get_target())); | |||
| 3096 | ||||
| 3097 | for (q = 0; q < _MANAGER_TIMESTAMP_MAX; q++) { | |||
| 3098 | /* The userspace and finish timestamps only apply to the host system, hence only serialize them there */ | |||
| 3099 | if (in_initrd() && IN_SET(q, MANAGER_TIMESTAMP_USERSPACE, MANAGER_TIMESTAMP_FINISH)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){MANAGER_TIMESTAMP_USERSPACE, MANAGER_TIMESTAMP_FINISH })/sizeof(int)]; switch(q) { case MANAGER_TIMESTAMP_USERSPACE : case MANAGER_TIMESTAMP_FINISH: _found = 1; break; default: break ; } _found; })) | |||
| 3100 | continue; | |||
| 3101 | ||||
| 3102 | t = manager_timestamp_to_string(q); | |||
| 3103 | { | |||
| 3104 | char field[strlen(t) + STRLEN("-timestamp")(sizeof("""-timestamp""") - 1) + 1]; | |||
| 3105 | strcpy(stpcpy(field, t), "-timestamp"); | |||
| 3106 | dual_timestamp_serialize(f, field, m->timestamps + q); | |||
| 3107 | } | |||
| 3108 | } | |||
| 3109 | ||||
| 3110 | if (!switching_root) | |||
| 3111 | (void) serialize_environment(f, m->environment); | |||
| 3112 | ||||
| 3113 | if (m->notify_fd >= 0) { | |||
| 3114 | int copy; | |||
| 3115 | ||||
| 3116 | copy = fdset_put_dup(fds, m->notify_fd); | |||
| 3117 | if (copy < 0) | |||
| 3118 | return copy; | |||
| 3119 | ||||
| 3120 | fprintf(f, "notify-fd=%i\n", copy); | |||
| 3121 | fprintf(f, "notify-socket=%s\n", m->notify_socket); | |||
| 3122 | } | |||
| 3123 | ||||
| 3124 | if (m->cgroups_agent_fd >= 0) { | |||
| 3125 | int copy; | |||
| 3126 | ||||
| 3127 | copy = fdset_put_dup(fds, m->cgroups_agent_fd); | |||
| 3128 | if (copy < 0) | |||
| 3129 | return copy; | |||
| 3130 | ||||
| 3131 | fprintf(f, "cgroups-agent-fd=%i\n", copy); | |||
| 3132 | } | |||
| 3133 | ||||
| 3134 | if (m->user_lookup_fds[0] >= 0) { | |||
| 3135 | int copy0, copy1; | |||
| 3136 | ||||
| 3137 | copy0 = fdset_put_dup(fds, m->user_lookup_fds[0]); | |||
| 3138 | if (copy0 < 0) | |||
| 3139 | return copy0; | |||
| 3140 | ||||
| 3141 | copy1 = fdset_put_dup(fds, m->user_lookup_fds[1]); | |||
| 3142 | if (copy1 < 0) | |||
| 3143 | return copy1; | |||
| 3144 | ||||
| 3145 | fprintf(f, "user-lookup=%i %i\n", copy0, copy1); | |||
| 3146 | } | |||
| 3147 | ||||
| 3148 | bus_track_serialize(m->subscribed, f, "subscribed"); | |||
| 3149 | ||||
| 3150 | r = dynamic_user_serialize(m, f, fds); | |||
| 3151 | if (r < 0) | |||
| 3152 | return r; | |||
| 3153 | ||||
| 3154 | manager_serialize_uid_refs(m, f); | |||
| 3155 | manager_serialize_gid_refs(m, f); | |||
| 3156 | ||||
| 3157 | r = exec_runtime_serialize(m, f, fds); | |||
| 3158 | if (r < 0) | |||
| 3159 | return r; | |||
| 3160 | ||||
| 3161 | (void) fputc('\n', f); | |||
| 3162 | ||||
| 3163 | HASHMAP_FOREACH_KEY(u, t, m->units, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((m->units), & (i), (void**)&(u), (const void**) &(t)); ) { | |||
| 3164 | if (u->id != t) | |||
| 3165 | continue; | |||
| 3166 | ||||
| 3167 | /* Start marker */ | |||
| 3168 | fputs(u->id, f); | |||
| 3169 | fputc('\n', f); | |||
| 3170 | ||||
| 3171 | r = unit_serialize(u, f, fds, !switching_root); | |||
| 3172 | if (r < 0) { | |||
| 3173 | m->n_reloading--; | |||
| 3174 | return r; | |||
| 3175 | } | |||
| 3176 | } | |||
| 3177 | ||||
| 3178 | assert(m->n_reloading > 0)do { if ((__builtin_expect(!!(!(m->n_reloading > 0)),0) )) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("m->n_reloading > 0" ), "../src/core/manager.c", 3178, __PRETTY_FUNCTION__); } while (0); | |||
| 3179 | m->n_reloading--; | |||
| 3180 | ||||
| 3181 | r = fflush_and_check(f); | |||
| 3182 | if (r < 0) | |||
| 3183 | return r; | |||
| 3184 | ||||
| 3185 | r = bus_fdset_add_all(m, fds); | |||
| 3186 | if (r < 0) | |||
| 3187 | return r; | |||
| 3188 | ||||
| 3189 | return 0; | |||
| 3190 | } | |||
| 3191 | ||||
| 3192 | int manager_deserialize(Manager *m, FILE *f, FDSet *fds) { | |||
| 3193 | int r = 0; | |||
| 3194 | ||||
| 3195 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 3195, __PRETTY_FUNCTION__ ); } while (0); | |||
| ||||
| 3196 | assert(f)do { if ((__builtin_expect(!!(!(f)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("f"), "../src/core/manager.c", 3196, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3197 | ||||
| 3198 | log_debug("Deserializing state...")({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3198, __func__, "Deserializing state..." ) : -abs(_e); }); | |||
| 3199 | ||||
| 3200 | m->n_reloading++; | |||
| 3201 | ||||
| 3202 | for (;;) { | |||
| 3203 | _cleanup_free___attribute__((cleanup(freep))) char *line = NULL((void*)0); | |||
| 3204 | const char *val, *l; | |||
| 3205 | ||||
| 3206 | r = read_line(f, LONG_LINE_MAX(1U*1024U*1024U), &line); | |||
| 3207 | if (r < 0) | |||
| 3208 | return log_error_errno(r, "Failed to read serialization line: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3208, __func__, "Failed to read serialization line: %m" ) : -abs(_e); }); | |||
| 3209 | if (r == 0) | |||
| 3210 | break; | |||
| 3211 | ||||
| 3212 | l = strstrip(line); | |||
| 3213 | if (isempty(l)) /* end marker */ | |||
| 3214 | break; | |||
| 3215 | ||||
| 3216 | if ((val = startswith(l, "current-job-id="))) { | |||
| 3217 | uint32_t id; | |||
| 3218 | ||||
| 3219 | if (safe_atou32(val, &id) < 0) | |||
| 3220 | log_notice("Failed to parse current job id value %s", val)({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3220, __func__, "Failed to parse current job id value %s" , val) : -abs(_e); }); | |||
| 3221 | else | |||
| 3222 | m->current_job_id = MAX(m->current_job_id, id)__extension__ ({ const typeof((m->current_job_id)) __unique_prefix_A74 = ((m->current_job_id)); const typeof((id)) __unique_prefix_B75 = ((id)); __unique_prefix_A74 > __unique_prefix_B75 ? __unique_prefix_A74 : __unique_prefix_B75; }); | |||
| 3223 | ||||
| 3224 | } else if ((val = startswith(l, "n-installed-jobs="))) { | |||
| 3225 | uint32_t n; | |||
| 3226 | ||||
| 3227 | if (safe_atou32(val, &n) < 0) | |||
| 3228 | log_notice("Failed to parse installed jobs counter %s", val)({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3228, __func__, "Failed to parse installed jobs counter %s" , val) : -abs(_e); }); | |||
| 3229 | else | |||
| 3230 | m->n_installed_jobs += n; | |||
| 3231 | ||||
| 3232 | } else if ((val = startswith(l, "n-failed-jobs="))) { | |||
| 3233 | uint32_t n; | |||
| 3234 | ||||
| 3235 | if (safe_atou32(val, &n) < 0) | |||
| 3236 | log_notice("Failed to parse failed jobs counter %s", val)({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3236, __func__, "Failed to parse failed jobs counter %s" , val) : -abs(_e); }); | |||
| 3237 | else | |||
| 3238 | m->n_failed_jobs += n; | |||
| 3239 | ||||
| 3240 | } else if ((val = startswith(l, "taint-usr="))) { | |||
| 3241 | int b; | |||
| 3242 | ||||
| 3243 | b = parse_boolean(val); | |||
| 3244 | if (b < 0) | |||
| 3245 | log_notice("Failed to parse taint /usr flag %s", val)({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3245, __func__, "Failed to parse taint /usr flag %s" , val) : -abs(_e); }); | |||
| 3246 | else | |||
| 3247 | m->taint_usr = m->taint_usr || b; | |||
| 3248 | ||||
| 3249 | } else if ((val = startswith(l, "ready-sent="))) { | |||
| 3250 | int b; | |||
| 3251 | ||||
| 3252 | b = parse_boolean(val); | |||
| 3253 | if (b < 0) | |||
| 3254 | log_notice("Failed to parse ready-sent flag %s", val)({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3254, __func__, "Failed to parse ready-sent flag %s" , val) : -abs(_e); }); | |||
| 3255 | else | |||
| 3256 | m->ready_sent = m->ready_sent || b; | |||
| 3257 | ||||
| 3258 | } else if ((val = startswith(l, "taint-logged="))) { | |||
| 3259 | int b; | |||
| 3260 | ||||
| 3261 | b = parse_boolean(val); | |||
| 3262 | if (b < 0) | |||
| 3263 | log_notice("Failed to parse taint-logged flag %s", val)({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3263, __func__, "Failed to parse taint-logged flag %s" , val) : -abs(_e); }); | |||
| 3264 | else | |||
| 3265 | m->taint_logged = m->taint_logged || b; | |||
| 3266 | ||||
| 3267 | } else if ((val = startswith(l, "service-watchdogs="))) { | |||
| 3268 | int b; | |||
| 3269 | ||||
| 3270 | b = parse_boolean(val); | |||
| 3271 | if (b < 0) | |||
| 3272 | log_notice("Failed to parse service-watchdogs flag %s", val)({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3272, __func__, "Failed to parse service-watchdogs flag %s" , val) : -abs(_e); }); | |||
| 3273 | else | |||
| 3274 | m->service_watchdogs = b; | |||
| 3275 | ||||
| 3276 | } else if ((val = startswith(l, "log-level-override="))) { | |||
| 3277 | int level; | |||
| 3278 | ||||
| 3279 | level = log_level_from_string(val); | |||
| 3280 | if (level < 0) | |||
| 3281 | log_notice("Failed to parse log-level-override value '%s', ignoring.", val)({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3281, __func__, "Failed to parse log-level-override value '%s', ignoring." , val) : -abs(_e); }); | |||
| 3282 | else | |||
| 3283 | manager_override_log_level(m, level); | |||
| 3284 | ||||
| 3285 | } else if ((val = startswith(l, "log-target-override="))) { | |||
| 3286 | LogTarget target; | |||
| 3287 | ||||
| 3288 | target = log_target_from_string(val); | |||
| 3289 | if (target < 0) | |||
| 3290 | log_notice("Failed to parse log-target-override value '%s', ignoring.", val)({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3290, __func__, "Failed to parse log-target-override value '%s', ignoring." , val) : -abs(_e); }); | |||
| 3291 | else | |||
| 3292 | manager_override_log_target(m, target); | |||
| 3293 | ||||
| 3294 | } else if (startswith(l, "env=")) { | |||
| 3295 | r = deserialize_environment(&m->environment, l); | |||
| 3296 | if (r == -ENOMEM12) | |||
| 3297 | goto finish; | |||
| 3298 | if (r < 0) | |||
| 3299 | log_notice_errno(r, "Failed to parse environment entry: \"%s\": %m", l)({ int _level = ((5)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3299, __func__, "Failed to parse environment entry: \"%s\": %m" , l) : -abs(_e); }); | |||
| 3300 | ||||
| 3301 | } else if ((val = startswith(l, "notify-fd="))) { | |||
| 3302 | int fd; | |||
| 3303 | ||||
| 3304 | if (safe_atoi(val, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |||
| 3305 | log_notice("Failed to parse notify fd: \"%s\"", val)({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3305, __func__, "Failed to parse notify fd: \"%s\"" , val) : -abs(_e); }); | |||
| 3306 | else { | |||
| 3307 | m->notify_event_source = sd_event_source_unref(m->notify_event_source); | |||
| 3308 | safe_close(m->notify_fd); | |||
| 3309 | m->notify_fd = fdset_remove(fds, fd); | |||
| 3310 | } | |||
| 3311 | ||||
| 3312 | } else if ((val = startswith(l, "notify-socket="))) { | |||
| 3313 | char *n; | |||
| 3314 | ||||
| 3315 | n = strdup(val); | |||
| 3316 | if (!n) { | |||
| 3317 | r = -ENOMEM12; | |||
| 3318 | goto finish; | |||
| 3319 | } | |||
| 3320 | ||||
| 3321 | free(m->notify_socket); | |||
| 3322 | m->notify_socket = n; | |||
| 3323 | ||||
| 3324 | } else if ((val = startswith(l, "cgroups-agent-fd="))) { | |||
| 3325 | int fd; | |||
| 3326 | ||||
| 3327 | if (safe_atoi(val, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |||
| 3328 | log_notice("Failed to parse cgroups agent fd: %s", val)({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3328, __func__, "Failed to parse cgroups agent fd: %s" , val) : -abs(_e); }); | |||
| 3329 | else { | |||
| 3330 | m->cgroups_agent_event_source = sd_event_source_unref(m->cgroups_agent_event_source); | |||
| 3331 | safe_close(m->cgroups_agent_fd); | |||
| 3332 | m->cgroups_agent_fd = fdset_remove(fds, fd); | |||
| 3333 | } | |||
| 3334 | ||||
| 3335 | } else if ((val = startswith(l, "user-lookup="))) { | |||
| 3336 | int fd0, fd1; | |||
| 3337 | ||||
| 3338 | if (sscanf(val, "%i %i", &fd0, &fd1) != 2 || fd0 < 0 || fd1 < 0 || fd0 == fd1 || !fdset_contains(fds, fd0) || !fdset_contains(fds, fd1)) | |||
| 3339 | log_notice("Failed to parse user lookup fd: %s", val)({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3339, __func__, "Failed to parse user lookup fd: %s" , val) : -abs(_e); }); | |||
| 3340 | else { | |||
| 3341 | m->user_lookup_event_source = sd_event_source_unref(m->user_lookup_event_source); | |||
| 3342 | safe_close_pair(m->user_lookup_fds); | |||
| 3343 | m->user_lookup_fds[0] = fdset_remove(fds, fd0); | |||
| 3344 | m->user_lookup_fds[1] = fdset_remove(fds, fd1); | |||
| 3345 | } | |||
| 3346 | ||||
| 3347 | } else if ((val = startswith(l, "dynamic-user="))) | |||
| 3348 | dynamic_user_deserialize_one(m, val, fds); | |||
| 3349 | else if ((val = startswith(l, "destroy-ipc-uid="))) | |||
| 3350 | manager_deserialize_uid_refs_one(m, val); | |||
| 3351 | else if ((val = startswith(l, "destroy-ipc-gid="))) | |||
| 3352 | manager_deserialize_gid_refs_one(m, val); | |||
| 3353 | else if ((val = startswith(l, "exec-runtime="))) | |||
| 3354 | exec_runtime_deserialize_one(m, val, fds); | |||
| 3355 | else if ((val = startswith(l, "subscribed="))) { | |||
| 3356 | ||||
| 3357 | if (strv_extend(&m->deserialized_subscribed, val) < 0) | |||
| 3358 | log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/core/manager.c", 3358 , __func__); | |||
| 3359 | } else { | |||
| 3360 | ManagerTimestamp q; | |||
| 3361 | ||||
| 3362 | for (q = 0; q < _MANAGER_TIMESTAMP_MAX; q++) { | |||
| 3363 | val = startswith(l, manager_timestamp_to_string(q)); | |||
| 3364 | if (!val) | |||
| 3365 | continue; | |||
| 3366 | ||||
| 3367 | val = startswith(val, "-timestamp="); | |||
| 3368 | if (val) | |||
| 3369 | break; | |||
| 3370 | } | |||
| 3371 | ||||
| 3372 | if (q < _MANAGER_TIMESTAMP_MAX) /* found it */ | |||
| 3373 | dual_timestamp_deserialize(val, m->timestamps + q); | |||
| 3374 | else if (!startswith(l, "kdbus-fd=")) /* ignore kdbus */ | |||
| 3375 | log_notice("Unknown serialization item '%s'", l)({ int _level = (((5))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3375, __func__, "Unknown serialization item '%s'" , l) : -abs(_e); }); | |||
| 3376 | } | |||
| 3377 | } | |||
| 3378 | ||||
| 3379 | for (;;) { | |||
| 3380 | _cleanup_free___attribute__((cleanup(freep))) char *line = NULL((void*)0); | |||
| 3381 | const char* unit_name; | |||
| 3382 | Unit *u; | |||
| 3383 | ||||
| 3384 | /* Start marker */ | |||
| 3385 | r = read_line(f, LONG_LINE_MAX(1U*1024U*1024U), &line); | |||
| 3386 | if (r < 0) | |||
| 3387 | return log_error_errno(r, "Failed to read serialization line: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3387, __func__, "Failed to read serialization line: %m" ) : -abs(_e); }); | |||
| 3388 | if (r == 0) | |||
| 3389 | break; | |||
| 3390 | ||||
| 3391 | unit_name = strstrip(line); | |||
| 3392 | ||||
| 3393 | r = manager_load_unit(m, unit_name, NULL((void*)0), NULL((void*)0), &u); | |||
| 3394 | if (r < 0) { | |||
| 3395 | log_notice_errno(r, "Failed to load unit \"%s\", skipping deserialization: %m", unit_name)({ int _level = ((5)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3395, __func__, "Failed to load unit \"%s\", skipping deserialization: %m" , unit_name) : -abs(_e); }); | |||
| 3396 | ||||
| 3397 | r = unit_deserialize_skip(f); | |||
| 3398 | if (r < 0) | |||
| 3399 | return r; | |||
| 3400 | ||||
| 3401 | continue; | |||
| 3402 | } | |||
| 3403 | ||||
| 3404 | r = unit_deserialize(u, f, fds); | |||
| ||||
| 3405 | if (r < 0) { | |||
| 3406 | log_notice_errno(r, "Failed to deserialize unit \"%s\": %m", unit_name)({ int _level = ((5)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3406, __func__, "Failed to deserialize unit \"%s\": %m" , unit_name) : -abs(_e); }); | |||
| 3407 | if (r == -ENOMEM12) | |||
| 3408 | goto finish; | |||
| 3409 | } | |||
| 3410 | } | |||
| 3411 | ||||
| 3412 | finish: | |||
| 3413 | if (ferror(f)) | |||
| 3414 | r = -EIO5; | |||
| 3415 | ||||
| 3416 | assert(m->n_reloading > 0)do { if ((__builtin_expect(!!(!(m->n_reloading > 0)),0) )) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("m->n_reloading > 0" ), "../src/core/manager.c", 3416, __PRETTY_FUNCTION__); } while (0); | |||
| 3417 | m->n_reloading--; | |||
| 3418 | ||||
| 3419 | return r; | |||
| 3420 | } | |||
| 3421 | ||||
| 3422 | static void manager_flush_finished_jobs(Manager *m) { | |||
| 3423 | Job *j; | |||
| 3424 | ||||
| 3425 | while ((j = set_steal_first(m->pending_finished_jobs))) { | |||
| 3426 | bus_job_send_removed_signal(j); | |||
| 3427 | job_free(j); | |||
| 3428 | } | |||
| 3429 | ||||
| 3430 | m->pending_finished_jobs = set_free(m->pending_finished_jobs); | |||
| 3431 | } | |||
| 3432 | ||||
| 3433 | int manager_reload(Manager *m) { | |||
| 3434 | int r, q; | |||
| 3435 | _cleanup_fclose___attribute__((cleanup(fclosep))) FILE *f = NULL((void*)0); | |||
| 3436 | _cleanup_fdset_free___attribute__((cleanup(fdset_freep))) FDSet *fds = NULL((void*)0); | |||
| 3437 | ||||
| 3438 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 3438, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3439 | ||||
| 3440 | r = manager_open_serialization(m, &f); | |||
| 3441 | if (r < 0) | |||
| 3442 | return r; | |||
| 3443 | ||||
| 3444 | m->n_reloading++; | |||
| 3445 | bus_manager_send_reloading(m, true1); | |||
| 3446 | ||||
| 3447 | fds = fdset_new(); | |||
| 3448 | if (!fds) { | |||
| 3449 | m->n_reloading--; | |||
| 3450 | return -ENOMEM12; | |||
| 3451 | } | |||
| 3452 | ||||
| 3453 | r = manager_serialize(m, f, fds, false0); | |||
| 3454 | if (r < 0) { | |||
| 3455 | m->n_reloading--; | |||
| 3456 | return r; | |||
| 3457 | } | |||
| 3458 | ||||
| 3459 | if (fseeko(f, 0, SEEK_SET0) < 0) { | |||
| 3460 | m->n_reloading--; | |||
| 3461 | return -errno(*__errno_location ()); | |||
| 3462 | } | |||
| 3463 | ||||
| 3464 | /* From here on there is no way back. */ | |||
| 3465 | manager_clear_jobs_and_units(m); | |||
| 3466 | lookup_paths_flush_generator(&m->lookup_paths); | |||
| 3467 | lookup_paths_free(&m->lookup_paths); | |||
| 3468 | exec_runtime_vacuum(m); | |||
| 3469 | dynamic_user_vacuum(m, false0); | |||
| 3470 | m->uid_refs = hashmap_free(m->uid_refs); | |||
| 3471 | m->gid_refs = hashmap_free(m->gid_refs); | |||
| 3472 | ||||
| 3473 | q = lookup_paths_init(&m->lookup_paths, m->unit_file_scope, 0, NULL((void*)0)); | |||
| 3474 | if (q < 0 && r >= 0) | |||
| 3475 | r = q; | |||
| 3476 | ||||
| 3477 | q = manager_run_environment_generators(m); | |||
| 3478 | if (q < 0 && r >= 0) | |||
| 3479 | r = q; | |||
| 3480 | ||||
| 3481 | /* Find new unit paths */ | |||
| 3482 | q = manager_run_generators(m); | |||
| 3483 | if (q < 0 && r >= 0) | |||
| 3484 | r = q; | |||
| 3485 | ||||
| 3486 | lookup_paths_reduce(&m->lookup_paths); | |||
| 3487 | manager_build_unit_path_cache(m); | |||
| 3488 | ||||
| 3489 | /* First, enumerate what we can from all config files */ | |||
| 3490 | manager_enumerate(m); | |||
| 3491 | ||||
| 3492 | /* Second, deserialize our stored data */ | |||
| 3493 | q = manager_deserialize(m, f, fds); | |||
| 3494 | if (q < 0) { | |||
| 3495 | log_error_errno(q, "Deserialization failed: %m")({ int _level = ((3)), _e = ((q)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3495, __func__, "Deserialization failed: %m" ) : -abs(_e); }); | |||
| 3496 | ||||
| 3497 | if (r >= 0) | |||
| 3498 | r = q; | |||
| 3499 | } | |||
| 3500 | ||||
| 3501 | f = safe_fclose(f); | |||
| 3502 | ||||
| 3503 | /* Re-register notify_fd as event source */ | |||
| 3504 | q = manager_setup_notify(m); | |||
| 3505 | if (q < 0 && r >= 0) | |||
| 3506 | r = q; | |||
| 3507 | ||||
| 3508 | q = manager_setup_cgroups_agent(m); | |||
| 3509 | if (q < 0 && r >= 0) | |||
| 3510 | r = q; | |||
| 3511 | ||||
| 3512 | q = manager_setup_user_lookup_fd(m); | |||
| 3513 | if (q < 0 && r >= 0) | |||
| 3514 | r = q; | |||
| 3515 | ||||
| 3516 | /* Third, fire things up! */ | |||
| 3517 | manager_coldplug(m); | |||
| 3518 | ||||
| 3519 | /* Release any dynamic users no longer referenced */ | |||
| 3520 | dynamic_user_vacuum(m, true1); | |||
| 3521 | ||||
| 3522 | /* Release any references to UIDs/GIDs no longer referenced, and destroy any IPC owned by them */ | |||
| 3523 | manager_vacuum_uid_refs(m); | |||
| 3524 | manager_vacuum_gid_refs(m); | |||
| 3525 | ||||
| 3526 | exec_runtime_vacuum(m); | |||
| 3527 | ||||
| 3528 | assert(m->n_reloading > 0)do { if ((__builtin_expect(!!(!(m->n_reloading > 0)),0) )) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("m->n_reloading > 0" ), "../src/core/manager.c", 3528, __PRETTY_FUNCTION__); } while (0); | |||
| 3529 | m->n_reloading--; | |||
| 3530 | ||||
| 3531 | /* It might be safe to log to the journal now and connect to dbus */ | |||
| 3532 | manager_recheck_journal(m); | |||
| 3533 | manager_recheck_dbus(m); | |||
| 3534 | ||||
| 3535 | /* Let's finally catch up with any changes that took place while we were reloading/reexecing */ | |||
| 3536 | manager_catchup(m); | |||
| 3537 | ||||
| 3538 | /* Sync current state of bus names with our set of listening units */ | |||
| 3539 | q = manager_enqueue_sync_bus_names(m); | |||
| 3540 | if (q < 0 && r >= 0) | |||
| 3541 | r = q; | |||
| 3542 | ||||
| 3543 | if (!MANAGER_IS_RELOADING(m)((m)->n_reloading > 0)) | |||
| 3544 | manager_flush_finished_jobs(m); | |||
| 3545 | ||||
| 3546 | m->send_reloading_done = true1; | |||
| 3547 | ||||
| 3548 | return r; | |||
| 3549 | } | |||
| 3550 | ||||
| 3551 | void manager_reset_failed(Manager *m) { | |||
| 3552 | Unit *u; | |||
| 3553 | Iterator i; | |||
| 3554 | ||||
| 3555 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 3555, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3556 | ||||
| 3557 | HASHMAP_FOREACH(u, m->units, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((m->units), & (i), (void**)&(u), ((void*)0)); ) | |||
| 3558 | unit_reset_failed(u); | |||
| 3559 | } | |||
| 3560 | ||||
| 3561 | bool_Bool manager_unit_inactive_or_pending(Manager *m, const char *name) { | |||
| 3562 | Unit *u; | |||
| 3563 | ||||
| 3564 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 3564, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3565 | assert(name)do { if ((__builtin_expect(!!(!(name)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("name"), "../src/core/manager.c", 3565, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3566 | ||||
| 3567 | /* Returns true if the unit is inactive or going down */ | |||
| 3568 | u = manager_get_unit(m, name); | |||
| 3569 | if (!u) | |||
| 3570 | return true1; | |||
| 3571 | ||||
| 3572 | return unit_inactive_or_pending(u); | |||
| 3573 | } | |||
| 3574 | ||||
| 3575 | static void log_taint_string(Manager *m) { | |||
| 3576 | _cleanup_free___attribute__((cleanup(freep))) char *taint = NULL((void*)0); | |||
| 3577 | ||||
| 3578 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 3578, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3579 | ||||
| 3580 | if (MANAGER_IS_USER(m)((m)->unit_file_scope != UNIT_FILE_SYSTEM) || m->taint_logged) | |||
| 3581 | return; | |||
| 3582 | ||||
| 3583 | m->taint_logged = true1; /* only check for taint once */ | |||
| 3584 | ||||
| 3585 | taint = manager_taint_string(m); | |||
| 3586 | if (isempty(taint)) | |||
| 3587 | return; | |||
| 3588 | ||||
| 3589 | log_struct(LOG_NOTICE,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (5)), 0 , "../src/core/manager.c", 3592, __func__, "MESSAGE=" "System is tainted: %s" , taint, "TAINT=%s", taint, "MESSAGE_ID=" "50" "87" "6a" "9d" "b0" "0f" "4c" "40" "bd" "e1" "a2" "ad" "38" "1c" "3a" "1b", ((void*)0)) | |||
| 3590 | LOG_MESSAGE("System is tainted: %s", taint),log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (5)), 0 , "../src/core/manager.c", 3592, __func__, "MESSAGE=" "System is tainted: %s" , taint, "TAINT=%s", taint, "MESSAGE_ID=" "50" "87" "6a" "9d" "b0" "0f" "4c" "40" "bd" "e1" "a2" "ad" "38" "1c" "3a" "1b", ((void*)0)) | |||
| 3591 | "TAINT=%s", taint,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (5)), 0 , "../src/core/manager.c", 3592, __func__, "MESSAGE=" "System is tainted: %s" , taint, "TAINT=%s", taint, "MESSAGE_ID=" "50" "87" "6a" "9d" "b0" "0f" "4c" "40" "bd" "e1" "a2" "ad" "38" "1c" "3a" "1b", ((void*)0)) | |||
| 3592 | "MESSAGE_ID=" SD_MESSAGE_TAINTED_STR)log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (5)), 0 , "../src/core/manager.c", 3592, __func__, "MESSAGE=" "System is tainted: %s" , taint, "TAINT=%s", taint, "MESSAGE_ID=" "50" "87" "6a" "9d" "b0" "0f" "4c" "40" "bd" "e1" "a2" "ad" "38" "1c" "3a" "1b", ((void*)0)); | |||
| 3593 | } | |||
| 3594 | ||||
| 3595 | static void manager_notify_finished(Manager *m) { | |||
| 3596 | char userspace[FORMAT_TIMESPAN_MAX64], initrd[FORMAT_TIMESPAN_MAX64], kernel[FORMAT_TIMESPAN_MAX64], sum[FORMAT_TIMESPAN_MAX64]; | |||
| 3597 | usec_t firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec; | |||
| 3598 | ||||
| 3599 | if (m->test_run_flags) | |||
| 3600 | return; | |||
| 3601 | ||||
| 3602 | if (MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM) && detect_container() <= 0) { | |||
| 3603 | char ts[FORMAT_TIMESPAN_MAX64]; | |||
| 3604 | char buf[FORMAT_TIMESPAN_MAX64 + STRLEN(" (firmware) + ")(sizeof(""" (firmware) + """) - 1) + FORMAT_TIMESPAN_MAX64 + STRLEN(" (loader) + ")(sizeof(""" (loader) + """) - 1)] | |||
| 3605 | = {}; | |||
| 3606 | char *p = buf; | |||
| 3607 | size_t size = sizeof buf; | |||
| 3608 | ||||
| 3609 | /* Note that MANAGER_TIMESTAMP_KERNEL's monotonic value is always at 0, and | |||
| 3610 | * MANAGER_TIMESTAMP_FIRMWARE's and MANAGER_TIMESTAMP_LOADER's monotonic value should be considered | |||
| 3611 | * negative values. */ | |||
| 3612 | ||||
| 3613 | firmware_usec = m->timestamps[MANAGER_TIMESTAMP_FIRMWARE].monotonic - m->timestamps[MANAGER_TIMESTAMP_LOADER].monotonic; | |||
| 3614 | loader_usec = m->timestamps[MANAGER_TIMESTAMP_LOADER].monotonic - m->timestamps[MANAGER_TIMESTAMP_KERNEL].monotonic; | |||
| 3615 | userspace_usec = m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic - m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic; | |||
| 3616 | total_usec = m->timestamps[MANAGER_TIMESTAMP_FIRMWARE].monotonic + m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic; | |||
| 3617 | ||||
| 3618 | if (firmware_usec > 0) | |||
| 3619 | size = strpcpyf(&p, size, "%s (firmware) + ", format_timespan(ts, sizeof(ts), firmware_usec, USEC_PER_MSEC((usec_t) 1000ULL))); | |||
| 3620 | if (loader_usec > 0) | |||
| 3621 | size = strpcpyf(&p, size, "%s (loader) + ", format_timespan(ts, sizeof(ts), loader_usec, USEC_PER_MSEC((usec_t) 1000ULL))); | |||
| 3622 | ||||
| 3623 | if (dual_timestamp_is_set(&m->timestamps[MANAGER_TIMESTAMP_INITRD])) { | |||
| 3624 | ||||
| 3625 | /* The initrd case on bare-metal*/ | |||
| 3626 | kernel_usec = m->timestamps[MANAGER_TIMESTAMP_INITRD].monotonic - m->timestamps[MANAGER_TIMESTAMP_KERNEL].monotonic; | |||
| 3627 | initrd_usec = m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic - m->timestamps[MANAGER_TIMESTAMP_INITRD].monotonic; | |||
| 3628 | ||||
| 3629 | log_struct(LOG_INFO,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3639, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "INITRD_USEC=" "%" "l" "u", initrd_usec, "USERSPACE_USEC=""%" "l" "u", userspace_usec , "MESSAGE=" "Startup finished in %s%s (kernel) + %s (initrd) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(initrd, sizeof(initrd), initrd_usec , ((usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3630 | "MESSAGE_ID=" SD_MESSAGE_STARTUP_FINISHED_STR,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3639, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "INITRD_USEC=" "%" "l" "u", initrd_usec, "USERSPACE_USEC=""%" "l" "u", userspace_usec , "MESSAGE=" "Startup finished in %s%s (kernel) + %s (initrd) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(initrd, sizeof(initrd), initrd_usec , ((usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3631 | "KERNEL_USEC="USEC_FMT, kernel_usec,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3639, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "INITRD_USEC=" "%" "l" "u", initrd_usec, "USERSPACE_USEC=""%" "l" "u", userspace_usec , "MESSAGE=" "Startup finished in %s%s (kernel) + %s (initrd) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(initrd, sizeof(initrd), initrd_usec , ((usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3632 | "INITRD_USEC="USEC_FMT, initrd_usec,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3639, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "INITRD_USEC=" "%" "l" "u", initrd_usec, "USERSPACE_USEC=""%" "l" "u", userspace_usec , "MESSAGE=" "Startup finished in %s%s (kernel) + %s (initrd) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(initrd, sizeof(initrd), initrd_usec , ((usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3633 | "USERSPACE_USEC="USEC_FMT, userspace_usec,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3639, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "INITRD_USEC=" "%" "l" "u", initrd_usec, "USERSPACE_USEC=""%" "l" "u", userspace_usec , "MESSAGE=" "Startup finished in %s%s (kernel) + %s (initrd) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(initrd, sizeof(initrd), initrd_usec , ((usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3634 | LOG_MESSAGE("Startup finished in %s%s (kernel) + %s (initrd) + %s (userspace) = %s.",log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3639, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "INITRD_USEC=" "%" "l" "u", initrd_usec, "USERSPACE_USEC=""%" "l" "u", userspace_usec , "MESSAGE=" "Startup finished in %s%s (kernel) + %s (initrd) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(initrd, sizeof(initrd), initrd_usec , ((usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3635 | buf,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3639, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "INITRD_USEC=" "%" "l" "u", initrd_usec, "USERSPACE_USEC=""%" "l" "u", userspace_usec , "MESSAGE=" "Startup finished in %s%s (kernel) + %s (initrd) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(initrd, sizeof(initrd), initrd_usec , ((usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3636 | format_timespan(kernel, sizeof(kernel), kernel_usec, USEC_PER_MSEC),log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3639, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "INITRD_USEC=" "%" "l" "u", initrd_usec, "USERSPACE_USEC=""%" "l" "u", userspace_usec , "MESSAGE=" "Startup finished in %s%s (kernel) + %s (initrd) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(initrd, sizeof(initrd), initrd_usec , ((usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3637 | format_timespan(initrd, sizeof(initrd), initrd_usec, USEC_PER_MSEC),log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3639, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "INITRD_USEC=" "%" "l" "u", initrd_usec, "USERSPACE_USEC=""%" "l" "u", userspace_usec , "MESSAGE=" "Startup finished in %s%s (kernel) + %s (initrd) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(initrd, sizeof(initrd), initrd_usec , ((usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3638 | format_timespan(userspace, sizeof(userspace), userspace_usec, USEC_PER_MSEC),log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3639, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "INITRD_USEC=" "%" "l" "u", initrd_usec, "USERSPACE_USEC=""%" "l" "u", userspace_usec , "MESSAGE=" "Startup finished in %s%s (kernel) + %s (initrd) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(initrd, sizeof(initrd), initrd_usec , ((usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3639 | format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC)))log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3639, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "INITRD_USEC=" "%" "l" "u", initrd_usec, "USERSPACE_USEC=""%" "l" "u", userspace_usec , "MESSAGE=" "Startup finished in %s%s (kernel) + %s (initrd) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(initrd, sizeof(initrd), initrd_usec , ((usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)); | |||
| 3640 | } else { | |||
| 3641 | /* The initrd-less case on bare-metal*/ | |||
| 3642 | ||||
| 3643 | kernel_usec = m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic - m->timestamps[MANAGER_TIMESTAMP_KERNEL].monotonic; | |||
| 3644 | initrd_usec = 0; | |||
| 3645 | ||||
| 3646 | log_struct(LOG_INFO,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3654, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "USERSPACE_USEC=" "%" "l" "u", userspace_usec, "MESSAGE=" "Startup finished in %s%s (kernel) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3647 | "MESSAGE_ID=" SD_MESSAGE_STARTUP_FINISHED_STR,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3654, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "USERSPACE_USEC=" "%" "l" "u", userspace_usec, "MESSAGE=" "Startup finished in %s%s (kernel) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3648 | "KERNEL_USEC="USEC_FMT, kernel_usec,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3654, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "USERSPACE_USEC=" "%" "l" "u", userspace_usec, "MESSAGE=" "Startup finished in %s%s (kernel) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3649 | "USERSPACE_USEC="USEC_FMT, userspace_usec,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3654, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "USERSPACE_USEC=" "%" "l" "u", userspace_usec, "MESSAGE=" "Startup finished in %s%s (kernel) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3650 | LOG_MESSAGE("Startup finished in %s%s (kernel) + %s (userspace) = %s.",log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3654, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "USERSPACE_USEC=" "%" "l" "u", userspace_usec, "MESSAGE=" "Startup finished in %s%s (kernel) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3651 | buf,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3654, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "USERSPACE_USEC=" "%" "l" "u", userspace_usec, "MESSAGE=" "Startup finished in %s%s (kernel) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3652 | format_timespan(kernel, sizeof(kernel), kernel_usec, USEC_PER_MSEC),log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3654, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "USERSPACE_USEC=" "%" "l" "u", userspace_usec, "MESSAGE=" "Startup finished in %s%s (kernel) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3653 | format_timespan(userspace, sizeof(userspace), userspace_usec, USEC_PER_MSEC),log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3654, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "USERSPACE_USEC=" "%" "l" "u", userspace_usec, "MESSAGE=" "Startup finished in %s%s (kernel) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3654 | format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC)))log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3654, __func__, "MESSAGE_ID=" "b0" "7a" "24" "9c" "d0" "24" "41" "4a" "82" "dd" "00" "cd" "18" "13" "78" "ff", "KERNEL_USEC=""%" "l" "u", kernel_usec, "USERSPACE_USEC=" "%" "l" "u", userspace_usec, "MESSAGE=" "Startup finished in %s%s (kernel) + %s (userspace) = %s." , buf, format_timespan(kernel, sizeof(kernel), kernel_usec, ( (usec_t) 1000ULL)), format_timespan(userspace, sizeof(userspace ), userspace_usec, ((usec_t) 1000ULL)), format_timespan(sum, sizeof (sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)); | |||
| 3655 | } | |||
| 3656 | } else { | |||
| 3657 | /* The container and --user case */ | |||
| 3658 | firmware_usec = loader_usec = initrd_usec = kernel_usec = 0; | |||
| 3659 | total_usec = userspace_usec = m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic - m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic; | |||
| 3660 | ||||
| 3661 | log_struct(LOG_INFO,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3665, __func__, "MESSAGE_ID=" "ee" "d0" "0a" "68" "ff" "d8" "4e" "31" "88" "21" "05" "fd" "97" "3a" "bd" "d1", "USERSPACE_USEC=""%" "l" "u", userspace_usec, "MESSAGE=" "Startup finished in %s.", format_timespan(sum, sizeof(sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3662 | "MESSAGE_ID=" SD_MESSAGE_USER_STARTUP_FINISHED_STR,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3665, __func__, "MESSAGE_ID=" "ee" "d0" "0a" "68" "ff" "d8" "4e" "31" "88" "21" "05" "fd" "97" "3a" "bd" "d1", "USERSPACE_USEC=""%" "l" "u", userspace_usec, "MESSAGE=" "Startup finished in %s.", format_timespan(sum, sizeof(sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3663 | "USERSPACE_USEC="USEC_FMT, userspace_usec,log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3665, __func__, "MESSAGE_ID=" "ee" "d0" "0a" "68" "ff" "d8" "4e" "31" "88" "21" "05" "fd" "97" "3a" "bd" "d1", "USERSPACE_USEC=""%" "l" "u", userspace_usec, "MESSAGE=" "Startup finished in %s.", format_timespan(sum, sizeof(sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3664 | LOG_MESSAGE("Startup finished in %s.",log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3665, __func__, "MESSAGE_ID=" "ee" "d0" "0a" "68" "ff" "d8" "4e" "31" "88" "21" "05" "fd" "97" "3a" "bd" "d1", "USERSPACE_USEC=""%" "l" "u", userspace_usec, "MESSAGE=" "Startup finished in %s.", format_timespan(sum, sizeof(sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)) | |||
| 3665 | format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC)))log_struct_internal(((LOG_REALM_SYSTEMD) << 10 | (6)), 0 , "../src/core/manager.c", 3665, __func__, "MESSAGE_ID=" "ee" "d0" "0a" "68" "ff" "d8" "4e" "31" "88" "21" "05" "fd" "97" "3a" "bd" "d1", "USERSPACE_USEC=""%" "l" "u", userspace_usec, "MESSAGE=" "Startup finished in %s.", format_timespan(sum, sizeof(sum), total_usec, ((usec_t) 1000ULL)), ((void*)0)); | |||
| 3666 | } | |||
| 3667 | ||||
| 3668 | bus_manager_send_finished(m, firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec); | |||
| 3669 | ||||
| 3670 | sd_notifyf(false0, | |||
| 3671 | m->ready_sent ? "STATUS=Startup finished in %s." | |||
| 3672 | : "READY=1\n" | |||
| 3673 | "STATUS=Startup finished in %s.", | |||
| 3674 | format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC((usec_t) 1000ULL))); | |||
| 3675 | m->ready_sent = true1; | |||
| 3676 | ||||
| 3677 | log_taint_string(m); | |||
| 3678 | } | |||
| 3679 | ||||
| 3680 | static void manager_send_ready(Manager *m) { | |||
| 3681 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 3681, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3682 | ||||
| 3683 | /* We send READY=1 on reaching basic.target only when running in --user mode. */ | |||
| 3684 | if (!MANAGER_IS_USER(m)((m)->unit_file_scope != UNIT_FILE_SYSTEM) || m->ready_sent) | |||
| 3685 | return; | |||
| 3686 | ||||
| 3687 | m->ready_sent = true1; | |||
| 3688 | ||||
| 3689 | sd_notifyf(false0, | |||
| 3690 | "READY=1\n" | |||
| 3691 | "STATUS=Reached " SPECIAL_BASIC_TARGET"basic.target" "."); | |||
| 3692 | } | |||
| 3693 | ||||
| 3694 | static void manager_check_basic_target(Manager *m) { | |||
| 3695 | Unit *u; | |||
| 3696 | ||||
| 3697 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 3697, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3698 | ||||
| 3699 | /* Small shortcut */ | |||
| 3700 | if (m->ready_sent && m->taint_logged) | |||
| 3701 | return; | |||
| 3702 | ||||
| 3703 | u = manager_get_unit(m, SPECIAL_BASIC_TARGET"basic.target"); | |||
| 3704 | if (!u || !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) | |||
| 3705 | return; | |||
| 3706 | ||||
| 3707 | /* For user managers, send out READY=1 as soon as we reach basic.target */ | |||
| 3708 | manager_send_ready(m); | |||
| 3709 | ||||
| 3710 | /* Log the taint string as soon as we reach basic.target */ | |||
| 3711 | log_taint_string(m); | |||
| 3712 | } | |||
| 3713 | ||||
| 3714 | void manager_check_finished(Manager *m) { | |||
| 3715 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 3715, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3716 | ||||
| 3717 | if (MANAGER_IS_RELOADING(m)((m)->n_reloading > 0)) | |||
| 3718 | return; | |||
| 3719 | ||||
| 3720 | /* Verify that we have entered the event loop already, and not left it again. */ | |||
| 3721 | if (!MANAGER_IS_RUNNING(m)((m)->exit_code == MANAGER_OK)) | |||
| 3722 | return; | |||
| 3723 | ||||
| 3724 | manager_check_basic_target(m); | |||
| 3725 | ||||
| 3726 | if (hashmap_size(m->jobs) > 0) { | |||
| 3727 | if (m->jobs_in_progress_event_source) | |||
| 3728 | /* Ignore any failure, this is only for feedback */ | |||
| 3729 | (void) sd_event_source_set_time(m->jobs_in_progress_event_source, now(CLOCK_MONOTONIC1) + JOBS_IN_PROGRESS_WAIT_USEC(5*((usec_t) 1000000ULL))); | |||
| 3730 | ||||
| 3731 | return; | |||
| 3732 | } | |||
| 3733 | ||||
| 3734 | manager_flip_auto_status(m, false0); | |||
| 3735 | ||||
| 3736 | /* Notify Type=idle units that we are done now */ | |||
| 3737 | manager_close_idle_pipe(m); | |||
| 3738 | ||||
| 3739 | /* Turn off confirm spawn now */ | |||
| 3740 | m->confirm_spawn = NULL((void*)0); | |||
| 3741 | ||||
| 3742 | /* No need to update ask password status when we're going non-interactive */ | |||
| 3743 | manager_close_ask_password(m); | |||
| 3744 | ||||
| 3745 | /* This is no longer the first boot */ | |||
| 3746 | manager_set_first_boot(m, false0); | |||
| 3747 | ||||
| 3748 | if (MANAGER_IS_FINISHED(m)(dual_timestamp_is_set((m)->timestamps + MANAGER_TIMESTAMP_FINISH ))) | |||
| 3749 | return; | |||
| 3750 | ||||
| 3751 | dual_timestamp_get(m->timestamps + MANAGER_TIMESTAMP_FINISH); | |||
| 3752 | ||||
| 3753 | manager_notify_finished(m); | |||
| 3754 | ||||
| 3755 | manager_invalidate_startup_units(m); | |||
| 3756 | } | |||
| 3757 | ||||
| 3758 | static bool_Bool generator_path_any(const char* const* paths) { | |||
| 3759 | char **path; | |||
| 3760 | bool_Bool found = false0; | |||
| 3761 | ||||
| 3762 | /* Optimize by skipping the whole process by not creating output directories | |||
| 3763 | * if no generators are found. */ | |||
| 3764 | STRV_FOREACH(path, (char**) paths)for ((path) = ((char**) paths); (path) && *(path); (path )++) | |||
| 3765 | if (access(*path, F_OK0) == 0) | |||
| 3766 | found = true1; | |||
| 3767 | else if (errno(*__errno_location ()) != ENOENT2) | |||
| 3768 | log_warning_errno(errno, "Failed to open generator directory %s: %m", *path)({ int _level = ((4)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 3768, __func__, "Failed to open generator directory %s: %m", *path) : -abs(_e ); }); | |||
| 3769 | ||||
| 3770 | return found; | |||
| 3771 | } | |||
| 3772 | ||||
| 3773 | static const char* system_env_generator_binary_paths[] = { | |||
| 3774 | "/run/systemd/system-environment-generators", | |||
| 3775 | "/etc/systemd/system-environment-generators", | |||
| 3776 | "/usr/local/lib/systemd/system-environment-generators", | |||
| 3777 | SYSTEM_ENV_GENERATOR_PATH"/usr/lib/systemd/system-environment-generators", | |||
| 3778 | NULL((void*)0) | |||
| 3779 | }; | |||
| 3780 | ||||
| 3781 | static const char* user_env_generator_binary_paths[] = { | |||
| 3782 | "/run/systemd/user-environment-generators", | |||
| 3783 | "/etc/systemd/user-environment-generators", | |||
| 3784 | "/usr/local/lib/systemd/user-environment-generators", | |||
| 3785 | USER_ENV_GENERATOR_PATH"/usr/lib/systemd/user-environment-generators", | |||
| 3786 | NULL((void*)0) | |||
| 3787 | }; | |||
| 3788 | ||||
| 3789 | static int manager_run_environment_generators(Manager *m) { | |||
| 3790 | char **tmp = NULL((void*)0); /* this is only used in the forked process, no cleanup here */ | |||
| 3791 | const char **paths; | |||
| 3792 | void* args[] = {&tmp, &tmp, &m->environment}; | |||
| 3793 | ||||
| 3794 | if (m->test_run_flags && !(m->test_run_flags & MANAGER_TEST_RUN_ENV_GENERATORS)) | |||
| 3795 | return 0; | |||
| 3796 | ||||
| 3797 | paths = MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM) ? system_env_generator_binary_paths : user_env_generator_binary_paths; | |||
| 3798 | ||||
| 3799 | if (!generator_path_any(paths)) | |||
| 3800 | return 0; | |||
| 3801 | ||||
| 3802 | return execute_directories(paths, DEFAULT_TIMEOUT_USEC(90*((usec_t) 1000000ULL)), gather_environment, args, NULL((void*)0)); | |||
| 3803 | } | |||
| 3804 | ||||
| 3805 | static int manager_run_generators(Manager *m) { | |||
| 3806 | _cleanup_strv_free___attribute__((cleanup(strv_freep))) char **paths = NULL((void*)0); | |||
| 3807 | const char *argv[5]; | |||
| 3808 | int r; | |||
| 3809 | ||||
| 3810 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 3810, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3811 | ||||
| 3812 | if (m->test_run_flags && !(m->test_run_flags & MANAGER_TEST_RUN_GENERATORS)) | |||
| 3813 | return 0; | |||
| 3814 | ||||
| 3815 | paths = generator_binary_paths(m->unit_file_scope); | |||
| 3816 | if (!paths) | |||
| 3817 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/core/manager.c", 3817 , __func__); | |||
| 3818 | ||||
| 3819 | if (!generator_path_any((const char* const*) paths)) | |||
| 3820 | return 0; | |||
| 3821 | ||||
| 3822 | r = lookup_paths_mkdir_generator(&m->lookup_paths); | |||
| 3823 | if (r < 0) | |||
| 3824 | goto finish; | |||
| 3825 | ||||
| 3826 | argv[0] = NULL((void*)0); /* Leave this empty, execute_directory() will fill something in */ | |||
| 3827 | argv[1] = m->lookup_paths.generator; | |||
| 3828 | argv[2] = m->lookup_paths.generator_early; | |||
| 3829 | argv[3] = m->lookup_paths.generator_late; | |||
| 3830 | argv[4] = NULL((void*)0); | |||
| 3831 | ||||
| 3832 | RUN_WITH_UMASK(0022)for (__attribute__((cleanup(_reset_umask_))) struct _umask_struct_ _saved_umask_ = { umask(0022), 0 }; !_saved_umask_.quit ; _saved_umask_ .quit = 1) | |||
| 3833 | execute_directories((const char* const*) paths, DEFAULT_TIMEOUT_USEC(90*((usec_t) 1000000ULL)), | |||
| 3834 | NULL((void*)0), NULL((void*)0), (char**) argv); | |||
| 3835 | ||||
| 3836 | finish: | |||
| 3837 | lookup_paths_trim_generator(&m->lookup_paths); | |||
| 3838 | return r; | |||
| 3839 | } | |||
| 3840 | ||||
| 3841 | int manager_environment_add(Manager *m, char **minus, char **plus) { | |||
| 3842 | char **a = NULL((void*)0), **b = NULL((void*)0), **l; | |||
| 3843 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 3843, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3844 | ||||
| 3845 | l = m->environment; | |||
| 3846 | ||||
| 3847 | if (!strv_isempty(minus)) { | |||
| 3848 | a = strv_env_delete(l, 1, minus); | |||
| 3849 | if (!a) | |||
| 3850 | return -ENOMEM12; | |||
| 3851 | ||||
| 3852 | l = a; | |||
| 3853 | } | |||
| 3854 | ||||
| 3855 | if (!strv_isempty(plus)) { | |||
| 3856 | b = strv_env_merge(2, l, plus); | |||
| 3857 | if (!b) { | |||
| 3858 | strv_free(a); | |||
| 3859 | return -ENOMEM12; | |||
| 3860 | } | |||
| 3861 | ||||
| 3862 | l = b; | |||
| 3863 | } | |||
| 3864 | ||||
| 3865 | if (m->environment != l) | |||
| 3866 | strv_free(m->environment); | |||
| 3867 | if (a != l) | |||
| 3868 | strv_free(a); | |||
| 3869 | if (b != l) | |||
| 3870 | strv_free(b); | |||
| 3871 | ||||
| 3872 | m->environment = l; | |||
| 3873 | manager_sanitize_environment(m); | |||
| 3874 | ||||
| 3875 | return 0; | |||
| 3876 | } | |||
| 3877 | ||||
| 3878 | int manager_set_default_rlimits(Manager *m, struct rlimit **default_rlimit) { | |||
| 3879 | int i; | |||
| 3880 | ||||
| 3881 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 3881, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3882 | ||||
| 3883 | for (i = 0; i < _RLIMIT_MAX(__RLIMIT_RTTIME+1 > __RLIMIT_NLIMITS ? __RLIMIT_RTTIME+1 : __RLIMIT_NLIMITS); i++) { | |||
| 3884 | m->rlimit[i] = mfree(m->rlimit[i]); | |||
| 3885 | ||||
| 3886 | if (!default_rlimit[i]) | |||
| 3887 | continue; | |||
| 3888 | ||||
| 3889 | m->rlimit[i] = newdup(struct rlimit, default_rlimit[i], 1)((struct rlimit*) memdup_multiply(default_rlimit[i], sizeof(struct rlimit), (1))); | |||
| 3890 | if (!m->rlimit[i]) | |||
| 3891 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/core/manager.c", 3891 , __func__); | |||
| 3892 | } | |||
| 3893 | ||||
| 3894 | return 0; | |||
| 3895 | } | |||
| 3896 | ||||
| 3897 | void manager_recheck_dbus(Manager *m) { | |||
| 3898 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 3898, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3899 | ||||
| 3900 | /* Connects to the bus if the dbus service and socket are running. If we are running in user mode this is all | |||
| 3901 | * it does. In system mode we'll also connect to the system bus (which will most likely just reuse the | |||
| 3902 | * connection of the API bus). That's because the system bus after all runs as service of the system instance, | |||
| 3903 | * while in the user instance we can assume it's already there. */ | |||
| 3904 | ||||
| 3905 | if (MANAGER_IS_RELOADING(m)((m)->n_reloading > 0)) | |||
| 3906 | return; /* don't check while we are reloading… */ | |||
| 3907 | ||||
| 3908 | if (manager_dbus_is_running(m, false0)) { | |||
| 3909 | (void) bus_init_api(m); | |||
| 3910 | ||||
| 3911 | if (MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) | |||
| 3912 | (void) bus_init_system(m); | |||
| 3913 | } else { | |||
| 3914 | (void) bus_done_api(m); | |||
| 3915 | ||||
| 3916 | if (MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) | |||
| 3917 | (void) bus_done_system(m); | |||
| 3918 | } | |||
| 3919 | } | |||
| 3920 | ||||
| 3921 | static bool_Bool manager_journal_is_running(Manager *m) { | |||
| 3922 | Unit *u; | |||
| 3923 | ||||
| 3924 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 3924, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3925 | ||||
| 3926 | if (m->test_run_flags != 0) | |||
| 3927 | return false0; | |||
| 3928 | ||||
| 3929 | /* If we are the user manager we can safely assume that the journal is up */ | |||
| 3930 | if (!MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) | |||
| 3931 | return true1; | |||
| 3932 | ||||
| 3933 | /* Check that the socket is not only up, but in RUNNING state */ | |||
| 3934 | u = manager_get_unit(m, SPECIAL_JOURNALD_SOCKET"systemd-journald.socket"); | |||
| 3935 | if (!u) | |||
| 3936 | return false0; | |||
| 3937 | if (SOCKET(u)->state != SOCKET_RUNNING) | |||
| 3938 | return false0; | |||
| 3939 | ||||
| 3940 | /* Similar, check if the daemon itself is fully up, too */ | |||
| 3941 | u = manager_get_unit(m, SPECIAL_JOURNALD_SERVICE"systemd-journald.service"); | |||
| 3942 | if (!u) | |||
| 3943 | return false0; | |||
| 3944 | if (!IN_SET(SERVICE(u)->state, SERVICE_RELOAD, SERVICE_RUNNING)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){SERVICE_RELOAD, SERVICE_RUNNING})/sizeof (int)]; switch(SERVICE(u)->state) { case SERVICE_RELOAD: case SERVICE_RUNNING: _found = 1; break; default: break; } _found ; })) | |||
| 3945 | return false0; | |||
| 3946 | ||||
| 3947 | return true1; | |||
| 3948 | } | |||
| 3949 | ||||
| 3950 | void manager_recheck_journal(Manager *m) { | |||
| 3951 | ||||
| 3952 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 3952, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3953 | ||||
| 3954 | /* Don't bother with this unless we are in the special situation of being PID 1 */ | |||
| 3955 | if (getpid_cached() != 1) | |||
| 3956 | return; | |||
| 3957 | ||||
| 3958 | /* Don't check this while we are reloading, things might still change */ | |||
| 3959 | if (MANAGER_IS_RELOADING(m)((m)->n_reloading > 0)) | |||
| 3960 | return; | |||
| 3961 | ||||
| 3962 | /* The journal is fully and entirely up? If so, let's permit logging to it, if that's configured. If the | |||
| 3963 | * journal is down, don't ever log to it, otherwise we might end up deadlocking ourselves as we might trigger | |||
| 3964 | * an activation ourselves we can't fulfill. */ | |||
| 3965 | log_set_prohibit_ipc(!manager_journal_is_running(m)); | |||
| 3966 | log_open(); | |||
| 3967 | } | |||
| 3968 | ||||
| 3969 | void manager_set_show_status(Manager *m, ShowStatus mode) { | |||
| 3970 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 3970, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3971 | assert(IN_SET(mode, SHOW_STATUS_AUTO, SHOW_STATUS_NO, SHOW_STATUS_YES, SHOW_STATUS_TEMPORARY))do { if ((__builtin_expect(!!(!(({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended[20 - sizeof((int[]){SHOW_STATUS_AUTO, SHOW_STATUS_NO, SHOW_STATUS_YES , SHOW_STATUS_TEMPORARY})/sizeof(int)]; switch(mode) { case SHOW_STATUS_AUTO : case SHOW_STATUS_NO: case SHOW_STATUS_YES: case SHOW_STATUS_TEMPORARY : _found = 1; break; default: break; } _found; }))),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("IN_SET(mode, SHOW_STATUS_AUTO, SHOW_STATUS_NO, SHOW_STATUS_YES, SHOW_STATUS_TEMPORARY)" ), "../src/core/manager.c", 3971, __PRETTY_FUNCTION__); } while (0); | |||
| 3972 | ||||
| 3973 | if (!MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) | |||
| 3974 | return; | |||
| 3975 | ||||
| 3976 | if (m->show_status != mode) | |||
| 3977 | log_debug("%s showing of status.",({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3978, __func__, "%s showing of status." , mode == SHOW_STATUS_NO ? "Disabling" : "Enabling") : -abs(_e ); }) | |||
| 3978 | mode == SHOW_STATUS_NO ? "Disabling" : "Enabling")({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 3978, __func__, "%s showing of status." , mode == SHOW_STATUS_NO ? "Disabling" : "Enabling") : -abs(_e ); }); | |||
| 3979 | m->show_status = mode; | |||
| 3980 | ||||
| 3981 | if (mode > 0) | |||
| 3982 | (void) touch("/run/systemd/show-status"); | |||
| 3983 | else | |||
| 3984 | (void) unlink("/run/systemd/show-status"); | |||
| 3985 | } | |||
| 3986 | ||||
| 3987 | static bool_Bool manager_get_show_status(Manager *m, StatusType type) { | |||
| 3988 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 3988, __PRETTY_FUNCTION__ ); } while (0); | |||
| 3989 | ||||
| 3990 | if (!MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) | |||
| 3991 | return false0; | |||
| 3992 | ||||
| 3993 | if (m->no_console_output) | |||
| 3994 | return false0; | |||
| 3995 | ||||
| 3996 | if (!IN_SET(manager_state(m), MANAGER_INITIALIZING, MANAGER_STARTING, MANAGER_STOPPING)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){MANAGER_INITIALIZING, MANAGER_STARTING, MANAGER_STOPPING })/sizeof(int)]; switch(manager_state(m)) { case MANAGER_INITIALIZING : case MANAGER_STARTING: case MANAGER_STOPPING: _found = 1; break ; default: break; } _found; })) | |||
| 3997 | return false0; | |||
| 3998 | ||||
| 3999 | /* If we cannot find out the status properly, just proceed. */ | |||
| 4000 | if (type != STATUS_TYPE_EMERGENCY && manager_check_ask_password(m) > 0) | |||
| 4001 | return false0; | |||
| 4002 | ||||
| 4003 | return m->show_status > 0; | |||
| 4004 | } | |||
| 4005 | ||||
| 4006 | const char *manager_get_confirm_spawn(Manager *m) { | |||
| 4007 | static int last_errno = 0; | |||
| 4008 | const char *vc = m->confirm_spawn; | |||
| 4009 | struct stat st; | |||
| 4010 | int r; | |||
| 4011 | ||||
| 4012 | /* Here's the deal: we want to test the validity of the console but don't want | |||
| 4013 | * PID1 to go through the whole console process which might block. But we also | |||
| 4014 | * want to warn the user only once if something is wrong with the console so we | |||
| 4015 | * cannot do the sanity checks after spawning our children. So here we simply do | |||
| 4016 | * really basic tests to hopefully trap common errors. | |||
| 4017 | * | |||
| 4018 | * If the console suddenly disappear at the time our children will really it | |||
| 4019 | * then they will simply fail to acquire it and a positive answer will be | |||
| 4020 | * assumed. New children will fallback to /dev/console though. | |||
| 4021 | * | |||
| 4022 | * Note: TTYs are devices that can come and go any time, and frequently aren't | |||
| 4023 | * available yet during early boot (consider a USB rs232 dongle...). If for any | |||
| 4024 | * reason the configured console is not ready, we fallback to the default | |||
| 4025 | * console. */ | |||
| 4026 | ||||
| 4027 | if (!vc || path_equal(vc, "/dev/console")) | |||
| 4028 | return vc; | |||
| 4029 | ||||
| 4030 | r = stat(vc, &st); | |||
| 4031 | if (r < 0) | |||
| 4032 | goto fail; | |||
| 4033 | ||||
| 4034 | if (!S_ISCHR(st.st_mode)((((st.st_mode)) & 0170000) == (0020000))) { | |||
| 4035 | errno(*__errno_location ()) = ENOTTY25; | |||
| 4036 | goto fail; | |||
| 4037 | } | |||
| 4038 | ||||
| 4039 | last_errno = 0; | |||
| 4040 | return vc; | |||
| 4041 | fail: | |||
| 4042 | if (last_errno != errno(*__errno_location ())) { | |||
| 4043 | last_errno = errno(*__errno_location ()); | |||
| 4044 | log_warning_errno(errno, "Failed to open %s: %m, using default console", vc)({ int _level = ((4)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 4044, __func__, "Failed to open %s: %m, using default console", vc) : -abs(_e ); }); | |||
| 4045 | } | |||
| 4046 | return "/dev/console"; | |||
| 4047 | } | |||
| 4048 | ||||
| 4049 | void manager_set_first_boot(Manager *m, bool_Bool b) { | |||
| 4050 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 4050, __PRETTY_FUNCTION__ ); } while (0); | |||
| 4051 | ||||
| 4052 | if (!MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) | |||
| 4053 | return; | |||
| 4054 | ||||
| 4055 | if (m->first_boot != (int) b) { | |||
| 4056 | if (b) | |||
| 4057 | (void) touch("/run/systemd/first-boot"); | |||
| 4058 | else | |||
| 4059 | (void) unlink("/run/systemd/first-boot"); | |||
| 4060 | } | |||
| 4061 | ||||
| 4062 | m->first_boot = b; | |||
| 4063 | } | |||
| 4064 | ||||
| 4065 | void manager_disable_confirm_spawn(void) { | |||
| 4066 | (void) touch("/run/systemd/confirm_spawn_disabled"); | |||
| 4067 | } | |||
| 4068 | ||||
| 4069 | bool_Bool manager_is_confirm_spawn_disabled(Manager *m) { | |||
| 4070 | if (!m->confirm_spawn) | |||
| 4071 | return true1; | |||
| 4072 | ||||
| 4073 | return access("/run/systemd/confirm_spawn_disabled", F_OK0) >= 0; | |||
| 4074 | } | |||
| 4075 | ||||
| 4076 | void manager_status_printf(Manager *m, StatusType type, const char *status, const char *format, ...) { | |||
| 4077 | va_list ap; | |||
| 4078 | ||||
| 4079 | /* If m is NULL, assume we're after shutdown and let the messages through. */ | |||
| 4080 | ||||
| 4081 | if (m && !manager_get_show_status(m, type)) | |||
| 4082 | return; | |||
| 4083 | ||||
| 4084 | /* XXX We should totally drop the check for ephemeral here | |||
| 4085 | * and thus effectively make 'Type=idle' pointless. */ | |||
| 4086 | if (type == STATUS_TYPE_EPHEMERAL && m && m->n_on_console > 0) | |||
| 4087 | return; | |||
| 4088 | ||||
| 4089 | va_start(ap, format)__builtin_va_start(ap, format); | |||
| 4090 | status_vprintf(status, true1, type == STATUS_TYPE_EPHEMERAL, format, ap); | |||
| 4091 | va_end(ap)__builtin_va_end(ap); | |||
| 4092 | } | |||
| 4093 | ||||
| 4094 | Set *manager_get_units_requiring_mounts_for(Manager *m, const char *path) { | |||
| 4095 | char p[strlen(path)+1]; | |||
| 4096 | ||||
| 4097 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 4097, __PRETTY_FUNCTION__ ); } while (0); | |||
| 4098 | assert(path)do { if ((__builtin_expect(!!(!(path)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("path"), "../src/core/manager.c", 4098, __PRETTY_FUNCTION__ ); } while (0); | |||
| 4099 | ||||
| 4100 | strcpy(p, path); | |||
| 4101 | path_simplify(p, false0); | |||
| 4102 | ||||
| 4103 | return hashmap_get(m->units_requiring_mounts_for, streq(p, "/")(strcmp((p),("/")) == 0) ? "" : p); | |||
| 4104 | } | |||
| 4105 | ||||
| 4106 | int manager_update_failed_units(Manager *m, Unit *u, bool_Bool failed) { | |||
| 4107 | unsigned size; | |||
| 4108 | int r; | |||
| 4109 | ||||
| 4110 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 4110, __PRETTY_FUNCTION__ ); } while (0); | |||
| 4111 | assert(u->manager == m)do { if ((__builtin_expect(!!(!(u->manager == m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u->manager == m"), "../src/core/manager.c" , 4111, __PRETTY_FUNCTION__); } while (0); | |||
| 4112 | ||||
| 4113 | size = set_size(m->failed_units); | |||
| 4114 | ||||
| 4115 | if (failed) { | |||
| 4116 | r = set_ensure_allocated(&m->failed_units, NULL)internal_set_ensure_allocated(&m->failed_units, ((void *)0) ); | |||
| 4117 | if (r < 0) | |||
| 4118 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/core/manager.c", 4118 , __func__); | |||
| 4119 | ||||
| 4120 | if (set_put(m->failed_units, u) < 0) | |||
| 4121 | return log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/core/manager.c", 4121 , __func__); | |||
| 4122 | } else | |||
| 4123 | (void) set_remove(m->failed_units, u); | |||
| 4124 | ||||
| 4125 | if (set_size(m->failed_units) != size) | |||
| 4126 | bus_manager_send_change_signal(m); | |||
| 4127 | ||||
| 4128 | return 0; | |||
| 4129 | } | |||
| 4130 | ||||
| 4131 | ManagerState manager_state(Manager *m) { | |||
| 4132 | Unit *u; | |||
| 4133 | ||||
| 4134 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 4134, __PRETTY_FUNCTION__ ); } while (0); | |||
| 4135 | ||||
| 4136 | /* Did we ever finish booting? If not then we are still starting up */ | |||
| 4137 | if (!MANAGER_IS_FINISHED(m)(dual_timestamp_is_set((m)->timestamps + MANAGER_TIMESTAMP_FINISH ))) { | |||
| 4138 | ||||
| 4139 | u = manager_get_unit(m, SPECIAL_BASIC_TARGET"basic.target"); | |||
| 4140 | if (!u || !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) | |||
| 4141 | return MANAGER_INITIALIZING; | |||
| 4142 | ||||
| 4143 | return MANAGER_STARTING; | |||
| 4144 | } | |||
| 4145 | ||||
| 4146 | /* Is the special shutdown target active or queued? If so, we are in shutdown state */ | |||
| 4147 | u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET"shutdown.target"); | |||
| 4148 | if (u && unit_active_or_pending(u)) | |||
| 4149 | return MANAGER_STOPPING; | |||
| 4150 | ||||
| 4151 | if (MANAGER_IS_SYSTEM(m)((m)->unit_file_scope == UNIT_FILE_SYSTEM)) { | |||
| 4152 | /* Are the rescue or emergency targets active or queued? If so we are in maintenance state */ | |||
| 4153 | u = manager_get_unit(m, SPECIAL_RESCUE_TARGET"rescue.target"); | |||
| 4154 | if (u && unit_active_or_pending(u)) | |||
| 4155 | return MANAGER_MAINTENANCE; | |||
| 4156 | ||||
| 4157 | u = manager_get_unit(m, SPECIAL_EMERGENCY_TARGET"emergency.target"); | |||
| 4158 | if (u && unit_active_or_pending(u)) | |||
| 4159 | return MANAGER_MAINTENANCE; | |||
| 4160 | } | |||
| 4161 | ||||
| 4162 | /* Are there any failed units? If so, we are in degraded mode */ | |||
| 4163 | if (set_size(m->failed_units) > 0) | |||
| 4164 | return MANAGER_DEGRADED; | |||
| 4165 | ||||
| 4166 | return MANAGER_RUNNING; | |||
| 4167 | } | |||
| 4168 | ||||
| 4169 | #define DESTROY_IPC_FLAG(1U << 31) (UINT32_C(1)1U << 31) | |||
| 4170 | ||||
| 4171 | static void manager_unref_uid_internal( | |||
| 4172 | Manager *m, | |||
| 4173 | Hashmap **uid_refs, | |||
| 4174 | uid_t uid, | |||
| 4175 | bool_Bool destroy_now, | |||
| 4176 | int (*_clean_ipc)(uid_t uid)) { | |||
| 4177 | ||||
| 4178 | uint32_t c, n; | |||
| 4179 | ||||
| 4180 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 4180, __PRETTY_FUNCTION__ ); } while (0); | |||
| 4181 | assert(uid_refs)do { if ((__builtin_expect(!!(!(uid_refs)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("uid_refs"), "../src/core/manager.c", 4181 , __PRETTY_FUNCTION__); } while (0); | |||
| 4182 | assert(uid_is_valid(uid))do { if ((__builtin_expect(!!(!(uid_is_valid(uid))),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("uid_is_valid(uid)"), "../src/core/manager.c" , 4182, __PRETTY_FUNCTION__); } while (0); | |||
| 4183 | assert(_clean_ipc)do { if ((__builtin_expect(!!(!(_clean_ipc)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("_clean_ipc"), "../src/core/manager.c", 4183 , __PRETTY_FUNCTION__); } while (0); | |||
| 4184 | ||||
| 4185 | /* A generic implementation, covering both manager_unref_uid() and manager_unref_gid(), under the assumption | |||
| 4186 | * that uid_t and gid_t are actually defined the same way, with the same validity rules. | |||
| 4187 | * | |||
| 4188 | * We store a hashmap where the UID/GID is they key and the value is a 32bit reference counter, whose highest | |||
| 4189 | * bit is used as flag for marking UIDs/GIDs whose IPC objects to remove when the last reference to the UID/GID | |||
| 4190 | * is dropped. The flag is set to on, once at least one reference from a unit where RemoveIPC= is set is added | |||
| 4191 | * on a UID/GID. It is reset when the UID's/GID's reference counter drops to 0 again. */ | |||
| 4192 | ||||
| 4193 | assert_cc(sizeof(uid_t) == sizeof(gid_t))GCC diagnostic push
; GCC diagnostic ignored "-Wdeclaration-after-statement" ; struct _assert_struct_76 { char x[(sizeof(uid_t) == sizeof (gid_t)) ? 0 : -1]; }; GCC diagnostic pop ; | |||
| 4194 | assert_cc(UID_INVALID == (uid_t) GID_INVALID)GCC diagnostic push
; GCC diagnostic ignored "-Wdeclaration-after-statement" ; struct _assert_struct_77 { char x[(((uid_t) -1) == (uid_t) ((gid_t) -1)) ? 0 : -1]; }; GCC diagnostic pop ; | |||
| 4195 | ||||
| 4196 | if (uid == 0) /* We don't keep track of root, and will never destroy it */ | |||
| 4197 | return; | |||
| 4198 | ||||
| 4199 | c = PTR_TO_UINT32(hashmap_get(*uid_refs, UID_TO_PTR(uid)))((uint32_t) ((uintptr_t) (hashmap_get(*uid_refs, ((void*) ((( uintptr_t) (uid))+1)))))); | |||
| 4200 | ||||
| 4201 | n = c & ~DESTROY_IPC_FLAG(1U << 31); | |||
| 4202 | assert(n > 0)do { if ((__builtin_expect(!!(!(n > 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("n > 0"), "../src/core/manager.c", 4202 , __PRETTY_FUNCTION__); } while (0); | |||
| 4203 | n--; | |||
| 4204 | ||||
| 4205 | if (destroy_now && n == 0) { | |||
| 4206 | hashmap_remove(*uid_refs, UID_TO_PTR(uid)((void*) (((uintptr_t) (uid))+1))); | |||
| 4207 | ||||
| 4208 | if (c & DESTROY_IPC_FLAG(1U << 31)) { | |||
| 4209 | log_debug("%s " UID_FMT " is no longer referenced, cleaning up its IPC.",({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 4211, __func__, "%s " "%" "u" " is no longer referenced, cleaning up its IPC." , _clean_ipc == clean_ipc_by_uid ? "UID" : "GID", uid) : -abs (_e); }) | |||
| 4210 | _clean_ipc == clean_ipc_by_uid ? "UID" : "GID",({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 4211, __func__, "%s " "%" "u" " is no longer referenced, cleaning up its IPC." , _clean_ipc == clean_ipc_by_uid ? "UID" : "GID", uid) : -abs (_e); }) | |||
| 4211 | uid)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 4211, __func__, "%s " "%" "u" " is no longer referenced, cleaning up its IPC." , _clean_ipc == clean_ipc_by_uid ? "UID" : "GID", uid) : -abs (_e); }); | |||
| 4212 | (void) _clean_ipc(uid); | |||
| 4213 | } | |||
| 4214 | } else { | |||
| 4215 | c = n | (c & DESTROY_IPC_FLAG(1U << 31)); | |||
| 4216 | assert_se(hashmap_update(*uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c)) >= 0)do { if ((__builtin_expect(!!(!(hashmap_update(*uid_refs, ((void *) (((uintptr_t) (uid))+1)), ((void *) ((uintptr_t) (c)))) >= 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("hashmap_update(*uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c)) >= 0" ), "../src/core/manager.c", 4216, __PRETTY_FUNCTION__); } while (0); | |||
| 4217 | } | |||
| 4218 | } | |||
| 4219 | ||||
| 4220 | void manager_unref_uid(Manager *m, uid_t uid, bool_Bool destroy_now) { | |||
| 4221 | manager_unref_uid_internal(m, &m->uid_refs, uid, destroy_now, clean_ipc_by_uid); | |||
| 4222 | } | |||
| 4223 | ||||
| 4224 | void manager_unref_gid(Manager *m, gid_t gid, bool_Bool destroy_now) { | |||
| 4225 | manager_unref_uid_internal(m, &m->gid_refs, (uid_t) gid, destroy_now, clean_ipc_by_gid); | |||
| 4226 | } | |||
| 4227 | ||||
| 4228 | static int manager_ref_uid_internal( | |||
| 4229 | Manager *m, | |||
| 4230 | Hashmap **uid_refs, | |||
| 4231 | uid_t uid, | |||
| 4232 | bool_Bool clean_ipc) { | |||
| 4233 | ||||
| 4234 | uint32_t c, n; | |||
| 4235 | int r; | |||
| 4236 | ||||
| 4237 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 4237, __PRETTY_FUNCTION__ ); } while (0); | |||
| 4238 | assert(uid_refs)do { if ((__builtin_expect(!!(!(uid_refs)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("uid_refs"), "../src/core/manager.c", 4238 , __PRETTY_FUNCTION__); } while (0); | |||
| 4239 | assert(uid_is_valid(uid))do { if ((__builtin_expect(!!(!(uid_is_valid(uid))),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("uid_is_valid(uid)"), "../src/core/manager.c" , 4239, __PRETTY_FUNCTION__); } while (0); | |||
| 4240 | ||||
| 4241 | /* A generic implementation, covering both manager_ref_uid() and manager_ref_gid(), under the assumption | |||
| 4242 | * that uid_t and gid_t are actually defined the same way, with the same validity rules. */ | |||
| 4243 | ||||
| 4244 | assert_cc(sizeof(uid_t) == sizeof(gid_t))GCC diagnostic push
; GCC diagnostic ignored "-Wdeclaration-after-statement" ; struct _assert_struct_78 { char x[(sizeof(uid_t) == sizeof (gid_t)) ? 0 : -1]; }; GCC diagnostic pop ; | |||
| 4245 | assert_cc(UID_INVALID == (uid_t) GID_INVALID)GCC diagnostic push
; GCC diagnostic ignored "-Wdeclaration-after-statement" ; struct _assert_struct_79 { char x[(((uid_t) -1) == (uid_t) ((gid_t) -1)) ? 0 : -1]; }; GCC diagnostic pop ; | |||
| 4246 | ||||
| 4247 | if (uid == 0) /* We don't keep track of root, and will never destroy it */ | |||
| 4248 | return 0; | |||
| 4249 | ||||
| 4250 | r = hashmap_ensure_allocated(uid_refs, &trivial_hash_ops)internal_hashmap_ensure_allocated(uid_refs, &trivial_hash_ops ); | |||
| 4251 | if (r < 0) | |||
| 4252 | return r; | |||
| 4253 | ||||
| 4254 | c = PTR_TO_UINT32(hashmap_get(*uid_refs, UID_TO_PTR(uid)))((uint32_t) ((uintptr_t) (hashmap_get(*uid_refs, ((void*) ((( uintptr_t) (uid))+1)))))); | |||
| 4255 | ||||
| 4256 | n = c & ~DESTROY_IPC_FLAG(1U << 31); | |||
| 4257 | n++; | |||
| 4258 | ||||
| 4259 | if (n & DESTROY_IPC_FLAG(1U << 31)) /* check for overflow */ | |||
| 4260 | return -EOVERFLOW75; | |||
| 4261 | ||||
| 4262 | c = n | (c & DESTROY_IPC_FLAG(1U << 31)) | (clean_ipc ? DESTROY_IPC_FLAG(1U << 31) : 0); | |||
| 4263 | ||||
| 4264 | return hashmap_replace(*uid_refs, UID_TO_PTR(uid)((void*) (((uintptr_t) (uid))+1)), UINT32_TO_PTR(c)((void *) ((uintptr_t) (c)))); | |||
| 4265 | } | |||
| 4266 | ||||
| 4267 | int manager_ref_uid(Manager *m, uid_t uid, bool_Bool clean_ipc) { | |||
| 4268 | return manager_ref_uid_internal(m, &m->uid_refs, uid, clean_ipc); | |||
| 4269 | } | |||
| 4270 | ||||
| 4271 | int manager_ref_gid(Manager *m, gid_t gid, bool_Bool clean_ipc) { | |||
| 4272 | return manager_ref_uid_internal(m, &m->gid_refs, (uid_t) gid, clean_ipc); | |||
| 4273 | } | |||
| 4274 | ||||
| 4275 | static void manager_vacuum_uid_refs_internal( | |||
| 4276 | Manager *m, | |||
| 4277 | Hashmap **uid_refs, | |||
| 4278 | int (*_clean_ipc)(uid_t uid)) { | |||
| 4279 | ||||
| 4280 | Iterator i; | |||
| 4281 | void *p, *k; | |||
| 4282 | ||||
| 4283 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 4283, __PRETTY_FUNCTION__ ); } while (0); | |||
| 4284 | assert(uid_refs)do { if ((__builtin_expect(!!(!(uid_refs)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("uid_refs"), "../src/core/manager.c", 4284 , __PRETTY_FUNCTION__); } while (0); | |||
| 4285 | assert(_clean_ipc)do { if ((__builtin_expect(!!(!(_clean_ipc)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("_clean_ipc"), "../src/core/manager.c", 4285 , __PRETTY_FUNCTION__); } while (0); | |||
| 4286 | ||||
| 4287 | HASHMAP_FOREACH_KEY(p, k, *uid_refs, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((*uid_refs), &( i), (void**)&(p), (const void**) &(k)); ) { | |||
| 4288 | uint32_t c, n; | |||
| 4289 | uid_t uid; | |||
| 4290 | ||||
| 4291 | uid = PTR_TO_UID(k)((uid_t) (((uintptr_t) (k))-1)); | |||
| 4292 | c = PTR_TO_UINT32(p)((uint32_t) ((uintptr_t) (p))); | |||
| 4293 | ||||
| 4294 | n = c & ~DESTROY_IPC_FLAG(1U << 31); | |||
| 4295 | if (n > 0) | |||
| 4296 | continue; | |||
| 4297 | ||||
| 4298 | if (c & DESTROY_IPC_FLAG(1U << 31)) { | |||
| 4299 | log_debug("Found unreferenced %s " UID_FMT " after reload/reexec. Cleaning up.",({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 4301, __func__, "Found unreferenced %s " "%" "u" " after reload/reexec. Cleaning up.", _clean_ipc == clean_ipc_by_uid ? "UID" : "GID", uid) : -abs(_e); }) | |||
| 4300 | _clean_ipc == clean_ipc_by_uid ? "UID" : "GID",({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 4301, __func__, "Found unreferenced %s " "%" "u" " after reload/reexec. Cleaning up.", _clean_ipc == clean_ipc_by_uid ? "UID" : "GID", uid) : -abs(_e); }) | |||
| 4301 | uid)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 4301, __func__, "Found unreferenced %s " "%" "u" " after reload/reexec. Cleaning up.", _clean_ipc == clean_ipc_by_uid ? "UID" : "GID", uid) : -abs(_e); }); | |||
| 4302 | (void) _clean_ipc(uid); | |||
| 4303 | } | |||
| 4304 | ||||
| 4305 | assert_se(hashmap_remove(*uid_refs, k) == p)do { if ((__builtin_expect(!!(!(hashmap_remove(*uid_refs, k) == p)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("hashmap_remove(*uid_refs, k) == p" ), "../src/core/manager.c", 4305, __PRETTY_FUNCTION__); } while (0); | |||
| 4306 | } | |||
| 4307 | } | |||
| 4308 | ||||
| 4309 | void manager_vacuum_uid_refs(Manager *m) { | |||
| 4310 | manager_vacuum_uid_refs_internal(m, &m->uid_refs, clean_ipc_by_uid); | |||
| 4311 | } | |||
| 4312 | ||||
| 4313 | void manager_vacuum_gid_refs(Manager *m) { | |||
| 4314 | manager_vacuum_uid_refs_internal(m, &m->gid_refs, clean_ipc_by_gid); | |||
| 4315 | } | |||
| 4316 | ||||
| 4317 | static void manager_serialize_uid_refs_internal( | |||
| 4318 | Manager *m, | |||
| 4319 | FILE *f, | |||
| 4320 | Hashmap **uid_refs, | |||
| 4321 | const char *field_name) { | |||
| 4322 | ||||
| 4323 | Iterator i; | |||
| 4324 | void *p, *k; | |||
| 4325 | ||||
| 4326 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 4326, __PRETTY_FUNCTION__ ); } while (0); | |||
| 4327 | assert(f)do { if ((__builtin_expect(!!(!(f)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("f"), "../src/core/manager.c", 4327, __PRETTY_FUNCTION__ ); } while (0); | |||
| 4328 | assert(uid_refs)do { if ((__builtin_expect(!!(!(uid_refs)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("uid_refs"), "../src/core/manager.c", 4328 , __PRETTY_FUNCTION__); } while (0); | |||
| 4329 | assert(field_name)do { if ((__builtin_expect(!!(!(field_name)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("field_name"), "../src/core/manager.c", 4329 , __PRETTY_FUNCTION__); } while (0); | |||
| 4330 | ||||
| 4331 | /* Serialize the UID reference table. Or actually, just the IPC destruction flag of it, as the actual counter | |||
| 4332 | * of it is better rebuild after a reload/reexec. */ | |||
| 4333 | ||||
| 4334 | HASHMAP_FOREACH_KEY(p, k, *uid_refs, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((*uid_refs), &( i), (void**)&(p), (const void**) &(k)); ) { | |||
| 4335 | uint32_t c; | |||
| 4336 | uid_t uid; | |||
| 4337 | ||||
| 4338 | uid = PTR_TO_UID(k)((uid_t) (((uintptr_t) (k))-1)); | |||
| 4339 | c = PTR_TO_UINT32(p)((uint32_t) ((uintptr_t) (p))); | |||
| 4340 | ||||
| 4341 | if (!(c & DESTROY_IPC_FLAG(1U << 31))) | |||
| 4342 | continue; | |||
| 4343 | ||||
| 4344 | fprintf(f, "%s=" UID_FMT"%" "u" "\n", field_name, uid); | |||
| 4345 | } | |||
| 4346 | } | |||
| 4347 | ||||
| 4348 | void manager_serialize_uid_refs(Manager *m, FILE *f) { | |||
| 4349 | manager_serialize_uid_refs_internal(m, f, &m->uid_refs, "destroy-ipc-uid"); | |||
| 4350 | } | |||
| 4351 | ||||
| 4352 | void manager_serialize_gid_refs(Manager *m, FILE *f) { | |||
| 4353 | manager_serialize_uid_refs_internal(m, f, &m->gid_refs, "destroy-ipc-gid"); | |||
| 4354 | } | |||
| 4355 | ||||
| 4356 | static void manager_deserialize_uid_refs_one_internal( | |||
| 4357 | Manager *m, | |||
| 4358 | Hashmap** uid_refs, | |||
| 4359 | const char *value) { | |||
| 4360 | ||||
| 4361 | uid_t uid; | |||
| 4362 | uint32_t c; | |||
| 4363 | int r; | |||
| 4364 | ||||
| 4365 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 4365, __PRETTY_FUNCTION__ ); } while (0); | |||
| 4366 | assert(uid_refs)do { if ((__builtin_expect(!!(!(uid_refs)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("uid_refs"), "../src/core/manager.c", 4366 , __PRETTY_FUNCTION__); } while (0); | |||
| 4367 | assert(value)do { if ((__builtin_expect(!!(!(value)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("value"), "../src/core/manager.c", 4367, __PRETTY_FUNCTION__); } while (0); | |||
| 4368 | ||||
| 4369 | r = parse_uid(value, &uid); | |||
| 4370 | if (r < 0 || uid == 0) { | |||
| 4371 | log_debug("Unable to parse UID reference serialization")({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 4371, __func__, "Unable to parse UID reference serialization" ) : -abs(_e); }); | |||
| 4372 | return; | |||
| 4373 | } | |||
| 4374 | ||||
| 4375 | r = hashmap_ensure_allocated(uid_refs, &trivial_hash_ops)internal_hashmap_ensure_allocated(uid_refs, &trivial_hash_ops ); | |||
| 4376 | if (r < 0) { | |||
| 4377 | log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/core/manager.c", 4377 , __func__); | |||
| 4378 | return; | |||
| 4379 | } | |||
| 4380 | ||||
| 4381 | c = PTR_TO_UINT32(hashmap_get(*uid_refs, UID_TO_PTR(uid)))((uint32_t) ((uintptr_t) (hashmap_get(*uid_refs, ((void*) ((( uintptr_t) (uid))+1)))))); | |||
| 4382 | if (c & DESTROY_IPC_FLAG(1U << 31)) | |||
| 4383 | return; | |||
| 4384 | ||||
| 4385 | c |= DESTROY_IPC_FLAG(1U << 31); | |||
| 4386 | ||||
| 4387 | r = hashmap_replace(*uid_refs, UID_TO_PTR(uid)((void*) (((uintptr_t) (uid))+1)), UINT32_TO_PTR(c)((void *) ((uintptr_t) (c)))); | |||
| 4388 | if (r < 0) { | |||
| 4389 | log_debug("Failed to add UID reference entry")({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 4389, __func__, "Failed to add UID reference entry" ) : -abs(_e); }); | |||
| 4390 | return; | |||
| 4391 | } | |||
| 4392 | } | |||
| 4393 | ||||
| 4394 | void manager_deserialize_uid_refs_one(Manager *m, const char *value) { | |||
| 4395 | manager_deserialize_uid_refs_one_internal(m, &m->uid_refs, value); | |||
| 4396 | } | |||
| 4397 | ||||
| 4398 | void manager_deserialize_gid_refs_one(Manager *m, const char *value) { | |||
| 4399 | manager_deserialize_uid_refs_one_internal(m, &m->gid_refs, value); | |||
| 4400 | } | |||
| 4401 | ||||
| 4402 | int manager_dispatch_user_lookup_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { | |||
| 4403 | struct buffer { | |||
| 4404 | uid_t uid; | |||
| 4405 | gid_t gid; | |||
| 4406 | char unit_name[UNIT_NAME_MAX256+1]; | |||
| 4407 | } _packed___attribute__ ((packed)) buffer; | |||
| 4408 | ||||
| 4409 | Manager *m = userdata; | |||
| 4410 | ssize_t l; | |||
| 4411 | size_t n; | |||
| 4412 | Unit *u; | |||
| 4413 | ||||
| 4414 | assert_se(source)do { if ((__builtin_expect(!!(!(source)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("source"), "../src/core/manager.c", 4414 , __PRETTY_FUNCTION__); } while (0); | |||
| 4415 | assert_se(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 4415, __PRETTY_FUNCTION__ ); } while (0); | |||
| 4416 | ||||
| 4417 | /* Invoked whenever a child process succeeded resolving its user/group to use and sent us the resulting UID/GID | |||
| 4418 | * in a datagram. We parse the datagram here and pass it off to the unit, so that it can add a reference to the | |||
| 4419 | * UID/GID so that it can destroy the UID/GID's IPC objects when the reference counter drops to 0. */ | |||
| 4420 | ||||
| 4421 | l = recv(fd, &buffer, sizeof(buffer), MSG_DONTWAITMSG_DONTWAIT); | |||
| 4422 | if (l < 0) { | |||
| 4423 | if (IN_SET(errno, EINTR, EAGAIN)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){4, 11})/sizeof(int)]; switch((*__errno_location ())) { case 4: case 11: _found = 1; break; default: break; } _found; })) | |||
| 4424 | return 0; | |||
| 4425 | ||||
| 4426 | return log_error_errno(errno, "Failed to read from user lookup fd: %m")({ int _level = ((3)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/core/manager.c", 4426, __func__, "Failed to read from user lookup fd: %m") : -abs(_e); }); | |||
| 4427 | } | |||
| 4428 | ||||
| 4429 | if ((size_t) l <= offsetof(struct buffer, unit_name)__builtin_offsetof(struct buffer, unit_name)) { | |||
| 4430 | log_warning("Received too short user lookup message, ignoring.")({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 4430, __func__, "Received too short user lookup message, ignoring." ) : -abs(_e); }); | |||
| 4431 | return 0; | |||
| 4432 | } | |||
| 4433 | ||||
| 4434 | if ((size_t) l > offsetof(struct buffer, unit_name)__builtin_offsetof(struct buffer, unit_name) + UNIT_NAME_MAX256) { | |||
| 4435 | log_warning("Received too long user lookup message, ignoring.")({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 4435, __func__, "Received too long user lookup message, ignoring." ) : -abs(_e); }); | |||
| 4436 | return 0; | |||
| 4437 | } | |||
| 4438 | ||||
| 4439 | if (!uid_is_valid(buffer.uid) && !gid_is_valid(buffer.gid)) { | |||
| 4440 | log_warning("Got user lookup message with invalid UID/GID pair, ignoring.")({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 4440, __func__, "Got user lookup message with invalid UID/GID pair, ignoring." ) : -abs(_e); }); | |||
| 4441 | return 0; | |||
| 4442 | } | |||
| 4443 | ||||
| 4444 | n = (size_t) l - offsetof(struct buffer, unit_name)__builtin_offsetof(struct buffer, unit_name); | |||
| 4445 | if (memchr(buffer.unit_name, 0, n)) { | |||
| 4446 | log_warning("Received lookup message with embedded NUL character, ignoring.")({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 4446, __func__, "Received lookup message with embedded NUL character, ignoring." ) : -abs(_e); }); | |||
| 4447 | return 0; | |||
| 4448 | } | |||
| 4449 | ||||
| 4450 | buffer.unit_name[n] = 0; | |||
| 4451 | u = manager_get_unit(m, buffer.unit_name); | |||
| 4452 | if (!u) { | |||
| 4453 | log_debug("Got user lookup message but unit doesn't exist, ignoring.")({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 4453, __func__, "Got user lookup message but unit doesn't exist, ignoring." ) : -abs(_e); }); | |||
| 4454 | return 0; | |||
| 4455 | } | |||
| 4456 | ||||
| 4457 | log_unit_debug(u, "User lookup succeeded: uid=" UID_FMT " gid=" GID_FMT, buffer.uid, buffer.gid)({ const Unit *_u = (u); _u ? log_object_internal(7, 0, "../src/core/manager.c" , 4457, __func__, _u->manager->unit_log_field, _u->id , _u->manager->invocation_log_field, _u->invocation_id_string , "User lookup succeeded: uid=" "%" "u" " gid=" "%" "u", buffer .uid, buffer.gid) : log_internal_realm(((LOG_REALM_SYSTEMD) << 10 | ((7))), 0, "../src/core/manager.c", 4457, __func__, "User lookup succeeded: uid=" "%" "u" " gid=" "%" "u", buffer.uid, buffer.gid); }); | |||
| 4458 | ||||
| 4459 | unit_notify_user_lookup(u, buffer.uid, buffer.gid); | |||
| 4460 | return 0; | |||
| 4461 | } | |||
| 4462 | ||||
| 4463 | char *manager_taint_string(Manager *m) { | |||
| 4464 | _cleanup_free___attribute__((cleanup(freep))) char *destination = NULL((void*)0), *overflowuid = NULL((void*)0), *overflowgid = NULL((void*)0); | |||
| 4465 | char *buf, *e; | |||
| 4466 | int r; | |||
| 4467 | ||||
| 4468 | /* Returns a "taint string", e.g. "local-hwclock:var-run-bad". | |||
| 4469 | * Only things that are detected at runtime should be tagged | |||
| 4470 | * here. For stuff that is set during compilation, emit a warning | |||
| 4471 | * in the configuration phase. */ | |||
| 4472 | ||||
| 4473 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 4473, __PRETTY_FUNCTION__ ); } while (0); | |||
| 4474 | ||||
| 4475 | buf = new(char, sizeof("split-usr:"((char*) malloc_multiply(sizeof(char), (sizeof("split-usr:" "cgroups-missing:" "local-hwclock:" "var-run-bad:" "overflowuid-not-65534:" "overflowgid-not-65534:" )))) | |||
| 4476 | "cgroups-missing:"((char*) malloc_multiply(sizeof(char), (sizeof("split-usr:" "cgroups-missing:" "local-hwclock:" "var-run-bad:" "overflowuid-not-65534:" "overflowgid-not-65534:" )))) | |||
| 4477 | "local-hwclock:"((char*) malloc_multiply(sizeof(char), (sizeof("split-usr:" "cgroups-missing:" "local-hwclock:" "var-run-bad:" "overflowuid-not-65534:" "overflowgid-not-65534:" )))) | |||
| 4478 | "var-run-bad:"((char*) malloc_multiply(sizeof(char), (sizeof("split-usr:" "cgroups-missing:" "local-hwclock:" "var-run-bad:" "overflowuid-not-65534:" "overflowgid-not-65534:" )))) | |||
| 4479 | "overflowuid-not-65534:"((char*) malloc_multiply(sizeof(char), (sizeof("split-usr:" "cgroups-missing:" "local-hwclock:" "var-run-bad:" "overflowuid-not-65534:" "overflowgid-not-65534:" )))) | |||
| 4480 | "overflowgid-not-65534:"))((char*) malloc_multiply(sizeof(char), (sizeof("split-usr:" "cgroups-missing:" "local-hwclock:" "var-run-bad:" "overflowuid-not-65534:" "overflowgid-not-65534:" )))); | |||
| 4481 | if (!buf) | |||
| 4482 | return NULL((void*)0); | |||
| 4483 | ||||
| 4484 | e = buf; | |||
| 4485 | buf[0] = 0; | |||
| 4486 | ||||
| 4487 | if (m->taint_usr) | |||
| 4488 | e = stpcpy(e, "split-usr:"); | |||
| 4489 | ||||
| 4490 | if (access("/proc/cgroups", F_OK0) < 0) | |||
| 4491 | e = stpcpy(e, "cgroups-missing:"); | |||
| 4492 | ||||
| 4493 | if (clock_is_localtime(NULL((void*)0)) > 0) | |||
| 4494 | e = stpcpy(e, "local-hwclock:"); | |||
| 4495 | ||||
| 4496 | r = readlink_malloc("/var/run", &destination); | |||
| 4497 | if (r < 0 || !PATH_IN_SET(destination, "../run", "/run")({ char **s; _Bool _found = 0; for ((s) = (((char**) ((const char *[]) { "../run", "/run", ((void*)0) }))); (s) && *(s) ; (s)++) if (path_equal(destination, *s)) { _found = 1; break ; } _found; })) | |||
| 4498 | e = stpcpy(e, "var-run-bad:"); | |||
| 4499 | ||||
| 4500 | r = read_one_line_file("/proc/sys/kernel/overflowuid", &overflowuid); | |||
| 4501 | if (r >= 0 && !streq(overflowuid, "65534")(strcmp((overflowuid),("65534")) == 0)) | |||
| 4502 | e = stpcpy(e, "overflowuid-not-65534:"); | |||
| 4503 | ||||
| 4504 | r = read_one_line_file("/proc/sys/kernel/overflowgid", &overflowgid); | |||
| 4505 | if (r >= 0 && !streq(overflowgid, "65534")(strcmp((overflowgid),("65534")) == 0)) | |||
| 4506 | e = stpcpy(e, "overflowgid-not-65534:"); | |||
| 4507 | ||||
| 4508 | /* remove the last ':' */ | |||
| 4509 | if (e != buf) | |||
| 4510 | e[-1] = 0; | |||
| 4511 | ||||
| 4512 | return buf; | |||
| 4513 | } | |||
| 4514 | ||||
| 4515 | void manager_ref_console(Manager *m) { | |||
| 4516 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 4516, __PRETTY_FUNCTION__ ); } while (0); | |||
| 4517 | ||||
| 4518 | m->n_on_console++; | |||
| 4519 | } | |||
| 4520 | ||||
| 4521 | void manager_unref_console(Manager *m) { | |||
| 4522 | ||||
| 4523 | assert(m->n_on_console > 0)do { if ((__builtin_expect(!!(!(m->n_on_console > 0)),0 ))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("m->n_on_console > 0" ), "../src/core/manager.c", 4523, __PRETTY_FUNCTION__); } while (0); | |||
| 4524 | m->n_on_console--; | |||
| 4525 | ||||
| 4526 | if (m->n_on_console == 0) | |||
| 4527 | m->no_console_output = false0; /* unset no_console_output flag, since the console is definitely free now */ | |||
| 4528 | } | |||
| 4529 | ||||
| 4530 | void manager_override_log_level(Manager *m, int level) { | |||
| 4531 | _cleanup_free___attribute__((cleanup(freep))) char *s = NULL((void*)0); | |||
| 4532 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 4532, __PRETTY_FUNCTION__ ); } while (0); | |||
| 4533 | ||||
| 4534 | if (!m->log_level_overridden) { | |||
| 4535 | m->original_log_level = log_get_max_level()log_get_max_level_realm(LOG_REALM_SYSTEMD); | |||
| 4536 | m->log_level_overridden = true1; | |||
| 4537 | } | |||
| 4538 | ||||
| 4539 | (void) log_level_to_string_alloc(level, &s); | |||
| 4540 | log_info("Setting log level to %s.", strna(s))({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 4540, __func__, "Setting log level to %s." , strna(s)) : -abs(_e); }); | |||
| 4541 | ||||
| 4542 | log_set_max_level(level)log_set_max_level_realm(LOG_REALM_SYSTEMD, (level)); | |||
| 4543 | } | |||
| 4544 | ||||
| 4545 | void manager_restore_original_log_level(Manager *m) { | |||
| 4546 | _cleanup_free___attribute__((cleanup(freep))) char *s = NULL((void*)0); | |||
| 4547 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 4547, __PRETTY_FUNCTION__ ); } while (0); | |||
| 4548 | ||||
| 4549 | if (!m->log_level_overridden) | |||
| 4550 | return; | |||
| 4551 | ||||
| 4552 | (void) log_level_to_string_alloc(m->original_log_level, &s); | |||
| 4553 | log_info("Restoring log level to original (%s).", strna(s))({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 4553, __func__, "Restoring log level to original (%s)." , strna(s)) : -abs(_e); }); | |||
| 4554 | ||||
| 4555 | log_set_max_level(m->original_log_level)log_set_max_level_realm(LOG_REALM_SYSTEMD, (m->original_log_level )); | |||
| 4556 | m->log_level_overridden = false0; | |||
| 4557 | } | |||
| 4558 | ||||
| 4559 | void manager_override_log_target(Manager *m, LogTarget target) { | |||
| 4560 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 4560, __PRETTY_FUNCTION__ ); } while (0); | |||
| 4561 | ||||
| 4562 | if (!m->log_target_overridden) { | |||
| 4563 | m->original_log_target = log_get_target(); | |||
| 4564 | m->log_target_overridden = true1; | |||
| 4565 | } | |||
| 4566 | ||||
| 4567 | log_info("Setting log target to %s.", log_target_to_string(target))({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 4567, __func__, "Setting log target to %s." , log_target_to_string(target)) : -abs(_e); }); | |||
| 4568 | log_set_target(target); | |||
| 4569 | } | |||
| 4570 | ||||
| 4571 | void manager_restore_original_log_target(Manager *m) { | |||
| 4572 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/core/manager.c", 4572, __PRETTY_FUNCTION__ ); } while (0); | |||
| 4573 | ||||
| 4574 | if (!m->log_target_overridden) | |||
| 4575 | return; | |||
| 4576 | ||||
| 4577 | log_info("Restoring log target to original %s.", log_target_to_string(m->original_log_target))({ int _level = (((6))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/core/manager.c", 4577, __func__, "Restoring log target to original %s." , log_target_to_string(m->original_log_target)) : -abs(_e) ; }); | |||
| 4578 | ||||
| 4579 | log_set_target(m->original_log_target); | |||
| 4580 | m->log_target_overridden = false0; | |||
| 4581 | } | |||
| 4582 | ||||
| 4583 | static const char *const manager_state_table[_MANAGER_STATE_MAX] = { | |||
| 4584 | [MANAGER_INITIALIZING] = "initializing", | |||
| 4585 | [MANAGER_STARTING] = "starting", | |||
| 4586 | [MANAGER_RUNNING] = "running", | |||
| 4587 | [MANAGER_DEGRADED] = "degraded", | |||
| 4588 | [MANAGER_MAINTENANCE] = "maintenance", | |||
| 4589 | [MANAGER_STOPPING] = "stopping", | |||
| 4590 | }; | |||
| 4591 | ||||
| 4592 | DEFINE_STRING_TABLE_LOOKUP(manager_state, ManagerState)const char *manager_state_to_string(ManagerState i) { if (i < 0 || i >= (ManagerState) __extension__ (__builtin_choose_expr ( !__builtin_types_compatible_p(typeof(manager_state_table), typeof (&*(manager_state_table))), sizeof(manager_state_table)/sizeof ((manager_state_table)[0]), ((void)0)))) return ((void*)0); return manager_state_table[i]; } ManagerState manager_state_from_string (const char *s) { return (ManagerState) string_table_lookup(manager_state_table , __extension__ (__builtin_choose_expr( !__builtin_types_compatible_p (typeof(manager_state_table), typeof(&*(manager_state_table ))), sizeof(manager_state_table)/sizeof((manager_state_table) [0]), ((void)0))), s); }; | |||
| 4593 | ||||
| 4594 | static const char *const manager_timestamp_table[_MANAGER_TIMESTAMP_MAX] = { | |||
| 4595 | [MANAGER_TIMESTAMP_FIRMWARE] = "firmware", | |||
| 4596 | [MANAGER_TIMESTAMP_LOADER] = "loader", | |||
| 4597 | [MANAGER_TIMESTAMP_KERNEL] = "kernel", | |||
| 4598 | [MANAGER_TIMESTAMP_INITRD] = "initrd", | |||
| 4599 | [MANAGER_TIMESTAMP_USERSPACE] = "userspace", | |||
| 4600 | [MANAGER_TIMESTAMP_FINISH] = "finish", | |||
| 4601 | [MANAGER_TIMESTAMP_SECURITY_START] = "security-start", | |||
| 4602 | [MANAGER_TIMESTAMP_SECURITY_FINISH] = "security-finish", | |||
| 4603 | [MANAGER_TIMESTAMP_GENERATORS_START] = "generators-start", | |||
| 4604 | [MANAGER_TIMESTAMP_GENERATORS_FINISH] = "generators-finish", | |||
| 4605 | [MANAGER_TIMESTAMP_UNITS_LOAD_START] = "units-load-start", | |||
| 4606 | [MANAGER_TIMESTAMP_UNITS_LOAD_FINISH] = "units-load-finish", | |||
| 4607 | }; | |||
| 4608 | ||||
| 4609 | DEFINE_STRING_TABLE_LOOKUP(manager_timestamp, ManagerTimestamp)const char *manager_timestamp_to_string(ManagerTimestamp i) { if (i < 0 || i >= (ManagerTimestamp) __extension__ (__builtin_choose_expr ( !__builtin_types_compatible_p(typeof(manager_timestamp_table ), typeof(&*(manager_timestamp_table))), sizeof(manager_timestamp_table )/sizeof((manager_timestamp_table)[0]), ((void)0)))) return ( (void*)0); return manager_timestamp_table[i]; } ManagerTimestamp manager_timestamp_from_string(const char *s) { return (ManagerTimestamp ) string_table_lookup(manager_timestamp_table, __extension__ ( __builtin_choose_expr( !__builtin_types_compatible_p(typeof(manager_timestamp_table ), typeof(&*(manager_timestamp_table))), sizeof(manager_timestamp_table )/sizeof((manager_timestamp_table)[0]), ((void)0))), s); }; |