| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/audit.h> /* IWYU pragma: keep */ |
| 4 |
#include <math.h> |
| 5 |
#include <sys/stat.h> |
| 6 |
#include <unistd.h> |
| 7 |
|
| 8 |
#include "sd-bus.h" |
| 9 |
#include "sd-json.h" |
| 10 |
#include "sd-messages.h" |
| 11 |
|
| 12 |
#include "alloc-util.h" |
| 13 |
#include "async.h" |
| 14 |
#include "bus-common-errors.h" |
| 15 |
#include "bus-error.h" |
| 16 |
#include "bus-util.h" |
| 17 |
#include "cgroup.h" |
| 18 |
#include "chase.h" |
| 19 |
#include "daemon-util.h" |
| 20 |
#include "dbus-service.h" |
| 21 |
#include "dbus-unit.h" |
| 22 |
#include "devnum-util.h" |
| 23 |
#include "env-util.h" |
| 24 |
#include "errno-util.h" |
| 25 |
#include "escape.h" |
| 26 |
#include "execute.h" |
| 27 |
#include "exec-credential.h" |
| 28 |
#include "exit-status.h" |
| 29 |
#include "extract-word.h" |
| 30 |
#include "fd-util.h" |
| 31 |
#include "fdset.h" |
| 32 |
#include "fileio.h" |
| 33 |
#include "format-util.h" |
| 34 |
#include "glyph-util.h" |
| 35 |
#include "image-policy.h" |
| 36 |
#include "log.h" |
| 37 |
#include "manager.h" |
| 38 |
#include "memfd-util.h" |
| 39 |
#include "mount-util.h" |
| 40 |
#include "namespace.h" |
| 41 |
#include "open-file.h" |
| 42 |
#include "parse-util.h" |
| 43 |
#include "path-util.h" |
| 44 |
#include "path.h" |
| 45 |
#include "pidfd-util.h" |
| 46 |
#include "process-util.h" |
| 47 |
#include "random-util.h" |
| 48 |
#include "selinux-util.h" |
| 49 |
#include "serialize.h" |
| 50 |
#include "service.h" |
| 51 |
#include "signal-util.h" |
| 52 |
#include "socket.h" |
| 53 |
#include "special.h" |
| 54 |
#include "stat-util.h" |
| 55 |
#include "string-table.h" |
| 56 |
#include "string-util.h" |
| 57 |
#include "strv.h" |
| 58 |
#include "transaction.h" |
| 59 |
#include "unit.h" |
| 60 |
#include "unit-name.h" |
| 61 |
#include "utf8.h" |
| 62 |
|
| 63 |
#define STATUS_TEXT_MAX (16U*1024U) |
| 64 |
|
| 65 |
#define service_spawn(...) service_spawn_internal(__func__, __VA_ARGS__) |
| 66 |
|
| 67 |
#define SERVICE_FD_STORE_POPULATED(s) (!!(s)->fd_store) |
| 68 |
|
| 69 |
static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = { |
| 70 |
[SERVICE_DEAD] = UNIT_INACTIVE, |
| 71 |
[SERVICE_CONDITION] = UNIT_ACTIVATING, |
| 72 |
[SERVICE_START_PRE] = UNIT_ACTIVATING, |
| 73 |
[SERVICE_START] = UNIT_ACTIVATING, |
| 74 |
[SERVICE_START_POST] = UNIT_ACTIVATING, |
| 75 |
[SERVICE_RUNNING] = UNIT_ACTIVE, |
| 76 |
[SERVICE_EXITED] = UNIT_ACTIVE, |
| 77 |
[SERVICE_REFRESH_EXTENSIONS] = UNIT_REFRESHING, |
| 78 |
[SERVICE_REFRESH_CREDENTIALS] = UNIT_REFRESHING, |
| 79 |
[SERVICE_RELOAD] = UNIT_RELOADING, |
| 80 |
[SERVICE_RELOAD_SIGNAL] = UNIT_RELOADING, |
| 81 |
[SERVICE_RELOAD_NOTIFY] = UNIT_RELOADING, |
| 82 |
[SERVICE_RELOAD_POST] = UNIT_RELOADING, |
| 83 |
[SERVICE_MOUNTING] = UNIT_REFRESHING, |
| 84 |
[SERVICE_STOP] = UNIT_DEACTIVATING, |
| 85 |
[SERVICE_STOP_WATCHDOG] = UNIT_DEACTIVATING, |
| 86 |
[SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, |
| 87 |
[SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, |
| 88 |
[SERVICE_STOP_POST] = UNIT_DEACTIVATING, |
| 89 |
[SERVICE_FINAL_WATCHDOG] = UNIT_DEACTIVATING, |
| 90 |
[SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, |
| 91 |
[SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, |
| 92 |
[SERVICE_FAILED] = UNIT_FAILED, |
| 93 |
[SERVICE_DEAD_BEFORE_AUTO_RESTART] = UNIT_INACTIVE, |
| 94 |
[SERVICE_FAILED_BEFORE_AUTO_RESTART] = UNIT_FAILED, |
| 95 |
[SERVICE_DEAD_RESOURCES_PINNED] = UNIT_INACTIVE, |
| 96 |
[SERVICE_AUTO_RESTART] = UNIT_ACTIVATING, |
| 97 |
[SERVICE_AUTO_RESTART_QUEUED] = UNIT_ACTIVATING, |
| 98 |
[SERVICE_CLEANING] = UNIT_MAINTENANCE, |
| 99 |
}; |
| 100 |
|
| 101 |
/* For Type=idle we never want to delay any other jobs, hence we |
| 102 |
* consider idle jobs active as soon as we start working on them */ |
| 103 |
static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = { |
| 104 |
[SERVICE_DEAD] = UNIT_INACTIVE, |
| 105 |
[SERVICE_CONDITION] = UNIT_ACTIVE, |
| 106 |
[SERVICE_START_PRE] = UNIT_ACTIVE, |
| 107 |
[SERVICE_START] = UNIT_ACTIVE, |
| 108 |
[SERVICE_START_POST] = UNIT_ACTIVE, |
| 109 |
[SERVICE_RUNNING] = UNIT_ACTIVE, |
| 110 |
[SERVICE_EXITED] = UNIT_ACTIVE, |
| 111 |
[SERVICE_REFRESH_EXTENSIONS] = UNIT_REFRESHING, |
| 112 |
[SERVICE_REFRESH_CREDENTIALS] = UNIT_REFRESHING, |
| 113 |
[SERVICE_RELOAD] = UNIT_RELOADING, |
| 114 |
[SERVICE_RELOAD_SIGNAL] = UNIT_RELOADING, |
| 115 |
[SERVICE_RELOAD_NOTIFY] = UNIT_RELOADING, |
| 116 |
[SERVICE_RELOAD_POST] = UNIT_RELOADING, |
| 117 |
[SERVICE_MOUNTING] = UNIT_REFRESHING, |
| 118 |
[SERVICE_STOP] = UNIT_DEACTIVATING, |
| 119 |
[SERVICE_STOP_WATCHDOG] = UNIT_DEACTIVATING, |
| 120 |
[SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, |
| 121 |
[SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, |
| 122 |
[SERVICE_STOP_POST] = UNIT_DEACTIVATING, |
| 123 |
[SERVICE_FINAL_WATCHDOG] = UNIT_DEACTIVATING, |
| 124 |
[SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, |
| 125 |
[SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, |
| 126 |
[SERVICE_FAILED] = UNIT_FAILED, |
| 127 |
[SERVICE_DEAD_BEFORE_AUTO_RESTART] = UNIT_INACTIVE, |
| 128 |
[SERVICE_FAILED_BEFORE_AUTO_RESTART] = UNIT_FAILED, |
| 129 |
[SERVICE_DEAD_RESOURCES_PINNED] = UNIT_INACTIVE, |
| 130 |
[SERVICE_AUTO_RESTART] = UNIT_ACTIVATING, |
| 131 |
[SERVICE_AUTO_RESTART_QUEUED] = UNIT_ACTIVATING, |
| 132 |
[SERVICE_CLEANING] = UNIT_MAINTENANCE, |
| 133 |
}; |
| 134 |
|
| 135 |
static int service_dispatch_inotify_io(sd_event_source *source, int fd, uint32_t events, void *userdata); |
| 136 |
static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); |
| 137 |
static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata); |
| 138 |
static int service_dispatch_exec_io(sd_event_source *source, int fd, uint32_t events, void *userdata); |
| 139 |
|
| 140 |
static void service_enter_signal(Service *s, ServiceState state, ServiceResult f); |
| 141 |
|
| 142 |
static void service_reload_finish(Service *s, ServiceResult f); |
| 143 |
static void service_enter_reload_by_notify(Service *s); |
| 144 |
|
| 145 |
static bool service_can_reload_extensions(Service *s, bool warn); |
| 146 |
|
| 147 |
static void service_set_state(Service *s, ServiceState state); |
| 148 |
|
| 149 |
static bool SERVICE_STATE_WITH_MAIN_PROCESS(ServiceState state) { |
| 150 |
return IN_SET(state, |
| 151 |
SERVICE_START, SERVICE_START_POST, |
| 152 |
SERVICE_RUNNING, |
| 153 |
SERVICE_REFRESH_EXTENSIONS, SERVICE_REFRESH_CREDENTIALS, |
| 154 |
SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, SERVICE_RELOAD_POST, |
| 155 |
SERVICE_MOUNTING, |
| 156 |
SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
| 157 |
SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL); |
| 158 |
} |
| 159 |
|
| 160 |
static bool SERVICE_STATE_WITH_CONTROL_PROCESS(ServiceState state) { |
| 161 |
return IN_SET(state, |
| 162 |
SERVICE_CONDITION, |
| 163 |
SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, |
| 164 |
SERVICE_REFRESH_EXTENSIONS, SERVICE_REFRESH_CREDENTIALS, SERVICE_RELOAD, SERVICE_RELOAD_POST, |
| 165 |
SERVICE_MOUNTING, |
| 166 |
SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
| 167 |
SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, |
| 168 |
SERVICE_CLEANING); |
| 169 |
} |
| 170 |
|
| 171 |
static bool SERVICE_STATE_WITH_WATCHDOG(ServiceState state) { |
| 172 |
return IN_SET(state, |
| 173 |
SERVICE_START_POST, |
| 174 |
SERVICE_RUNNING, |
| 175 |
SERVICE_REFRESH_EXTENSIONS, SERVICE_REFRESH_CREDENTIALS, |
| 176 |
SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, SERVICE_RELOAD_POST, |
| 177 |
SERVICE_MOUNTING); |
| 178 |
} |
| 179 |
|
| 180 |
static void service_init(Unit *u) { |
| 181 |
Service *s = SERVICE(u); |
| 182 |
|
| 183 |
assert(u); |
| 184 |
assert(u->load_state == UNIT_STUB); |
| 185 |
|
| 186 |
s->timeout_start_usec = u->manager->defaults.timeout_start_usec; |
| 187 |
s->timeout_stop_usec = u->manager->defaults.timeout_stop_usec; |
| 188 |
s->timeout_abort_usec = u->manager->defaults.timeout_abort_usec; |
| 189 |
s->timeout_abort_set = u->manager->defaults.timeout_abort_set; |
| 190 |
s->restart_usec = u->manager->defaults.restart_usec; |
| 191 |
s->restart_max_delay_usec = USEC_INFINITY; |
| 192 |
s->runtime_max_usec = USEC_INFINITY; |
| 193 |
s->type = _SERVICE_TYPE_INVALID; |
| 194 |
s->socket_fd = -EBADF; |
| 195 |
s->stdin_fd = s->stdout_fd = s->stderr_fd = -EBADF; |
| 196 |
s->root_directory_fd = -EBADF; |
| 197 |
s->guess_main_pid = true; |
| 198 |
s->main_pid = PIDREF_NULL; |
| 199 |
s->control_pid = PIDREF_NULL; |
| 200 |
s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
| 201 |
|
| 202 |
s->exec_context.keyring_mode = MANAGER_IS_SYSTEM(u->manager) ? |
| 203 |
EXEC_KEYRING_PRIVATE : EXEC_KEYRING_INHERIT; |
| 204 |
|
| 205 |
s->notify_access_override = _NOTIFY_ACCESS_INVALID; |
| 206 |
s->notify_state = _NOTIFY_STATE_INVALID; |
| 207 |
|
| 208 |
s->watchdog_original_usec = USEC_INFINITY; |
| 209 |
|
| 210 |
s->oom_policy = _OOM_POLICY_INVALID; |
| 211 |
s->reload_begin_usec = USEC_INFINITY; |
| 212 |
s->reload_signal = SIGHUP; |
| 213 |
|
| 214 |
s->fd_store_preserve_mode = EXEC_PRESERVE_RESTART; |
| 215 |
} |
| 216 |
|
| 217 |
static void service_unwatch_control_pid(Service *s) { |
| 218 |
assert(s); |
| 219 |
unit_unwatch_pidref_done(UNIT(s), &s->control_pid); |
| 220 |
} |
| 221 |
|
| 222 |
static void service_unwatch_main_pid(Service *s) { |
| 223 |
assert(s); |
| 224 |
unit_unwatch_pidref_done(UNIT(s), &s->main_pid); |
| 225 |
} |
| 226 |
|
| 227 |
static void service_unwatch_pid_file(Service *s) { |
| 228 |
assert(s); |
| 229 |
|
| 230 |
if (!s->pid_file_pathspec) |
| 231 |
return; |
| 232 |
|
| 233 |
log_unit_debug(UNIT(s), "Stopping watch for PID file %s", s->pid_file_pathspec->path); |
| 234 |
path_spec_unwatch(s->pid_file_pathspec); |
| 235 |
path_spec_done(s->pid_file_pathspec); |
| 236 |
s->pid_file_pathspec = mfree(s->pid_file_pathspec); |
| 237 |
} |
| 238 |
|
| 239 |
static int service_set_main_pidref(Service *s, PidRef pidref_consume, const dual_timestamp *start_timestamp) { |
| 240 |
_cleanup_(pidref_done) PidRef pidref = pidref_consume; |
| 241 |
int r; |
| 242 |
|
| 243 |
assert(s); |
| 244 |
|
| 245 |
/* Takes ownership of the specified pidref on both success and failure. */ |
| 246 |
|
| 247 |
if (!pidref_is_set(&pidref)) |
| 248 |
return -ESRCH; |
| 249 |
|
| 250 |
if (pidref.pid <= 1) |
| 251 |
return -EINVAL; |
| 252 |
|
| 253 |
if (pidref_is_self(&pidref)) |
| 254 |
return -EINVAL; |
| 255 |
|
| 256 |
if (s->main_pid_known && pidref_equal(&s->main_pid, &pidref)) |
| 257 |
return 0; |
| 258 |
|
| 259 |
if (!pidref_equal(&s->main_pid, &pidref)) { |
| 260 |
service_unwatch_main_pid(s); |
| 261 |
|
| 262 |
dual_timestamp pid_start_time; |
| 263 |
|
| 264 |
if (!start_timestamp) { |
| 265 |
usec_t t; |
| 266 |
|
| 267 |
if (pidref_get_start_time(&pidref, &t) >= 0) |
| 268 |
start_timestamp = dual_timestamp_from_boottime(&pid_start_time, t); |
| 269 |
} |
| 270 |
|
| 271 |
exec_status_start(&s->main_exec_status, pidref.pid, start_timestamp); |
| 272 |
} |
| 273 |
|
| 274 |
s->main_pid = TAKE_PIDREF(pidref); |
| 275 |
s->main_pid_known = true; |
| 276 |
|
| 277 |
r = pidref_is_my_child(&s->main_pid); |
| 278 |
if (r < 0) |
| 279 |
log_unit_warning_errno(UNIT(s), r, "Can't determine if process "PID_FMT" is our child, assuming it is not: %m", s->main_pid.pid); |
| 280 |
else if (r == 0) // FIXME: Supervise through pidfd here |
| 281 |
log_unit_warning(UNIT(s), "Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.", s->main_pid.pid); |
| 282 |
s->main_pid_alien = r <= 0; |
| 283 |
|
| 284 |
return 0; |
| 285 |
} |
| 286 |
|
| 287 |
void service_release_socket_fd(Service *s) { |
| 288 |
assert(s); |
| 289 |
|
| 290 |
if (s->socket_fd < 0 && !UNIT_ISSET(s->accept_socket) && !s->socket_peer) |
| 291 |
return; |
| 292 |
|
| 293 |
log_unit_debug(UNIT(s), "Closing connection socket."); |
| 294 |
|
| 295 |
/* Undo the effect of service_set_socket_fd(). */ |
| 296 |
|
| 297 |
s->socket_fd = asynchronous_close(s->socket_fd); |
| 298 |
|
| 299 |
if (UNIT_ISSET(s->accept_socket)) { |
| 300 |
socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket))); |
| 301 |
unit_ref_unset(&s->accept_socket); |
| 302 |
} |
| 303 |
|
| 304 |
s->socket_peer = socket_peer_unref(s->socket_peer); |
| 305 |
} |
| 306 |
|
| 307 |
static void service_override_notify_access(Service *s, NotifyAccess notify_access_override) { |
| 308 |
assert(s); |
| 309 |
|
| 310 |
s->notify_access_override = notify_access_override; |
| 311 |
|
| 312 |
log_unit_debug(UNIT(s), "notify_access=%s", notify_access_to_string(s->notify_access)); |
| 313 |
log_unit_debug(UNIT(s), "notify_access_override=%s", notify_access_to_string(s->notify_access_override)); |
| 314 |
} |
| 315 |
|
| 316 |
static void service_stop_watchdog(Service *s) { |
| 317 |
assert(s); |
| 318 |
|
| 319 |
s->watchdog_event_source = sd_event_source_disable_unref(s->watchdog_event_source); |
| 320 |
s->watchdog_timestamp = DUAL_TIMESTAMP_NULL; |
| 321 |
} |
| 322 |
|
| 323 |
static void service_start_watchdog(Service *s) { |
| 324 |
usec_t watchdog_usec; |
| 325 |
int r; |
| 326 |
|
| 327 |
assert(s); |
| 328 |
|
| 329 |
watchdog_usec = service_get_watchdog_usec(s); |
| 330 |
if (!timestamp_is_set(watchdog_usec)) { |
| 331 |
service_stop_watchdog(s); |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
if (s->watchdog_event_source) { |
| 336 |
r = sd_event_source_set_time(s->watchdog_event_source, usec_add(s->watchdog_timestamp.monotonic, watchdog_usec)); |
| 337 |
if (r < 0) { |
| 338 |
log_unit_warning_errno(UNIT(s), r, "Failed to reset watchdog timer: %m"); |
| 339 |
return; |
| 340 |
} |
| 341 |
|
| 342 |
r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT); |
| 343 |
} else { |
| 344 |
r = sd_event_add_time( |
| 345 |
UNIT(s)->manager->event, |
| 346 |
&s->watchdog_event_source, |
| 347 |
CLOCK_MONOTONIC, |
| 348 |
usec_add(s->watchdog_timestamp.monotonic, watchdog_usec), 0, |
| 349 |
service_dispatch_watchdog, s); |
| 350 |
if (r < 0) { |
| 351 |
log_unit_warning_errno(UNIT(s), r, "Failed to add watchdog timer: %m"); |
| 352 |
return; |
| 353 |
} |
| 354 |
|
| 355 |
(void) sd_event_source_set_description(s->watchdog_event_source, "service-watchdog"); |
| 356 |
|
| 357 |
/* Let's process everything else which might be a sign |
| 358 |
* of living before we consider a service died. */ |
| 359 |
r = sd_event_source_set_priority(s->watchdog_event_source, EVENT_PRIORITY_SERVICE_WATCHDOG); |
| 360 |
} |
| 361 |
if (r < 0) |
| 362 |
log_unit_warning_errno(UNIT(s), r, "Failed to install watchdog timer: %m"); |
| 363 |
} |
| 364 |
|
| 365 |
usec_t service_restart_usec_next(const Service *s) { |
| 366 |
unsigned n_restarts_next; |
| 367 |
|
| 368 |
assert(s); |
| 369 |
|
| 370 |
/* When the service state is in SERVICE_*_BEFORE_AUTO_RESTART or SERVICE_AUTO_RESTART, we still need |
| 371 |
* to add 1 to s->n_restarts manually, because s->n_restarts is not updated until a restart job is |
| 372 |
* enqueued, i.e. state has transitioned to SERVICE_AUTO_RESTART_QUEUED. */ |
| 373 |
n_restarts_next = s->n_restarts + (s->state == SERVICE_AUTO_RESTART_QUEUED ? 0 : 1); |
| 374 |
|
| 375 |
if (n_restarts_next <= 1 || |
| 376 |
s->restart_steps == 0 || |
| 377 |
s->restart_usec == 0 || |
| 378 |
s->restart_max_delay_usec == USEC_INFINITY || |
| 379 |
s->restart_usec >= s->restart_max_delay_usec) |
| 380 |
return s->restart_usec; |
| 381 |
|
| 382 |
if (n_restarts_next > s->restart_steps) |
| 383 |
return s->restart_max_delay_usec; |
| 384 |
|
| 385 |
/* Enforced in service_verify() and above */ |
| 386 |
assert(s->restart_max_delay_usec > s->restart_usec); |
| 387 |
|
| 388 |
/* r_i / r_0 = (r_n / r_0) ^ (i / n) |
| 389 |
* where, |
| 390 |
* r_0 : initial restart usec (s->restart_usec), |
| 391 |
* r_i : i-th restart usec (value), |
| 392 |
* r_n : maximum restart usec (s->restart_max_delay_usec), |
| 393 |
* i : index of the next step (n_restarts_next - 1) |
| 394 |
* n : num maximum steps (s->restart_steps) */ |
| 395 |
return (usec_t) (s->restart_usec * powl((long double) s->restart_max_delay_usec / s->restart_usec, |
| 396 |
(long double) (n_restarts_next - 1) / s->restart_steps)); |
| 397 |
} |
| 398 |
|
| 399 |
static void service_extend_event_source_timeout(Service *s, sd_event_source *source, usec_t extended) { |
| 400 |
usec_t current; |
| 401 |
int r; |
| 402 |
|
| 403 |
assert(s); |
| 404 |
|
| 405 |
/* Extends the specified event source timer to at least the specified time, unless it is already later |
| 406 |
* anyway. */ |
| 407 |
|
| 408 |
if (!source) |
| 409 |
return; |
| 410 |
|
| 411 |
r = sd_event_source_get_time(source, ¤t); |
| 412 |
if (r < 0) { |
| 413 |
const char *desc; |
| 414 |
(void) sd_event_source_get_description(s->timer_event_source, &desc); |
| 415 |
log_unit_warning_errno(UNIT(s), r, "Failed to retrieve timeout time for event source '%s', ignoring: %m", strna(desc)); |
| 416 |
return; |
| 417 |
} |
| 418 |
|
| 419 |
if (current >= extended) /* Current timeout is already longer, ignore this. */ |
| 420 |
return; |
| 421 |
|
| 422 |
r = sd_event_source_set_time(source, extended); |
| 423 |
if (r < 0) { |
| 424 |
const char *desc; |
| 425 |
(void) sd_event_source_get_description(s->timer_event_source, &desc); |
| 426 |
log_unit_warning_errno(UNIT(s), r, "Failed to set timeout time for event source '%s', ignoring: %m", strna(desc)); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
static void service_extend_timeout(Service *s, usec_t extend_timeout_usec) { |
| 431 |
usec_t extended; |
| 432 |
|
| 433 |
assert(s); |
| 434 |
|
| 435 |
if (!timestamp_is_set(extend_timeout_usec)) |
| 436 |
return; |
| 437 |
|
| 438 |
extended = usec_add(now(CLOCK_MONOTONIC), extend_timeout_usec); |
| 439 |
|
| 440 |
service_extend_event_source_timeout(s, s->timer_event_source, extended); |
| 441 |
service_extend_event_source_timeout(s, s->watchdog_event_source, extended); |
| 442 |
} |
| 443 |
|
| 444 |
static void service_reset_watchdog(Service *s) { |
| 445 |
assert(s); |
| 446 |
|
| 447 |
if (freezer_state_objective(UNIT(s)->freezer_state) != FREEZER_RUNNING) { |
| 448 |
log_unit_debug(UNIT(s), "Service is currently %s, skipping resetting watchdog.", |
| 449 |
freezer_state_to_string(UNIT(s)->freezer_state)); |
| 450 |
return; |
| 451 |
} |
| 452 |
|
| 453 |
dual_timestamp_now(&s->watchdog_timestamp); |
| 454 |
service_start_watchdog(s); |
| 455 |
} |
| 456 |
|
| 457 |
static void service_override_watchdog_timeout(Service *s, usec_t watchdog_override_usec) { |
| 458 |
assert(s); |
| 459 |
|
| 460 |
s->watchdog_override_enable = true; |
| 461 |
s->watchdog_override_usec = watchdog_override_usec; |
| 462 |
service_reset_watchdog(s); |
| 463 |
|
| 464 |
log_unit_debug(UNIT(s), "watchdog_usec="USEC_FMT, s->watchdog_usec); |
| 465 |
log_unit_debug(UNIT(s), "watchdog_override_usec="USEC_FMT, s->watchdog_override_usec); |
| 466 |
} |
| 467 |
|
| 468 |
static ServiceFDStore* service_fd_store_unlink_full(ServiceFDStore *fs, bool propagate_upstream) { |
| 469 |
if (!fs) |
| 470 |
return NULL; |
| 471 |
|
| 472 |
if (fs->service) { |
| 473 |
assert(fs->service->n_fd_store > 0); |
| 474 |
|
| 475 |
/* If we previously propagated this fd to an enveloping service/container manager via |
| 476 |
* the FDSTORE=1 protocol on its NOTIFY_SOCKET (only done when persistence is on), |
| 477 |
* tell that supervisor to drop it now too, so the upstream fd store stays in sync. |
| 478 |
* Only do this for explicit removals (EPOLLHUP/EPOLLERR or app FDSTOREREMOVE), not |
| 479 |
* for local cleanup like service shutdown or fdstore-limit truncation: in those |
| 480 |
* cases we want the upstream copy to survive so it can be handed back to us later. */ |
| 481 |
if (propagate_upstream && fs->index > 0) { |
| 482 |
(void) notify_remove_fd_warnf(SERVICE_FDSTORE_SUB_FDNAME_PREFIX "%" PRIu64, fs->index); |
| 483 |
fs->index = 0; |
| 484 |
/* Refresh the upstream JSON mapping so the supervisor's view stays in sync |
| 485 |
* with what fds are actually still around. */ |
| 486 |
(void) service_propagate_fd_store_mapping_upstream(UNIT(fs->service)->manager); |
| 487 |
} |
| 488 |
|
| 489 |
LIST_REMOVE(fd_store, fs->service->fd_store, fs); |
| 490 |
fs->service->n_fd_store--; |
| 491 |
} |
| 492 |
|
| 493 |
sd_event_source_disable_unref(fs->event_source); |
| 494 |
|
| 495 |
free(fs->fdname); |
| 496 |
asynchronous_close(fs->fd); |
| 497 |
return mfree(fs); |
| 498 |
} |
| 499 |
|
| 500 |
static ServiceFDStore* service_fd_store_unlink(ServiceFDStore *fs) { |
| 501 |
return service_fd_store_unlink_full(fs, /* propagate_upstream= */ false); |
| 502 |
} |
| 503 |
|
| 504 |
DEFINE_TRIVIAL_CLEANUP_FUNC(ServiceFDStore*, service_fd_store_unlink); |
| 505 |
|
| 506 |
static void service_release_fd_store(Service *s) { |
| 507 |
assert(s); |
| 508 |
|
| 509 |
if (!SERVICE_FD_STORE_POPULATED(s)) |
| 510 |
return; |
| 511 |
|
| 512 |
log_unit_debug(UNIT(s), "Releasing all stored fds."); |
| 513 |
|
| 514 |
while (SERVICE_FD_STORE_POPULATED(s)) |
| 515 |
service_fd_store_unlink(s->fd_store); |
| 516 |
|
| 517 |
assert(s->n_fd_store == 0); |
| 518 |
} |
| 519 |
|
| 520 |
static void service_truncate_fd_store(Service *s) { |
| 521 |
assert(s); |
| 522 |
|
| 523 |
/* Drop fds that exceed the (possibly newly lowered) n_fd_store_max, e.g. after the fragment was |
| 524 |
* parsed and FileDescriptorStoreMax= shrunk the configured limit. Newest entries are at the head |
| 525 |
* of the list, so drop from the head (newest first). */ |
| 526 |
|
| 527 |
while (s->n_fd_store > s->n_fd_store_max) { |
| 528 |
ServiceFDStore *fs = ASSERT_PTR(s->fd_store); |
| 529 |
log_unit_debug(UNIT(s), "Dropping stored fd '%s' to honor FileDescriptorStoreMax=%u.", |
| 530 |
strna(fs->fdname), s->n_fd_store_max); |
| 531 |
service_fd_store_unlink(fs); |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
static void service_release_extra_fds(Service *s) { |
| 536 |
assert(s); |
| 537 |
|
| 538 |
if (!s->extra_fds) |
| 539 |
return; |
| 540 |
|
| 541 |
log_unit_debug(UNIT(s), "Releasing extra file descriptors."); |
| 542 |
|
| 543 |
FOREACH_ARRAY(i, s->extra_fds, s->n_extra_fds) { |
| 544 |
asynchronous_close(i->fd); |
| 545 |
free(i->fdname); |
| 546 |
} |
| 547 |
|
| 548 |
s->extra_fds = mfree(s->extra_fds); |
| 549 |
s->n_extra_fds = 0; |
| 550 |
} |
| 551 |
|
| 552 |
ServiceExtraFD* service_extra_fd_free(ServiceExtraFD *fd) { |
| 553 |
if (!fd) |
| 554 |
return NULL; |
| 555 |
|
| 556 |
safe_close(fd->fd); |
| 557 |
free(fd->fdname); |
| 558 |
return mfree(fd); |
| 559 |
} |
| 560 |
|
| 561 |
static void service_release_stdio_fd(Service *s) { |
| 562 |
assert(s); |
| 563 |
|
| 564 |
if (s->stdin_fd < 0 && s->stdout_fd < 0 && s->stderr_fd < 0) |
| 565 |
return; |
| 566 |
|
| 567 |
log_unit_debug(UNIT(s), "Releasing stdin/stdout/stderr file descriptors."); |
| 568 |
|
| 569 |
s->stdin_fd = asynchronous_close(s->stdin_fd); |
| 570 |
s->stdout_fd = asynchronous_close(s->stdout_fd); |
| 571 |
s->stderr_fd = asynchronous_close(s->stderr_fd); |
| 572 |
} |
| 573 |
|
| 574 |
static void service_done(Unit *u) { |
| 575 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 576 |
|
| 577 |
open_file_free_many(&s->open_files); |
| 578 |
|
| 579 |
s->pid_file = mfree(s->pid_file); |
| 580 |
s->status_text = mfree(s->status_text); |
| 581 |
s->status_bus_error = mfree(s->status_bus_error); |
| 582 |
s->status_varlink_error = mfree(s->status_varlink_error); |
| 583 |
|
| 584 |
s->exec_runtime = exec_runtime_free(s->exec_runtime); |
| 585 |
|
| 586 |
exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX); |
| 587 |
s->control_command = NULL; |
| 588 |
s->main_command = NULL; |
| 589 |
|
| 590 |
exit_status_set_free(&s->restart_prevent_status); |
| 591 |
exit_status_set_free(&s->restart_force_status); |
| 592 |
exit_status_set_free(&s->success_status); |
| 593 |
|
| 594 |
/* This will leak a process, but at least no memory or any of our resources */ |
| 595 |
service_unwatch_main_pid(s); |
| 596 |
service_unwatch_control_pid(s); |
| 597 |
service_unwatch_pid_file(s); |
| 598 |
|
| 599 |
if (s->bus_name) { |
| 600 |
unit_unwatch_bus_name(u, s->bus_name); |
| 601 |
s->bus_name = mfree(s->bus_name); |
| 602 |
} |
| 603 |
|
| 604 |
s->usb_function_descriptors = mfree(s->usb_function_descriptors); |
| 605 |
s->usb_function_strings = mfree(s->usb_function_strings); |
| 606 |
|
| 607 |
service_stop_watchdog(s); |
| 608 |
|
| 609 |
s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
| 610 |
s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); |
| 611 |
|
| 612 |
s->bus_name_pid_lookup_slot = sd_bus_slot_unref(s->bus_name_pid_lookup_slot); |
| 613 |
|
| 614 |
service_release_socket_fd(s); |
| 615 |
service_release_stdio_fd(s); |
| 616 |
service_release_fd_store(s); |
| 617 |
service_release_extra_fds(s); |
| 618 |
s->root_directory_fd = asynchronous_close(s->root_directory_fd); |
| 619 |
|
| 620 |
s->mount_request = sd_bus_message_unref(s->mount_request); |
| 621 |
} |
| 622 |
|
| 623 |
static int on_fd_store_io(sd_event_source *e, int fd, uint32_t revents, void *userdata) { |
| 624 |
ServiceFDStore *fs = ASSERT_PTR(userdata); |
| 625 |
Service *s = fs->service; |
| 626 |
|
| 627 |
assert(e); |
| 628 |
|
| 629 |
/* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */ |
| 630 |
log_unit_debug(UNIT(s), |
| 631 |
"Received %s on stored fd %d (%s), closing.", |
| 632 |
revents & EPOLLERR ? "EPOLLERR" : "EPOLLHUP", |
| 633 |
fs->fd, strna(fs->fdname)); |
| 634 |
service_fd_store_unlink_full(fs, /* propagate_upstream= */ true); |
| 635 |
|
| 636 |
if (s->state == SERVICE_DEAD_RESOURCES_PINNED && !SERVICE_FD_STORE_POPULATED(s)) |
| 637 |
service_set_state(s, SERVICE_DEAD); |
| 638 |
|
| 639 |
return 0; |
| 640 |
} |
| 641 |
|
| 642 |
int service_add_fd_store(Service *s, int fd_in, const char *name, bool do_poll, bool propagate_upstream) { |
| 643 |
_cleanup_(service_fd_store_unlinkp) ServiceFDStore *fs = NULL; |
| 644 |
_cleanup_(asynchronous_closep) int fd = ASSERT_FD(fd_in); |
| 645 |
struct stat st; |
| 646 |
int r; |
| 647 |
|
| 648 |
/* fd is always consumed even if the function fails. */ |
| 649 |
|
| 650 |
assert(s); |
| 651 |
|
| 652 |
if (fstat(fd, &st) < 0) |
| 653 |
return -errno; |
| 654 |
|
| 655 |
log_unit_debug(UNIT(s), "Trying to stash fd for dev=" DEVNUM_FORMAT_STR "/inode=%" PRIu64, |
| 656 |
DEVNUM_FORMAT_VAL(st.st_dev), (uint64_t) st.st_ino); |
| 657 |
|
| 658 |
if (s->n_fd_store >= s->n_fd_store_max) |
| 659 |
/* Our store is full. Use this errno rather than E[NM]FILE to distinguish from the case |
| 660 |
* where systemd itself hits the file limit. */ |
| 661 |
return log_unit_debug_errno(UNIT(s), SYNTHETIC_ERRNO(EXFULL), "Hit fd store limit."); |
| 662 |
|
| 663 |
LIST_FOREACH(fd_store, i, s->fd_store) { |
| 664 |
r = same_fd(i->fd, fd); |
| 665 |
if (r < 0) |
| 666 |
return r; |
| 667 |
if (r > 0) { |
| 668 |
log_unit_debug(UNIT(s), "Suppressing duplicate fd %i in fd store.", fd); |
| 669 |
return 0; /* fd already included */ |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
fs = new(ServiceFDStore, 1); |
| 674 |
if (!fs) |
| 675 |
return -ENOMEM; |
| 676 |
|
| 677 |
*fs = (ServiceFDStore) { |
| 678 |
.fd = TAKE_FD(fd), |
| 679 |
.do_poll = do_poll, |
| 680 |
.fdname = strdup(name ?: "stored"), |
| 681 |
}; |
| 682 |
|
| 683 |
if (!fs->fdname) |
| 684 |
return -ENOMEM; |
| 685 |
|
| 686 |
if (do_poll) { |
| 687 |
r = sd_event_add_io(UNIT(s)->manager->event, &fs->event_source, fs->fd, 0, on_fd_store_io, fs); |
| 688 |
if (r < 0 && r != -EPERM) /* EPERM indicates fds that aren't pollable, which is OK */ |
| 689 |
return r; |
| 690 |
if (r >= 0) |
| 691 |
(void) sd_event_source_set_description(fs->event_source, "service-fd-store"); |
| 692 |
} |
| 693 |
|
| 694 |
log_unit_debug(UNIT(s), "Added fd %i (%s) to fd store.", fs->fd, fs->fdname); |
| 695 |
|
| 696 |
/* If fd-store persistence is enabled and we have an enveloping service/container manager (i.e. |
| 697 |
* NOTIFY_SOCKET is set), forward the fd to it via sd_notify(FDSTORE=1) tagged with a fresh |
| 698 |
* incrementing index, and (re-)push the JSON mapping memfd that pairs the index back to this |
| 699 |
* unit and the original fdname. This way fdstore persistence chains all the way up to whichever |
| 700 |
* entity is ultimately responsible for surviving across kexec/restart, regardless of fdname |
| 701 |
* length or charset constraints. */ |
| 702 |
if (propagate_upstream && IN_SET(s->fd_store_preserve_mode, EXEC_PRESERVE_YES, EXEC_PRESERVE_ON_SUCCESS)) { |
| 703 |
Manager *m = ASSERT_PTR(UNIT(s)->manager); |
| 704 |
char idx_str[STRLEN(SERVICE_FDSTORE_SUB_FDNAME_PREFIX) + DECIMAL_STR_MAX(uint64_t)]; |
| 705 |
|
| 706 |
assert(m->fd_store_upstream_next_index < UINT64_MAX); |
| 707 |
uint64_t idx = ++m->fd_store_upstream_next_index; |
| 708 |
|
| 709 |
xsprintf(idx_str, SERVICE_FDSTORE_SUB_FDNAME_PREFIX "%" PRIu64, idx); |
| 710 |
|
| 711 |
r = notify_push_fd(fs->fd, idx_str); |
| 712 |
if (r < 0) |
| 713 |
log_unit_debug_errno(UNIT(s), r, |
| 714 |
"Failed to propagate fd '%s' to upstream supervisor as index %" PRIu64 ", ignoring: %m", |
| 715 |
fs->fdname, idx); |
| 716 |
else |
| 717 |
fs->index = idx; |
| 718 |
} |
| 719 |
|
| 720 |
fs->service = s; |
| 721 |
LIST_PREPEND(fd_store, s->fd_store, TAKE_PTR(fs)); |
| 722 |
s->n_fd_store++; |
| 723 |
|
| 724 |
if (propagate_upstream && IN_SET(s->fd_store_preserve_mode, EXEC_PRESERVE_YES, EXEC_PRESERVE_ON_SUCCESS)) |
| 725 |
/* Refresh the JSON mapping memfd so the supervisor can resolve the new index. Do this |
| 726 |
* after LIST_PREPEND so the new entry is visible to the helper. */ |
| 727 |
(void) service_propagate_fd_store_mapping_upstream(UNIT(s)->manager); |
| 728 |
|
| 729 |
return 1; /* fd newly stored */ |
| 730 |
} |
| 731 |
|
| 732 |
static int service_add_fd_store_set(Service *s, FDSet *fds, const char *name, bool do_poll, bool propagate_upstream) { |
| 733 |
int r; |
| 734 |
|
| 735 |
assert(s); |
| 736 |
|
| 737 |
for (;;) { |
| 738 |
int fd; |
| 739 |
|
| 740 |
fd = fdset_steal_first(fds); |
| 741 |
if (fd < 0) |
| 742 |
break; |
| 743 |
|
| 744 |
r = service_add_fd_store(s, fd, name, do_poll, propagate_upstream); |
| 745 |
if (r == -EXFULL) |
| 746 |
return log_unit_warning_errno(UNIT(s), r, |
| 747 |
"Cannot store more fds than FileDescriptorStoreMax=%u, closing remaining.", |
| 748 |
s->n_fd_store_max); |
| 749 |
if (r < 0) |
| 750 |
return log_unit_error_errno(UNIT(s), r, "Failed to add fd to store: %m"); |
| 751 |
} |
| 752 |
|
| 753 |
return 0; |
| 754 |
} |
| 755 |
|
| 756 |
int service_propagate_fd_store_mapping_upstream(Manager *m) { |
| 757 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *root = NULL; |
| 758 |
_cleanup_close_ int fd = -EBADF; |
| 759 |
_cleanup_free_ char *text = NULL; |
| 760 |
Unit *u; |
| 761 |
int r; |
| 762 |
|
| 763 |
assert(m); |
| 764 |
|
| 765 |
/* Build a JSON object listing all fdstore entries that have been propagated upstream: |
| 766 |
* |
| 767 |
* { |
| 768 |
* "unit-name.service": [ |
| 769 |
* { "name": "fdname1", "index": 1 }, |
| 770 |
* { "name": "fdname2", "index": 2 } |
| 771 |
* ], |
| 772 |
* ... |
| 773 |
* } |
| 774 |
* |
| 775 |
* Push it as a sealed memfd to the upstream supervisor under a fixed FDNAME so it can resolve |
| 776 |
* the per-fd numeric indices back to (unit_id, original fdname) at startup. The mapping is |
| 777 |
* regenerated and re-pushed after every add/remove, so the supervisor's view stays in sync. */ |
| 778 |
HASHMAP_FOREACH(u, m->units) { |
| 779 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *entries = NULL; |
| 780 |
Service *s; |
| 781 |
|
| 782 |
if (u->type != UNIT_SERVICE) |
| 783 |
continue; |
| 784 |
|
| 785 |
s = SERVICE(u); |
| 786 |
if (!s->fd_store) |
| 787 |
continue; |
| 788 |
|
| 789 |
LIST_FOREACH(fd_store, fs, s->fd_store) { |
| 790 |
if (fs->index == 0) |
| 791 |
continue; |
| 792 |
|
| 793 |
r = sd_json_variant_append_arraybo( |
| 794 |
&entries, |
| 795 |
SD_JSON_BUILD_PAIR_STRING("name", fs->fdname), |
| 796 |
SD_JSON_BUILD_PAIR_UNSIGNED("index", fs->index)); |
| 797 |
if (r < 0) |
| 798 |
return log_warning_errno(r, "Failed to build fdstore-mapping JSON entry: %m"); |
| 799 |
} |
| 800 |
|
| 801 |
if (!entries) |
| 802 |
continue; |
| 803 |
|
| 804 |
r = sd_json_variant_set_field(&root, u->id, entries); |
| 805 |
if (r < 0) |
| 806 |
return log_warning_errno(r, "Failed to add unit to fdstore-mapping JSON: %m"); |
| 807 |
} |
| 808 |
|
| 809 |
if (!root) { |
| 810 |
/* Nothing to map: tell the supervisor to drop any previously-pushed mapping memfd |
| 811 |
* so it doesn't keep stale entries around. Only do this if we have actually pushed |
| 812 |
* one in the past (i.e. we ever assigned an upstream index, either in this |
| 813 |
* incarnation or in a previous one whose counter we deserialized), otherwise we |
| 814 |
* might inadvertently remove a mapping that was just handed back to us via |
| 815 |
* LISTEN_FDS during a fresh manager startup. */ |
| 816 |
if (m->fd_store_upstream_next_index > 0) |
| 817 |
(void) notify_remove_fd_warn(SERVICE_FDSTORE_MAPPING_FDNAME); |
| 818 |
return 0; |
| 819 |
} |
| 820 |
|
| 821 |
r = sd_json_variant_format(root, /* flags= */ 0, &text); |
| 822 |
if (r < 0) |
| 823 |
return log_warning_errno(r, "Failed to format fdstore-mapping JSON: %m"); |
| 824 |
|
| 825 |
fd = memfd_new_and_seal_string(SERVICE_FDSTORE_MAPPING_FDNAME, text); |
| 826 |
if (fd < 0) |
| 827 |
return log_warning_errno(fd, "Failed to create fdstore-mapping memfd: %m"); |
| 828 |
|
| 829 |
r = notify_push_fd(fd, SERVICE_FDSTORE_MAPPING_FDNAME); |
| 830 |
if (r < 0) |
| 831 |
return log_warning_errno(r, "Failed to propagate fdstore-mapping to upstream supervisor: %m"); |
| 832 |
|
| 833 |
return 0; |
| 834 |
} |
| 835 |
|
| 836 |
static int service_attach_external_fd_to_fdstore(Unit *u, int fd, const char *fdname, uint64_t index) { |
| 837 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 838 |
int r; |
| 839 |
|
| 840 |
assert(u->type == UNIT_SERVICE); |
| 841 |
|
| 842 |
/* If the unit file is absent, bump the limit by one and force preserve so the fd is |
| 843 |
* accepted and pins the unit until a daemon-reload picks up the unit file or it is |
| 844 |
* explicitly stopped. */ |
| 845 |
if (u->load_state == UNIT_NOT_FOUND) { |
| 846 |
s->fd_store_preserve_mode = EXEC_PRESERVE_YES; |
| 847 |
s->n_fd_store_max++; |
| 848 |
} |
| 849 |
|
| 850 |
/* Don't propagate upstream: the fd just came back from upstream, forwarding it would loop. */ |
| 851 |
r = service_add_fd_store(s, fd, fdname, /* do_poll= */ true, /* propagate_upstream= */ false); |
| 852 |
if (r <= 0 && u->load_state == UNIT_NOT_FOUND) |
| 853 |
s->n_fd_store_max--; |
| 854 |
if (r < 0) |
| 855 |
return log_unit_debug_errno(u, r, "Failed to add LUO fd '%s' to fd store: %m", fdname); |
| 856 |
|
| 857 |
/* If the fd was previously propagated to an upstream supervisor under a numeric index, |
| 858 |
* preserve that index on the freshly-added entry so that future FDSTOREREMOVE messages |
| 859 |
* (and the fdstore-mapping memfd we re-push to the supervisor) reference the same index |
| 860 |
* the supervisor already knows about. service_add_fd_store() does LIST_PREPEND() on |
| 861 |
* success, so the new entry is at the head. Also keep the manager's allocator counter |
| 862 |
* past the highest restored index, to avoid collisions with newly allocated indices. */ |
| 863 |
if (r > 0 && index > 0 && s->fd_store) { |
| 864 |
Manager *m = ASSERT_PTR(u->manager); |
| 865 |
|
| 866 |
s->fd_store->index = index; |
| 867 |
if (index > m->fd_store_upstream_next_index) |
| 868 |
m->fd_store_upstream_next_index = index; |
| 869 |
} |
| 870 |
|
| 871 |
/* If the unit is otherwise inactive (typical for LUO/upstream restore), pin its resources so it |
| 872 |
* isn't garbage-collected before something explicitly stops it. Only flip the state when both |
| 873 |
* runtime and deserialized state agree on DEAD, to avoid clobbering a just-deserialized live |
| 874 |
* state (e.g. SERVICE_RUNNING after daemon-reload, where service_coldplug() will set the proper |
| 875 |
* state later). */ |
| 876 |
if (r > 0 && |
| 877 |
s->state == SERVICE_DEAD && |
| 878 |
s->deserialized_state == SERVICE_DEAD && |
| 879 |
IN_SET(s->fd_store_preserve_mode, EXEC_PRESERVE_YES, EXEC_PRESERVE_ON_SUCCESS)) { |
| 880 |
service_set_state(s, SERVICE_DEAD_RESOURCES_PINNED); |
| 881 |
s->deserialized_state = SERVICE_DEAD_RESOURCES_PINNED; |
| 882 |
} |
| 883 |
|
| 884 |
if (r > 0) |
| 885 |
log_unit_debug(u, "Restored fd '%s'.", fdname); |
| 886 |
return r; |
| 887 |
} |
| 888 |
|
| 889 |
static void service_remove_fd_store(Service *s, const char *name) { |
| 890 |
assert(s); |
| 891 |
assert(name); |
| 892 |
|
| 893 |
LIST_FOREACH(fd_store, fs, s->fd_store) { |
| 894 |
if (!streq(fs->fdname, name)) |
| 895 |
continue; |
| 896 |
|
| 897 |
log_unit_debug(UNIT(s), "Got explicit request to remove fd %i (%s), closing.", fs->fd, name); |
| 898 |
service_fd_store_unlink_full(fs, /* propagate_upstream= */ true); |
| 899 |
} |
| 900 |
} |
| 901 |
|
| 902 |
static usec_t service_running_timeout(Service *s) { |
| 903 |
usec_t delta = 0; |
| 904 |
|
| 905 |
assert(s); |
| 906 |
|
| 907 |
if (s->runtime_rand_extra_usec != 0) { |
| 908 |
delta = random_u64_range(s->runtime_rand_extra_usec); |
| 909 |
log_unit_debug(UNIT(s), "Adding delta of %s sec to timeout", FORMAT_TIMESPAN(delta, USEC_PER_SEC)); |
| 910 |
} |
| 911 |
|
| 912 |
return usec_add(usec_add(UNIT(s)->active_enter_timestamp.monotonic, |
| 913 |
s->runtime_max_usec), |
| 914 |
delta); |
| 915 |
} |
| 916 |
|
| 917 |
static int service_arm_timer(Service *s, bool relative, usec_t usec) { |
| 918 |
assert(s); |
| 919 |
|
| 920 |
return unit_arm_timer(UNIT(s), &s->timer_event_source, relative, usec, service_dispatch_timer); |
| 921 |
} |
| 922 |
|
| 923 |
static int service_verify(Service *s) { |
| 924 |
assert(s); |
| 925 |
assert(UNIT(s)->load_state == UNIT_LOADED); |
| 926 |
|
| 927 |
if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP] && |
| 928 |
UNIT(s)->success_action == EMERGENCY_ACTION_NONE) |
| 929 |
/* FailureAction= only makes sense if one of the start or stop commands is specified. |
| 930 |
* SuccessAction= will be executed unconditionally if no commands are specified. Hence, |
| 931 |
* either a command or SuccessAction= are required. */ |
| 932 |
return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has no ExecStart=, ExecStop=, or SuccessAction=. Refusing."); |
| 933 |
|
| 934 |
if (s->type != SERVICE_ONESHOT && !s->exec_command[SERVICE_EXEC_START]) |
| 935 |
return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing."); |
| 936 |
|
| 937 |
if (!s->remain_after_exit && !s->exec_command[SERVICE_EXEC_START] && UNIT(s)->success_action == EMERGENCY_ACTION_NONE) |
| 938 |
return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has no ExecStart= and no SuccessAction= settings and does not have RemainAfterExit=yes set. Refusing."); |
| 939 |
|
| 940 |
if (s->type != SERVICE_ONESHOT && s->exec_command[SERVICE_EXEC_START]->command_next) |
| 941 |
return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing."); |
| 942 |
|
| 943 |
if (s->type == SERVICE_ONESHOT && IN_SET(s->restart, SERVICE_RESTART_ALWAYS, SERVICE_RESTART_ON_SUCCESS)) |
| 944 |
return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has Restart= set to either always or on-success, which isn't allowed for Type=oneshot services. Refusing."); |
| 945 |
|
| 946 |
if (s->type == SERVICE_ONESHOT && s->exit_type == SERVICE_EXIT_CGROUP) |
| 947 |
return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has ExitType=cgroup set, which isn't allowed for Type=oneshot services. Refusing."); |
| 948 |
|
| 949 |
if (s->type == SERVICE_DBUS && !s->bus_name) |
| 950 |
return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing."); |
| 951 |
|
| 952 |
if (s->type == SERVICE_FORKING && exec_needs_pid_namespace(&s->exec_context, /* params= */ NULL)) |
| 953 |
return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service of Type=forking does not support PrivatePIDs=yes. Refusing."); |
| 954 |
|
| 955 |
if (s->usb_function_descriptors && !s->usb_function_strings) |
| 956 |
log_unit_warning(UNIT(s), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring."); |
| 957 |
|
| 958 |
if (!s->usb_function_descriptors && s->usb_function_strings) |
| 959 |
log_unit_warning(UNIT(s), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring."); |
| 960 |
|
| 961 |
if (s->runtime_max_usec != USEC_INFINITY && s->type == SERVICE_ONESHOT) |
| 962 |
log_unit_warning(UNIT(s), "RuntimeMaxSec= has no effect in combination with Type=oneshot. Ignoring."); |
| 963 |
|
| 964 |
if (s->runtime_max_usec == USEC_INFINITY && s->runtime_rand_extra_usec != 0) |
| 965 |
log_unit_warning(UNIT(s), "Service has RuntimeRandomizedExtraSec= setting, but no RuntimeMaxSec=. Ignoring."); |
| 966 |
|
| 967 |
if (s->type == SERVICE_SIMPLE && s->exec_command[SERVICE_EXEC_START_POST] && exec_context_has_credentials(&s->exec_context)) |
| 968 |
log_unit_warning(UNIT(s), "Service uses a combination of Type=simple, ExecStartPost=, and credentials. This could lead to race conditions. Continuing."); |
| 969 |
|
| 970 |
if (s->restart_max_delay_usec == USEC_INFINITY && s->restart_steps > 0) |
| 971 |
log_unit_warning(UNIT(s), "Service has RestartSteps= but no RestartMaxDelaySec= setting. Ignoring."); |
| 972 |
|
| 973 |
if (s->restart_max_delay_usec != USEC_INFINITY && s->restart_steps == 0) |
| 974 |
log_unit_warning(UNIT(s), "Service has RestartMaxDelaySec= but no RestartSteps= setting. Ignoring."); |
| 975 |
|
| 976 |
if (s->restart_max_delay_usec < s->restart_usec) { |
| 977 |
log_unit_warning(UNIT(s), "RestartMaxDelaySec= has a value smaller than RestartSec=, resetting RestartSec= to RestartMaxDelaySec=."); |
| 978 |
s->restart_usec = s->restart_max_delay_usec; |
| 979 |
} |
| 980 |
|
| 981 |
if (s->refresh_on_reload_set && s->refresh_on_reload_flags != _SERVICE_REFRESH_ON_RELOAD_ALL) { |
| 982 |
if (FLAGS_SET(s->refresh_on_reload_flags, SERVICE_RELOAD_EXTENSIONS)) |
| 983 |
service_can_reload_extensions(s, /* warn = */ true); |
| 984 |
|
| 985 |
if (FLAGS_SET(s->refresh_on_reload_flags, SERVICE_RELOAD_CREDENTIALS) && |
| 986 |
!exec_context_has_credentials(&s->exec_context)) |
| 987 |
log_unit_warning(UNIT(s), "Service has RefreshOnReload=credentials, but no credentials are in use. The credentials tree will be masked which blocks further refreshing. Continuing."); |
| 988 |
} |
| 989 |
|
| 990 |
return 0; |
| 991 |
} |
| 992 |
|
| 993 |
static int service_add_default_dependencies(Service *s) { |
| 994 |
int r; |
| 995 |
|
| 996 |
assert(s); |
| 997 |
|
| 998 |
if (!UNIT(s)->default_dependencies) |
| 999 |
return 0; |
| 1000 |
|
| 1001 |
/* Add a number of automatic dependencies useful for the |
| 1002 |
* majority of services. */ |
| 1003 |
|
| 1004 |
if (MANAGER_IS_SYSTEM(UNIT(s)->manager)) { |
| 1005 |
/* First, pull in the really early boot stuff, and |
| 1006 |
* require it, so that we fail if we can't acquire |
| 1007 |
* it. */ |
| 1008 |
|
| 1009 |
r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
| 1010 |
if (r < 0) |
| 1011 |
return r; |
| 1012 |
} else { |
| 1013 |
|
| 1014 |
/* In the --user instance there's no sysinit.target, |
| 1015 |
* in that case require basic.target instead. */ |
| 1016 |
|
| 1017 |
r = unit_add_dependency_by_name(UNIT(s), UNIT_REQUIRES, SPECIAL_BASIC_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
| 1018 |
if (r < 0) |
| 1019 |
return r; |
| 1020 |
} |
| 1021 |
|
| 1022 |
/* Second, if the rest of the base system is in the same |
| 1023 |
* transaction, order us after it, but do not pull it in or |
| 1024 |
* even require it. */ |
| 1025 |
r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_BASIC_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
| 1026 |
if (r < 0) |
| 1027 |
return r; |
| 1028 |
|
| 1029 |
/* Third, add us in for normal shutdown. */ |
| 1030 |
return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
| 1031 |
} |
| 1032 |
|
| 1033 |
static void service_fix_stdio(Service *s) { |
| 1034 |
assert(s); |
| 1035 |
|
| 1036 |
/* Note that EXEC_INPUT_NULL and EXEC_OUTPUT_INHERIT play a special role here: they are both the |
| 1037 |
* default value that is subject to automatic overriding triggered by other settings and an explicit |
| 1038 |
* choice the user can make. We don't distinguish between these cases currently. */ |
| 1039 |
|
| 1040 |
if (s->exec_context.std_input == EXEC_INPUT_NULL && |
| 1041 |
s->exec_context.stdin_data_size > 0) |
| 1042 |
s->exec_context.std_input = EXEC_INPUT_DATA; |
| 1043 |
|
| 1044 |
if (exec_input_is_inheritable(s->exec_context.std_input)) |
| 1045 |
return; |
| 1046 |
|
| 1047 |
/* Override EXEC_OUTPUT_INHERIT with the default stderr/stdout setting if not applicable for |
| 1048 |
* given stdin mode. */ |
| 1049 |
|
| 1050 |
if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT && |
| 1051 |
s->exec_context.std_output == EXEC_OUTPUT_INHERIT) |
| 1052 |
s->exec_context.std_error = UNIT(s)->manager->defaults.std_error; |
| 1053 |
|
| 1054 |
if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT) |
| 1055 |
s->exec_context.std_output = UNIT(s)->manager->defaults.std_output; |
| 1056 |
} |
| 1057 |
|
| 1058 |
static int service_setup_bus_name(Service *s) { |
| 1059 |
int r; |
| 1060 |
|
| 1061 |
assert(s); |
| 1062 |
|
| 1063 |
/* If s->bus_name is not set, then the unit will be refused by service_verify() later. */ |
| 1064 |
if (!s->bus_name) |
| 1065 |
return 0; |
| 1066 |
|
| 1067 |
if (s->type == SERVICE_DBUS) { |
| 1068 |
r = unit_add_dependency_by_name(UNIT(s), UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, true, UNIT_DEPENDENCY_FILE); |
| 1069 |
if (r < 0) |
| 1070 |
return log_unit_error_errno(UNIT(s), r, "Failed to add dependency on %s: %m", SPECIAL_DBUS_SOCKET); |
| 1071 |
|
| 1072 |
/* We always want to be ordered against dbus.socket if both are in the transaction. */ |
| 1073 |
r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_DBUS_SOCKET, true, UNIT_DEPENDENCY_FILE); |
| 1074 |
if (r < 0) |
| 1075 |
return log_unit_error_errno(UNIT(s), r, "Failed to add dependency on %s: %m", SPECIAL_DBUS_SOCKET); |
| 1076 |
} |
| 1077 |
|
| 1078 |
r = unit_watch_bus_name(UNIT(s), s->bus_name); |
| 1079 |
if (r == -EEXIST) |
| 1080 |
return log_unit_error_errno(UNIT(s), r, "Two services allocated for the same bus name %s, refusing operation.", s->bus_name); |
| 1081 |
if (r < 0) |
| 1082 |
return log_unit_error_errno(UNIT(s), r, "Cannot watch bus name %s: %m", s->bus_name); |
| 1083 |
|
| 1084 |
return 0; |
| 1085 |
} |
| 1086 |
|
| 1087 |
static int service_add_extras(Service *s) { |
| 1088 |
int r; |
| 1089 |
|
| 1090 |
assert(s); |
| 1091 |
|
| 1092 |
if (s->type == _SERVICE_TYPE_INVALID) { |
| 1093 |
/* Figure out a type automatically */ |
| 1094 |
if (s->bus_name) |
| 1095 |
s->type = SERVICE_DBUS; |
| 1096 |
else if (exec_context_has_credentials(&s->exec_context)) |
| 1097 |
s->type = SERVICE_EXEC; |
| 1098 |
else if (s->exec_command[SERVICE_EXEC_START]) |
| 1099 |
s->type = SERVICE_SIMPLE; |
| 1100 |
else |
| 1101 |
s->type = SERVICE_ONESHOT; |
| 1102 |
} |
| 1103 |
|
| 1104 |
/* Oneshot services have disabled start timeout by default */ |
| 1105 |
if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined) |
| 1106 |
s->timeout_start_usec = USEC_INFINITY; |
| 1107 |
|
| 1108 |
service_fix_stdio(s); |
| 1109 |
|
| 1110 |
r = unit_patch_contexts(UNIT(s)); |
| 1111 |
if (r < 0) |
| 1112 |
return r; |
| 1113 |
|
| 1114 |
r = unit_add_exec_dependencies(UNIT(s), &s->exec_context); |
| 1115 |
if (r < 0) |
| 1116 |
return r; |
| 1117 |
|
| 1118 |
r = unit_set_default_slice(UNIT(s)); |
| 1119 |
if (r < 0) |
| 1120 |
return r; |
| 1121 |
|
| 1122 |
/* If the service needs the notify socket, let's enable it automatically. */ |
| 1123 |
if (s->notify_access == NOTIFY_NONE && |
| 1124 |
(IN_SET(s->type, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD) || s->watchdog_usec > 0 || s->n_fd_store_max > 0)) |
| 1125 |
s->notify_access = NOTIFY_MAIN; |
| 1126 |
|
| 1127 |
if (!s->refresh_on_reload_set) { |
| 1128 |
assert_cc(SERVICE_REFRESH_ON_RELOAD_DEFAULT == SERVICE_RELOAD_EXTENSIONS); |
| 1129 |
s->refresh_on_reload_flags = service_can_reload_extensions(s, /* warn = */ false) ? SERVICE_RELOAD_EXTENSIONS : 0; |
| 1130 |
} |
| 1131 |
|
| 1132 |
/* If no OOM policy was explicitly set, then default to the configure default OOM policy. Except when |
| 1133 |
* delegation is on, in that case it we assume the payload knows better what to do and can process |
| 1134 |
* things in a more focused way. */ |
| 1135 |
if (s->oom_policy < 0) |
| 1136 |
s->oom_policy = s->cgroup_context.delegate ? OOM_CONTINUE : UNIT(s)->manager->defaults.oom_policy; |
| 1137 |
|
| 1138 |
/* Let the kernel do the killing if that's requested. */ |
| 1139 |
s->cgroup_context.memory_oom_group = s->oom_policy == OOM_KILL; |
| 1140 |
|
| 1141 |
r = service_add_default_dependencies(s); |
| 1142 |
if (r < 0) |
| 1143 |
return r; |
| 1144 |
|
| 1145 |
r = service_setup_bus_name(s); |
| 1146 |
if (r < 0) |
| 1147 |
return r; |
| 1148 |
|
| 1149 |
return 0; |
| 1150 |
} |
| 1151 |
|
| 1152 |
static int service_load(Unit *u) { |
| 1153 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 1154 |
int r; |
| 1155 |
|
| 1156 |
r = unit_load_fragment_and_dropin(u, true); |
| 1157 |
if (r < 0) |
| 1158 |
return r; |
| 1159 |
|
| 1160 |
if (u->load_state != UNIT_LOADED) |
| 1161 |
return 0; |
| 1162 |
|
| 1163 |
/* The fragment may have lowered FileDescriptorStoreMax= below the number of fds currently in the |
| 1164 |
* store (e.g. fds restored from LUO into a synthesized UNIT_NOT_FOUND service that just got a real |
| 1165 |
* fragment via lazy reload, but which now disables the fd store). */ |
| 1166 |
service_truncate_fd_store(s); |
| 1167 |
|
| 1168 |
/* This is a new unit? Then let's add in some extras */ |
| 1169 |
r = service_add_extras(s); |
| 1170 |
if (r < 0) |
| 1171 |
return r; |
| 1172 |
|
| 1173 |
return service_verify(s); |
| 1174 |
} |
| 1175 |
|
| 1176 |
static int service_dump_fd(int fd, const char *fdname, const char *header, FILE *f, const char *prefix) { |
| 1177 |
_cleanup_free_ char *path = NULL; |
| 1178 |
struct stat st; |
| 1179 |
int flags; |
| 1180 |
|
| 1181 |
assert(fd >= 0); |
| 1182 |
assert(fdname); |
| 1183 |
assert(header); |
| 1184 |
assert(f); |
| 1185 |
assert(prefix); |
| 1186 |
|
| 1187 |
if (fstat(fd, &st) < 0) |
| 1188 |
return log_debug_errno(errno, "Failed to stat service fd: %m"); |
| 1189 |
|
| 1190 |
flags = fcntl(fd, F_GETFL); |
| 1191 |
if (flags < 0) |
| 1192 |
return log_debug_errno(errno, "Failed to get service fd flags: %m"); |
| 1193 |
|
| 1194 |
(void) fd_get_path(fd, &path); |
| 1195 |
|
| 1196 |
fprintf(f, |
| 1197 |
"%s%s '%s' (type=%s; dev=" DEVNUM_FORMAT_STR "; inode=%" PRIu64 "; rdev=" DEVNUM_FORMAT_STR "; path=%s; access=%s)\n", |
| 1198 |
prefix, |
| 1199 |
header, |
| 1200 |
fdname, |
| 1201 |
strna(inode_type_to_string(st.st_mode)), |
| 1202 |
DEVNUM_FORMAT_VAL(st.st_dev), |
| 1203 |
(uint64_t) st.st_ino, |
| 1204 |
DEVNUM_FORMAT_VAL(st.st_rdev), |
| 1205 |
strna(path), |
| 1206 |
strna(accmode_to_string(flags))); |
| 1207 |
|
| 1208 |
return 0; |
| 1209 |
} |
| 1210 |
|
| 1211 |
static void service_dump(Unit *u, FILE *f, const char *prefix) { |
| 1212 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 1213 |
const char *prefix2; |
| 1214 |
|
| 1215 |
prefix = strempty(prefix); |
| 1216 |
prefix2 = strjoina(prefix, "\t"); |
| 1217 |
|
| 1218 |
fprintf(f, |
| 1219 |
"%sService State: %s\n" |
| 1220 |
"%sResult: %s\n" |
| 1221 |
"%sReload Result: %s\n" |
| 1222 |
"%sClean Result: %s\n" |
| 1223 |
"%sLiveMount Result: %s\n" |
| 1224 |
"%sPermissionsStartOnly: %s\n" |
| 1225 |
"%sRootDirectoryStartOnly: %s\n" |
| 1226 |
"%sRemainAfterExit: %s\n" |
| 1227 |
"%sGuessMainPID: %s\n" |
| 1228 |
"%sType: %s\n" |
| 1229 |
"%sRestart: %s\n" |
| 1230 |
"%sNotifyAccess: %s\n" |
| 1231 |
"%sNotifyState: %s\n" |
| 1232 |
"%sOOMPolicy: %s\n" |
| 1233 |
"%sReloadSignal: %s\n", |
| 1234 |
prefix, service_state_to_string(s->state), |
| 1235 |
prefix, service_result_to_string(s->result), |
| 1236 |
prefix, service_result_to_string(s->reload_result), |
| 1237 |
prefix, service_result_to_string(s->live_mount_result), |
| 1238 |
prefix, service_result_to_string(s->clean_result), |
| 1239 |
prefix, yes_no(s->permissions_start_only), |
| 1240 |
prefix, yes_no(s->root_directory_start_only), |
| 1241 |
prefix, yes_no(s->remain_after_exit), |
| 1242 |
prefix, yes_no(s->guess_main_pid), |
| 1243 |
prefix, service_type_to_string(s->type), |
| 1244 |
prefix, service_restart_to_string(s->restart), |
| 1245 |
prefix, notify_access_to_string(service_get_notify_access(s)), |
| 1246 |
prefix, notify_state_to_string(s->notify_state), |
| 1247 |
prefix, oom_policy_to_string(s->oom_policy), |
| 1248 |
prefix, signal_to_string(s->reload_signal)); |
| 1249 |
|
| 1250 |
if (pidref_is_set(&s->control_pid)) |
| 1251 |
fprintf(f, |
| 1252 |
"%sControl PID: "PID_FMT"\n", |
| 1253 |
prefix, s->control_pid.pid); |
| 1254 |
|
| 1255 |
if (pidref_is_set(&s->main_pid)) |
| 1256 |
fprintf(f, |
| 1257 |
"%sMain PID: "PID_FMT"\n" |
| 1258 |
"%sMain PID Known: %s\n" |
| 1259 |
"%sMain PID Alien: %s\n", |
| 1260 |
prefix, s->main_pid.pid, |
| 1261 |
prefix, yes_no(s->main_pid_known), |
| 1262 |
prefix, yes_no(s->main_pid_alien)); |
| 1263 |
|
| 1264 |
if (s->pid_file) |
| 1265 |
fprintf(f, |
| 1266 |
"%sPIDFile: %s\n", |
| 1267 |
prefix, s->pid_file); |
| 1268 |
|
| 1269 |
if (s->bus_name) |
| 1270 |
fprintf(f, |
| 1271 |
"%sBusName: %s\n" |
| 1272 |
"%sBus Name Good: %s\n", |
| 1273 |
prefix, s->bus_name, |
| 1274 |
prefix, yes_no(s->bus_name_good)); |
| 1275 |
|
| 1276 |
if (UNIT_ISSET(s->accept_socket)) |
| 1277 |
fprintf(f, |
| 1278 |
"%sAccept Socket: %s\n", |
| 1279 |
prefix, UNIT_DEREF(s->accept_socket)->id); |
| 1280 |
|
| 1281 |
fprintf(f, |
| 1282 |
"%sRestartSec: %s\n" |
| 1283 |
"%sRestartSteps: %u\n" |
| 1284 |
"%sRestartMaxDelaySec: %s\n" |
| 1285 |
"%sTimeoutStartSec: %s\n" |
| 1286 |
"%sTimeoutStopSec: %s\n" |
| 1287 |
"%sTimeoutStartFailureMode: %s\n" |
| 1288 |
"%sTimeoutStopFailureMode: %s\n", |
| 1289 |
prefix, FORMAT_TIMESPAN(s->restart_usec, USEC_PER_SEC), |
| 1290 |
prefix, s->restart_steps, |
| 1291 |
prefix, FORMAT_TIMESPAN(s->restart_max_delay_usec, USEC_PER_SEC), |
| 1292 |
prefix, FORMAT_TIMESPAN(s->timeout_start_usec, USEC_PER_SEC), |
| 1293 |
prefix, FORMAT_TIMESPAN(s->timeout_stop_usec, USEC_PER_SEC), |
| 1294 |
prefix, service_timeout_failure_mode_to_string(s->timeout_start_failure_mode), |
| 1295 |
prefix, service_timeout_failure_mode_to_string(s->timeout_stop_failure_mode)); |
| 1296 |
|
| 1297 |
if (s->timeout_abort_set) |
| 1298 |
fprintf(f, |
| 1299 |
"%sTimeoutAbortSec: %s\n", |
| 1300 |
prefix, FORMAT_TIMESPAN(s->timeout_abort_usec, USEC_PER_SEC)); |
| 1301 |
|
| 1302 |
fprintf(f, |
| 1303 |
"%sRuntimeMaxSec: %s\n" |
| 1304 |
"%sRuntimeRandomizedExtraSec: %s\n" |
| 1305 |
"%sWatchdogSec: %s\n", |
| 1306 |
prefix, FORMAT_TIMESPAN(s->runtime_max_usec, USEC_PER_SEC), |
| 1307 |
prefix, FORMAT_TIMESPAN(s->runtime_rand_extra_usec, USEC_PER_SEC), |
| 1308 |
prefix, FORMAT_TIMESPAN(s->watchdog_usec, USEC_PER_SEC)); |
| 1309 |
|
| 1310 |
kill_context_dump(&s->kill_context, f, prefix); |
| 1311 |
exec_context_dump(&s->exec_context, f, prefix); |
| 1312 |
|
| 1313 |
for (ServiceExecCommand c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) { |
| 1314 |
if (!s->exec_command[c]) |
| 1315 |
continue; |
| 1316 |
|
| 1317 |
fprintf(f, "%s%s %s:\n", |
| 1318 |
prefix, glyph(GLYPH_ARROW_RIGHT), service_exec_command_to_string(c)); |
| 1319 |
|
| 1320 |
exec_command_dump_list(s->exec_command[c], f, prefix2); |
| 1321 |
} |
| 1322 |
|
| 1323 |
if (s->status_text) |
| 1324 |
fprintf(f, "%sStatus Text: %s\n", |
| 1325 |
prefix, s->status_text); |
| 1326 |
|
| 1327 |
if (s->status_errno > 0) |
| 1328 |
fprintf(f, "%sStatus Errno: %s\n", |
| 1329 |
prefix, STRERROR(s->status_errno)); |
| 1330 |
|
| 1331 |
if (s->status_bus_error) |
| 1332 |
fprintf(f, "%sStatus Bus Error: %s\n", |
| 1333 |
prefix, s->status_bus_error); |
| 1334 |
|
| 1335 |
if (s->status_varlink_error) |
| 1336 |
fprintf(f, "%sStatus Varlink Error: %s\n", |
| 1337 |
prefix, s->status_varlink_error); |
| 1338 |
|
| 1339 |
if (s->n_fd_store_max > 0) { |
| 1340 |
fprintf(f, |
| 1341 |
"%sFile Descriptor Store Max: %u\n" |
| 1342 |
"%sFile Descriptor Store Pin: %s\n" |
| 1343 |
"%sFile Descriptor Store Current: %zu\n", |
| 1344 |
prefix, s->n_fd_store_max, |
| 1345 |
prefix, exec_preserve_mode_to_string(s->fd_store_preserve_mode), |
| 1346 |
prefix, s->n_fd_store); |
| 1347 |
|
| 1348 |
LIST_FOREACH(fd_store, i, s->fd_store) |
| 1349 |
(void) service_dump_fd(i->fd, |
| 1350 |
i->fdname, |
| 1351 |
i == s->fd_store ? "File Descriptor Store Entry:" : " ", |
| 1352 |
f, |
| 1353 |
prefix); |
| 1354 |
} |
| 1355 |
|
| 1356 |
FOREACH_ARRAY(i, s->extra_fds, s->n_extra_fds) |
| 1357 |
(void) service_dump_fd(i->fd, |
| 1358 |
i->fdname, |
| 1359 |
i == s->extra_fds ? "Extra File Descriptor Entry:" : " ", |
| 1360 |
f, |
| 1361 |
prefix); |
| 1362 |
|
| 1363 |
if (s->root_directory_fd >= 0) |
| 1364 |
(void) service_dump_fd(s->root_directory_fd, "Root Directory File Descriptor", "", f, prefix); |
| 1365 |
|
| 1366 |
if (s->open_files) |
| 1367 |
LIST_FOREACH(open_files, of, s->open_files) { |
| 1368 |
_cleanup_free_ char *ofs = NULL; |
| 1369 |
int r; |
| 1370 |
|
| 1371 |
r = open_file_to_string(of, &ofs); |
| 1372 |
if (r < 0) { |
| 1373 |
log_debug_errno(r, |
| 1374 |
"Failed to convert OpenFile= setting to string, ignoring: %m"); |
| 1375 |
continue; |
| 1376 |
} |
| 1377 |
|
| 1378 |
fprintf(f, "%sOpen File: %s\n", prefix, ofs); |
| 1379 |
} |
| 1380 |
|
| 1381 |
cgroup_context_dump(UNIT(s), f, prefix); |
| 1382 |
} |
| 1383 |
|
| 1384 |
static int service_is_suitable_main_pid(Service *s, PidRef *pid, int prio) { |
| 1385 |
Unit *owner; |
| 1386 |
int r; |
| 1387 |
|
| 1388 |
assert(s); |
| 1389 |
assert(pidref_is_set(pid)); |
| 1390 |
|
| 1391 |
/* Checks whether the specified PID is suitable as main PID for this service. returns negative if not, 0 if the |
| 1392 |
* PID is questionnable but should be accepted if the source of configuration is trusted. > 0 if the PID is |
| 1393 |
* good */ |
| 1394 |
|
| 1395 |
if (pidref_is_self(pid) || pid->pid == 1) |
| 1396 |
return log_unit_full_errno(UNIT(s), prio, SYNTHETIC_ERRNO(EPERM), "New main PID "PID_FMT" is the manager, refusing.", pid->pid); |
| 1397 |
|
| 1398 |
if (pidref_equal(pid, &s->control_pid)) |
| 1399 |
return log_unit_full_errno(UNIT(s), prio, SYNTHETIC_ERRNO(EPERM), "New main PID "PID_FMT" is the control process, refusing.", pid->pid); |
| 1400 |
|
| 1401 |
r = pidref_is_alive(pid); |
| 1402 |
if (r < 0) |
| 1403 |
return log_unit_full_errno(UNIT(s), prio, r, "Failed to check if main PID "PID_FMT" exists or is a zombie: %m", pid->pid); |
| 1404 |
if (r == 0) |
| 1405 |
return log_unit_full_errno(UNIT(s), prio, SYNTHETIC_ERRNO(ESRCH), "New main PID "PID_FMT" does not exist or is a zombie.", pid->pid); |
| 1406 |
|
| 1407 |
owner = manager_get_unit_by_pidref(UNIT(s)->manager, pid); |
| 1408 |
if (owner == UNIT(s)) { |
| 1409 |
log_unit_debug(UNIT(s), "New main PID "PID_FMT" belongs to service, we are happy.", pid->pid); |
| 1410 |
return 1; /* Yay, it's definitely a good PID */ |
| 1411 |
} |
| 1412 |
|
| 1413 |
return 0; /* Hmm it's a suspicious PID, let's accept it if configuration source is trusted */ |
| 1414 |
} |
| 1415 |
|
| 1416 |
static int service_load_pid_file(Service *s, bool may_warn) { |
| 1417 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 1418 |
_cleanup_fclose_ FILE *f = NULL; |
| 1419 |
_cleanup_free_ char *k = NULL; |
| 1420 |
bool questionable_pid_file = false; |
| 1421 |
int r, prio = may_warn ? LOG_INFO : LOG_DEBUG; |
| 1422 |
|
| 1423 |
assert(s); |
| 1424 |
|
| 1425 |
if (!s->pid_file) |
| 1426 |
return -ENOENT; |
| 1427 |
|
| 1428 |
r = chase_and_fopen_unlocked(s->pid_file, NULL, CHASE_SAFE, "re", NULL, &f); |
| 1429 |
if (r == -ENOLINK) { |
| 1430 |
log_unit_debug_errno(UNIT(s), r, |
| 1431 |
"Potentially unsafe symlink chain, will now retry with relaxed checks: %s", s->pid_file); |
| 1432 |
|
| 1433 |
questionable_pid_file = true; |
| 1434 |
|
| 1435 |
r = chase_and_fopen_unlocked(s->pid_file, NULL, 0, "re", NULL, &f); |
| 1436 |
} |
| 1437 |
if (r < 0) |
| 1438 |
return log_unit_full_errno(UNIT(s), prio, r, |
| 1439 |
"Can't open PID file '%s' (yet?) after %s: %m", s->pid_file, service_state_to_string(s->state)); |
| 1440 |
|
| 1441 |
/* Let's read the PID file now that we chased it down. */ |
| 1442 |
r = read_line(f, LINE_MAX, &k); |
| 1443 |
if (r < 0) |
| 1444 |
return log_unit_error_errno(UNIT(s), r, "Failed to read PID file '%s': %m", s->pid_file); |
| 1445 |
|
| 1446 |
r = pidref_set_pidstr(&pidref, k); |
| 1447 |
if (r < 0) |
| 1448 |
return log_unit_full_errno(UNIT(s), prio, r, "Failed to create reference to PID %s from file '%s': %m", k, s->pid_file); |
| 1449 |
|
| 1450 |
if (s->main_pid_known && pidref_equal(&pidref, &s->main_pid)) |
| 1451 |
return 0; |
| 1452 |
|
| 1453 |
r = service_is_suitable_main_pid(s, &pidref, prio); |
| 1454 |
if (r < 0) |
| 1455 |
return r; |
| 1456 |
if (r == 0) { |
| 1457 |
struct stat st; |
| 1458 |
|
| 1459 |
if (questionable_pid_file) |
| 1460 |
return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(EPERM), |
| 1461 |
"Refusing to accept PID outside of service control group, acquired through unsafe symlink chain: %s", s->pid_file); |
| 1462 |
|
| 1463 |
/* Hmm, it's not clear if the new main PID is safe. Let's allow this if the PID file is owned by root */ |
| 1464 |
|
| 1465 |
if (fstat(fileno(f), &st) < 0) |
| 1466 |
return log_unit_error_errno(UNIT(s), errno, "Failed to fstat() PID file '%s': %m", s->pid_file); |
| 1467 |
|
| 1468 |
if (st.st_uid != getuid()) |
| 1469 |
return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(EPERM), |
| 1470 |
"New main PID "PID_FMT" from PID file does not belong to service, and PID file is owned by "UID_FMT" (must be owned by "UID_FMT"). Refusing.", |
| 1471 |
pidref.pid, st.st_uid, getuid()); |
| 1472 |
|
| 1473 |
log_unit_debug(UNIT(s), "New main PID "PID_FMT" does not belong to service, accepting anyway since PID file is owned by "UID_FMT".", |
| 1474 |
pidref.pid, st.st_uid); |
| 1475 |
} |
| 1476 |
|
| 1477 |
if (s->main_pid_known) { |
| 1478 |
log_unit_debug(UNIT(s), "Main PID changing: "PID_FMT" -> "PID_FMT, s->main_pid.pid, pidref.pid); |
| 1479 |
|
| 1480 |
service_unwatch_main_pid(s); |
| 1481 |
s->main_pid_known = false; |
| 1482 |
} else |
| 1483 |
log_unit_debug(UNIT(s), "Main PID loaded: "PID_FMT, pidref.pid); |
| 1484 |
|
| 1485 |
r = service_set_main_pidref(s, TAKE_PIDREF(pidref), /* start_timestamp= */ NULL); |
| 1486 |
if (r < 0) |
| 1487 |
return r; |
| 1488 |
|
| 1489 |
r = unit_watch_pidref(UNIT(s), &s->main_pid, /* exclusive= */ false); |
| 1490 |
if (r < 0) /* FIXME: we need to do something here */ |
| 1491 |
return log_unit_warning_errno(UNIT(s), r, "Failed to watch PID "PID_FMT" for service: %m", s->main_pid.pid); |
| 1492 |
|
| 1493 |
return 1; |
| 1494 |
} |
| 1495 |
|
| 1496 |
static void service_search_main_pid(Service *s) { |
| 1497 |
_cleanup_(pidref_done) PidRef pid = PIDREF_NULL; |
| 1498 |
int r; |
| 1499 |
|
| 1500 |
assert(s); |
| 1501 |
|
| 1502 |
/* If we know it anyway, don't ever fall back to unreliable heuristics */ |
| 1503 |
if (s->main_pid_known) |
| 1504 |
return; |
| 1505 |
|
| 1506 |
if (!s->guess_main_pid) |
| 1507 |
return; |
| 1508 |
|
| 1509 |
assert(!pidref_is_set(&s->main_pid)); |
| 1510 |
|
| 1511 |
if (unit_search_main_pid(UNIT(s), &pid) < 0) |
| 1512 |
return; |
| 1513 |
|
| 1514 |
log_unit_debug(UNIT(s), "Main PID guessed: "PID_FMT, pid.pid); |
| 1515 |
if (service_set_main_pidref(s, TAKE_PIDREF(pid), /* start_timestamp= */ NULL) < 0) |
| 1516 |
return; |
| 1517 |
|
| 1518 |
r = unit_watch_pidref(UNIT(s), &s->main_pid, /* exclusive= */ false); |
| 1519 |
if (r < 0) |
| 1520 |
/* FIXME: we need to do something here */ |
| 1521 |
log_unit_warning_errno(UNIT(s), r, "Failed to watch main PID "PID_FMT": %m", s->main_pid.pid); |
| 1522 |
} |
| 1523 |
|
| 1524 |
static void service_set_state(Service *s, ServiceState state) { |
| 1525 |
Unit *u = UNIT(ASSERT_PTR(s)); |
| 1526 |
ServiceState old_state; |
| 1527 |
const UnitActiveState *table; |
| 1528 |
|
| 1529 |
if (s->state != state) |
| 1530 |
bus_unit_send_pending_change_signal(u, false); |
| 1531 |
|
| 1532 |
table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table; |
| 1533 |
|
| 1534 |
old_state = s->state; |
| 1535 |
s->state = state; |
| 1536 |
|
| 1537 |
service_unwatch_pid_file(s); |
| 1538 |
|
| 1539 |
if (!IN_SET(state, |
| 1540 |
SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, |
| 1541 |
SERVICE_RUNNING, |
| 1542 |
SERVICE_REFRESH_EXTENSIONS, SERVICE_REFRESH_CREDENTIALS, |
| 1543 |
SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, SERVICE_RELOAD_POST, |
| 1544 |
SERVICE_MOUNTING, |
| 1545 |
SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
| 1546 |
SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, |
| 1547 |
SERVICE_AUTO_RESTART, |
| 1548 |
SERVICE_CLEANING)) |
| 1549 |
s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
| 1550 |
|
| 1551 |
if (!SERVICE_STATE_WITH_MAIN_PROCESS(state)) { |
| 1552 |
service_unwatch_main_pid(s); |
| 1553 |
s->main_command = NULL; |
| 1554 |
} |
| 1555 |
|
| 1556 |
if (!SERVICE_STATE_WITH_CONTROL_PROCESS(state)) { |
| 1557 |
service_unwatch_control_pid(s); |
| 1558 |
s->control_command = NULL; |
| 1559 |
s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
| 1560 |
} |
| 1561 |
|
| 1562 |
if (IN_SET(state, |
| 1563 |
SERVICE_DEAD, SERVICE_FAILED, |
| 1564 |
SERVICE_DEAD_BEFORE_AUTO_RESTART, SERVICE_FAILED_BEFORE_AUTO_RESTART, SERVICE_AUTO_RESTART, SERVICE_AUTO_RESTART_QUEUED, |
| 1565 |
SERVICE_DEAD_RESOURCES_PINNED)) |
| 1566 |
unit_unwatch_all_pids(u); |
| 1567 |
|
| 1568 |
if (state != SERVICE_START) |
| 1569 |
s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); |
| 1570 |
|
| 1571 |
if (!SERVICE_STATE_WITH_WATCHDOG(state)) |
| 1572 |
service_stop_watchdog(s); |
| 1573 |
|
| 1574 |
if (state != SERVICE_MOUNTING) /* Just in case */ |
| 1575 |
s->mount_request = sd_bus_message_unref(s->mount_request); |
| 1576 |
|
| 1577 |
if (state == SERVICE_EXITED && !MANAGER_IS_RELOADING(u->manager)) { |
| 1578 |
/* For the inactive states unit_notify() will trim the cgroup. But for exit we have to |
| 1579 |
* do that ourselves... */ |
| 1580 |
unit_prune_cgroup(u); |
| 1581 |
|
| 1582 |
/* If none of ExecReload= and ExecStop*= is used, we can safely destroy runtime data |
| 1583 |
* as soon as the service enters SERVICE_EXITED. This saves us from keeping the credential mount |
| 1584 |
* for the whole duration of the oneshot service while no processes are actually running, |
| 1585 |
* among other things. */ |
| 1586 |
|
| 1587 |
bool start_only = true; |
| 1588 |
for (ServiceExecCommand c = SERVICE_EXEC_RELOAD; c < _SERVICE_EXEC_COMMAND_MAX; c++) |
| 1589 |
if (s->exec_command[c]) { |
| 1590 |
start_only = false; |
| 1591 |
break; |
| 1592 |
} |
| 1593 |
|
| 1594 |
if (start_only) |
| 1595 |
unit_destroy_runtime_data(u, &s->exec_context, /* destroy_runtime_dir= */ false); |
| 1596 |
} |
| 1597 |
|
| 1598 |
if (old_state != state) |
| 1599 |
log_unit_debug(u, "Changed %s -> %s", service_state_to_string(old_state), service_state_to_string(state)); |
| 1600 |
|
| 1601 |
unit_notify(u, table[old_state], table[state], s->reload_result == SERVICE_SUCCESS); |
| 1602 |
} |
| 1603 |
|
| 1604 |
static usec_t service_coldplug_timeout(Service *s) { |
| 1605 |
assert(s); |
| 1606 |
|
| 1607 |
switch (s->deserialized_state) { |
| 1608 |
|
| 1609 |
case SERVICE_CONDITION: |
| 1610 |
case SERVICE_START_PRE: |
| 1611 |
case SERVICE_START: |
| 1612 |
case SERVICE_START_POST: |
| 1613 |
case SERVICE_REFRESH_EXTENSIONS: |
| 1614 |
case SERVICE_REFRESH_CREDENTIALS: |
| 1615 |
case SERVICE_RELOAD: |
| 1616 |
case SERVICE_RELOAD_SIGNAL: |
| 1617 |
case SERVICE_RELOAD_NOTIFY: |
| 1618 |
case SERVICE_RELOAD_POST: |
| 1619 |
case SERVICE_MOUNTING: |
| 1620 |
return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->timeout_start_usec); |
| 1621 |
|
| 1622 |
case SERVICE_RUNNING: |
| 1623 |
return service_running_timeout(s); |
| 1624 |
|
| 1625 |
case SERVICE_STOP: |
| 1626 |
case SERVICE_STOP_SIGTERM: |
| 1627 |
case SERVICE_STOP_SIGKILL: |
| 1628 |
case SERVICE_STOP_POST: |
| 1629 |
case SERVICE_FINAL_SIGTERM: |
| 1630 |
case SERVICE_FINAL_SIGKILL: |
| 1631 |
return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->timeout_stop_usec); |
| 1632 |
|
| 1633 |
case SERVICE_STOP_WATCHDOG: |
| 1634 |
case SERVICE_FINAL_WATCHDOG: |
| 1635 |
return usec_add(UNIT(s)->state_change_timestamp.monotonic, service_timeout_abort_usec(s)); |
| 1636 |
|
| 1637 |
case SERVICE_AUTO_RESTART: |
| 1638 |
return usec_add(UNIT(s)->inactive_enter_timestamp.monotonic, service_restart_usec_next(s)); |
| 1639 |
|
| 1640 |
case SERVICE_CLEANING: |
| 1641 |
return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->exec_context.timeout_clean_usec); |
| 1642 |
|
| 1643 |
default: |
| 1644 |
return USEC_INFINITY; |
| 1645 |
} |
| 1646 |
} |
| 1647 |
|
| 1648 |
static int service_coldplug(Unit *u) { |
| 1649 |
Service *s = SERVICE(u); |
| 1650 |
int r; |
| 1651 |
|
| 1652 |
assert(s); |
| 1653 |
/* Ensure we can insert FD store into units at boot */ |
| 1654 |
assert(IN_SET(s->state, SERVICE_DEAD, SERVICE_DEAD_RESOURCES_PINNED)); |
| 1655 |
|
| 1656 |
if (s->deserialized_state == s->state) |
| 1657 |
return 0; |
| 1658 |
|
| 1659 |
r = service_arm_timer(s, /* relative= */ false, service_coldplug_timeout(s)); |
| 1660 |
if (r < 0) |
| 1661 |
return r; |
| 1662 |
|
| 1663 |
if (pidref_is_set(&s->main_pid) && |
| 1664 |
pidref_is_unwaited(&s->main_pid) > 0 && |
| 1665 |
SERVICE_STATE_WITH_MAIN_PROCESS(s->deserialized_state)) { |
| 1666 |
r = unit_watch_pidref(UNIT(s), &s->main_pid, /* exclusive= */ false); |
| 1667 |
if (r < 0) |
| 1668 |
return r; |
| 1669 |
} |
| 1670 |
|
| 1671 |
if (pidref_is_set(&s->control_pid) && |
| 1672 |
pidref_is_unwaited(&s->control_pid) > 0 && |
| 1673 |
SERVICE_STATE_WITH_CONTROL_PROCESS(s->deserialized_state)) { |
| 1674 |
r = unit_watch_pidref(UNIT(s), &s->control_pid, /* exclusive= */ false); |
| 1675 |
if (r < 0) |
| 1676 |
return r; |
| 1677 |
} |
| 1678 |
|
| 1679 |
if (!IN_SET(s->deserialized_state, |
| 1680 |
SERVICE_DEAD, SERVICE_FAILED, |
| 1681 |
SERVICE_DEAD_BEFORE_AUTO_RESTART, SERVICE_FAILED_BEFORE_AUTO_RESTART, SERVICE_AUTO_RESTART, SERVICE_AUTO_RESTART_QUEUED, |
| 1682 |
SERVICE_CLEANING, |
| 1683 |
SERVICE_DEAD_RESOURCES_PINNED)) |
| 1684 |
(void) unit_setup_exec_runtime(u); |
| 1685 |
|
| 1686 |
if (SERVICE_STATE_WITH_WATCHDOG(s->deserialized_state) && |
| 1687 |
freezer_state_objective(u->freezer_state) == FREEZER_RUNNING) |
| 1688 |
service_start_watchdog(s); |
| 1689 |
|
| 1690 |
if (UNIT_ISSET(s->accept_socket)) { |
| 1691 |
Socket *socket = SOCKET(UNIT_DEREF(s->accept_socket)); |
| 1692 |
|
| 1693 |
if (socket->max_connections_per_source > 0) { |
| 1694 |
SocketPeer *peer; |
| 1695 |
|
| 1696 |
/* Make a best-effort attempt at bumping the connection count */ |
| 1697 |
if (socket_acquire_peer(socket, s->socket_fd, &peer) > 0) { |
| 1698 |
socket_peer_unref(s->socket_peer); |
| 1699 |
s->socket_peer = peer; |
| 1700 |
} |
| 1701 |
} |
| 1702 |
} |
| 1703 |
|
| 1704 |
service_set_state(s, s->deserialized_state); |
| 1705 |
return 0; |
| 1706 |
} |
| 1707 |
|
| 1708 |
static int service_collect_fds( |
| 1709 |
Service *s, |
| 1710 |
int **fds, |
| 1711 |
char ***fd_names, |
| 1712 |
size_t *n_socket_fds, |
| 1713 |
size_t *n_stashed_fds) { |
| 1714 |
|
| 1715 |
_cleanup_strv_free_ char **rfd_names = NULL; |
| 1716 |
_cleanup_free_ int *rfds = NULL; |
| 1717 |
size_t rn_socket_fds = 0; |
| 1718 |
int r; |
| 1719 |
|
| 1720 |
assert(s); |
| 1721 |
assert(fds); |
| 1722 |
assert(fd_names); |
| 1723 |
assert(n_socket_fds); |
| 1724 |
|
| 1725 |
if (s->socket_fd >= 0) { |
| 1726 |
Socket *sock = ASSERT_PTR(SOCKET(UNIT_DEREF(s->accept_socket))); |
| 1727 |
|
| 1728 |
/* Pass the per-connection socket */ |
| 1729 |
|
| 1730 |
rfds = newdup(int, &s->socket_fd, 1); |
| 1731 |
if (!rfds) |
| 1732 |
return -ENOMEM; |
| 1733 |
|
| 1734 |
rfd_names = strv_new(socket_fdname(sock)); |
| 1735 |
if (!rfd_names) |
| 1736 |
return -ENOMEM; |
| 1737 |
|
| 1738 |
rn_socket_fds = 1; |
| 1739 |
} else { |
| 1740 |
/* Pass all our configured sockets for singleton services */ |
| 1741 |
|
| 1742 |
Unit *u; |
| 1743 |
UNIT_FOREACH_DEPENDENCY(u, UNIT(s), UNIT_ATOM_TRIGGERED_BY) { |
| 1744 |
_cleanup_free_ int *cfds = NULL; |
| 1745 |
int cn_fds; |
| 1746 |
Socket *sock; |
| 1747 |
|
| 1748 |
sock = SOCKET(u); |
| 1749 |
if (!sock) |
| 1750 |
continue; |
| 1751 |
|
| 1752 |
cn_fds = socket_collect_fds(sock, &cfds); |
| 1753 |
if (cn_fds < 0) |
| 1754 |
return cn_fds; |
| 1755 |
if (cn_fds == 0) |
| 1756 |
continue; |
| 1757 |
|
| 1758 |
if (!rfds) { |
| 1759 |
rfds = TAKE_PTR(cfds); |
| 1760 |
rn_socket_fds = cn_fds; |
| 1761 |
} else if (!GREEDY_REALLOC_APPEND(rfds, rn_socket_fds, cfds, cn_fds)) |
| 1762 |
return -ENOMEM; |
| 1763 |
|
| 1764 |
r = strv_extend_n(&rfd_names, socket_fdname(sock), cn_fds); |
| 1765 |
if (r < 0) |
| 1766 |
return r; |
| 1767 |
} |
| 1768 |
} |
| 1769 |
|
| 1770 |
if (n_stashed_fds && s->n_fd_store + s->n_extra_fds > 0) { |
| 1771 |
int *t = reallocarray(rfds, rn_socket_fds + s->n_fd_store + s->n_extra_fds, sizeof(int)); |
| 1772 |
if (!t) |
| 1773 |
return -ENOMEM; |
| 1774 |
rfds = t; |
| 1775 |
|
| 1776 |
char **nl = reallocarray(rfd_names, rn_socket_fds + s->n_fd_store + s->n_extra_fds + 1, sizeof(char *)); |
| 1777 |
if (!nl) |
| 1778 |
return -ENOMEM; |
| 1779 |
rfd_names = nl; |
| 1780 |
|
| 1781 |
size_t n_fds = rn_socket_fds; |
| 1782 |
|
| 1783 |
LIST_FOREACH(fd_store, fs, s->fd_store) { |
| 1784 |
rfds[n_fds] = fs->fd; |
| 1785 |
rfd_names[n_fds] = strdup(fs->fdname); |
| 1786 |
if (!rfd_names[n_fds]) |
| 1787 |
return -ENOMEM; |
| 1788 |
|
| 1789 |
n_fds++; |
| 1790 |
} |
| 1791 |
|
| 1792 |
FOREACH_ARRAY(i, s->extra_fds, s->n_extra_fds) { |
| 1793 |
rfds[n_fds] = i->fd; |
| 1794 |
rfd_names[n_fds] = strdup(i->fdname); |
| 1795 |
if (!rfd_names[n_fds]) |
| 1796 |
return -ENOMEM; |
| 1797 |
|
| 1798 |
n_fds++; |
| 1799 |
} |
| 1800 |
|
| 1801 |
rfd_names[n_fds] = NULL; |
| 1802 |
} |
| 1803 |
|
| 1804 |
*fds = TAKE_PTR(rfds); |
| 1805 |
*fd_names = TAKE_PTR(rfd_names); |
| 1806 |
*n_socket_fds = rn_socket_fds; |
| 1807 |
if (n_stashed_fds) |
| 1808 |
*n_stashed_fds = s->n_fd_store + s->n_extra_fds; |
| 1809 |
|
| 1810 |
return 0; |
| 1811 |
} |
| 1812 |
|
| 1813 |
static int service_allocate_exec_fd_event_source( |
| 1814 |
Service *s, |
| 1815 |
int fd, |
| 1816 |
sd_event_source **ret_event_source) { |
| 1817 |
|
| 1818 |
_cleanup_(sd_event_source_unrefp) sd_event_source *source = NULL; |
| 1819 |
int r; |
| 1820 |
|
| 1821 |
assert(s); |
| 1822 |
assert(fd >= 0); |
| 1823 |
assert(ret_event_source); |
| 1824 |
|
| 1825 |
r = sd_event_add_io(UNIT(s)->manager->event, &source, fd, 0, service_dispatch_exec_io, s); |
| 1826 |
if (r < 0) |
| 1827 |
return log_unit_error_errno(UNIT(s), r, "Failed to allocate exec_fd event source: %m"); |
| 1828 |
|
| 1829 |
/* This is a bit higher priority than SIGCHLD, to make sure we don't confuse the case "failed to |
| 1830 |
* start" from the case "succeeded to start, but failed immediately after". */ |
| 1831 |
|
| 1832 |
r = sd_event_source_set_priority(source, EVENT_PRIORITY_EXEC_FD); |
| 1833 |
if (r < 0) |
| 1834 |
return log_unit_error_errno(UNIT(s), r, "Failed to adjust priority of exec_fd event source: %m"); |
| 1835 |
|
| 1836 |
(void) sd_event_source_set_description(source, "service exec_fd"); |
| 1837 |
|
| 1838 |
r = sd_event_source_set_io_fd_own(source, true); |
| 1839 |
if (r < 0) |
| 1840 |
return log_unit_error_errno(UNIT(s), r, "Failed to pass ownership of fd to event source: %m"); |
| 1841 |
|
| 1842 |
*ret_event_source = TAKE_PTR(source); |
| 1843 |
return 0; |
| 1844 |
} |
| 1845 |
|
| 1846 |
static int service_allocate_exec_fd( |
| 1847 |
Service *s, |
| 1848 |
sd_event_source **ret_event_source, |
| 1849 |
int *ret_exec_fd) { |
| 1850 |
|
| 1851 |
_cleanup_close_pair_ int p[] = EBADF_PAIR; |
| 1852 |
int r; |
| 1853 |
|
| 1854 |
assert(s); |
| 1855 |
assert(ret_event_source); |
| 1856 |
assert(ret_exec_fd); |
| 1857 |
|
| 1858 |
if (pipe2(p, O_CLOEXEC|O_NONBLOCK) < 0) |
| 1859 |
return log_unit_error_errno(UNIT(s), errno, "Failed to allocate exec_fd pipe: %m"); |
| 1860 |
|
| 1861 |
r = service_allocate_exec_fd_event_source(s, p[0], ret_event_source); |
| 1862 |
if (r < 0) |
| 1863 |
return r; |
| 1864 |
|
| 1865 |
TAKE_FD(p[0]); |
| 1866 |
*ret_exec_fd = TAKE_FD(p[1]); |
| 1867 |
|
| 1868 |
return 0; |
| 1869 |
} |
| 1870 |
|
| 1871 |
static bool service_exec_needs_notify_socket(Service *s, ExecFlags flags) { |
| 1872 |
assert(s); |
| 1873 |
|
| 1874 |
/* Notifications are accepted depending on the process and |
| 1875 |
* the access setting of the service: |
| 1876 |
* process: \ access: NONE MAIN EXEC ALL |
| 1877 |
* main no yes yes yes |
| 1878 |
* control no no yes yes |
| 1879 |
* other (forked) no no no yes */ |
| 1880 |
|
| 1881 |
if (flags & EXEC_IS_CONTROL) |
| 1882 |
/* A control process */ |
| 1883 |
return IN_SET(service_get_notify_access(s), NOTIFY_EXEC, NOTIFY_ALL); |
| 1884 |
|
| 1885 |
/* We only spawn main processes and control processes, so any |
| 1886 |
* process that is not a control process is a main process */ |
| 1887 |
return service_get_notify_access(s) != NOTIFY_NONE; |
| 1888 |
} |
| 1889 |
|
| 1890 |
static Service *service_get_triggering_service(Service *s) { |
| 1891 |
Unit *candidate = NULL, *other; |
| 1892 |
|
| 1893 |
assert(s); |
| 1894 |
|
| 1895 |
/* Return the service which triggered service 's', this means dependency |
| 1896 |
* types which include the UNIT_ATOM_ON_{FAILURE,SUCCESS}_OF atoms. |
| 1897 |
* |
| 1898 |
* N.B. if there are multiple services which could trigger 's' via OnFailure= |
| 1899 |
* or OnSuccess= then we return NULL. This is since we don't know from which |
| 1900 |
* one to propagate the exit status. */ |
| 1901 |
|
| 1902 |
UNIT_FOREACH_DEPENDENCY(other, UNIT(s), UNIT_ATOM_ON_SUCCESS_OF|UNIT_ATOM_ON_FAILURE_OF) { |
| 1903 |
if (candidate) |
| 1904 |
goto have_other; |
| 1905 |
candidate = other; |
| 1906 |
} |
| 1907 |
|
| 1908 |
return SERVICE(candidate); |
| 1909 |
|
| 1910 |
have_other: |
| 1911 |
log_unit_warning(UNIT(s), "multiple trigger source candidates for exit status propagation (%s, %s), skipping.", |
| 1912 |
candidate->id, other->id); |
| 1913 |
return NULL; |
| 1914 |
} |
| 1915 |
|
| 1916 |
static ExecFlags service_exec_flags( |
| 1917 |
const Service *s, |
| 1918 |
ServiceExecCommand command_id, |
| 1919 |
ExecFlags cred_flag) { |
| 1920 |
|
| 1921 |
/* All service main/control processes honor sandboxing and namespacing options (except those |
| 1922 |
explicitly excluded in service_spawn()) */ |
| 1923 |
ExecFlags flags = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT; |
| 1924 |
|
| 1925 |
assert(s); |
| 1926 |
assert(command_id >= 0); |
| 1927 |
assert(command_id < _SERVICE_EXEC_COMMAND_MAX); |
| 1928 |
assert((cred_flag & ~(EXEC_SETUP_CREDENTIALS_FRESH|EXEC_SETUP_CREDENTIALS)) == 0); |
| 1929 |
assert((cred_flag != 0) == (command_id == SERVICE_EXEC_START)); |
| 1930 |
|
| 1931 |
/* Control processes spawned before main process also get tty access */ |
| 1932 |
if (IN_SET(command_id, SERVICE_EXEC_CONDITION, SERVICE_EXEC_START_PRE, SERVICE_EXEC_START)) |
| 1933 |
flags |= EXEC_APPLY_TTY_STDIN; |
| 1934 |
|
| 1935 |
/* All start phases get access to credentials. ExecStartPre= gets a new credential store upon |
| 1936 |
* every invocation, so that updating credential files through it works. When the first main process |
| 1937 |
* starts, passed creds become stable. Also see 'cred_flag'. */ |
| 1938 |
if (command_id == SERVICE_EXEC_CONDITION) |
| 1939 |
flags |= EXEC_SETUP_CREDENTIALS; |
| 1940 |
if (command_id == SERVICE_EXEC_START_PRE) |
| 1941 |
flags |= EXEC_SETUP_CREDENTIALS_FRESH; |
| 1942 |
if (command_id == SERVICE_EXEC_START_POST) |
| 1943 |
flags |= EXEC_SETUP_CREDENTIALS; |
| 1944 |
|
| 1945 |
if (IN_SET(command_id, SERVICE_EXEC_START_PRE, SERVICE_EXEC_START)) |
| 1946 |
flags |= EXEC_SETENV_MONITOR_RESULT; |
| 1947 |
|
| 1948 |
if (command_id == SERVICE_EXEC_START) |
| 1949 |
return flags|cred_flag|EXEC_PASS_FDS|EXEC_SET_WATCHDOG; |
| 1950 |
|
| 1951 |
flags |= EXEC_IS_CONTROL; |
| 1952 |
|
| 1953 |
/* Put control processes spawned later than main process under .control sub-cgroup if appropriate */ |
| 1954 |
if (!IN_SET(command_id, SERVICE_EXEC_CONDITION, SERVICE_EXEC_START_PRE)) |
| 1955 |
flags |= EXEC_CONTROL_CGROUP; |
| 1956 |
|
| 1957 |
/* Pass credentials to ExecReload*= too, but only if the credentials are actually refreshed, |
| 1958 |
* to make sure they have the same understanding of the world as the main process. */ |
| 1959 |
if (IN_SET(command_id, SERVICE_EXEC_RELOAD, SERVICE_EXEC_RELOAD_POST) && |
| 1960 |
FLAGS_SET(s->refreshed_mask, SERVICE_RELOAD_CREDENTIALS)) |
| 1961 |
flags |= EXEC_SETUP_CREDENTIALS; |
| 1962 |
|
| 1963 |
if (IN_SET(command_id, SERVICE_EXEC_STOP, SERVICE_EXEC_STOP_POST)) |
| 1964 |
flags |= EXEC_SETENV_RESULT; |
| 1965 |
|
| 1966 |
return flags; |
| 1967 |
} |
| 1968 |
|
| 1969 |
static int service_spawn_internal( |
| 1970 |
const char *caller, |
| 1971 |
Service *s, |
| 1972 |
ExecCommand *c, |
| 1973 |
ExecFlags flags, |
| 1974 |
usec_t timeout, |
| 1975 |
PidRef *ret_pid) { |
| 1976 |
|
| 1977 |
_cleanup_(exec_params_shallow_clear) ExecParameters exec_params = EXEC_PARAMETERS_INIT(flags); |
| 1978 |
_cleanup_(sd_event_source_unrefp) sd_event_source *exec_fd_source = NULL; |
| 1979 |
_cleanup_strv_free_ char **final_env = NULL, **our_env = NULL; |
| 1980 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 1981 |
size_t n_env = 0; |
| 1982 |
int r; |
| 1983 |
|
| 1984 |
assert(caller); |
| 1985 |
assert(s); |
| 1986 |
assert(c); |
| 1987 |
assert(ret_pid); |
| 1988 |
|
| 1989 |
log_unit_debug(UNIT(s), "Will spawn child (%s): %s", caller, c->path); |
| 1990 |
|
| 1991 |
r = unit_prepare_exec(UNIT(s)); /* This realizes the cgroup, among other things */ |
| 1992 |
if (r < 0) |
| 1993 |
return r; |
| 1994 |
|
| 1995 |
assert(!s->exec_fd_event_source); |
| 1996 |
|
| 1997 |
if (FLAGS_SET(exec_params.flags, EXEC_IS_CONTROL)) { |
| 1998 |
/* If this is a control process, mask the permissions/chroot application if this is requested. */ |
| 1999 |
if (s->permissions_start_only) |
| 2000 |
exec_params.flags &= ~EXEC_APPLY_SANDBOXING; |
| 2001 |
if (s->root_directory_start_only) |
| 2002 |
exec_params.flags &= ~EXEC_APPLY_CHROOT; |
| 2003 |
} |
| 2004 |
|
| 2005 |
if (FLAGS_SET(exec_params.flags, EXEC_PASS_FDS)) { |
| 2006 |
r = service_collect_fds(s, |
| 2007 |
&exec_params.fds, |
| 2008 |
&exec_params.fd_names, |
| 2009 |
&exec_params.n_socket_fds, |
| 2010 |
&exec_params.n_stashed_fds); |
| 2011 |
if (r < 0) |
| 2012 |
return r; |
| 2013 |
|
| 2014 |
log_unit_debug(UNIT(s), "Passing %zu fds to service", exec_params.n_socket_fds + exec_params.n_stashed_fds); |
| 2015 |
|
| 2016 |
exec_params.open_files = s->open_files; |
| 2017 |
|
| 2018 |
} else if (IN_SET(s->exec_context.std_input, EXEC_INPUT_SOCKET, EXEC_INPUT_NAMED_FD) || |
| 2019 |
IN_SET(s->exec_context.std_output, EXEC_OUTPUT_SOCKET, EXEC_OUTPUT_NAMED_FD) || |
| 2020 |
IN_SET(s->exec_context.std_error, EXEC_OUTPUT_SOCKET, EXEC_OUTPUT_NAMED_FD)) { |
| 2021 |
|
| 2022 |
r = service_collect_fds(s, |
| 2023 |
&exec_params.fds, |
| 2024 |
&exec_params.fd_names, |
| 2025 |
&exec_params.n_socket_fds, |
| 2026 |
/* n_stashed_fds= */ NULL); |
| 2027 |
if (r < 0) |
| 2028 |
return r; |
| 2029 |
|
| 2030 |
log_unit_debug(UNIT(s), "Passing %zu sockets to service", exec_params.n_socket_fds); |
| 2031 |
} |
| 2032 |
|
| 2033 |
if (!FLAGS_SET(exec_params.flags, EXEC_IS_CONTROL) && s->type == SERVICE_EXEC) { |
| 2034 |
r = service_allocate_exec_fd(s, &exec_fd_source, &exec_params.exec_fd); |
| 2035 |
if (r < 0) |
| 2036 |
return r; |
| 2037 |
} |
| 2038 |
|
| 2039 |
r = service_arm_timer(s, /* relative= */ true, timeout); |
| 2040 |
if (r < 0) |
| 2041 |
return r; |
| 2042 |
|
| 2043 |
our_env = new0(char*, 16); |
| 2044 |
if (!our_env) |
| 2045 |
return -ENOMEM; |
| 2046 |
|
| 2047 |
if (service_exec_needs_notify_socket(s, exec_params.flags)) { |
| 2048 |
exec_params.notify_socket = UNIT(s)->manager->notify_socket; |
| 2049 |
|
| 2050 |
if (s->n_fd_store_max > 0) |
| 2051 |
if (asprintf(our_env + n_env++, "FDSTORE=%u", s->n_fd_store_max) < 0) |
| 2052 |
return -ENOMEM; |
| 2053 |
} |
| 2054 |
|
| 2055 |
if (pidref_is_set(&s->main_pid)) { |
| 2056 |
if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid.pid) < 0) |
| 2057 |
return -ENOMEM; |
| 2058 |
|
| 2059 |
if (pidref_acquire_pidfd_id(&s->main_pid) >= 0) |
| 2060 |
if (asprintf(our_env + n_env++, "MAINPIDFDID=%" PRIu64, s->main_pid.fd_id) < 0) |
| 2061 |
return -ENOMEM; |
| 2062 |
} |
| 2063 |
|
| 2064 |
if (MANAGER_IS_USER(UNIT(s)->manager) && |
| 2065 |
!exec_needs_pid_namespace(&s->exec_context, /* params= */ NULL)) { |
| 2066 |
|
| 2067 |
if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid_cached()) < 0) |
| 2068 |
return -ENOMEM; |
| 2069 |
|
| 2070 |
uint64_t pidfdid; |
| 2071 |
if (pidfd_get_inode_id_self_cached(&pidfdid) >= 0) |
| 2072 |
if (asprintf(our_env + n_env++, "MANAGERPIDFDID=%" PRIu64, pidfdid) < 0) |
| 2073 |
return -ENOMEM; |
| 2074 |
} |
| 2075 |
|
| 2076 |
if (s->pid_file) |
| 2077 |
if (asprintf(our_env + n_env++, "PIDFILE=%s", s->pid_file) < 0) |
| 2078 |
return -ENOMEM; |
| 2079 |
|
| 2080 |
if (s->socket_fd >= 0) { |
| 2081 |
union sockaddr_union sa; |
| 2082 |
socklen_t salen = sizeof(sa); |
| 2083 |
|
| 2084 |
/* If this is a per-connection service instance, let's set $REMOTE_ADDR and $REMOTE_PORT to something |
| 2085 |
* useful. Note that we do this only when we are still connected at this point in time, which we might |
| 2086 |
* very well not be. Hence we ignore all errors when retrieving peer information (as that might result |
| 2087 |
* in ENOTCONN), and just use whate we can use. */ |
| 2088 |
|
| 2089 |
if (getpeername(s->socket_fd, &sa.sa, &salen) >= 0 && |
| 2090 |
IN_SET(sa.sa.sa_family, AF_INET, AF_INET6, AF_VSOCK, AF_UNIX)) { |
| 2091 |
_cleanup_free_ char *addr = NULL; |
| 2092 |
char *t; |
| 2093 |
|
| 2094 |
r = sockaddr_pretty(&sa.sa, salen, /* translate_ipv6= */ true, /* include_port= */ false, &addr); |
| 2095 |
if (r < 0) |
| 2096 |
return r; |
| 2097 |
|
| 2098 |
if (sa.sa.sa_family != AF_UNIX || IN_SET(addr[0], '/', '@')) { |
| 2099 |
t = strjoin("REMOTE_ADDR=", addr); |
| 2100 |
if (!t) |
| 2101 |
return -ENOMEM; |
| 2102 |
our_env[n_env++] = t; |
| 2103 |
} |
| 2104 |
|
| 2105 |
if (IN_SET(sa.sa.sa_family, AF_INET, AF_INET6, AF_VSOCK)) { |
| 2106 |
unsigned port; |
| 2107 |
|
| 2108 |
r = sockaddr_port(&sa.sa, &port); |
| 2109 |
if (r < 0) |
| 2110 |
return r; |
| 2111 |
|
| 2112 |
if (asprintf(&t, "REMOTE_PORT=%u", port) < 0) |
| 2113 |
return -ENOMEM; |
| 2114 |
our_env[n_env++] = t; |
| 2115 |
} |
| 2116 |
} |
| 2117 |
|
| 2118 |
uint64_t cookie; |
| 2119 |
if (socket_get_cookie(s->socket_fd, &cookie) >= 0) { |
| 2120 |
char *t; |
| 2121 |
if (asprintf(&t, "SO_COOKIE=%" PRIu64, cookie) < 0) |
| 2122 |
return -ENOMEM; |
| 2123 |
our_env[n_env++] = t; |
| 2124 |
} |
| 2125 |
} |
| 2126 |
|
| 2127 |
Service *env_source = NULL; |
| 2128 |
const char *monitor_prefix; |
| 2129 |
if (FLAGS_SET(exec_params.flags, EXEC_SETENV_RESULT)) { |
| 2130 |
env_source = s; |
| 2131 |
monitor_prefix = ""; |
| 2132 |
} else if (FLAGS_SET(exec_params.flags, EXEC_SETENV_MONITOR_RESULT)) { |
| 2133 |
env_source = service_get_triggering_service(s); |
| 2134 |
monitor_prefix = "MONITOR_"; |
| 2135 |
} |
| 2136 |
|
| 2137 |
if (env_source) { |
| 2138 |
if (asprintf(our_env + n_env++, "%sSERVICE_RESULT=%s", monitor_prefix, service_result_to_string(env_source->result)) < 0) |
| 2139 |
return -ENOMEM; |
| 2140 |
|
| 2141 |
if (env_source->main_exec_status.pid > 0 && |
| 2142 |
dual_timestamp_is_set(&env_source->main_exec_status.exit_timestamp)) { |
| 2143 |
if (asprintf(our_env + n_env++, "%sEXIT_CODE=%s", monitor_prefix, sigchld_code_to_string(env_source->main_exec_status.code)) < 0) |
| 2144 |
return -ENOMEM; |
| 2145 |
|
| 2146 |
if (env_source->main_exec_status.code == CLD_EXITED) |
| 2147 |
r = asprintf(our_env + n_env++, "%sEXIT_STATUS=%i", monitor_prefix, env_source->main_exec_status.status); |
| 2148 |
else |
| 2149 |
r = asprintf(our_env + n_env++, "%sEXIT_STATUS=%s", monitor_prefix, signal_to_string(env_source->main_exec_status.status)); |
| 2150 |
if (r < 0) |
| 2151 |
return -ENOMEM; |
| 2152 |
} |
| 2153 |
|
| 2154 |
if (env_source != s) { |
| 2155 |
if (!sd_id128_is_null(UNIT(env_source)->invocation_id)) |
| 2156 |
if (asprintf(our_env + n_env++, "%sINVOCATION_ID=" SD_ID128_FORMAT_STR, |
| 2157 |
monitor_prefix, SD_ID128_FORMAT_VAL(UNIT(env_source)->invocation_id)) < 0) |
| 2158 |
return -ENOMEM; |
| 2159 |
|
| 2160 |
if (asprintf(our_env + n_env++, "%sUNIT=%s", monitor_prefix, UNIT(env_source)->id) < 0) |
| 2161 |
return -ENOMEM; |
| 2162 |
} |
| 2163 |
} |
| 2164 |
|
| 2165 |
if (UNIT(s)->debug_invocation) { |
| 2166 |
char *t = strdup("DEBUG_INVOCATION=1"); |
| 2167 |
if (!t) |
| 2168 |
return -ENOMEM; |
| 2169 |
our_env[n_env++] = t; |
| 2170 |
} |
| 2171 |
|
| 2172 |
if (UNIT(s)->activation_details) { |
| 2173 |
r = activation_details_append_env(UNIT(s)->activation_details, &our_env); |
| 2174 |
if (r < 0) |
| 2175 |
return r; |
| 2176 |
/* The number of env vars added here can vary, rather than keeping the allocation block in |
| 2177 |
* sync manually, these functions simply use the strv methods to append to it, so we need |
| 2178 |
* to update n_env when we are done in case of future usage. */ |
| 2179 |
n_env += r; |
| 2180 |
} |
| 2181 |
|
| 2182 |
r = unit_set_exec_params(UNIT(s), &exec_params); |
| 2183 |
if (r < 0) |
| 2184 |
return r; |
| 2185 |
|
| 2186 |
final_env = strv_env_merge(exec_params.environment, our_env); |
| 2187 |
if (!final_env) |
| 2188 |
return -ENOMEM; |
| 2189 |
|
| 2190 |
/* System D-Bus needs nss-systemd disabled, so that we don't deadlock */ |
| 2191 |
SET_FLAG(exec_params.flags, EXEC_NSS_DYNAMIC_BYPASS, |
| 2192 |
MANAGER_IS_SYSTEM(UNIT(s)->manager) && unit_has_name(UNIT(s), SPECIAL_DBUS_SERVICE)); |
| 2193 |
|
| 2194 |
strv_free_and_replace(exec_params.environment, final_env); |
| 2195 |
exec_params.watchdog_usec = service_get_watchdog_usec(s); |
| 2196 |
exec_params.selinux_context_net = s->socket_fd_selinux_context_net; |
| 2197 |
if (s->type == SERVICE_IDLE) |
| 2198 |
exec_params.idle_pipe = UNIT(s)->manager->idle_pipe; |
| 2199 |
exec_params.stdin_fd = s->stdin_fd; |
| 2200 |
exec_params.stdout_fd = s->stdout_fd; |
| 2201 |
exec_params.stderr_fd = s->stderr_fd; |
| 2202 |
|
| 2203 |
if (s->root_directory_fd >= 0) { |
| 2204 |
r = mount_fd_clone(s->root_directory_fd, /* recursive= */ true, &s->root_directory_fd); |
| 2205 |
if (r < 0) |
| 2206 |
return r; |
| 2207 |
|
| 2208 |
exec_params.root_directory_fd = r; |
| 2209 |
} |
| 2210 |
|
| 2211 |
r = exec_spawn(UNIT(s), |
| 2212 |
c, |
| 2213 |
&s->exec_context, |
| 2214 |
&exec_params, |
| 2215 |
s->exec_runtime, |
| 2216 |
&s->cgroup_context, |
| 2217 |
&pidref); |
| 2218 |
if (r < 0) |
| 2219 |
return r; |
| 2220 |
|
| 2221 |
s->exec_fd_event_source = TAKE_PTR(exec_fd_source); |
| 2222 |
s->exec_fd_hot = false; |
| 2223 |
|
| 2224 |
r = unit_watch_pidref(UNIT(s), &pidref, /* exclusive= */ true); |
| 2225 |
if (r < 0) |
| 2226 |
return r; |
| 2227 |
|
| 2228 |
*ret_pid = TAKE_PIDREF(pidref); |
| 2229 |
return 0; |
| 2230 |
} |
| 2231 |
|
| 2232 |
static int main_pid_good(Service *s) { |
| 2233 |
assert(s); |
| 2234 |
|
| 2235 |
/* Returns 0 if the pid is dead, > 0 if it is good, < 0 if we don't know */ |
| 2236 |
|
| 2237 |
/* If we know the pid file, then let's just check if it is still valid */ |
| 2238 |
if (s->main_pid_known) { |
| 2239 |
|
| 2240 |
/* If it's an alien child let's check if it is still alive ... */ |
| 2241 |
if (s->main_pid_alien && pidref_is_set(&s->main_pid)) |
| 2242 |
return pidref_is_alive(&s->main_pid); |
| 2243 |
|
| 2244 |
/* .. otherwise assume we'll get a SIGCHLD for it, which we really should wait for to collect |
| 2245 |
* exit status and code */ |
| 2246 |
return pidref_is_set(&s->main_pid); |
| 2247 |
} |
| 2248 |
|
| 2249 |
/* We don't know the pid */ |
| 2250 |
return -EAGAIN; |
| 2251 |
} |
| 2252 |
|
| 2253 |
static int control_pid_good(Service *s) { |
| 2254 |
assert(s); |
| 2255 |
|
| 2256 |
/* Returns 0 if the control PID is dead, > 0 if it is good. We never actually return < 0 here, but in order to |
| 2257 |
* make this function as similar as possible to main_pid_good() and cgroup_good(), we pretend that < 0 also |
| 2258 |
* means: we can't figure it out. */ |
| 2259 |
|
| 2260 |
return pidref_is_set(&s->control_pid); |
| 2261 |
} |
| 2262 |
|
| 2263 |
static int cgroup_good(Service *s) { |
| 2264 |
int r; |
| 2265 |
|
| 2266 |
assert(s); |
| 2267 |
|
| 2268 |
/* Returns 0 if the cgroup is empty or doesn't exist, > 0 if it is exists and is populated, < 0 if we can't |
| 2269 |
* figure it out */ |
| 2270 |
|
| 2271 |
if (!s->cgroup_runtime || !s->cgroup_runtime->cgroup_path) |
| 2272 |
return 0; |
| 2273 |
|
| 2274 |
r = cg_is_empty(s->cgroup_runtime->cgroup_path); |
| 2275 |
if (r < 0) |
| 2276 |
return r; |
| 2277 |
|
| 2278 |
return r == 0; |
| 2279 |
} |
| 2280 |
|
| 2281 |
static bool service_shall_restart(Service *s, const char **reason) { |
| 2282 |
assert(s); |
| 2283 |
assert(reason); |
| 2284 |
|
| 2285 |
/* Don't restart after manual stops */ |
| 2286 |
if (s->forbid_restart) { |
| 2287 |
*reason = "manual stop"; |
| 2288 |
return false; |
| 2289 |
} |
| 2290 |
|
| 2291 |
/* Never restart if this is configured as special exception */ |
| 2292 |
if (exit_status_set_test(&s->restart_prevent_status, s->main_exec_status.code, s->main_exec_status.status)) { |
| 2293 |
*reason = "prevented by exit status"; |
| 2294 |
return false; |
| 2295 |
} |
| 2296 |
|
| 2297 |
/* Restart if the exit code/status are configured as restart triggers */ |
| 2298 |
if (exit_status_set_test(&s->restart_force_status, s->main_exec_status.code, s->main_exec_status.status)) { |
| 2299 |
/* Don't allow Type=oneshot services to restart on success. Note that Restart=always/on-success |
| 2300 |
* is already rejected in service_verify. */ |
| 2301 |
if (s->type == SERVICE_ONESHOT && s->result == SERVICE_SUCCESS) { |
| 2302 |
*reason = "service type and exit status"; |
| 2303 |
return false; |
| 2304 |
} |
| 2305 |
|
| 2306 |
*reason = "forced by exit status"; |
| 2307 |
return true; |
| 2308 |
} |
| 2309 |
|
| 2310 |
*reason = "restart setting"; |
| 2311 |
switch (s->restart) { |
| 2312 |
|
| 2313 |
case SERVICE_RESTART_NO: |
| 2314 |
return false; |
| 2315 |
|
| 2316 |
case SERVICE_RESTART_ALWAYS: |
| 2317 |
return s->result != SERVICE_SKIP_CONDITION; |
| 2318 |
|
| 2319 |
case SERVICE_RESTART_ON_SUCCESS: |
| 2320 |
return s->result == SERVICE_SUCCESS; |
| 2321 |
|
| 2322 |
case SERVICE_RESTART_ON_FAILURE: |
| 2323 |
return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_SKIP_CONDITION); |
| 2324 |
|
| 2325 |
case SERVICE_RESTART_ON_ABNORMAL: |
| 2326 |
return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE, SERVICE_SKIP_CONDITION); |
| 2327 |
|
| 2328 |
case SERVICE_RESTART_ON_WATCHDOG: |
| 2329 |
return s->result == SERVICE_FAILURE_WATCHDOG; |
| 2330 |
|
| 2331 |
case SERVICE_RESTART_ON_ABORT: |
| 2332 |
return IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP); |
| 2333 |
|
| 2334 |
default: |
| 2335 |
assert_not_reached(); |
| 2336 |
} |
| 2337 |
} |
| 2338 |
|
| 2339 |
static bool service_will_restart(Unit *u) { |
| 2340 |
Service *s = SERVICE(u); |
| 2341 |
|
| 2342 |
assert(s); |
| 2343 |
|
| 2344 |
if (IN_SET(s->state, SERVICE_DEAD_BEFORE_AUTO_RESTART, SERVICE_FAILED_BEFORE_AUTO_RESTART, SERVICE_AUTO_RESTART, SERVICE_AUTO_RESTART_QUEUED)) |
| 2345 |
return true; |
| 2346 |
|
| 2347 |
return unit_will_restart_default(u); |
| 2348 |
} |
| 2349 |
|
| 2350 |
static ServiceState service_determine_dead_state(Service *s) { |
| 2351 |
assert(s); |
| 2352 |
|
| 2353 |
return SERVICE_FD_STORE_POPULATED(s) && IN_SET(s->fd_store_preserve_mode, EXEC_PRESERVE_YES, EXEC_PRESERVE_ON_SUCCESS) ? SERVICE_DEAD_RESOURCES_PINNED : SERVICE_DEAD; |
| 2354 |
} |
| 2355 |
|
| 2356 |
static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) { |
| 2357 |
ServiceState end_state, restart_state; |
| 2358 |
int r; |
| 2359 |
|
| 2360 |
assert(s); |
| 2361 |
|
| 2362 |
/* If there's a stop job queued before we enter the DEAD state, we shouldn't act on Restart=, in order to not |
| 2363 |
* undo what has already been enqueued. */ |
| 2364 |
if (unit_stop_pending(UNIT(s))) |
| 2365 |
allow_restart = false; |
| 2366 |
|
| 2367 |
if (s->result == SERVICE_SUCCESS || f == SERVICE_FAILURE_START_LIMIT_HIT) |
| 2368 |
s->result = f; |
| 2369 |
|
| 2370 |
if (s->result == SERVICE_SUCCESS) { |
| 2371 |
unit_log_success(UNIT(s)); |
| 2372 |
end_state = service_determine_dead_state(s); |
| 2373 |
restart_state = SERVICE_DEAD_BEFORE_AUTO_RESTART; |
| 2374 |
} else if (s->result == SERVICE_SKIP_CONDITION) { |
| 2375 |
unit_log_skip(UNIT(s), service_result_to_string(s->result)); |
| 2376 |
end_state = service_determine_dead_state(s); |
| 2377 |
restart_state = _SERVICE_STATE_INVALID; /* Never restart if skipped due to condition failure */ |
| 2378 |
} else { |
| 2379 |
unit_log_failure(UNIT(s), service_result_to_string(s->result)); |
| 2380 |
end_state = SERVICE_FAILED; |
| 2381 |
restart_state = SERVICE_FAILED_BEFORE_AUTO_RESTART; |
| 2382 |
} |
| 2383 |
unit_warn_leftover_processes(UNIT(s), /* start= */ false); |
| 2384 |
|
| 2385 |
if (!allow_restart) |
| 2386 |
log_unit_debug(UNIT(s), "Service restart not allowed."); |
| 2387 |
else { |
| 2388 |
const char *reason; |
| 2389 |
|
| 2390 |
allow_restart = service_shall_restart(s, &reason); |
| 2391 |
log_unit_debug(UNIT(s), "Service will %srestart (%s)", |
| 2392 |
allow_restart ? "" : "not ", |
| 2393 |
reason); |
| 2394 |
} |
| 2395 |
|
| 2396 |
if (allow_restart) { |
| 2397 |
usec_t restart_usec_next; |
| 2398 |
|
| 2399 |
assert(restart_state >= 0 && restart_state < _SERVICE_STATE_MAX); |
| 2400 |
|
| 2401 |
/* We make two state changes here: one that maps to the high-level UNIT_INACTIVE/UNIT_FAILED |
| 2402 |
* state (i.e. a state indicating deactivation), and then one that maps to the |
| 2403 |
* high-level UNIT_STARTING state (i.e. a state indicating activation). We do this so that |
| 2404 |
* external software can watch the state changes and see all service failures, even if they |
| 2405 |
* are only transitionary and followed by an automatic restart. We have fine-grained |
| 2406 |
* low-level states for this though so that software can distinguish the permanent UNIT_INACTIVE |
| 2407 |
* state from this transitionary UNIT_INACTIVE state by looking at the low-level states. */ |
| 2408 |
if (s->restart_mode != SERVICE_RESTART_MODE_DIRECT) |
| 2409 |
service_set_state(s, restart_state); |
| 2410 |
|
| 2411 |
restart_usec_next = service_restart_usec_next(s); |
| 2412 |
|
| 2413 |
r = service_arm_timer(s, /* relative= */ true, restart_usec_next); |
| 2414 |
if (r < 0) { |
| 2415 |
log_unit_warning_errno(UNIT(s), r, "Failed to install restart timer: %m"); |
| 2416 |
return service_enter_dead(s, SERVICE_FAILURE_RESOURCES, /* allow_restart= */ false); |
| 2417 |
} |
| 2418 |
|
| 2419 |
/* If the relevant option is set, and the unit doesn't already have logging level set to |
| 2420 |
* debug, enable it now. Make sure to overwrite the state in /run/systemd/units/ too, to |
| 2421 |
* ensure journald doesn't prune the messages. The previous state is saved and restored |
| 2422 |
* once the auto-restart flow ends. */ |
| 2423 |
if (s->restart_mode == SERVICE_RESTART_MODE_DEBUG) { |
| 2424 |
r = unit_set_debug_invocation(UNIT(s), true); |
| 2425 |
if (r < 0) |
| 2426 |
log_unit_warning_errno(UNIT(s), r, "Failed to enable debug invocation, ignoring: %m"); |
| 2427 |
if (r > 0) |
| 2428 |
log_unit_notice(UNIT(s), "Service dead, subsequent restarts will be executed with debug level logging."); |
| 2429 |
} |
| 2430 |
|
| 2431 |
log_unit_debug(UNIT(s), "Next restart interval calculated as: %s", FORMAT_TIMESPAN(restart_usec_next, 0)); |
| 2432 |
|
| 2433 |
service_set_state(s, SERVICE_AUTO_RESTART); |
| 2434 |
} else { |
| 2435 |
/* If we shan't restart, the restart counter would be flushed out. But rather than doing that |
| 2436 |
* immediately here, this is delegated to service_start(), i.e. next start, so that the user |
| 2437 |
* can still introspect the counter. */ |
| 2438 |
service_set_state(s, end_state); |
| 2439 |
|
| 2440 |
(void) unit_set_debug_invocation(UNIT(s), false); |
| 2441 |
} |
| 2442 |
|
| 2443 |
/* The next restart might not be a manual stop, hence reset the flag indicating manual stops */ |
| 2444 |
s->forbid_restart = false; |
| 2445 |
|
| 2446 |
/* Reset notify states */ |
| 2447 |
s->notify_access_override = _NOTIFY_ACCESS_INVALID; |
| 2448 |
s->notify_state = _NOTIFY_STATE_INVALID; |
| 2449 |
|
| 2450 |
/* We want fresh tmpdirs and ephemeral snapshots in case the service is started again immediately. */ |
| 2451 |
s->exec_runtime = exec_runtime_destroy(s->exec_runtime); |
| 2452 |
|
| 2453 |
/* Also, remove the runtime directory */ |
| 2454 |
unit_destroy_runtime_data(UNIT(s), &s->exec_context, /* destroy_runtime_dir= */ true); |
| 2455 |
|
| 2456 |
/* Also get rid of the fd store, if that's configured. */ |
| 2457 |
if (s->fd_store_preserve_mode == EXEC_PRESERVE_NO || |
| 2458 |
(s->fd_store_preserve_mode == EXEC_PRESERVE_ON_SUCCESS && s->state == SERVICE_FAILED)) |
| 2459 |
service_release_fd_store(s); |
| 2460 |
|
| 2461 |
/* Get rid of the IPC bits of the user */ |
| 2462 |
unit_unref_uid_gid(UNIT(s), true); |
| 2463 |
|
| 2464 |
/* Try to delete the pid file. At this point it will be |
| 2465 |
* out-of-date, and some software might be confused by it, so |
| 2466 |
* let's remove it. */ |
| 2467 |
if (s->pid_file) |
| 2468 |
(void) unlink(s->pid_file); |
| 2469 |
|
| 2470 |
/* Reset TTY ownership if necessary */ |
| 2471 |
exec_context_revert_tty(&s->exec_context, UNIT(s)->invocation_id); |
| 2472 |
} |
| 2473 |
|
| 2474 |
static void service_enter_stop_post(Service *s, ServiceResult f) { |
| 2475 |
int r; |
| 2476 |
assert(s); |
| 2477 |
|
| 2478 |
if (s->result == SERVICE_SUCCESS) |
| 2479 |
s->result = f; |
| 2480 |
|
| 2481 |
service_unwatch_control_pid(s); |
| 2482 |
|
| 2483 |
s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]; |
| 2484 |
if (s->control_command) { |
| 2485 |
s->control_command_id = SERVICE_EXEC_STOP_POST; |
| 2486 |
|
| 2487 |
r = service_spawn(s, |
| 2488 |
s->control_command, |
| 2489 |
service_exec_flags(s, s->control_command_id, /* cred_flag = */ 0), |
| 2490 |
s->timeout_stop_usec, |
| 2491 |
&s->control_pid); |
| 2492 |
if (r < 0) { |
| 2493 |
log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'stop-post' task: %m"); |
| 2494 |
service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
| 2495 |
return; |
| 2496 |
} |
| 2497 |
|
| 2498 |
service_set_state(s, SERVICE_STOP_POST); |
| 2499 |
} else |
| 2500 |
service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS); |
| 2501 |
} |
| 2502 |
|
| 2503 |
static int state_to_kill_operation(Service *s, ServiceState state) { |
| 2504 |
switch (state) { |
| 2505 |
|
| 2506 |
case SERVICE_STOP_WATCHDOG: |
| 2507 |
case SERVICE_FINAL_WATCHDOG: |
| 2508 |
return KILL_WATCHDOG; |
| 2509 |
|
| 2510 |
case SERVICE_STOP_SIGTERM: |
| 2511 |
if (unit_has_job_type(UNIT(s), JOB_RESTART)) |
| 2512 |
return KILL_RESTART; |
| 2513 |
_fallthrough_; |
| 2514 |
|
| 2515 |
case SERVICE_FINAL_SIGTERM: |
| 2516 |
return KILL_TERMINATE; |
| 2517 |
|
| 2518 |
case SERVICE_STOP_SIGKILL: |
| 2519 |
case SERVICE_FINAL_SIGKILL: |
| 2520 |
return KILL_KILL; |
| 2521 |
|
| 2522 |
default: |
| 2523 |
return _KILL_OPERATION_INVALID; |
| 2524 |
} |
| 2525 |
} |
| 2526 |
|
| 2527 |
static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) { |
| 2528 |
int kill_operation, r; |
| 2529 |
|
| 2530 |
assert(s); |
| 2531 |
|
| 2532 |
if (s->result == SERVICE_SUCCESS) |
| 2533 |
s->result = f; |
| 2534 |
|
| 2535 |
kill_operation = state_to_kill_operation(s, state); |
| 2536 |
r = unit_kill_context(UNIT(s), kill_operation); |
| 2537 |
if (r < 0) { |
| 2538 |
log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m"); |
| 2539 |
goto fail; |
| 2540 |
} |
| 2541 |
|
| 2542 |
if (r > 0) { |
| 2543 |
r = service_arm_timer(s, /* relative= */ true, |
| 2544 |
kill_operation == KILL_WATCHDOG ? service_timeout_abort_usec(s) : s->timeout_stop_usec); |
| 2545 |
if (r < 0) { |
| 2546 |
log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m"); |
| 2547 |
goto fail; |
| 2548 |
} |
| 2549 |
|
| 2550 |
service_set_state(s, state); |
| 2551 |
} else if (IN_SET(state, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM) && s->kill_context.send_sigkill) |
| 2552 |
service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS); |
| 2553 |
else if (IN_SET(state, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL)) |
| 2554 |
service_enter_stop_post(s, SERVICE_SUCCESS); |
| 2555 |
else if (IN_SET(state, SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM) && s->kill_context.send_sigkill) |
| 2556 |
service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS); |
| 2557 |
else |
| 2558 |
service_enter_dead(s, SERVICE_SUCCESS, /* allow_restart= */ true); |
| 2559 |
|
| 2560 |
return; |
| 2561 |
|
| 2562 |
fail: |
| 2563 |
if (IN_SET(state, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL)) |
| 2564 |
service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES); |
| 2565 |
else |
| 2566 |
service_enter_dead(s, SERVICE_FAILURE_RESOURCES, /* allow_restart= */ true); |
| 2567 |
} |
| 2568 |
|
| 2569 |
static void service_enter_stop_by_notify(Service *s) { |
| 2570 |
int r; |
| 2571 |
|
| 2572 |
assert(s); |
| 2573 |
|
| 2574 |
r = service_arm_timer(s, /* relative= */ true, s->timeout_stop_usec); |
| 2575 |
if (r < 0) { |
| 2576 |
log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m"); |
| 2577 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
| 2578 |
return; |
| 2579 |
} |
| 2580 |
|
| 2581 |
/* The service told us it's stopping, so it's as if we SIGTERM'd it. */ |
| 2582 |
service_set_state(s, SERVICE_STOP_SIGTERM); |
| 2583 |
} |
| 2584 |
|
| 2585 |
static void service_enter_stop(Service *s, ServiceResult f) { |
| 2586 |
int r; |
| 2587 |
|
| 2588 |
assert(s); |
| 2589 |
|
| 2590 |
if (s->result == SERVICE_SUCCESS) |
| 2591 |
s->result = f; |
| 2592 |
|
| 2593 |
service_unwatch_control_pid(s); |
| 2594 |
|
| 2595 |
s->control_command = s->exec_command[SERVICE_EXEC_STOP]; |
| 2596 |
if (s->control_command) { |
| 2597 |
s->control_command_id = SERVICE_EXEC_STOP; |
| 2598 |
|
| 2599 |
r = service_spawn(s, |
| 2600 |
s->control_command, |
| 2601 |
service_exec_flags(s, s->control_command_id, /* cred_flag = */ 0), |
| 2602 |
s->timeout_stop_usec, |
| 2603 |
&s->control_pid); |
| 2604 |
if (r < 0) { |
| 2605 |
log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'stop' task: %m"); |
| 2606 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
| 2607 |
return; |
| 2608 |
} |
| 2609 |
|
| 2610 |
service_set_state(s, SERVICE_STOP); |
| 2611 |
} else |
| 2612 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); |
| 2613 |
} |
| 2614 |
|
| 2615 |
static bool service_good(Service *s) { |
| 2616 |
int main_pid_ok; |
| 2617 |
|
| 2618 |
assert(s); |
| 2619 |
|
| 2620 |
if (s->type == SERVICE_DBUS && !s->bus_name_good) |
| 2621 |
return false; |
| 2622 |
|
| 2623 |
main_pid_ok = main_pid_good(s); |
| 2624 |
if (main_pid_ok > 0) /* It's alive */ |
| 2625 |
return true; |
| 2626 |
if (main_pid_ok == 0 && s->exit_type == SERVICE_EXIT_MAIN) /* It's dead */ |
| 2627 |
return false; |
| 2628 |
|
| 2629 |
/* OK, we don't know anything about the main PID, maybe |
| 2630 |
* because there is none. Let's check the control group |
| 2631 |
* instead. */ |
| 2632 |
|
| 2633 |
return cgroup_good(s) != 0; |
| 2634 |
} |
| 2635 |
|
| 2636 |
static void service_enter_running(Service *s, ServiceResult f) { |
| 2637 |
int r; |
| 2638 |
|
| 2639 |
assert(s); |
| 2640 |
|
| 2641 |
if (s->result == SERVICE_SUCCESS) |
| 2642 |
s->result = f; |
| 2643 |
|
| 2644 |
service_unwatch_control_pid(s); |
| 2645 |
|
| 2646 |
if (s->result != SERVICE_SUCCESS) |
| 2647 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
| 2648 |
else if (service_good(s)) { |
| 2649 |
|
| 2650 |
/* If there are any queued up sd_notify() notifications, process them now */ |
| 2651 |
if (s->notify_state == NOTIFY_RELOADING) |
| 2652 |
service_enter_reload_by_notify(s); |
| 2653 |
else if (s->notify_state == NOTIFY_STOPPING) |
| 2654 |
service_enter_stop_by_notify(s); |
| 2655 |
else { |
| 2656 |
service_set_state(s, SERVICE_RUNNING); |
| 2657 |
|
| 2658 |
r = service_arm_timer(s, /* relative= */ false, service_running_timeout(s)); |
| 2659 |
if (r < 0) { |
| 2660 |
log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m"); |
| 2661 |
service_enter_running(s, SERVICE_FAILURE_RESOURCES); |
| 2662 |
return; |
| 2663 |
} |
| 2664 |
} |
| 2665 |
|
| 2666 |
} else if (s->remain_after_exit) |
| 2667 |
service_set_state(s, SERVICE_EXITED); |
| 2668 |
else |
| 2669 |
service_enter_stop(s, SERVICE_SUCCESS); |
| 2670 |
} |
| 2671 |
|
| 2672 |
static void service_enter_start_post(Service *s) { |
| 2673 |
int r; |
| 2674 |
|
| 2675 |
assert(s); |
| 2676 |
|
| 2677 |
service_unwatch_control_pid(s); |
| 2678 |
service_reset_watchdog(s); |
| 2679 |
|
| 2680 |
s->control_command = s->exec_command[SERVICE_EXEC_START_POST]; |
| 2681 |
if (s->control_command) { |
| 2682 |
s->control_command_id = SERVICE_EXEC_START_POST; |
| 2683 |
|
| 2684 |
r = service_spawn(s, |
| 2685 |
s->control_command, |
| 2686 |
service_exec_flags(s, s->control_command_id, /* cred_flag = */ 0), |
| 2687 |
s->timeout_start_usec, |
| 2688 |
&s->control_pid); |
| 2689 |
if (r < 0) { |
| 2690 |
log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'start-post' task: %m"); |
| 2691 |
service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
| 2692 |
return; |
| 2693 |
} |
| 2694 |
|
| 2695 |
service_set_state(s, SERVICE_START_POST); |
| 2696 |
} else |
| 2697 |
service_enter_running(s, SERVICE_SUCCESS); |
| 2698 |
} |
| 2699 |
|
| 2700 |
static void service_kill_control_process(Service *s) { |
| 2701 |
int r; |
| 2702 |
|
| 2703 |
assert(s); |
| 2704 |
|
| 2705 |
if (!pidref_is_set(&s->control_pid)) |
| 2706 |
return; |
| 2707 |
|
| 2708 |
r = pidref_kill_and_sigcont(&s->control_pid, SIGKILL); |
| 2709 |
if (r < 0) { |
| 2710 |
_cleanup_free_ char *comm = NULL; |
| 2711 |
|
| 2712 |
(void) pidref_get_comm(&s->control_pid, &comm); |
| 2713 |
|
| 2714 |
log_unit_debug_errno(UNIT(s), r, "Failed to kill control process " PID_FMT " (%s), ignoring: %m", |
| 2715 |
s->control_pid.pid, strna(comm)); |
| 2716 |
} |
| 2717 |
} |
| 2718 |
|
| 2719 |
static int service_adverse_to_leftover_processes(Service *s) { |
| 2720 |
assert(s); |
| 2721 |
|
| 2722 |
/* KillMode=mixed and control group are used to indicate that all process should be killed off. |
| 2723 |
* SendSIGKILL= is used for services that require a clean shutdown. These are typically database |
| 2724 |
* service where a SigKilled process would result in a lengthy recovery and who's shutdown or startup |
| 2725 |
* time is quite variable (so Timeout settings aren't of use). |
| 2726 |
* |
| 2727 |
* Here we take these two factors and refuse to start a service if there are existing processes |
| 2728 |
* within a control group. Databases, while generally having some protection against multiple |
| 2729 |
* instances running, lets not stress the rigor of these. Also ExecStartPre= parts of the service |
| 2730 |
* aren't as rigoriously written to protect against multiple use. */ |
| 2731 |
|
| 2732 |
if (unit_warn_leftover_processes(UNIT(s), /* start= */ true) > 0 && |
| 2733 |
IN_SET(s->kill_context.kill_mode, KILL_MIXED, KILL_CONTROL_GROUP) && |
| 2734 |
!s->kill_context.send_sigkill) |
| 2735 |
return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(EBUSY), |
| 2736 |
"Will not start SendSIGKILL=no service of type KillMode=control-group or mixed while processes exist"); |
| 2737 |
|
| 2738 |
return 0; |
| 2739 |
} |
| 2740 |
|
| 2741 |
static void service_enter_start(Service *s) { |
| 2742 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 2743 |
ExecCommand *c; |
| 2744 |
usec_t timeout; |
| 2745 |
int r; |
| 2746 |
|
| 2747 |
assert(s); |
| 2748 |
|
| 2749 |
service_unwatch_control_pid(s); |
| 2750 |
service_unwatch_main_pid(s); |
| 2751 |
|
| 2752 |
r = service_adverse_to_leftover_processes(s); |
| 2753 |
if (r < 0) |
| 2754 |
goto fail; |
| 2755 |
|
| 2756 |
if (s->type == SERVICE_FORKING) { |
| 2757 |
s->control_command_id = SERVICE_EXEC_START; |
| 2758 |
c = s->control_command = s->exec_command[SERVICE_EXEC_START]; |
| 2759 |
|
| 2760 |
s->main_command = NULL; |
| 2761 |
} else { |
| 2762 |
s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
| 2763 |
s->control_command = NULL; |
| 2764 |
|
| 2765 |
c = s->main_command = s->exec_command[SERVICE_EXEC_START]; |
| 2766 |
} |
| 2767 |
|
| 2768 |
if (!c) { |
| 2769 |
if (s->type != SERVICE_ONESHOT) { |
| 2770 |
/* There's no command line configured for the main command? Hmm, that is strange. |
| 2771 |
* This can only happen if the configuration changes at runtime. In this case, |
| 2772 |
* let's enter a failure state. */ |
| 2773 |
r = log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENXIO), "There's no 'start' task anymore we could start."); |
| 2774 |
goto fail; |
| 2775 |
} |
| 2776 |
|
| 2777 |
/* We force a fake state transition here. Otherwise, the unit would go directly from |
| 2778 |
* SERVICE_DEAD to SERVICE_DEAD without SERVICE_ACTIVATING or SERVICE_ACTIVE |
| 2779 |
* in between. This way we can later trigger actions that depend on the state |
| 2780 |
* transition, including SuccessAction=. */ |
| 2781 |
service_set_state(s, SERVICE_START); |
| 2782 |
|
| 2783 |
service_enter_start_post(s); |
| 2784 |
return; |
| 2785 |
} |
| 2786 |
|
| 2787 |
if (IN_SET(s->type, SERVICE_SIMPLE, SERVICE_IDLE)) |
| 2788 |
/* For simple + idle this is the main process. We don't apply any timeout here, but |
| 2789 |
* service_enter_running() will later apply the .runtime_max_usec timeout. */ |
| 2790 |
timeout = USEC_INFINITY; |
| 2791 |
else |
| 2792 |
timeout = s->timeout_start_usec; |
| 2793 |
|
| 2794 |
r = service_spawn(s, |
| 2795 |
c, |
| 2796 |
service_exec_flags(s, SERVICE_EXEC_START, EXEC_SETUP_CREDENTIALS_FRESH), |
| 2797 |
timeout, |
| 2798 |
&pidref); |
| 2799 |
if (r < 0) { |
| 2800 |
log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'start' task: %m"); |
| 2801 |
goto fail; |
| 2802 |
} |
| 2803 |
|
| 2804 |
assert(pidref.pid == c->exec_status.pid); |
| 2805 |
|
| 2806 |
switch (s->type) { |
| 2807 |
|
| 2808 |
case SERVICE_SIMPLE: |
| 2809 |
case SERVICE_IDLE: |
| 2810 |
/* For simple services we immediately start the START_POST binaries. */ |
| 2811 |
(void) service_set_main_pidref(s, TAKE_PIDREF(pidref), &c->exec_status.start_timestamp); |
| 2812 |
return service_enter_start_post(s); |
| 2813 |
|
| 2814 |
case SERVICE_FORKING: |
| 2815 |
/* For forking services we wait until the start process exited. */ |
| 2816 |
s->control_pid = TAKE_PIDREF(pidref); |
| 2817 |
return service_set_state(s, SERVICE_START); |
| 2818 |
|
| 2819 |
case SERVICE_ONESHOT: /* For oneshot services we wait until the start process exited, too, but it is our main process. */ |
| 2820 |
case SERVICE_EXEC: |
| 2821 |
case SERVICE_DBUS: |
| 2822 |
case SERVICE_NOTIFY: |
| 2823 |
case SERVICE_NOTIFY_RELOAD: |
| 2824 |
/* For D-Bus services we know the main pid right away, but wait for the bus name to appear |
| 2825 |
* on the bus. 'notify' and 'exec' services wait for readiness notification and EOF |
| 2826 |
* on exec_fd, respectively. */ |
| 2827 |
(void) service_set_main_pidref(s, TAKE_PIDREF(pidref), &c->exec_status.start_timestamp); |
| 2828 |
return service_set_state(s, SERVICE_START); |
| 2829 |
|
| 2830 |
default: |
| 2831 |
assert_not_reached(); |
| 2832 |
} |
| 2833 |
|
| 2834 |
fail: |
| 2835 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
| 2836 |
} |
| 2837 |
|
| 2838 |
static void service_enter_start_pre(Service *s) { |
| 2839 |
int r; |
| 2840 |
|
| 2841 |
assert(s); |
| 2842 |
|
| 2843 |
service_unwatch_control_pid(s); |
| 2844 |
|
| 2845 |
s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]; |
| 2846 |
if (s->control_command) { |
| 2847 |
|
| 2848 |
r = service_adverse_to_leftover_processes(s); |
| 2849 |
if (r < 0) |
| 2850 |
goto fail; |
| 2851 |
|
| 2852 |
s->control_command_id = SERVICE_EXEC_START_PRE; |
| 2853 |
|
| 2854 |
r = service_spawn(s, |
| 2855 |
s->control_command, |
| 2856 |
service_exec_flags(s, s->control_command_id, /* cred_flag = */ 0), |
| 2857 |
s->timeout_start_usec, |
| 2858 |
&s->control_pid); |
| 2859 |
if (r < 0) { |
| 2860 |
log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'start-pre' task: %m"); |
| 2861 |
goto fail; |
| 2862 |
} |
| 2863 |
|
| 2864 |
service_set_state(s, SERVICE_START_PRE); |
| 2865 |
} else |
| 2866 |
service_enter_start(s); |
| 2867 |
|
| 2868 |
return; |
| 2869 |
|
| 2870 |
fail: |
| 2871 |
service_enter_dead(s, SERVICE_FAILURE_RESOURCES, /* allow_restart= */ true); |
| 2872 |
} |
| 2873 |
|
| 2874 |
static void service_enter_condition(Service *s) { |
| 2875 |
int r; |
| 2876 |
|
| 2877 |
assert(s); |
| 2878 |
|
| 2879 |
service_unwatch_control_pid(s); |
| 2880 |
|
| 2881 |
s->control_command = s->exec_command[SERVICE_EXEC_CONDITION]; |
| 2882 |
if (s->control_command) { |
| 2883 |
|
| 2884 |
r = service_adverse_to_leftover_processes(s); |
| 2885 |
if (r < 0) |
| 2886 |
goto fail; |
| 2887 |
|
| 2888 |
s->control_command_id = SERVICE_EXEC_CONDITION; |
| 2889 |
|
| 2890 |
r = service_spawn(s, |
| 2891 |
s->control_command, |
| 2892 |
service_exec_flags(s, s->control_command_id, /* cred_flag = */ 0), |
| 2893 |
s->timeout_start_usec, |
| 2894 |
&s->control_pid); |
| 2895 |
if (r < 0) { |
| 2896 |
log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'exec-condition' task: %m"); |
| 2897 |
goto fail; |
| 2898 |
} |
| 2899 |
|
| 2900 |
service_set_state(s, SERVICE_CONDITION); |
| 2901 |
} else |
| 2902 |
service_enter_start_pre(s); |
| 2903 |
|
| 2904 |
return; |
| 2905 |
|
| 2906 |
fail: |
| 2907 |
service_enter_dead(s, SERVICE_FAILURE_RESOURCES, /* allow_restart= */ true); |
| 2908 |
} |
| 2909 |
|
| 2910 |
static void service_enter_restart(Service *s, bool shortcut) { |
| 2911 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 2912 |
int r; |
| 2913 |
|
| 2914 |
/* shortcut: a manual start request is received, restart immediately */ |
| 2915 |
|
| 2916 |
assert(s); |
| 2917 |
assert(s->state == SERVICE_AUTO_RESTART); |
| 2918 |
|
| 2919 |
if (!shortcut && unit_has_job_type(UNIT(s), JOB_STOP)) { |
| 2920 |
/* Don't restart things if we are going down anyway */ |
| 2921 |
log_unit_info(UNIT(s), "Stop job pending for unit, skipping automatic restart."); |
| 2922 |
return; |
| 2923 |
} |
| 2924 |
|
| 2925 |
/* Any units that are bound to this service must also be restarted, unless RestartMode=direct. |
| 2926 |
* We use JOB_START for ourselves but then set JOB_RESTART_DEPENDENCIES which will enqueue JOB_RESTART |
| 2927 |
* for those dependency jobs in the former case, plain JOB_REPLACE when RestartMode=direct. |
| 2928 |
* |
| 2929 |
* Also, when RestartMode=direct is used, the service being restarted don't enter the inactive/failed state, |
| 2930 |
* i.e. unit_process_job -> job_finish_and_invalidate is never called, and the previous job might still |
| 2931 |
* be running (especially for Type=oneshot services). |
| 2932 |
* We need to refuse late merge and re-enqueue the anchor job. */ |
| 2933 |
r = manager_add_job_full(UNIT(s)->manager, |
| 2934 |
JOB_START, UNIT(s), |
| 2935 |
s->restart_mode == SERVICE_RESTART_MODE_DIRECT ? JOB_REPLACE : JOB_RESTART_DEPENDENCIES, |
| 2936 |
TRANSACTION_REENQUEUE_ANCHOR, |
| 2937 |
/* affected_jobs= */ NULL, |
| 2938 |
&error, /* ret= */ NULL); |
| 2939 |
if (r < 0) { |
| 2940 |
log_unit_warning(UNIT(s), "Failed to schedule restart job: %s", bus_error_message(&error, r)); |
| 2941 |
return service_enter_dead(s, SERVICE_FAILURE_RESOURCES, /* allow_restart= */ false); |
| 2942 |
} |
| 2943 |
|
| 2944 |
/* Count the jobs we enqueue for restarting. This counter is maintained as long as the unit isn't |
| 2945 |
* fully stopped, i.e. as long as it remains up or remains in auto-start states. The user can reset |
| 2946 |
* the counter explicitly however via the usual "systemctl reset-failure" logic. */ |
| 2947 |
s->n_restarts++; |
| 2948 |
|
| 2949 |
log_unit_struct(UNIT(s), LOG_INFO, |
| 2950 |
LOG_MESSAGE_ID(SD_MESSAGE_UNIT_RESTART_SCHEDULED_STR), |
| 2951 |
LOG_UNIT_INVOCATION_ID(UNIT(s)), |
| 2952 |
LOG_UNIT_MESSAGE(UNIT(s), |
| 2953 |
"Scheduled restart job%s, restart counter is at %u.", |
| 2954 |
shortcut ? " immediately on client request" : "", s->n_restarts), |
| 2955 |
LOG_ITEM("N_RESTARTS=%u", s->n_restarts)); |
| 2956 |
|
| 2957 |
service_set_state(s, SERVICE_AUTO_RESTART_QUEUED); |
| 2958 |
|
| 2959 |
/* Notify clients about changed restart counter */ |
| 2960 |
unit_add_to_dbus_queue(UNIT(s)); |
| 2961 |
} |
| 2962 |
|
| 2963 |
static void service_enter_reload_by_notify(Service *s) { |
| 2964 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 2965 |
int r; |
| 2966 |
|
| 2967 |
assert(s); |
| 2968 |
|
| 2969 |
r = service_arm_timer(s, /* relative= */ true, s->timeout_start_usec); |
| 2970 |
if (r < 0) { |
| 2971 |
log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m"); |
| 2972 |
return service_reload_finish(s, SERVICE_FAILURE_RESOURCES); |
| 2973 |
} |
| 2974 |
|
| 2975 |
service_set_state(s, SERVICE_RELOAD_NOTIFY); |
| 2976 |
|
| 2977 |
/* service_enter_reload_by_notify is never called during a reload, thus no loops are possible. */ |
| 2978 |
r = manager_propagate_reload(UNIT(s)->manager, UNIT(s), JOB_FAIL, &error); |
| 2979 |
if (r < 0) |
| 2980 |
log_unit_warning(UNIT(s), "Failed to schedule propagation of reload, ignoring: %s", bus_error_message(&error, r)); |
| 2981 |
} |
| 2982 |
|
| 2983 |
static void service_enter_reload_post(Service *s) { |
| 2984 |
int r; |
| 2985 |
|
| 2986 |
assert(s); |
| 2987 |
|
| 2988 |
service_unwatch_control_pid(s); |
| 2989 |
|
| 2990 |
s->control_command = s->exec_command[SERVICE_EXEC_RELOAD_POST]; |
| 2991 |
if (s->control_command) { |
| 2992 |
s->control_command_id = SERVICE_EXEC_RELOAD_POST; |
| 2993 |
|
| 2994 |
r = service_spawn(s, |
| 2995 |
s->control_command, |
| 2996 |
service_exec_flags(s, s->control_command_id, /* cred_flag = */ 0), |
| 2997 |
s->timeout_start_usec, |
| 2998 |
&s->control_pid); |
| 2999 |
if (r < 0) { |
| 3000 |
log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'reload-post' task: %m"); |
| 3001 |
return service_reload_finish(s, SERVICE_FAILURE_RESOURCES); |
| 3002 |
} |
| 3003 |
|
| 3004 |
service_set_state(s, SERVICE_RELOAD_POST); |
| 3005 |
} else |
| 3006 |
service_reload_finish(s, SERVICE_SUCCESS); |
| 3007 |
} |
| 3008 |
|
| 3009 |
static void service_enter_reload_signal(Service *s) { |
| 3010 |
int r; |
| 3011 |
|
| 3012 |
assert(s); |
| 3013 |
|
| 3014 |
if (s->type != SERVICE_NOTIFY_RELOAD) |
| 3015 |
return service_enter_reload_post(s); |
| 3016 |
|
| 3017 |
if (s->state == SERVICE_RELOAD) { |
| 3018 |
/* We executed ExecReload=, and the service has already notified us the result? |
| 3019 |
* Directly transition to next state. */ |
| 3020 |
if (s->notify_state == NOTIFY_RELOADING) |
| 3021 |
return service_set_state(s, SERVICE_RELOAD_NOTIFY); |
| 3022 |
if (s->notify_state == NOTIFY_RELOAD_READY) |
| 3023 |
return service_enter_reload_post(s); |
| 3024 |
} |
| 3025 |
|
| 3026 |
if (pidref_is_set(&s->main_pid)) { |
| 3027 |
r = service_arm_timer(s, /* relative= */ true, s->timeout_start_usec); |
| 3028 |
if (r < 0) { |
| 3029 |
log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m"); |
| 3030 |
goto fail; |
| 3031 |
} |
| 3032 |
|
| 3033 |
r = pidref_kill_and_sigcont(&s->main_pid, s->reload_signal); |
| 3034 |
if (r < 0) { |
| 3035 |
log_unit_warning_errno(UNIT(s), r, "Failed to send reload signal: %m"); |
| 3036 |
goto fail; |
| 3037 |
} |
| 3038 |
|
| 3039 |
service_set_state(s, SERVICE_RELOAD_SIGNAL); |
| 3040 |
} else |
| 3041 |
service_enter_reload_post(s); |
| 3042 |
|
| 3043 |
return; |
| 3044 |
|
| 3045 |
fail: |
| 3046 |
service_reload_finish(s, SERVICE_FAILURE_RESOURCES); |
| 3047 |
} |
| 3048 |
|
| 3049 |
static void service_enter_reload(Service *s) { |
| 3050 |
int r; |
| 3051 |
|
| 3052 |
assert(s); |
| 3053 |
|
| 3054 |
service_unwatch_control_pid(s); |
| 3055 |
|
| 3056 |
if (IN_SET(s->notify_state, NOTIFY_RELOADING, NOTIFY_RELOAD_READY)) |
| 3057 |
s->notify_state = _NOTIFY_STATE_INVALID; |
| 3058 |
|
| 3059 |
/* Store the timestamp when we started reloading: when reloading via SIGHUP we won't leave the reload |
| 3060 |
* state until we received both RELOADING=1 and READY=1 with MONOTONIC_USEC= set to a value above |
| 3061 |
* this. Thus we know for sure the reload cycle was executed *after* we requested it, and is not one |
| 3062 |
* that was already in progress before. */ |
| 3063 |
s->reload_begin_usec = now(CLOCK_MONOTONIC); |
| 3064 |
|
| 3065 |
s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]; |
| 3066 |
if (s->control_command) { |
| 3067 |
s->control_command_id = SERVICE_EXEC_RELOAD; |
| 3068 |
|
| 3069 |
r = service_spawn(s, |
| 3070 |
s->control_command, |
| 3071 |
service_exec_flags(s, s->control_command_id, /* cred_flag = */ 0), |
| 3072 |
s->timeout_start_usec, |
| 3073 |
&s->control_pid); |
| 3074 |
if (r < 0) { |
| 3075 |
log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'reload' task: %m"); |
| 3076 |
return service_reload_finish(s, SERVICE_FAILURE_RESOURCES); |
| 3077 |
} |
| 3078 |
|
| 3079 |
service_set_state(s, SERVICE_RELOAD); |
| 3080 |
} else |
| 3081 |
service_enter_reload_signal(s); |
| 3082 |
} |
| 3083 |
|
| 3084 |
static bool service_get_effective_reload_credentials(Service *s) { |
| 3085 |
assert(s); |
| 3086 |
|
| 3087 |
return FLAGS_SET(s->refresh_on_reload_flags, SERVICE_RELOAD_CREDENTIALS) && |
| 3088 |
exec_context_has_credentials(&s->exec_context); |
| 3089 |
} |
| 3090 |
|
| 3091 |
static void service_enter_refresh_credentials(Service *s) { |
| 3092 |
_cleanup_(pidref_done) PidRef worker = PIDREF_NULL; |
| 3093 |
int r; |
| 3094 |
|
| 3095 |
assert(s); |
| 3096 |
|
| 3097 |
if (!service_get_effective_reload_credentials(s)) |
| 3098 |
return service_enter_reload(s); |
| 3099 |
|
| 3100 |
service_unwatch_control_pid(s); |
| 3101 |
s->control_command = NULL; |
| 3102 |
s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
| 3103 |
|
| 3104 |
r = service_arm_timer(s, /* relative = */ true, s->timeout_start_usec); |
| 3105 |
if (r < 0) { |
| 3106 |
log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m"); |
| 3107 |
goto fail; |
| 3108 |
} |
| 3109 |
|
| 3110 |
r = unit_fork_helper_process_full(UNIT(s), "(sd-refresh-creds)", /* into_cgroup = */ false, |
| 3111 |
FORK_ALLOW_DLOPEN, /* allow loading libacl to avoid doing so in pid1 */ |
| 3112 |
&worker); |
| 3113 |
if (r < 0) { |
| 3114 |
log_unit_error_errno(UNIT(s), r, "Failed to fork process to refresh credentials: %m"); |
| 3115 |
goto fail; |
| 3116 |
} |
| 3117 |
if (r == 0) { |
| 3118 |
LOG_CONTEXT_PUSH_UNIT(UNIT(s)); |
| 3119 |
|
| 3120 |
r = unit_refresh_credentials(UNIT(s)); |
| 3121 |
if (ERRNO_IS_NEG_PRIVILEGE(r)) |
| 3122 |
_exit(EXIT_NOPERMISSION); |
| 3123 |
if (r < 0) |
| 3124 |
_exit(EXIT_FAILURE); |
| 3125 |
if (r == 0) |
| 3126 |
_exit(EXIT_NOTINSTALLED); |
| 3127 |
|
| 3128 |
_exit(EXIT_SUCCESS); |
| 3129 |
} |
| 3130 |
|
| 3131 |
r = unit_watch_pidref(UNIT(s), &worker, /* exclusive = */ true); |
| 3132 |
if (r < 0) { |
| 3133 |
log_unit_warning_errno(UNIT(s), r, "Failed to watch credentials refresh helper process: %m"); |
| 3134 |
goto fail; |
| 3135 |
} |
| 3136 |
|
| 3137 |
s->control_pid = TAKE_PIDREF(worker); |
| 3138 |
service_set_state(s, SERVICE_REFRESH_CREDENTIALS); |
| 3139 |
return; |
| 3140 |
|
| 3141 |
fail: |
| 3142 |
service_reload_finish(s, SERVICE_FAILURE_RESOURCES); |
| 3143 |
} |
| 3144 |
|
| 3145 |
static bool service_can_reload_extensions(Service *s, bool warn) { |
| 3146 |
assert(s); |
| 3147 |
|
| 3148 |
// TODO: Add support for user services, which can use ExtensionDirectories= + notify-reload. |
| 3149 |
// For now, skip for user services. |
| 3150 |
|
| 3151 |
if (exec_context_has_vpicked_extensions(&s->exec_context) <= 0) { |
| 3152 |
if (warn) |
| 3153 |
log_unit_warning(UNIT(s), "Service uses RefreshOnReload=extensions, but has no extensions using vpick. Ignoring."); |
| 3154 |
return false; |
| 3155 |
} |
| 3156 |
|
| 3157 |
if (!s->exec_command[SERVICE_EXEC_START]) { |
| 3158 |
if (warn) |
| 3159 |
log_unit_warning(UNIT(s), "Service uses RefreshOnReload=extensions, but has no main process (ExecStart=). Ignoring."); |
| 3160 |
return false; |
| 3161 |
} |
| 3162 |
|
| 3163 |
if (!MANAGER_IS_SYSTEM(UNIT(s)->manager)) { |
| 3164 |
if (warn) |
| 3165 |
log_unit_warning(UNIT(s), "Service uses RefreshOnReload=extensions, which is not supported in user mode. Ignoring."); |
| 3166 |
return false; |
| 3167 |
} |
| 3168 |
|
| 3169 |
return true; |
| 3170 |
} |
| 3171 |
|
| 3172 |
static bool service_get_effective_reload_extensions(Service *s) { |
| 3173 |
assert(s); |
| 3174 |
|
| 3175 |
if (!FLAGS_SET(s->refresh_on_reload_flags, SERVICE_RELOAD_EXTENSIONS)) |
| 3176 |
return false; |
| 3177 |
|
| 3178 |
if (!service_can_reload_extensions(s, /* warn = */ false)) |
| 3179 |
return false; |
| 3180 |
|
| 3181 |
return true; |
| 3182 |
} |
| 3183 |
|
| 3184 |
static bool service_should_reload_extensions(Service *s) { |
| 3185 |
assert(s); |
| 3186 |
|
| 3187 |
if (!service_get_effective_reload_extensions(s)) |
| 3188 |
return false; |
| 3189 |
|
| 3190 |
if (!pidref_is_set(&s->main_pid)) { |
| 3191 |
log_unit_debug(UNIT(s), "Not reloading extensions for service without main PID."); |
| 3192 |
return false; |
| 3193 |
} |
| 3194 |
|
| 3195 |
return true; |
| 3196 |
} |
| 3197 |
|
| 3198 |
static void service_enter_refresh_extensions(Service *s) { |
| 3199 |
_cleanup_(pidref_done) PidRef worker = PIDREF_NULL; |
| 3200 |
int r; |
| 3201 |
|
| 3202 |
assert(s); |
| 3203 |
|
| 3204 |
/* If we don't have extensions to refresh, immediately transition to next state */ |
| 3205 |
if (!service_should_reload_extensions(s)) |
| 3206 |
return service_enter_refresh_credentials(s); |
| 3207 |
|
| 3208 |
service_unwatch_control_pid(s); |
| 3209 |
s->control_command = NULL; |
| 3210 |
s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
| 3211 |
|
| 3212 |
r = service_arm_timer(s, /* relative= */ true, s->timeout_start_usec); |
| 3213 |
if (r < 0) { |
| 3214 |
log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m"); |
| 3215 |
goto fail; |
| 3216 |
} |
| 3217 |
|
| 3218 |
/* Given we are running from PID1, avoid doing potentially heavy I/O operations like opening images |
| 3219 |
* directly, and instead fork a worker process. */ |
| 3220 |
r = unit_fork_helper_process_full(UNIT(s), "(sd-refresh-extensions)", /* into_cgroup= */ false, |
| 3221 |
FORK_ALLOW_DLOPEN, /* permit dlopen() to avoid load of libcryptsetup in pid1 */ |
| 3222 |
&worker); |
| 3223 |
if (r < 0) { |
| 3224 |
log_unit_error_errno(UNIT(s), r, "Failed to fork process to refresh extensions in unit's namespace: %m"); |
| 3225 |
goto fail; |
| 3226 |
} |
| 3227 |
if (r == 0) { |
| 3228 |
LOG_CONTEXT_PUSH_UNIT(UNIT(s)); |
| 3229 |
|
| 3230 |
assert(pidref_is_set(&s->main_pid)); |
| 3231 |
|
| 3232 |
_cleanup_free_ char *propagate_dir = path_join("/run/systemd/propagate/", UNIT(s)->id); |
| 3233 |
if (!propagate_dir) { |
| 3234 |
log_unit_error_errno(UNIT(s), -ENOMEM, "Failed to allocate memory for propagate directory: %m"); |
| 3235 |
_exit(EXIT_FAILURE); |
| 3236 |
} |
| 3237 |
|
| 3238 |
NamespaceParameters p = { |
| 3239 |
.private_namespace_dir = "/run/systemd", |
| 3240 |
.incoming_dir = "/run/systemd/incoming", |
| 3241 |
.propagate_dir = propagate_dir, |
| 3242 |
.runtime_scope = UNIT(s)->manager->runtime_scope, |
| 3243 |
.extension_images = s->exec_context.extension_images, |
| 3244 |
.n_extension_images = s->exec_context.n_extension_images, |
| 3245 |
.extension_directories = s->exec_context.extension_directories, |
| 3246 |
.extension_image_policy = s->exec_context.extension_image_policy, |
| 3247 |
}; |
| 3248 |
|
| 3249 |
/* Only reload confext, and not sysext as they also typically contain the executable(s) used |
| 3250 |
* by the service and a simply reload cannot meaningfully handle that. */ |
| 3251 |
r = refresh_extensions_in_namespace( |
| 3252 |
&s->main_pid, |
| 3253 |
"SYSTEMD_CONFEXT_HIERARCHIES", |
| 3254 |
&p); |
| 3255 |
if (r < 0) |
| 3256 |
log_unit_error_errno(UNIT(s), r, "Failed to refresh extensions in unit's namespace: %m"); |
| 3257 |
else |
| 3258 |
log_unit_debug(UNIT(s), "Refreshed extensions in unit's namespace"); |
| 3259 |
|
| 3260 |
_exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
| 3261 |
} |
| 3262 |
|
| 3263 |
r = unit_watch_pidref(UNIT(s), &worker, /* exclusive= */ true); |
| 3264 |
if (r < 0) { |
| 3265 |
log_unit_warning_errno(UNIT(s), r, "Failed to watch extensions refresh helper process: %m"); |
| 3266 |
goto fail; |
| 3267 |
} |
| 3268 |
|
| 3269 |
s->control_pid = TAKE_PIDREF(worker); |
| 3270 |
service_set_state(s, SERVICE_REFRESH_EXTENSIONS); |
| 3271 |
return; |
| 3272 |
|
| 3273 |
fail: |
| 3274 |
service_reload_finish(s, SERVICE_FAILURE_RESOURCES); |
| 3275 |
} |
| 3276 |
|
| 3277 |
static void service_run_next_control(Service *s) { |
| 3278 |
usec_t timeout; |
| 3279 |
int r; |
| 3280 |
|
| 3281 |
assert(s); |
| 3282 |
assert(s->control_command); |
| 3283 |
assert(s->control_command->command_next); |
| 3284 |
|
| 3285 |
assert(s->control_command_id != SERVICE_EXEC_START); |
| 3286 |
|
| 3287 |
s->control_command = s->control_command->command_next; |
| 3288 |
service_unwatch_control_pid(s); |
| 3289 |
|
| 3290 |
if (IN_SET(s->state, |
| 3291 |
SERVICE_CONDITION, |
| 3292 |
SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, |
| 3293 |
SERVICE_RUNNING, |
| 3294 |
SERVICE_RELOAD, SERVICE_RELOAD_POST)) |
| 3295 |
timeout = s->timeout_start_usec; |
| 3296 |
else |
| 3297 |
timeout = s->timeout_stop_usec; |
| 3298 |
|
| 3299 |
r = service_spawn(s, |
| 3300 |
s->control_command, |
| 3301 |
service_exec_flags(s, s->control_command_id, /* cred_flag = */ 0), |
| 3302 |
timeout, |
| 3303 |
&s->control_pid); |
| 3304 |
if (r < 0) { |
| 3305 |
log_unit_warning_errno(UNIT(s), r, "Failed to spawn next control task: %m"); |
| 3306 |
|
| 3307 |
if (IN_SET(s->state, SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START_POST, SERVICE_STOP)) |
| 3308 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
| 3309 |
else if (s->state == SERVICE_STOP_POST) |
| 3310 |
service_enter_dead(s, SERVICE_FAILURE_RESOURCES, /* allow_restart= */ true); |
| 3311 |
else if (IN_SET(s->state, SERVICE_RELOAD, SERVICE_RELOAD_POST)) |
| 3312 |
service_reload_finish(s, SERVICE_FAILURE_RESOURCES); |
| 3313 |
else |
| 3314 |
service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
| 3315 |
} |
| 3316 |
} |
| 3317 |
|
| 3318 |
static void service_run_next_main(Service *s) { |
| 3319 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 3320 |
int r; |
| 3321 |
|
| 3322 |
assert(s); |
| 3323 |
assert(s->main_command); |
| 3324 |
assert(s->main_command->command_next); |
| 3325 |
assert(s->type == SERVICE_ONESHOT); |
| 3326 |
|
| 3327 |
s->main_command = s->main_command->command_next; |
| 3328 |
service_unwatch_main_pid(s); |
| 3329 |
|
| 3330 |
r = service_spawn(s, |
| 3331 |
s->main_command, |
| 3332 |
service_exec_flags(s, SERVICE_EXEC_START, EXEC_SETUP_CREDENTIALS), |
| 3333 |
s->timeout_start_usec, |
| 3334 |
&pidref); |
| 3335 |
if (r < 0) { |
| 3336 |
log_unit_warning_errno(UNIT(s), r, "Failed to spawn next main task: %m"); |
| 3337 |
service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
| 3338 |
return; |
| 3339 |
} |
| 3340 |
|
| 3341 |
(void) service_set_main_pidref(s, TAKE_PIDREF(pidref), &s->main_command->exec_status.start_timestamp); |
| 3342 |
} |
| 3343 |
|
| 3344 |
static int service_start(Unit *u) { |
| 3345 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 3346 |
int r; |
| 3347 |
|
| 3348 |
if (s->state == SERVICE_AUTO_RESTART) { |
| 3349 |
/* As mentioned in unit_start(), we allow manual starts to act as "hurry up" signals |
| 3350 |
* for auto restart. We need to re-enqueue the job though, as the job type has changed |
| 3351 |
* (JOB_RESTART_DEPENDENCIES). */ |
| 3352 |
|
| 3353 |
service_enter_restart(s, /* shortcut= */ true); |
| 3354 |
return -EAGAIN; |
| 3355 |
} |
| 3356 |
|
| 3357 |
/* SERVICE_*_BEFORE_AUTO_RESTART are not to be expected here, as those are intermediate states |
| 3358 |
* that should never be seen outside of service_enter_dead(). */ |
| 3359 |
assert(IN_SET(s->state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_DEAD_RESOURCES_PINNED, SERVICE_AUTO_RESTART_QUEUED)); |
| 3360 |
|
| 3361 |
r = unit_acquire_invocation_id(u); |
| 3362 |
if (r < 0) |
| 3363 |
return r; |
| 3364 |
|
| 3365 |
s->main_pid_known = false; |
| 3366 |
s->main_pid_alien = false; |
| 3367 |
s->forbid_restart = false; |
| 3368 |
|
| 3369 |
/* This is not an automatic restart? Flush the restart counter then. */ |
| 3370 |
if (s->state != SERVICE_AUTO_RESTART_QUEUED) |
| 3371 |
s->n_restarts = 0; |
| 3372 |
|
| 3373 |
s->result = SERVICE_SUCCESS; |
| 3374 |
s->reload_result = SERVICE_SUCCESS; |
| 3375 |
s->reload_begin_usec = USEC_INFINITY; |
| 3376 |
|
| 3377 |
s->status_text = mfree(s->status_text); |
| 3378 |
s->status_errno = 0; |
| 3379 |
s->status_bus_error = mfree(s->status_bus_error); |
| 3380 |
s->status_varlink_error = mfree(s->status_varlink_error); |
| 3381 |
|
| 3382 |
s->notify_access_override = _NOTIFY_ACCESS_INVALID; |
| 3383 |
s->notify_state = _NOTIFY_STATE_INVALID; |
| 3384 |
|
| 3385 |
s->watchdog_original_usec = s->watchdog_usec; |
| 3386 |
s->watchdog_override_enable = false; |
| 3387 |
s->watchdog_override_usec = USEC_INFINITY; |
| 3388 |
|
| 3389 |
exec_command_reset_status_list_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX); |
| 3390 |
exec_status_reset(&s->main_exec_status); |
| 3391 |
|
| 3392 |
CGroupRuntime *crt = unit_get_cgroup_runtime(u); |
| 3393 |
if (crt) { |
| 3394 |
unit_cgroup_disable_all_controllers(u); |
| 3395 |
crt->reset_accounting = true; |
| 3396 |
} |
| 3397 |
|
| 3398 |
service_enter_condition(s); |
| 3399 |
return 1; |
| 3400 |
} |
| 3401 |
|
| 3402 |
static void service_live_mount_finish(Service *s, ServiceResult f, const char *error) { |
| 3403 |
assert(s); |
| 3404 |
assert(error); |
| 3405 |
|
| 3406 |
s->live_mount_result = f; |
| 3407 |
|
| 3408 |
if (!s->mount_request) |
| 3409 |
return; |
| 3410 |
|
| 3411 |
if (f == SERVICE_SUCCESS) { |
| 3412 |
(void) sd_bus_reply_method_return(s->mount_request, NULL); |
| 3413 |
log_unit_debug(UNIT(s), |
| 3414 |
"'%s' method succeeded", |
| 3415 |
strna(sd_bus_message_get_member(s->mount_request))); |
| 3416 |
} else { |
| 3417 |
(void) sd_bus_reply_method_errorf(s->mount_request, error, |
| 3418 |
"method '%s' for unit '%s' failed", |
| 3419 |
strna(sd_bus_message_get_member(s->mount_request)), |
| 3420 |
UNIT(s)->id); |
| 3421 |
log_unit_debug(UNIT(s), |
| 3422 |
"'%s' method failed: %s", |
| 3423 |
strna(sd_bus_message_get_member(s->mount_request)), |
| 3424 |
error); |
| 3425 |
} |
| 3426 |
|
| 3427 |
s->mount_request = sd_bus_message_unref(s->mount_request); |
| 3428 |
} |
| 3429 |
|
| 3430 |
static void service_reload_finish(Service *s, ServiceResult f) { |
| 3431 |
assert(s); |
| 3432 |
|
| 3433 |
s->reload_result = f; |
| 3434 |
s->reload_begin_usec = USEC_INFINITY; |
| 3435 |
|
| 3436 |
/* If notify state is still in dangling NOTIFY_RELOADING, reset it so service_enter_running() |
| 3437 |
* won't get confused (see #37515) */ |
| 3438 |
if (s->notify_state == NOTIFY_RELOADING) |
| 3439 |
s->notify_state = _NOTIFY_STATE_INVALID; |
| 3440 |
|
| 3441 |
service_enter_running(s, SERVICE_SUCCESS); |
| 3442 |
} |
| 3443 |
|
| 3444 |
static int service_stop(Unit *u) { |
| 3445 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 3446 |
|
| 3447 |
/* Don't create restart jobs from manual stops. */ |
| 3448 |
s->forbid_restart = true; |
| 3449 |
|
| 3450 |
switch (s->state) { |
| 3451 |
|
| 3452 |
case SERVICE_STOP: |
| 3453 |
case SERVICE_STOP_SIGTERM: |
| 3454 |
case SERVICE_STOP_SIGKILL: |
| 3455 |
case SERVICE_STOP_POST: |
| 3456 |
case SERVICE_FINAL_WATCHDOG: |
| 3457 |
case SERVICE_FINAL_SIGTERM: |
| 3458 |
case SERVICE_FINAL_SIGKILL: |
| 3459 |
/* Already on it */ |
| 3460 |
return 0; |
| 3461 |
|
| 3462 |
case SERVICE_AUTO_RESTART: |
| 3463 |
case SERVICE_AUTO_RESTART_QUEUED: |
| 3464 |
/* Give up on the auto restart */ |
| 3465 |
service_set_state(s, service_determine_dead_state(s)); |
| 3466 |
return 0; |
| 3467 |
|
| 3468 |
case SERVICE_MOUNTING: |
| 3469 |
service_live_mount_finish(s, SERVICE_FAILURE_PROTOCOL, BUS_ERROR_UNIT_INACTIVE); |
| 3470 |
_fallthrough_; |
| 3471 |
case SERVICE_REFRESH_EXTENSIONS: |
| 3472 |
case SERVICE_REFRESH_CREDENTIALS: |
| 3473 |
service_kill_control_process(s); |
| 3474 |
_fallthrough_; |
| 3475 |
case SERVICE_CONDITION: |
| 3476 |
case SERVICE_START_PRE: |
| 3477 |
case SERVICE_START: |
| 3478 |
case SERVICE_START_POST: |
| 3479 |
case SERVICE_RELOAD: |
| 3480 |
case SERVICE_RELOAD_SIGNAL: |
| 3481 |
case SERVICE_RELOAD_NOTIFY: |
| 3482 |
case SERVICE_RELOAD_POST: |
| 3483 |
case SERVICE_STOP_WATCHDOG: |
| 3484 |
/* If there's already something running we go directly into kill mode. */ |
| 3485 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); |
| 3486 |
return 0; |
| 3487 |
|
| 3488 |
case SERVICE_CLEANING: |
| 3489 |
/* If we are currently cleaning, then abort it, brutally. */ |
| 3490 |
service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS); |
| 3491 |
return 0; |
| 3492 |
|
| 3493 |
case SERVICE_RUNNING: |
| 3494 |
case SERVICE_EXITED: |
| 3495 |
service_enter_stop(s, SERVICE_SUCCESS); |
| 3496 |
return 1; |
| 3497 |
|
| 3498 |
case SERVICE_DEAD_BEFORE_AUTO_RESTART: |
| 3499 |
case SERVICE_FAILED_BEFORE_AUTO_RESTART: |
| 3500 |
case SERVICE_DEAD: |
| 3501 |
case SERVICE_FAILED: |
| 3502 |
case SERVICE_DEAD_RESOURCES_PINNED: |
| 3503 |
default: |
| 3504 |
/* Unknown state, or unit_stop() should already have handled these */ |
| 3505 |
assert_not_reached(); |
| 3506 |
} |
| 3507 |
} |
| 3508 |
|
| 3509 |
static int service_reload(Unit *u) { |
| 3510 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 3511 |
|
| 3512 |
assert(IN_SET(s->state, SERVICE_RUNNING, SERVICE_EXITED)); |
| 3513 |
|
| 3514 |
s->reload_result = SERVICE_SUCCESS; |
| 3515 |
s->refreshed_mask = 0; |
| 3516 |
|
| 3517 |
service_enter_refresh_extensions(s); |
| 3518 |
|
| 3519 |
return 1; |
| 3520 |
} |
| 3521 |
|
| 3522 |
static bool service_can_reload(Unit *u) { |
| 3523 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 3524 |
|
| 3525 |
return s->exec_command[SERVICE_EXEC_RELOAD] || |
| 3526 |
s->type == SERVICE_NOTIFY_RELOAD || |
| 3527 |
(s->refresh_on_reload_set && |
| 3528 |
(service_get_effective_reload_extensions(s) || |
| 3529 |
service_get_effective_reload_credentials(s))); |
| 3530 |
} |
| 3531 |
|
| 3532 |
static unsigned service_exec_command_index(Unit *u, ServiceExecCommand id, const ExecCommand *current) { |
| 3533 |
Service *s = SERVICE(u); |
| 3534 |
unsigned idx = 0; |
| 3535 |
|
| 3536 |
assert(s); |
| 3537 |
assert(id >= 0); |
| 3538 |
assert(id < _SERVICE_EXEC_COMMAND_MAX); |
| 3539 |
|
| 3540 |
const ExecCommand *first = s->exec_command[id]; |
| 3541 |
|
| 3542 |
/* Figure out where we are in the list by walking back to the beginning */ |
| 3543 |
for (const ExecCommand *c = current; c != first; c = c->command_prev) |
| 3544 |
idx++; |
| 3545 |
|
| 3546 |
return idx; |
| 3547 |
} |
| 3548 |
|
| 3549 |
static int service_serialize_exec_command(Unit *u, FILE *f, const ExecCommand *command) { |
| 3550 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 3551 |
_cleanup_free_ char *args = NULL, *p = NULL; |
| 3552 |
const char *type, *key; |
| 3553 |
ServiceExecCommand id; |
| 3554 |
size_t length = 0; |
| 3555 |
unsigned idx; |
| 3556 |
|
| 3557 |
assert(f); |
| 3558 |
|
| 3559 |
if (!command) |
| 3560 |
return 0; |
| 3561 |
|
| 3562 |
if (command == s->control_command) { |
| 3563 |
type = "control"; |
| 3564 |
id = s->control_command_id; |
| 3565 |
} else { |
| 3566 |
type = "main"; |
| 3567 |
id = SERVICE_EXEC_START; |
| 3568 |
} |
| 3569 |
|
| 3570 |
idx = service_exec_command_index(u, id, command); |
| 3571 |
|
| 3572 |
STRV_FOREACH(arg, command->argv) { |
| 3573 |
_cleanup_free_ char *e = NULL; |
| 3574 |
size_t n; |
| 3575 |
|
| 3576 |
e = cescape(*arg); |
| 3577 |
if (!e) |
| 3578 |
return log_oom(); |
| 3579 |
|
| 3580 |
n = strlen(e); |
| 3581 |
if (!GREEDY_REALLOC(args, length + 2 + n + 2)) |
| 3582 |
return log_oom(); |
| 3583 |
|
| 3584 |
if (length > 0) |
| 3585 |
args[length++] = ' '; |
| 3586 |
|
| 3587 |
args[length++] = '"'; |
| 3588 |
memcpy(args + length, e, n); |
| 3589 |
length += n; |
| 3590 |
args[length++] = '"'; |
| 3591 |
} |
| 3592 |
|
| 3593 |
if (!GREEDY_REALLOC(args, length + 1)) |
| 3594 |
return log_oom(); |
| 3595 |
|
| 3596 |
args[length++] = 0; |
| 3597 |
|
| 3598 |
p = cescape(command->path); |
| 3599 |
if (!p) |
| 3600 |
return log_oom(); |
| 3601 |
|
| 3602 |
key = strjoina(type, "-command"); |
| 3603 |
|
| 3604 |
/* We use '+1234' instead of '1234' to mark the last command in a sequence. |
| 3605 |
* This is used in service_deserialize_exec_command(). */ |
| 3606 |
(void) serialize_item_format( |
| 3607 |
f, key, |
| 3608 |
"%s %s%u %s %s", |
| 3609 |
service_exec_command_to_string(id), |
| 3610 |
command->command_next ? "" : "+", |
| 3611 |
idx, |
| 3612 |
p, args); |
| 3613 |
|
| 3614 |
return 0; |
| 3615 |
} |
| 3616 |
|
| 3617 |
static int service_serialize(Unit *u, FILE *f, FDSet *fds) { |
| 3618 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 3619 |
int r; |
| 3620 |
|
| 3621 |
assert(f); |
| 3622 |
assert(fds); |
| 3623 |
|
| 3624 |
(void) serialize_item(f, "state", service_state_to_string(s->state)); |
| 3625 |
(void) serialize_item(f, "result", service_result_to_string(s->result)); |
| 3626 |
(void) serialize_item(f, "reload-result", service_result_to_string(s->reload_result)); |
| 3627 |
(void) serialize_item(f, "live-mount-result", service_result_to_string(s->live_mount_result)); |
| 3628 |
|
| 3629 |
(void) serialize_pidref(f, fds, "control-pid", &s->control_pid); |
| 3630 |
if (s->main_pid_known) |
| 3631 |
(void) serialize_pidref(f, fds, "main-pid", &s->main_pid); |
| 3632 |
|
| 3633 |
(void) serialize_bool(f, "main-pid-known", s->main_pid_known); |
| 3634 |
(void) serialize_bool(f, "bus-name-good", s->bus_name_good); |
| 3635 |
|
| 3636 |
(void) serialize_item_format(f, "n-restarts", "%u", s->n_restarts); |
| 3637 |
(void) serialize_bool(f, "forbid-restart", s->forbid_restart); |
| 3638 |
|
| 3639 |
service_serialize_exec_command(u, f, s->control_command); |
| 3640 |
service_serialize_exec_command(u, f, s->main_command); |
| 3641 |
|
| 3642 |
r = serialize_fd(f, fds, "stdin-fd", s->stdin_fd); |
| 3643 |
if (r < 0) |
| 3644 |
return r; |
| 3645 |
|
| 3646 |
r = serialize_fd(f, fds, "stdout-fd", s->stdout_fd); |
| 3647 |
if (r < 0) |
| 3648 |
return r; |
| 3649 |
|
| 3650 |
r = serialize_fd(f, fds, "stderr-fd", s->stderr_fd); |
| 3651 |
if (r < 0) |
| 3652 |
return r; |
| 3653 |
|
| 3654 |
r = serialize_fd(f, fds, "root-directory-fd", s->root_directory_fd); |
| 3655 |
if (r < 0) |
| 3656 |
return r; |
| 3657 |
|
| 3658 |
if (s->exec_fd_event_source) { |
| 3659 |
r = serialize_fd(f, fds, "exec-fd", sd_event_source_get_io_fd(s->exec_fd_event_source)); |
| 3660 |
if (r < 0) |
| 3661 |
return r; |
| 3662 |
|
| 3663 |
(void) serialize_bool(f, "exec-fd-hot", s->exec_fd_hot); |
| 3664 |
} |
| 3665 |
|
| 3666 |
if (UNIT_ISSET(s->accept_socket)) { |
| 3667 |
r = serialize_item(f, "accept-socket", UNIT_DEREF(s->accept_socket)->id); |
| 3668 |
if (r < 0) |
| 3669 |
return r; |
| 3670 |
} |
| 3671 |
|
| 3672 |
r = serialize_fd(f, fds, "socket-fd", s->socket_fd); |
| 3673 |
if (r < 0) |
| 3674 |
return r; |
| 3675 |
|
| 3676 |
LIST_FOREACH(fd_store, fs, s->fd_store) { |
| 3677 |
_cleanup_free_ char *c = NULL; |
| 3678 |
int copy; |
| 3679 |
|
| 3680 |
copy = fdset_put_dup(fds, fs->fd); |
| 3681 |
if (copy < 0) |
| 3682 |
return log_error_errno(copy, "Failed to copy file descriptor for serialization: %m"); |
| 3683 |
|
| 3684 |
c = cescape(fs->fdname); |
| 3685 |
if (!c) |
| 3686 |
return log_oom(); |
| 3687 |
|
| 3688 |
(void) serialize_item_format(f, "fd-store-fd", "%i \"%s\" %s %" PRIu64, |
| 3689 |
copy, c, one_zero(fs->do_poll), fs->index); |
| 3690 |
} |
| 3691 |
|
| 3692 |
FOREACH_ARRAY(i, s->extra_fds, s->n_extra_fds) { |
| 3693 |
_cleanup_free_ char *c = NULL; |
| 3694 |
int copy; |
| 3695 |
|
| 3696 |
copy = fdset_put_dup(fds, i->fd); |
| 3697 |
if (copy < 0) |
| 3698 |
return log_error_errno(copy, "Failed to copy file descriptor for serialization: %m"); |
| 3699 |
|
| 3700 |
c = cescape(i->fdname); |
| 3701 |
if (!c) |
| 3702 |
return log_oom(); |
| 3703 |
|
| 3704 |
(void) serialize_item_format(f, "extra-fd", "%i \"%s\"", copy, c); |
| 3705 |
} |
| 3706 |
|
| 3707 |
if (s->main_exec_status.pid > 0) { |
| 3708 |
(void) serialize_item_format(f, "main-exec-status-pid", PID_FMT, s->main_exec_status.pid); |
| 3709 |
(void) serialize_dual_timestamp(f, "main-exec-status-start", &s->main_exec_status.start_timestamp); |
| 3710 |
(void) serialize_dual_timestamp(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp); |
| 3711 |
(void) serialize_dual_timestamp(f, "main-exec-status-handoff", &s->main_exec_status.handoff_timestamp); |
| 3712 |
|
| 3713 |
if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) { |
| 3714 |
(void) serialize_item_format(f, "main-exec-status-code", "%i", s->main_exec_status.code); |
| 3715 |
(void) serialize_item_format(f, "main-exec-status-status", "%i", s->main_exec_status.status); |
| 3716 |
} |
| 3717 |
} |
| 3718 |
|
| 3719 |
if (s->notify_access_override >= 0) |
| 3720 |
(void) serialize_item(f, "notify-access-override", notify_access_to_string(s->notify_access_override)); |
| 3721 |
if (s->notify_state >= 0) |
| 3722 |
(void) serialize_item(f, "notify-state", notify_state_to_string(s->notify_state)); |
| 3723 |
|
| 3724 |
r = serialize_item_escaped(f, "status-text", s->status_text); |
| 3725 |
if (r < 0) |
| 3726 |
return r; |
| 3727 |
|
| 3728 |
(void) serialize_item_format(f, "status-errno", "%d", s->status_errno); |
| 3729 |
(void) serialize_item(f, "status-bus-error", s->status_bus_error); |
| 3730 |
(void) serialize_item(f, "status-varlink-error", s->status_varlink_error); |
| 3731 |
|
| 3732 |
(void) serialize_dual_timestamp(f, "watchdog-timestamp", &s->watchdog_timestamp); |
| 3733 |
|
| 3734 |
(void) serialize_usec(f, "watchdog-original-usec", s->watchdog_original_usec); |
| 3735 |
if (s->watchdog_override_enable) |
| 3736 |
(void) serialize_usec(f, "watchdog-override-usec", s->watchdog_override_usec); |
| 3737 |
|
| 3738 |
(void) serialize_usec(f, "reload-begin-usec", s->reload_begin_usec); |
| 3739 |
|
| 3740 |
if (s->refreshed_mask > 0) { |
| 3741 |
_cleanup_strv_free_ char **l = NULL; |
| 3742 |
_cleanup_free_ char *t = NULL; |
| 3743 |
|
| 3744 |
r = service_refresh_on_reload_to_strv(s->refreshed_mask, &l); |
| 3745 |
if (r < 0) |
| 3746 |
return log_oom(); |
| 3747 |
|
| 3748 |
t = strv_join(l, " "); |
| 3749 |
if (!t) |
| 3750 |
return log_oom(); |
| 3751 |
|
| 3752 |
(void) serialize_item(f, "refreshed-mask", t); |
| 3753 |
} |
| 3754 |
|
| 3755 |
return 0; |
| 3756 |
} |
| 3757 |
|
| 3758 |
int service_deserialize_exec_command( |
| 3759 |
Unit *u, |
| 3760 |
const char *key, |
| 3761 |
const char *value) { |
| 3762 |
|
| 3763 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 3764 |
ExecCommand *command = NULL; |
| 3765 |
ServiceExecCommand id = _SERVICE_EXEC_COMMAND_INVALID; |
| 3766 |
_cleanup_free_ char *path = NULL; |
| 3767 |
_cleanup_strv_free_ char **argv = NULL; |
| 3768 |
unsigned idx = 0, i; |
| 3769 |
bool control, found = false, last = false; |
| 3770 |
int r; |
| 3771 |
|
| 3772 |
enum { |
| 3773 |
STATE_EXEC_COMMAND_TYPE, |
| 3774 |
STATE_EXEC_COMMAND_INDEX, |
| 3775 |
STATE_EXEC_COMMAND_PATH, |
| 3776 |
STATE_EXEC_COMMAND_ARGS, |
| 3777 |
_STATE_EXEC_COMMAND_MAX, |
| 3778 |
_STATE_EXEC_COMMAND_INVALID = -EINVAL, |
| 3779 |
} state; |
| 3780 |
|
| 3781 |
assert(key); |
| 3782 |
assert(value); |
| 3783 |
|
| 3784 |
control = streq(key, "control-command"); |
| 3785 |
|
| 3786 |
state = STATE_EXEC_COMMAND_TYPE; |
| 3787 |
|
| 3788 |
for (;;) { |
| 3789 |
_cleanup_free_ char *arg = NULL; |
| 3790 |
|
| 3791 |
r = extract_first_word(&value, &arg, NULL, EXTRACT_CUNESCAPE | EXTRACT_UNQUOTE); |
| 3792 |
if (r < 0) |
| 3793 |
return r; |
| 3794 |
if (r == 0) |
| 3795 |
break; |
| 3796 |
|
| 3797 |
switch (state) { |
| 3798 |
|
| 3799 |
case STATE_EXEC_COMMAND_TYPE: |
| 3800 |
id = service_exec_command_from_string(arg); |
| 3801 |
if (id < 0) |
| 3802 |
return id; |
| 3803 |
|
| 3804 |
state = STATE_EXEC_COMMAND_INDEX; |
| 3805 |
break; |
| 3806 |
|
| 3807 |
case STATE_EXEC_COMMAND_INDEX: |
| 3808 |
/* ExecCommand index 1234 is serialized as either '1234' or '+1234'. The second form |
| 3809 |
* is used to mark the last command in a sequence. We warn if the deserialized command |
| 3810 |
* doesn't match what we have loaded from the unit, but we don't need to warn if |
| 3811 |
* that is the last command. */ |
| 3812 |
|
| 3813 |
r = safe_atou(arg, &idx); |
| 3814 |
if (r < 0) |
| 3815 |
return r; |
| 3816 |
last = arg[0] == '+'; |
| 3817 |
|
| 3818 |
state = STATE_EXEC_COMMAND_PATH; |
| 3819 |
break; |
| 3820 |
|
| 3821 |
case STATE_EXEC_COMMAND_PATH: |
| 3822 |
path = TAKE_PTR(arg); |
| 3823 |
state = STATE_EXEC_COMMAND_ARGS; |
| 3824 |
break; |
| 3825 |
|
| 3826 |
case STATE_EXEC_COMMAND_ARGS: |
| 3827 |
r = strv_extend(&argv, arg); |
| 3828 |
if (r < 0) |
| 3829 |
return r; |
| 3830 |
break; |
| 3831 |
|
| 3832 |
default: |
| 3833 |
assert_not_reached(); |
| 3834 |
} |
| 3835 |
} |
| 3836 |
|
| 3837 |
if (state != STATE_EXEC_COMMAND_ARGS) |
| 3838 |
return -EINVAL; |
| 3839 |
if (strv_isempty(argv)) |
| 3840 |
return -EINVAL; /* At least argv[0] must be always present. */ |
| 3841 |
|
| 3842 |
/* Let's check whether exec command on given offset matches data that we just deserialized */ |
| 3843 |
for (command = s->exec_command[id], i = 0; command; command = command->command_next, i++) { |
| 3844 |
if (i != idx) |
| 3845 |
continue; |
| 3846 |
|
| 3847 |
found = strv_equal(argv, command->argv) && streq(command->path, path); |
| 3848 |
break; |
| 3849 |
} |
| 3850 |
|
| 3851 |
if (!found) { |
| 3852 |
/* Command at the index we serialized is different, let's look for command that exactly |
| 3853 |
* matches but is on different index. If there is no such command we will not resume execution. */ |
| 3854 |
for (command = s->exec_command[id]; command; command = command->command_next) |
| 3855 |
if (strv_equal(command->argv, argv) && streq(command->path, path)) |
| 3856 |
break; |
| 3857 |
} |
| 3858 |
|
| 3859 |
if (command && control) { |
| 3860 |
s->control_command = command; |
| 3861 |
s->control_command_id = id; |
| 3862 |
} else if (command) |
| 3863 |
s->main_command = command; |
| 3864 |
else if (last) |
| 3865 |
log_unit_debug(u, "Current command vanished from the unit file."); |
| 3866 |
else |
| 3867 |
log_unit_warning(u, "Current command vanished from the unit file, execution of the command list won't be resumed."); |
| 3868 |
|
| 3869 |
return 0; |
| 3870 |
} |
| 3871 |
|
| 3872 |
static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { |
| 3873 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 3874 |
int r; |
| 3875 |
|
| 3876 |
assert(key); |
| 3877 |
assert(value); |
| 3878 |
assert(fds); |
| 3879 |
|
| 3880 |
if (streq(key, "state")) { |
| 3881 |
ServiceState state; |
| 3882 |
|
| 3883 |
state = service_state_from_string(value); |
| 3884 |
if (state < 0) |
| 3885 |
log_unit_debug_errno(u, state, "Failed to parse state value: %s", value); |
| 3886 |
else |
| 3887 |
s->deserialized_state = state; |
| 3888 |
} else if (streq(key, "result")) { |
| 3889 |
ServiceResult f; |
| 3890 |
|
| 3891 |
f = service_result_from_string(value); |
| 3892 |
if (f < 0) |
| 3893 |
log_unit_debug_errno(u, f, "Failed to parse result value: %s", value); |
| 3894 |
else if (f != SERVICE_SUCCESS) |
| 3895 |
s->result = f; |
| 3896 |
|
| 3897 |
} else if (streq(key, "reload-result")) { |
| 3898 |
ServiceResult f; |
| 3899 |
|
| 3900 |
f = service_result_from_string(value); |
| 3901 |
if (f < 0) |
| 3902 |
log_unit_debug_errno(u, f, "Failed to parse reload result value: %s", value); |
| 3903 |
else if (f != SERVICE_SUCCESS) |
| 3904 |
s->reload_result = f; |
| 3905 |
|
| 3906 |
} else if (streq(key, "live-mount-result")) { |
| 3907 |
ServiceResult f; |
| 3908 |
|
| 3909 |
f = service_result_from_string(value); |
| 3910 |
if (f < 0) |
| 3911 |
log_unit_debug_errno(u, f, "Failed to parse live mount result value: %s", value); |
| 3912 |
else if (f != SERVICE_SUCCESS) |
| 3913 |
s->live_mount_result = f; |
| 3914 |
|
| 3915 |
} else if (streq(key, "control-pid")) { |
| 3916 |
|
| 3917 |
if (!pidref_is_set(&s->control_pid)) |
| 3918 |
(void) deserialize_pidref(fds, value, &s->control_pid); |
| 3919 |
|
| 3920 |
} else if (streq(key, "main-pid")) { |
| 3921 |
PidRef pidref; |
| 3922 |
|
| 3923 |
if (!pidref_is_set(&s->main_pid) && deserialize_pidref(fds, value, &pidref) >= 0) |
| 3924 |
(void) service_set_main_pidref(s, pidref, /* start_timestamp= */ NULL); |
| 3925 |
|
| 3926 |
} else if (streq(key, "main-pid-known")) { |
| 3927 |
r = parse_boolean(value); |
| 3928 |
if (r < 0) |
| 3929 |
log_unit_debug_errno(u, r, "Failed to parse main-pid-known value: %s", value); |
| 3930 |
else |
| 3931 |
s->main_pid_known = r; |
| 3932 |
} else if (streq(key, "bus-name-good")) { |
| 3933 |
r = parse_boolean(value); |
| 3934 |
if (r < 0) |
| 3935 |
log_unit_debug_errno(u, r, "Failed to parse bus-name-good value: %s", value); |
| 3936 |
else |
| 3937 |
s->bus_name_good = r; |
| 3938 |
} else if (streq(key, "accept-socket")) { |
| 3939 |
Unit *socket; |
| 3940 |
|
| 3941 |
if (unit_name_to_type(value) != UNIT_SOCKET) { |
| 3942 |
log_unit_debug(u, "Deserialized accept-socket is not a socket unit, ignoring: %s", value); |
| 3943 |
return 0; |
| 3944 |
} |
| 3945 |
|
| 3946 |
r = manager_load_unit(u->manager, value, NULL, NULL, &socket); |
| 3947 |
if (r < 0) |
| 3948 |
log_unit_debug_errno(u, r, "Failed to load accept-socket unit '%s': %m", value); |
| 3949 |
else { |
| 3950 |
unit_ref_set(&s->accept_socket, u, socket); |
| 3951 |
ASSERT_PTR(SOCKET(socket))->n_connections++; |
| 3952 |
} |
| 3953 |
|
| 3954 |
} else if (streq(key, "socket-fd")) { |
| 3955 |
asynchronous_close(s->socket_fd); |
| 3956 |
s->socket_fd = deserialize_fd(fds, value); |
| 3957 |
|
| 3958 |
} else if (streq(key, "fd-store-fd")) { |
| 3959 |
_cleanup_free_ char *fdv = NULL, *fdn = NULL, *fdp = NULL, *fdi = NULL; |
| 3960 |
_cleanup_close_ int fd = -EBADF; |
| 3961 |
int do_poll; |
| 3962 |
uint64_t index = 0; |
| 3963 |
|
| 3964 |
r = extract_many_words(&value, " ", EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE, &fdv, &fdn, &fdp, &fdi); |
| 3965 |
if (r < 2 || r > 4) { |
| 3966 |
log_unit_debug(u, "Failed to deserialize fd-store-fd, ignoring: %s", value); |
| 3967 |
return 0; |
| 3968 |
} |
| 3969 |
|
| 3970 |
fd = deserialize_fd(fds, fdv); |
| 3971 |
if (fd < 0) |
| 3972 |
return 0; |
| 3973 |
|
| 3974 |
do_poll = r >= 3 ? parse_boolean(fdp) : true; |
| 3975 |
if (do_poll < 0) { |
| 3976 |
log_unit_debug_errno(u, do_poll, |
| 3977 |
"Failed to deserialize fd-store-fd do_poll, ignoring: %s", fdp); |
| 3978 |
return 0; |
| 3979 |
} |
| 3980 |
|
| 3981 |
if (r == 4 && safe_atou64(fdi, &index) < 0) { |
| 3982 |
log_unit_debug(u, "Failed to parse fd-store-fd index '%s', ignoring.", fdi); |
| 3983 |
index = 0; |
| 3984 |
} |
| 3985 |
|
| 3986 |
/* If the unit file is currently absent (e.g. after switch-root, before the unit file is |
| 3987 |
* available in the new root), the synthesized service has n_fd_store_max=0 and |
| 3988 |
* preserve_mode=NO, which would reject the fd. Grow the limit by one per fd so it matches |
| 3989 |
* exactly what was handed back, and force EXEC_PRESERVE_YES, so the fd survives until |
| 3990 |
* either a daemon-reload picks up the unit file or the service is explicitly stopped. |
| 3991 |
* Same logic as in luo_dispatch_fd(). */ |
| 3992 |
if (u->load_state == UNIT_NOT_FOUND) { |
| 3993 |
s->fd_store_preserve_mode = EXEC_PRESERVE_YES; |
| 3994 |
s->n_fd_store_max++; |
| 3995 |
} |
| 3996 |
|
| 3997 |
/* Don't propagate upstream during deserialization: the upstream supervisor (if any) |
| 3998 |
* already has these fds from when they were originally pushed. */ |
| 3999 |
r = service_add_fd_store(s, TAKE_FD(fd), fdn, do_poll, /* propagate_upstream= */ false); |
| 4000 |
if (r <= 0 && u->load_state == UNIT_NOT_FOUND) |
| 4001 |
/* The fd was not actually stored, roll back the limit bump. */ |
| 4002 |
s->n_fd_store_max--; |
| 4003 |
if (r < 0) { |
| 4004 |
log_unit_debug_errno(u, r, |
| 4005 |
"Failed to store deserialized fd '%s', ignoring: %m", fdn); |
| 4006 |
return 0; |
| 4007 |
} |
| 4008 |
/* If preservation is enabled then this fd was previously propagated upstream when it |
| 4009 |
* was first pushed. Restore the index so future removals can be forwarded upstream |
| 4010 |
* and the JSON mapping memfd can be regenerated. */ |
| 4011 |
if (r > 0 && s->fd_store && index > 0) |
| 4012 |
s->fd_store->index = index; |
| 4013 |
} else if (streq(key, "extra-fd")) { |
| 4014 |
_cleanup_free_ char *fdv = NULL, *fdn = NULL; |
| 4015 |
_cleanup_close_ int fd = -EBADF; |
| 4016 |
|
| 4017 |
r = extract_many_words(&value, " ", EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE, &fdv, &fdn); |
| 4018 |
if (r != 2) { |
| 4019 |
log_unit_debug(u, "Failed to deserialize extra-fd, ignoring: %s", value); |
| 4020 |
return 0; |
| 4021 |
} |
| 4022 |
|
| 4023 |
fd = deserialize_fd(fds, fdv); |
| 4024 |
if (fd < 0) |
| 4025 |
return 0; |
| 4026 |
|
| 4027 |
if (!GREEDY_REALLOC(s->extra_fds, s->n_extra_fds + 1)) { |
| 4028 |
log_oom_debug(); |
| 4029 |
return 0; |
| 4030 |
} |
| 4031 |
|
| 4032 |
s->extra_fds[s->n_extra_fds++] = (ServiceExtraFD) { |
| 4033 |
.fd = TAKE_FD(fd), |
| 4034 |
.fdname = TAKE_PTR(fdn), |
| 4035 |
}; |
| 4036 |
} else if (streq(key, "main-exec-status-pid")) { |
| 4037 |
pid_t pid; |
| 4038 |
|
| 4039 |
if (parse_pid(value, &pid) < 0) |
| 4040 |
log_unit_debug(u, "Failed to parse main-exec-status-pid value: %s", value); |
| 4041 |
else |
| 4042 |
s->main_exec_status.pid = pid; |
| 4043 |
} else if (streq(key, "main-exec-status-code")) { |
| 4044 |
int i; |
| 4045 |
|
| 4046 |
if (safe_atoi(value, &i) < 0) |
| 4047 |
log_unit_debug(u, "Failed to parse main-exec-status-code value: %s", value); |
| 4048 |
else |
| 4049 |
s->main_exec_status.code = i; |
| 4050 |
} else if (streq(key, "main-exec-status-status")) { |
| 4051 |
int i; |
| 4052 |
|
| 4053 |
if (safe_atoi(value, &i) < 0) |
| 4054 |
log_unit_debug(u, "Failed to parse main-exec-status-status value: %s", value); |
| 4055 |
else |
| 4056 |
s->main_exec_status.status = i; |
| 4057 |
} else if (streq(key, "main-exec-status-start")) |
| 4058 |
(void) deserialize_dual_timestamp(value, &s->main_exec_status.start_timestamp); |
| 4059 |
else if (streq(key, "main-exec-status-exit")) |
| 4060 |
(void) deserialize_dual_timestamp(value, &s->main_exec_status.exit_timestamp); |
| 4061 |
else if (streq(key, "main-exec-status-handoff")) |
| 4062 |
(void) deserialize_dual_timestamp(value, &s->main_exec_status.handoff_timestamp); |
| 4063 |
else if (STR_IN_SET(key, "main-command", "control-command")) { |
| 4064 |
r = service_deserialize_exec_command(u, key, value); |
| 4065 |
if (r < 0) |
| 4066 |
log_unit_debug_errno(u, r, "Failed to parse serialized command \"%s\": %m", value); |
| 4067 |
} else if (streq(key, "notify-access-override")) { |
| 4068 |
NotifyAccess notify_access; |
| 4069 |
|
| 4070 |
notify_access = notify_access_from_string(value); |
| 4071 |
if (notify_access < 0) |
| 4072 |
log_unit_debug(u, "Failed to parse notify-access-override value: %s", value); |
| 4073 |
else |
| 4074 |
s->notify_access_override = notify_access; |
| 4075 |
|
| 4076 |
} else if (streq(key, "notify-state")) { |
| 4077 |
NotifyState notify_state; |
| 4078 |
|
| 4079 |
notify_state = notify_state_from_string(value); |
| 4080 |
if (notify_state < 0) |
| 4081 |
log_unit_debug(u, "Failed to parse notify-state value: %s", value); |
| 4082 |
else |
| 4083 |
s->notify_state = notify_state; |
| 4084 |
|
| 4085 |
} else if (streq(key, "n-restarts")) { |
| 4086 |
r = safe_atou(value, &s->n_restarts); |
| 4087 |
if (r < 0) |
| 4088 |
log_unit_debug_errno(u, r, "Failed to parse serialized restart counter '%s': %m", value); |
| 4089 |
|
| 4090 |
} else if (streq(key, "forbid-restart")) { |
| 4091 |
r = parse_boolean(value); |
| 4092 |
if (r < 0) |
| 4093 |
log_unit_debug_errno(u, r, "Failed to parse forbid-restart value: %s", value); |
| 4094 |
else |
| 4095 |
s->forbid_restart = r; |
| 4096 |
} else if (streq(key, "stdin-fd")) { |
| 4097 |
|
| 4098 |
asynchronous_close(s->stdin_fd); |
| 4099 |
s->stdin_fd = deserialize_fd(fds, value); |
| 4100 |
if (s->stdin_fd >= 0) |
| 4101 |
s->exec_context.stdio_as_fds = true; |
| 4102 |
|
| 4103 |
} else if (streq(key, "stdout-fd")) { |
| 4104 |
|
| 4105 |
asynchronous_close(s->stdout_fd); |
| 4106 |
s->stdout_fd = deserialize_fd(fds, value); |
| 4107 |
if (s->stdout_fd >= 0) |
| 4108 |
s->exec_context.stdio_as_fds = true; |
| 4109 |
|
| 4110 |
} else if (streq(key, "stderr-fd")) { |
| 4111 |
|
| 4112 |
asynchronous_close(s->stderr_fd); |
| 4113 |
s->stderr_fd = deserialize_fd(fds, value); |
| 4114 |
if (s->stderr_fd >= 0) |
| 4115 |
s->exec_context.stdio_as_fds = true; |
| 4116 |
|
| 4117 |
} else if (streq(key, "root-directory-fd")) { |
| 4118 |
|
| 4119 |
asynchronous_close(s->root_directory_fd); |
| 4120 |
s->root_directory_fd = deserialize_fd(fds, value); |
| 4121 |
if (s->root_directory_fd >= 0) |
| 4122 |
s->exec_context.root_directory_as_fd = true; |
| 4123 |
|
| 4124 |
} else if (streq(key, "exec-fd")) { |
| 4125 |
_cleanup_close_ int fd = -EBADF; |
| 4126 |
|
| 4127 |
fd = deserialize_fd(fds, value); |
| 4128 |
if (fd >= 0) { |
| 4129 |
s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); |
| 4130 |
|
| 4131 |
if (service_allocate_exec_fd_event_source(s, fd, &s->exec_fd_event_source) >= 0) |
| 4132 |
TAKE_FD(fd); |
| 4133 |
} |
| 4134 |
|
| 4135 |
} else if (streq(key, "status-text")) { |
| 4136 |
char *t; |
| 4137 |
ssize_t l; |
| 4138 |
|
| 4139 |
l = cunescape(value, 0, &t); |
| 4140 |
if (l < 0) |
| 4141 |
log_unit_debug_errno(u, l, "Failed to unescape status text '%s': %m", value); |
| 4142 |
else |
| 4143 |
free_and_replace(s->status_text, t); |
| 4144 |
|
| 4145 |
} else if (streq(key, "status-errno")) { |
| 4146 |
int i; |
| 4147 |
|
| 4148 |
if (safe_atoi(value, &i) < 0) |
| 4149 |
log_unit_debug(u, "Failed to parse status-errno value: %s", value); |
| 4150 |
else |
| 4151 |
s->status_errno = i; |
| 4152 |
|
| 4153 |
} else if (streq(key, "status-bus-error")) { |
| 4154 |
if (free_and_strdup(&s->status_bus_error, value) < 0) |
| 4155 |
log_oom_debug(); |
| 4156 |
|
| 4157 |
} else if (streq(key, "status-varlink-error")) { |
| 4158 |
if (free_and_strdup(&s->status_varlink_error, value) < 0) |
| 4159 |
log_oom_debug(); |
| 4160 |
|
| 4161 |
} else if (streq(key, "watchdog-timestamp")) |
| 4162 |
(void) deserialize_dual_timestamp(value, &s->watchdog_timestamp); |
| 4163 |
else if (streq(key, "watchdog-original-usec")) |
| 4164 |
(void) deserialize_usec(value, &s->watchdog_original_usec); |
| 4165 |
else if (streq(key, "watchdog-override-usec")) { |
| 4166 |
if (deserialize_usec(value, &s->watchdog_override_usec) >= 0) |
| 4167 |
s->watchdog_override_enable = true; |
| 4168 |
|
| 4169 |
} else if (streq(key, "reload-begin-usec")) |
| 4170 |
(void) deserialize_usec(value, &s->reload_begin_usec); |
| 4171 |
else if (streq(key, "refreshed-mask")) { |
| 4172 |
r = service_refresh_on_reload_from_string_many(value, &s->refreshed_mask); |
| 4173 |
if (r < 0) |
| 4174 |
log_unit_debug_errno(u, r, "Failed to parse refreshed-mask value: %s", value); |
| 4175 |
} else |
| 4176 |
log_unit_debug(u, "Unknown serialization key: %s", key); |
| 4177 |
|
| 4178 |
return 0; |
| 4179 |
} |
| 4180 |
|
| 4181 |
static UnitActiveState service_active_state(Unit *u) { |
| 4182 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 4183 |
const UnitActiveState *table; |
| 4184 |
|
| 4185 |
table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table; |
| 4186 |
|
| 4187 |
return table[s->state]; |
| 4188 |
} |
| 4189 |
|
| 4190 |
static const char *service_sub_state_to_string(Unit *u) { |
| 4191 |
assert(u); |
| 4192 |
|
| 4193 |
return service_state_to_string(SERVICE(u)->state); |
| 4194 |
} |
| 4195 |
|
| 4196 |
static bool service_may_gc(Unit *u) { |
| 4197 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 4198 |
|
| 4199 |
/* Never clean up services that still have a process around, even if the service is formally dead. Note that |
| 4200 |
* unit_may_gc() already checked our cgroup for us, we just check our two additional PIDs, too, in case they |
| 4201 |
* have moved outside of the cgroup. */ |
| 4202 |
|
| 4203 |
if (main_pid_good(s) > 0 || |
| 4204 |
control_pid_good(s) > 0) |
| 4205 |
return false; |
| 4206 |
|
| 4207 |
/* Only allow collection of actually dead services, i.e. not those that are in the transitionary |
| 4208 |
* SERVICE_DEAD_BEFORE_AUTO_RESTART/SERVICE_FAILED_BEFORE_AUTO_RESTART states, and not those |
| 4209 |
* that still have resources pinned (fd store with FileDescriptorStorePreserve=yes) in case they are |
| 4210 |
* started again later despite not having any reverse dependency. */ |
| 4211 |
if (!IN_SET(s->state, SERVICE_DEAD, SERVICE_FAILED)) |
| 4212 |
return false; |
| 4213 |
|
| 4214 |
return true; |
| 4215 |
} |
| 4216 |
|
| 4217 |
static int service_retry_pid_file(Service *s) { |
| 4218 |
int r; |
| 4219 |
|
| 4220 |
assert(s); |
| 4221 |
assert(s->pid_file); |
| 4222 |
assert(IN_SET(s->state, SERVICE_START, SERVICE_START_POST)); |
| 4223 |
|
| 4224 |
r = service_load_pid_file(s, false); |
| 4225 |
if (r < 0) |
| 4226 |
return r; |
| 4227 |
|
| 4228 |
service_unwatch_pid_file(s); |
| 4229 |
|
| 4230 |
service_enter_running(s, SERVICE_SUCCESS); |
| 4231 |
return 0; |
| 4232 |
} |
| 4233 |
|
| 4234 |
static int service_watch_pid_file(Service *s) { |
| 4235 |
int r; |
| 4236 |
|
| 4237 |
assert(s); |
| 4238 |
|
| 4239 |
log_unit_debug(UNIT(s), "Setting watch for PID file %s", s->pid_file_pathspec->path); |
| 4240 |
|
| 4241 |
r = path_spec_watch(s->pid_file_pathspec, service_dispatch_inotify_io); |
| 4242 |
if (r < 0) { |
| 4243 |
log_unit_error_errno(UNIT(s), r, "Failed to set a watch for PID file %s: %m", s->pid_file_pathspec->path); |
| 4244 |
service_unwatch_pid_file(s); |
| 4245 |
return r; |
| 4246 |
} |
| 4247 |
|
| 4248 |
/* the pidfile might have appeared just before we set the watch */ |
| 4249 |
log_unit_debug(UNIT(s), "Trying to read PID file %s in case it changed", s->pid_file_pathspec->path); |
| 4250 |
service_retry_pid_file(s); |
| 4251 |
|
| 4252 |
return 0; |
| 4253 |
} |
| 4254 |
|
| 4255 |
static int service_demand_pid_file(Service *s) { |
| 4256 |
_cleanup_free_ PathSpec *ps = NULL; |
| 4257 |
|
| 4258 |
assert(s); |
| 4259 |
assert(s->pid_file); |
| 4260 |
assert(!s->pid_file_pathspec); |
| 4261 |
|
| 4262 |
ps = new(PathSpec, 1); |
| 4263 |
if (!ps) |
| 4264 |
return -ENOMEM; |
| 4265 |
|
| 4266 |
*ps = (PathSpec) { |
| 4267 |
.unit = UNIT(s), |
| 4268 |
.path = strdup(s->pid_file), |
| 4269 |
/* PATH_CHANGED would not be enough. There are daemons (sendmail) that keep their PID file |
| 4270 |
* open all the time. */ |
| 4271 |
.type = PATH_MODIFIED, |
| 4272 |
.inotify_fd = -EBADF, |
| 4273 |
}; |
| 4274 |
|
| 4275 |
if (!ps->path) |
| 4276 |
return -ENOMEM; |
| 4277 |
|
| 4278 |
path_simplify(ps->path); |
| 4279 |
|
| 4280 |
s->pid_file_pathspec = TAKE_PTR(ps); |
| 4281 |
|
| 4282 |
return service_watch_pid_file(s); |
| 4283 |
} |
| 4284 |
|
| 4285 |
static int service_dispatch_inotify_io(sd_event_source *source, int fd, uint32_t events, void *userdata) { |
| 4286 |
PathSpec *p = ASSERT_PTR(userdata); |
| 4287 |
Service *s = ASSERT_PTR(SERVICE(p->unit)); |
| 4288 |
|
| 4289 |
assert(fd >= 0); |
| 4290 |
assert(IN_SET(s->state, SERVICE_START, SERVICE_START_POST)); |
| 4291 |
assert(s->pid_file_pathspec); |
| 4292 |
assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd)); |
| 4293 |
|
| 4294 |
log_unit_debug(UNIT(s), "inotify event"); |
| 4295 |
|
| 4296 |
if (path_spec_fd_event(p, events) < 0) |
| 4297 |
goto fail; |
| 4298 |
|
| 4299 |
if (service_retry_pid_file(s) == 0) |
| 4300 |
return 0; |
| 4301 |
|
| 4302 |
if (service_watch_pid_file(s) < 0) |
| 4303 |
goto fail; |
| 4304 |
|
| 4305 |
return 0; |
| 4306 |
|
| 4307 |
fail: |
| 4308 |
service_unwatch_pid_file(s); |
| 4309 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
| 4310 |
return 0; |
| 4311 |
} |
| 4312 |
|
| 4313 |
static int service_dispatch_exec_io(sd_event_source *source, int fd, uint32_t events, void *userdata) { |
| 4314 |
Service *s = ASSERT_PTR(SERVICE(userdata)); |
| 4315 |
|
| 4316 |
log_unit_debug(UNIT(s), "got exec-fd event"); |
| 4317 |
|
| 4318 |
/* If Type=exec is set, we'll consider a service started successfully the instant we invoked execve() |
| 4319 |
* successfully for it. We implement this through a pipe() towards the child, which the kernel |
| 4320 |
* automatically closes for us due to O_CLOEXEC on execve() in the child, which then triggers EOF on |
| 4321 |
* the pipe in the parent. We need to be careful however, as there are other reasons that we might |
| 4322 |
* cause the child's side of the pipe to be closed (for example, a simple exit()). To deal with that |
| 4323 |
* we'll ignore EOFs on the pipe unless the child signalled us first that it is about to call the |
| 4324 |
* execve(). It does so by sending us a simple non-zero byte via the pipe. We also provide the child |
| 4325 |
* with a way to inform us in case execve() failed: if it sends a zero byte we'll ignore POLLHUP on |
| 4326 |
* the fd again. */ |
| 4327 |
|
| 4328 |
for (;;) { |
| 4329 |
uint8_t x; |
| 4330 |
ssize_t n; |
| 4331 |
|
| 4332 |
n = read(fd, &x, sizeof(x)); |
| 4333 |
if (n < 0) { |
| 4334 |
if (errno == EAGAIN) /* O_NONBLOCK in effect → everything queued has now been processed. */ |
| 4335 |
return 0; |
| 4336 |
|
| 4337 |
return log_unit_error_errno(UNIT(s), errno, "Failed to read from exec_fd: %m"); |
| 4338 |
} |
| 4339 |
if (n == 0) { /* EOF → the event we are waiting for in case of Type=exec */ |
| 4340 |
s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); |
| 4341 |
|
| 4342 |
if (s->exec_fd_hot) { /* Did the child tell us to expect EOF now? */ |
| 4343 |
log_unit_debug(UNIT(s), "Got EOF on exec-fd"); |
| 4344 |
|
| 4345 |
s->exec_fd_hot = false; |
| 4346 |
|
| 4347 |
/* Nice! This is what we have been waiting for. Transition to next state. */ |
| 4348 |
if (s->type == SERVICE_EXEC && s->state == SERVICE_START) |
| 4349 |
service_enter_start_post(s); |
| 4350 |
} else |
| 4351 |
log_unit_debug(UNIT(s), "Got EOF on exec-fd while it was disabled, ignoring."); |
| 4352 |
|
| 4353 |
return 0; |
| 4354 |
} |
| 4355 |
|
| 4356 |
/* A byte was read → this turns on/off the exec fd logic */ |
| 4357 |
assert(n == sizeof(x)); |
| 4358 |
|
| 4359 |
s->exec_fd_hot = x; |
| 4360 |
} |
| 4361 |
} |
| 4362 |
|
| 4363 |
static void service_notify_cgroup_empty_event(Unit *u) { |
| 4364 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 4365 |
|
| 4366 |
log_unit_debug(u, "Control group is empty."); |
| 4367 |
|
| 4368 |
switch (s->state) { |
| 4369 |
|
| 4370 |
/* Waiting for SIGCHLD is usually more interesting, because it includes return |
| 4371 |
* codes/signals. Which is why we ignore the cgroup events for most cases, except when we |
| 4372 |
* don't know pid which to expect the SIGCHLD for. */ |
| 4373 |
|
| 4374 |
case SERVICE_START: |
| 4375 |
if (IN_SET(s->type, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD) && |
| 4376 |
main_pid_good(s) == 0 && |
| 4377 |
control_pid_good(s) == 0) { |
| 4378 |
/* No chance of getting a ready notification anymore */ |
| 4379 |
service_enter_stop_post(s, SERVICE_FAILURE_PROTOCOL); |
| 4380 |
break; |
| 4381 |
} |
| 4382 |
|
| 4383 |
if (s->exit_type == SERVICE_EXIT_CGROUP && main_pid_good(s) <= 0) { |
| 4384 |
service_enter_stop_post(s, SERVICE_SUCCESS); |
| 4385 |
break; |
| 4386 |
} |
| 4387 |
|
| 4388 |
_fallthrough_; |
| 4389 |
case SERVICE_START_POST: |
| 4390 |
if (s->pid_file_pathspec && |
| 4391 |
main_pid_good(s) == 0 && |
| 4392 |
control_pid_good(s) == 0) { |
| 4393 |
|
| 4394 |
/* Give up hoping for the daemon to write its PID file */ |
| 4395 |
log_unit_warning(u, "Daemon never wrote its PID file. Failing."); |
| 4396 |
|
| 4397 |
service_unwatch_pid_file(s); |
| 4398 |
if (s->state == SERVICE_START) |
| 4399 |
service_enter_stop_post(s, SERVICE_FAILURE_PROTOCOL); |
| 4400 |
else |
| 4401 |
service_enter_stop(s, SERVICE_FAILURE_PROTOCOL); |
| 4402 |
} |
| 4403 |
break; |
| 4404 |
|
| 4405 |
case SERVICE_RUNNING: |
| 4406 |
/* service_enter_running() will figure out what to do */ |
| 4407 |
service_enter_running(s, SERVICE_SUCCESS); |
| 4408 |
break; |
| 4409 |
|
| 4410 |
case SERVICE_STOP_WATCHDOG: |
| 4411 |
case SERVICE_STOP_SIGTERM: |
| 4412 |
case SERVICE_STOP_SIGKILL: |
| 4413 |
|
| 4414 |
if (main_pid_good(s) <= 0 && control_pid_good(s) <= 0) |
| 4415 |
service_enter_stop_post(s, SERVICE_SUCCESS); |
| 4416 |
|
| 4417 |
break; |
| 4418 |
|
| 4419 |
case SERVICE_STOP_POST: |
| 4420 |
case SERVICE_FINAL_WATCHDOG: |
| 4421 |
case SERVICE_FINAL_SIGTERM: |
| 4422 |
case SERVICE_FINAL_SIGKILL: |
| 4423 |
if (main_pid_good(s) <= 0 && control_pid_good(s) <= 0) |
| 4424 |
service_enter_dead(s, SERVICE_SUCCESS, true); |
| 4425 |
|
| 4426 |
break; |
| 4427 |
|
| 4428 |
/* If the cgroup empty notification comes when the unit is not active, we must have failed to clean |
| 4429 |
* up the cgroup earlier and should do it now. */ |
| 4430 |
case SERVICE_AUTO_RESTART: |
| 4431 |
case SERVICE_AUTO_RESTART_QUEUED: |
| 4432 |
unit_prune_cgroup(u); |
| 4433 |
break; |
| 4434 |
|
| 4435 |
default: |
| 4436 |
; |
| 4437 |
} |
| 4438 |
} |
| 4439 |
|
| 4440 |
static void service_notify_cgroup_oom_event(Unit *u, bool managed_oom) { |
| 4441 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 4442 |
|
| 4443 |
if (managed_oom) |
| 4444 |
log_unit_debug(u, "Process(es) of control group were killed by systemd-oomd."); |
| 4445 |
else |
| 4446 |
log_unit_debug(u, "Process of control group was killed by the OOM killer."); |
| 4447 |
|
| 4448 |
if (s->oom_policy == OOM_CONTINUE) |
| 4449 |
return; |
| 4450 |
|
| 4451 |
switch (s->state) { |
| 4452 |
|
| 4453 |
case SERVICE_CONDITION: |
| 4454 |
case SERVICE_START_PRE: |
| 4455 |
case SERVICE_START: |
| 4456 |
case SERVICE_START_POST: |
| 4457 |
case SERVICE_STOP: |
| 4458 |
if (s->oom_policy == OOM_STOP) |
| 4459 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_OOM_KILL); |
| 4460 |
else if (s->oom_policy == OOM_KILL) |
| 4461 |
service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_OOM_KILL); |
| 4462 |
|
| 4463 |
break; |
| 4464 |
|
| 4465 |
case SERVICE_EXITED: |
| 4466 |
case SERVICE_RUNNING: |
| 4467 |
if (s->oom_policy == OOM_STOP) |
| 4468 |
service_enter_stop(s, SERVICE_FAILURE_OOM_KILL); |
| 4469 |
else if (s->oom_policy == OOM_KILL) |
| 4470 |
service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_OOM_KILL); |
| 4471 |
|
| 4472 |
break; |
| 4473 |
|
| 4474 |
case SERVICE_STOP_WATCHDOG: |
| 4475 |
case SERVICE_STOP_SIGTERM: |
| 4476 |
service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_OOM_KILL); |
| 4477 |
break; |
| 4478 |
|
| 4479 |
case SERVICE_STOP_SIGKILL: |
| 4480 |
case SERVICE_FINAL_SIGKILL: |
| 4481 |
if (s->result == SERVICE_SUCCESS) |
| 4482 |
s->result = SERVICE_FAILURE_OOM_KILL; |
| 4483 |
break; |
| 4484 |
|
| 4485 |
case SERVICE_STOP_POST: |
| 4486 |
case SERVICE_FINAL_SIGTERM: |
| 4487 |
service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_OOM_KILL); |
| 4488 |
break; |
| 4489 |
|
| 4490 |
default: |
| 4491 |
; |
| 4492 |
} |
| 4493 |
} |
| 4494 |
|
| 4495 |
static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
| 4496 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 4497 |
bool notify_dbus = true; |
| 4498 |
ServiceResult f; |
| 4499 |
ExitClean clean_mode; |
| 4500 |
int r; |
| 4501 |
|
| 4502 |
assert(pid >= 0); |
| 4503 |
|
| 4504 |
/* Oneshot services and non-SERVICE_EXEC_START commands should not be |
| 4505 |
* considered daemons as they are typically not long running. */ |
| 4506 |
if (s->type == SERVICE_ONESHOT || (s->control_pid.pid == pid && s->control_command_id != SERVICE_EXEC_START)) |
| 4507 |
clean_mode = EXIT_CLEAN_COMMAND; |
| 4508 |
else |
| 4509 |
clean_mode = EXIT_CLEAN_DAEMON; |
| 4510 |
|
| 4511 |
/* Our own helper processes are not subject to SuccessExitStatus= as they're opaque to users */ |
| 4512 |
if (is_clean_exit(code, status, clean_mode, s->control_pid.pid == pid && s->control_command_id < 0 ? NULL : &s->success_status)) |
| 4513 |
f = SERVICE_SUCCESS; |
| 4514 |
else if (code == CLD_EXITED) |
| 4515 |
f = SERVICE_FAILURE_EXIT_CODE; |
| 4516 |
else if (code == CLD_KILLED) |
| 4517 |
f = SERVICE_FAILURE_SIGNAL; |
| 4518 |
else if (code == CLD_DUMPED) |
| 4519 |
f = SERVICE_FAILURE_CORE_DUMP; |
| 4520 |
else |
| 4521 |
assert_not_reached(); |
| 4522 |
|
| 4523 |
if (s->main_pid.pid == pid) { |
| 4524 |
/* Clean up the exec_fd event source. We want to do this here, not later in |
| 4525 |
* service_set_state(), because service_enter_stop_post() calls service_spawn(). |
| 4526 |
* The source owns its end of the pipe, so this will close that too. */ |
| 4527 |
s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); |
| 4528 |
|
| 4529 |
/* Forking services may occasionally move to a new PID. |
| 4530 |
* As long as they update the PID file before exiting the old |
| 4531 |
* PID, they're fine. */ |
| 4532 |
if (service_load_pid_file(s, false) > 0) |
| 4533 |
return; |
| 4534 |
|
| 4535 |
pidref_done(&s->main_pid); |
| 4536 |
exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status); |
| 4537 |
|
| 4538 |
if (s->main_command) { |
| 4539 |
/* If this is not a forking service than the |
| 4540 |
* main process got started and hence we copy |
| 4541 |
* the exit status so that it is recorded both |
| 4542 |
* as main and as control process exit |
| 4543 |
* status */ |
| 4544 |
|
| 4545 |
s->main_command->exec_status = s->main_exec_status; |
| 4546 |
|
| 4547 |
if (s->main_command->flags & EXEC_COMMAND_IGNORE_FAILURE) |
| 4548 |
f = SERVICE_SUCCESS; |
| 4549 |
} else if (s->exec_command[SERVICE_EXEC_START]) { |
| 4550 |
|
| 4551 |
/* If this is a forked process, then we should |
| 4552 |
* ignore the return value if this was |
| 4553 |
* configured for the starter process */ |
| 4554 |
|
| 4555 |
if (s->exec_command[SERVICE_EXEC_START]->flags & EXEC_COMMAND_IGNORE_FAILURE) |
| 4556 |
f = SERVICE_SUCCESS; |
| 4557 |
} |
| 4558 |
|
| 4559 |
unit_log_process_exit( |
| 4560 |
u, |
| 4561 |
"Main process", |
| 4562 |
service_exec_command_to_string(SERVICE_EXEC_START), |
| 4563 |
f == SERVICE_SUCCESS, |
| 4564 |
code, status); |
| 4565 |
|
| 4566 |
if (s->result == SERVICE_SUCCESS) |
| 4567 |
s->result = f; |
| 4568 |
|
| 4569 |
if (s->main_command && |
| 4570 |
s->main_command->command_next && |
| 4571 |
s->type == SERVICE_ONESHOT && |
| 4572 |
f == SERVICE_SUCCESS) { |
| 4573 |
|
| 4574 |
/* There is another command to execute, so let's do that. */ |
| 4575 |
|
| 4576 |
log_unit_debug(u, "Running next main command for state %s.", service_state_to_string(s->state)); |
| 4577 |
service_run_next_main(s); |
| 4578 |
|
| 4579 |
} else { |
| 4580 |
s->main_command = NULL; |
| 4581 |
|
| 4582 |
/* Services with ExitType=cgroup do not act on main PID exiting, unless the cgroup is |
| 4583 |
* already empty */ |
| 4584 |
if (s->exit_type == SERVICE_EXIT_MAIN || cgroup_good(s) <= 0) { |
| 4585 |
/* The service exited, so the service is officially gone. */ |
| 4586 |
switch (s->state) { |
| 4587 |
|
| 4588 |
case SERVICE_START_POST: |
| 4589 |
case SERVICE_REFRESH_EXTENSIONS: |
| 4590 |
case SERVICE_REFRESH_CREDENTIALS: |
| 4591 |
case SERVICE_RELOAD: |
| 4592 |
case SERVICE_RELOAD_SIGNAL: |
| 4593 |
case SERVICE_RELOAD_NOTIFY: |
| 4594 |
case SERVICE_RELOAD_POST: |
| 4595 |
case SERVICE_MOUNTING: |
| 4596 |
/* If neither main nor control processes are running then the current |
| 4597 |
* state can never exit cleanly, hence immediately terminate the |
| 4598 |
* service. */ |
| 4599 |
if (control_pid_good(s) <= 0) |
| 4600 |
service_enter_stop(s, f); |
| 4601 |
|
| 4602 |
/* Otherwise need to wait until the operation is done. */ |
| 4603 |
break; |
| 4604 |
|
| 4605 |
case SERVICE_STOP: |
| 4606 |
/* Need to wait until the operation is done. */ |
| 4607 |
break; |
| 4608 |
|
| 4609 |
case SERVICE_START: |
| 4610 |
if (s->type == SERVICE_ONESHOT) { |
| 4611 |
/* This was our main goal, so let's go on */ |
| 4612 |
if (f == SERVICE_SUCCESS) |
| 4613 |
service_enter_start_post(s); |
| 4614 |
else |
| 4615 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
| 4616 |
break; |
| 4617 |
} else if (IN_SET(s->type, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD)) { |
| 4618 |
/* Only enter running through a notification, so that the |
| 4619 |
* SERVICE_START state signifies that no ready notification |
| 4620 |
* has been received */ |
| 4621 |
if (f != SERVICE_SUCCESS) |
| 4622 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
| 4623 |
else if (!s->remain_after_exit || service_get_notify_access(s) == NOTIFY_MAIN) |
| 4624 |
/* The service has never been and will never be active */ |
| 4625 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_PROTOCOL); |
| 4626 |
break; |
| 4627 |
} |
| 4628 |
|
| 4629 |
_fallthrough_; |
| 4630 |
case SERVICE_RUNNING: |
| 4631 |
service_enter_running(s, f); |
| 4632 |
break; |
| 4633 |
|
| 4634 |
case SERVICE_STOP_WATCHDOG: |
| 4635 |
case SERVICE_STOP_SIGTERM: |
| 4636 |
case SERVICE_STOP_SIGKILL: |
| 4637 |
|
| 4638 |
if (control_pid_good(s) <= 0) |
| 4639 |
service_enter_stop_post(s, f); |
| 4640 |
|
| 4641 |
/* If there is still a control process, wait for that first */ |
| 4642 |
break; |
| 4643 |
|
| 4644 |
case SERVICE_STOP_POST: |
| 4645 |
|
| 4646 |
if (control_pid_good(s) <= 0) |
| 4647 |
service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); |
| 4648 |
|
| 4649 |
break; |
| 4650 |
|
| 4651 |
case SERVICE_FINAL_WATCHDOG: |
| 4652 |
case SERVICE_FINAL_SIGTERM: |
| 4653 |
case SERVICE_FINAL_SIGKILL: |
| 4654 |
|
| 4655 |
if (control_pid_good(s) <= 0) |
| 4656 |
service_enter_dead(s, f, true); |
| 4657 |
break; |
| 4658 |
|
| 4659 |
default: |
| 4660 |
assert_not_reached(); |
| 4661 |
} |
| 4662 |
} else if (s->exit_type == SERVICE_EXIT_CGROUP && s->state == SERVICE_START && |
| 4663 |
!IN_SET(s->type, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD, SERVICE_DBUS)) |
| 4664 |
/* If a main process exits very quickly, this function might be executed |
| 4665 |
* before service_dispatch_exec_io(). Since this function disabled IO events |
| 4666 |
* to monitor the main process above, we need to update the state here too. |
| 4667 |
* Let's consider the process is successfully launched and exited, but |
| 4668 |
* only when we're not expecting a readiness notification or dbus name. */ |
| 4669 |
service_enter_start_post(s); |
| 4670 |
} |
| 4671 |
|
| 4672 |
} else if (s->control_pid.pid == pid) { |
| 4673 |
const char *kind; |
| 4674 |
bool success; |
| 4675 |
|
| 4676 |
pidref_done(&s->control_pid); |
| 4677 |
|
| 4678 |
if (s->control_command) { |
| 4679 |
exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status); |
| 4680 |
|
| 4681 |
if (s->control_command->flags & EXEC_COMMAND_IGNORE_FAILURE) |
| 4682 |
f = SERVICE_SUCCESS; |
| 4683 |
} |
| 4684 |
|
| 4685 |
/* ExecCondition= calls that exit with (0, 254] should invoke skip-like behavior instead of failing */ |
| 4686 |
if (s->state == SERVICE_CONDITION) { |
| 4687 |
if (f == SERVICE_FAILURE_EXIT_CODE && status < 255) { |
| 4688 |
UNIT(s)->condition_result = false; |
| 4689 |
f = SERVICE_SKIP_CONDITION; |
| 4690 |
success = true; |
| 4691 |
} else if (f == SERVICE_SUCCESS) { |
| 4692 |
UNIT(s)->condition_result = true; |
| 4693 |
success = true; |
| 4694 |
} else |
| 4695 |
success = false; |
| 4696 |
|
| 4697 |
kind = "Condition check process"; |
| 4698 |
} else { |
| 4699 |
kind = "Control process"; |
| 4700 |
success = f == SERVICE_SUCCESS; |
| 4701 |
} |
| 4702 |
|
| 4703 |
unit_log_process_exit( |
| 4704 |
u, |
| 4705 |
kind, |
| 4706 |
service_exec_command_to_string(s->control_command_id), |
| 4707 |
success, |
| 4708 |
code, status); |
| 4709 |
|
| 4710 |
if (!IN_SET(s->state, SERVICE_REFRESH_EXTENSIONS, SERVICE_REFRESH_CREDENTIALS, SERVICE_RELOAD, SERVICE_RELOAD_POST, SERVICE_MOUNTING) && |
| 4711 |
s->result == SERVICE_SUCCESS) |
| 4712 |
s->result = f; |
| 4713 |
|
| 4714 |
if (s->control_command && |
| 4715 |
s->control_command->command_next && |
| 4716 |
f == SERVICE_SUCCESS) { |
| 4717 |
|
| 4718 |
/* There is another command to execute, so let's do that. */ |
| 4719 |
|
| 4720 |
log_unit_debug(u, "Running next control command for state %s.", service_state_to_string(s->state)); |
| 4721 |
service_run_next_control(s); |
| 4722 |
|
| 4723 |
} else { |
| 4724 |
/* No further commands for this step, so let's figure out what to do next */ |
| 4725 |
|
| 4726 |
s->control_command = NULL; |
| 4727 |
s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
| 4728 |
|
| 4729 |
log_unit_debug(u, "Got final SIGCHLD for state %s.", service_state_to_string(s->state)); |
| 4730 |
|
| 4731 |
switch (s->state) { |
| 4732 |
|
| 4733 |
case SERVICE_CONDITION: |
| 4734 |
if (f == SERVICE_SUCCESS) |
| 4735 |
service_enter_start_pre(s); |
| 4736 |
else |
| 4737 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
| 4738 |
break; |
| 4739 |
|
| 4740 |
case SERVICE_START_PRE: |
| 4741 |
if (f == SERVICE_SUCCESS) |
| 4742 |
service_enter_start(s); |
| 4743 |
else |
| 4744 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
| 4745 |
break; |
| 4746 |
|
| 4747 |
case SERVICE_START: |
| 4748 |
if (s->type != SERVICE_FORKING) |
| 4749 |
/* Maybe spurious event due to a reload that changed the type? */ |
| 4750 |
break; |
| 4751 |
|
| 4752 |
if (f != SERVICE_SUCCESS) { |
| 4753 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
| 4754 |
break; |
| 4755 |
} |
| 4756 |
|
| 4757 |
if (s->pid_file) { |
| 4758 |
bool has_start_post; |
| 4759 |
|
| 4760 |
/* Let's try to load the pid file here if we can. |
| 4761 |
* The PID file might actually be created by a START_POST |
| 4762 |
* script. In that case don't worry if the loading fails. */ |
| 4763 |
|
| 4764 |
has_start_post = s->exec_command[SERVICE_EXEC_START_POST]; |
| 4765 |
r = service_load_pid_file(s, !has_start_post); |
| 4766 |
if (!has_start_post && r < 0) { |
| 4767 |
r = service_demand_pid_file(s); |
| 4768 |
if (r < 0 || cgroup_good(s) == 0) |
| 4769 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_PROTOCOL); |
| 4770 |
break; |
| 4771 |
} |
| 4772 |
} else |
| 4773 |
service_search_main_pid(s); |
| 4774 |
|
| 4775 |
service_enter_start_post(s); |
| 4776 |
break; |
| 4777 |
|
| 4778 |
case SERVICE_START_POST: |
| 4779 |
if (f != SERVICE_SUCCESS) { |
| 4780 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
| 4781 |
break; |
| 4782 |
} |
| 4783 |
|
| 4784 |
if (s->pid_file) { |
| 4785 |
r = service_load_pid_file(s, true); |
| 4786 |
if (r < 0) { |
| 4787 |
r = service_demand_pid_file(s); |
| 4788 |
if (r < 0 || cgroup_good(s) == 0) |
| 4789 |
service_enter_stop(s, SERVICE_FAILURE_PROTOCOL); |
| 4790 |
break; |
| 4791 |
} |
| 4792 |
} else |
| 4793 |
service_search_main_pid(s); |
| 4794 |
|
| 4795 |
service_enter_running(s, SERVICE_SUCCESS); |
| 4796 |
break; |
| 4797 |
|
| 4798 |
case SERVICE_REFRESH_EXTENSIONS: |
| 4799 |
if (f == SERVICE_SUCCESS) { |
| 4800 |
s->refreshed_mask |= SERVICE_RELOAD_EXTENSIONS; |
| 4801 |
service_enter_refresh_credentials(s); |
| 4802 |
} else |
| 4803 |
service_reload_finish(s, f); |
| 4804 |
break; |
| 4805 |
|
| 4806 |
case SERVICE_REFRESH_CREDENTIALS: |
| 4807 |
if (f == SERVICE_SUCCESS || |
| 4808 |
(f == SERVICE_FAILURE_EXIT_CODE && IN_SET(status, EXIT_NOTINSTALLED, EXIT_NOPERMISSION))) { |
| 4809 |
|
| 4810 |
/* Refreshing asynchronously done, proceed to reload */ |
| 4811 |
s->refreshed_mask |= f == SERVICE_SUCCESS ? SERVICE_RELOAD_CREDENTIALS : 0; |
| 4812 |
service_enter_reload(s); |
| 4813 |
} else |
| 4814 |
service_reload_finish(s, f); |
| 4815 |
break; |
| 4816 |
|
| 4817 |
case SERVICE_RELOAD: |
| 4818 |
if (f != SERVICE_SUCCESS) { |
| 4819 |
service_reload_finish(s, f); |
| 4820 |
break; |
| 4821 |
} |
| 4822 |
|
| 4823 |
if (service_load_pid_file(s, true) < 0) |
| 4824 |
service_search_main_pid(s); |
| 4825 |
|
| 4826 |
service_enter_reload_signal(s); |
| 4827 |
break; |
| 4828 |
|
| 4829 |
case SERVICE_RELOAD_POST: |
| 4830 |
service_reload_finish(s, f); |
| 4831 |
break; |
| 4832 |
|
| 4833 |
case SERVICE_MOUNTING: |
| 4834 |
service_live_mount_finish(s, f, SD_BUS_ERROR_FAILED); |
| 4835 |
|
| 4836 |
service_enter_running(s, SERVICE_SUCCESS); |
| 4837 |
break; |
| 4838 |
|
| 4839 |
case SERVICE_STOP: |
| 4840 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
| 4841 |
break; |
| 4842 |
|
| 4843 |
case SERVICE_STOP_WATCHDOG: |
| 4844 |
case SERVICE_STOP_SIGTERM: |
| 4845 |
case SERVICE_STOP_SIGKILL: |
| 4846 |
if (main_pid_good(s) <= 0) |
| 4847 |
service_enter_stop_post(s, f); |
| 4848 |
|
| 4849 |
/* If there is still a service process around, wait until |
| 4850 |
* that one quit, too */ |
| 4851 |
break; |
| 4852 |
|
| 4853 |
case SERVICE_STOP_POST: |
| 4854 |
if (main_pid_good(s) <= 0) |
| 4855 |
service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); |
| 4856 |
break; |
| 4857 |
|
| 4858 |
case SERVICE_FINAL_WATCHDOG: |
| 4859 |
case SERVICE_FINAL_SIGTERM: |
| 4860 |
case SERVICE_FINAL_SIGKILL: |
| 4861 |
if (main_pid_good(s) <= 0) |
| 4862 |
service_enter_dead(s, f, true); |
| 4863 |
break; |
| 4864 |
|
| 4865 |
case SERVICE_CLEANING: |
| 4866 |
|
| 4867 |
if (s->clean_result == SERVICE_SUCCESS) |
| 4868 |
s->clean_result = f; |
| 4869 |
|
| 4870 |
service_enter_dead(s, SERVICE_SUCCESS, false); |
| 4871 |
break; |
| 4872 |
|
| 4873 |
default: |
| 4874 |
assert_not_reached(); |
| 4875 |
} |
| 4876 |
} |
| 4877 |
} else /* Neither control nor main PID? If so, don't notify about anything */ |
| 4878 |
notify_dbus = false; |
| 4879 |
|
| 4880 |
/* Notify clients about changed exit status */ |
| 4881 |
if (notify_dbus) |
| 4882 |
unit_add_to_dbus_queue(u); |
| 4883 |
} |
| 4884 |
|
| 4885 |
static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
| 4886 |
Service *s = ASSERT_PTR(SERVICE(userdata)); |
| 4887 |
|
| 4888 |
assert(source == s->timer_event_source); |
| 4889 |
|
| 4890 |
switch (s->state) { |
| 4891 |
|
| 4892 |
case SERVICE_CONDITION: |
| 4893 |
case SERVICE_START_PRE: |
| 4894 |
case SERVICE_START: |
| 4895 |
case SERVICE_START_POST: |
| 4896 |
switch (s->timeout_start_failure_mode) { |
| 4897 |
|
| 4898 |
case SERVICE_TIMEOUT_TERMINATE: |
| 4899 |
log_unit_warning(UNIT(s), "%s operation timed out. Terminating.", service_state_to_string(s->state)); |
| 4900 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
| 4901 |
break; |
| 4902 |
|
| 4903 |
case SERVICE_TIMEOUT_ABORT: |
| 4904 |
log_unit_warning(UNIT(s), "%s operation timed out. Aborting.", service_state_to_string(s->state)); |
| 4905 |
service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_TIMEOUT); |
| 4906 |
break; |
| 4907 |
|
| 4908 |
case SERVICE_TIMEOUT_KILL: |
| 4909 |
if (s->kill_context.send_sigkill) { |
| 4910 |
log_unit_warning(UNIT(s), "%s operation timed out. Killing.", service_state_to_string(s->state)); |
| 4911 |
service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
| 4912 |
} else { |
| 4913 |
log_unit_warning(UNIT(s), "%s operation timed out. Skipping SIGKILL.", service_state_to_string(s->state)); |
| 4914 |
service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
| 4915 |
} |
| 4916 |
break; |
| 4917 |
|
| 4918 |
default: |
| 4919 |
assert_not_reached(); |
| 4920 |
} |
| 4921 |
break; |
| 4922 |
|
| 4923 |
case SERVICE_RUNNING: |
| 4924 |
log_unit_warning(UNIT(s), "Service reached runtime time limit. Stopping."); |
| 4925 |
service_enter_stop(s, SERVICE_FAILURE_TIMEOUT); |
| 4926 |
break; |
| 4927 |
|
| 4928 |
case SERVICE_REFRESH_EXTENSIONS: |
| 4929 |
case SERVICE_REFRESH_CREDENTIALS: |
| 4930 |
case SERVICE_RELOAD: |
| 4931 |
case SERVICE_RELOAD_SIGNAL: |
| 4932 |
case SERVICE_RELOAD_NOTIFY: |
| 4933 |
case SERVICE_RELOAD_POST: |
| 4934 |
log_unit_warning(UNIT(s), "Reload operation timed out. Killing reload process."); |
| 4935 |
service_kill_control_process(s); |
| 4936 |
service_reload_finish(s, SERVICE_FAILURE_TIMEOUT); |
| 4937 |
break; |
| 4938 |
|
| 4939 |
case SERVICE_MOUNTING: |
| 4940 |
log_unit_warning(UNIT(s), "Mount operation timed out. Killing mount process."); |
| 4941 |
service_kill_control_process(s); |
| 4942 |
service_live_mount_finish(s, SERVICE_FAILURE_TIMEOUT, SD_BUS_ERROR_TIMEOUT); |
| 4943 |
service_enter_running(s, SERVICE_SUCCESS); |
| 4944 |
break; |
| 4945 |
|
| 4946 |
case SERVICE_STOP: |
| 4947 |
switch (s->timeout_stop_failure_mode) { |
| 4948 |
|
| 4949 |
case SERVICE_TIMEOUT_TERMINATE: |
| 4950 |
log_unit_warning(UNIT(s), "Stopping timed out. Terminating."); |
| 4951 |
service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
| 4952 |
break; |
| 4953 |
|
| 4954 |
case SERVICE_TIMEOUT_ABORT: |
| 4955 |
log_unit_warning(UNIT(s), "Stopping timed out. Aborting."); |
| 4956 |
service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_TIMEOUT); |
| 4957 |
break; |
| 4958 |
|
| 4959 |
case SERVICE_TIMEOUT_KILL: |
| 4960 |
if (s->kill_context.send_sigkill) { |
| 4961 |
log_unit_warning(UNIT(s), "Stopping timed out. Killing."); |
| 4962 |
service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
| 4963 |
} else { |
| 4964 |
log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL."); |
| 4965 |
service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
| 4966 |
} |
| 4967 |
break; |
| 4968 |
|
| 4969 |
default: |
| 4970 |
assert_not_reached(); |
| 4971 |
} |
| 4972 |
break; |
| 4973 |
|
| 4974 |
case SERVICE_STOP_WATCHDOG: |
| 4975 |
if (s->kill_context.send_sigkill) { |
| 4976 |
log_unit_warning(UNIT(s), "State 'stop-watchdog' timed out. Killing."); |
| 4977 |
service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
| 4978 |
} else { |
| 4979 |
log_unit_warning(UNIT(s), "State 'stop-watchdog' timed out. Skipping SIGKILL."); |
| 4980 |
service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
| 4981 |
} |
| 4982 |
break; |
| 4983 |
|
| 4984 |
case SERVICE_STOP_SIGTERM: |
| 4985 |
if (s->timeout_stop_failure_mode == SERVICE_TIMEOUT_ABORT) { |
| 4986 |
log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Aborting."); |
| 4987 |
service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_TIMEOUT); |
| 4988 |
} else if (s->kill_context.send_sigkill) { |
| 4989 |
log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Killing."); |
| 4990 |
service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
| 4991 |
} else { |
| 4992 |
log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Skipping SIGKILL."); |
| 4993 |
service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
| 4994 |
} |
| 4995 |
|
| 4996 |
break; |
| 4997 |
|
| 4998 |
case SERVICE_STOP_SIGKILL: |
| 4999 |
/* Uh, we sent a SIGKILL and it is still not gone? |
| 5000 |
* Must be something we cannot kill, so let's just be |
| 5001 |
* weirded out and continue */ |
| 5002 |
|
| 5003 |
log_unit_warning(UNIT(s), "Processes still around after SIGKILL. Ignoring."); |
| 5004 |
service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
| 5005 |
break; |
| 5006 |
|
| 5007 |
case SERVICE_STOP_POST: |
| 5008 |
switch (s->timeout_stop_failure_mode) { |
| 5009 |
|
| 5010 |
case SERVICE_TIMEOUT_TERMINATE: |
| 5011 |
log_unit_warning(UNIT(s), "State 'stop-post' timed out. Terminating."); |
| 5012 |
service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
| 5013 |
break; |
| 5014 |
|
| 5015 |
case SERVICE_TIMEOUT_ABORT: |
| 5016 |
log_unit_warning(UNIT(s), "State 'stop-post' timed out. Aborting."); |
| 5017 |
service_enter_signal(s, SERVICE_FINAL_WATCHDOG, SERVICE_FAILURE_TIMEOUT); |
| 5018 |
break; |
| 5019 |
|
| 5020 |
case SERVICE_TIMEOUT_KILL: |
| 5021 |
if (s->kill_context.send_sigkill) { |
| 5022 |
log_unit_warning(UNIT(s), "State 'stop-post' timed out. Killing."); |
| 5023 |
service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
| 5024 |
} else { |
| 5025 |
log_unit_warning(UNIT(s), "State 'stop-post' timed out. Skipping SIGKILL. Entering failed mode."); |
| 5026 |
service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); |
| 5027 |
} |
| 5028 |
break; |
| 5029 |
|
| 5030 |
default: |
| 5031 |
assert_not_reached(); |
| 5032 |
} |
| 5033 |
break; |
| 5034 |
|
| 5035 |
case SERVICE_FINAL_WATCHDOG: |
| 5036 |
if (s->kill_context.send_sigkill) { |
| 5037 |
log_unit_warning(UNIT(s), "State 'final-watchdog' timed out. Killing."); |
| 5038 |
service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
| 5039 |
} else { |
| 5040 |
log_unit_warning(UNIT(s), "State 'final-watchdog' timed out. Skipping SIGKILL. Entering failed mode."); |
| 5041 |
service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); |
| 5042 |
} |
| 5043 |
break; |
| 5044 |
|
| 5045 |
case SERVICE_FINAL_SIGTERM: |
| 5046 |
if (s->timeout_stop_failure_mode == SERVICE_TIMEOUT_ABORT) { |
| 5047 |
log_unit_warning(UNIT(s), "State 'final-sigterm' timed out. Aborting."); |
| 5048 |
service_enter_signal(s, SERVICE_FINAL_WATCHDOG, SERVICE_FAILURE_TIMEOUT); |
| 5049 |
} else if (s->kill_context.send_sigkill) { |
| 5050 |
log_unit_warning(UNIT(s), "State 'final-sigterm' timed out. Killing."); |
| 5051 |
service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
| 5052 |
} else { |
| 5053 |
log_unit_warning(UNIT(s), "State 'final-sigterm' timed out. Skipping SIGKILL. Entering failed mode."); |
| 5054 |
service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); |
| 5055 |
} |
| 5056 |
|
| 5057 |
break; |
| 5058 |
|
| 5059 |
case SERVICE_FINAL_SIGKILL: |
| 5060 |
log_unit_warning(UNIT(s), "Processes still around after final SIGKILL. Entering failed mode."); |
| 5061 |
service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true); |
| 5062 |
break; |
| 5063 |
|
| 5064 |
case SERVICE_AUTO_RESTART: |
| 5065 |
if (s->restart_usec > 0) |
| 5066 |
log_unit_debug(UNIT(s), |
| 5067 |
"Service restart interval %s expired, scheduling restart.", |
| 5068 |
FORMAT_TIMESPAN(service_restart_usec_next(s), USEC_PER_SEC)); |
| 5069 |
else |
| 5070 |
log_unit_debug(UNIT(s), |
| 5071 |
"Service has no hold-off time (RestartSec=0), scheduling restart."); |
| 5072 |
|
| 5073 |
service_enter_restart(s, /* shortcut= */ false); |
| 5074 |
break; |
| 5075 |
|
| 5076 |
case SERVICE_CLEANING: |
| 5077 |
log_unit_warning(UNIT(s), "Cleaning timed out. killing."); |
| 5078 |
|
| 5079 |
if (s->clean_result == SERVICE_SUCCESS) |
| 5080 |
s->clean_result = SERVICE_FAILURE_TIMEOUT; |
| 5081 |
|
| 5082 |
service_enter_signal(s, SERVICE_FINAL_SIGKILL, 0); |
| 5083 |
break; |
| 5084 |
|
| 5085 |
default: |
| 5086 |
assert_not_reached(); |
| 5087 |
} |
| 5088 |
|
| 5089 |
return 0; |
| 5090 |
} |
| 5091 |
|
| 5092 |
static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) { |
| 5093 |
Service *s = ASSERT_PTR(SERVICE(userdata)); |
| 5094 |
usec_t watchdog_usec; |
| 5095 |
|
| 5096 |
assert(source == s->watchdog_event_source); |
| 5097 |
|
| 5098 |
watchdog_usec = service_get_watchdog_usec(s); |
| 5099 |
|
| 5100 |
if (UNIT(s)->manager->service_watchdogs) { |
| 5101 |
log_unit_error(UNIT(s), "Watchdog timeout (limit %s)!", |
| 5102 |
FORMAT_TIMESPAN(watchdog_usec, 1)); |
| 5103 |
|
| 5104 |
service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_WATCHDOG); |
| 5105 |
} else |
| 5106 |
log_unit_warning(UNIT(s), "Watchdog disabled! Ignoring watchdog timeout (limit %s)!", |
| 5107 |
FORMAT_TIMESPAN(watchdog_usec, 1)); |
| 5108 |
|
| 5109 |
return 0; |
| 5110 |
} |
| 5111 |
|
| 5112 |
static void service_force_watchdog(Service *s) { |
| 5113 |
assert(s); |
| 5114 |
|
| 5115 |
if (!UNIT(s)->manager->service_watchdogs) |
| 5116 |
return; |
| 5117 |
|
| 5118 |
log_unit_error(UNIT(s), "Watchdog request (last status: %s)!", |
| 5119 |
s->status_text ?: "<unset>"); |
| 5120 |
|
| 5121 |
service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_WATCHDOG); |
| 5122 |
} |
| 5123 |
|
| 5124 |
static bool service_notify_message_authorized(Service *s, PidRef *pid) { |
| 5125 |
assert(s); |
| 5126 |
assert(pidref_is_set(pid)); |
| 5127 |
|
| 5128 |
switch (service_get_notify_access(s)) { |
| 5129 |
|
| 5130 |
case NOTIFY_NONE: |
| 5131 |
/* Warn level only if no notifications are expected */ |
| 5132 |
log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception is disabled", pid->pid); |
| 5133 |
return false; |
| 5134 |
|
| 5135 |
case NOTIFY_ALL: |
| 5136 |
return true; |
| 5137 |
|
| 5138 |
case NOTIFY_MAIN: |
| 5139 |
if (pidref_equal(pid, &s->main_pid)) |
| 5140 |
return true; |
| 5141 |
|
| 5142 |
if (pidref_is_set(&s->main_pid)) |
| 5143 |
log_unit_debug(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT, pid->pid, s->main_pid.pid); |
| 5144 |
else |
| 5145 |
log_unit_debug(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID which is currently not known", pid->pid); |
| 5146 |
|
| 5147 |
return false; |
| 5148 |
|
| 5149 |
case NOTIFY_EXEC: |
| 5150 |
if (pidref_equal(pid, &s->main_pid) || pidref_equal(pid, &s->control_pid)) |
| 5151 |
return true; |
| 5152 |
|
| 5153 |
if (pidref_is_set(&s->main_pid) && pidref_is_set(&s->control_pid)) |
| 5154 |
log_unit_debug(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT" and control PID "PID_FMT, |
| 5155 |
pid->pid, s->main_pid.pid, s->control_pid.pid); |
| 5156 |
else if (pidref_is_set(&s->main_pid)) |
| 5157 |
log_unit_debug(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT, pid->pid, s->main_pid.pid); |
| 5158 |
else if (pidref_is_set(&s->control_pid)) |
| 5159 |
log_unit_debug(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for control PID "PID_FMT, pid->pid, s->control_pid.pid); |
| 5160 |
else |
| 5161 |
log_unit_debug(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID and control PID which are currently not known", pid->pid); |
| 5162 |
|
| 5163 |
return false; |
| 5164 |
|
| 5165 |
default: |
| 5166 |
assert_not_reached(); |
| 5167 |
} |
| 5168 |
} |
| 5169 |
|
| 5170 |
static int service_notify_message_parse_new_pid( |
| 5171 |
Unit *u, |
| 5172 |
char * const *tags, |
| 5173 |
FDSet *fds, |
| 5174 |
PidRef *ret) { |
| 5175 |
|
| 5176 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 5177 |
const char *e; |
| 5178 |
int r; |
| 5179 |
|
| 5180 |
assert(u); |
| 5181 |
assert(ret); |
| 5182 |
|
| 5183 |
/* MAINPIDFD=1 always takes precedence */ |
| 5184 |
if (strv_contains(tags, "MAINPIDFD=1")) { |
| 5185 |
unsigned n_fds = fdset_size(fds); |
| 5186 |
if (n_fds != 1) |
| 5187 |
return log_unit_warning_errno(u, SYNTHETIC_ERRNO(EINVAL), |
| 5188 |
"Got MAINPIDFD=1 with %s fd, ignoring.", n_fds == 0 ? "no" : "more than one"); |
| 5189 |
|
| 5190 |
r = pidref_set_pidfd_consume(&pidref, ASSERT_FD(fdset_steal_first(fds))); |
| 5191 |
if (r < 0) |
| 5192 |
return log_unit_warning_errno(u, r, "Failed to create reference to received new main pidfd: %m"); |
| 5193 |
|
| 5194 |
goto finish; |
| 5195 |
} |
| 5196 |
|
| 5197 |
e = strv_find_startswith(tags, "MAINPID="); |
| 5198 |
if (!e) { |
| 5199 |
*ret = PIDREF_NULL; |
| 5200 |
return 0; |
| 5201 |
} |
| 5202 |
|
| 5203 |
r = pidref_set_pidstr(&pidref, e); |
| 5204 |
if (r < 0) |
| 5205 |
return log_unit_warning_errno(u, r, "Failed to parse MAINPID=%s field in notification message, ignoring: %m", e); |
| 5206 |
|
| 5207 |
e = strv_find_startswith(tags, "MAINPIDFDID="); |
| 5208 |
if (!e) |
| 5209 |
goto finish; |
| 5210 |
|
| 5211 |
uint64_t pidfd_id; |
| 5212 |
|
| 5213 |
r = safe_atou64(e, &pidfd_id); |
| 5214 |
if (r < 0) |
| 5215 |
return log_unit_warning_errno(u, r, "Failed to parse MAINPIDFDID= in notification message, refusing: %s", e); |
| 5216 |
|
| 5217 |
r = pidref_acquire_pidfd_id(&pidref); |
| 5218 |
if (r < 0) { |
| 5219 |
if (!ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 5220 |
log_unit_warning_errno(u, r, |
| 5221 |
"Failed to acquire pidfd id of process " PID_FMT ", not validating MAINPIDFDID=%" PRIu64 ": %m", |
| 5222 |
pidref.pid, pidfd_id); |
| 5223 |
goto finish; |
| 5224 |
} |
| 5225 |
|
| 5226 |
if (pidref.fd_id != pidfd_id) |
| 5227 |
return log_unit_warning_errno(u, SYNTHETIC_ERRNO(ESRCH), |
| 5228 |
"PIDFD ID of process " PID_FMT " (%" PRIu64 ") mismatches with received MAINPIDFDID=%" PRIu64 ", not changing main PID.", |
| 5229 |
pidref.pid, pidref.fd_id, pidfd_id); |
| 5230 |
|
| 5231 |
finish: |
| 5232 |
*ret = TAKE_PIDREF(pidref); |
| 5233 |
return 1; |
| 5234 |
} |
| 5235 |
|
| 5236 |
static void service_notify_message_process_state(Service *s, char * const *tags) { |
| 5237 |
usec_t monotonic_usec = USEC_INFINITY; |
| 5238 |
int r; |
| 5239 |
|
| 5240 |
assert(s); |
| 5241 |
|
| 5242 |
const char *e = strv_find_startswith(tags, "MONOTONIC_USEC="); |
| 5243 |
if (e) { |
| 5244 |
r = safe_atou64(e, &monotonic_usec); |
| 5245 |
if (r < 0) |
| 5246 |
log_unit_warning_errno(UNIT(s), r, "Failed to parse MONOTONIC_USEC= field in notification message, ignoring: %s", e); |
| 5247 |
} |
| 5248 |
|
| 5249 |
/* Interpret READY=/STOPPING=/RELOADING=. STOPPING= wins over the others, and READY= over RELOADING= */ |
| 5250 |
if (strv_contains(tags, "STOPPING=1")) { |
| 5251 |
s->notify_state = NOTIFY_STOPPING; |
| 5252 |
|
| 5253 |
if (IN_SET(s->state, SERVICE_RUNNING, |
| 5254 |
SERVICE_REFRESH_EXTENSIONS, SERVICE_REFRESH_CREDENTIALS, |
| 5255 |
SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY)) |
| 5256 |
service_enter_stop_by_notify(s); |
| 5257 |
|
| 5258 |
return; |
| 5259 |
} |
| 5260 |
|
| 5261 |
/* Disallow resurrecting a dying service */ |
| 5262 |
if (s->notify_state == NOTIFY_STOPPING) |
| 5263 |
return; |
| 5264 |
|
| 5265 |
if (strv_contains(tags, "READY=1")) { |
| 5266 |
|
| 5267 |
if (s->notify_state == NOTIFY_RELOADING) |
| 5268 |
s->notify_state = NOTIFY_RELOAD_READY; |
| 5269 |
else |
| 5270 |
s->notify_state = NOTIFY_READY; |
| 5271 |
|
| 5272 |
/* Combined RELOADING=1 and READY=1? Then this is indication that the service started and |
| 5273 |
* immediately finished reloading. */ |
| 5274 |
if (strv_contains(tags, "RELOADING=1")) { |
| 5275 |
if (s->state == SERVICE_RELOAD_SIGNAL && |
| 5276 |
monotonic_usec != USEC_INFINITY && |
| 5277 |
monotonic_usec >= s->reload_begin_usec) |
| 5278 |
/* Valid Type=notify-reload protocol? Then we're all good. */ |
| 5279 |
service_enter_reload_post(s); |
| 5280 |
|
| 5281 |
else if (s->state == SERVICE_RUNNING) { |
| 5282 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 5283 |
|
| 5284 |
/* Propagate a reload explicitly for plain RELOADING=1 (semantically equivalent to |
| 5285 |
* service_enter_reload_by_notify() call in below) */ |
| 5286 |
r = manager_propagate_reload(UNIT(s)->manager, UNIT(s), JOB_FAIL, &error); |
| 5287 |
if (r < 0) |
| 5288 |
log_unit_warning(UNIT(s), "Failed to schedule propagation of reload, ignoring: %s", |
| 5289 |
bus_error_message(&error, r)); |
| 5290 |
} |
| 5291 |
} |
| 5292 |
|
| 5293 |
/* Type=notify(-reload) services inform us about completed initialization with READY=1 */ |
| 5294 |
if (IN_SET(s->type, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD) && |
| 5295 |
s->state == SERVICE_START) |
| 5296 |
service_enter_start_post(s); |
| 5297 |
|
| 5298 |
/* Sending READY=1 while we are reloading informs us that the reloading is complete. */ |
| 5299 |
if (s->state == SERVICE_RELOAD_NOTIFY) |
| 5300 |
service_enter_reload_post(s); |
| 5301 |
|
| 5302 |
} else if (strv_contains(tags, "RELOADING=1")) { |
| 5303 |
|
| 5304 |
s->notify_state = NOTIFY_RELOADING; |
| 5305 |
|
| 5306 |
/* Sending RELOADING=1 after we send SIGHUP to request a reload will transition |
| 5307 |
* things to "reload-notify" state, where we'll wait for READY=1 to let us know the |
| 5308 |
* reload is done. Note that we insist on a timestamp being sent along here, so that |
| 5309 |
* we know for sure this is a reload cycle initiated *after* we sent the signal */ |
| 5310 |
if (s->state == SERVICE_RELOAD_SIGNAL && |
| 5311 |
monotonic_usec != USEC_INFINITY && |
| 5312 |
monotonic_usec >= s->reload_begin_usec) |
| 5313 |
/* Note, we don't call service_enter_reload_by_notify() here, because we |
| 5314 |
* don't need reload propagation nor do we want to restart the timeout. */ |
| 5315 |
service_set_state(s, SERVICE_RELOAD_NOTIFY); |
| 5316 |
|
| 5317 |
if (s->state == SERVICE_RUNNING) |
| 5318 |
service_enter_reload_by_notify(s); |
| 5319 |
} |
| 5320 |
} |
| 5321 |
|
| 5322 |
static void service_notify_message( |
| 5323 |
Unit *u, |
| 5324 |
PidRef *pidref, |
| 5325 |
const struct ucred *ucred, |
| 5326 |
char * const *tags, |
| 5327 |
FDSet *fds) { |
| 5328 |
|
| 5329 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 5330 |
int r; |
| 5331 |
|
| 5332 |
assert(pidref_is_set(pidref)); |
| 5333 |
assert(ucred); |
| 5334 |
|
| 5335 |
if (!service_notify_message_authorized(s, pidref)) |
| 5336 |
return; |
| 5337 |
|
| 5338 |
if (DEBUG_LOGGING) { |
| 5339 |
_cleanup_free_ char *cc = strv_join(tags, ", "); |
| 5340 |
log_unit_debug(u, "Got notification message from PID "PID_FMT": %s", pidref->pid, empty_to_na(cc)); |
| 5341 |
} |
| 5342 |
|
| 5343 |
bool notify_dbus = false; |
| 5344 |
const char *e; |
| 5345 |
|
| 5346 |
/* Interpret MAINPID= (+ MAINPIDFDID=) / MAINPIDFD=1 */ |
| 5347 |
_cleanup_(pidref_done) PidRef new_main_pid = PIDREF_NULL; |
| 5348 |
|
| 5349 |
r = service_notify_message_parse_new_pid(u, tags, fds, &new_main_pid); |
| 5350 |
if (r > 0 && |
| 5351 |
IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, |
| 5352 |
SERVICE_REFRESH_EXTENSIONS, SERVICE_REFRESH_CREDENTIALS, |
| 5353 |
SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, SERVICE_RELOAD_POST, |
| 5354 |
SERVICE_STOP, SERVICE_STOP_SIGTERM) && |
| 5355 |
(!s->main_pid_known || !pidref_equal(&new_main_pid, &s->main_pid))) { |
| 5356 |
|
| 5357 |
r = service_is_suitable_main_pid(s, &new_main_pid, LOG_WARNING); |
| 5358 |
if (r == 0) { |
| 5359 |
/* The new main PID is a bit suspicious, which is OK if the sender is privileged. */ |
| 5360 |
|
| 5361 |
if (ucred->uid == 0) { |
| 5362 |
log_unit_debug(u, "New main PID "PID_FMT" does not belong to service, but we'll accept it as the request to change it came from a privileged process.", new_main_pid.pid); |
| 5363 |
r = 1; |
| 5364 |
} else |
| 5365 |
log_unit_warning(u, "New main PID "PID_FMT" does not belong to service, refusing.", new_main_pid.pid); |
| 5366 |
} |
| 5367 |
if (r > 0) { |
| 5368 |
(void) service_set_main_pidref(s, TAKE_PIDREF(new_main_pid), /* start_timestamp= */ NULL); |
| 5369 |
|
| 5370 |
r = unit_watch_pidref(UNIT(s), &s->main_pid, /* exclusive= */ false); |
| 5371 |
if (r < 0) |
| 5372 |
log_unit_warning_errno(UNIT(s), r, "Failed to watch new main PID "PID_FMT" for service: %m", s->main_pid.pid); |
| 5373 |
|
| 5374 |
notify_dbus = true; |
| 5375 |
} |
| 5376 |
} |
| 5377 |
|
| 5378 |
service_notify_message_process_state(s, tags); |
| 5379 |
|
| 5380 |
/* Interpret STATUS= */ |
| 5381 |
e = strv_find_startswith(tags, "STATUS="); |
| 5382 |
if (e) { |
| 5383 |
_cleanup_free_ char *t = NULL; |
| 5384 |
|
| 5385 |
if (!isempty(e)) { |
| 5386 |
/* Note that this size limit check is mostly paranoia: since the datagram size we are willing |
| 5387 |
* to process is already limited to NOTIFY_BUFFER_MAX, this limit here should never be hit. */ |
| 5388 |
if (strlen(e) > STATUS_TEXT_MAX) |
| 5389 |
log_unit_warning(u, "Status message overly long (%zu > %u), ignoring.", strlen(e), STATUS_TEXT_MAX); |
| 5390 |
else if (!utf8_is_valid(e)) |
| 5391 |
log_unit_warning(u, "Status message in notification message is not UTF-8 clean, ignoring."); |
| 5392 |
else { |
| 5393 |
t = strdup(e); |
| 5394 |
if (!t) |
| 5395 |
log_oom_warning(); |
| 5396 |
} |
| 5397 |
} |
| 5398 |
|
| 5399 |
if (!streq_ptr(s->status_text, t)) { |
| 5400 |
free_and_replace(s->status_text, t); |
| 5401 |
notify_dbus = true; |
| 5402 |
} |
| 5403 |
} |
| 5404 |
|
| 5405 |
/* Interpret NOTIFYACCESS= */ |
| 5406 |
e = strv_find_startswith(tags, "NOTIFYACCESS="); |
| 5407 |
if (e) { |
| 5408 |
NotifyAccess notify_access; |
| 5409 |
|
| 5410 |
notify_access = notify_access_from_string(e); |
| 5411 |
if (notify_access < 0) |
| 5412 |
log_unit_warning_errno(u, notify_access, |
| 5413 |
"Failed to parse NOTIFYACCESS= field value '%s' in notification message, ignoring: %m", e); |
| 5414 |
|
| 5415 |
/* We don't need to check whether the new access mode is more strict than what is |
| 5416 |
* already in use, since only the privileged process is allowed to change it |
| 5417 |
* in the first place. */ |
| 5418 |
if (service_get_notify_access(s) != notify_access) { |
| 5419 |
service_override_notify_access(s, notify_access); |
| 5420 |
notify_dbus = true; |
| 5421 |
} |
| 5422 |
} |
| 5423 |
|
| 5424 |
/* Interpret ERRNO= */ |
| 5425 |
e = strv_find_startswith(tags, "ERRNO="); |
| 5426 |
if (e) { |
| 5427 |
int status_errno; |
| 5428 |
|
| 5429 |
status_errno = parse_errno(e); |
| 5430 |
if (status_errno < 0) |
| 5431 |
log_unit_warning_errno(u, status_errno, |
| 5432 |
"Failed to parse ERRNO= field value '%s' in notification message: %m", e); |
| 5433 |
else if (s->status_errno != status_errno) { |
| 5434 |
s->status_errno = status_errno; |
| 5435 |
notify_dbus = true; |
| 5436 |
} |
| 5437 |
} |
| 5438 |
|
| 5439 |
static const struct { |
| 5440 |
const char *tag; |
| 5441 |
size_t status_offset; |
| 5442 |
} status_errors[] = { |
| 5443 |
{ "BUSERROR=", offsetof(Service, status_bus_error) }, |
| 5444 |
{ "VARLINKERROR=", offsetof(Service, status_varlink_error) }, |
| 5445 |
}; |
| 5446 |
|
| 5447 |
FOREACH_ELEMENT(i, status_errors) { |
| 5448 |
e = strv_find_startswith(tags, i->tag); |
| 5449 |
if (!e) |
| 5450 |
continue; |
| 5451 |
|
| 5452 |
char **status_error = (char**) ((uint8_t*) s + i->status_offset); |
| 5453 |
|
| 5454 |
e = empty_to_null(e); |
| 5455 |
|
| 5456 |
if (e && !string_is_safe(e, STRING_ASCII)) { |
| 5457 |
_cleanup_free_ char *escaped = cescape(e); |
| 5458 |
log_unit_warning(u, "Got invalid %s string, ignoring: %s", i->tag, strna(escaped)); |
| 5459 |
} else if (free_and_strdup_warn(status_error, e) > 0) |
| 5460 |
notify_dbus = true; |
| 5461 |
} |
| 5462 |
|
| 5463 |
/* Interpret EXTEND_TIMEOUT= */ |
| 5464 |
e = strv_find_startswith(tags, "EXTEND_TIMEOUT_USEC="); |
| 5465 |
if (e) { |
| 5466 |
usec_t extend_timeout_usec; |
| 5467 |
|
| 5468 |
if (safe_atou64(e, &extend_timeout_usec) < 0) |
| 5469 |
log_unit_warning(u, "Failed to parse EXTEND_TIMEOUT_USEC=%s", e); |
| 5470 |
else |
| 5471 |
service_extend_timeout(s, extend_timeout_usec); |
| 5472 |
} |
| 5473 |
|
| 5474 |
/* Interpret WATCHDOG= */ |
| 5475 |
e = strv_find_startswith(tags, "WATCHDOG="); |
| 5476 |
if (e) { |
| 5477 |
if (streq(e, "1")) |
| 5478 |
service_reset_watchdog(s); |
| 5479 |
else if (streq(e, "trigger")) |
| 5480 |
service_force_watchdog(s); |
| 5481 |
else |
| 5482 |
log_unit_warning(u, "Passed WATCHDOG= field is invalid, ignoring."); |
| 5483 |
} |
| 5484 |
|
| 5485 |
e = strv_find_startswith(tags, "WATCHDOG_USEC="); |
| 5486 |
if (e) { |
| 5487 |
usec_t watchdog_override_usec; |
| 5488 |
if (safe_atou64(e, &watchdog_override_usec) < 0) |
| 5489 |
log_unit_warning(u, "Failed to parse WATCHDOG_USEC=%s", e); |
| 5490 |
else |
| 5491 |
service_override_watchdog_timeout(s, watchdog_override_usec); |
| 5492 |
} |
| 5493 |
|
| 5494 |
/* Interpret RESTART_RESET=1 */ |
| 5495 |
if (strv_contains(tags, "RESTART_RESET=1") && IN_SET(s->state, SERVICE_RUNNING, SERVICE_STOP)) { |
| 5496 |
log_unit_struct(u, LOG_NOTICE, |
| 5497 |
LOG_UNIT_MESSAGE(u, "Got RESTART_RESET=1, resetting restart counter from %u.", s->n_restarts), |
| 5498 |
LOG_ITEM("N_RESTARTS=0"), |
| 5499 |
LOG_UNIT_INVOCATION_ID(u)); |
| 5500 |
|
| 5501 |
s->n_restarts = 0; |
| 5502 |
notify_dbus = true; |
| 5503 |
} |
| 5504 |
|
| 5505 |
/* Process FD store messages. Either FDSTOREREMOVE=1 for removal, or FDSTORE=1 for addition. In both cases, |
| 5506 |
* process FDNAME= for picking the file descriptor name to use. Note that FDNAME= is required when removing |
| 5507 |
* fds, but optional when pushing in new fds, for compatibility reasons. */ |
| 5508 |
if (strv_contains(tags, "FDSTOREREMOVE=1")) { |
| 5509 |
const char *name; |
| 5510 |
|
| 5511 |
name = strv_find_startswith(tags, "FDNAME="); |
| 5512 |
if (!name || !fdname_is_valid(name)) |
| 5513 |
log_unit_warning(u, "FDSTOREREMOVE=1 requested, but no valid file descriptor name passed, ignoring."); |
| 5514 |
else |
| 5515 |
service_remove_fd_store(s, name); |
| 5516 |
|
| 5517 |
} else if (strv_contains(tags, "FDSTORE=1")) { |
| 5518 |
const char *name; |
| 5519 |
|
| 5520 |
name = strv_find_startswith(tags, "FDNAME="); |
| 5521 |
if (name && !fdname_is_valid(name)) { |
| 5522 |
log_unit_warning(u, "Passed FDNAME= name is invalid, ignoring."); |
| 5523 |
name = NULL; |
| 5524 |
} |
| 5525 |
|
| 5526 |
(void) service_add_fd_store_set(s, fds, name, !strv_contains(tags, "FDPOLL=0"), /* propagate_upstream= */ fdstore_detected()); |
| 5527 |
} |
| 5528 |
|
| 5529 |
/* Notify clients about changed status or main pid */ |
| 5530 |
if (notify_dbus) |
| 5531 |
unit_add_to_dbus_queue(u); |
| 5532 |
} |
| 5533 |
|
| 5534 |
static void service_handoff_timestamp( |
| 5535 |
Unit *u, |
| 5536 |
const struct ucred *ucred, |
| 5537 |
const dual_timestamp *ts) { |
| 5538 |
|
| 5539 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 5540 |
|
| 5541 |
assert(ucred); |
| 5542 |
assert(ts); |
| 5543 |
|
| 5544 |
if (s->main_pid.pid == ucred->pid) { |
| 5545 |
if (s->main_command) |
| 5546 |
exec_status_handoff(&s->main_command->exec_status, ucred, ts); |
| 5547 |
|
| 5548 |
exec_status_handoff(&s->main_exec_status, ucred, ts); |
| 5549 |
} else if (s->control_pid.pid == ucred->pid && s->control_command) |
| 5550 |
exec_status_handoff(&s->control_command->exec_status, ucred, ts); |
| 5551 |
else |
| 5552 |
return; |
| 5553 |
|
| 5554 |
unit_add_to_dbus_queue(u); |
| 5555 |
} |
| 5556 |
|
| 5557 |
static void service_notify_pidref(Unit *u, PidRef *parent_pidref, PidRef *child_pidref) { |
| 5558 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 5559 |
int r; |
| 5560 |
|
| 5561 |
assert(pidref_is_set(parent_pidref)); |
| 5562 |
assert(pidref_is_set(child_pidref)); |
| 5563 |
|
| 5564 |
if (pidref_equal(&s->main_pid, parent_pidref)) { |
| 5565 |
r = service_set_main_pidref(s, TAKE_PIDREF(*child_pidref), /* start_timestamp= */ NULL); |
| 5566 |
if (r < 0) |
| 5567 |
return (void) log_unit_warning_errno(u, r, "Failed to set new main pid: %m"); |
| 5568 |
|
| 5569 |
/* Since the child process is PID 1 in a new PID namespace, it must be exclusive to this unit. */ |
| 5570 |
r = unit_watch_pidref(u, &s->main_pid, /* exclusive= */ true); |
| 5571 |
if (r < 0) |
| 5572 |
log_unit_warning_errno(u, r, "Failed to watch new main PID " PID_FMT ": %m", s->main_pid.pid); |
| 5573 |
} else if (pidref_equal(&s->control_pid, parent_pidref)) { |
| 5574 |
service_unwatch_control_pid(s); |
| 5575 |
s->control_pid = TAKE_PIDREF(*child_pidref); |
| 5576 |
|
| 5577 |
r = unit_watch_pidref(u, &s->control_pid, /* exclusive= */ true); |
| 5578 |
if (r < 0) |
| 5579 |
log_unit_warning_errno(u, r, "Failed to watch new control PID " PID_FMT ": %m", s->control_pid.pid); |
| 5580 |
} else |
| 5581 |
return (void) log_unit_debug(u, "Parent process " PID_FMT " does not match main or control processes, ignoring.", parent_pidref->pid); |
| 5582 |
|
| 5583 |
unit_add_to_dbus_queue(u); |
| 5584 |
} |
| 5585 |
|
| 5586 |
static int service_get_timeout(Unit *u, usec_t *timeout) { |
| 5587 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 5588 |
uint64_t t; |
| 5589 |
int r; |
| 5590 |
|
| 5591 |
assert(timeout); |
| 5592 |
|
| 5593 |
if (!s->timer_event_source) |
| 5594 |
return 0; |
| 5595 |
|
| 5596 |
r = sd_event_source_get_time(s->timer_event_source, &t); |
| 5597 |
if (r < 0) |
| 5598 |
return r; |
| 5599 |
if (t == USEC_INFINITY) |
| 5600 |
return 0; |
| 5601 |
|
| 5602 |
*timeout = t; |
| 5603 |
return 1; |
| 5604 |
} |
| 5605 |
|
| 5606 |
static usec_t service_get_timeout_start_usec(Unit *u) { |
| 5607 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 5608 |
return s->timeout_start_usec; |
| 5609 |
} |
| 5610 |
|
| 5611 |
static bool pick_up_pid_from_bus_name(Service *s) { |
| 5612 |
assert(s); |
| 5613 |
|
| 5614 |
/* If the service is running but we have no main PID yet, get it from the owner of the D-Bus name */ |
| 5615 |
|
| 5616 |
return !pidref_is_set(&s->main_pid) && |
| 5617 |
IN_SET(s->state, |
| 5618 |
SERVICE_START, |
| 5619 |
SERVICE_START_POST, |
| 5620 |
SERVICE_RUNNING, |
| 5621 |
SERVICE_REFRESH_EXTENSIONS, |
| 5622 |
SERVICE_REFRESH_CREDENTIALS, |
| 5623 |
SERVICE_RELOAD, |
| 5624 |
SERVICE_RELOAD_SIGNAL, |
| 5625 |
SERVICE_RELOAD_NOTIFY, |
| 5626 |
SERVICE_RELOAD_POST, |
| 5627 |
SERVICE_MOUNTING); |
| 5628 |
} |
| 5629 |
|
| 5630 |
static int bus_name_pid_lookup_callback(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) { |
| 5631 |
Service *s = ASSERT_PTR(SERVICE(userdata)); |
| 5632 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 5633 |
const sd_bus_error *e; |
| 5634 |
uint32_t pid; |
| 5635 |
int r; |
| 5636 |
|
| 5637 |
assert(reply); |
| 5638 |
|
| 5639 |
s->bus_name_pid_lookup_slot = sd_bus_slot_unref(s->bus_name_pid_lookup_slot); |
| 5640 |
|
| 5641 |
if (!s->bus_name || !pick_up_pid_from_bus_name(s)) |
| 5642 |
return 1; |
| 5643 |
|
| 5644 |
e = sd_bus_message_get_error(reply); |
| 5645 |
if (e) { |
| 5646 |
r = sd_bus_error_get_errno(e); |
| 5647 |
log_unit_warning_errno(UNIT(s), r, "GetConnectionUnixProcessID() failed: %s", bus_error_message(e, r)); |
| 5648 |
return 1; |
| 5649 |
} |
| 5650 |
|
| 5651 |
r = sd_bus_message_read(reply, "u", &pid); |
| 5652 |
if (r < 0) { |
| 5653 |
bus_log_parse_error(r); |
| 5654 |
return 1; |
| 5655 |
} |
| 5656 |
|
| 5657 |
r = pidref_set_pid(&pidref, pid); |
| 5658 |
if (r < 0) { |
| 5659 |
log_unit_debug_errno(UNIT(s), r, "GetConnectionUnixProcessID() returned invalid PID: %m"); |
| 5660 |
return 1; |
| 5661 |
} |
| 5662 |
|
| 5663 |
log_unit_debug(UNIT(s), "D-Bus name %s is now owned by process " PID_FMT, s->bus_name, pidref.pid); |
| 5664 |
|
| 5665 |
(void) service_set_main_pidref(s, TAKE_PIDREF(pidref), /* start_timestamp= */ NULL); |
| 5666 |
(void) unit_watch_pidref(UNIT(s), &s->main_pid, /* exclusive= */ false); |
| 5667 |
return 1; |
| 5668 |
} |
| 5669 |
|
| 5670 |
static void service_bus_name_owner_change(Unit *u, const char *new_owner) { |
| 5671 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 5672 |
int r; |
| 5673 |
|
| 5674 |
if (new_owner) |
| 5675 |
log_unit_debug(u, "D-Bus name %s now owned by %s", s->bus_name, new_owner); |
| 5676 |
else |
| 5677 |
log_unit_debug(u, "D-Bus name %s now not owned by anyone.", s->bus_name); |
| 5678 |
|
| 5679 |
s->bus_name_good = new_owner; |
| 5680 |
|
| 5681 |
if (s->type == SERVICE_DBUS) { |
| 5682 |
/* service_enter_running() will figure out what to do */ |
| 5683 |
if (s->state == SERVICE_RUNNING) |
| 5684 |
service_enter_running(s, SERVICE_SUCCESS); |
| 5685 |
else if (s->state == SERVICE_START && new_owner) |
| 5686 |
service_enter_start_post(s); |
| 5687 |
|
| 5688 |
} else if (new_owner && pick_up_pid_from_bus_name(s)) { |
| 5689 |
|
| 5690 |
/* Try to acquire PID from bus service */ |
| 5691 |
|
| 5692 |
s->bus_name_pid_lookup_slot = sd_bus_slot_unref(s->bus_name_pid_lookup_slot); |
| 5693 |
|
| 5694 |
r = sd_bus_call_method_async( |
| 5695 |
u->manager->api_bus, |
| 5696 |
&s->bus_name_pid_lookup_slot, |
| 5697 |
"org.freedesktop.DBus", |
| 5698 |
"/org/freedesktop/DBus", |
| 5699 |
"org.freedesktop.DBus", |
| 5700 |
"GetConnectionUnixProcessID", |
| 5701 |
bus_name_pid_lookup_callback, |
| 5702 |
s, |
| 5703 |
"s", |
| 5704 |
s->bus_name); |
| 5705 |
if (r < 0) |
| 5706 |
log_unit_debug_errno(u, r, "Failed to request owner PID of service name, ignoring: %m"); |
| 5707 |
} |
| 5708 |
} |
| 5709 |
|
| 5710 |
int service_set_socket_fd( |
| 5711 |
Service *s, |
| 5712 |
int fd, |
| 5713 |
Socket *sock, |
| 5714 |
SocketPeer *peer, /* reference to object is donated to us on success */ |
| 5715 |
bool selinux_context_net) { |
| 5716 |
|
| 5717 |
_cleanup_free_ char *peer_text = NULL; |
| 5718 |
int r; |
| 5719 |
|
| 5720 |
assert(s); |
| 5721 |
assert(fd >= 0); |
| 5722 |
assert(sock); |
| 5723 |
|
| 5724 |
/* This is called by the socket code when instantiating a new service for a stream socket and the socket needs |
| 5725 |
* to be configured. We take ownership of the passed fd on success. */ |
| 5726 |
|
| 5727 |
if (UNIT(s)->load_state != UNIT_LOADED) |
| 5728 |
return -EINVAL; |
| 5729 |
|
| 5730 |
if (s->socket_fd >= 0) |
| 5731 |
return -EBUSY; |
| 5732 |
|
| 5733 |
assert(!s->socket_peer); |
| 5734 |
|
| 5735 |
if (!IN_SET(s->state, SERVICE_DEAD, SERVICE_DEAD_RESOURCES_PINNED)) |
| 5736 |
return -EAGAIN; |
| 5737 |
|
| 5738 |
if (getpeername_pretty(fd, true, &peer_text) >= 0) { |
| 5739 |
|
| 5740 |
if (UNIT(s)->description) { |
| 5741 |
_cleanup_free_ char *a = NULL; |
| 5742 |
|
| 5743 |
a = strjoin(UNIT(s)->description, " (", peer_text, ")"); |
| 5744 |
if (!a) |
| 5745 |
return -ENOMEM; |
| 5746 |
|
| 5747 |
r = unit_set_description(UNIT(s), a); |
| 5748 |
} else |
| 5749 |
r = unit_set_description(UNIT(s), peer_text); |
| 5750 |
if (r < 0) |
| 5751 |
return r; |
| 5752 |
} |
| 5753 |
|
| 5754 |
r = unit_add_two_dependencies(UNIT(s), UNIT_AFTER, UNIT_TRIGGERED_BY, UNIT(sock), false, UNIT_DEPENDENCY_IMPLICIT); |
| 5755 |
if (r < 0) |
| 5756 |
return log_unit_debug_errno(UNIT(s), r, |
| 5757 |
"Failed to add After=/TriggeredBy= dependencies on socket unit: %m"); |
| 5758 |
|
| 5759 |
s->socket_fd = fd; |
| 5760 |
s->socket_peer = peer; |
| 5761 |
s->socket_fd_selinux_context_net = selinux_context_net; |
| 5762 |
|
| 5763 |
unit_ref_set(&s->accept_socket, UNIT(s), UNIT(sock)); |
| 5764 |
return 0; |
| 5765 |
} |
| 5766 |
|
| 5767 |
static void service_reset_failed(Unit *u) { |
| 5768 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 5769 |
|
| 5770 |
if (s->state == SERVICE_FAILED) |
| 5771 |
service_set_state(s, service_determine_dead_state(s)); |
| 5772 |
|
| 5773 |
s->result = SERVICE_SUCCESS; |
| 5774 |
s->reload_result = SERVICE_SUCCESS; |
| 5775 |
s->live_mount_result = SERVICE_SUCCESS; |
| 5776 |
s->clean_result = SERVICE_SUCCESS; |
| 5777 |
s->n_restarts = 0; |
| 5778 |
} |
| 5779 |
|
| 5780 |
static PidRef* service_main_pid(Unit *u, bool *ret_is_alien) { |
| 5781 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 5782 |
|
| 5783 |
if (ret_is_alien) |
| 5784 |
*ret_is_alien = s->main_pid_alien; |
| 5785 |
|
| 5786 |
return &s->main_pid; |
| 5787 |
} |
| 5788 |
|
| 5789 |
static PidRef* service_control_pid(Unit *u) { |
| 5790 |
return &ASSERT_PTR(SERVICE(u))->control_pid; |
| 5791 |
} |
| 5792 |
|
| 5793 |
static bool service_needs_console(Unit *u) { |
| 5794 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 5795 |
|
| 5796 |
/* We provide our own implementation of this here, instead of relying of the generic implementation |
| 5797 |
* unit_needs_console() provides, since we want to return false if we are in SERVICE_EXITED state. */ |
| 5798 |
|
| 5799 |
if (!exec_context_may_touch_console(&s->exec_context)) |
| 5800 |
return false; |
| 5801 |
|
| 5802 |
return IN_SET(s->state, |
| 5803 |
SERVICE_CONDITION, |
| 5804 |
SERVICE_START_PRE, |
| 5805 |
SERVICE_START, |
| 5806 |
SERVICE_START_POST, |
| 5807 |
SERVICE_RUNNING, |
| 5808 |
SERVICE_REFRESH_EXTENSIONS, |
| 5809 |
SERVICE_REFRESH_CREDENTIALS, |
| 5810 |
SERVICE_RELOAD, |
| 5811 |
SERVICE_RELOAD_SIGNAL, |
| 5812 |
SERVICE_RELOAD_NOTIFY, |
| 5813 |
SERVICE_RELOAD_POST, |
| 5814 |
SERVICE_MOUNTING, |
| 5815 |
SERVICE_STOP, |
| 5816 |
SERVICE_STOP_WATCHDOG, |
| 5817 |
SERVICE_STOP_SIGTERM, |
| 5818 |
SERVICE_STOP_SIGKILL, |
| 5819 |
SERVICE_STOP_POST, |
| 5820 |
SERVICE_FINAL_WATCHDOG, |
| 5821 |
SERVICE_FINAL_SIGTERM, |
| 5822 |
SERVICE_FINAL_SIGKILL); |
| 5823 |
} |
| 5824 |
|
| 5825 |
static int service_exit_status(Unit *u) { |
| 5826 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 5827 |
|
| 5828 |
if (s->main_exec_status.pid <= 0 || |
| 5829 |
!dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) |
| 5830 |
return -ENODATA; |
| 5831 |
|
| 5832 |
if (s->main_exec_status.code != CLD_EXITED) |
| 5833 |
return -EBADE; |
| 5834 |
|
| 5835 |
return s->main_exec_status.status; |
| 5836 |
} |
| 5837 |
|
| 5838 |
static const char* service_status_text(Unit *u) { |
| 5839 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 5840 |
|
| 5841 |
return s->status_text; |
| 5842 |
} |
| 5843 |
|
| 5844 |
static int service_clean(Unit *u, ExecCleanMask mask) { |
| 5845 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 5846 |
_cleanup_strv_free_ char **l = NULL; |
| 5847 |
bool may_clean_fdstore = false; |
| 5848 |
int r; |
| 5849 |
|
| 5850 |
assert(mask != 0); |
| 5851 |
|
| 5852 |
if (!IN_SET(s->state, SERVICE_DEAD, SERVICE_DEAD_RESOURCES_PINNED)) |
| 5853 |
return -EBUSY; |
| 5854 |
|
| 5855 |
/* Determine if there's anything we could potentially clean */ |
| 5856 |
r = exec_context_get_clean_directories(&s->exec_context, u->manager->prefix, mask, &l); |
| 5857 |
if (r < 0) |
| 5858 |
return r; |
| 5859 |
|
| 5860 |
if (mask & EXEC_CLEAN_FDSTORE) |
| 5861 |
may_clean_fdstore = s->n_fd_store > 0 || s->n_fd_store_max > 0; |
| 5862 |
|
| 5863 |
if (strv_isempty(l) && !may_clean_fdstore) |
| 5864 |
return -EUNATCH; /* Nothing to potentially clean */ |
| 5865 |
|
| 5866 |
/* Let's clean the stuff we can clean quickly */ |
| 5867 |
if (may_clean_fdstore) |
| 5868 |
service_release_fd_store(s); |
| 5869 |
|
| 5870 |
/* If we are done, leave quickly */ |
| 5871 |
if (strv_isempty(l)) { |
| 5872 |
if (s->state == SERVICE_DEAD_RESOURCES_PINNED && !SERVICE_FD_STORE_POPULATED(s)) |
| 5873 |
service_set_state(s, SERVICE_DEAD); |
| 5874 |
return 0; |
| 5875 |
} |
| 5876 |
|
| 5877 |
/* We need to clean disk stuff. This is slow, hence do it out of process, and change state */ |
| 5878 |
service_unwatch_control_pid(s); |
| 5879 |
s->clean_result = SERVICE_SUCCESS; |
| 5880 |
s->control_command = NULL; |
| 5881 |
s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
| 5882 |
|
| 5883 |
r = service_arm_timer(s, /* relative= */ true, s->exec_context.timeout_clean_usec); |
| 5884 |
if (r < 0) { |
| 5885 |
log_unit_warning_errno(u, r, "Failed to install timer: %m"); |
| 5886 |
goto fail; |
| 5887 |
} |
| 5888 |
|
| 5889 |
unit_cgroup_disable_all_controllers(u); |
| 5890 |
r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid); |
| 5891 |
if (r < 0) { |
| 5892 |
log_unit_warning_errno(u, r, "Failed to spawn cleaning task: %m"); |
| 5893 |
goto fail; |
| 5894 |
} |
| 5895 |
|
| 5896 |
service_set_state(s, SERVICE_CLEANING); |
| 5897 |
return 0; |
| 5898 |
|
| 5899 |
fail: |
| 5900 |
s->clean_result = SERVICE_FAILURE_RESOURCES; |
| 5901 |
s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
| 5902 |
return r; |
| 5903 |
} |
| 5904 |
|
| 5905 |
static int service_can_clean(Unit *u, ExecCleanMask *ret) { |
| 5906 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 5907 |
ExecCleanMask mask = 0; |
| 5908 |
int r; |
| 5909 |
|
| 5910 |
assert(ret); |
| 5911 |
|
| 5912 |
r = exec_context_get_clean_mask(&s->exec_context, &mask); |
| 5913 |
if (r < 0) |
| 5914 |
return r; |
| 5915 |
|
| 5916 |
if (s->n_fd_store_max > 0) |
| 5917 |
mask |= EXEC_CLEAN_FDSTORE; |
| 5918 |
|
| 5919 |
*ret = mask; |
| 5920 |
return 0; |
| 5921 |
} |
| 5922 |
|
| 5923 |
static int service_live_mount( |
| 5924 |
Unit *u, |
| 5925 |
const char *src, |
| 5926 |
const char *dst, |
| 5927 |
sd_bus_message *message, |
| 5928 |
MountInNamespaceFlags flags, |
| 5929 |
const MountOptions *options, |
| 5930 |
sd_bus_error *reterr_error) { |
| 5931 |
|
| 5932 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 5933 |
_cleanup_(pidref_done) PidRef worker = PIDREF_NULL; |
| 5934 |
int r; |
| 5935 |
|
| 5936 |
assert(u); |
| 5937 |
assert(u->manager); |
| 5938 |
assert(src); |
| 5939 |
assert(dst); |
| 5940 |
assert(message); |
| 5941 |
assert(!s->mount_request); |
| 5942 |
|
| 5943 |
if (s->state != SERVICE_RUNNING || !pidref_is_set(&s->main_pid)) { |
| 5944 |
log_unit_warning(u, "Service is not running, cannot live mount."); |
| 5945 |
return sd_bus_error_setf( |
| 5946 |
reterr_error, |
| 5947 |
BUS_ERROR_UNIT_INACTIVE, |
| 5948 |
"Live mounting '%s' on '%s' for unit '%s' cannot be scheduled: service not running", |
| 5949 |
src, |
| 5950 |
dst, |
| 5951 |
u->id); |
| 5952 |
} |
| 5953 |
|
| 5954 |
if (mount_point_is_credentials(u->manager->prefix[EXEC_DIRECTORY_RUNTIME], dst)) { |
| 5955 |
log_unit_warning(u, "Refusing to live mount over credential mount '%s'.", dst); |
| 5956 |
return sd_bus_error_setf( |
| 5957 |
reterr_error, |
| 5958 |
SD_BUS_ERROR_INVALID_ARGS, |
| 5959 |
"Live mounting '%s' on '%s' for unit '%s' cannot be scheduled: cannot mount over credential mount", |
| 5960 |
src, |
| 5961 |
dst, |
| 5962 |
u->id); |
| 5963 |
} |
| 5964 |
|
| 5965 |
if (path_startswith_strv(dst, s->exec_context.inaccessible_paths)) { |
| 5966 |
log_unit_warning(u, "%s is not accessible to this unit, cannot live mount.", dst); |
| 5967 |
return sd_bus_error_setf( |
| 5968 |
reterr_error, |
| 5969 |
SD_BUS_ERROR_INVALID_ARGS, |
| 5970 |
"Live mounting '%s' on '%s' for unit '%s' cannot be scheduled: destination is not accessible to this unit", |
| 5971 |
src, |
| 5972 |
dst, |
| 5973 |
u->id); |
| 5974 |
} |
| 5975 |
|
| 5976 |
service_unwatch_control_pid(s); |
| 5977 |
s->live_mount_result = SERVICE_SUCCESS; |
| 5978 |
s->control_command = NULL; |
| 5979 |
s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
| 5980 |
|
| 5981 |
r = service_arm_timer(s, /* relative= */ true, s->timeout_start_usec); |
| 5982 |
if (r < 0) { |
| 5983 |
log_unit_error_errno(u, r, "Failed to install timer: %m"); |
| 5984 |
sd_bus_error_set_errnof(reterr_error, r, |
| 5985 |
"Live mounting '%s' on '%s' for unit '%s': failed to install timer: %m", |
| 5986 |
src, dst, u->id); |
| 5987 |
goto fail; |
| 5988 |
} |
| 5989 |
|
| 5990 |
const char *propagate_directory = strjoina("/run/systemd/propagate/", u->id); |
| 5991 |
|
| 5992 |
/* Given we are running from PID1, avoid doing potentially heavy I/O operations like opening images |
| 5993 |
* directly, and instead fork a worker process. We record the D-Bus message, so that we can reply |
| 5994 |
* after the operation has finished. This way callers can wait on the message and know that the new |
| 5995 |
* resource is available (or the operation failed) once they receive the response. */ |
| 5996 |
r = unit_fork_helper_process_full(u, "(sd-mount-in-ns)", /* into_cgroup= */ false, |
| 5997 |
FORK_ALLOW_DLOPEN, |
| 5998 |
&worker); |
| 5999 |
if (r < 0) { |
| 6000 |
log_unit_error_errno(u, r, |
| 6001 |
"Failed to fork process to mount '%s' on '%s' in unit's namespace: %m", |
| 6002 |
src, dst); |
| 6003 |
sd_bus_error_set_errnof(reterr_error, r, |
| 6004 |
"Live mounting '%s' on '%s' for unit '%s': failed to fork off helper process into namespace: %m", |
| 6005 |
src, dst, u->id); |
| 6006 |
goto fail; |
| 6007 |
} |
| 6008 |
if (r == 0) { |
| 6009 |
LOG_CONTEXT_PUSH_UNIT(u); |
| 6010 |
|
| 6011 |
if (flags & MOUNT_IN_NAMESPACE_IS_IMAGE) |
| 6012 |
r = mount_image_in_namespace( |
| 6013 |
&s->main_pid, |
| 6014 |
propagate_directory, |
| 6015 |
"/run/systemd/incoming/", |
| 6016 |
src, dst, |
| 6017 |
flags, |
| 6018 |
options, |
| 6019 |
s->exec_context.mount_image_policy ?: &image_policy_service); |
| 6020 |
else |
| 6021 |
r = bind_mount_in_namespace( |
| 6022 |
&s->main_pid, |
| 6023 |
propagate_directory, |
| 6024 |
"/run/systemd/incoming/", |
| 6025 |
src, dst, |
| 6026 |
flags); |
| 6027 |
if (r < 0) |
| 6028 |
log_unit_error_errno(u, r, |
| 6029 |
"Failed to mount '%s' on '%s' in unit's namespace: %m", |
| 6030 |
src, dst); |
| 6031 |
else |
| 6032 |
log_unit_debug(u, "Mounted '%s' on '%s' in unit's namespace", src, dst); |
| 6033 |
|
| 6034 |
_exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
| 6035 |
} |
| 6036 |
|
| 6037 |
r = unit_watch_pidref(u, &worker, /* exclusive= */ true); |
| 6038 |
if (r < 0) { |
| 6039 |
log_unit_warning_errno(u, r, "Failed to watch live mount helper process: %m"); |
| 6040 |
sd_bus_error_set_errnof(reterr_error, r, |
| 6041 |
"Live mounting '%s' on '%s' for unit '%s': failed to watch live mount helper process: %m", |
| 6042 |
src, dst, u->id); |
| 6043 |
goto fail; |
| 6044 |
} |
| 6045 |
|
| 6046 |
s->mount_request = sd_bus_message_ref(message); |
| 6047 |
s->control_pid = TAKE_PIDREF(worker); |
| 6048 |
service_set_state(s, SERVICE_MOUNTING); |
| 6049 |
return 0; |
| 6050 |
|
| 6051 |
fail: |
| 6052 |
s->live_mount_result = SERVICE_FAILURE_RESOURCES; |
| 6053 |
service_enter_running(s, SERVICE_SUCCESS); |
| 6054 |
return r; |
| 6055 |
} |
| 6056 |
|
| 6057 |
static int service_can_live_mount(Unit *u, sd_bus_error *reterr_error) { |
| 6058 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 6059 |
|
| 6060 |
/* Ensure that the unit runs in a private mount namespace */ |
| 6061 |
if (!exec_needs_mount_namespace(&s->exec_context, /* params= */ NULL)) |
| 6062 |
return sd_bus_error_setf( |
| 6063 |
reterr_error, |
| 6064 |
SD_BUS_ERROR_INVALID_ARGS, |
| 6065 |
"Unit '%s' not running in private mount namespace, cannot live mount.", |
| 6066 |
u->id); |
| 6067 |
|
| 6068 |
return 0; |
| 6069 |
} |
| 6070 |
|
| 6071 |
static const char* service_finished_job(Unit *u, JobType t, JobResult result) { |
| 6072 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 6073 |
|
| 6074 |
if (t == JOB_START && |
| 6075 |
result == JOB_DONE && |
| 6076 |
s->type == SERVICE_ONESHOT) |
| 6077 |
return "Finished %s."; |
| 6078 |
|
| 6079 |
/* Fall back to generic */ |
| 6080 |
return NULL; |
| 6081 |
} |
| 6082 |
|
| 6083 |
static int service_test_startable(Unit *u) { |
| 6084 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 6085 |
int r; |
| 6086 |
|
| 6087 |
/* First check the state, and do not increment start limit counter if the service cannot start due to |
| 6088 |
* that e.g. it is already being started. Note, the service states mapped to UNIT_ACTIVE, |
| 6089 |
* UNIT_RELOADING, UNIT_DEACTIVATING, UNIT_MAINTENANCE, and UNIT_REFRESHING are already filtered in |
| 6090 |
* unit_start(). Hence, here we only need to check states that mapped to UNIT_ACTIVATING. */ |
| 6091 |
if (IN_SET(s->state, SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST)) |
| 6092 |
return false; |
| 6093 |
|
| 6094 |
/* Make sure we don't enter a busy loop of some kind. */ |
| 6095 |
r = unit_test_start_limit(u); |
| 6096 |
if (r < 0) { |
| 6097 |
service_enter_dead(s, SERVICE_FAILURE_START_LIMIT_HIT, false); |
| 6098 |
return r; |
| 6099 |
} |
| 6100 |
|
| 6101 |
return true; |
| 6102 |
} |
| 6103 |
|
| 6104 |
static void service_release_resources(Unit *u) { |
| 6105 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 6106 |
|
| 6107 |
/* Invoked by the unit state engine, whenever it realizes that unit is dead and there's no job |
| 6108 |
* anymore for it, and it hence is a good idea to release resources */ |
| 6109 |
|
| 6110 |
/* Don't release resources if this is a transitionary failed/dead state |
| 6111 |
* (i.e. SERVICE_DEAD_BEFORE_AUTO_RESTART/SERVICE_FAILED_BEFORE_AUTO_RESTART), insist on a permanent |
| 6112 |
* failure state. */ |
| 6113 |
if (!IN_SET(s->state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_DEAD_RESOURCES_PINNED)) |
| 6114 |
return; |
| 6115 |
|
| 6116 |
log_unit_debug(u, "Releasing resources..."); |
| 6117 |
|
| 6118 |
service_release_socket_fd(s); |
| 6119 |
service_release_stdio_fd(s); |
| 6120 |
service_release_extra_fds(s); |
| 6121 |
s->root_directory_fd = asynchronous_close(s->root_directory_fd); |
| 6122 |
|
| 6123 |
if (IN_SET(s->fd_store_preserve_mode, EXEC_PRESERVE_NO, EXEC_PRESERVE_RESTART) || |
| 6124 |
(s->fd_store_preserve_mode == EXEC_PRESERVE_ON_SUCCESS && s->state == SERVICE_FAILED)) |
| 6125 |
service_release_fd_store(s); |
| 6126 |
|
| 6127 |
if (s->state == SERVICE_DEAD_RESOURCES_PINNED && !SERVICE_FD_STORE_POPULATED(s)) |
| 6128 |
service_set_state(s, SERVICE_DEAD); |
| 6129 |
} |
| 6130 |
|
| 6131 |
int service_determine_exec_selinux_label(Service *s, char **ret) { |
| 6132 |
int r; |
| 6133 |
|
| 6134 |
assert(s); |
| 6135 |
assert(ret); |
| 6136 |
|
| 6137 |
if (!mac_selinux_use()) |
| 6138 |
return -ENODATA; |
| 6139 |
|
| 6140 |
/* Returns the SELinux label used for execution of the main service binary */ |
| 6141 |
|
| 6142 |
if (s->exec_context.selinux_context) |
| 6143 |
/* Prefer the explicitly configured label if there is one */ |
| 6144 |
return strdup_to(ret, s->exec_context.selinux_context); |
| 6145 |
|
| 6146 |
if (s->exec_context.root_image || |
| 6147 |
s->exec_context.n_extension_images > 0 || |
| 6148 |
!strv_isempty(s->exec_context.extension_directories) || |
| 6149 |
s->exec_context.root_mstack) /* We cannot chase paths through images */ |
| 6150 |
return log_unit_debug_errno(UNIT(s), SYNTHETIC_ERRNO(ENODATA), "Service with RootImage=, ExtensionImages=, ExtensionDirectories= or RootMStack= set cannot determine socket SELinux label before activation, ignoring."); |
| 6151 |
|
| 6152 |
ExecCommand *c = s->exec_command[SERVICE_EXEC_START]; |
| 6153 |
if (!c) |
| 6154 |
return -ENODATA; |
| 6155 |
|
| 6156 |
_cleanup_free_ char *path = NULL; |
| 6157 |
if (s->exec_context.root_directory_as_fd) |
| 6158 |
r = chaseat(s->root_directory_fd, s->root_directory_fd, c->path, CHASE_TRIGGER_AUTOFS, &path, NULL); |
| 6159 |
else |
| 6160 |
r = chase(c->path, s->exec_context.root_directory, CHASE_PREFIX_ROOT|CHASE_TRIGGER_AUTOFS, &path, NULL); |
| 6161 |
if (r < 0) { |
| 6162 |
log_unit_debug_errno(UNIT(s), r, "Failed to resolve service binary '%s', ignoring.", c->path); |
| 6163 |
return -ENODATA; |
| 6164 |
} |
| 6165 |
|
| 6166 |
r = mac_selinux_get_create_label_from_exe(path, ret); |
| 6167 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) { |
| 6168 |
log_unit_debug_errno(UNIT(s), r, "Reading SELinux label off binary '%s' is not supported, ignoring.", path); |
| 6169 |
return -ENODATA; |
| 6170 |
} |
| 6171 |
if (ERRNO_IS_NEG_PRIVILEGE(r)) { |
| 6172 |
log_unit_debug_errno(UNIT(s), r, "Can't read SELinux label off binary '%s', due to privileges, ignoring.", path); |
| 6173 |
return -ENODATA; |
| 6174 |
} |
| 6175 |
if (r < 0) |
| 6176 |
return log_unit_debug_errno(UNIT(s), r, "Failed to read SELinux label off binary '%s': %m", path); |
| 6177 |
|
| 6178 |
return 0; |
| 6179 |
} |
| 6180 |
|
| 6181 |
static int service_freezer_action(Unit *u, FreezerAction action) { |
| 6182 |
Service *s = ASSERT_PTR(SERVICE(u)); |
| 6183 |
FreezerState old_objective, new_objective; |
| 6184 |
int r; |
| 6185 |
|
| 6186 |
old_objective = freezer_state_objective(u->freezer_state); |
| 6187 |
|
| 6188 |
r = unit_cgroup_freezer_action(u, action); |
| 6189 |
if (r < 0) |
| 6190 |
return r; |
| 6191 |
|
| 6192 |
new_objective = freezer_state_objective(u->freezer_state); |
| 6193 |
|
| 6194 |
/* Note that we cannot trivially check the retval of unit_cgroup_freezer_action() here, since |
| 6195 |
* that signals whether the operation is ongoing from *kernel's PoV*. If the freeze operation |
| 6196 |
* is aborted, the frozen attribute of the cgroup would never have been flipped in kernel, |
| 6197 |
* and unit_cgroup_freezer_action() will happily return 0, yet the watchdog still needs to be reset; |
| 6198 |
* vice versa. */ |
| 6199 |
|
| 6200 |
if (old_objective != new_objective) { |
| 6201 |
if (new_objective == FREEZER_FROZEN) |
| 6202 |
service_stop_watchdog(s); |
| 6203 |
else if (new_objective == FREEZER_RUNNING) |
| 6204 |
service_reset_watchdog(s); |
| 6205 |
else |
| 6206 |
assert_not_reached(); |
| 6207 |
} |
| 6208 |
|
| 6209 |
return r; |
| 6210 |
} |
| 6211 |
|
| 6212 |
static const char* const service_restart_table[_SERVICE_RESTART_MAX] = { |
| 6213 |
[SERVICE_RESTART_NO] = "no", |
| 6214 |
[SERVICE_RESTART_ON_SUCCESS] = "on-success", |
| 6215 |
[SERVICE_RESTART_ON_FAILURE] = "on-failure", |
| 6216 |
[SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal", |
| 6217 |
[SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog", |
| 6218 |
[SERVICE_RESTART_ON_ABORT] = "on-abort", |
| 6219 |
[SERVICE_RESTART_ALWAYS] = "always", |
| 6220 |
}; |
| 6221 |
|
| 6222 |
DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart); |
| 6223 |
|
| 6224 |
static const char* const service_restart_mode_table[_SERVICE_RESTART_MODE_MAX] = { |
| 6225 |
[SERVICE_RESTART_MODE_NORMAL] = "normal", |
| 6226 |
[SERVICE_RESTART_MODE_DIRECT] = "direct", |
| 6227 |
[SERVICE_RESTART_MODE_DEBUG] = "debug", |
| 6228 |
}; |
| 6229 |
|
| 6230 |
DEFINE_STRING_TABLE_LOOKUP(service_restart_mode, ServiceRestartMode); |
| 6231 |
|
| 6232 |
static const char* const service_type_table[_SERVICE_TYPE_MAX] = { |
| 6233 |
[SERVICE_SIMPLE] = "simple", |
| 6234 |
[SERVICE_FORKING] = "forking", |
| 6235 |
[SERVICE_ONESHOT] = "oneshot", |
| 6236 |
[SERVICE_DBUS] = "dbus", |
| 6237 |
[SERVICE_NOTIFY] = "notify", |
| 6238 |
[SERVICE_NOTIFY_RELOAD] = "notify-reload", |
| 6239 |
[SERVICE_IDLE] = "idle", |
| 6240 |
[SERVICE_EXEC] = "exec", |
| 6241 |
}; |
| 6242 |
|
| 6243 |
DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType); |
| 6244 |
|
| 6245 |
static const char* const service_exit_type_table[_SERVICE_EXIT_TYPE_MAX] = { |
| 6246 |
[SERVICE_EXIT_MAIN] = "main", |
| 6247 |
[SERVICE_EXIT_CGROUP] = "cgroup", |
| 6248 |
}; |
| 6249 |
|
| 6250 |
DEFINE_STRING_TABLE_LOOKUP(service_exit_type, ServiceExitType); |
| 6251 |
|
| 6252 |
static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = { |
| 6253 |
[SERVICE_EXEC_CONDITION] = "ExecCondition", |
| 6254 |
[SERVICE_EXEC_START_PRE] = "ExecStartPre", |
| 6255 |
[SERVICE_EXEC_START] = "ExecStart", |
| 6256 |
[SERVICE_EXEC_START_POST] = "ExecStartPost", |
| 6257 |
[SERVICE_EXEC_RELOAD] = "ExecReload", |
| 6258 |
[SERVICE_EXEC_RELOAD_POST] = "ExecReloadPost", |
| 6259 |
[SERVICE_EXEC_STOP] = "ExecStop", |
| 6260 |
[SERVICE_EXEC_STOP_POST] = "ExecStopPost", |
| 6261 |
}; |
| 6262 |
|
| 6263 |
DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand); |
| 6264 |
|
| 6265 |
static const char* const service_exec_ex_command_table[_SERVICE_EXEC_COMMAND_MAX] = { |
| 6266 |
[SERVICE_EXEC_CONDITION] = "ExecConditionEx", |
| 6267 |
[SERVICE_EXEC_START_PRE] = "ExecStartPreEx", |
| 6268 |
[SERVICE_EXEC_START] = "ExecStartEx", |
| 6269 |
[SERVICE_EXEC_START_POST] = "ExecStartPostEx", |
| 6270 |
[SERVICE_EXEC_RELOAD] = "ExecReloadEx", |
| 6271 |
[SERVICE_EXEC_RELOAD_POST] = "ExecReloadPostEx", |
| 6272 |
[SERVICE_EXEC_STOP] = "ExecStopEx", |
| 6273 |
[SERVICE_EXEC_STOP_POST] = "ExecStopPostEx", |
| 6274 |
}; |
| 6275 |
|
| 6276 |
DEFINE_STRING_TABLE_LOOKUP(service_exec_ex_command, ServiceExecCommand); |
| 6277 |
|
| 6278 |
static const char* const notify_state_table[_NOTIFY_STATE_MAX] = { |
| 6279 |
[NOTIFY_READY] = "ready", |
| 6280 |
[NOTIFY_RELOADING] = "reloading", |
| 6281 |
[NOTIFY_RELOAD_READY] = "reload-ready", |
| 6282 |
[NOTIFY_STOPPING] = "stopping", |
| 6283 |
}; |
| 6284 |
|
| 6285 |
DEFINE_STRING_TABLE_LOOKUP(notify_state, NotifyState); |
| 6286 |
|
| 6287 |
static const char* const service_result_table[_SERVICE_RESULT_MAX] = { |
| 6288 |
[SERVICE_SUCCESS] = "success", |
| 6289 |
[SERVICE_FAILURE_RESOURCES] = "resources", |
| 6290 |
[SERVICE_FAILURE_PROTOCOL] = "protocol", |
| 6291 |
[SERVICE_FAILURE_TIMEOUT] = "timeout", |
| 6292 |
[SERVICE_FAILURE_EXIT_CODE] = "exit-code", |
| 6293 |
[SERVICE_FAILURE_SIGNAL] = "signal", |
| 6294 |
[SERVICE_FAILURE_CORE_DUMP] = "core-dump", |
| 6295 |
[SERVICE_FAILURE_WATCHDOG] = "watchdog", |
| 6296 |
[SERVICE_FAILURE_START_LIMIT_HIT] = "start-limit-hit", |
| 6297 |
[SERVICE_FAILURE_OOM_KILL] = "oom-kill", |
| 6298 |
[SERVICE_SKIP_CONDITION] = "exec-condition", |
| 6299 |
}; |
| 6300 |
|
| 6301 |
DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult); |
| 6302 |
|
| 6303 |
static const char* const service_timeout_failure_mode_table[_SERVICE_TIMEOUT_FAILURE_MODE_MAX] = { |
| 6304 |
[SERVICE_TIMEOUT_TERMINATE] = "terminate", |
| 6305 |
[SERVICE_TIMEOUT_ABORT] = "abort", |
| 6306 |
[SERVICE_TIMEOUT_KILL] = "kill", |
| 6307 |
}; |
| 6308 |
|
| 6309 |
DEFINE_STRING_TABLE_LOOKUP(service_timeout_failure_mode, ServiceTimeoutFailureMode); |
| 6310 |
|
| 6311 |
static const struct { |
| 6312 |
ServiceRefreshOnReload flag; |
| 6313 |
const char *name; |
| 6314 |
} service_refresh_on_reload_table[] = { |
| 6315 |
{ SERVICE_RELOAD_EXTENSIONS, "extensions" }, |
| 6316 |
{ SERVICE_RELOAD_CREDENTIALS, "credentials" }, |
| 6317 |
}; |
| 6318 |
|
| 6319 |
ServiceRefreshOnReload service_refresh_on_reload_flag_from_string(const char *s) { |
| 6320 |
assert(s); |
| 6321 |
|
| 6322 |
FOREACH_ELEMENT(i, service_refresh_on_reload_table) |
| 6323 |
if (streq(s, i->name)) |
| 6324 |
return i->flag; |
| 6325 |
|
| 6326 |
return _SERVICE_REFRESH_ON_RELOAD_INVALID; |
| 6327 |
} |
| 6328 |
|
| 6329 |
int service_refresh_on_reload_from_string_many(const char *s, ServiceRefreshOnReload *ret) { |
| 6330 |
ServiceRefreshOnReload flags = 0; |
| 6331 |
int r; |
| 6332 |
|
| 6333 |
assert(s); |
| 6334 |
assert(ret); |
| 6335 |
|
| 6336 |
for (;;) { |
| 6337 |
_cleanup_free_ char *v = NULL; |
| 6338 |
ServiceRefreshOnReload f; |
| 6339 |
|
| 6340 |
r = extract_first_word(&s, &v, NULL, 0); |
| 6341 |
if (r < 0) |
| 6342 |
return r; |
| 6343 |
if (r == 0) |
| 6344 |
break; |
| 6345 |
|
| 6346 |
f = service_refresh_on_reload_flag_from_string(v); |
| 6347 |
if (f < 0) |
| 6348 |
return f; |
| 6349 |
assert(f > 0); |
| 6350 |
|
| 6351 |
flags |= f; |
| 6352 |
} |
| 6353 |
|
| 6354 |
*ret = flags; |
| 6355 |
return 0; |
| 6356 |
} |
| 6357 |
|
| 6358 |
int service_refresh_on_reload_to_strv(ServiceRefreshOnReload flags, char ***ret) { |
| 6359 |
_cleanup_strv_free_ char **l = NULL; |
| 6360 |
int r; |
| 6361 |
|
| 6362 |
assert(flags >= 0); |
| 6363 |
assert(ret); |
| 6364 |
|
| 6365 |
FOREACH_ELEMENT(i, service_refresh_on_reload_table) { |
| 6366 |
if (!FLAGS_SET(flags, i->flag)) |
| 6367 |
continue; |
| 6368 |
|
| 6369 |
r = strv_extend(&l, i->name); |
| 6370 |
if (r < 0) |
| 6371 |
return r; |
| 6372 |
} |
| 6373 |
|
| 6374 |
*ret = TAKE_PTR(l); |
| 6375 |
return 0; |
| 6376 |
} |
| 6377 |
|
| 6378 |
const UnitVTable service_vtable = { |
| 6379 |
.object_size = sizeof(Service), |
| 6380 |
.exec_context_offset = offsetof(Service, exec_context), |
| 6381 |
.cgroup_context_offset = offsetof(Service, cgroup_context), |
| 6382 |
.kill_context_offset = offsetof(Service, kill_context), |
| 6383 |
.exec_runtime_offset = offsetof(Service, exec_runtime), |
| 6384 |
.cgroup_runtime_offset = offsetof(Service, cgroup_runtime), |
| 6385 |
|
| 6386 |
.sections = |
| 6387 |
"Unit\0" |
| 6388 |
"Service\0" |
| 6389 |
"Install\0", |
| 6390 |
.private_section = "Service", |
| 6391 |
|
| 6392 |
.can_transient = true, |
| 6393 |
.can_delegate = true, |
| 6394 |
.can_fail = true, |
| 6395 |
.can_set_managed_oom = true, |
| 6396 |
.notify_plymouth = true, |
| 6397 |
.track_orphaned = true, |
| 6398 |
|
| 6399 |
.init = service_init, |
| 6400 |
.done = service_done, |
| 6401 |
.load = service_load, |
| 6402 |
.release_resources = service_release_resources, |
| 6403 |
|
| 6404 |
.coldplug = service_coldplug, |
| 6405 |
|
| 6406 |
.dump = service_dump, |
| 6407 |
|
| 6408 |
.start = service_start, |
| 6409 |
.stop = service_stop, |
| 6410 |
.reload = service_reload, |
| 6411 |
|
| 6412 |
.can_reload = service_can_reload, |
| 6413 |
|
| 6414 |
.clean = service_clean, |
| 6415 |
.can_clean = service_can_clean, |
| 6416 |
|
| 6417 |
.live_mount = service_live_mount, |
| 6418 |
.can_live_mount = service_can_live_mount, |
| 6419 |
|
| 6420 |
.freezer_action = service_freezer_action, |
| 6421 |
|
| 6422 |
.serialize = service_serialize, |
| 6423 |
.deserialize_item = service_deserialize_item, |
| 6424 |
|
| 6425 |
.attach_external_fd_to_fdstore = service_attach_external_fd_to_fdstore, |
| 6426 |
|
| 6427 |
.active_state = service_active_state, |
| 6428 |
.sub_state_to_string = service_sub_state_to_string, |
| 6429 |
|
| 6430 |
.will_restart = service_will_restart, |
| 6431 |
|
| 6432 |
.may_gc = service_may_gc, |
| 6433 |
|
| 6434 |
.sigchld_event = service_sigchld_event, |
| 6435 |
|
| 6436 |
.reset_failed = service_reset_failed, |
| 6437 |
|
| 6438 |
.notify_cgroup_empty = service_notify_cgroup_empty_event, |
| 6439 |
.notify_cgroup_oom = service_notify_cgroup_oom_event, |
| 6440 |
.notify_message = service_notify_message, |
| 6441 |
.notify_handoff_timestamp = service_handoff_timestamp, |
| 6442 |
.notify_pidref = service_notify_pidref, |
| 6443 |
|
| 6444 |
.main_pid = service_main_pid, |
| 6445 |
.control_pid = service_control_pid, |
| 6446 |
|
| 6447 |
.bus_name_owner_change = service_bus_name_owner_change, |
| 6448 |
|
| 6449 |
.bus_set_property = bus_service_set_property, |
| 6450 |
.bus_commit_properties = bus_service_commit_properties, |
| 6451 |
|
| 6452 |
.get_timeout = service_get_timeout, |
| 6453 |
.get_timeout_start_usec = service_get_timeout_start_usec, |
| 6454 |
.needs_console = service_needs_console, |
| 6455 |
.exit_status = service_exit_status, |
| 6456 |
.status_text = service_status_text, |
| 6457 |
|
| 6458 |
.status_message_formats = { |
| 6459 |
.finished_job = service_finished_job, |
| 6460 |
}, |
| 6461 |
|
| 6462 |
.test_startable = service_test_startable, |
| 6463 |
|
| 6464 |
.audit_start_message_type = AUDIT_SERVICE_START, |
| 6465 |
.audit_stop_message_type = AUDIT_SERVICE_STOP, |
| 6466 |
}; |