Merge pull request #26944 from aafeijoo-suse/systemd-network-generator-initrd-fix

network-generator: do not parse kernel command line more than once
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2023-04-26 14:55:41 +02:00
committed by GitHub
4 changed files with 80 additions and 33 deletions
+1 -1
View File
@@ -820,7 +820,7 @@ int conservative_renameat(
_cleanup_close_ int old_fd = -EBADF, new_fd = -EBADF;
struct stat old_stat, new_stat;
/* Renames the old path to thew new path, much like renameat() — except if both are regular files and
/* Renames the old path to the new path, much like renameat() — except if both are regular files and
* have the exact same contents and basic file attributes already. In that case remove the new file
* instead. This call is useful for reducing inotify wakeups on files that are updated but don't
* actually change. This function is written in a style that we rather rename too often than suppress
+43 -12
View File
@@ -4,6 +4,7 @@
#include "build.h"
#include "fd-util.h"
#include "fs-util.h"
#include "generator.h"
#include "macro.h"
#include "main-func.h"
@@ -17,67 +18,97 @@
static const char *arg_root = NULL;
static int network_save(Network *network, const char *dest_dir) {
_cleanup_free_ char *filename = NULL;
_cleanup_free_ char *filename = NULL, *p = NULL;
_cleanup_(unlink_and_freep) char *temp_path = NULL;
_cleanup_fclose_ FILE *f = NULL;
int r;
assert(network);
r = generator_open_unit_file_full(dest_dir, NULL, NULL, &f, &temp_path);
if (r < 0)
return r;
network_dump(network, f);
r = asprintf(&filename, "%s-%s.network",
isempty(network->ifname) ? "91" : "90",
isempty(network->ifname) ? "default" : network->ifname);
if (r < 0)
return log_oom();
r = generator_open_unit_file(dest_dir, "kernel command line", filename, &f);
p = path_join(dest_dir, filename);
if (!p)
return log_oom();
r = conservative_rename(temp_path, p);
if (r < 0)
return r;
network_dump(network, f);
temp_path = mfree(temp_path);
return 0;
}
static int netdev_save(NetDev *netdev, const char *dest_dir) {
_cleanup_free_ char *filename = NULL;
_cleanup_free_ char *filename = NULL, *p = NULL;
_cleanup_(unlink_and_freep) char *temp_path = NULL;
_cleanup_fclose_ FILE *f = NULL;
int r;
assert(netdev);
r = generator_open_unit_file_full(dest_dir, NULL, NULL, &f, &temp_path);
if (r < 0)
return r;
netdev_dump(netdev, f);
r = asprintf(&filename, "90-%s.netdev",
netdev->ifname);
if (r < 0)
return log_oom();
r = generator_open_unit_file(dest_dir, "kernel command line", filename, &f);
p = path_join(dest_dir, filename);
if (!p)
return log_oom();
r = conservative_rename(temp_path, p);
if (r < 0)
return r;
netdev_dump(netdev, f);
temp_path = mfree(temp_path);
return 0;
}
static int link_save(Link *link, const char *dest_dir) {
_cleanup_free_ char *filename = NULL;
_cleanup_free_ char *filename = NULL, *p = NULL;
_cleanup_(unlink_and_freep) char *temp_path = NULL;
_cleanup_fclose_ FILE *f = NULL;
int r;
assert(link);
r = generator_open_unit_file_full(dest_dir, NULL, NULL, &f, &temp_path);
if (r < 0)
return r;
link_dump(link, f);
filename = strjoin(!isempty(link->ifname) ? "90" :
!hw_addr_is_null(&link->mac) ? "91" : "92",
"-", link->filename, ".link");
if (!filename)
return log_oom();
r = generator_open_unit_file(dest_dir, "kernel command line", filename, &f);
p = path_join(dest_dir, filename);
if (!p)
return log_oom();
r = conservative_rename(temp_path, p);
if (r < 0)
return r;
link_dump(link, f);
temp_path = mfree(temp_path);
return 0;
}
+31 -15
View File
@@ -21,41 +21,57 @@
#include "specifier.h"
#include "string-util.h"
#include "time-util.h"
#include "tmpfile-util.h"
#include "unit-name.h"
int generator_open_unit_file(
int generator_open_unit_file_full(
const char *dir,
const char *source,
const char *fn,
FILE **ret) {
FILE **ret_file,
char **ret_temp_path) {
_cleanup_free_ char *p = NULL;
FILE *f;
int r;
assert(dir);
assert(fn);
assert(ret);
assert(ret_file);
p = path_join(dir, fn);
if (!p)
return log_oom();
/* If <ret_temp_path> is specified, it creates a temporary unit file and also returns its
* temporary path. */
r = fopen_unlocked(p, "wxe", &f);
if (r < 0) {
if (source && r == -EEXIST)
return log_error_errno(r,
"Failed to create unit file '%s', as it already exists. Duplicate entry in '%s'?",
p, source);
if (ret_temp_path) {
r = fopen_temporary(dir, &f, &p);
if (r < 0)
return log_error_errno(r, "Failed to create temporary unit file in '%s': %m", dir);
return log_error_errno(r, "Failed to create unit file '%s': %m", p);
(void) fchmod(fileno(f), 0644);
*ret_temp_path = TAKE_PTR(p);
} else {
assert(fn);
p = path_join(dir, fn);
if (!p)
return log_oom();
r = fopen_unlocked(p, "wxe", &f);
if (r < 0) {
if (source && r == -EEXIST)
return log_error_errno(r,
"Failed to create unit file '%s', as it already exists. Duplicate entry in '%s'?",
p, source);
return log_error_errno(r, "Failed to create unit file '%s': %m", p);
}
}
fprintf(f,
"# Automatically generated by %s\n\n",
program_invocation_short_name);
*ret = f;
*ret_file = f;
return 0;
}
+5 -5
View File
@@ -6,11 +6,11 @@
#include "macro.h"
#include "main-func.h"
int generator_open_unit_file(
const char *dest,
const char *source,
const char *name,
FILE **file);
int generator_open_unit_file_full(const char *dest, const char *source, const char *name, FILE **ret_file, char **ret_temp_path);
static inline int generator_open_unit_file(const char *dest, const char *source, const char *name, FILE **ret_file) {
return generator_open_unit_file_full(dest, source, name, ret_file, NULL);
}
int generator_add_symlink_full(const char *dir, const char *dst, const char *dep_type, const char *src, const char *instance);