HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/core/service.c

280,768 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 5c097c139d401c81662897faf4f6e6779f5302c7

Lines 5001–6000 of 6,768

                                        service_enter_start_pre(s);
                                else
                                        service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
                                break;
 
                        case SERVICE_START_PRE:
                                if (f == SERVICE_SUCCESS)
                                        service_enter_start(s);
                                else
                                        service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
                                break;
 
                        case SERVICE_START:
                                if (s->type != SERVICE_FORKING)
                                        /* Maybe spurious event due to a reload that changed the type? */
                                        break;
 
                                if (f != SERVICE_SUCCESS) {
                                        service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
                                        break;
                                }
 
                                if (s->pid_file) {
                                        bool has_start_post;
 
                                        /* Let's try to load the pid file here if we can.
                                         * The PID file might actually be created by a START_POST
                                         * script. In that case don't worry if the loading fails. */
 
                                        has_start_post = s->exec_command[SERVICE_EXEC_START_POST];
                                        r = service_load_pid_file(s, !has_start_post);
                                        if (!has_start_post && r < 0) {
                                                r = service_demand_pid_file(s);
                                                if (r < 0 || cgroup_good(s) == 0)
                                                        service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_PROTOCOL);
                                                break;
                                        }
                                } else
                                        service_search_main_pid(s);
 
                                service_enter_start_post(s);
                                break;
 
                        case SERVICE_START_POST:
                                if (f != SERVICE_SUCCESS) {
                                        service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
                                        break;
                                }
 
                                if (s->pid_file) {
                                        r = service_load_pid_file(s, true);
                                        if (r < 0) {
                                                r = service_demand_pid_file(s);
                                                if (r < 0 || cgroup_good(s) == 0)
                                                        service_enter_stop(s, SERVICE_FAILURE_PROTOCOL);
                                                break;
                                        }
                                } else
                                        service_search_main_pid(s);
 
                                service_enter_running(s, SERVICE_SUCCESS);
                                break;
 
                        case SERVICE_REFRESH_EXTENSIONS:
                                if (f == SERVICE_SUCCESS) {
                                        s->refreshed_mask |= SERVICE_RELOAD_EXTENSIONS;
                                        service_enter_refresh_credentials(s);
                                } else
                                        service_reload_finish(s, f);
                                break;
 
                        case SERVICE_REFRESH_CREDENTIALS:
                                if (f == SERVICE_SUCCESS ||
                                    (f == SERVICE_FAILURE_EXIT_CODE && IN_SET(status, EXIT_NOTINSTALLED, EXIT_NOPERMISSION))) {
 
                                        /* Refreshing asynchronously done, proceed to reload */
                                        s->refreshed_mask |= f == SERVICE_SUCCESS ? SERVICE_RELOAD_CREDENTIALS : 0;
                                        service_enter_reload(s);
                                } else
                                        service_reload_finish(s, f);
                                break;
 
                        case SERVICE_RELOAD:
                                if (f != SERVICE_SUCCESS) {
                                        service_reload_finish(s, f);
                                        break;
                                }
 
                                if (service_load_pid_file(s, true) < 0)
                                        service_search_main_pid(s);
 
                                service_enter_reload_signal(s);
                                break;
 
                        case SERVICE_RELOAD_POST:
                                service_reload_finish(s, f);
                                break;
 
                        case SERVICE_MOUNTING:
                                service_live_mount_finish(s, f, SD_BUS_ERROR_FAILED);
 
                                service_enter_running(s, SERVICE_SUCCESS);
                                break;
 
                        case SERVICE_STOP:
                                service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
                                break;
 
                        case SERVICE_STOP_WATCHDOG:
                        case SERVICE_STOP_SIGTERM:
                        case SERVICE_STOP_SIGKILL:
                                if (main_pid_good(s) <= 0)
                                        service_enter_stop_post(s, f);
 
                                /* If there is still a service process around, wait until
                                 * that one quit, too */
                                break;
 
                        case SERVICE_STOP_POST:
                                if (main_pid_good(s) <= 0)
                                        service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
                                break;
 
                        case SERVICE_FINAL_WATCHDOG:
                        case SERVICE_FINAL_SIGTERM:
                        case SERVICE_FINAL_SIGKILL:
                                if (main_pid_good(s) <= 0)
                                        service_enter_dead(s, f, true);
                                break;
 
                        case SERVICE_CLEANING:
 
                                if (s->clean_result == SERVICE_SUCCESS)
                                        s->clean_result = f;
 
                                service_enter_dead(s, SERVICE_SUCCESS, false);
                                break;
 
                        default:
                                assert_not_reached();
                        }
                }
        } else /* Neither control nor main PID? If so, don't notify about anything */
                notify_dbus = false;
 
        /* Notify clients about changed exit status */
        if (notify_dbus)
                unit_add_to_dbus_queue(u);
}
 
static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
        Service *s = ASSERT_PTR(SERVICE(userdata));
 
        assert(source == s->timer_event_source);
 
        switch (s->state) {
 
        case SERVICE_CONDITION:
        case SERVICE_START_PRE:
        case SERVICE_START:
        case SERVICE_START_POST:
                switch (s->timeout_start_failure_mode) {
 
                case SERVICE_TIMEOUT_TERMINATE:
                        log_unit_warning(UNIT(s), "%s operation timed out. Terminating.", service_state_to_string(s->state));
                        service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
                        break;
 
                case SERVICE_TIMEOUT_ABORT:
                        log_unit_warning(UNIT(s), "%s operation timed out. Aborting.", service_state_to_string(s->state));
                        service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_TIMEOUT);
                        break;
 
                case SERVICE_TIMEOUT_KILL:
                        if (s->kill_context.send_sigkill) {
                                log_unit_warning(UNIT(s), "%s operation timed out. Killing.", service_state_to_string(s->state));
                                service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
                        } else {
                                log_unit_warning(UNIT(s), "%s operation timed out. Skipping SIGKILL.", service_state_to_string(s->state));
                                service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
                        }
                        break;
 
                default:
                        assert_not_reached();
                }
                break;
 
        case SERVICE_RUNNING:
                log_unit_warning(UNIT(s), "Service reached runtime time limit. Stopping.");
                service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
                break;
 
        case SERVICE_REFRESH_EXTENSIONS:
        case SERVICE_REFRESH_CREDENTIALS:
        case SERVICE_RELOAD:
        case SERVICE_RELOAD_SIGNAL:
        case SERVICE_RELOAD_NOTIFY:
        case SERVICE_RELOAD_POST:
                log_unit_warning(UNIT(s), "Reload operation timed out. Killing reload process.");
                service_kill_control_process(s);
                service_reload_finish(s, SERVICE_FAILURE_TIMEOUT);
                break;
 
        case SERVICE_MOUNTING:
                log_unit_warning(UNIT(s), "Mount operation timed out. Killing mount process.");
                service_kill_control_process(s);
                service_live_mount_finish(s, SERVICE_FAILURE_TIMEOUT, SD_BUS_ERROR_TIMEOUT);
                service_enter_running(s, SERVICE_SUCCESS);
                break;
 
        case SERVICE_STOP:
                switch (s->timeout_stop_failure_mode) {
 
                case SERVICE_TIMEOUT_TERMINATE:
                        log_unit_warning(UNIT(s), "Stopping timed out. Terminating.");
                        service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
                        break;
 
                case SERVICE_TIMEOUT_ABORT:
                        log_unit_warning(UNIT(s), "Stopping timed out. Aborting.");
                        service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_TIMEOUT);
                        break;
 
                case SERVICE_TIMEOUT_KILL:
                        if (s->kill_context.send_sigkill) {
                                log_unit_warning(UNIT(s), "Stopping timed out. Killing.");
                                service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
                        } else {
                                log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL.");
                                service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
                        }
                        break;
 
                default:
                        assert_not_reached();
                }
                break;
 
        case SERVICE_STOP_WATCHDOG:
                if (s->kill_context.send_sigkill) {
                        log_unit_warning(UNIT(s), "State 'stop-watchdog' timed out. Killing.");
                        service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
                } else {
                        log_unit_warning(UNIT(s), "State 'stop-watchdog' timed out. Skipping SIGKILL.");
                        service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
                }
                break;
 
        case SERVICE_STOP_SIGTERM:
                if (s->timeout_stop_failure_mode == SERVICE_TIMEOUT_ABORT) {
                        log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Aborting.");
                        service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_TIMEOUT);
                } else if (s->kill_context.send_sigkill) {
                        log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Killing.");
                        service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
                } else {
                        log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
                        service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
                }
 
                break;
 
        case SERVICE_STOP_SIGKILL:
                /* Uh, we sent a SIGKILL and it is still not gone?
                 * Must be something we cannot kill, so let's just be
                 * weirded out and continue */
 
                log_unit_warning(UNIT(s), "Processes still around after SIGKILL. Ignoring.");
                service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
                break;
 
        case SERVICE_STOP_POST:
                switch (s->timeout_stop_failure_mode) {
 
                case SERVICE_TIMEOUT_TERMINATE:
                        log_unit_warning(UNIT(s), "State 'stop-post' timed out. Terminating.");
                        service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
                        break;
 
                case SERVICE_TIMEOUT_ABORT:
                        log_unit_warning(UNIT(s), "State 'stop-post' timed out. Aborting.");
                        service_enter_signal(s, SERVICE_FINAL_WATCHDOG, SERVICE_FAILURE_TIMEOUT);
                        break;
 
                case SERVICE_TIMEOUT_KILL:
                        if (s->kill_context.send_sigkill) {
                                log_unit_warning(UNIT(s), "State 'stop-post' timed out. Killing.");
                                service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
                        } else {
                                log_unit_warning(UNIT(s), "State 'stop-post' timed out. Skipping SIGKILL. Entering failed mode.");
                                service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
                        }
                        break;
 
                default:
                        assert_not_reached();
                }
                break;
 
        case SERVICE_FINAL_WATCHDOG:
                if (s->kill_context.send_sigkill) {
                        log_unit_warning(UNIT(s), "State 'final-watchdog' timed out. Killing.");
                        service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
                } else {
                        log_unit_warning(UNIT(s), "State 'final-watchdog' timed out. Skipping SIGKILL. Entering failed mode.");
                        service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
                }
                break;
 
        case SERVICE_FINAL_SIGTERM:
                if (s->timeout_stop_failure_mode == SERVICE_TIMEOUT_ABORT) {
                        log_unit_warning(UNIT(s), "State 'final-sigterm' timed out. Aborting.");
                        service_enter_signal(s, SERVICE_FINAL_WATCHDOG, SERVICE_FAILURE_TIMEOUT);
                } else if (s->kill_context.send_sigkill) {
                        log_unit_warning(UNIT(s), "State 'final-sigterm' timed out. Killing.");
                        service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
                } else {
                        log_unit_warning(UNIT(s), "State 'final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
                        service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
                }
 
                break;
 
        case SERVICE_FINAL_SIGKILL:
                log_unit_warning(UNIT(s), "Processes still around after final SIGKILL. Entering failed mode.");
                service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
                break;
 
        case SERVICE_AUTO_RESTART:
                if (s->restart_usec > 0)
                        log_unit_debug(UNIT(s),
                                       "Service restart interval %s expired, scheduling restart.",
                                       FORMAT_TIMESPAN(service_restart_usec_next(s), USEC_PER_SEC));
                else
                        log_unit_debug(UNIT(s),
                                       "Service has no hold-off time (RestartSec=0), scheduling restart.");
 
                service_enter_restart(s, /* shortcut= */ false);
                break;
 
        case SERVICE_CLEANING:
                log_unit_warning(UNIT(s), "Cleaning timed out. killing.");
 
                if (s->clean_result == SERVICE_SUCCESS)
                        s->clean_result = SERVICE_FAILURE_TIMEOUT;
 
                service_enter_signal(s, SERVICE_FINAL_SIGKILL, 0);
                break;
 
        default:
                assert_not_reached();
        }
 
        return 0;
}
 
static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) {
        Service *s = ASSERT_PTR(SERVICE(userdata));
        usec_t watchdog_usec;
 
        assert(source == s->watchdog_event_source);
 
        watchdog_usec = service_get_watchdog_usec(s);
 
        if (UNIT(s)->manager->service_watchdogs) {
                log_unit_error(UNIT(s), "Watchdog timeout (limit %s)!",
                               FORMAT_TIMESPAN(watchdog_usec, 1));
 
                service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_WATCHDOG);
        } else
                log_unit_warning(UNIT(s), "Watchdog disabled! Ignoring watchdog timeout (limit %s)!",
                                 FORMAT_TIMESPAN(watchdog_usec, 1));
 
        return 0;
}
 
static void service_force_watchdog(Service *s) {
        assert(s);
 
        if (!UNIT(s)->manager->service_watchdogs)
                return;
 
        log_unit_error(UNIT(s), "Watchdog request (last status: %s)!",
                       s->status_text ?: "<unset>");
 
        service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_WATCHDOG);
}
 
static bool service_notify_message_authorized(Service *s, PidRef *pid) {
        assert(s);
        assert(pidref_is_set(pid));
 
        switch (service_get_notify_access(s)) {
 
        case NOTIFY_NONE:
                /* Warn level only if no notifications are expected */
                log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception is disabled", pid->pid);
                return false;
 
        case NOTIFY_ALL:
                return true;
 
        case NOTIFY_MAIN:
                if (pidref_equal(pid, &s->main_pid))
                        return true;
 
                if (pidref_is_set(&s->main_pid))
                        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);
                else
                        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);
 
                return false;
 
        case NOTIFY_EXEC:
                if (pidref_equal(pid, &s->main_pid) || pidref_equal(pid, &s->control_pid))
                        return true;
 
                if (pidref_is_set(&s->main_pid) && pidref_is_set(&s->control_pid))
                        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,
                                       pid->pid, s->main_pid.pid, s->control_pid.pid);
                else if (pidref_is_set(&s->main_pid))
                        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);
                else if (pidref_is_set(&s->control_pid))
                        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);
                else
                        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);
 
                return false;
 
        default:
                assert_not_reached();
        }
}
 
static int service_notify_message_parse_new_pid(
                Unit *u,
                char * const *tags,
                FDSet *fds,
                PidRef *ret) {
 
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
        const char *e;
        int r;
 
        assert(u);
        assert(ret);
 
        /* MAINPIDFD=1 always takes precedence */
        if (strv_contains(tags, "MAINPIDFD=1")) {
                unsigned n_fds = fdset_size(fds);
                if (n_fds != 1)
                        return log_unit_warning_errno(u, SYNTHETIC_ERRNO(EINVAL),
                                                      "Got MAINPIDFD=1 with %s fd, ignoring.", n_fds == 0 ? "no" : "more than one");
 
                r = pidref_set_pidfd_consume(&pidref, ASSERT_FD(fdset_steal_first(fds)));
                if (r < 0)
                        return log_unit_warning_errno(u, r, "Failed to create reference to received new main pidfd: %m");
 
                goto finish;
        }
 
        e = strv_find_startswith(tags, "MAINPID=");
        if (!e) {
                *ret = PIDREF_NULL;
                return 0;
        }
 
        r = pidref_set_pidstr(&pidref, e);
        if (r < 0)
                return log_unit_warning_errno(u, r, "Failed to parse MAINPID=%s field in notification message, ignoring: %m", e);
 
        e = strv_find_startswith(tags, "MAINPIDFDID=");
        if (!e)
                goto finish;
 
        uint64_t pidfd_id;
 
        r = safe_atou64(e, &pidfd_id);
        if (r < 0)
                return log_unit_warning_errno(u, r, "Failed to parse MAINPIDFDID= in notification message, refusing: %s", e);
 
        r = pidref_acquire_pidfd_id(&pidref);
        if (r < 0) {
                if (!ERRNO_IS_NEG_NOT_SUPPORTED(r))
                        log_unit_warning_errno(u, r,
                                               "Failed to acquire pidfd id of process " PID_FMT ", not validating MAINPIDFDID=%" PRIu64 ": %m",
                                               pidref.pid, pidfd_id);
                goto finish;
        }
 
        if (pidref.fd_id != pidfd_id)
                return log_unit_warning_errno(u, SYNTHETIC_ERRNO(ESRCH),
                                              "PIDFD ID of process " PID_FMT " (%" PRIu64 ") mismatches with received MAINPIDFDID=%" PRIu64 ", not changing main PID.",
                                              pidref.pid, pidref.fd_id, pidfd_id);
 
finish:
        *ret = TAKE_PIDREF(pidref);
        return 1;
}
 
static void service_notify_message_process_state(Service *s, char * const *tags) {
        usec_t monotonic_usec = USEC_INFINITY;
        int r;
 
        assert(s);
 
        const char *e = strv_find_startswith(tags, "MONOTONIC_USEC=");
        if (e) {
                r = safe_atou64(e, &monotonic_usec);
                if (r < 0)
                        log_unit_warning_errno(UNIT(s), r, "Failed to parse MONOTONIC_USEC= field in notification message, ignoring: %s", e);
        }
 
        /* Interpret READY=/STOPPING=/RELOADING=. STOPPING= wins over the others, and READY= over RELOADING= */
        if (strv_contains(tags, "STOPPING=1")) {
                s->notify_state = NOTIFY_STOPPING;
 
                if (IN_SET(s->state, SERVICE_RUNNING, SERVICE_RUNNING_REVALIDATING,
                                     SERVICE_REFRESH_EXTENSIONS, SERVICE_REFRESH_CREDENTIALS,
                                     SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY))
                        service_enter_stop_by_notify(s);
 
                return;
        }
 
        if (strv_contains(tags, "READY=1")) {
                if (s->notify_state == NOTIFY_STOPPING) {
                        log_unit_error(UNIT(s),
                                       "Service must stop after STOPPING=1 notification, refusing attempted transition to READY.");
                        return;
                }
 
                if (s->type == SERVICE_NOTIFY_RELOAD && s->state == SERVICE_START) {
                        r = service_check_reload_signal_handler(s, ", refusing service startup", ", ignoring");
                        if (r == -EOPNOTSUPP) {
                                service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_PROTOCOL);
                                return;
                        }
                }
 
                if (s->notify_state == NOTIFY_RELOADING)
                        s->notify_state = NOTIFY_RELOAD_READY;
                else
                        s->notify_state = NOTIFY_READY;
 
                /* Combined RELOADING=1 and READY=1? Then this is indication that the service started and
                 * immediately finished reloading. */
                if (strv_contains(tags, "RELOADING=1")) {
                        if (s->state == SERVICE_RELOAD_SIGNAL &&
                            monotonic_usec != USEC_INFINITY &&
                            monotonic_usec >= s->reload_begin_usec)
                                /* Valid Type=notify-reload protocol? Then we're all good. */
                                service_enter_reload_post(s);
 
                        else if (IN_SET(s->state, SERVICE_RUNNING, SERVICE_RUNNING_REVALIDATING)) {
                                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
 
                                /* Propagate a reload explicitly for plain RELOADING=1 (semantically equivalent to
                                 * service_enter_reload_by_notify() call in below) */
                                r = manager_propagate_reload(UNIT(s)->manager, UNIT(s), JOB_FAIL, &error);
                                if (r < 0)
                                        log_unit_warning(UNIT(s), "Failed to schedule propagation of reload, ignoring: %s",
                                                         bus_error_message(&error, r));
                        }
                }
 
                /* Type=notify(-reload) services inform us about completed initialization with READY=1 */
                if (IN_SET(s->type, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD) &&
                    s->state == SERVICE_START)
                        service_enter_start_post(s);
 
                /* Sending READY=1 while we are reloading informs us that the reloading is complete. */
                if (s->state == SERVICE_RELOAD_NOTIFY)
                        service_enter_reload_post(s);
 
        } else if (strv_contains(tags, "RELOADING=1")) {
                if (s->notify_state == NOTIFY_STOPPING) {
                        log_unit_error(UNIT(s),
                                       "Service must stop after STOPPING=1 notification, refusing attempted transition to RELOADING.");
                        return;
                }
 
                s->notify_state = NOTIFY_RELOADING;
 
                /* Sending RELOADING=1 after we send SIGHUP to request a reload will transition
                 * things to "reload-notify" state, where we'll wait for READY=1 to let us know the
                 * reload is done. Note that we insist on a timestamp being sent along here, so that
                 * we know for sure this is a reload cycle initiated *after* we sent the signal */
                if (s->state == SERVICE_RELOAD_SIGNAL &&
                    monotonic_usec != USEC_INFINITY &&
                    monotonic_usec >= s->reload_begin_usec)
                        /* Note, we don't call service_enter_reload_by_notify() here, because we
                         * don't need reload propagation nor do we want to restart the timeout. */
                        service_set_state(s, SERVICE_RELOAD_NOTIFY);
 
                /* The reload will still be preserved by service_enter_running() replaying it once the bus
                 * name returns. */
                if (s->state == SERVICE_RUNNING)
                        service_enter_reload_by_notify(s);
        }
}
 
static void service_notify_message(
                Unit *u,
                PidRef *pidref,
                const struct ucred *ucred,
                char * const *tags,
                FDSet *fds) {
 
        Service *s = ASSERT_PTR(SERVICE(u));
        int r;
 
        assert(pidref_is_set(pidref));
        assert(ucred);
 
        if (!service_notify_message_authorized(s, pidref))
                return;
 
        if (DEBUG_LOGGING) {
                _cleanup_free_ char *cc = strv_join(tags, ", ");
                log_unit_debug(u, "Got notification message from PID "PID_FMT": %s", pidref->pid, empty_to_na(cc));
        }
 
        bool notify_dbus = false;
        const char *e;
 
        /* Interpret MAINPID= (+ MAINPIDFDID=) / MAINPIDFD=1 */
        _cleanup_(pidref_done) PidRef new_main_pid = PIDREF_NULL;
 
        r = service_notify_message_parse_new_pid(u, tags, fds, &new_main_pid);
        if (r > 0 &&
            IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RUNNING_REVALIDATING,
                             SERVICE_REFRESH_EXTENSIONS, SERVICE_REFRESH_CREDENTIALS,
                             SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, SERVICE_RELOAD_POST,
                             SERVICE_STOP, SERVICE_STOP_SIGTERM) &&
            (!s->main_pid_known || !pidref_equal(&new_main_pid, &s->main_pid))) {
 
                r = service_is_suitable_main_pid(s, &new_main_pid, LOG_WARNING);
                if (r == 0) {
                        /* The new main PID is a bit suspicious, which is OK if the sender is privileged. */
 
                        if (ucred->uid == 0) {
                                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);
                                r = 1;
                        } else
                                log_unit_warning(u, "New main PID "PID_FMT" does not belong to service, refusing.", new_main_pid.pid);
                }
                if (r > 0) {
                        (void) service_set_main_pidref(s, TAKE_PIDREF(new_main_pid), /* start_timestamp= */ NULL);
 
                        r = unit_watch_pidref(UNIT(s), &s->main_pid, /* exclusive= */ false);
                        if (r < 0)
                                log_unit_warning_errno(UNIT(s), r, "Failed to watch new main PID "PID_FMT" for service: %m", s->main_pid.pid);
 
                        notify_dbus = true;
                }
        }
 
        service_notify_message_process_state(s, tags);
 
        /* Interpret STATUS= */
        e = strv_find_startswith(tags, "STATUS=");
        if (e) {
                _cleanup_free_ char *t = NULL;
 
                if (!isempty(e)) {
                        /* Note that this size limit check is mostly paranoia: since the datagram size we are willing
                         * to process is already limited to NOTIFY_BUFFER_MAX, this limit here should never be hit. */
                        if (strlen(e) > STATUS_TEXT_MAX)
                                log_unit_warning(u, "Status message overly long (%zu > %u), ignoring.", strlen(e), STATUS_TEXT_MAX);
                        else if (!utf8_is_valid(e))
                                log_unit_warning(u, "Status message in notification message is not UTF-8 clean, ignoring.");
                        else {
                                t = strdup(e);
                                if (!t)
                                        log_oom_warning();
                        }
                }
 
                if (!streq_ptr(s->status_text, t)) {
                        free_and_replace(s->status_text, t);
                        notify_dbus = true;
                }
        }
 
        /* Interpret NOTIFYACCESS= */
        e = strv_find_startswith(tags, "NOTIFYACCESS=");
        if (e) {
                NotifyAccess notify_access;
 
                notify_access = notify_access_from_string(e);
                if (notify_access < 0)
                        log_unit_warning_errno(u, notify_access,
                                               "Failed to parse NOTIFYACCESS= field value '%s' in notification message, ignoring: %m", e);
 
                /* We don't need to check whether the new access mode is more strict than what is
                 * already in use, since only the privileged process is allowed to change it
                 * in the first place. */
                if (service_get_notify_access(s) != notify_access) {
                        service_override_notify_access(s, notify_access);
                        notify_dbus = true;
                }
        }
 
        /* Interpret ERRNO= */
        e = strv_find_startswith(tags, "ERRNO=");
        if (e) {
                int status_errno;
 
                status_errno = parse_errno(e);
                if (status_errno < 0)
                        log_unit_warning_errno(u, status_errno,
                                               "Failed to parse ERRNO= field value '%s' in notification message: %m", e);
                else if (s->status_errno != status_errno) {
                        s->status_errno = status_errno;
                        notify_dbus = true;
                }
        }
 
        static const struct {
                const char *tag;
                size_t status_offset;
        } status_errors[] = {
                { "BUSERROR=",     offsetof(Service, status_bus_error)     },
                { "VARLINKERROR=", offsetof(Service, status_varlink_error) },
        };
 
        FOREACH_ELEMENT(i, status_errors) {
                e = strv_find_startswith(tags, i->tag);
                if (!e)
                        continue;
 
                char **status_error = (char**) ((uint8_t*) s + i->status_offset);
 
                e = empty_to_null(e);
 
                if (e && !string_is_safe(e, STRING_ASCII)) {
                        _cleanup_free_ char *escaped = cescape(e);
                        log_unit_warning(u, "Got invalid %s string, ignoring: %s", i->tag, strna(escaped));
                } else if (free_and_strdup_warn(status_error, e) > 0)
                        notify_dbus = true;
        }
 
        /* Interpret EXTEND_TIMEOUT= */
        e = strv_find_startswith(tags, "EXTEND_TIMEOUT_USEC=");
        if (e) {
                usec_t extend_timeout_usec;
 
                if (safe_atou64(e, &extend_timeout_usec) < 0)
                        log_unit_warning(u, "Failed to parse EXTEND_TIMEOUT_USEC=%s", e);
                else
                        service_extend_timeout(s, extend_timeout_usec);
        }
 
        /* Interpret WATCHDOG= */
        e = strv_find_startswith(tags, "WATCHDOG=");
        if (e) {
                if (streq(e, "1"))
                        service_reset_watchdog(s);
                else if (streq(e, "trigger"))
                        service_force_watchdog(s);
                else
                        log_unit_warning(u, "Passed WATCHDOG= field is invalid, ignoring.");
        }
 
        e = strv_find_startswith(tags, "WATCHDOG_USEC=");
        if (e) {
                usec_t watchdog_override_usec;
                if (safe_atou64(e, &watchdog_override_usec) < 0)
                        log_unit_warning(u, "Failed to parse WATCHDOG_USEC=%s", e);
                else
                        service_override_watchdog_timeout(s, watchdog_override_usec);
        }
 
        /* Interpret RESTART_RESET=1 */
        if (strv_contains(tags, "RESTART_RESET=1") && IN_SET(s->state, SERVICE_RUNNING, SERVICE_RUNNING_REVALIDATING, SERVICE_STOP)) {
                log_unit_struct(u, LOG_NOTICE,
                                LOG_UNIT_MESSAGE(u, "Got RESTART_RESET=1, resetting restart counter from %u.", s->n_restarts),
                                LOG_ITEM("N_RESTARTS=0"),
                                LOG_UNIT_INVOCATION_ID(u));
 
                s->n_restarts = 0;
                notify_dbus = true;
        }
 
        /* Process FD store messages. Either FDSTOREREMOVE=1 for removal, or FDSTORE=1 for addition. In both cases,
         * process FDNAME= for picking the file descriptor name to use. Note that FDNAME= is required when removing
         * fds, but optional when pushing in new fds, for compatibility reasons. */
        if (strv_contains(tags, "FDSTOREREMOVE=1")) {
                const char *name;
 
                name = strv_find_startswith(tags, "FDNAME=");
                if (!name || !fdname_is_valid(name))
                        log_unit_warning(u, "FDSTOREREMOVE=1 requested, but no valid file descriptor name passed, ignoring.");
                else
                        service_remove_fd_store(s, name);
 
        } else if (strv_contains(tags, "FDSTORE=1")) {
                const char *name;
 
                name = strv_find_startswith(tags, "FDNAME=");
                if (name && !fdname_is_valid(name)) {
                        log_unit_warning(u, "Passed FDNAME= name is invalid, ignoring.");
                        name = NULL;
                }
 
                (void) service_add_fd_store_set(s, fds, name, !strv_contains(tags, "FDPOLL=0"), /* propagate_upstream= */ fdstore_detected());
        }
 
        /* Notify clients about changed status or main pid */
        if (notify_dbus)
                unit_add_to_dbus_queue(u);
}
 
static void service_handoff_timestamp(
                Unit *u,
                const struct ucred *ucred,
                const dual_timestamp *ts) {
 
        Service *s = ASSERT_PTR(SERVICE(u));
 
        assert(ucred);
        assert(ts);
 
        if (s->main_pid.pid == ucred->pid) {
                if (s->main_command)
                        exec_status_handoff(&s->main_command->exec_status, ucred, ts);
 
                exec_status_handoff(&s->main_exec_status, ucred, ts);
        } else if (s->control_pid.pid == ucred->pid && s->control_command)
                exec_status_handoff(&s->control_command->exec_status, ucred, ts);
        else
                return;
 
        unit_add_to_dbus_queue(u);
}
 
static void service_notify_pidref(Unit *u, PidRef *parent_pidref, PidRef *child_pidref) {
        Service *s = ASSERT_PTR(SERVICE(u));
        int r;
 
        assert(pidref_is_set(parent_pidref));
        assert(pidref_is_set(child_pidref));
 
        if (pidref_equal(&s->main_pid, parent_pidref)) {
                r = service_set_main_pidref(s, TAKE_PIDREF(*child_pidref), /* start_timestamp= */ NULL);
                if (r < 0)
                        return (void) log_unit_warning_errno(u, r, "Failed to set new main pid: %m");
 
                /* Since the child process is PID 1 in a new PID namespace, it must be exclusive to this unit. */
                r = unit_watch_pidref(u, &s->main_pid, /* exclusive= */ true);
                if (r < 0)
                        log_unit_warning_errno(u, r, "Failed to watch new main PID " PID_FMT ": %m", s->main_pid.pid);
        } else if (pidref_equal(&s->control_pid, parent_pidref)) {
                service_unwatch_control_pid(s);
                s->control_pid = TAKE_PIDREF(*child_pidref);
 
                r = unit_watch_pidref(u, &s->control_pid, /* exclusive= */ true);
                if (r < 0)
                        log_unit_warning_errno(u, r, "Failed to watch new control PID " PID_FMT ": %m", s->control_pid.pid);
        } else
                return (void) log_unit_debug(u, "Parent process " PID_FMT " does not match main or control processes, ignoring.", parent_pidref->pid);
 
        unit_add_to_dbus_queue(u);
}
 
static int service_get_timeout(Unit *u, usec_t *timeout) {
        Service *s = ASSERT_PTR(SERVICE(u));
        uint64_t t;
        int r;
 
        assert(timeout);
 
        if (!s->timer_event_source)
                return 0;
 
        r = sd_event_source_get_time(s->timer_event_source, &t);
        if (r < 0)
                return r;
        if (t == USEC_INFINITY)
                return 0;
 
        *timeout = t;
        return 1;
}
 
static usec_t service_get_timeout_start_usec(Unit *u) {
        Service *s = ASSERT_PTR(SERVICE(u));
        return s->timeout_start_usec;
}
 
static bool pick_up_pid_from_bus_name(Service *s) {
        assert(s);
 
        /* If the service is running but we have no main PID yet, get it from the owner of the D-Bus name */
 
        return !pidref_is_set(&s->main_pid) &&
                IN_SET(s->state,
                       SERVICE_START,
                       SERVICE_START_POST,
                       SERVICE_RUNNING,
                       SERVICE_REFRESH_EXTENSIONS,
                       SERVICE_REFRESH_CREDENTIALS,
                       SERVICE_RELOAD,
                       SERVICE_RELOAD_SIGNAL,
                       SERVICE_RELOAD_NOTIFY,
                       SERVICE_RELOAD_POST,
                       SERVICE_MOUNTING);
}
 
static int bus_name_pid_lookup_callback(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) {
        Service *s = ASSERT_PTR(SERVICE(userdata));
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
        const sd_bus_error *e;
        uint32_t pid;
        int r;
 
        assert(reply);
 
        s->bus_name_pid_lookup_slot = sd_bus_slot_unref(s->bus_name_pid_lookup_slot);
 
        if (!s->bus_name || !pick_up_pid_from_bus_name(s))
                return 1;
 
        e = sd_bus_message_get_error(reply);
        if (e) {
                r = sd_bus_error_get_errno(e);
                log_unit_warning_errno(UNIT(s), r, "GetConnectionUnixProcessID() failed: %s", bus_error_message(e, r));
                return 1;
        }
 
        r = sd_bus_message_read(reply, "u", &pid);
        if (r < 0) {
                bus_log_parse_error(r);
                return 1;
        }
 
        r = pidref_set_pid(&pidref, pid);
        if (r < 0) {
                log_unit_debug_errno(UNIT(s), r, "GetConnectionUnixProcessID() returned invalid PID: %m");
                return 1;
        }
 
        log_unit_debug(UNIT(s), "D-Bus name %s is now owned by process " PID_FMT, s->bus_name, pidref.pid);
 
        (void) service_set_main_pidref(s, TAKE_PIDREF(pidref), /* start_timestamp= */ NULL);
        (void) unit_watch_pidref(UNIT(s), &s->main_pid, /* exclusive= */ false);
        return 1;
}
 
static int service_dispatch_bus_name_grace(sd_event_source *source, usec_t usec, void *userdata) {
        Service *s = ASSERT_PTR(SERVICE(userdata));
 
        assert(source == s->bus_name_grace_event_source);
        assert(s->state == SERVICE_RUNNING_REVALIDATING);
 
        log_unit_warning(UNIT(s), "D-Bus name %s still not owned after grace period, giving up.", s->bus_name);
        service_enter_exited_or_stop(s);
 
        return 0;
}
 
static void service_bus_name_owner_change(Unit *u, const char *new_owner, bool from_signal) {
        Service *s = ASSERT_PTR(SERVICE(u));
        int r;
 
        if (new_owner)
                log_unit_debug(u, "D-Bus name %s now owned by %s", s->bus_name, new_owner);
        else
                log_unit_debug(u, "D-Bus name %s now not owned by anyone.", s->bus_name);
 
        s->bus_name_good = new_owner;
 
        if (s->type == SERVICE_DBUS) {
                if (IN_SET(s->state, SERVICE_RUNNING, SERVICE_RUNNING_REVALIDATING)) {
                        if (!new_owner && from_signal)
                                service_enter_exited_or_stop(s);
                        else
                                service_enter_running(s, SERVICE_SUCCESS);
                } else if (s->state == SERVICE_START && new_owner)
                        service_enter_start_post(s);
 
        } else if (new_owner && pick_up_pid_from_bus_name(s)) {
 
                /* Try to acquire PID from bus service */
 
                s->bus_name_pid_lookup_slot = sd_bus_slot_unref(s->bus_name_pid_lookup_slot);
 
                r = sd_bus_call_method_async(
                                u->manager->api_bus,
                                &s->bus_name_pid_lookup_slot,
                                "org.freedesktop.DBus",
                                "/org/freedesktop/DBus",
                                "org.freedesktop.DBus",
                                "GetConnectionUnixProcessID",
                                bus_name_pid_lookup_callback,
                                s,
                                "s",
                                s->bus_name);