#include "alloc-util.h"
#include "env-file.h"
#include "env-util.h"
+#include "errno-util.h"
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
+#include "label.h"
#include "log.h"
#include "string-util.h"
#include "strv.h"
fputc_unlocked('\n', f);
}
-int write_env_file(int dir_fd, const char *fname, char **headers, char **l) {
+int write_env_file(int dir_fd, const char *fname, char **headers, char **l, WriteEnvFileFlags flags) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *p = NULL;
int r;
assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
assert(fname);
+ bool call_label_ops_post = false;
+ if (FLAGS_SET(flags, WRITE_ENV_FILE_LABEL)) {
+ r = label_ops_pre(dir_fd, fname, S_IFREG);
+ if (r < 0)
+ return r;
+
+ call_label_ops_post = true;
+ }
+
r = fopen_tmpfile_linkable_at(dir_fd, fname, O_WRONLY|O_CLOEXEC, &p, &f);
+ if (call_label_ops_post)
+ RET_GATHER(r, label_ops_post(f ? fileno(f) : dir_fd, f ? NULL : fname, /* created= */ !!f));
if (r < 0)
return r;
"# Written by systemd-localed(8) or systemd-firstboot(1), read by systemd-localed",
"# and systemd-vconsole-setup(8). Use localectl(1) to update this file.");
- return write_env_file(dir_fd, fname, headers, l);
+ return write_env_file(dir_fd, fname, headers, l, WRITE_ENV_FILE_LABEL);
}
int merge_env_file(char ***env, FILE *f, const char *fname);
-int write_env_file(int dir_fd, const char *fname, char **headers, char **l);
+typedef enum WriteEnvFileFlags {
+ WRITE_ENV_FILE_LABEL = 1 << 0,
+} WriteEnvFileFlags;
+
+int write_env_file(int dir_fd, const char *fname, char **headers, char **l, WriteEnvFileFlags flags);
int write_vconsole_conf(int dir_fd, const char *fname, char **l);
locales[i] = NULL;
- r = write_env_file(pfd, f, NULL, locales);
+ r = write_env_file(
+ pfd,
+ f,
+ /* headers= */ NULL,
+ locales,
+ WRITE_ENV_FILE_LABEL);
if (r < 0)
return log_error_errno(r, "Failed to write /etc/locale.conf: %m");
#include "daemon-util.h"
#include "device-private.h"
#include "env-file.h"
-#include "env-file-label.h"
#include "env-util.h"
#include "extract-word.h"
#include "fileio.h"
return 0;
}
- r = write_env_file_label(AT_FDCWD, "/etc/machine-info", NULL, l);
+ r = write_env_file(
+ AT_FDCWD,
+ "/etc/machine-info",
+ /* headers= */ NULL,
+ l,
+ WRITE_ENV_FILE_LABEL);
if (r < 0)
return r;
#include "alloc-util.h"
#include "copy.h"
#include "env-file.h"
-#include "env-file-label.h"
#include "errno-util.h"
#include "extract-word.h"
#include "fd-util.h"
return 0;
}
- r = write_vconsole_conf_label(l);
+ r = write_vconsole_conf(AT_FDCWD, "/etc/vconsole.conf", l);
if (r < 0)
return r;
+++ /dev/null
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-
-#include <sys/stat.h>
-
-#include "env-file.h"
-#include "env-file-label.h"
-#include "selinux-util.h"
-
-int write_env_file_label(int dir_fd, const char *fname, char **headers, char **l) {
- int r;
-
- r = mac_selinux_create_file_prepare(fname, S_IFREG);
- if (r < 0)
- return r;
-
- r = write_env_file(dir_fd, fname, headers, l);
-
- mac_selinux_create_file_clear();
-
- return r;
-}
-
-int write_vconsole_conf_label(char **l) {
- int r;
-
- r = mac_selinux_create_file_prepare("/etc/vconsole.conf", S_IFREG);
- if (r < 0)
- return r;
-
- r = write_vconsole_conf(AT_FDCWD, "/etc/vconsole.conf", l);
-
- mac_selinux_create_file_clear();
-
- return r;
-}
+++ /dev/null
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-#pragma once
-
-#include "forward.h"
-
-/* These functions are split out of fileio.h (and not for example just flags to the functions they wrap) in order to
- * optimize linking: This way, -lselinux is needed only for the callers of these functions that need selinux, but not
- * for all */
-
-int write_env_file_label(int dir_fd, const char *fname, char **headers, char **l);
-
-int write_vconsole_conf_label(char **l);
#include "alloc-util.h"
#include "env-file.h"
-#include "env-file-label.h"
#include "env-util.h"
#include "errno-util.h"
#include "fd-util.h"
return 0;
}
- r = write_env_file_label(AT_FDCWD, "/etc/locale.conf", NULL, set);
+ r = write_env_file(
+ AT_FDCWD,
+ "/etc/locale.conf",
+ /* headers= */ NULL,
+ set,
+ WRITE_ENV_FILE_LABEL);
if (r < 0)
return r;
'efi-loader.c',
'elf-util.c',
'enable-mempool.c',
- 'env-file-label.c',
'ethtool-util.c',
'exec-util.c',
'exit-status.c',
assert_se(tempfn_random_child(NULL, NULL, &p) >= 0);
assert_se(j = strjoin("TEST=", v));
- assert_se(write_env_file(AT_FDCWD, p, STRV_MAKE("# header 1", "", "# header 2"), STRV_MAKE(j)) >= 0);
+ assert_se(write_env_file(AT_FDCWD, p, STRV_MAKE("# header 1", "", "# header 2"), STRV_MAKE(j), /* flags= */ 0) >= 0);
assert_se(cmd = strjoin(". ", p, " && /bin/echo -n \"$TEST\""));
assert_se(f = popen(cmd, "re"));
assert_se(fd >= 0);
}
- r = write_env_file(AT_FDCWD, p, NULL, a);
+ r = write_env_file(AT_FDCWD, p, /* headers= */ NULL, a, /* flags= */ 0);
assert_se(r >= 0);
r = load_env_file(NULL, p, &b);
assert_se(fd >= 0);
}
- r = write_env_file(AT_FDCWD, p, NULL, a);
+ r = write_env_file(AT_FDCWD, p, /* headers= */ NULL, a, /* flags= */ 0);
assert_se(r >= 0);
r = load_env_file(NULL, p, &b);