mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-01-05 09:17:44 +03:00
basic/env-util: add little helper to call setenv or unsetenv
This commit is contained in:
parent
55c540d39f
commit
063f9f0da9
@ -747,3 +747,15 @@ int getenv_bool_secure(const char *p) {
|
||||
|
||||
return parse_boolean(e);
|
||||
}
|
||||
|
||||
int set_unset_env(const char *name, const char *value, bool overwrite) {
|
||||
int r;
|
||||
|
||||
if (value)
|
||||
r = setenv(name, value, overwrite);
|
||||
else
|
||||
r = unsetenv(name);
|
||||
if (r < 0)
|
||||
return -errno;
|
||||
return 0;
|
||||
}
|
||||
|
@ -52,3 +52,6 @@ char *strv_env_get(char **x, const char *n) _pure_;
|
||||
|
||||
int getenv_bool(const char *p);
|
||||
int getenv_bool_secure(const char *p);
|
||||
|
||||
/* Like setenv, but calls unsetenv if value == NULL. */
|
||||
int set_unset_env(const char *name, const char *value, bool overwrite);
|
||||
|
@ -189,12 +189,9 @@ int pager_open(PagerFlags flags) {
|
||||
|
||||
/* We generally always set variables used by less, even if we end up using a different pager.
|
||||
* They shouldn't hurt in any case, and ideally other pagers would look at them too. */
|
||||
if (use_secure_mode)
|
||||
r = setenv("LESSSECURE", "1", 1);
|
||||
else
|
||||
r = unsetenv("LESSSECURE");
|
||||
r = set_unset_env("LESSSECURE", use_secure_mode ? "1" : NULL, true);
|
||||
if (r < 0) {
|
||||
log_error_errno(errno, "Failed to adjust environment variable LESSSECURE: %m");
|
||||
log_error_errno(r, "Failed to adjust environment variable LESSSECURE: %m");
|
||||
_exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
@ -372,10 +372,7 @@ static void test_environment_gathering(void) {
|
||||
assert_se(streq(strv_env_get(env, "PATH"), DEFAULT_PATH ":/no/such/file"));
|
||||
|
||||
/* reset environ PATH */
|
||||
if (old)
|
||||
(void) setenv("PATH", old, 1);
|
||||
else
|
||||
(void) unsetenv("PATH");
|
||||
assert_se(set_unset_env("PATH", old, true) == 0);
|
||||
}
|
||||
|
||||
static void test_error_catching(void) {
|
||||
|
@ -12,6 +12,7 @@
|
||||
#include "bus-locator.h"
|
||||
#include "bus-map-properties.h"
|
||||
#include "bus-print-properties.h"
|
||||
#include "env-util.h"
|
||||
#include "format-table.h"
|
||||
#include "in-addr-util.h"
|
||||
#include "main-func.h"
|
||||
@ -139,12 +140,9 @@ static int print_status_info(const StatusInfo *i) {
|
||||
|
||||
|
||||
/* Restore the $TZ */
|
||||
if (old_tz)
|
||||
r = setenv("TZ", old_tz, true);
|
||||
else
|
||||
r = unsetenv("TZ");
|
||||
r = set_unset_env("TZ", old_tz, true);
|
||||
if (r < 0)
|
||||
log_warning_errno(errno, "Failed to set TZ environment variable, ignoring: %m");
|
||||
log_warning_errno(r, "Failed to set TZ environment variable, ignoring: %m");
|
||||
else
|
||||
tzset();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user