env-util: add new setenvf() helper

And convert some pieces of code over.
This commit is contained in:
Lennart Poettering
2024-01-08 23:22:58 +01:00
parent bdd2036e81
commit b20e9dc515
4 changed files with 36 additions and 15 deletions
+26 -5
View File
@@ -1012,8 +1012,8 @@ int putenv_dup(const char *assignment, bool override) {
}
int setenv_systemd_exec_pid(bool update_only) {
char str[DECIMAL_STR_MAX(pid_t)];
const char *e;
int r;
/* Update $SYSTEMD_EXEC_PID=pid except when '*' is set for the variable. */
@@ -1024,10 +1024,9 @@ int setenv_systemd_exec_pid(bool update_only) {
if (streq_ptr(e, "*"))
return 0;
xsprintf(str, PID_FMT, getpid_cached());
if (setenv("SYSTEMD_EXEC_PID", str, 1) < 0)
return -errno;
r = setenvf("SYSTEMD_EXEC_PID", /* overwrite= */ 1, PID_FMT, getpid_cached());
if (r < 0)
return r;
return 1;
}
@@ -1122,3 +1121,25 @@ int set_full_environment(char **env) {
return 0;
}
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);
DISABLE_WARNING_FORMAT_NONLITERAL;
r = vasprintf(&value, valuef, ap);
REENABLE_WARNING;
va_end(ap);
if (r < 0)
return -ENOMEM;
return RET_NERRNO(setenv(name, value, overwrite));
}
+2
View File
@@ -80,3 +80,5 @@ int getenv_path_list(const char *name, char ***ret_paths);
int getenv_steal_erase(const char *name, char **ret);
int set_full_environment(char **env);
int setenvf(const char *name, bool overwrite, const char *valuef, ...) _printf_(3,4);
+4 -6
View File
@@ -12,6 +12,7 @@
#include "alloc-util.h"
#include "build.h"
#include "env-util.h"
#include "fd-util.h"
#include "format-util.h"
#include "main-func.h"
@@ -157,7 +158,6 @@ static int run(int argc, char *argv[]) {
if (argc <= optind)
(void) execl("/bin/cat", "/bin/cat", NULL);
else {
_cleanup_free_ char *s = NULL;
struct stat st;
if (fstat(STDERR_FILENO, &st) < 0)
@@ -165,11 +165,9 @@ static int run(int argc, char *argv[]) {
"Failed to fstat(%s): %m",
FORMAT_PROC_FD_PATH(STDERR_FILENO));
if (asprintf(&s, DEV_FMT ":" INO_FMT, (dev_t)st.st_dev, st.st_ino) < 0)
return log_oom();
if (setenv("JOURNAL_STREAM", s, /* overwrite = */ true) < 0)
return log_error_errno(errno, "Failed to set environment variable JOURNAL_STREAM: %m");
r = setenvf("JOURNAL_STREAM", /* overwrite = */ true, DEV_FMT ":" INO_FMT, (dev_t) st.st_dev, st.st_ino);
if (r < 0)
return log_error_errno(r, "Failed to set environment variable JOURNAL_STREAM: %m");
(void) execvp(argv[optind], argv + optind);
}
+4 -4
View File
@@ -5,6 +5,7 @@
#include "sd-daemon.h"
#include "common-signal.h"
#include "env-util.h"
#include "fd-util.h"
#include "fs-util.h"
#include "mkdir.h"
@@ -157,7 +158,6 @@ static int start_one_worker(Manager *m) {
if (r < 0)
return log_error_errno(r, "Failed to fork new worker child: %m");
if (r == 0) {
char pids[DECIMAL_STR_MAX(pid_t)];
/* Child */
if (m->listen_fd == 3) {
@@ -175,9 +175,9 @@ static int start_one_worker(Manager *m) {
safe_close(m->listen_fd);
}
xsprintf(pids, PID_FMT, pid);
if (setenv("LISTEN_PID", pids, 1) < 0) {
log_error_errno(errno, "Failed to set $LISTEN_PID: %m");
r = setenvf("LISTEN_PID", /* overwrite= */ true, PID_FMT, pid);
if (r < 0) {
log_error_errno(r, "Failed to set $LISTEN_PID: %m");
_exit(EXIT_FAILURE);
}