HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/shared/generator.c

41,525 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: fa5bd25f1f82da076381bd4aa7b1ec3689e9b029

Lines 1001–1138 of 1,138

                /* The systemd-udevd.service ordering is mostly about shutdown, not startup: on stop the dm
                 * device is detached via an ioctl carrying a udev cookie that blocks in dm_udev_wait() until
                 * udev releases it (95-dm-notify.rules). Stopping before udevd keeps it around to service the
                 * cookie; otherwise during soft-reboot/shutdown udevd may exit first and the detach hangs. */
                "After=veritysetup-pre.target systemd-udevd-kernel.socket systemd-udevd.service\n"
                "Before=blockdev@dev-mapper-%%i.target\n"
                "Wants=blockdev@dev-mapper-%%i.target\n");
 
        return 0;
}
 
int generator_write_veritysetup_service_section(
                FILE *f,
                const char *name,
                const char *data_what,
                const char *hash_what,
                const char *roothash,
                const char *options) {
 
        _cleanup_free_ char *name_escaped = NULL, *data_what_escaped = NULL, *hash_what_escaped = NULL,
                            *roothash_escaped = NULL, *options_escaped = NULL;
 
        assert(f);
        assert(name);
        assert(data_what);
        assert(hash_what);
 
        name_escaped = specifier_escape(name);
        if (!name_escaped)
                return log_oom();
 
        data_what_escaped = specifier_escape(data_what);
        if (!data_what_escaped)
                return log_oom();
 
        hash_what_escaped = specifier_escape(hash_what);
        if (!hash_what_escaped)
                return log_oom();
 
        if (roothash) {
                roothash_escaped = specifier_escape(roothash);
                if (!roothash_escaped)
                        return log_oom();
        }
 
        if (options) {
                options_escaped = specifier_escape(options);
                if (!options_escaped)
                        return log_oom();
        }
 
        fprintf(f,
                "\n"
                "[Service]\n"
                "Type=oneshot\n"
                "RemainAfterExit=yes\n"
                "ExecStart=" SYSTEMD_VERITYSETUP_PATH " attach '%s' '%s' '%s' '%s' '%s'\n"
                "ExecStop=" SYSTEMD_VERITYSETUP_PATH " detach '%s'\n",
                name_escaped, data_what_escaped, hash_what_escaped, empty_to_dash(roothash_escaped), strempty(options_escaped),
                name_escaped);
 
        return 0;
}
 
void log_setup_generator(void) {
        if (invoked_by_systemd()) {
                /* Disable talking to syslog/journal (i.e. the two IPC-based loggers) if we run in system context. */
                if (streq_ptr(getenv("SYSTEMD_SCOPE"), "system"))
                        log_set_prohibit_ipc(true);
 
                /* This effectively means: journal for per-user service manager generators, kmsg for per-system service manager generators */
                log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
        } else
                log_set_target(LOG_TARGET_AUTO);
 
        log_parse_environment();
        log_open();
}
 
bool generator_soft_rebooted(void) {
        static int cached = -1;
        int r;
 
        if (cached >= 0)
                return cached;
 
        const char *e = secure_getenv("SYSTEMD_SOFT_REBOOTS_COUNT");
        if (!e)
                return (cached = false);
 
        unsigned u;
 
        r = safe_atou(e, &u);
        if (r < 0) {
                log_debug_errno(r, "Failed to parse $SYSTEMD_SOFT_REBOOTS_COUNT, assuming the system hasn't soft-rebooted: %m");
                return (cached = false);
        }
 
        return (cached = (u > 0));
}
 
GptAutoRoot parse_gpt_auto_root(const char *switch_name, const char *value) {
        assert(switch_name);
        assert(value);
 
        /* Parses the 'gpt-auto'/'gpt-auto-root'/'dissect'/'dissect-force' parameters to root=
         *
         * note that we are not using a regular string table here, because the mode names don't fully match
         * the parameter names. And root= being something else is not an error. */
 
        if (streq(value, "gpt-auto")) {
                log_debug("Enabling partition auto-detection (respecting factory reset mode), %s is explicitly set to 'gpt-auto'.", switch_name);
                return GPT_AUTO_ROOT_ON;
        }
 
        if (streq(value, "gpt-auto-force")) {
                log_debug("Enabling partition auto-detection (ignoring factory reset mode), %s is explicitly set to 'gpt-auto-force'.", switch_name);
                return GPT_AUTO_ROOT_FORCE;
        }
 
        if (streq(value, "dissect")) {
                log_debug("Enabling partition auto-detection via full image dissection (respecting factory reset mode), %s is explicitly set to 'dissect'.", switch_name);
                return GPT_AUTO_ROOT_DISSECT;
        }
 
        if (streq(value, "dissect-force")) {
                log_debug("Enabling partition auto-detection via full image dissection (ignoring factory reset mode), %s is explicitly set to 'dissect-force'.", switch_name);
                return GPT_AUTO_ROOT_DISSECT_FORCE;
        }
 
        if (streq(value, "off"))
                log_debug("Disabling partition auto-detection, %s handling is explicitly turned off.", switch_name);
        else
                log_debug("Disabling partition auto-detection, %s is neither unset, nor set to 'gpt-auto', 'gpt-auto-force', 'dissect' or 'dissect-force'.", switch_name);
 
        return GPT_AUTO_ROOT_OFF;
}