Compare commits

..

No commits in common. "faa73d4e0c8095fedd98ff29851b9634810ff97e" and "ca121e20c42219e3bc4e5cb63dcc96cc5eae2879" have entirely different histories.

21 changed files with 138 additions and 119 deletions

View File

@ -747,15 +747,3 @@ int getenv_bool_secure(const char *p) {
return parse_boolean(e); 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;
}

View File

@ -52,6 +52,3 @@ char *strv_env_get(char **x, const char *n) _pure_;
int getenv_bool(const char *p); int getenv_bool(const char *p);
int getenv_bool_secure(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);

View File

@ -117,7 +117,7 @@ int write_string_stream_ts(
FILE *f, FILE *f,
const char *line, const char *line,
WriteStringFileFlags flags, WriteStringFileFlags flags,
const struct timespec *ts) { struct timespec *ts) {
bool needs_nl; bool needs_nl;
int r, fd; int r, fd;
@ -161,7 +161,7 @@ int write_string_stream_ts(
return r; return r;
if (ts) { if (ts) {
const struct timespec twice[2] = {*ts, *ts}; struct timespec twice[2] = {*ts, *ts};
if (futimens(fd, twice) < 0) if (futimens(fd, twice) < 0)
return -errno; return -errno;
@ -174,7 +174,7 @@ static int write_string_file_atomic(
const char *fn, const char *fn,
const char *line, const char *line,
WriteStringFileFlags flags, WriteStringFileFlags flags,
const struct timespec *ts) { struct timespec *ts) {
_cleanup_fclose_ FILE *f = NULL; _cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *p = NULL; _cleanup_free_ char *p = NULL;
@ -221,7 +221,7 @@ int write_string_file_ts(
const char *fn, const char *fn,
const char *line, const char *line,
WriteStringFileFlags flags, WriteStringFileFlags flags,
const struct timespec *ts) { struct timespec *ts) {
_cleanup_fclose_ FILE *f = NULL; _cleanup_fclose_ FILE *f = NULL;
int q, r, fd; int q, r, fd;

View File

@ -48,11 +48,11 @@ DIR* take_fdopendir(int *dfd);
FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc); FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc);
FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode); FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode);
int write_string_stream_ts(FILE *f, const char *line, WriteStringFileFlags flags, const struct timespec *ts); int write_string_stream_ts(FILE *f, const char *line, WriteStringFileFlags flags, struct timespec *ts);
static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) { static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) {
return write_string_stream_ts(f, line, flags, NULL); return write_string_stream_ts(f, line, flags, NULL);
} }
int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, const struct timespec *ts); int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts);
static inline int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) { static inline int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) {
return write_string_file_ts(fn, line, flags, NULL); return write_string_file_ts(fn, line, flags, NULL);
} }

View File

@ -1418,8 +1418,9 @@ static int fixup_environment(void) {
return -errno; return -errno;
/* The kernels sets HOME=/ for init. Let's undo this. */ /* The kernels sets HOME=/ for init. Let's undo this. */
if (path_equal_ptr(getenv("HOME"), "/")) if (path_equal_ptr(getenv("HOME"), "/") &&
assert_se(unsetenv("HOME") == 0); unsetenv("HOME") < 0)
log_warning_errno(errno, "Failed to unset $HOME: %m");
return 0; return 0;
} }

View File

@ -215,7 +215,9 @@ static int acquire_existing_password(const char *user_name, UserRecord *hr, bool
return log_error_errno(r, "Failed to store password: %m"); return log_error_errno(r, "Failed to store password: %m");
string_erase(e); string_erase(e);
assert_se(unsetenv("PASSWORD") == 0);
if (unsetenv("PASSWORD") < 0)
return log_error_errno(errno, "Failed to unset $PASSWORD: %m");
return 0; return 0;
} }
@ -253,7 +255,9 @@ static int acquire_token_pin(const char *user_name, UserRecord *hr) {
return log_error_errno(r, "Failed to store token PIN: %m"); return log_error_errno(r, "Failed to store token PIN: %m");
string_erase(e); string_erase(e);
assert_se(unsetenv("PIN") == 0);
if (unsetenv("PIN") < 0)
return log_error_errno(errno, "Failed to unset $PIN: %m");
return 0; return 0;
} }
@ -993,7 +997,9 @@ static int acquire_new_password(
return log_error_errno(r, "Failed to store password: %m"); return log_error_errno(r, "Failed to store password: %m");
string_erase(e); string_erase(e);
assert_se(unsetenv("NEWPASSWORD") == 0);
if (unsetenv("NEWPASSWORD") < 0)
return log_error_errno(errno, "Failed to unset $NEWPASSWORD: %m");
if (ret) if (ret)
*ret = TAKE_PTR(copy); *ret = TAKE_PTR(copy);

View File

@ -106,7 +106,7 @@ static int _bind_raw_socket(int ifindex, union sockaddr_union *link,
.sll_hatype = htobe16(arp_type), .sll_hatype = htobe16(arp_type),
.sll_halen = bcast_addr_len, .sll_halen = bcast_addr_len,
}; };
memcpy(link->ll.sll_addr, bcast_addr, bcast_addr_len); /* We may overflow link->ll. link->ll_buffer ensures we have enough space. */ memcpy(link->ll.sll_addr, bcast_addr, bcast_addr_len);
r = bind(s, &link->sa, SOCKADDR_LL_LEN(link->ll)); r = bind(s, &link->sa, SOCKADDR_LL_LEN(link->ll));
if (r < 0) if (r < 0)

View File

@ -30,12 +30,13 @@
#define SNDBUF_SIZE (8*1024*1024) #define SNDBUF_SIZE (8*1024*1024)
static void unsetenv_all(bool unset_environment) { static void unsetenv_all(bool unset_environment) {
if (!unset_environment) if (!unset_environment)
return; return;
assert_se(unsetenv("LISTEN_PID") == 0); unsetenv("LISTEN_PID");
assert_se(unsetenv("LISTEN_FDS") == 0); unsetenv("LISTEN_FDS");
assert_se(unsetenv("LISTEN_FDNAMES") == 0); unsetenv("LISTEN_FDNAMES");
} }
_public_ int sd_listen_fds(int unset_environment) { _public_ int sd_listen_fds(int unset_environment) {
@ -547,7 +548,7 @@ _public_ int sd_pid_notify_with_fds(
finish: finish:
if (unset_environment) if (unset_environment)
assert_se(unsetenv("NOTIFY_SOCKET") == 0); unsetenv("NOTIFY_SOCKET");
return r; return r;
} }
@ -671,9 +672,9 @@ _public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
finish: finish:
if (unset_environment && s) if (unset_environment && s)
assert_se(unsetenv("WATCHDOG_USEC") == 0); unsetenv("WATCHDOG_USEC");
if (unset_environment && p) if (unset_environment && p)
assert_se(unsetenv("WATCHDOG_PID") == 0); unsetenv("WATCHDOG_PID");
return r; return r;
} }

View File

@ -402,7 +402,8 @@ static int source_io_register(
if (epoll_ctl(s->event->epoll_fd, if (epoll_ctl(s->event->epoll_fd,
s->io.registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, s->io.registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD,
s->io.fd, &ev) < 0) s->io.fd,
&ev) < 0)
return -errno; return -errno;
s->io.registered = true; s->io.registered = true;
@ -429,6 +430,8 @@ static void source_child_pidfd_unregister(sd_event_source *s) {
} }
static int source_child_pidfd_register(sd_event_source *s, int enabled) { static int source_child_pidfd_register(sd_event_source *s, int enabled) {
int r;
assert(s); assert(s);
assert(s->type == SOURCE_CHILD); assert(s->type == SOURCE_CHILD);
assert(enabled != SD_EVENT_OFF); assert(enabled != SD_EVENT_OFF);
@ -439,9 +442,11 @@ static int source_child_pidfd_register(sd_event_source *s, int enabled) {
.data.ptr = s, .data.ptr = s,
}; };
if (epoll_ctl(s->event->epoll_fd, if (s->child.registered)
s->child.registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_MOD, s->child.pidfd, &ev);
s->child.pidfd, &ev) < 0) else
r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_ADD, s->child.pidfd, &ev);
if (r < 0)
return -errno; return -errno;
} }
@ -1335,25 +1340,31 @@ _public_ int sd_event_add_child(
if (r < 0) if (r < 0)
return r; return r;
e->n_enabled_child_sources++;
if (EVENT_SOURCE_WATCH_PIDFD(s)) { if (EVENT_SOURCE_WATCH_PIDFD(s)) {
/* We have a pidfd and we only want to watch for exit */ /* We have a pidfd and we only want to watch for exit */
r = source_child_pidfd_register(s, s->enabled);
if (r < 0)
return r;
r = source_child_pidfd_register(s, s->enabled);
if (r < 0) {
e->n_enabled_child_sources--;
return r;
}
} else { } else {
/* We have no pidfd or we shall wait for some other event than WEXITED */ /* We have no pidfd or we shall wait for some other event than WEXITED */
r = event_make_signal_data(e, SIGCHLD, NULL); r = event_make_signal_data(e, SIGCHLD, NULL);
if (r < 0) if (r < 0) {
e->n_enabled_child_sources--;
return r; return r;
}
e->need_process_child = true; e->need_process_child = true;
} }
e->n_enabled_child_sources++;
if (ret) if (ret)
*ret = s; *ret = s;
TAKE_PTR(s); TAKE_PTR(s);
return 0; return 0;
} }
@ -1418,24 +1429,31 @@ _public_ int sd_event_add_child_pidfd(
if (r < 0) if (r < 0)
return r; return r;
e->n_enabled_child_sources++;
if (EVENT_SOURCE_WATCH_PIDFD(s)) { if (EVENT_SOURCE_WATCH_PIDFD(s)) {
/* We only want to watch for WEXITED */ /* We only want to watch for WEXITED */
r = source_child_pidfd_register(s, s->enabled); r = source_child_pidfd_register(s, s->enabled);
if (r < 0) if (r < 0) {
e->n_enabled_child_sources--;
return r; return r;
}
} else { } else {
/* We shall wait for some other event than WEXITED */ /* We shall wait for some other event than WEXITED */
r = event_make_signal_data(e, SIGCHLD, NULL); r = event_make_signal_data(e, SIGCHLD, NULL);
if (r < 0) if (r < 0) {
e->n_enabled_child_sources--;
return r; return r;
}
e->need_process_child = true; e->need_process_child = true;
} }
e->n_enabled_child_sources++;
if (ret) if (ret)
*ret = s; *ret = s;
TAKE_PTR(s); TAKE_PTR(s);
return 0; return 0;
} }
@ -2293,11 +2311,11 @@ static int event_source_disable(sd_event_source *s) {
return 0; return 0;
} }
static int event_source_enable(sd_event_source *s, int enable) { static int event_source_enable(sd_event_source *s, int m) {
int r; int r;
assert(s); assert(s);
assert(IN_SET(enable, SD_EVENT_ON, SD_EVENT_ONESHOT)); assert(IN_SET(m, SD_EVENT_ON, SD_EVENT_ONESHOT));
assert(s->enabled == SD_EVENT_OFF); assert(s->enabled == SD_EVENT_OFF);
/* Unset the pending flag when this event source is enabled */ /* Unset the pending flag when this event source is enabled */
@ -2307,62 +2325,19 @@ static int event_source_enable(sd_event_source *s, int enable) {
return r; return r;
} }
s->enabled = m;
switch (s->type) { switch (s->type) {
case SOURCE_IO: case SOURCE_IO:
r = source_io_register(s, enable, s->io.events); r = source_io_register(s, m, s->io.events);
if (r < 0)
return r;
break;
case SOURCE_SIGNAL:
r = event_make_signal_data(s->event, s->signal.sig, NULL);
if (r < 0) { if (r < 0) {
event_gc_signal_data(s->event, &s->priority, s->signal.sig); s->enabled = SD_EVENT_OFF;
return r; return r;
} }
break; break;
case SOURCE_CHILD:
if (EVENT_SOURCE_WATCH_PIDFD(s)) {
/* yes, we have pidfd */
r = source_child_pidfd_register(s, enable);
if (r < 0)
return r;
} else {
/* no pidfd, or something other to watch for than WEXITED */
r = event_make_signal_data(s->event, SIGCHLD, NULL);
if (r < 0) {
event_gc_signal_data(s->event, &s->priority, SIGCHLD);
return r;
}
}
s->event->n_enabled_child_sources++;
break;
case SOURCE_TIME_REALTIME:
case SOURCE_TIME_BOOTTIME:
case SOURCE_TIME_MONOTONIC:
case SOURCE_TIME_REALTIME_ALARM:
case SOURCE_TIME_BOOTTIME_ALARM:
case SOURCE_EXIT:
case SOURCE_DEFER:
case SOURCE_POST:
case SOURCE_INOTIFY:
break;
default:
assert_not_reached("Wut? I shouldn't exist.");
}
s->enabled = enable;
/* Non-failing operations below */
switch (s->type) {
case SOURCE_TIME_REALTIME: case SOURCE_TIME_REALTIME:
case SOURCE_TIME_BOOTTIME: case SOURCE_TIME_BOOTTIME:
case SOURCE_TIME_MONOTONIC: case SOURCE_TIME_MONOTONIC:
@ -2371,12 +2346,53 @@ static int event_source_enable(sd_event_source *s, int enable) {
event_source_time_prioq_reshuffle(s); event_source_time_prioq_reshuffle(s);
break; break;
case SOURCE_SIGNAL:
r = event_make_signal_data(s->event, s->signal.sig, NULL);
if (r < 0) {
s->enabled = SD_EVENT_OFF;
event_gc_signal_data(s->event, &s->priority, s->signal.sig);
return r;
}
break;
case SOURCE_CHILD:
s->event->n_enabled_child_sources++;
if (EVENT_SOURCE_WATCH_PIDFD(s)) {
/* yes, we have pidfd */
r = source_child_pidfd_register(s, s->enabled);
if (r < 0) {
s->enabled = SD_EVENT_OFF;
s->event->n_enabled_child_sources--;
return r;
}
} else {
/* no pidfd, or something other to watch for than WEXITED */
r = event_make_signal_data(s->event, SIGCHLD, NULL);
if (r < 0) {
s->enabled = SD_EVENT_OFF;
s->event->n_enabled_child_sources--;
event_gc_signal_data(s->event, &s->priority, SIGCHLD);
return r;
}
}
break;
case SOURCE_EXIT: case SOURCE_EXIT:
prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index); prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
break; break;
default: case SOURCE_DEFER:
case SOURCE_POST:
case SOURCE_INOTIFY:
break; break;
default:
assert_not_reached("Wut? I shouldn't exist.");
} }
return 0; return 0;

View File

@ -189,9 +189,12 @@ int pager_open(PagerFlags flags) {
/* We generally always set variables used by less, even if we end up using a different pager. /* 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. */ * They shouldn't hurt in any case, and ideally other pagers would look at them too. */
r = set_unset_env("LESSSECURE", use_secure_mode ? "1" : NULL, true); if (use_secure_mode)
r = setenv("LESSSECURE", "1", 1);
else
r = unsetenv("LESSSECURE");
if (r < 0) { if (r < 0) {
log_error_errno(r, "Failed to adjust environment variable LESSSECURE: %m"); log_error_errno(errno, "Failed to adjust environment variable LESSSECURE: %m");
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }

View File

@ -372,7 +372,10 @@ static void test_environment_gathering(void) {
assert_se(streq(strv_env_get(env, "PATH"), DEFAULT_PATH ":/no/such/file")); assert_se(streq(strv_env_get(env, "PATH"), DEFAULT_PATH ":/no/such/file"));
/* reset environ PATH */ /* reset environ PATH */
assert_se(set_unset_env("PATH", old, true) == 0); if (old)
(void) setenv("PATH", old, 1);
else
(void) unsetenv("PATH");
} }
static void test_error_catching(void) { static void test_error_catching(void) {

View File

@ -898,11 +898,11 @@ int main(int argc, char *argv[]) {
} }
#endif #endif
assert_se(unsetenv("USER") == 0); (void) unsetenv("USER");
assert_se(unsetenv("LOGNAME") == 0); (void) unsetenv("LOGNAME");
assert_se(unsetenv("SHELL") == 0); (void) unsetenv("SHELL");
assert_se(unsetenv("HOME") == 0); (void) unsetenv("HOME");
assert_se(unsetenv("TMPDIR") == 0); (void) unsetenv("TMPDIR");
can_unshare = have_namespaces(); can_unshare = have_namespaces();

View File

@ -184,7 +184,7 @@ static void test_find_executable_full(void) {
if (p) if (p)
assert_se(oldpath = strdup(p)); assert_se(oldpath = strdup(p));
assert_se(unsetenv("PATH") == 0); assert_se(unsetenv("PATH") >= 0);
assert_se(find_executable_full("sh", true, &p) == 0); assert_se(find_executable_full("sh", true, &p) == 0);
puts(p); puts(p);
@ -347,7 +347,7 @@ static void test_fsck_exists(void) {
log_info("/* %s */", __func__); log_info("/* %s */", __func__);
/* Ensure we use a sane default for PATH. */ /* Ensure we use a sane default for PATH. */
assert_se(unsetenv("PATH") == 0); unsetenv("PATH");
/* fsck.minix is provided by util-linux and will probably exist. */ /* fsck.minix is provided by util-linux and will probably exist. */
assert_se(fsck_exists("minix") == 1); assert_se(fsck_exists("minix") == 1);

View File

@ -480,7 +480,7 @@ static void test_in_utc_timezone(void) {
assert_se(streq(tzname[0], "CET")); assert_se(streq(tzname[0], "CET"));
assert_se(streq(tzname[1], "CEST")); assert_se(streq(tzname[1], "CEST"));
assert_se(unsetenv("TZ") == 0); assert_se(unsetenv("TZ") >= 0);
} }
static void test_map_clock_usec(void) { static void test_map_clock_usec(void) {

View File

@ -12,7 +12,6 @@
#include "bus-locator.h" #include "bus-locator.h"
#include "bus-map-properties.h" #include "bus-map-properties.h"
#include "bus-print-properties.h" #include "bus-print-properties.h"
#include "env-util.h"
#include "format-table.h" #include "format-table.h"
#include "in-addr-util.h" #include "in-addr-util.h"
#include "main-func.h" #include "main-func.h"
@ -140,9 +139,12 @@ static int print_status_info(const StatusInfo *i) {
/* Restore the $TZ */ /* Restore the $TZ */
r = set_unset_env("TZ", old_tz, true); if (old_tz)
r = setenv("TZ", old_tz, true);
else
r = unsetenv("TZ");
if (r < 0) if (r < 0)
log_warning_errno(r, "Failed to set TZ environment variable, ignoring: %m"); log_warning_errno(errno, "Failed to set TZ environment variable, ignoring: %m");
else else
tzset(); tzset();

View File

@ -565,7 +565,7 @@ static int worker_main(Manager *_manager, sd_device_monitor *monitor, sd_device
assert(monitor); assert(monitor);
assert(dev); assert(dev);
assert_se(unsetenv("NOTIFY_SOCKET") == 0); unsetenv("NOTIFY_SOCKET");
assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, -1) >= 0); assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, -1) >= 0);

View File

@ -780,8 +780,10 @@ static int run(int argc, char *argv[]) {
return log_error_errno(r, "Failed to set $SYSTEMD_ONLY_USERDB: %m"); return log_error_errno(r, "Failed to set $SYSTEMD_ONLY_USERDB: %m");
log_info("Enabled services: %s", e); log_info("Enabled services: %s", e);
} else } else {
assert_se(unsetenv("SYSTEMD_ONLY_USERDB") == 0); if (unsetenv("SYSTEMD_ONLY_USERDB") < 0)
return log_error_errno(r, "Failed to unset $SYSTEMD_ONLY_USERDB: %m");
}
return dispatch_verb(argc, argv, verbs, NULL); return dispatch_verb(argc, argv, verbs, NULL);
} }

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1-or-later # SPDX-License-Identifier: LGPL-2.1+
# #
# sd-script.py: create LOTS of sd device entries in fake sysfs # sd-script.py: create LOTS of sd device entries in fake sysfs
# #

View File

@ -1,4 +1,4 @@
# SPDX-License-Identifier: LGPL-2.1-or-later # SPDX-License-Identifier: LGPL-2.1+
# #
# This file is part of systemd. # This file is part of systemd.
# #

View File

@ -1,4 +1,4 @@
# SPDX-License-Identifier: LGPL-2.1-or-later # SPDX-License-Identifier: LGPL-2.1+
# #
# This file is part of systemd. # This file is part of systemd.
# #

View File

@ -1,4 +1,4 @@
# SPDX-License-Identifier: LGPL-2.1-or-later # SPDX-License-Identifier: LGPL-2.1+
# #
# This file is part of systemd. # This file is part of systemd.
# #