basic: add RuntimeScope enum

In various tools and services we have a per-system and per-user concept.
So far we sometimes used a boolean indicating whether we are in system
mode, or a reversed boolean indicating whether we are in user mode, or
the LookupScope enum used by the lookup path logic.

Let's address that, in introduce a common enum for this, we can use all
across the board.

This is mostly just search/replace, no actual code changes.
This commit is contained in:
Lennart Poettering
2023-03-10 09:47:39 +01:00
parent 5f64d2bf33
commit 4870133bfa
89 changed files with 938 additions and 814 deletions
+1
View File
@@ -71,6 +71,7 @@ basic_sources = files(
'recurse-dir.c',
'replace-var.c',
'rlimit-util.c',
'runtime-scope.c',
'sigbus.c',
'signal-util.c',
'siphash24.c',
+44 -38
View File
@@ -232,7 +232,7 @@ bool path_is_user_config_dir(const char *path) {
}
static int acquire_generator_dirs(
LookupScope scope,
RuntimeScope scope,
const char *tempdir,
char **generator,
char **generator_early,
@@ -244,17 +244,17 @@ static int acquire_generator_dirs(
assert(generator);
assert(generator_early);
assert(generator_late);
assert(IN_SET(scope, LOOKUP_SCOPE_SYSTEM, LOOKUP_SCOPE_USER, LOOKUP_SCOPE_GLOBAL));
assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL));
if (scope == LOOKUP_SCOPE_GLOBAL)
if (scope == RUNTIME_SCOPE_GLOBAL)
return -EOPNOTSUPP;
if (tempdir)
prefix = tempdir;
else if (scope == LOOKUP_SCOPE_SYSTEM)
else if (scope == RUNTIME_SCOPE_SYSTEM)
prefix = "/run/systemd";
else {
/* LOOKUP_SCOPE_USER */
/* RUNTIME_SCOPE_USER */
const char *e;
e = getenv("XDG_RUNTIME_DIR");
@@ -288,21 +288,21 @@ static int acquire_generator_dirs(
}
static int acquire_transient_dir(
LookupScope scope,
RuntimeScope scope,
const char *tempdir,
char **ret) {
char *transient;
assert(ret);
assert(IN_SET(scope, LOOKUP_SCOPE_SYSTEM, LOOKUP_SCOPE_USER, LOOKUP_SCOPE_GLOBAL));
assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL));
if (scope == LOOKUP_SCOPE_GLOBAL)
if (scope == RUNTIME_SCOPE_GLOBAL)
return -EOPNOTSUPP;
if (tempdir)
transient = path_join(tempdir, "transient");
else if (scope == LOOKUP_SCOPE_SYSTEM)
else if (scope == RUNTIME_SCOPE_SYSTEM)
transient = strdup("/run/systemd/transient");
else
return xdg_user_runtime_dir(ret, "/systemd/transient");
@@ -313,7 +313,7 @@ static int acquire_transient_dir(
return 0;
}
static int acquire_config_dirs(LookupScope scope, char **persistent, char **runtime) {
static int acquire_config_dirs(RuntimeScope scope, char **persistent, char **runtime) {
_cleanup_free_ char *a = NULL, *b = NULL;
int r;
@@ -322,17 +322,17 @@ static int acquire_config_dirs(LookupScope scope, char **persistent, char **runt
switch (scope) {
case LOOKUP_SCOPE_SYSTEM:
case RUNTIME_SCOPE_SYSTEM:
a = strdup(SYSTEM_CONFIG_UNIT_DIR);
b = strdup("/run/systemd/system");
break;
case LOOKUP_SCOPE_GLOBAL:
case RUNTIME_SCOPE_GLOBAL:
a = strdup(USER_CONFIG_UNIT_DIR);
b = strdup("/run/systemd/user");
break;
case LOOKUP_SCOPE_USER:
case RUNTIME_SCOPE_USER:
r = xdg_user_config_dir(&a, "/systemd/user");
if (r < 0 && r != -ENXIO)
return r;
@@ -364,7 +364,7 @@ static int acquire_config_dirs(LookupScope scope, char **persistent, char **runt
return 0;
}
static int acquire_control_dirs(LookupScope scope, char **persistent, char **runtime) {
static int acquire_control_dirs(RuntimeScope scope, char **persistent, char **runtime) {
_cleanup_free_ char *a = NULL;
int r;
@@ -373,7 +373,7 @@ static int acquire_control_dirs(LookupScope scope, char **persistent, char **run
switch (scope) {
case LOOKUP_SCOPE_SYSTEM: {
case RUNTIME_SCOPE_SYSTEM: {
_cleanup_free_ char *b = NULL;
a = strdup("/etc/systemd/system.control");
@@ -389,7 +389,7 @@ static int acquire_control_dirs(LookupScope scope, char **persistent, char **run
break;
}
case LOOKUP_SCOPE_USER:
case RUNTIME_SCOPE_USER:
r = xdg_user_config_dir(&a, "/systemd/user.control");
if (r < 0 && r != -ENXIO)
return r;
@@ -406,7 +406,7 @@ static int acquire_control_dirs(LookupScope scope, char **persistent, char **run
break;
case LOOKUP_SCOPE_GLOBAL:
case RUNTIME_SCOPE_GLOBAL:
return -EOPNOTSUPP;
default:
@@ -419,7 +419,7 @@ static int acquire_control_dirs(LookupScope scope, char **persistent, char **run
}
static int acquire_attached_dirs(
LookupScope scope,
RuntimeScope scope,
char **ret_persistent,
char **ret_runtime) {
@@ -429,7 +429,7 @@ static int acquire_attached_dirs(
assert(ret_runtime);
/* Portable services are not available to regular users for now. */
if (scope != LOOKUP_SCOPE_SYSTEM)
if (scope != RUNTIME_SCOPE_SYSTEM)
return -EOPNOTSUPP;
a = strdup("/etc/systemd/system.attached");
@@ -509,7 +509,7 @@ static int get_paths_from_environ(const char *var, char ***paths, bool *append)
int lookup_paths_init(
LookupPaths *lp,
LookupScope scope,
RuntimeScope scope,
LookupPathsFlags flags,
const char *root_dir) {
@@ -528,14 +528,14 @@ int lookup_paths_init(
assert(lp);
assert(scope >= 0);
assert(scope < _LOOKUP_SCOPE_MAX);
assert(scope < _RUNTIME_SCOPE_MAX);
#if HAVE_SPLIT_USR
flags |= LOOKUP_PATHS_SPLIT_USR;
#endif
if (!empty_or_root(root_dir)) {
if (scope == LOOKUP_SCOPE_USER)
if (scope == RUNTIME_SCOPE_USER)
return -EINVAL;
r = is_dir(root_dir, true);
@@ -560,8 +560,8 @@ int lookup_paths_init(
if (r < 0)
return r;
if (scope == LOOKUP_SCOPE_USER) {
r = acquire_config_dirs(LOOKUP_SCOPE_GLOBAL, &global_persistent_config, &global_runtime_config);
if (scope == RUNTIME_SCOPE_USER) {
r = acquire_config_dirs(RUNTIME_SCOPE_GLOBAL, &global_persistent_config, &global_runtime_config);
if (r < 0)
return r;
}
@@ -606,7 +606,7 @@ int lookup_paths_init(
switch (scope) {
case LOOKUP_SCOPE_SYSTEM:
case RUNTIME_SCOPE_SYSTEM:
add = strv_new(
/* If you modify this you also want to modify
* systemdsystemunitpath= in systemd.pc.in! */
@@ -629,7 +629,7 @@ int lookup_paths_init(
STRV_IFNOTNULL(generator_late));
break;
case LOOKUP_SCOPE_GLOBAL:
case RUNTIME_SCOPE_GLOBAL:
add = strv_new(
/* If you modify this you also want to modify
* systemduserunitpath= in systemd.pc.in, and
@@ -652,7 +652,7 @@ int lookup_paths_init(
STRV_IFNOTNULL(generator_late));
break;
case LOOKUP_SCOPE_USER:
case RUNTIME_SCOPE_USER:
add = user_dirs(persistent_config, runtime_config,
global_persistent_config, global_runtime_config,
generator, generator_early, generator_late,
@@ -741,7 +741,7 @@ int lookup_paths_init(
return 0;
}
int lookup_paths_init_or_warn(LookupPaths *lp, LookupScope scope, LookupPathsFlags flags, const char *root_dir) {
int lookup_paths_init_or_warn(LookupPaths *lp, RuntimeScope scope, LookupPathsFlags flags, const char *root_dir) {
int r;
r = lookup_paths_init(lp, scope, flags, root_dir);
@@ -790,7 +790,7 @@ void lookup_paths_log(LookupPaths *lp) {
}
}
char **generator_binary_paths(LookupScope scope) {
char **generator_binary_paths(RuntimeScope scope) {
bool append = false; /* Add items from SYSTEMD_GENERATOR_PATH before normal directories */
_cleanup_strv_free_ char **paths = NULL;
int r;
@@ -805,15 +805,15 @@ char **generator_binary_paths(LookupScope scope) {
switch (scope) {
case LOOKUP_SCOPE_SYSTEM:
case RUNTIME_SCOPE_SYSTEM:
add = strv_new("/run/systemd/system-generators",
"/etc/systemd/system-generators",
"/usr/local/lib/systemd/system-generators",
SYSTEM_GENERATOR_DIR);
break;
case LOOKUP_SCOPE_GLOBAL:
case LOOKUP_SCOPE_USER:
case RUNTIME_SCOPE_GLOBAL:
case RUNTIME_SCOPE_USER:
add = strv_new("/run/systemd/user-generators",
"/etc/systemd/user-generators",
"/usr/local/lib/systemd/user-generators",
@@ -823,7 +823,6 @@ char **generator_binary_paths(LookupScope scope) {
default:
assert_not_reached();
}
if (!add)
return NULL;
@@ -840,10 +839,9 @@ char **generator_binary_paths(LookupScope scope) {
return TAKE_PTR(paths);
}
char **env_generator_binary_paths(bool is_system) {
char **env_generator_binary_paths(RuntimeScope runtime_scope) {
_cleanup_strv_free_ char **paths = NULL, **add = NULL;
bool append = false; /* Add items from SYSTEMD_ENVIRONMENT_GENERATOR_PATH before normal directories */
_cleanup_strv_free_ char **paths = NULL;
_cleanup_strv_free_ char **add = NULL;
int r;
/* First priority is whatever has been passed to us via env vars */
@@ -852,17 +850,25 @@ char **env_generator_binary_paths(bool is_system) {
return NULL;
if (!paths || append) {
if (is_system)
switch (runtime_scope) {
case RUNTIME_SCOPE_SYSTEM:
add = strv_new("/run/systemd/system-environment-generators",
"/etc/systemd/system-environment-generators",
"/usr/local/lib/systemd/system-environment-generators",
SYSTEM_ENV_GENERATOR_DIR);
else
break;
case RUNTIME_SCOPE_USER:
add = strv_new("/run/systemd/user-environment-generators",
"/etc/systemd/user-environment-generators",
"/usr/local/lib/systemd/user-environment-generators",
USER_ENV_GENERATOR_DIR);
break;
default:
assert_not_reached();
}
if (!add)
return NULL;
}
+5 -12
View File
@@ -5,6 +5,7 @@
#include "constants.h"
#include "macro.h"
#include "runtime-scope.h"
typedef enum LookupPathsFlags {
LOOKUP_PATHS_EXCLUDE_GENERATED = 1 << 0,
@@ -12,14 +13,6 @@ typedef enum LookupPathsFlags {
LOOKUP_PATHS_SPLIT_USR = 1 << 2,
} LookupPathsFlags;
typedef enum LookupScope {
LOOKUP_SCOPE_SYSTEM,
LOOKUP_SCOPE_GLOBAL,
LOOKUP_SCOPE_USER,
_LOOKUP_SCOPE_MAX,
_LOOKUP_SCOPE_INVALID = -EINVAL,
} LookupScope;
typedef struct LookupPaths {
/* Where we look for unit files. This includes the individual special paths below, but also any vendor
* supplied, static unit file paths. */
@@ -59,8 +52,8 @@ typedef struct LookupPaths {
char *temporary_dir;
} LookupPaths;
int lookup_paths_init(LookupPaths *lp, LookupScope scope, LookupPathsFlags flags, const char *root_dir);
int lookup_paths_init_or_warn(LookupPaths *lp, LookupScope scope, LookupPathsFlags flags, const char *root_dir);
int lookup_paths_init(LookupPaths *lp, RuntimeScope scope, LookupPathsFlags flags, const char *root_dir);
int lookup_paths_init_or_warn(LookupPaths *lp, RuntimeScope scope, LookupPathsFlags flags, const char *root_dir);
int xdg_user_dirs(char ***ret_config_dirs, char ***ret_data_dirs);
int xdg_user_runtime_dir(char **ret, const char *suffix);
@@ -73,8 +66,8 @@ bool path_is_user_config_dir(const char *path);
void lookup_paths_log(LookupPaths *p);
void lookup_paths_free(LookupPaths *p);
char **generator_binary_paths(LookupScope scope);
char **env_generator_binary_paths(bool is_system);
char **generator_binary_paths(RuntimeScope scope);
char **env_generator_binary_paths(RuntimeScope scope);
#define NETWORK_DIRS ((const char* const*) CONF_PATHS_STRV("systemd/network"))
#define NETWORK_DIRS_NULSTR CONF_PATHS_NULSTR("systemd/network")
+12
View File
@@ -0,0 +1,12 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "runtime-scope.h"
#include "string-table.h"
static const char* const runtime_scope_table[_RUNTIME_SCOPE_MAX] = {
[RUNTIME_SCOPE_SYSTEM] = "system",
[RUNTIME_SCOPE_USER] = "user",
[RUNTIME_SCOPE_GLOBAL] = "global",
};
DEFINE_STRING_TABLE_LOOKUP(runtime_scope, RuntimeScope);
+17
View File
@@ -0,0 +1,17 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include <errno.h>
#include "macro.h"
typedef enum RuntimeScope {
RUNTIME_SCOPE_SYSTEM, /* for the system */
RUNTIME_SCOPE_USER, /* for a user */
RUNTIME_SCOPE_GLOBAL, /* for all users */
_RUNTIME_SCOPE_MAX,
_RUNTIME_SCOPE_INVALID = -EINVAL,
} RuntimeScope;
const char *runtime_scope_to_string(RuntimeScope scope) _const_;
RuntimeScope runtime_scope_from_string(const char *s) _const_;