]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Merge pull request #12390 from poettering/string-file-mkdir
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 28 May 2019 12:42:55 +0000 (14:42 +0200)
committerGitHub <noreply@github.com>
Tue, 28 May 2019 12:42:55 +0000 (14:42 +0200)
fileio: add a WRITE_STRING_FILE_MKDIR_0755 flag to write_string_file() that creates parent directories if needed

src/basic/fileio.c
src/basic/fileio.h
src/binfmt/binfmt.c
src/firstboot/firstboot.c
src/gpt-auto-generator/gpt-auto-generator.c
src/nspawn/nspawn-cgroup.c
src/rfkill/rfkill.c

index 16034fb9e8a2d08a7813c6728ced5c86afc9ed95..4c18212668eddc4a7c466bcda787d3ae20ef5471 100644 (file)
@@ -21,6 +21,7 @@
 #include "log.h"
 #include "macro.h"
 #include "missing.h"
+#include "mkdir.h"
 #include "parse-util.h"
 #include "path-util.h"
 #include "stdio-util.h"
@@ -174,6 +175,12 @@ int write_string_file_ts(
         /* We don't know how to verify whether the file contents was already on-disk. */
         assert(!((flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE) && (flags & WRITE_STRING_FILE_SYNC)));
 
+        if (flags & WRITE_STRING_FILE_MKDIR_0755) {
+                r = mkdir_parents(fn, 0755);
+                if (r < 0)
+                        return r;
+        }
+
         if (flags & WRITE_STRING_FILE_ATOMIC) {
                 assert(flags & WRITE_STRING_FILE_CREATE);
 
index eb551c7ac14c9be21d55c84eccce519f3bf18af6..05f6c89da09dcdc52d337af9b74d9fd8ea9f3b87 100644 (file)
@@ -21,6 +21,7 @@ typedef enum {
         WRITE_STRING_FILE_SYNC              = 1 << 4,
         WRITE_STRING_FILE_DISABLE_BUFFER    = 1 << 5,
         WRITE_STRING_FILE_NOFOLLOW          = 1 << 6,
+        WRITE_STRING_FILE_MKDIR_0755        = 1 << 7,
 
         /* And before you wonder, why write_string_file_atomic_label_ts() is a separate function instead of just one
            more flag here: it's about linking: we don't want to pull -lselinux into all users of write_string_file()
index 66e2f0131c7e0b55e0295234451303633f2f9b10..dda979cc42f818599d723fca1054bf1d653cd1b8 100644 (file)
@@ -213,7 +213,7 @@ static int run(int argc, char *argv[]) {
                 }
 
                 /* Flush out all rules */
-                write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", WRITE_STRING_FILE_DISABLE_BUFFER);
+                (void) write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", WRITE_STRING_FILE_DISABLE_BUFFER);
 
                 STRV_FOREACH(f, files) {
                         k = apply_file(*f, true);
index 7d97777b0dcef9efbc675a3c70fee1d342f7a071..e6b40294bf6df17883eb6867c4a767e363136d11 100644 (file)
@@ -253,7 +253,7 @@ static int process_locale(void) {
 
         if (arg_copy_locale && arg_root) {
 
-                mkdir_parents(etc_localeconf, 0755);
+                (void) mkdir_parents(etc_localeconf, 0755);
                 r = copy_file("/etc/locale.conf", etc_localeconf, 0, 0644, 0, 0, COPY_REFLINK);
                 if (r != -ENOENT) {
                         if (r < 0)
@@ -278,7 +278,7 @@ static int process_locale(void) {
 
         locales[i] = NULL;
 
-        mkdir_parents(etc_localeconf, 0755);
+        (void) mkdir_parents(etc_localeconf, 0755);
         r = write_env_file(etc_localeconf, locales);
         if (r < 0)
                 return log_error_errno(r, "Failed to write %s: %m", etc_localeconf);
@@ -327,7 +327,7 @@ static int process_keymap(void) {
 
         if (arg_copy_keymap && arg_root) {
 
-                mkdir_parents(etc_vconsoleconf, 0755);
+                (void) mkdir_parents(etc_vconsoleconf, 0755);
                 r = copy_file("/etc/vconsole.conf", etc_vconsoleconf, 0, 0644, 0, 0, COPY_REFLINK);
                 if (r != -ENOENT) {
                         if (r < 0)
@@ -411,7 +411,7 @@ static int process_timezone(void) {
                         if (r < 0)
                                 return log_error_errno(r, "Failed to read host timezone: %m");
 
-                        mkdir_parents(etc_localtime, 0755);
+                        (void) mkdir_parents(etc_localtime, 0755);
                         if (symlink(p, etc_localtime) < 0)
                                 return log_error_errno(errno, "Failed to create %s symlink: %m", etc_localtime);
 
@@ -429,7 +429,7 @@ static int process_timezone(void) {
 
         e = strjoina("../usr/share/zoneinfo/", arg_timezone);
 
-        mkdir_parents(etc_localtime, 0755);
+        (void) mkdir_parents(etc_localtime, 0755);
         if (symlink(e, etc_localtime) < 0)
                 return log_error_errno(errno, "Failed to create %s symlink: %m", etc_localtime);
 
@@ -490,9 +490,8 @@ static int process_hostname(void) {
         if (isempty(arg_hostname))
                 return 0;
 
-        mkdir_parents(etc_hostname, 0755);
         r = write_string_file(etc_hostname, arg_hostname,
-                              WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC);
+                              WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC | WRITE_STRING_FILE_MKDIR_0755);
         if (r < 0)
                 return log_error_errno(r, "Failed to write %s: %m", etc_hostname);
 
@@ -512,9 +511,8 @@ static int process_machine_id(void) {
         if (sd_id128_is_null(arg_machine_id))
                 return 0;
 
-        mkdir_parents(etc_machine_id, 0755);
         r = write_string_file(etc_machine_id, sd_id128_to_string(arg_machine_id, id),
-                              WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC);
+                              WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC | WRITE_STRING_FILE_MKDIR_0755);
         if (r < 0)
                 return log_error_errno(r, "Failed to write machine id: %m");
 
@@ -615,7 +613,7 @@ static int process_root_password(void) {
         if (laccess(etc_shadow, F_OK) >= 0)
                 return 0;
 
-        mkdir_parents(etc_shadow, 0755);
+        (void) mkdir_parents(etc_shadow, 0755);
 
         lock = take_etc_passwd_lock(arg_root);
         if (lock < 0)
index 0f1e184eea932cc149eb1c8a8fa42e4750b57aa2..6c70054d934ebbfd91ac688bf3ce3888cc5c1953 100644 (file)
@@ -521,13 +521,12 @@ static int add_root_rw(DissectedPartition *p) {
         (void) generator_enable_remount_fs_service(arg_dest);
 
         path = strjoina(arg_dest, "/systemd-remount-fs.service.d/50-remount-rw.conf");
-        (void) mkdir_parents(path, 0755);
 
         r = write_string_file(path,
                               "# Automatically generated by systemd-gpt-generator\n\n"
                               "[Service]\n"
                               "Environment=SYSTEMD_REMOUNT_ROOT_RW=1\n",
-                              WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_NOFOLLOW);
+                              WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_NOFOLLOW|WRITE_STRING_FILE_MKDIR_0755);
         if (r < 0)
                 return log_error_errno(r, "Failed to write drop-in file %s: %m", path);
 
index ec62b64b008c80bce368cf518cabd15e5a5636bc..f84bb3979628a8bdb4560844c7b78c5ffcef8d9c 100644 (file)
@@ -121,10 +121,9 @@ int sync_cgroup(pid_t pid, CGroupUnified unified_requested, uid_t uid_shift) {
         (void) rm_rf(fn, REMOVE_ROOT|REMOVE_ONLY_DIRECTORIES);
 
         fn = strjoina(tree, cgroup, "/cgroup.procs");
-        (void) mkdir_parents(fn, 0755);
 
         sprintf(pid_string, PID_FMT, pid);
-        r = write_string_file(fn, pid_string, WRITE_STRING_FILE_DISABLE_BUFFER);
+        r = write_string_file(fn, pid_string, WRITE_STRING_FILE_DISABLE_BUFFER|WRITE_STRING_FILE_MKDIR_0755);
         if (r < 0) {
                 log_error_errno(r, "Failed to move process: %m");
                 goto finish;
index 16610cd4766335887fe311a4460f99229d99f043..fe26566fa3dd8d1d8ba0b0b4952c9375f2382f2f 100644 (file)
@@ -154,7 +154,7 @@ static int load_state(Context *c, const struct rfkill_event *event) {
         if (IN_SET(r, -ENOENT, 0)) {
                 /* No state file or it's truncated? Then save the current state */
 
-                r = write_string_file(state_file, one_zero(event->soft), WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
+                r = write_string_file(state_file, one_zero(event->soft), WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_MKDIR_0755);
                 if (r < 0)
                         return log_error_errno(r, "Failed to write state file %s: %m", state_file);
 
@@ -246,7 +246,7 @@ static int save_state_cancel(Context *c, const struct rfkill_event *event) {
 static int save_state_write_one(struct write_queue_item *item) {
         int r;
 
-        r = write_string_file(item->file, one_zero(item->state), WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
+        r = write_string_file(item->file, one_zero(item->state), WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_MKDIR_0755);
         if (r < 0)
                 return log_error_errno(r, "Failed to write state file %s: %m", item->file);
 
@@ -280,10 +280,6 @@ static int run(int argc, char *argv[]) {
 
         umask(0022);
 
-        r = mkdir_p("/var/lib/systemd/rfkill", 0755);
-        if (r < 0)
-                return log_error_errno(r, "Failed to create rfkill directory: %m");
-
         n = sd_listen_fds(false);
         if (n < 0)
                 return log_error_errno(n, "Failed to determine whether we got any file descriptors passed: %m");