HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/basic/env-util.c
Git blob: 4f677565e935d497b16577d55f47251b9075ca79
Lines 1001–1174 of 1,174
}
int secure_getenv_bool(const char *p) {
const char *e;
e = secure_getenv(p);
if (!e)
return -ENXIO;
return parse_boolean(e);
}
int secure_getenv_uint64(const char *p, uint64_t *ret) {
const char *e;
assert(p);
e = secure_getenv(p);
if (!e)
return -ENXIO;
return safe_atou64(e, ret);
}
int set_unset_env(const char *name, const char *value, bool overwrite) {
assert(name);
if (value)
return RET_NERRNO(setenv(name, value, overwrite));
return RET_NERRNO(unsetenv(name));
}
int putenv_dup(const char *assignment, bool override) {
const char *e, *n;
e = strchr(assignment, '=');
if (!e)
return -EINVAL;
n = strndupa_safe(assignment, e - assignment);
/* This is like putenv(), but uses setenv() so that our memory doesn't become part of environ[]. */
return RET_NERRNO(setenv(n, e + 1, override));
}
int setenv_systemd_exec_pid(bool update_only) {
const char *e;
int r;
/* Update $SYSTEMD_EXEC_PID=pid except when '*' is set for the variable. */
e = secure_getenv("SYSTEMD_EXEC_PID");
if (!e && update_only)
return 0;
if (streq_ptr(e, "*"))
return 0;
r = setenvf("SYSTEMD_EXEC_PID", /* overwrite= */ 1, PID_FMT, getpid_cached());
if (r < 0)
return r;
return 1;
}
int setenv_systemd_log_level(void) {
_cleanup_free_ char *val = NULL;
int r;
r = log_level_to_string_alloc(log_get_max_level(), &val);
if (r < 0)
return r;
return RET_NERRNO(setenv("SYSTEMD_LOG_LEVEL", val, /* overwrite= */ true));
}
int getenv_path_list(const char *name, char ***ret_paths) {
_cleanup_strv_free_ char **l = NULL;
const char *e;
int r;
assert(name);
assert(ret_paths);
e = secure_getenv(name);
if (!e)
return -ENXIO;
r = strv_split_full(&l, e, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
if (r < 0)
return log_debug_errno(r, "Failed to parse $%s: %m", name);
STRV_FOREACH(p, l) {
if (!path_is_absolute(*p))
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
"Path '%s' is not absolute, refusing.", *p);
if (!path_is_normalized(*p))
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
"Path '%s' is not normalized, refusing.", *p);
if (path_equal(*p, "/"))
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
"Path '%s' is the root fs, refusing.", *p);
}
if (strv_isempty(l))
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
"No paths specified, refusing.");
*ret_paths = TAKE_PTR(l);
return 1;
}
int getenv_steal_erase(const char *name, char **ret) {
_cleanup_(erase_and_freep) char *a = NULL;
char *e;
assert(name);
/* Reads an environment variable, makes a copy of it, erases its memory in the environment block and removes
* it from there. Usecase: reading passwords from the env block (which is a bad idea, but useful for
* testing, and given that people are likely going to misuse this, be thorough) */
e = secure_getenv(name);
if (!e) {
if (ret)
*ret = NULL;
return 0;
}
if (ret) {
a = strdup(e);
if (!a)
return -ENOMEM;
}
string_erase(e);
if (unsetenv(name) < 0)
return -errno;
if (ret)
*ret = TAKE_PTR(a);
return 1;
}
int setenvf(const char *name, bool overwrite, const char *valuef, ...) {
_cleanup_free_ char *value = NULL;
va_list ap;
int r;
assert(name);
if (!valuef)
return RET_NERRNO(unsetenv(name));
va_start(ap, valuef);
r = vasprintf(&value, valuef, ap);
va_end(ap);
if (r < 0)
return -ENOMEM;
/* Try to suppress writes if the value is already set correctly (simply because memory management of
* environment variables sucks a bit. */
if (streq_ptr(getenv(name), value))
return 0;
return RET_NERRNO(setenv(name, value, overwrite));
}