port write_env_file() to label_os_pre() + teach firstboot a bit of relabelling (#37869)
This is a clean-up, and also addresses some of the points of #37857.
This commit is contained in:
+15
-2
@@ -5,10 +5,12 @@
|
||||
#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"
|
||||
@@ -629,7 +631,7 @@ static void write_env_var(FILE *f, const char *v) {
|
||||
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;
|
||||
@@ -637,7 +639,18 @@ int write_env_file(int dir_fd, const char *fname, char **headers, char **l) {
|
||||
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;
|
||||
|
||||
@@ -672,5 +685,5 @@ int write_vconsole_conf(int dir_fd, const char *fname, char **l) {
|
||||
"# 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);
|
||||
}
|
||||
|
||||
@@ -15,7 +15,11 @@ int load_env_file_pairs_fd(int fd, const char *fname, char ***ret);
|
||||
|
||||
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);
|
||||
|
||||
|
||||
+18
-5
@@ -459,14 +459,14 @@ int symlinkat_idempotent(const char *from, int atfd, const char *to, bool make_r
|
||||
return 0;
|
||||
}
|
||||
|
||||
int symlinkat_atomic_full(const char *from, int atfd, const char *to, bool make_relative) {
|
||||
_cleanup_free_ char *relpath = NULL, *t = NULL;
|
||||
int symlinkat_atomic_full(const char *from, int atfd, const char *to, SymlinkFlags flags) {
|
||||
int r;
|
||||
|
||||
assert(from);
|
||||
assert(to);
|
||||
|
||||
if (make_relative) {
|
||||
_cleanup_free_ char *relpath = NULL;
|
||||
if (FLAGS_SET(flags, SYMLINK_MAKE_RELATIVE)) {
|
||||
r = path_make_relative_parent(to, from, &relpath);
|
||||
if (r < 0)
|
||||
return r;
|
||||
@@ -474,12 +474,25 @@ int symlinkat_atomic_full(const char *from, int atfd, const char *to, bool make_
|
||||
from = relpath;
|
||||
}
|
||||
|
||||
_cleanup_free_ char *t = NULL;
|
||||
r = tempfn_random(to, NULL, &t);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (symlinkat(from, atfd, t) < 0)
|
||||
return -errno;
|
||||
bool call_label_ops_post = false;
|
||||
if (FLAGS_SET(flags, SYMLINK_LABEL)) {
|
||||
r = label_ops_pre(atfd, to, S_IFLNK);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
call_label_ops_post = true;
|
||||
}
|
||||
|
||||
r = RET_NERRNO(symlinkat(from, atfd, t));
|
||||
if (call_label_ops_post)
|
||||
RET_GATHER(r, label_ops_post(atfd, t, /* created= */ r >= 0));
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = RET_NERRNO(renameat(atfd, t, atfd, to));
|
||||
if (r < 0) {
|
||||
|
||||
+7
-2
@@ -48,9 +48,14 @@ static inline int symlink_idempotent(const char *from, const char *to, bool make
|
||||
return symlinkat_idempotent(from, AT_FDCWD, to, make_relative);
|
||||
}
|
||||
|
||||
int symlinkat_atomic_full(const char *from, int atfd, const char *to, bool make_relative);
|
||||
typedef enum SymlinkFlags {
|
||||
SYMLINK_MAKE_RELATIVE = 1 << 0,
|
||||
SYMLINK_LABEL = 1 << 1,
|
||||
} SymlinkFlags;
|
||||
|
||||
int symlinkat_atomic_full(const char *from, int atfd, const char *to, SymlinkFlags flags);
|
||||
static inline int symlink_atomic(const char *from, const char *to) {
|
||||
return symlinkat_atomic_full(from, AT_FDCWD, to, false);
|
||||
return symlinkat_atomic_full(from, AT_FDCWD, to, 0);
|
||||
}
|
||||
|
||||
int mknodat_atomic(int atfd, const char *path, mode_t mode, dev_t dev);
|
||||
|
||||
Reference in New Issue
Block a user