HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/core/execute.c

114,975 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 78c34b4e092b80db503d10431609260c5b3d8f72

Lines 3001–3235 of 3,235

                return;
 
        /* This is called on the sd-executor side, where everything received is owned by the process and has
         * to be fully cleaned up to make sanitizers and analyzers happy, as opposed as the shallow clean
         * function above. */
 
        close_many_unset(p->fds, p->n_socket_fds + p->n_stashed_fds);
 
        p->cgroup_path = mfree(p->cgroup_path);
 
        if (p->prefix) {
                free_many_charp(p->prefix, _EXEC_DIRECTORY_TYPE_MAX);
                p->prefix = mfree(p->prefix);
        }
 
        p->received_credentials_directory = mfree(p->received_credentials_directory);
        p->received_encrypted_credentials_directory = mfree(p->received_encrypted_credentials_directory);
 
        if (p->idle_pipe) {
                close_many_and_free(p->idle_pipe, 4);
                p->idle_pipe = NULL;
        }
 
        p->stdin_fd = safe_close(p->stdin_fd);
        p->stdout_fd = safe_close(p->stdout_fd);
        p->stderr_fd = safe_close(p->stderr_fd);
 
        p->notify_socket = mfree(p->notify_socket);
 
        open_file_free_many(&p->open_files);
 
        p->fallback_smack_process_label = mfree(p->fallback_smack_process_label);
 
        exec_params_shallow_clear(p);
}
 
void exec_directory_done(ExecDirectory *d) {
        if (!d)
                return;
 
        FOREACH_ARRAY(i, d->items, d->n_items) {
                free(i->path);
                strv_free(i->symlinks);
        }
 
        d->items = mfree(d->items);
        d->n_items = 0;
        d->mode = 0755;
}
 
static ExecDirectoryItem *exec_directory_find(ExecDirectory *d, const char *path) {
        assert(d);
        assert(path);
 
        FOREACH_ARRAY(i, d->items, d->n_items)
                if (path_equal(i->path, path))
                        return i;
 
        return NULL;
}
 
int exec_directory_add(ExecDirectory *d, const char *path, const char *symlink, ExecDirectoryFlags flags) {
        _cleanup_strv_free_ char **s = NULL;
        _cleanup_free_ char *p = NULL;
        ExecDirectoryItem *existing;
        int r;
 
        assert(d);
        assert(path);
 
        existing = exec_directory_find(d, path);
        if (existing) {
                r = strv_extend(&existing->symlinks, symlink);
                if (r < 0)
                        return r;
 
                existing->flags |= flags;
 
                return 0; /* existing item is updated */
        }
 
        p = strdup(path);
        if (!p)
                return -ENOMEM;
 
        if (symlink) {
                s = strv_new(symlink);
                if (!s)
                        return -ENOMEM;
        }
 
        if (!GREEDY_REALLOC(d->items, d->n_items + 1))
                return -ENOMEM;
 
        d->items[d->n_items++] = (ExecDirectoryItem) {
                .path = TAKE_PTR(p),
                .symlinks = TAKE_PTR(s),
                .flags = flags,
        };
 
        return 1; /* new item is added */
}
 
static int exec_directory_item_compare_func(const ExecDirectoryItem *a, const ExecDirectoryItem *b) {
        assert(a);
        assert(b);
 
        return path_compare(a->path, b->path);
}
 
void exec_directory_sort(ExecDirectory *d) {
        assert(d);
 
        /* Sort the exec directories to make always parent directories processed at first in
         * setup_exec_directory(), e.g., even if StateDirectory=foo/bar foo, we need to create foo at first,
         * then foo/bar. Also, set the ONLY_CREATE flag if one of the parent directories is contained in the
         * list. See also comments in setup_exec_directory() and issue #24783. */
 
        if (d->n_items <= 1)
                return;
 
        typesafe_qsort(d->items, d->n_items, exec_directory_item_compare_func);
 
        for (size_t i = 1; i < d->n_items; i++)
                for (size_t j = 0; j < i; j++)
                        if (path_startswith(d->items[i].path, d->items[j].path)) {
                                d->items[i].flags |= EXEC_DIRECTORY_ONLY_CREATE;
                                break;
                        }
}
 
ExecCleanMask exec_clean_mask_from_string(const char *s) {
        ExecDirectoryType t;
 
        assert(s);
 
        if (streq(s, "all"))
                return EXEC_CLEAN_ALL;
        if (streq(s, "fdstore"))
                return EXEC_CLEAN_FDSTORE;
 
        t = exec_resource_type_from_string(s);
        if (t < 0)
                return (ExecCleanMask) t;
 
        return 1U << t;
}
 
static const char* const exec_input_table[_EXEC_INPUT_MAX] = {
        [EXEC_INPUT_NULL]      = "null",
        [EXEC_INPUT_TTY]       = "tty",
        [EXEC_INPUT_TTY_FORCE] = "tty-force",
        [EXEC_INPUT_TTY_FAIL]  = "tty-fail",
        [EXEC_INPUT_SOCKET]    = "socket",
        [EXEC_INPUT_NAMED_FD]  = "fd",
        [EXEC_INPUT_DATA]      = "data",
        [EXEC_INPUT_FILE]      = "file",
};
 
DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput);
 
static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = {
        [EXEC_OUTPUT_INHERIT]             = "inherit",
        [EXEC_OUTPUT_NULL]                = "null",
        [EXEC_OUTPUT_TTY]                 = "tty",
        [EXEC_OUTPUT_KMSG]                = "kmsg",
        [EXEC_OUTPUT_KMSG_AND_CONSOLE]    = "kmsg+console",
        [EXEC_OUTPUT_JOURNAL]             = "journal",
        [EXEC_OUTPUT_JOURNAL_AND_CONSOLE] = "journal+console",
        [EXEC_OUTPUT_SOCKET]              = "socket",
        [EXEC_OUTPUT_NAMED_FD]            = "fd",
        [EXEC_OUTPUT_FILE]                = "file",
        [EXEC_OUTPUT_FILE_APPEND]         = "append",
        [EXEC_OUTPUT_FILE_TRUNCATE]       = "truncate",
};
 
DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput);
 
static const char* const exec_utmp_mode_table[_EXEC_UTMP_MODE_MAX] = {
        [EXEC_UTMP_INIT]  = "init",
        [EXEC_UTMP_LOGIN] = "login",
        [EXEC_UTMP_USER]  = "user",
};
 
DEFINE_STRING_TABLE_LOOKUP(exec_utmp_mode, ExecUtmpMode);
 
static const char* const exec_preserve_mode_table[_EXEC_PRESERVE_MODE_MAX] = {
        [EXEC_PRESERVE_NO]         = "no",
        [EXEC_PRESERVE_YES]        = "yes",
        [EXEC_PRESERVE_RESTART]    = "restart",
        [EXEC_PRESERVE_ON_SUCCESS] = "on-success",
};
 
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(exec_preserve_mode, ExecPreserveMode, EXEC_PRESERVE_YES);
 
static const char* const exec_directory_type_mode_table[_EXEC_DIRECTORY_TYPE_MAX] = {
        [EXEC_DIRECTORY_RUNTIME]       = "RuntimeDirectoryMode",
        [EXEC_DIRECTORY_STATE]         = "StateDirectoryMode",
        [EXEC_DIRECTORY_CACHE]         = "CacheDirectoryMode",
        [EXEC_DIRECTORY_LOGS]          = "LogsDirectoryMode",
        [EXEC_DIRECTORY_CONFIGURATION] = "ConfigurationDirectoryMode",
};
 
DEFINE_STRING_TABLE_LOOKUP(exec_directory_type_mode, ExecDirectoryType);
 
/* And this table maps ExecDirectoryType too, but to a generic term identifying the type of resource. This
 * one is supposed to be generic enough to be used for unit types that don't use ExecContext and per-unit
 * directories, specifically .timer units with their timestamp touch file. */
static const char* const exec_resource_type_table[_EXEC_DIRECTORY_TYPE_MAX] = {
        [EXEC_DIRECTORY_RUNTIME]       = "runtime",
        [EXEC_DIRECTORY_STATE]         = "state",
        [EXEC_DIRECTORY_CACHE]         = "cache",
        [EXEC_DIRECTORY_LOGS]          = "logs",
        [EXEC_DIRECTORY_CONFIGURATION] = "configuration",
};
 
DEFINE_STRING_TABLE_LOOKUP(exec_resource_type, ExecDirectoryType);
 
static const char* const exec_keyring_mode_table[_EXEC_KEYRING_MODE_MAX] = {
        [EXEC_KEYRING_INHERIT] = "inherit",
        [EXEC_KEYRING_PRIVATE] = "private",
        [EXEC_KEYRING_SHARED]  = "shared",
};
 
DEFINE_STRING_TABLE_LOOKUP(exec_keyring_mode, ExecKeyringMode);
 
static const char* const exec_memory_thp_table[_EXEC_MEMORY_THP_MAX] = {
        [EXEC_MEMORY_THP_INHERIT] = "inherit",
        [EXEC_MEMORY_THP_DISABLE] = "disable",
        [EXEC_MEMORY_THP_MADVISE] = "madvise",
        [EXEC_MEMORY_THP_SYSTEM]  = "system",
};
 
DEFINE_STRING_TABLE_LOOKUP(exec_memory_thp, ExecMemoryTHP);