diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2023-04-26 14:55:41 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-26 14:55:41 +0200 |
commit | d21d71fb285ebd4726536159c4ccaa08b7046215 (patch) | |
tree | 03c8202c726881e003229aed98bf94ffeac7cb41 /src/shared | |
parent | 914f280d0c0f1555ff94d5490dc4f264af7f331f (diff) | |
parent | f3e4d04298bb73b836dd7ca90f7c7b09de1e776b (diff) | |
download | systemd-d21d71fb285ebd4726536159c4ccaa08b7046215.tar.gz |
Merge pull request #26944 from aafeijoo-suse/systemd-network-generator-initrd-fix
network-generator: do not parse kernel command line more than once
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/generator.c | 46 | ||||
-rw-r--r-- | src/shared/generator.h | 10 |
2 files changed, 36 insertions, 20 deletions
diff --git a/src/shared/generator.c b/src/shared/generator.c index 2ed4be2bf3..b16d0a0ef2 100644 --- a/src/shared/generator.c +++ b/src/shared/generator.c @@ -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); + + (void) fchmod(fileno(f), 0644); - return log_error_errno(r, "Failed to create unit file '%s': %m", p); + *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; } diff --git a/src/shared/generator.h b/src/shared/generator.h index 111900fd45..d97d6edc67 100644 --- a/src/shared/generator.h +++ b/src/shared/generator.h @@ -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); |