Blame shared/systemd/src/basic/fileio.c

Packit Service 87a54e
/* SPDX-License-Identifier: LGPL-2.1-or-later */
Packit 5756e2
Packit 5756e2
#include "nm-sd-adapt-shared.h"
Packit 5756e2
Packit 5756e2
#include <ctype.h>
Packit 5756e2
#include <errno.h>
Packit 5756e2
#include <fcntl.h>
Packit 5756e2
#include <limits.h>
Packit 5756e2
#include <stdarg.h>
Packit 5756e2
#include <stdint.h>
Packit 5756e2
#include <stdio_ext.h>
Packit 5756e2
#include <stdlib.h>
Packit 5756e2
#include <sys/stat.h>
Packit 5756e2
#include <sys/types.h>
Packit 5756e2
#include <unistd.h>
Packit 5756e2
Packit 5756e2
#include "alloc-util.h"
Packit 5756e2
#include "fd-util.h"
Packit 5756e2
#include "fileio.h"
Packit 5756e2
#include "fs-util.h"
Packit 5756e2
#include "hexdecoct.h"
Packit 5756e2
#include "log.h"
Packit 5756e2
#include "macro.h"
Packit 5756e2
#include "mkdir.h"
Packit 5756e2
#include "parse-util.h"
Packit 5756e2
#include "path-util.h"
Packit 5756e2
#include "socket-util.h"
Packit 5756e2
#include "stdio-util.h"
Packit 5756e2
#include "string-util.h"
Packit 5756e2
#include "tmpfile-util.h"
Packit 5756e2
Packit 5756e2
#define READ_FULL_BYTES_MAX (4U*1024U*1024U)
Packit 5756e2
Packit 5756e2
int fopen_unlocked(const char *path, const char *options, FILE **ret) {
Packit 5756e2
        assert(ret);
Packit 5756e2
Packit 5756e2
        FILE *f = fopen(path, options);
Packit 5756e2
        if (!f)
Packit 5756e2
                return -errno;
Packit 5756e2
Packit 5756e2
        (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
Packit 5756e2
Packit 5756e2
        *ret = f;
Packit 5756e2
        return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int fdopen_unlocked(int fd, const char *options, FILE **ret) {
Packit 5756e2
        assert(ret);
Packit 5756e2
Packit 5756e2
        FILE *f = fdopen(fd, options);
Packit 5756e2
        if (!f)
Packit 5756e2
                return -errno;
Packit 5756e2
Packit 5756e2
        (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
Packit 5756e2
Packit 5756e2
        *ret = f;
Packit 5756e2
        return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int take_fdopen_unlocked(int *fd, const char *options, FILE **ret) {
Packit 5756e2
        int     r;
Packit 5756e2
Packit 5756e2
        assert(fd);
Packit 5756e2
Packit 5756e2
        r = fdopen_unlocked(*fd, options, ret);
Packit 5756e2
        if (r < 0)
Packit 5756e2
                return r;
Packit 5756e2
Packit 5756e2
        *fd = -1;
Packit 5756e2
Packit 5756e2
        return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
FILE* take_fdopen(int *fd, const char *options) {
Packit 5756e2
        assert(fd);
Packit 5756e2
Packit 5756e2
        FILE *f = fdopen(*fd, options);
Packit 5756e2
        if (!f)
Packit 5756e2
                return NULL;
Packit 5756e2
Packit 5756e2
        *fd = -1;
Packit 5756e2
Packit 5756e2
        return f;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
DIR* take_fdopendir(int *dfd) {
Packit 5756e2
        assert(dfd);
Packit 5756e2
Packit 5756e2
        DIR *d = fdopendir(*dfd);
Packit 5756e2
        if (!d)
Packit 5756e2
                return NULL;
Packit 5756e2
Packit 5756e2
        *dfd = -1;
Packit 5756e2
Packit 5756e2
        return d;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc) {
Packit 5756e2
        FILE *f = open_memstream(ptr, sizeloc);
Packit 5756e2
        if (!f)
Packit 5756e2
                return NULL;
Packit 5756e2
Packit 5756e2
        (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
Packit 5756e2
Packit 5756e2
        return f;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode) {
Packit 5756e2
        FILE *f = fmemopen(buf, size, mode);
Packit 5756e2
        if (!f)
Packit 5756e2
                return NULL;
Packit 5756e2
Packit 5756e2
        (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
Packit 5756e2
Packit 5756e2
        return f;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
#if 0 /* NM_IGNORED */
Packit 5756e2
int write_string_stream_ts(
Packit 5756e2
                FILE *f,
Packit 5756e2
                const char *line,
Packit 5756e2
                WriteStringFileFlags flags,
Packit Service 87a54e
                const struct timespec *ts) {
Packit 5756e2
Packit 5756e2
        bool needs_nl;
Packit 5756e2
        int r, fd;
Packit 5756e2
Packit 5756e2
        assert(f);
Packit 5756e2
        assert(line);
Packit 5756e2
Packit 5756e2
        if (ferror(f))
Packit 5756e2
                return -EIO;
Packit 5756e2
Packit 5756e2
        if (ts) {
Packit 5756e2
                /* If we shall set the timestamp we need the fd. But fmemopen() streams generally don't have
Packit 5756e2
                 * an fd. Let's fail early in that case. */
Packit 5756e2
                fd = fileno(f);
Packit 5756e2
                if (fd < 0)
Packit 5756e2
                        return -EBADF;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        needs_nl = !(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n");
Packit 5756e2
Packit 5756e2
        if (needs_nl && (flags & WRITE_STRING_FILE_DISABLE_BUFFER)) {
Packit 5756e2
                /* If STDIO buffering was disabled, then let's append the newline character to the string itself, so
Packit 5756e2
                 * that the write goes out in one go, instead of two */
Packit 5756e2
Packit 5756e2
                line = strjoina(line, "\n");
Packit 5756e2
                needs_nl = false;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        if (fputs(line, f) == EOF)
Packit 5756e2
                return -errno;
Packit 5756e2
Packit 5756e2
        if (needs_nl)
Packit 5756e2
                if (fputc('\n', f) == EOF)
Packit 5756e2
                        return -errno;
Packit 5756e2
Packit 5756e2
        if (flags & WRITE_STRING_FILE_SYNC)
Packit 5756e2
                r = fflush_sync_and_check(f);
Packit 5756e2
        else
Packit 5756e2
                r = fflush_and_check(f);
Packit 5756e2
        if (r < 0)
Packit 5756e2
                return r;
Packit 5756e2
Packit 5756e2
        if (ts) {
Packit Service 87a54e
                const struct timespec twice[2] = {*ts, *ts};
Packit 5756e2
Packit 5756e2
                if (futimens(fd, twice) < 0)
Packit 5756e2
                        return -errno;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static int write_string_file_atomic(
Packit 5756e2
                const char *fn,
Packit 5756e2
                const char *line,
Packit 5756e2
                WriteStringFileFlags flags,
Packit Service 87a54e
                const struct timespec *ts) {
Packit 5756e2
Packit 5756e2
        _cleanup_fclose_ FILE *f = NULL;
Packit 5756e2
        _cleanup_free_ char *p = NULL;
Packit 5756e2
        int r;
Packit 5756e2
Packit 5756e2
        assert(fn);
Packit 5756e2
        assert(line);
Packit 5756e2
Packit 5756e2
        /* Note that we'd really like to use O_TMPFILE here, but can't really, since we want replacement
Packit 5756e2
         * semantics here, and O_TMPFILE can't offer that. i.e. rename() replaces but linkat() doesn't. */
Packit 5756e2
Packit 5756e2
        r = fopen_temporary(fn, &f, &p);
Packit 5756e2
        if (r < 0)
Packit 5756e2
                return r;
Packit 5756e2
Packit 5756e2
        r = write_string_stream_ts(f, line, flags, ts);
Packit 5756e2
        if (r < 0)
Packit 5756e2
                goto fail;
Packit 5756e2
Packit 5756e2
        r = fchmod_umask(fileno(f), FLAGS_SET(flags, WRITE_STRING_FILE_MODE_0600) ? 0600 : 0644);
Packit 5756e2
        if (r < 0)
Packit 5756e2
                goto fail;
Packit 5756e2
Packit 5756e2
        if (rename(p, fn) < 0) {
Packit 5756e2
                r = -errno;
Packit 5756e2
                goto fail;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        if (FLAGS_SET(flags, WRITE_STRING_FILE_SYNC)) {
Packit 5756e2
                /* Sync the rename, too */
Packit 5756e2
                r = fsync_directory_of_file(fileno(f));
Packit 5756e2
                if (r < 0)
Packit 5756e2
                        return r;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        return 0;
Packit 5756e2
Packit 5756e2
fail:
Packit 5756e2
        (void) unlink(p);
Packit 5756e2
        return r;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int write_string_file_ts(
Packit 5756e2
                const char *fn,
Packit 5756e2
                const char *line,
Packit 5756e2
                WriteStringFileFlags flags,
Packit Service 87a54e
                const struct timespec *ts) {
Packit 5756e2
Packit 5756e2
        _cleanup_fclose_ FILE *f = NULL;
Packit 5756e2
        int q, r, fd;
Packit 5756e2
Packit 5756e2
        assert(fn);
Packit 5756e2
        assert(line);
Packit 5756e2
Packit 5756e2
        /* We don't know how to verify whether the file contents was already on-disk. */
Packit 5756e2
        assert(!((flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE) && (flags & WRITE_STRING_FILE_SYNC)));
Packit 5756e2
Packit 5756e2
        if (flags & WRITE_STRING_FILE_MKDIR_0755) {
Packit 5756e2
                r = mkdir_parents(fn, 0755);
Packit 5756e2
                if (r < 0)
Packit 5756e2
                        return r;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        if (flags & WRITE_STRING_FILE_ATOMIC) {
Packit 5756e2
                assert(flags & WRITE_STRING_FILE_CREATE);
Packit 5756e2
Packit 5756e2
                r = write_string_file_atomic(fn, line, flags, ts);
Packit 5756e2
                if (r < 0)
Packit 5756e2
                        goto fail;
Packit 5756e2
Packit 5756e2
                return r;
Packit 5756e2
        } else
Packit 5756e2
                assert(!ts);
Packit 5756e2
Packit 5756e2
        /* We manually build our own version of fopen(..., "we") that works without O_CREAT and with O_NOFOLLOW if needed. */
Packit 5756e2
        fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY |
Packit 5756e2
                  (FLAGS_SET(flags, WRITE_STRING_FILE_NOFOLLOW) ? O_NOFOLLOW : 0) |
Packit Service 87a54e
                  (FLAGS_SET(flags, WRITE_STRING_FILE_CREATE) ? O_CREAT : 0) |
Packit Service 87a54e
                  (FLAGS_SET(flags, WRITE_STRING_FILE_TRUNCATE) ? O_TRUNC : 0),
Packit 5756e2
                  (FLAGS_SET(flags, WRITE_STRING_FILE_MODE_0600) ? 0600 : 0666));
Packit 5756e2
        if (fd < 0) {
Packit 5756e2
                r = -errno;
Packit 5756e2
                goto fail;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        r = fdopen_unlocked(fd, "w", &f);
Packit 5756e2
        if (r < 0) {
Packit 5756e2
                safe_close(fd);
Packit 5756e2
                goto fail;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        if (flags & WRITE_STRING_FILE_DISABLE_BUFFER)
Packit 5756e2
                setvbuf(f, NULL, _IONBF, 0);
Packit 5756e2
Packit 5756e2
        r = write_string_stream_ts(f, line, flags, ts);
Packit 5756e2
        if (r < 0)
Packit 5756e2
                goto fail;
Packit 5756e2
Packit 5756e2
        return 0;
Packit 5756e2
Packit 5756e2
fail:
Packit 5756e2
        if (!(flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE))
Packit 5756e2
                return r;
Packit 5756e2
Packit 5756e2
        f = safe_fclose(f);
Packit 5756e2
Packit 5756e2
        /* OK, the operation failed, but let's see if the right
Packit 5756e2
         * contents in place already. If so, eat up the error. */
Packit 5756e2
Packit 5756e2
        q = verify_file(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
Packit 5756e2
        if (q <= 0)
Packit 5756e2
                return r;
Packit 5756e2
Packit 5756e2
        return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int write_string_filef(
Packit 5756e2
                const char *fn,
Packit 5756e2
                WriteStringFileFlags flags,
Packit 5756e2
                const char *format, ...) {
Packit 5756e2
Packit 5756e2
        _cleanup_free_ char *p = NULL;
Packit 5756e2
        va_list ap;
Packit 5756e2
        int r;
Packit 5756e2
Packit 5756e2
        va_start(ap, format);
Packit 5756e2
        r = vasprintf(&p, format, ap);
Packit 5756e2
        va_end(ap);
Packit 5756e2
Packit 5756e2
        if (r < 0)
Packit 5756e2
                return -ENOMEM;
Packit 5756e2
Packit 5756e2
        return write_string_file(fn, p, flags);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int read_one_line_file(const char *fn, char **line) {
Packit 5756e2
        _cleanup_fclose_ FILE *f = NULL;
Packit 5756e2
        int r;
Packit 5756e2
Packit 5756e2
        assert(fn);
Packit 5756e2
        assert(line);
Packit 5756e2
Packit 5756e2
        r = fopen_unlocked(fn, "re", &f);
Packit 5756e2
        if (r < 0)
Packit 5756e2
                return r;
Packit 5756e2
Packit 5756e2
        return read_line(f, LONG_LINE_MAX, line);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int verify_file(const char *fn, const char *blob, bool accept_extra_nl) {
Packit 5756e2
        _cleanup_fclose_ FILE *f = NULL;
Packit 5756e2
        _cleanup_free_ char *buf = NULL;
Packit 5756e2
        size_t l, k;
Packit 5756e2
        int r;
Packit 5756e2
Packit 5756e2
        assert(fn);
Packit 5756e2
        assert(blob);
Packit 5756e2
Packit 5756e2
        l = strlen(blob);
Packit 5756e2
Packit 5756e2
        if (accept_extra_nl && endswith(blob, "\n"))
Packit 5756e2
                accept_extra_nl = false;
Packit 5756e2
Packit 5756e2
        buf = malloc(l + accept_extra_nl + 1);
Packit 5756e2
        if (!buf)
Packit 5756e2
                return -ENOMEM;
Packit 5756e2
Packit 5756e2
        r = fopen_unlocked(fn, "re", &f);
Packit 5756e2
        if (r < 0)
Packit 5756e2
                return r;
Packit 5756e2
Packit 5756e2
        /* We try to read one byte more than we need, so that we know whether we hit eof */
Packit 5756e2
        errno = 0;
Packit 5756e2
        k = fread(buf, 1, l + accept_extra_nl + 1, f);
Packit 5756e2
        if (ferror(f))
Packit 5756e2
                return errno_or_else(EIO);
Packit 5756e2
Packit 5756e2
        if (k != l && k != l + accept_extra_nl)
Packit 5756e2
                return 0;
Packit 5756e2
        if (memcmp(buf, blob, l) != 0)
Packit 5756e2
                return 0;
Packit 5756e2
        if (k > l && buf[l] != '\n')
Packit 5756e2
                return 0;
Packit 5756e2
Packit 5756e2
        return 1;
Packit 5756e2
}
Packit 5756e2
#endif /* NM_IGNORED */
Packit 5756e2
Packit 5756e2
int read_full_virtual_file(const char *filename, char **ret_contents, size_t *ret_size) {
Packit 5756e2
        _cleanup_free_ char *buf = NULL;
Packit 5756e2
        _cleanup_close_ int fd = -1;
Packit 5756e2
        struct stat st;
Packit 5756e2
        size_t n, size;
Packit 5756e2
        int n_retries;
Packit 5756e2
        char *p;
Packit 5756e2
Packit 5756e2
        assert(ret_contents);
Packit 5756e2
Packit 5756e2
        /* Virtual filesystems such as sysfs or procfs use kernfs, and kernfs can work
Packit 5756e2
         * with two sorts of virtual files. One sort uses "seq_file", and the results of
Packit 5756e2
         * the first read are buffered for the second read. The other sort uses "raw"
Packit 5756e2
         * reads which always go direct to the device. In the latter case, the content of
Packit 5756e2
         * the virtual file must be retrieved with a single read otherwise a second read
Packit 5756e2
         * might get the new value instead of finding EOF immediately. That's the reason
Packit 5756e2
         * why the usage of fread(3) is prohibited in this case as it always performs a
Packit 5756e2
         * second call to read(2) looking for EOF. See issue 13585. */
Packit 5756e2
Packit 5756e2
        fd = open(filename, O_RDONLY|O_CLOEXEC);
Packit 5756e2
        if (fd < 0)
Packit 5756e2
                return -errno;
Packit 5756e2
Packit 5756e2
        /* Start size for files in /proc which usually report a file size of 0. */
Packit 5756e2
        size = LINE_MAX / 2;
Packit 5756e2
Packit 5756e2
        /* Limit the number of attempts to read the number of bytes returned by fstat(). */
Packit 5756e2
        n_retries = 3;
Packit 5756e2
Packit 5756e2
        for (;;) {
Packit 5756e2
                if (n_retries <= 0)
Packit 5756e2
                        return -EIO;
Packit 5756e2
Packit 5756e2
                if (fstat(fd, &st) < 0)
Packit 5756e2
                        return -errno;
Packit 5756e2
Packit 5756e2
                if (!S_ISREG(st.st_mode))
Packit 5756e2
                        return -EBADF;
Packit 5756e2
Packit 5756e2
                /* Be prepared for files from /proc which generally report a file size of 0. */
Packit 5756e2
                if (st.st_size > 0) {
Packit 5756e2
                        size = st.st_size;
Packit 5756e2
                        n_retries--;
Packit 5756e2
                } else
Packit 5756e2
                        size = size * 2;
Packit 5756e2
Packit 5756e2
                if (size > READ_FULL_BYTES_MAX)
Packit 5756e2
                        return -E2BIG;
Packit 5756e2
Packit 5756e2
                p = realloc(buf, size + 1);
Packit 5756e2
                if (!p)
Packit 5756e2
                        return -ENOMEM;
Packit 5756e2
                buf = TAKE_PTR(p);
Packit 5756e2
Packit 5756e2
                for (;;) {
Packit 5756e2
                        ssize_t k;
Packit 5756e2
Packit 5756e2
                        /* Read one more byte so we can detect whether the content of the
Packit 5756e2
                         * file has already changed or the guessed size for files from /proc
Packit 5756e2
                         * wasn't large enough . */
Packit 5756e2
                        k = read(fd, buf, size + 1);
Packit 5756e2
                        if (k >= 0) {
Packit 5756e2
                                n = k;
Packit 5756e2
                                break;
Packit 5756e2
                        }
Packit 5756e2
Packit 5756e2
                        if (errno != EINTR)
Packit 5756e2
                                return -errno;
Packit 5756e2
                }
Packit 5756e2
Packit 5756e2
                /* Consider a short read as EOF */
Packit 5756e2
                if (n <= size)
Packit 5756e2
                        break;
Packit 5756e2
Packit 5756e2
                /* Hmm... either we read too few bytes from /proc or less likely the content
Packit 5756e2
                 * of the file might have been changed (and is now bigger) while we were
Packit 5756e2
                 * processing, let's try again either with a bigger guessed size or the new
Packit 5756e2
                 * file size. */
Packit 5756e2
Packit 5756e2
                if (lseek(fd, 0, SEEK_SET) < 0)
Packit 5756e2
                        return -errno;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        if (n < size) {
Packit 5756e2
                p = realloc(buf, n + 1);
Packit 5756e2
                if (!p)
Packit 5756e2
                        return -ENOMEM;
Packit 5756e2
                buf = TAKE_PTR(p);
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        if (!ret_size) {
Packit 5756e2
                /* Safety check: if the caller doesn't want to know the size of what we
Packit 5756e2
                 * just read it will rely on the trailing NUL byte. But if there's an
Packit 5756e2
                 * embedded NUL byte, then we should refuse operation as otherwise
Packit 5756e2
                 * there'd be ambiguity about what we just read. */
Packit 5756e2
Packit 5756e2
                if (memchr(buf, 0, n))
Packit 5756e2
                        return -EBADMSG;
Packit 5756e2
        } else
Packit 5756e2
                *ret_size = n;
Packit 5756e2
Packit 5756e2
        buf[n] = 0;
Packit 5756e2
        *ret_contents = TAKE_PTR(buf);
Packit 5756e2
Packit 5756e2
        return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int read_full_stream_full(
Packit 5756e2
                FILE *f,
Packit 5756e2
                const char *filename,
Packit Service 87a54e
                uint64_t offset,
Packit Service 87a54e
                size_t size,
Packit 5756e2
                ReadFullFileFlags flags,
Packit 5756e2
                char **ret_contents,
Packit 5756e2
                size_t *ret_size) {
Packit 5756e2
Packit 5756e2
        _cleanup_free_ char *buf = NULL;
Packit 5756e2
        size_t n, n_next, l;
Packit 5756e2
        int fd, r;
Packit 5756e2
Packit 5756e2
        assert(f);
Packit 5756e2
        assert(ret_contents);
Packit 5756e2
        assert(!FLAGS_SET(flags, READ_FULL_FILE_UNBASE64 | READ_FULL_FILE_UNHEX));
Packit 5756e2
Packit Service 87a54e
        if (offset != UINT64_MAX && offset > LONG_MAX)
Packit Service 87a54e
                return -ERANGE;
Packit Service 87a54e
Packit Service 87a54e
        n_next = size != SIZE_MAX ? size : LINE_MAX; /* Start size */
Packit 5756e2
Packit 5756e2
        fd = fileno(f);
Packit Service 87a54e
        if (fd >= 0) { /* If the FILE* object is backed by an fd (as opposed to memory or such, see
Packit Service 87a54e
                        * fmemopen()), let's optimize our buffering */
Packit Service 87a54e
                struct stat st;
Packit 5756e2
Packit 5756e2
                if (fstat(fd, &st) < 0)
Packit 5756e2
                        return -errno;
Packit 5756e2
Packit 5756e2
                if (S_ISREG(st.st_mode)) {
Packit Service 87a54e
                        if (size == SIZE_MAX) {
Packit Service 87a54e
                                uint64_t rsize =
Packit Service 87a54e
                                        LESS_BY((uint64_t) st.st_size, offset == UINT64_MAX ? 0 : offset);
Packit Service 87a54e
Packit Service 87a54e
                                /* Safety check */
Packit Service 87a54e
                                if (rsize > READ_FULL_BYTES_MAX)
Packit Service 87a54e
                                        return -E2BIG;
Packit Service 87a54e
Packit Service 87a54e
                                /* Start with the right file size. Note that we increase the size to read
Packit Service 87a54e
                                 * here by one, so that the first read attempt already makes us notice the
Packit Service 87a54e
                                 * EOF. If the reported size of the file is zero, we avoid this logic
Packit Service 87a54e
                                 * however, since quite likely it might be a virtual file in procfs that all
Packit Service 87a54e
                                 * report a zero file size. */
Packit Service 87a54e
                                if (st.st_size > 0)
Packit Service 87a54e
                                        n_next = rsize + 1;
Packit Service 87a54e
                        }
Packit 5756e2
Packit 5756e2
                        if (flags & READ_FULL_FILE_WARN_WORLD_READABLE)
Packit 5756e2
                                (void) warn_file_is_world_accessible(filename, &st, NULL, 0);
Packit 5756e2
                }
Packit 5756e2
        }
Packit 5756e2
Packit Service 87a54e
        if (offset != UINT64_MAX && fseek(f, offset, SEEK_SET) < 0)
Packit Service 87a54e
                return -errno;
Packit Service 87a54e
Packit 5756e2
        n = l = 0;
Packit 5756e2
        for (;;) {
Packit 5756e2
                char *t;
Packit 5756e2
                size_t k;
Packit 5756e2
Packit 5756e2
                if (flags & READ_FULL_FILE_SECURE) {
Packit 5756e2
                        t = malloc(n_next + 1);
Packit 5756e2
                        if (!t) {
Packit 5756e2
                                r = -ENOMEM;
Packit 5756e2
                                goto finalize;
Packit 5756e2
                        }
Packit 5756e2
                        memcpy_safe(t, buf, n);
Packit 5756e2
                        explicit_bzero_safe(buf, n);
Packit 5756e2
                        buf = mfree(buf);
Packit 5756e2
                } else {
Packit 5756e2
                        t = realloc(buf, n_next + 1);
Packit 5756e2
                        if (!t)
Packit 5756e2
                                return -ENOMEM;
Packit 5756e2
                }
Packit 5756e2
Packit 5756e2
                buf = t;
Packit 5756e2
                n = n_next;
Packit 5756e2
Packit 5756e2
                errno = 0;
Packit 5756e2
                k = fread(buf + l, 1, n - l, f);
Packit 5756e2
Packit 5756e2
                assert(k <= n - l);
Packit 5756e2
                l += k;
Packit 5756e2
Packit 5756e2
                if (ferror(f)) {
Packit 5756e2
                        r = errno_or_else(EIO);
Packit 5756e2
                        goto finalize;
Packit 5756e2
                }
Packit 5756e2
                if (feof(f))
Packit 5756e2
                        break;
Packit 5756e2
Packit Service 87a54e
                if (size != SIZE_MAX) { /* If we got asked to read some specific size, we already sized the buffer right, hence leave */
Packit Service 87a54e
                        assert(l == size);
Packit Service 87a54e
                        break;
Packit Service 87a54e
                }
Packit Service 87a54e
Packit 5756e2
                assert(k > 0); /* we can't have read zero bytes because that would have been EOF */
Packit 5756e2
Packit 5756e2
                /* Safety check */
Packit 5756e2
                if (n >= READ_FULL_BYTES_MAX) {
Packit 5756e2
                        r = -E2BIG;
Packit 5756e2
                        goto finalize;
Packit 5756e2
                }
Packit 5756e2
Packit 5756e2
                n_next = MIN(n * 2, READ_FULL_BYTES_MAX);
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        if (flags & (READ_FULL_FILE_UNBASE64 | READ_FULL_FILE_UNHEX)) {
Packit 5756e2
                _cleanup_free_ void *decoded = NULL;
Packit 5756e2
                size_t decoded_size;
Packit 5756e2
Packit 5756e2
                buf[l++] = 0;
Packit 5756e2
                if (flags & READ_FULL_FILE_UNBASE64)
Packit 5756e2
                        r = unbase64mem_full(buf, l, flags & READ_FULL_FILE_SECURE, &decoded, &decoded_size);
Packit 5756e2
                else
Packit 5756e2
                        r = unhexmem_full(buf, l, flags & READ_FULL_FILE_SECURE, &decoded, &decoded_size);
Packit 5756e2
                if (r < 0)
Packit 5756e2
                        goto finalize;
Packit 5756e2
Packit 5756e2
                if (flags & READ_FULL_FILE_SECURE)
Packit 5756e2
                        explicit_bzero_safe(buf, n);
Packit 5756e2
                free_and_replace(buf, decoded);
Packit 5756e2
                n = l = decoded_size;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        if (!ret_size) {
Packit 5756e2
                /* Safety check: if the caller doesn't want to know the size of what we just read it will rely on the
Packit 5756e2
                 * trailing NUL byte. But if there's an embedded NUL byte, then we should refuse operation as otherwise
Packit 5756e2
                 * there'd be ambiguity about what we just read. */
Packit 5756e2
Packit 5756e2
                if (memchr(buf, 0, l)) {
Packit 5756e2
                        r = -EBADMSG;
Packit 5756e2
                        goto finalize;
Packit 5756e2
                }
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        buf[l] = 0;
Packit 5756e2
        *ret_contents = TAKE_PTR(buf);
Packit 5756e2
Packit 5756e2
        if (ret_size)
Packit 5756e2
                *ret_size = l;
Packit 5756e2
Packit 5756e2
        return 0;
Packit 5756e2
Packit 5756e2
finalize:
Packit 5756e2
        if (flags & READ_FULL_FILE_SECURE)
Packit 5756e2
                explicit_bzero_safe(buf, n);
Packit 5756e2
Packit 5756e2
        return r;
Packit 5756e2
}
Packit 5756e2
Packit Service 87a54e
int read_full_file_full(
Packit Service 87a54e
                int dir_fd,
Packit Service 87a54e
                const char *filename,
Packit Service 87a54e
                uint64_t offset,
Packit Service 87a54e
                size_t size,
Packit Service 87a54e
                ReadFullFileFlags flags,
Packit Service 87a54e
                const char *bind_name,
Packit Service 87a54e
                char **ret_contents,
Packit Service 87a54e
                size_t *ret_size) {
Packit Service 87a54e
Packit 5756e2
        _cleanup_fclose_ FILE *f = NULL;
Packit 5756e2
        int r;
Packit 5756e2
Packit 5756e2
        assert(filename);
Packit Service 87a54e
        assert(ret_contents);
Packit 5756e2
Packit 5756e2
        r = xfopenat(dir_fd, filename, "re", 0, &f);
Packit 5756e2
        if (r < 0) {
Packit 5756e2
                _cleanup_close_ int dfd = -1, sk = -1;
Packit 5756e2
                union sockaddr_union sa;
Packit 5756e2
Packit 5756e2
                /* ENXIO is what Linux returns if we open a node that is an AF_UNIX socket */
Packit 5756e2
                if (r != -ENXIO)
Packit 5756e2
                        return r;
Packit 5756e2
Packit 5756e2
                /* If this is enabled, let's try to connect to it */
Packit 5756e2
                if (!FLAGS_SET(flags, READ_FULL_FILE_CONNECT_SOCKET))
Packit 5756e2
                        return -ENXIO;
Packit 5756e2
Packit Service 87a54e
                /* Seeking is not supported on AF_UNIX sockets */
Packit Service 87a54e
                if (offset != UINT64_MAX)
Packit Service 87a54e
                        return -ESPIPE;
Packit Service 87a54e
Packit 5756e2
                if (dir_fd == AT_FDCWD)
Packit 5756e2
                        r = sockaddr_un_set_path(&sa.un, filename);
Packit 5756e2
                else {
Packit 5756e2
                        char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
Packit 5756e2
Packit 5756e2
                        /* If we shall operate relative to some directory, then let's use O_PATH first to
Packit 5756e2
                         * open the socket inode, and then connect to it via /proc/self/fd/. We have to do
Packit 5756e2
                         * this since there's not connectat() that takes a directory fd as first arg. */
Packit 5756e2
Packit 5756e2
                        dfd = openat(dir_fd, filename, O_PATH|O_CLOEXEC);
Packit 5756e2
                        if (dfd < 0)
Packit 5756e2
                                return -errno;
Packit 5756e2
Packit 5756e2
                        xsprintf(procfs_path, "/proc/self/fd/%i", dfd);
Packit 5756e2
                        r = sockaddr_un_set_path(&sa.un, procfs_path);
Packit 5756e2
                }
Packit 5756e2
                if (r < 0)
Packit 5756e2
                        return r;
Packit 5756e2
Packit 5756e2
                sk = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
Packit 5756e2
                if (sk < 0)
Packit 5756e2
                        return -errno;
Packit 5756e2
Packit Service 87a54e
                if (bind_name) {
Packit Service 87a54e
                        /* If the caller specified a socket name to bind to, do so before connecting. This is
Packit Service 87a54e
                         * useful to communicate some minor, short meta-information token from the client to
Packit Service 87a54e
                         * the server. */
Packit Service 87a54e
                        union sockaddr_union bsa;
Packit Service 87a54e
Packit Service 87a54e
                        r = sockaddr_un_set_path(&bsa.un, bind_name);
Packit Service 87a54e
                        if (r < 0)
Packit Service 87a54e
                                return r;
Packit Service 87a54e
Packit Service 87a54e
                        if (bind(sk, &bsa.sa, r) < 0)
Packit Service 87a54e
                                return r;
Packit Service 87a54e
                }
Packit Service 87a54e
Packit 5756e2
                if (connect(sk, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0)
Packit 5756e2
                        return errno == ENOTSOCK ? -ENXIO : -errno; /* propagate original error if this is
Packit 5756e2
                                                                     * not a socket after all */
Packit 5756e2
Packit 5756e2
                if (shutdown(sk, SHUT_WR) < 0)
Packit 5756e2
                        return -errno;
Packit 5756e2
Packit 5756e2
                f = fdopen(sk, "r");
Packit 5756e2
                if (!f)
Packit 5756e2
                        return -errno;
Packit 5756e2
Packit 5756e2
                TAKE_FD(sk);
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
Packit 5756e2
Packit Service 87a54e
        return read_full_stream_full(f, filename, offset, size, flags, ret_contents, ret_size);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
#if 0 /* NM_IGNORED */
Packit 5756e2
int executable_is_script(const char *path, char **interpreter) {
Packit 5756e2
        _cleanup_free_ char *line = NULL;
Packit 5756e2
        size_t len;
Packit 5756e2
        char *ans;
Packit 5756e2
        int r;
Packit 5756e2
Packit 5756e2
        assert(path);
Packit 5756e2
Packit 5756e2
        r = read_one_line_file(path, &line);
Packit 5756e2
        if (r == -ENOBUFS) /* First line overly long? if so, then it's not a script */
Packit 5756e2
                return 0;
Packit 5756e2
        if (r < 0)
Packit 5756e2
                return r;
Packit 5756e2
Packit 5756e2
        if (!startswith(line, "#!"))
Packit 5756e2
                return 0;
Packit 5756e2
Packit 5756e2
        ans = strstrip(line + 2);
Packit 5756e2
        len = strcspn(ans, " \t");
Packit 5756e2
Packit 5756e2
        if (len == 0)
Packit 5756e2
                return 0;
Packit 5756e2
Packit 5756e2
        ans = strndup(ans, len);
Packit 5756e2
        if (!ans)
Packit 5756e2
                return -ENOMEM;
Packit 5756e2
Packit 5756e2
        *interpreter = ans;
Packit 5756e2
        return 1;
Packit 5756e2
}
Packit 5756e2
#endif /* NM_IGNORED */
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * Retrieve one field from a file like /proc/self/status.  pattern
Packit 5756e2
 * should not include whitespace or the delimiter (':'). pattern matches only
Packit 5756e2
 * the beginning of a line. Whitespace before ':' is skipped. Whitespace and
Packit 5756e2
 * zeros after the ':' will be skipped. field must be freed afterwards.
Packit 5756e2
 * terminator specifies the terminating characters of the field value (not
Packit 5756e2
 * included in the value).
Packit 5756e2
 */
Packit 5756e2
int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field) {
Packit 5756e2
        _cleanup_free_ char *status = NULL;
Packit 5756e2
        char *t, *f;
Packit 5756e2
        size_t len;
Packit 5756e2
        int r;
Packit 5756e2
Packit 5756e2
        assert(terminator);
Packit 5756e2
        assert(filename);
Packit 5756e2
        assert(pattern);
Packit 5756e2
        assert(field);
Packit 5756e2
Packit 5756e2
        r = read_full_virtual_file(filename, &status, NULL);
Packit 5756e2
        if (r < 0)
Packit 5756e2
                return r;
Packit 5756e2
Packit 5756e2
        t = status;
Packit 5756e2
Packit 5756e2
        do {
Packit 5756e2
                bool pattern_ok;
Packit 5756e2
Packit 5756e2
                do {
Packit 5756e2
                        t = strstr(t, pattern);
Packit 5756e2
                        if (!t)
Packit 5756e2
                                return -ENOENT;
Packit 5756e2
Packit 5756e2
                        /* Check that pattern occurs in beginning of line. */
Packit 5756e2
                        pattern_ok = (t == status || t[-1] == '\n');
Packit 5756e2
Packit 5756e2
                        t += strlen(pattern);
Packit 5756e2
Packit 5756e2
                } while (!pattern_ok);
Packit 5756e2
Packit 5756e2
                t += strspn(t, " \t");
Packit 5756e2
                if (!*t)
Packit 5756e2
                        return -ENOENT;
Packit 5756e2
Packit 5756e2
        } while (*t != ':');
Packit 5756e2
Packit 5756e2
        t++;
Packit 5756e2
Packit 5756e2
        if (*t) {
Packit 5756e2
                t += strspn(t, " \t");
Packit 5756e2
Packit 5756e2
                /* Also skip zeros, because when this is used for
Packit 5756e2
                 * capabilities, we don't want the zeros. This way the
Packit 5756e2
                 * same capability set always maps to the same string,
Packit 5756e2
                 * irrespective of the total capability set size. For
Packit 5756e2
                 * other numbers it shouldn't matter. */
Packit 5756e2
                t += strspn(t, "0");
Packit 5756e2
                /* Back off one char if there's nothing but whitespace
Packit 5756e2
                   and zeros */
Packit 5756e2
                if (!*t || isspace(*t))
Packit 5756e2
                        t--;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        len = strcspn(t, terminator);
Packit 5756e2
Packit 5756e2
        f = strndup(t, len);
Packit 5756e2
        if (!f)
Packit 5756e2
                return -ENOMEM;
Packit 5756e2
Packit 5756e2
        *field = f;
Packit 5756e2
        return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
DIR *xopendirat(int fd, const char *name, int flags) {
Packit 5756e2
        int nfd;
Packit 5756e2
        DIR *d;
Packit 5756e2
Packit 5756e2
        assert(!(flags & O_CREAT));
Packit 5756e2
Packit 5756e2
        nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags, 0);
Packit 5756e2
        if (nfd < 0)
Packit 5756e2
                return NULL;
Packit 5756e2
Packit 5756e2
        d = fdopendir(nfd);
Packit 5756e2
        if (!d) {
Packit 5756e2
                safe_close(nfd);
Packit 5756e2
                return NULL;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        return d;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static int mode_to_flags(const char *mode) {
Packit 5756e2
        const char *p;
Packit 5756e2
        int flags;
Packit 5756e2
Packit 5756e2
        if ((p = startswith(mode, "r+")))
Packit 5756e2
                flags = O_RDWR;
Packit 5756e2
        else if ((p = startswith(mode, "r")))
Packit 5756e2
                flags = O_RDONLY;
Packit 5756e2
        else if ((p = startswith(mode, "w+")))
Packit 5756e2
                flags = O_RDWR|O_CREAT|O_TRUNC;
Packit 5756e2
        else if ((p = startswith(mode, "w")))
Packit 5756e2
                flags = O_WRONLY|O_CREAT|O_TRUNC;
Packit 5756e2
        else if ((p = startswith(mode, "a+")))
Packit 5756e2
                flags = O_RDWR|O_CREAT|O_APPEND;
Packit 5756e2
        else if ((p = startswith(mode, "a")))
Packit 5756e2
                flags = O_WRONLY|O_CREAT|O_APPEND;
Packit 5756e2
        else
Packit 5756e2
                return -EINVAL;
Packit 5756e2
Packit 5756e2
        for (; *p != 0; p++) {
Packit 5756e2
Packit 5756e2
                switch (*p) {
Packit 5756e2
Packit 5756e2
                case 'e':
Packit 5756e2
                        flags |= O_CLOEXEC;
Packit 5756e2
                        break;
Packit 5756e2
Packit 5756e2
                case 'x':
Packit 5756e2
                        flags |= O_EXCL;
Packit 5756e2
                        break;
Packit 5756e2
Packit 5756e2
                case 'm':
Packit 5756e2
                        /* ignore this here, fdopen() might care later though */
Packit 5756e2
                        break;
Packit 5756e2
Packit 5756e2
                case 'c': /* not sure what to do about this one */
Packit 5756e2
                default:
Packit 5756e2
                        return -EINVAL;
Packit 5756e2
                }
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        return flags;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int xfopenat(int dir_fd, const char *path, const char *mode, int flags, FILE **ret) {
Packit 5756e2
        FILE *f;
Packit 5756e2
Packit 5756e2
        /* A combination of fopen() with openat() */
Packit 5756e2
Packit 5756e2
        if (dir_fd == AT_FDCWD && flags == 0) {
Packit 5756e2
                f = fopen(path, mode);
Packit 5756e2
                if (!f)
Packit 5756e2
                        return -errno;
Packit 5756e2
        } else {
Packit 5756e2
                int fd, mode_flags;
Packit 5756e2
Packit 5756e2
                mode_flags = mode_to_flags(mode);
Packit 5756e2
                if (mode_flags < 0)
Packit 5756e2
                        return mode_flags;
Packit 5756e2
Packit 5756e2
                fd = openat(dir_fd, path, mode_flags | flags);
Packit 5756e2
                if (fd < 0)
Packit 5756e2
                        return -errno;
Packit 5756e2
Packit 5756e2
                f = fdopen(fd, mode);
Packit 5756e2
                if (!f) {
Packit 5756e2
                        safe_close(fd);
Packit 5756e2
                        return -errno;
Packit 5756e2
                }
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        *ret = f;
Packit 5756e2
        return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
#if 0 /* NM_IGNORED */
Packit 5756e2
static int search_and_fopen_internal(const char *path, const char *mode, const char *root, char **search, FILE **_f) {
Packit 5756e2
        char **i;
Packit 5756e2
Packit 5756e2
        assert(path);
Packit 5756e2
        assert(mode);
Packit 5756e2
        assert(_f);
Packit 5756e2
Packit 5756e2
        if (!path_strv_resolve_uniq(search, root))
Packit 5756e2
                return -ENOMEM;
Packit 5756e2
Packit 5756e2
        STRV_FOREACH(i, search) {
Packit 5756e2
                _cleanup_free_ char *p = NULL;
Packit 5756e2
                FILE *f;
Packit 5756e2
Packit 5756e2
                p = path_join(root, *i, path);
Packit 5756e2
                if (!p)
Packit 5756e2
                        return -ENOMEM;
Packit 5756e2
Packit 5756e2
                f = fopen(p, mode);
Packit 5756e2
                if (f) {
Packit 5756e2
                        *_f = f;
Packit 5756e2
                        return 0;
Packit 5756e2
                }
Packit 5756e2
Packit 5756e2
                if (errno != ENOENT)
Packit 5756e2
                        return -errno;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        return -ENOENT;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f) {
Packit 5756e2
        _cleanup_strv_free_ char **copy = NULL;
Packit 5756e2
Packit 5756e2
        assert(path);
Packit 5756e2
        assert(mode);
Packit 5756e2
        assert(_f);
Packit 5756e2
Packit 5756e2
        if (path_is_absolute(path)) {
Packit 5756e2
                FILE *f;
Packit 5756e2
Packit 5756e2
                f = fopen(path, mode);
Packit 5756e2
                if (f) {
Packit 5756e2
                        *_f = f;
Packit 5756e2
                        return 0;
Packit 5756e2
                }
Packit 5756e2
Packit 5756e2
                return -errno;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        copy = strv_copy((char**) search);
Packit 5756e2
        if (!copy)
Packit 5756e2
                return -ENOMEM;
Packit 5756e2
Packit 5756e2
        return search_and_fopen_internal(path, mode, root, copy, _f);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f) {
Packit 5756e2
        _cleanup_strv_free_ char **s = NULL;
Packit 5756e2
Packit 5756e2
        if (path_is_absolute(path)) {
Packit 5756e2
                FILE *f;
Packit 5756e2
Packit 5756e2
                f = fopen(path, mode);
Packit 5756e2
                if (f) {
Packit 5756e2
                        *_f = f;
Packit 5756e2
                        return 0;
Packit 5756e2
                }
Packit 5756e2
Packit 5756e2
                return -errno;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        s = strv_split_nulstr(search);
Packit 5756e2
        if (!s)
Packit 5756e2
                return -ENOMEM;
Packit 5756e2
Packit 5756e2
        return search_and_fopen_internal(path, mode, root, s, _f);
Packit 5756e2
}
Packit Service a1bd4f
Packit Service a1bd4f
int chase_symlinks_and_fopen_unlocked(
Packit Service a1bd4f
                const char *path,
Packit Service a1bd4f
                const char *root,
Packit Service a1bd4f
                unsigned chase_flags,
Packit Service a1bd4f
                const char *open_flags,
Packit Service a1bd4f
                FILE **ret_file,
Packit Service a1bd4f
                char **ret_path) {
Packit Service a1bd4f
Packit Service a1bd4f
        _cleanup_close_ int fd = -1;
Packit Service a1bd4f
        _cleanup_free_ char *final_path = NULL;
Packit Service a1bd4f
        int mode_flags, r;
Packit Service a1bd4f
        FILE *f;
Packit Service a1bd4f
Packit Service a1bd4f
        assert(path);
Packit Service a1bd4f
        assert(open_flags);
Packit Service a1bd4f
        assert(ret_file);
Packit Service a1bd4f
Packit Service a1bd4f
        mode_flags = mode_to_flags(open_flags);
Packit Service a1bd4f
        if (mode_flags < 0)
Packit Service a1bd4f
                return mode_flags;
Packit Service a1bd4f
Packit Service a1bd4f
        fd = chase_symlinks_and_open(path, root, chase_flags, mode_flags, ret_path ? &final_path : NULL);
Packit Service a1bd4f
        if (fd < 0)
Packit Service a1bd4f
                return fd;
Packit Service a1bd4f
Packit Service a1bd4f
        r = fdopen_unlocked(fd, open_flags, &f);
Packit Service a1bd4f
        if (r < 0)
Packit Service a1bd4f
                return r;
Packit Service a1bd4f
        TAKE_FD(fd);
Packit Service a1bd4f
Packit Service a1bd4f
        *ret_file = f;
Packit Service a1bd4f
        if (ret_path)
Packit Service a1bd4f
                *ret_path = TAKE_PTR(final_path);
Packit Service a1bd4f
        return 0;
Packit Service a1bd4f
}
Packit 5756e2
#endif /* NM_IGNORED */
Packit 5756e2
Packit 5756e2
int fflush_and_check(FILE *f) {
Packit 5756e2
        assert(f);
Packit 5756e2
Packit 5756e2
        errno = 0;
Packit 5756e2
        fflush(f);
Packit 5756e2
Packit 5756e2
        if (ferror(f))
Packit 5756e2
                return errno_or_else(EIO);
Packit 5756e2
Packit 5756e2
        return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
#if 0 /* NM_IGNORED */
Packit 5756e2
int fflush_sync_and_check(FILE *f) {
Packit 5756e2
        int r, fd;
Packit 5756e2
Packit 5756e2
        assert(f);
Packit 5756e2
Packit 5756e2
        r = fflush_and_check(f);
Packit 5756e2
        if (r < 0)
Packit 5756e2
                return r;
Packit 5756e2
Packit 5756e2
        /* Not all file streams have an fd associated (think: fmemopen()), let's handle this gracefully and
Packit 5756e2
         * assume that in that case we need no explicit syncing */
Packit 5756e2
        fd = fileno(f);
Packit 5756e2
        if (fd < 0)
Packit 5756e2
                return 0;
Packit 5756e2
Packit 5756e2
        if (fsync(fd) < 0)
Packit 5756e2
                return -errno;
Packit 5756e2
Packit 5756e2
        r = fsync_directory_of_file(fd);
Packit 5756e2
        if (r < 0)
Packit 5756e2
                return r;
Packit 5756e2
Packit 5756e2
        return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int write_timestamp_file_atomic(const char *fn, usec_t n) {
Packit 5756e2
        char ln[DECIMAL_STR_MAX(n)+2];
Packit 5756e2
Packit 5756e2
        /* Creates a "timestamp" file, that contains nothing but a
Packit 5756e2
         * usec_t timestamp, formatted in ASCII. */
Packit 5756e2
Packit 5756e2
        if (n <= 0 || n >= USEC_INFINITY)
Packit 5756e2
                return -ERANGE;
Packit 5756e2
Packit 5756e2
        xsprintf(ln, USEC_FMT "\n", n);
Packit 5756e2
Packit 5756e2
        return write_string_file(fn, ln, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int read_timestamp_file(const char *fn, usec_t *ret) {
Packit 5756e2
        _cleanup_free_ char *ln = NULL;
Packit 5756e2
        uint64_t t;
Packit 5756e2
        int r;
Packit 5756e2
Packit 5756e2
        r = read_one_line_file(fn, &ln);
Packit 5756e2
        if (r < 0)
Packit 5756e2
                return r;
Packit 5756e2
Packit 5756e2
        r = safe_atou64(ln, &t);
Packit 5756e2
        if (r < 0)
Packit 5756e2
                return r;
Packit 5756e2
Packit 5756e2
        if (t <= 0 || t >= (uint64_t) USEC_INFINITY)
Packit 5756e2
                return -ERANGE;
Packit 5756e2
Packit 5756e2
        *ret = (usec_t) t;
Packit 5756e2
        return 0;
Packit 5756e2
}
Packit 5756e2
#endif /* NM_IGNORED */
Packit 5756e2
Packit 5756e2
int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space) {
Packit 5756e2
        int r;
Packit 5756e2
Packit 5756e2
        assert(s);
Packit 5756e2
Packit 5756e2
        /* Outputs the specified string with fputs(), but optionally prefixes it with a separator. The *space parameter
Packit 5756e2
         * when specified shall initially point to a boolean variable initialized to false. It is set to true after the
Packit 5756e2
         * first invocation. This call is supposed to be use in loops, where a separator shall be inserted between each
Packit 5756e2
         * element, but not before the first one. */
Packit 5756e2
Packit 5756e2
        if (!f)
Packit 5756e2
                f = stdout;
Packit 5756e2
Packit 5756e2
        if (space) {
Packit 5756e2
                if (!separator)
Packit 5756e2
                        separator = " ";
Packit 5756e2
Packit 5756e2
                if (*space) {
Packit 5756e2
                        r = fputs(separator, f);
Packit 5756e2
                        if (r < 0)
Packit 5756e2
                                return r;
Packit 5756e2
                }
Packit 5756e2
Packit 5756e2
                *space = true;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        return fputs(s, f);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
#if 0 /* NM_IGNORED */
Packit 5756e2
/* A bitmask of the EOL markers we know */
Packit 5756e2
typedef enum EndOfLineMarker {
Packit 5756e2
        EOL_NONE     = 0,
Packit 5756e2
        EOL_ZERO     = 1 << 0,  /* \0 (aka NUL) */
Packit 5756e2
        EOL_TEN      = 1 << 1,  /* \n (aka NL, aka LF)  */
Packit 5756e2
        EOL_THIRTEEN = 1 << 2,  /* \r (aka CR)  */
Packit 5756e2
} EndOfLineMarker;
Packit 5756e2
Packit 5756e2
static EndOfLineMarker categorize_eol(char c, ReadLineFlags flags) {
Packit 5756e2
Packit 5756e2
        if (!IN_SET(flags, READ_LINE_ONLY_NUL)) {
Packit 5756e2
                if (c == '\n')
Packit 5756e2
                        return EOL_TEN;
Packit 5756e2
                if (c == '\r')
Packit 5756e2
                        return EOL_THIRTEEN;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        if (c == '\0')
Packit 5756e2
                return EOL_ZERO;
Packit 5756e2
Packit 5756e2
        return EOL_NONE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, funlockfile);
Packit 5756e2
Packit 5756e2
int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
Packit 5756e2
        size_t n = 0, allocated = 0, count = 0;
Packit 5756e2
        _cleanup_free_ char *buffer = NULL;
Packit 5756e2
        int r;
Packit 5756e2
Packit 5756e2
        assert(f);
Packit 5756e2
Packit 5756e2
        /* Something like a bounded version of getline().
Packit 5756e2
         *
Packit 5756e2
         * Considers EOF, \n, \r and \0 end of line delimiters (or combinations of these), and does not include these
Packit 5756e2
         * delimiters in the string returned. Specifically, recognizes the following combinations of markers as line
Packit 5756e2
         * endings:
Packit 5756e2
         *
Packit 5756e2
         *     • \n        (UNIX)
Packit 5756e2
         *     • \r        (old MacOS)
Packit 5756e2
         *     • \0        (C strings)
Packit 5756e2
         *     • \n\0
Packit 5756e2
         *     • \r\0
Packit 5756e2
         *     • \r\n      (Windows)
Packit 5756e2
         *     • \n\r
Packit 5756e2
         *     • \r\n\0
Packit 5756e2
         *     • \n\r\0
Packit 5756e2
         *
Packit 5756e2
         * Returns the number of bytes read from the files (i.e. including delimiters — this hence usually differs from
Packit 5756e2
         * the number of characters in the returned string). When EOF is hit, 0 is returned.
Packit 5756e2
         *
Packit 5756e2
         * The input parameter limit is the maximum numbers of characters in the returned string, i.e. excluding
Packit 5756e2
         * delimiters. If the limit is hit we fail and return -ENOBUFS.
Packit 5756e2
         *
Packit 5756e2
         * If a line shall be skipped ret may be initialized as NULL. */
Packit 5756e2
Packit 5756e2
        if (ret) {
Packit 5756e2
                if (!GREEDY_REALLOC(buffer, allocated, 1))
Packit 5756e2
                        return -ENOMEM;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        {
Packit 5756e2
                _unused_ _cleanup_(funlockfilep) FILE *flocked = f;
Packit 5756e2
                EndOfLineMarker previous_eol = EOL_NONE;
Packit 5756e2
                flockfile(f);
Packit 5756e2
Packit 5756e2
                for (;;) {
Packit 5756e2
                        EndOfLineMarker eol;
Packit 5756e2
                        char c;
Packit 5756e2
Packit 5756e2
                        if (n >= limit)
Packit 5756e2
                                return -ENOBUFS;
Packit 5756e2
Packit 5756e2
                        if (count >= INT_MAX) /* We couldn't return the counter anymore as "int", hence refuse this */
Packit 5756e2
                                return -ENOBUFS;
Packit 5756e2
Packit 5756e2
                        r = safe_fgetc(f, &c);
Packit 5756e2
                        if (r < 0)
Packit 5756e2
                                return r;
Packit 5756e2
                        if (r == 0) /* EOF is definitely EOL */
Packit 5756e2
                                break;
Packit 5756e2
Packit 5756e2
                        eol = categorize_eol(c, flags);
Packit 5756e2
Packit 5756e2
                        if (FLAGS_SET(previous_eol, EOL_ZERO) ||
Packit 5756e2
                            (eol == EOL_NONE && previous_eol != EOL_NONE) ||
Packit 5756e2
                            (eol != EOL_NONE && (previous_eol & eol) != 0)) {
Packit 5756e2
                                /* Previous char was a NUL? This is not an EOL, but the previous char was? This type of
Packit 5756e2
                                 * EOL marker has been seen right before?  In either of these three cases we are
Packit 5756e2
                                 * done. But first, let's put this character back in the queue. (Note that we have to
Packit 5756e2
                                 * cast this to (unsigned char) here as ungetc() expects a positive 'int', and if we
Packit 5756e2
                                 * are on an architecture where 'char' equals 'signed char' we need to ensure we don't
Packit 5756e2
                                 * pass a negative value here. That said, to complicate things further ungetc() is
Packit 5756e2
                                 * actually happy with most negative characters and implicitly casts them back to
Packit 5756e2
                                 * positive ones as needed, except for \xff (aka -1, aka EOF), which it refuses. What a
Packit 5756e2
                                 * godawful API!) */
Packit 5756e2
                                assert_se(ungetc((unsigned char) c, f) != EOF);
Packit 5756e2
                                break;
Packit 5756e2
                        }
Packit 5756e2
Packit 5756e2
                        count++;
Packit 5756e2
Packit 5756e2
                        if (eol != EOL_NONE) {
Packit 5756e2
                                /* If we are on a tty, we can't shouldn't wait for more input, because that
Packit 5756e2
                                 * generally means waiting for the user, interactively. In the case of a TTY
Packit 5756e2
                                 * we expect only \n as the single EOL marker, so we are in the lucky
Packit 5756e2
                                 * position that there is no need to wait. We check this condition last, to
Packit 5756e2
                                 * avoid isatty() check if not necessary. */
Packit 5756e2
Packit 5756e2
                                if ((flags & (READ_LINE_IS_A_TTY|READ_LINE_NOT_A_TTY)) == 0) {
Packit 5756e2
                                        int fd;
Packit 5756e2
Packit 5756e2
                                        fd = fileno(f);
Packit 5756e2
                                        if (fd < 0) /* Maybe an fmemopen() stream? Handle this gracefully,
Packit 5756e2
                                                     * and don't call isatty() on an invalid fd */
Packit 5756e2
                                                flags |= READ_LINE_NOT_A_TTY;
Packit 5756e2
                                        else
Packit 5756e2
                                                flags |= isatty(fd) ? READ_LINE_IS_A_TTY : READ_LINE_NOT_A_TTY;
Packit 5756e2
                                }
Packit 5756e2
                                if (FLAGS_SET(flags, READ_LINE_IS_A_TTY))
Packit 5756e2
                                        break;
Packit 5756e2
                        }
Packit 5756e2
Packit 5756e2
                        if (eol != EOL_NONE) {
Packit 5756e2
                                previous_eol |= eol;
Packit 5756e2
                                continue;
Packit 5756e2
                        }
Packit 5756e2
Packit 5756e2
                        if (ret) {
Packit 5756e2
                                if (!GREEDY_REALLOC(buffer, allocated, n + 2))
Packit 5756e2
                                        return -ENOMEM;
Packit 5756e2
Packit 5756e2
                                buffer[n] = c;
Packit 5756e2
                        }
Packit 5756e2
Packit 5756e2
                        n++;
Packit 5756e2
                }
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        if (ret) {
Packit 5756e2
                buffer[n] = 0;
Packit 5756e2
Packit 5756e2
                *ret = TAKE_PTR(buffer);
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        return (int) count;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int safe_fgetc(FILE *f, char *ret) {
Packit 5756e2
        int k;
Packit 5756e2
Packit 5756e2
        assert(f);
Packit 5756e2
Packit 5756e2
        /* A safer version of plain fgetc(): let's propagate the error that happened while reading as such, and
Packit 5756e2
         * separate the EOF condition from the byte read, to avoid those confusion signed/unsigned issues fgetc()
Packit 5756e2
         * has. */
Packit 5756e2
Packit 5756e2
        errno = 0;
Packit 5756e2
        k = fgetc(f);
Packit 5756e2
        if (k == EOF) {
Packit 5756e2
                if (ferror(f))
Packit 5756e2
                        return errno_or_else(EIO);
Packit 5756e2
Packit 5756e2
                if (ret)
Packit 5756e2
                        *ret = 0;
Packit 5756e2
Packit 5756e2
                return 0;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        if (ret)
Packit 5756e2
                *ret = k;
Packit 5756e2
Packit 5756e2
        return 1;
Packit 5756e2
}
Packit 5756e2
#endif /* NM_IGNORED */
Packit 5756e2
Packit 5756e2
int warn_file_is_world_accessible(const char *filename, struct stat *st, const char *unit, unsigned line) {
Packit 5756e2
        struct stat _st;
Packit 5756e2
Packit 5756e2
        if (!filename)
Packit 5756e2
                return 0;
Packit 5756e2
Packit 5756e2
        if (!st) {
Packit 5756e2
                if (stat(filename, &_st) < 0)
Packit 5756e2
                        return -errno;
Packit 5756e2
                st = &_st;
Packit 5756e2
        }
Packit 5756e2
Packit 5756e2
        if ((st->st_mode & S_IRWXO) == 0)
Packit 5756e2
                return 0;
Packit 5756e2
Packit 5756e2
        if (unit)
Packit 5756e2
                log_syntax(unit, LOG_WARNING, filename, line, 0,
Packit 5756e2
                           "%s has %04o mode that is too permissive, please adjust the ownership and access mode.",
Packit 5756e2
                           filename, st->st_mode & 07777);
Packit 5756e2
        else
Packit 5756e2
                log_warning("%s has %04o mode that is too permissive, please adjust the ownership and access mode.",
Packit 5756e2
                            filename, st->st_mode & 07777);
Packit 5756e2
        return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
#if 0 /* NM_IGNORED */
Packit 5756e2
int sync_rights(int from, int to) {
Packit 5756e2
        struct stat st;
Packit 5756e2
Packit 5756e2
        if (fstat(from, &st) < 0)
Packit 5756e2
                return -errno;
Packit 5756e2
Packit 5756e2
        return fchmod_and_chown(to, st.st_mode & 07777, st.st_uid, st.st_gid);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int rename_and_apply_smack_floor_label(const char *from, const char *to) {
Packit 5756e2
        int r = 0;
Packit 5756e2
        if (rename(from, to) < 0)
Packit 5756e2
                return -errno;
Packit 5756e2
Packit 5756e2
#ifdef SMACK_RUN_LABEL
Packit 5756e2
        r = mac_smack_apply(to, SMACK_ATTR_ACCESS, SMACK_FLOOR_LABEL);
Packit 5756e2
        if (r < 0)
Packit 5756e2
                return r;
Packit 5756e2
#endif
Packit 5756e2
        return r;
Packit 5756e2
}
Packit 5756e2
#endif /* NM_IGNORED */