]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
util-lib: split out all temporary file related calls into tmpfiles-util.c
authorLennart Poettering <lennart@poettering.net>
Fri, 30 Nov 2018 20:05:27 +0000 (21:05 +0100)
committerLennart Poettering <lennart@poettering.net>
Sun, 2 Dec 2018 12:22:29 +0000 (13:22 +0100)
This splits out a bunch of functions from fileio.c that have to do with
temporary files. Simply to make the header files a bit shorter, and to
group things more nicely.

No code changes, just some rearranging of source files.

90 files changed:
src/basic/copy.c
src/basic/fd-util.c
src/basic/fileio.c
src/basic/fileio.h
src/basic/fs-util.c
src/basic/meson.build
src/basic/tmpfile-util.c [new file with mode: 0644]
src/basic/tmpfile-util.h [new file with mode: 0644]
src/boot/bootctl.c
src/core/unit.c
src/coredump/coredump.c
src/coredump/coredumpctl.c
src/fuzz/fuzz-catalog.c
src/fuzz/fuzz-journald-native-fd.c
src/import/export-raw.c
src/import/export-tar.c
src/import/import-common.c
src/import/import-fs.c
src/import/import-raw.c
src/import/import-tar.c
src/import/pull-raw.c
src/import/pull-tar.c
src/journal-remote/journal-gatewayd.c
src/journal-remote/journal-upload.c
src/journal/catalog.c
src/journal/journal-send.c
src/journal/journal-verify.c
src/journal/journalctl.c
src/journal/journald-stream.c
src/journal/test-catalog.c
src/journal/test-compress.c
src/journal/test-mmap-cache.c
src/libsystemd-network/sd-dhcp-lease.c
src/libsystemd/sd-bus/test-bus-watch-bind.c
src/libsystemd/sd-device/device-private.c
src/libsystemd/sd-event/test-event.c
src/libsystemd/sd-hwdb/hwdb-util.c
src/locale/keymap-util.c
src/login/logind-dbus.c
src/login/logind-inhibit.c
src/login/logind-seat.c
src/login/logind-session.c
src/login/logind-user.c
src/machine/machine-dbus.c
src/machine/machine.c
src/machine/machined-dbus.c
src/network/fuzz-netdev-parser.c
src/network/fuzz-network-parser.c
src/network/networkd-link.c
src/network/networkd-manager.c
src/network/test-routing-policy-rule.c
src/nspawn/nspawn-mount.c
src/nspawn/nspawn.c
src/portable/portable.c
src/resolve/resolved-link.c
src/resolve/resolved-resolv-conf.c
src/resolve/test-resolved-etc-hosts.c
src/shared/ask-password-api.c
src/shared/dissect-image.c
src/shared/exec-util.c
src/shared/fileio-label.c
src/shared/fileio-label.h
src/shared/meson.build
src/shared/path-lookup.c
src/shared/tmpfile-util-label.c [new file with mode: 0644]
src/shared/tmpfile-util-label.h [new file with mode: 0644]
src/systemctl/systemctl.c
src/sysusers/sysusers.c
src/test/test-acl-util.c
src/test/test-async.c
src/test/test-chown-rec.c
src/test/test-clock.c
src/test/test-conf-parser.c
src/test/test-copy.c
src/test/test-dev-setup.c
src/test/test-fd-util.c
src/test/test-fdset.c
src/test/test-fileio.c
src/test/test-fs-util.c
src/test/test-glob-util.c
src/test/test-hostname-util.c
src/test/test-id128.c
src/test/test-serialize.c
src/test/test-socket-util.c
src/test/test-stat-util.c
src/test/test-terminal-util.c
src/test/test-tmpfiles.c
src/test/test-unit-file.c
src/test/test-xattr-util.c
src/timedate/timedated.c

index 50240de8b6e280627c74d2fc3796ce3143a5fa63..34e01ea1cf4dbdaada79542ded53d9c3287dfaba 100644 (file)
@@ -19,7 +19,6 @@
 #include "copy.h"
 #include "dirent-util.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "io-util.h"
 #include "macro.h"
@@ -28,6 +27,7 @@
 #include "string-util.h"
 #include "strv.h"
 #include "time-util.h"
+#include "tmpfile-util.h"
 #include "umask-util.h"
 #include "user-util.h"
 #include "xattr-util.h"
index 97dc40357ccbfff3721a8a44a6a7f8faf23232de..2fae4da15bcde170de90f97a69cf39455dcbf172 100644 (file)
@@ -23,6 +23,7 @@
 #include "socket-util.h"
 #include "stdio-util.h"
 #include "util.h"
+#include "tmpfile-util.h"
 
 int close_nointr(int fd) {
         assert(fd >= 0);
index d86ad5815336b86ce69e6dcdebe59772582167d1..6ccbae02a9a30d7771eed6ab7f83aaa954fb9f50 100644 (file)
 #include "fd-util.h"
 #include "fileio.h"
 #include "fs-util.h"
-#include "hexdecoct.h"
 #include "log.h"
 #include "macro.h"
 #include "missing.h"
 #include "parse-util.h"
 #include "path-util.h"
 #include "process-util.h"
-#include "random-util.h"
 #include "stdio-util.h"
 #include "string-util.h"
 #include "strv.h"
-#include "time-util.h"
-#include "umask-util.h"
+#include "tmpfile-util.h"
 #include "utf8.h"
 
 #define READ_FULL_BYTES_MAX (4U*1024U*1024U)
@@ -1128,39 +1125,6 @@ int search_and_fopen_nulstr(const char *path, const char *mode, const char *root
         return search_and_fopen_internal(path, mode, root, s, _f);
 }
 
-int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
-        FILE *f;
-        char *t;
-        int r, fd;
-
-        assert(path);
-        assert(_f);
-        assert(_temp_path);
-
-        r = tempfn_xxxxxx(path, NULL, &t);
-        if (r < 0)
-                return r;
-
-        fd = mkostemp_safe(t);
-        if (fd < 0) {
-                free(t);
-                return -errno;
-        }
-
-        f = fdopen(fd, "we");
-        if (!f) {
-                unlink_noerrno(t);
-                free(t);
-                safe_close(fd);
-                return -errno;
-        }
-
-        *_f = f;
-        *_temp_path = t;
-
-        return 0;
-}
-
 int fflush_and_check(FILE *f) {
         assert(f);
 
@@ -1192,163 +1156,6 @@ int fflush_sync_and_check(FILE *f) {
         return 0;
 }
 
-/* This is much like mkostemp() but is subject to umask(). */
-int mkostemp_safe(char *pattern) {
-        _cleanup_umask_ mode_t u = 0;
-        int fd;
-
-        assert(pattern);
-
-        u = umask(077);
-
-        fd = mkostemp(pattern, O_CLOEXEC);
-        if (fd < 0)
-                return -errno;
-
-        return fd;
-}
-
-int fmkostemp_safe(char *pattern, const char *mode, FILE **ret_f) {
-        int fd;
-        FILE *f;
-
-        fd = mkostemp_safe(pattern);
-        if (fd < 0)
-                return fd;
-
-        f = fdopen(fd, mode);
-        if (!f) {
-                safe_close(fd);
-                return -errno;
-        }
-
-        *ret_f = f;
-        return 0;
-}
-
-int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
-        const char *fn;
-        char *t;
-
-        assert(ret);
-
-        if (isempty(p))
-                return -EINVAL;
-        if (path_equal(p, "/"))
-                return -EINVAL;
-
-        /*
-         * Turns this:
-         *         /foo/bar/waldo
-         *
-         * Into this:
-         *         /foo/bar/.#<extra>waldoXXXXXX
-         */
-
-        fn = basename(p);
-        if (!filename_is_valid(fn))
-                return -EINVAL;
-
-        extra = strempty(extra);
-
-        t = new(char, strlen(p) + 2 + strlen(extra) + 6 + 1);
-        if (!t)
-                return -ENOMEM;
-
-        strcpy(stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn), "XXXXXX");
-
-        *ret = path_simplify(t, false);
-        return 0;
-}
-
-int tempfn_random(const char *p, const char *extra, char **ret) {
-        const char *fn;
-        char *t, *x;
-        uint64_t u;
-        unsigned i;
-
-        assert(ret);
-
-        if (isempty(p))
-                return -EINVAL;
-        if (path_equal(p, "/"))
-                return -EINVAL;
-
-        /*
-         * Turns this:
-         *         /foo/bar/waldo
-         *
-         * Into this:
-         *         /foo/bar/.#<extra>waldobaa2a261115984a9
-         */
-
-        fn = basename(p);
-        if (!filename_is_valid(fn))
-                return -EINVAL;
-
-        extra = strempty(extra);
-
-        t = new(char, strlen(p) + 2 + strlen(extra) + 16 + 1);
-        if (!t)
-                return -ENOMEM;
-
-        x = stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn);
-
-        u = random_u64();
-        for (i = 0; i < 16; i++) {
-                *(x++) = hexchar(u & 0xF);
-                u >>= 4;
-        }
-
-        *x = 0;
-
-        *ret = path_simplify(t, false);
-        return 0;
-}
-
-int tempfn_random_child(const char *p, const char *extra, char **ret) {
-        char *t, *x;
-        uint64_t u;
-        unsigned i;
-        int r;
-
-        assert(ret);
-
-        /* Turns this:
-         *         /foo/bar/waldo
-         * Into this:
-         *         /foo/bar/waldo/.#<extra>3c2b6219aa75d7d0
-         */
-
-        if (!p) {
-                r = tmp_dir(&p);
-                if (r < 0)
-                        return r;
-        }
-
-        extra = strempty(extra);
-
-        t = new(char, strlen(p) + 3 + strlen(extra) + 16 + 1);
-        if (!t)
-                return -ENOMEM;
-
-        if (isempty(p))
-                x = stpcpy(stpcpy(t, ".#"), extra);
-        else
-                x = stpcpy(stpcpy(stpcpy(t, p), "/.#"), extra);
-
-        u = random_u64();
-        for (i = 0; i < 16; i++) {
-                *(x++) = hexchar(u & 0xF);
-                u >>= 4;
-        }
-
-        *x = 0;
-
-        *ret = path_simplify(t, false);
-        return 0;
-}
-
 int write_timestamp_file_atomic(const char *fn, usec_t n) {
         char ln[DECIMAL_STR_MAX(n)+2];
 
@@ -1412,71 +1219,6 @@ int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space)
         return fputs(s, f);
 }
 
-int open_tmpfile_unlinkable(const char *directory, int flags) {
-        char *p;
-        int fd, r;
-
-        if (!directory) {
-                r = tmp_dir(&directory);
-                if (r < 0)
-                        return r;
-        } else if (isempty(directory))
-                return -EINVAL;
-
-        /* Returns an unlinked temporary file that cannot be linked into the file system anymore */
-
-        /* Try O_TMPFILE first, if it is supported */
-        fd = open(directory, flags|O_TMPFILE|O_EXCL, S_IRUSR|S_IWUSR);
-        if (fd >= 0)
-                return fd;
-
-        /* Fall back to unguessable name + unlinking */
-        p = strjoina(directory, "/systemd-tmp-XXXXXX");
-
-        fd = mkostemp_safe(p);
-        if (fd < 0)
-                return fd;
-
-        (void) unlink(p);
-
-        return fd;
-}
-
-int open_tmpfile_linkable(const char *target, int flags, char **ret_path) {
-        _cleanup_free_ char *tmp = NULL;
-        int r, fd;
-
-        assert(target);
-        assert(ret_path);
-
-        /* Don't allow O_EXCL, as that has a special meaning for O_TMPFILE */
-        assert((flags & O_EXCL) == 0);
-
-        /* Creates a temporary file, that shall be renamed to "target" later. If possible, this uses O_TMPFILE â€“ in
-         * which case "ret_path" will be returned as NULL. If not possible a the tempoary path name used is returned in
-         * "ret_path". Use link_tmpfile() below to rename the result after writing the file in full. */
-
-        fd = open_parent(target, O_TMPFILE|flags, 0640);
-        if (fd >= 0) {
-                *ret_path = NULL;
-                return fd;
-        }
-
-        log_debug_errno(fd, "Failed to use O_TMPFILE for %s: %m", target);
-
-        r = tempfn_random(target, NULL, &tmp);
-        if (r < 0)
-                return r;
-
-        fd = open(tmp, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|flags, 0640);
-        if (fd < 0)
-                return -errno;
-
-        *ret_path = TAKE_PTR(tmp);
-
-        return fd;
-}
-
 int open_serialization_fd(const char *ident) {
         int fd;
 
@@ -1496,35 +1238,6 @@ int open_serialization_fd(const char *ident) {
         return fd;
 }
 
-int link_tmpfile(int fd, const char *path, const char *target) {
-        int r;
-
-        assert(fd >= 0);
-        assert(target);
-
-        /* Moves a temporary file created with open_tmpfile() above into its final place. if "path" is NULL an fd
-         * created with O_TMPFILE is assumed, and linkat() is used. Otherwise it is assumed O_TMPFILE is not supported
-         * on the directory, and renameat2() is used instead.
-         *
-         * Note that in both cases we will not replace existing files. This is because linkat() does not support this
-         * operation currently (renameat2() does), and there is no nice way to emulate this. */
-
-        if (path) {
-                r = rename_noreplace(AT_FDCWD, path, AT_FDCWD, target);
-                if (r < 0)
-                        return r;
-        } else {
-                char proc_fd_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(fd) + 1];
-
-                xsprintf(proc_fd_path, "/proc/self/fd/%i", fd);
-
-                if (linkat(AT_FDCWD, proc_fd_path, AT_FDCWD, target, AT_SYMLINK_FOLLOW) < 0)
-                        return -errno;
-        }
-
-        return 0;
-}
-
 int read_nul_string(FILE *f, char **ret) {
         _cleanup_free_ char *x = NULL;
         size_t allocated = 0, n = 0;
@@ -1565,33 +1278,6 @@ int read_nul_string(FILE *f, char **ret) {
         return 0;
 }
 
-int mkdtemp_malloc(const char *template, char **ret) {
-        _cleanup_free_ char *p = NULL;
-        int r;
-
-        assert(ret);
-
-        if (template)
-                p = strdup(template);
-        else {
-                const char *tmp;
-
-                r = tmp_dir(&tmp);
-                if (r < 0)
-                        return r;
-
-                p = strjoin(tmp, "/XXXXXX");
-        }
-        if (!p)
-                return -ENOMEM;
-
-        if (!mkdtemp(p))
-                return -errno;
-
-        *ret = TAKE_PTR(p);
-        return 0;
-}
-
 DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, funlockfile);
 
 int read_line(FILE *f, size_t limit, char **ret) {
index dae115e3bbc83fe7a64b38b0e066c792ecb3495b..8ca717f309891c9e0da661df7e826428ee29ea8b 100644 (file)
@@ -66,27 +66,13 @@ int search_and_fopen_nulstr(const char *path, const char *mode, const char *root
 int fflush_and_check(FILE *f);
 int fflush_sync_and_check(FILE *f);
 
-int fopen_temporary(const char *path, FILE **_f, char **_temp_path);
-int mkostemp_safe(char *pattern);
-int fmkostemp_safe(char *pattern, const char *mode, FILE**_f);
-
-int tempfn_xxxxxx(const char *p, const char *extra, char **ret);
-int tempfn_random(const char *p, const char *extra, char **ret);
-int tempfn_random_child(const char *p, const char *extra, char **ret);
-
 int write_timestamp_file_atomic(const char *fn, usec_t n);
 int read_timestamp_file(const char *fn, usec_t *ret);
 
 int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space);
 
-int open_tmpfile_unlinkable(const char *directory, int flags);
-int open_tmpfile_linkable(const char *target, int flags, char **ret_path);
 int open_serialization_fd(const char *ident);
 
-int link_tmpfile(int fd, const char *path, const char *target);
-
 int read_nul_string(FILE *f, char **ret);
 
-int mkdtemp_malloc(const char *template, char **ret);
-
 int read_line(FILE *f, size_t limit, char **ret);
index 94efca08cac44d717216c89ca74bb832dd61d654..f70878fb8e11c22061e9d4e9e54bdf32bc45691e 100644 (file)
@@ -13,7 +13,6 @@
 #include "alloc-util.h"
 #include "dirent-util.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "log.h"
 #include "macro.h"
@@ -27,6 +26,7 @@
 #include "string-util.h"
 #include "strv.h"
 #include "time-util.h"
+#include "tmpfile-util.h"
 #include "user-util.h"
 #include "util.h"
 
index 124a3e9f847edd9423331f1ef7ef9b8a80106d71..d49aa65d8f411f5079275e3e678e1a79a1d9627d 100644 (file)
@@ -159,6 +159,8 @@ basic_sources = files('''
         terminal-util.h
         time-util.c
         time-util.h
+        tmpfile-util.c
+        tmpfile-util.h
         umask-util.h
         unaligned.h
         unit-def.c
diff --git a/src/basic/tmpfile-util.c b/src/basic/tmpfile-util.c
new file mode 100644 (file)
index 0000000..dba0492
--- /dev/null
@@ -0,0 +1,329 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+#include <sys/mman.h>
+
+#include "alloc-util.h"
+#include "fd-util.h"
+#include "fs-util.h"
+#include "hexdecoct.h"
+#include "macro.h"
+#include "memfd-util.h"
+#include "missing_syscall.h"
+#include "path-util.h"
+#include "process-util.h"
+#include "random-util.h"
+#include "stdio-util.h"
+#include "string-util.h"
+#include "tmpfile-util.h"
+#include "umask-util.h"
+
+int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
+        FILE *f;
+        char *t;
+        int r, fd;
+
+        assert(path);
+        assert(_f);
+        assert(_temp_path);
+
+        r = tempfn_xxxxxx(path, NULL, &t);
+        if (r < 0)
+                return r;
+
+        fd = mkostemp_safe(t);
+        if (fd < 0) {
+                free(t);
+                return -errno;
+        }
+
+        f = fdopen(fd, "we");
+        if (!f) {
+                unlink_noerrno(t);
+                free(t);
+                safe_close(fd);
+                return -errno;
+        }
+
+        *_f = f;
+        *_temp_path = t;
+
+        return 0;
+}
+
+/* This is much like mkostemp() but is subject to umask(). */
+int mkostemp_safe(char *pattern) {
+        _cleanup_umask_ mode_t u = 0;
+        int fd;
+
+        assert(pattern);
+
+        u = umask(077);
+
+        fd = mkostemp(pattern, O_CLOEXEC);
+        if (fd < 0)
+                return -errno;
+
+        return fd;
+}
+
+int fmkostemp_safe(char *pattern, const char *mode, FILE **ret_f) {
+        int fd;
+        FILE *f;
+
+        fd = mkostemp_safe(pattern);
+        if (fd < 0)
+                return fd;
+
+        f = fdopen(fd, mode);
+        if (!f) {
+                safe_close(fd);
+                return -errno;
+        }
+
+        *ret_f = f;
+        return 0;
+}
+
+int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
+        const char *fn;
+        char *t;
+
+        assert(ret);
+
+        if (isempty(p))
+                return -EINVAL;
+        if (path_equal(p, "/"))
+                return -EINVAL;
+
+        /*
+         * Turns this:
+         *         /foo/bar/waldo
+         *
+         * Into this:
+         *         /foo/bar/.#<extra>waldoXXXXXX
+         */
+
+        fn = basename(p);
+        if (!filename_is_valid(fn))
+                return -EINVAL;
+
+        extra = strempty(extra);
+
+        t = new(char, strlen(p) + 2 + strlen(extra) + 6 + 1);
+        if (!t)
+                return -ENOMEM;
+
+        strcpy(stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn), "XXXXXX");
+
+        *ret = path_simplify(t, false);
+        return 0;
+}
+
+int tempfn_random(const char *p, const char *extra, char **ret) {
+        const char *fn;
+        char *t, *x;
+        uint64_t u;
+        unsigned i;
+
+        assert(ret);
+
+        if (isempty(p))
+                return -EINVAL;
+        if (path_equal(p, "/"))
+                return -EINVAL;
+
+        /*
+         * Turns this:
+         *         /foo/bar/waldo
+         *
+         * Into this:
+         *         /foo/bar/.#<extra>waldobaa2a261115984a9
+         */
+
+        fn = basename(p);
+        if (!filename_is_valid(fn))
+                return -EINVAL;
+
+        extra = strempty(extra);
+
+        t = new(char, strlen(p) + 2 + strlen(extra) + 16 + 1);
+        if (!t)
+                return -ENOMEM;
+
+        x = stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn);
+
+        u = random_u64();
+        for (i = 0; i < 16; i++) {
+                *(x++) = hexchar(u & 0xF);
+                u >>= 4;
+        }
+
+        *x = 0;
+
+        *ret = path_simplify(t, false);
+        return 0;
+}
+
+int tempfn_random_child(const char *p, const char *extra, char **ret) {
+        char *t, *x;
+        uint64_t u;
+        unsigned i;
+        int r;
+
+        assert(ret);
+
+        /* Turns this:
+         *         /foo/bar/waldo
+         * Into this:
+         *         /foo/bar/waldo/.#<extra>3c2b6219aa75d7d0
+         */
+
+        if (!p) {
+                r = tmp_dir(&p);
+                if (r < 0)
+                        return r;
+        }
+
+        extra = strempty(extra);
+
+        t = new(char, strlen(p) + 3 + strlen(extra) + 16 + 1);
+        if (!t)
+                return -ENOMEM;
+
+        if (isempty(p))
+                x = stpcpy(stpcpy(t, ".#"), extra);
+        else
+                x = stpcpy(stpcpy(stpcpy(t, p), "/.#"), extra);
+
+        u = random_u64();
+        for (i = 0; i < 16; i++) {
+                *(x++) = hexchar(u & 0xF);
+                u >>= 4;
+        }
+
+        *x = 0;
+
+        *ret = path_simplify(t, false);
+        return 0;
+}
+
+int open_tmpfile_unlinkable(const char *directory, int flags) {
+        char *p;
+        int fd, r;
+
+        if (!directory) {
+                r = tmp_dir(&directory);
+                if (r < 0)
+                        return r;
+        } else if (isempty(directory))
+                return -EINVAL;
+
+        /* Returns an unlinked temporary file that cannot be linked into the file system anymore */
+
+        /* Try O_TMPFILE first, if it is supported */
+        fd = open(directory, flags|O_TMPFILE|O_EXCL, S_IRUSR|S_IWUSR);
+        if (fd >= 0)
+                return fd;
+
+        /* Fall back to unguessable name + unlinking */
+        p = strjoina(directory, "/systemd-tmp-XXXXXX");
+
+        fd = mkostemp_safe(p);
+        if (fd < 0)
+                return fd;
+
+        (void) unlink(p);
+
+        return fd;
+}
+
+int open_tmpfile_linkable(const char *target, int flags, char **ret_path) {
+        _cleanup_free_ char *tmp = NULL;
+        int r, fd;
+
+        assert(target);
+        assert(ret_path);
+
+        /* Don't allow O_EXCL, as that has a special meaning for O_TMPFILE */
+        assert((flags & O_EXCL) == 0);
+
+        /* Creates a temporary file, that shall be renamed to "target" later. If possible, this uses O_TMPFILE â€“ in
+         * which case "ret_path" will be returned as NULL. If not possible a the tempoary path name used is returned in
+         * "ret_path". Use link_tmpfile() below to rename the result after writing the file in full. */
+
+        fd = open_parent(target, O_TMPFILE|flags, 0640);
+        if (fd >= 0) {
+                *ret_path = NULL;
+                return fd;
+        }
+
+        log_debug_errno(fd, "Failed to use O_TMPFILE for %s: %m", target);
+
+        r = tempfn_random(target, NULL, &tmp);
+        if (r < 0)
+                return r;
+
+        fd = open(tmp, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|flags, 0640);
+        if (fd < 0)
+                return -errno;
+
+        *ret_path = TAKE_PTR(tmp);
+
+        return fd;
+}
+
+int link_tmpfile(int fd, const char *path, const char *target) {
+        int r;
+
+        assert(fd >= 0);
+        assert(target);
+
+        /* Moves a temporary file created with open_tmpfile() above into its final place. if "path" is NULL an fd
+         * created with O_TMPFILE is assumed, and linkat() is used. Otherwise it is assumed O_TMPFILE is not supported
+         * on the directory, and renameat2() is used instead.
+         *
+         * Note that in both cases we will not replace existing files. This is because linkat() does not support this
+         * operation currently (renameat2() does), and there is no nice way to emulate this. */
+
+        if (path) {
+                r = rename_noreplace(AT_FDCWD, path, AT_FDCWD, target);
+                if (r < 0)
+                        return r;
+        } else {
+                char proc_fd_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(fd) + 1];
+
+                xsprintf(proc_fd_path, "/proc/self/fd/%i", fd);
+
+                if (linkat(AT_FDCWD, proc_fd_path, AT_FDCWD, target, AT_SYMLINK_FOLLOW) < 0)
+                        return -errno;
+        }
+
+        return 0;
+}
+
+int mkdtemp_malloc(const char *template, char **ret) {
+        _cleanup_free_ char *p = NULL;
+        int r;
+
+        assert(ret);
+
+        if (template)
+                p = strdup(template);
+        else {
+                const char *tmp;
+
+                r = tmp_dir(&tmp);
+                if (r < 0)
+                        return r;
+
+                p = strjoin(tmp, "/XXXXXX");
+        }
+        if (!p)
+                return -ENOMEM;
+
+        if (!mkdtemp(p))
+                return -errno;
+
+        *ret = TAKE_PTR(p);
+        return 0;
+}
diff --git a/src/basic/tmpfile-util.h b/src/basic/tmpfile-util.h
new file mode 100644 (file)
index 0000000..802c85d
--- /dev/null
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+#pragma once
+
+#include <stdio.h>
+
+int fopen_temporary(const char *path, FILE **_f, char **_temp_path);
+int mkostemp_safe(char *pattern);
+int fmkostemp_safe(char *pattern, const char *mode, FILE**_f);
+
+int tempfn_xxxxxx(const char *p, const char *extra, char **ret);
+int tempfn_random(const char *p, const char *extra, char **ret);
+int tempfn_random_child(const char *p, const char *extra, char **ret);
+
+int open_tmpfile_unlinkable(const char *directory, int flags);
+int open_tmpfile_linkable(const char *target, int flags, char **ret_path);
+
+int link_tmpfile(int fd, const char *path, const char *target);
+
+int mkdtemp_malloc(const char *template, char **ret);
index 608b35b7158981ad7cfd94e68902161e50823b8e..2008341f631e1132ca6c008195625809cd917c1b 100644 (file)
@@ -38,6 +38,7 @@
 #include "string-util.h"
 #include "strv.h"
 #include "terminal-util.h"
+#include "tmpfile-util.h"
 #include "umask-util.h"
 #include "utf8.h"
 #include "util.h"
index d58f2eaacf45d3a7fa22b76ad5e3e708a41423c3..122b399d66805c4123268a83ca4918dd0f1ef75b 100644 (file)
@@ -48,6 +48,7 @@
 #include "string-util.h"
 #include "strv.h"
 #include "terminal-util.h"
+#include "tmpfile-util.h"
 #include "umask-util.h"
 #include "unit-name.h"
 #include "unit.h"
index 8c2cf7daa61bad6f5f13c36abc2a288b760e22ee..79d627c465cf28e3bf25a4412453a14af974091b 100644 (file)
@@ -46,6 +46,7 @@
 #include "string-table.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 #include "user-util.h"
 #include "util.h"
 
index de26bee931e6eda5958fe38c96e8ef682579fcf5..8fbe653f0cc9e49d6b848473183fff0a72c71e97 100644 (file)
@@ -17,7 +17,6 @@
 #include "compress.h"
 #include "def.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "journal-internal.h"
 #include "journal-util.h"
@@ -35,6 +34,7 @@
 #include "string-util.h"
 #include "strv.h"
 #include "terminal-util.h"
+#include "tmpfile-util.h"
 #include "user-util.h"
 #include "util.h"
 #include "verbs.h"
index e40fe60bfbb1f31dbfe8ef8ace4ebaaddde76c7e..7ee97501418a3c5d749c004380359ff5051ca817 100644 (file)
@@ -2,9 +2,9 @@
 
 #include "catalog.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "fuzz.h"
+#include "tmpfile-util.h"
 
 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
         _cleanup_(unlink_tempfilep) char name[] = "/tmp/fuzz-catalog.XXXXXX";
index 95415d9f8524eddb6f724a6261800c67c1fe6cf9..8e3e850fbb96fd35b93ff82c5205aa727bfc9486 100644 (file)
@@ -1,13 +1,13 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
-#include "fuzz.h"
 #include "fuzz-journald.h"
+#include "fuzz.h"
 #include "journald-native.h"
 #include "memfd-util.h"
 #include "process-util.h"
+#include "tmpfile-util.h"
 
 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
         Server s;
index 04ad129ca138a934c4fe87e3cb46296ee8b435cb..6a02b47a1745577ebb3415b7bffede7bcc28b579 100644 (file)
 #include "copy.h"
 #include "export-raw.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "import-common.h"
 #include "missing.h"
 #include "ratelimit.h"
 #include "stat-util.h"
 #include "string-util.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 #define COPY_BUFFER_SIZE (16*1024)
index 3aab390637a4c1e890cd58a7ea13e70d15f78d9e..ed546769f340c611eeb5882fa145f6b775424aaf 100644 (file)
@@ -6,11 +6,11 @@
 #include "btrfs-util.h"
 #include "export-tar.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "import-common.h"
 #include "process-util.h"
 #include "ratelimit.h"
 #include "string-util.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 #define COPY_BUFFER_SIZE (16*1024)
index 24cab484a20f88eaeb723331d187c114e5f7691c..89f03010d1349a972e9097f1b12b32d659d0936c 100644 (file)
@@ -16,6 +16,7 @@
 #include "os-util.h"
 #include "process-util.h"
 #include "signal-util.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 int import_make_read_only_fd(int fd) {
index bc2e631e7bd7c626f2fc9d67b490bb7a33e81293..35ba6ba382cea98f36eb048dfc6ece50a2e6db61 100644 (file)
@@ -5,18 +5,18 @@
 #include "alloc-util.h"
 #include "btrfs-util.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "hostname-util.h"
 #include "import-common.h"
 #include "import-util.h"
 #include "machine-image.h"
 #include "mkdir.h"
+#include "parse-util.h"
 #include "ratelimit.h"
 #include "rm-rf.h"
 #include "string-util.h"
+#include "tmpfile-util.h"
 #include "verbs.h"
-#include "parse-util.h"
 
 static bool arg_force = false;
 static bool arg_read_only = false;
index 2b96330c1499ff433affc4d31e1138cd88e07f43..4b1161557ddc28656428415b35bcb16d2581d9fe 100644 (file)
@@ -10,7 +10,6 @@
 #include "chattr-util.h"
 #include "copy.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "hostname-util.h"
 #include "import-common.h"
@@ -24,6 +23,7 @@
 #include "ratelimit.h"
 #include "rm-rf.h"
 #include "string-util.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 struct RawImport {
index 0bb086f1429d354d4e14491649812f060ff76e91..4d0d9254d0125888f11482fa160f9d167b38fd05 100644 (file)
@@ -24,6 +24,7 @@
 #include "ratelimit.h"
 #include "rm-rf.h"
 #include "string-util.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 struct TarImport {
index 39d1747f7d1a3df7e1f59c0a0b00ab7a072f95db..3a3e015df8faad76c45c92e3740f0b7b2af464d5 100644 (file)
@@ -12,7 +12,6 @@
 #include "copy.h"
 #include "curl-util.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "hostname-util.h"
 #include "import-common.h"
@@ -27,6 +26,7 @@
 #include "rm-rf.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 #include "utf8.h"
 #include "util.h"
 #include "web-util.h"
index b19d4ea39105524516837be542f4491a1021aabc..e7a208e9048caa671b4dbc4663a633ec55ab436d 100644 (file)
@@ -10,7 +10,6 @@
 #include "copy.h"
 #include "curl-util.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "hostname-util.h"
 #include "import-common.h"
@@ -25,6 +24,7 @@
 #include "rm-rf.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 #include "utf8.h"
 #include "util.h"
 #include "web-util.h"
index 91e25a87ab28b5662612d4a90cf6cd5a596e13e4..69bc447920ec21c1c67c1faa9e743c9133467f4a 100644 (file)
@@ -23,6 +23,7 @@
 #include "parse-util.h"
 #include "pretty-print.h"
 #include "sigbus.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 #define JOURNAL_WAIT_TIMEOUT (10*USEC_PER_SEC)
index 924ea4936e6be35247d9f43268ec28b9e5d5ec00..e2cb76141f15bbce491c3dca6aec5da5a7a2b52a 100644 (file)
@@ -26,6 +26,7 @@
 #include "signal-util.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 #define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-upload.pem"
index 0f18d31addf26afab754fd14bb42ec167fcd1da3..e4ca959bd643bb7267d516cd8f5b5f098b4da1d5 100644 (file)
@@ -24,6 +24,7 @@
 #include "strbuf.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 const char * const catalog_file_dirs[] = {
index a0621524a9bfcec8fb1f9130d344feaa0a03c17c..87056435fcdd9973237e1f8c230884b4a5057975 100644 (file)
 
 #include "alloc-util.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "io-util.h"
 #include "memfd-util.h"
 #include "socket-util.h"
 #include "stdio-util.h"
 #include "string-util.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 #define SNDBUF_SIZE (8*1024*1024)
index 5569ddc31368650f008eee4df508bb9d3b80232b..5eff80a99f01737d9a2b5a1365cb75e1f2f4c0d7 100644 (file)
@@ -17,6 +17,7 @@
 #include "lookup3.h"
 #include "macro.h"
 #include "terminal-util.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 static void draw_progress(uint64_t p, usec_t *last_usec) {
index 1d3766d971168475b17ca5c3524b20f9b45a596d..14a02eda749b4ec9549ab6bd067f16d90fe32d36 100644 (file)
@@ -62,6 +62,7 @@
 #include "strv.h"
 #include "syslog-util.h"
 #include "terminal-util.h"
+#include "tmpfile-util.h"
 #include "unit-name.h"
 #include "user-util.h"
 
index ebe49850f53f6c7dfdfd9dfacec49fd71c42d8ba..7283001158d542c69f741b4075daf3a08e40bf5d 100644 (file)
@@ -31,6 +31,7 @@
 #include "stdio-util.h"
 #include "string-util.h"
 #include "syslog-util.h"
+#include "tmpfile-util.h"
 #include "unit-name.h"
 
 #define STDOUT_STREAMS_MAX 4096
index e9a751bdfd75ddd307f13ed3d6f4e32b822ce396..192bb0cb07e0bac67847069f21870ebd0dd87659 100644 (file)
 #include "catalog.h"
 #include "fd-util.h"
 #include "fs-util.h"
-#include "fileio.h"
 #include "log.h"
 #include "macro.h"
 #include "path-util.h"
 #include "string-util.h"
 #include "strv.h"
 #include "tests.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 static char** catalog_dirs = NULL;
index bf35a5f4b937b282ab182dd3437edd67fd42465f..1b050b70523d4f74cb89e9a32a7061c137275567 100644 (file)
@@ -7,12 +7,12 @@
 #include "alloc-util.h"
 #include "compress.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "macro.h"
 #include "path-util.h"
 #include "random-util.h"
 #include "tests.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 #if HAVE_XZ
index 5c55b35f57912b75f4dd4b541762199c29d3aecf..8f755efdde27f5f98e48f6ffd7af86ec1d3c4561 100644 (file)
@@ -6,9 +6,9 @@
 #include <unistd.h>
 
 #include "fd-util.h"
-#include "fileio.h"
 #include "macro.h"
 #include "mmap-cache.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 int main(int argc, char *argv[]) {
index c2455c7133b54494938c9acc6cb56b8077be8f3b..1c6efc5563d128618e05c66c7d01e57c494dd6e0 100644 (file)
@@ -26,6 +26,7 @@
 #include "stdio-util.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 #include "unaligned.h"
 
 int sd_dhcp_lease_get_address(sd_dhcp_lease *lease, struct in_addr *addr) {
index 40879b85375a623ce8e522822724024f65a09d35..4b3da30079764cf66ea5500b120a74530e0cf6b1 100644 (file)
@@ -8,7 +8,6 @@
 
 #include "alloc-util.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "mkdir.h"
 #include "path-util.h"
@@ -16,6 +15,7 @@
 #include "rm-rf.h"
 #include "socket-util.h"
 #include "string-util.h"
+#include "tmpfile-util.h"
 
 static int method_foobar(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
         log_info("Got Foobar() call.");
index 8cc216c353985eacbf811738ee1bfb692a0f25ff..95a581e66efdcb12481bdb54bb344c059b8124a5 100644 (file)
@@ -24,6 +24,7 @@
 #include "string-util.h"
 #include "strv.h"
 #include "strxcpyx.h"
+#include "tmpfile-util.h"
 #include "user-util.h"
 #include "util.h"
 
index 50074a56be81fa9f974eb6c8312252691e9135e0..6fd6d9f0b041fb81005e5a61cfd93a9e9b45712c 100644 (file)
@@ -6,7 +6,6 @@
 
 #include "alloc-util.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "log.h"
 #include "macro.h"
@@ -17,6 +16,7 @@
 #include "stdio-util.h"
 #include "string-util.h"
 #include "tests.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 static int prepare_handler(sd_event_source *s, void *userdata) {
index 87259daadc78b9a6a98929ee9b1e01d51ce3c33b..c5c329f2ac6c7538172a7541059188cff40b70db 100644 (file)
@@ -16,6 +16,7 @@
 #include "strbuf.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 
 static const char *default_hwdb_bin_dir = "/etc/udev";
 static const char * const conf_file_dirs[] = {
index 0bf62df22e1322849b7e557cd6d7616ca872cfe0..5a84438fbd84c2f4a8ab444cbc225716d01b8966 100644 (file)
@@ -17,6 +17,7 @@
 #include "mkdir.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 
 static bool startswith_comma(const char *s, const char *prefix) {
         s = startswith(s, prefix);
index 0858fbd96c37c96800d889a253bebe4cde07a8da..0e441b5631f15ec41f1a196b58fcdf6905131832 100644 (file)
@@ -33,6 +33,7 @@
 #include "special.h"
 #include "strv.h"
 #include "terminal-util.h"
+#include "tmpfile-util.h"
 #include "unit-name.h"
 #include "user-util.h"
 #include "utmp-wtmp.h"
index 71eea72da59fe8101e7532272fff80d7b473c032..dc9e4d93dc354fd96e95f1d1e5b173956d4648a8 100644 (file)
@@ -15,6 +15,7 @@
 #include "parse-util.h"
 #include "string-table.h"
 #include "string-util.h"
+#include "tmpfile-util.h"
 #include "user-util.h"
 #include "util.h"
 
index 3b06169c1de2bde15139641a18dd499dbdebee8b..c758ffd5fad0a0130de29cf880b923274af3820b 100644 (file)
@@ -19,6 +19,7 @@
 #include "stdio-util.h"
 #include "string-util.h"
 #include "terminal-util.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 int seat_new(Seat** ret, Manager *m, const char *id) {
index 576a17a7f031dd58d9bdb938f7ba3ccf9eda6e7a..676ba37db83cf9239b7247bf3ddbd46ddcfbee00 100644 (file)
@@ -30,6 +30,7 @@
 #include "string-table.h"
 #include "strv.h"
 #include "terminal-util.h"
+#include "tmpfile-util.h"
 #include "user-util.h"
 #include "util.h"
 
index 8d8294461876241e5d4917b3efe2e60c6e68eb49..9f4835a8df51100f22d1d0f2687436fdef1015b3 100644 (file)
@@ -28,6 +28,7 @@
 #include "stdio-util.h"
 #include "string-table.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 #include "unit-name.h"
 #include "user-util.h"
 #include "util.h"
index da861c536cdb8ed84904ffe958d5e42c8133a3ee..c9580d99bef7076b13c045109d053b783c2534ee 100644 (file)
@@ -34,6 +34,7 @@
 #include "signal-util.h"
 #include "strv.h"
 #include "terminal-util.h"
+#include "tmpfile-util.h"
 #include "user-util.h"
 
 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class, machine_class, MachineClass);
index e114541b3f0c078daed9584f401d6483094bc390..1120cd577b6dfb17e4a7415e4e88d4d8a2763b9e 100644 (file)
@@ -26,6 +26,7 @@
 #include "stdio-util.h"
 #include "string-table.h"
 #include "terminal-util.h"
+#include "tmpfile-util.h"
 #include "unit-name.h"
 #include "user-util.h"
 #include "util.h"
index 453ca30b64a7261998d1ab068634bae5ef8df89e..64fa5f03449345da2efe060ade08f1cbd7a4e48a 100644 (file)
@@ -25,6 +25,7 @@
 #include "process-util.h"
 #include "stdio-util.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 #include "unit-name.h"
 #include "user-util.h"
 
index 207d67874f00c110901ed5ebf4fe5fc51e27cc80..37f7cf8ccdbbc6dd30030ed014b022df541cb9ec 100644 (file)
@@ -1,10 +1,10 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "fuzz.h"
 #include "networkd-manager.h"
+#include "tmpfile-util.h"
 
 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
         _cleanup_(manager_freep) Manager *manager = NULL;
index 078b1e6d93c230087b834d66b3269d4735bcea62..4b795008055d47ec9b3cdb53b879c026f3d0fb68 100644 (file)
@@ -1,10 +1,10 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "fuzz.h"
 #include "networkd-manager.h"
+#include "tmpfile-util.h"
 
 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
         _cleanup_(manager_freep) Manager *manager = NULL;
index db2dbef52c76fc77986a0eaec0b992180620ccc4..45908340ed9ce4e651e32469cd424f70a8cb52a7 100644 (file)
@@ -25,6 +25,7 @@
 #include "stdio-util.h"
 #include "string-table.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 #include "util.h"
 #include "virt.h"
 
index 3b1ae0b41fcf60371d7d39d2889e15953ed0e30a..2a69e7b0a29339dc779198748a6f7650322baae7 100644 (file)
@@ -23,6 +23,7 @@
 #include "path-util.h"
 #include "set.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 #include "virt.h"
 
 /* use 8 MB for receive socket kernel queue. */
index d112471587752eb5966f86186292904d804de581..f80f3c09a90f0b5f1c03165b34ef18c45c4c3d44 100644 (file)
@@ -10,6 +10,7 @@
 #include "networkd-manager.h"
 #include "string-util.h"
 #include "tests.h"
+#include "tmpfile-util.h"
 
 static void test_rule_serialization(const char *title, const char *ruleset, const char *expected) {
         char pattern[] = "/tmp/systemd-test-routing-policy-rule.XXXXXX",
index 7fe73211e8ca94aa1cc0cf8fc1406c1ea7ca092b..a9af889747b838e730c3b2c4f1526debf02d2c5c 100644 (file)
@@ -6,7 +6,6 @@
 #include "alloc-util.h"
 #include "escape.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "label.h"
 #include "mkdir.h"
@@ -20,6 +19,7 @@
 #include "stat-util.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 #include "user-util.h"
 #include "util.h"
 
index 8c6a74bea21975d93f505383c4d31f07d04d310e..0fb42d1a74779d42e2c6cb716fb9c8f006e8f37b 100644 (file)
@@ -93,6 +93,7 @@
 #include "string-util.h"
 #include "strv.h"
 #include "terminal-util.h"
+#include "tmpfile-util.h"
 #include "umask-util.h"
 #include "user-util.h"
 #include "util.h"
index ca8043b41eeca0da4821fd34c4358061812c3807..17a3e23ae013113d1471022f224762bab687f639 100644 (file)
@@ -26,6 +26,7 @@
 #include "socket-util.h"
 #include "string-table.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 #include "user-util.h"
 
 static const char profile_dirs[] = CONF_PATHS_NULSTR("systemd/portable/profile");
index 0ae3b400181c862d4303ac7503c12f988679f9a9..bff5277b2dc289f12722ceb7f81d0167bf4d6e49 100644 (file)
@@ -16,6 +16,7 @@
 #include "resolved-mdns.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 
 int link_new(Manager *m, Link **ret, int ifindex) {
         _cleanup_(link_freep) Link *l = NULL;
index 42225572f251206b68d38758d2f10d904363b1ed..ad47d13d238891f452bed617975bb3cd997e5f57 100644 (file)
@@ -13,6 +13,7 @@
 #include "resolved-resolv-conf.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tmpfile-util-label.h"
 
 /* A resolv.conf file containing the DNS server and domain data we learnt from uplink, i.e. the full uplink data */
 #define PRIVATE_UPLINK_RESOLV_CONF "/run/systemd/resolve/resolv.conf"
index 1b22e3820233354bc616ab58d7eeb74b16239ac5..da10391d74eec6e35ad1d8e61fed7cf4aea9bae5 100644 (file)
@@ -1,10 +1,10 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "log.h"
 #include "resolved-etc-hosts.h"
+#include "tmpfile-util.h"
 
 static void test_parse_etc_hosts_system(void) {
         _cleanup_fclose_ FILE *f = NULL;
index 37441a8f5b0883972a08a2d32f55e34ac1d7d129..246e27a13579457b6ed82ac12b521e5961f6b84e 100644 (file)
@@ -41,6 +41,7 @@
 #include "strv.h"
 #include "terminal-util.h"
 #include "time-util.h"
+#include "tmpfile-util.h"
 #include "umask-util.h"
 #include "utf8.h"
 #include "util.h"
index ab956ede9e8413f4f5eae570d03a446112921887..b234ba15a22529795c96fa110400ffab49fabdce 100644 (file)
@@ -26,8 +26,8 @@
 #include "id128-util.h"
 #include "linux-3.13/dm-ioctl.h"
 #include "missing.h"
-#include "mountpoint-util.h"
 #include "mount-util.h"
+#include "mountpoint-util.h"
 #include "os-util.h"
 #include "path-util.h"
 #include "process-util.h"
@@ -38,6 +38,7 @@
 #include "string-table.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 #include "user-util.h"
 #include "xattr-util.h"
 
index 10d774dfcdfbd36908abfff40295adff5ae27703..972231d6876bdde0bca46a5d4a2304c277f70b9c 100644 (file)
@@ -23,6 +23,7 @@
 #include "string-util.h"
 #include "strv.h"
 #include "terminal-util.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 /* Put this test here for a lack of better place */
index b5362b5cbc7c53621a0515da23883842b982924f..22df9c937905fa4e8315e238108a3e50bc5e3cf6 100644 (file)
@@ -34,21 +34,6 @@ int write_env_file_label(const char *fname, char **l) {
         return r;
 }
 
-int fopen_temporary_label(const char *target,
-                          const char *path, FILE **f, char **temp_path) {
-        int r;
-
-        r = mac_selinux_create_file_prepare(target, S_IFREG);
-        if (r < 0)
-                return r;
-
-        r = fopen_temporary(path, f, temp_path);
-
-        mac_selinux_create_file_clear();
-
-        return r;
-}
-
 int create_shutdown_run_nologin_or_warn(void) {
         int r;
 
index 2347753fe4f8ee7a297806ceace7e3664a39a8d4..2ef2c541deaf64d8749b977ae8631a69a8263811 100644 (file)
@@ -12,6 +12,5 @@ static inline int write_string_file_atomic_label(const char *fn, const char *lin
         return write_string_file_atomic_label_ts(fn, line, NULL);
 }
 int write_env_file_label(const char *fname, char **l);
-int fopen_temporary_label(const char *target, const char *path, FILE **f, char **temp_path);
 
 int create_shutdown_run_nologin_or_warn(void);
index 85c546eb5bb8a9fd678cae62b4479bad1d454954..4971ce2d72a61786b57fb9798457afb02d8f3395 100644 (file)
@@ -139,6 +139,8 @@ shared_sources = files('''
         switch-root.h
         sysctl-util.c
         sysctl-util.h
+        tmpfile-util-label.c
+        tmpfile-util-label.h
         tomoyo-util.c
         tomoyo-util.h
         udev-util.c
index dc06502c8118e4ad744066bbed2b1a1d44544cef..442fde7b2d2db732158a0e0974c7183b091f3b34 100644 (file)
@@ -6,7 +6,6 @@
 #include <string.h>
 
 #include "alloc-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "install.h"
 #include "log.h"
@@ -18,6 +17,7 @@
 #include "stat-util.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 #include "user-util.h"
 #include "util.h"
 
diff --git a/src/shared/tmpfile-util-label.c b/src/shared/tmpfile-util-label.c
new file mode 100644 (file)
index 0000000..c12d7c1
--- /dev/null
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+#include <sys/stat.h>
+
+#include "selinux-util.h"
+#include "tmpfile-util-label.h"
+#include "tmpfile-util.h"
+
+int fopen_temporary_label(
+                const char *target,
+                const char *path,
+                FILE **f,
+                char **temp_path) {
+
+        int r;
+
+        r = mac_selinux_create_file_prepare(target, S_IFREG);
+        if (r < 0)
+                return r;
+
+        r = fopen_temporary(path, f, temp_path);
+
+        mac_selinux_create_file_clear();
+
+        return r;
+}
diff --git a/src/shared/tmpfile-util-label.h b/src/shared/tmpfile-util-label.h
new file mode 100644 (file)
index 0000000..97a8751
--- /dev/null
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+#pragma once
+
+#include <stdio.h>
+
+/* These functions are split out of tmpfile-util.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 fopen_temporary_label(const char *target, const char *path, FILE **f, char **temp_path);
index 87ae4eb5d76a748a43101a91f6b91bddbdb83da1..7f289ad374edcc794daccd020332aaf7ca2652d4 100644 (file)
@@ -37,7 +37,6 @@
 #include "escape.h"
 #include "exit-status.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "format-util.h"
 #include "fs-util.h"
 #include "glob-util.h"
@@ -73,6 +72,7 @@
 #include "string-table.h"
 #include "strv.h"
 #include "terminal-util.h"
+#include "tmpfile-util.h"
 #include "unit-def.h"
 #include "unit-name.h"
 #include "user-util.h"
index d0ab5a9b6ce686cc497166ebbdb9b46d0850f531..384f89e5f5bb11f455896f450146a2314f215be4 100644 (file)
@@ -20,6 +20,7 @@
 #include "specifier.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tmpfile-util-label.h"
 #include "uid-range.h"
 #include "user-util.h"
 #include "utf8.h"
index a8d413533e43f2b9d4d572bd2a6b6d47c326e937..df879747f5643871235d57fc2c65a51c352cf689 100644 (file)
@@ -7,8 +7,8 @@
 
 #include "acl-util.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "string-util.h"
+#include "tmpfile-util.h"
 #include "user-util.h"
 
 static void test_add_acls_for_user(void) {
index 183f2be234cfdf59ba052181289f5cd4643747b6..4f5307889ea5c5e1049d59214f3883e62450b904 100644 (file)
@@ -3,8 +3,8 @@
 #include <unistd.h>
 
 #include "async.h"
-#include "fileio.h"
 #include "macro.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 static bool test_async = false;
index f635b4ff92aa5f2d76cb1aa2ae067e6876f428b1..305d44f568b001f01fd3a1f2ca9919e12d0199a8 100644 (file)
@@ -4,11 +4,11 @@
 
 #include "alloc-util.h"
 #include "chown-recursive.h"
-#include "fileio.h"
 #include "log.h"
 #include "rm-rf.h"
 #include "string-util.h"
 #include "tests.h"
+#include "tmpfile-util.h"
 
 static const uint8_t acl[] = {
         0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x07, 0x00,
index f5b9a727271e9255bd4dcf26377db26ddb30a4ce..018e679b4544962ddfe69022838a1439961e30d1 100644 (file)
@@ -12,6 +12,7 @@
 #include "fs-util.h"
 #include "log.h"
 #include "macro.h"
+#include "tmpfile-util.h"
 
 static void test_clock_is_localtime(void) {
         _cleanup_(unlink_tempfilep) char adjtime[] = "/tmp/test-adjtime.XXXXXX";
index 497becff731c62df97781fde9c1a3b5a1affdccb..2921338f62a8a52a53ecdca55fc14844ab7e5f14 100644 (file)
@@ -2,12 +2,12 @@
 
 #include "conf-parser.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "log.h"
 #include "macro.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 static void test_config_parse_path_one(const char *rvalue, const char *expected) {
index 4e265374ab116548305e565cba9e2b7365f2c95e..b17a1c53fe67d3509a02f93ef163bd346ba6d0ea 100644 (file)
@@ -15,6 +15,7 @@
 #include "string-util.h"
 #include "strv.h"
 #include "tests.h"
+#include "tmpfile-util.h"
 #include "user-util.h"
 #include "util.h"
 
index 523cfe43b13c6bcf7322b8338a28560da979f066..9414ea6c3e708f141e9fd59c09a1e49c399e124e 100644 (file)
@@ -2,10 +2,10 @@
 
 #include "capability-util.h"
 #include "dev-setup.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "path-util.h"
 #include "rm-rf.h"
+#include "tmpfile-util.h"
 
 int main(int argc, char *argv[]) {
         _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
index 157dc88320789b43c9f4838b191924da3c3d0e9b..a9ecfb6bfe66fda8892fc1d0c0e1dbf6632967d7 100644 (file)
@@ -11,8 +11,9 @@
 #include "process-util.h"
 #include "random-util.h"
 #include "string-util.h"
-#include "util.h"
 #include "tests.h"
+#include "tmpfile-util.h"
+#include "util.h"
 
 static void test_close_many(void) {
         int fds[3];
index 7d68a6b966f6ffaae909b1c183c3c9d7988fe741..fb9d3975695cf5f2aca2ffbb8396a9b55ba5c298 100644 (file)
@@ -5,8 +5,8 @@
 
 #include "fd-util.h"
 #include "fdset.h"
-#include "fileio.h"
 #include "macro.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 static void test_fdset_new_fill(void) {
index de95a1433f75105b8271f00829b3d38528ed8e56..da536fe5ab3e926165e4ec0b0f424c74c317c2ff 100644 (file)
@@ -16,6 +16,7 @@
 #include "string-util.h"
 #include "strv.h"
 #include "tests.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 static void test_parse_env_file(void) {
index d5f652c938395d3c72e7af72e49a9c7c49aa49c0..4962471141a70d561d58310265bf88d613e0dd0f 100644 (file)
@@ -5,7 +5,6 @@
 #include "alloc-util.h"
 #include "fd-util.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "id128-util.h"
 #include "macro.h"
@@ -16,6 +15,7 @@
 #include "string-util.h"
 #include "strv.h"
 #include "tests.h"
+#include "tmpfile-util.h"
 #include "user-util.h"
 #include "util.h"
 #include "virt.h"
index d78d6223c05f5b8443ca811cf67ae824ce3deaff..b4f41445fe22d65446e6be3e043f5f00607829d2 100644 (file)
@@ -7,11 +7,11 @@
 
 #include "alloc-util.h"
 #include "dirent-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "glob-util.h"
 #include "macro.h"
 #include "rm-rf.h"
+#include "tmpfile-util.h"
 
 static void test_glob_exists(void) {
         char name[] = "/tmp/test-glob_exists.XXXXXX";
index 194a640eee55218b8b50f34637475a0d46d23dca..8ba47d316baf527b0c37fd900542e1ec830a9b36 100644 (file)
@@ -4,6 +4,7 @@
 #include "fileio.h"
 #include "hostname-util.h"
 #include "string-util.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 static void test_hostname_is_valid(void) {
index e3d07a69f3c64d2f7db39df2db81a23c9e6be579..ec50e057a3655c38eed232a43409ce98302c259e 100644 (file)
@@ -7,10 +7,10 @@
 
 #include "alloc-util.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "id128-util.h"
 #include "macro.h"
 #include "string-util.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 #define ID128_WALDI SD_ID128_MAKE(01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 10)
index e306b1f1d63782a6a830d98ac5de8b8d4f960cd1..a57d5db2b10c4242082f3a24bf906731d9192c7f 100644 (file)
@@ -8,6 +8,7 @@
 #include "serialize.h"
 #include "strv.h"
 #include "tests.h"
+#include "tmpfile-util.h"
 
 char long_string[LONG_LINE_MAX+1];
 
index 349d866442adf7db8057972261b501fa36d3e719..f902f500e0cc349052a0572bb88ce58498bb94b1 100644 (file)
@@ -9,7 +9,6 @@
 #include "escape.h"
 #include "exit-status.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "in-addr-util.h"
 #include "io-util.h"
 #include "log.h"
@@ -17,8 +16,9 @@
 #include "process-util.h"
 #include "socket-util.h"
 #include "string-util.h"
-#include "util.h"
 #include "tests.h"
+#include "tmpfile-util.h"
+#include "util.h"
 
 static void test_ifname_valid(void) {
         log_info("/* %s */", __func__);
index 3487ac785d2681e4bdd944b3ba9952c64e7e7535..d16fdd90d1bc2898bad5a31198831b3923515769 100644 (file)
@@ -6,12 +6,12 @@
 
 #include "alloc-util.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "macro.h"
 #include "missing.h"
 #include "mountpoint-util.h"
-#include "stat-util.h"
 #include "path-util.h"
+#include "stat-util.h"
+#include "tmpfile-util.h"
 
 static void test_files_same(void) {
         _cleanup_close_ int fd = -1;
index 7edf7681c25f7db8b252c687da6ff1c4d33d760c..958d3694303d5480d36a65ae017dfa205d8c29ca 100644 (file)
@@ -5,11 +5,11 @@
 
 #include "alloc-util.h"
 #include "fd-util.h"
-#include "fileio.h"
-#include "tests.h"
 #include "macro.h"
 #include "strv.h"
 #include "terminal-util.h"
+#include "tests.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 static void test_default_term_for_tty(void) {
index 7f288e54ceff72d86e3fa0d6e8e6370d399dd3cc..b5268716554bbc981975a2c7b2ad2d2a6a2f3809 100644 (file)
@@ -14,6 +14,7 @@
 #include "process-util.h"
 #include "string-util.h"
 #include "tests.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 int main(int argc, char** argv) {
index 493ca50220a366d50d3e788551e0888c57ccb1df..b1aa3871104c8169fa16bc771b8c9ce337448584 100644 (file)
@@ -7,8 +7,8 @@
 #include <sys/capability.h>
 #include <unistd.h>
 
-#include "alloc-util.h"
 #include "all-units.h"
+#include "alloc-util.h"
 #include "capability-util.h"
 #include "conf-parser.h"
 #include "fd-util.h"
@@ -26,6 +26,7 @@
 #include "strv.h"
 #include "test-helper.h"
 #include "tests.h"
+#include "tmpfile-util.h"
 #include "user-util.h"
 #include "util.h"
 
index 2d93e65be21aea37ac69e7e9ca13ae321f8c08b3..3e6df96c5df5f51917b015990be365f04845f658 100644 (file)
@@ -8,11 +8,11 @@
 
 #include "alloc-util.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "macro.h"
 #include "string-util.h"
 #include "tests.h"
+#include "tmpfile-util.h"
 #include "xattr-util.h"
 
 static void test_fgetxattrat_fake(void) {
index a79185a59ae2a26133c898bdfc007a0cddc5effa..e6344815240d858c330475d7f5cef7e2ed7a1e77 100644 (file)
@@ -15,6 +15,7 @@
 #include "clock-util.h"
 #include "def.h"
 #include "fileio-label.h"
+#include "fileio.h"
 #include "fs-util.h"
 #include "hashmap.h"
 #include "list.h"