Merge pull request #12217 from keszybz/unlocked-operations

Refactor how we do unlocked file operations
This commit is contained in:
Lennart Poettering 2019-04-12 13:51:53 +02:00 committed by GitHub
commit 3661dc349e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
52 changed files with 294 additions and 326 deletions

View File

@ -0,0 +1,71 @@
@@
expression f, path, options;
@@
- f = fopen(path, options);
- if (!f)
- return -errno;
- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+ r = fopen_unlocked(path, options, &f);
+ if (r < 0)
+ return r;
@@
expression f, path, options;
@@
- f = fopen(path, options);
- if (!f) {
- if (errno == ENOENT)
- return -ESRCH;
- return -errno;
- }
- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+ r = fopen_unlocked(path, options, &f);
+ if (r == -ENOENT)
+ return -ESRCH;
+ if (r < 0)
+ return r;
@@
expression f, path, options;
@@
- f = fopen(path, options);
- if (!f)
- return errno == ENOENT ? -ESRCH : -errno;
- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+ r = fopen_unlocked(path, options, &f);
+ if (r == -ENOENT)
+ return -ESRCH;
+ if (r < 0)
+ return r;
@@
expression f, path, p;
@@
r = fopen_temporary(path, &f, &p);
if (r < 0)
return ...;
- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
@@
expression f, g, path, p;
@@
r = fopen_temporary_label(path, g, &f, &p);
if (r < 0)
return ...;
- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
@@
expression f, fd, options;
@@
- f = fdopen(fd, options);
+ r = fdopen_unlocked(fd, options, &f);
+ if (r < 0) {
- if (!f) {
...
- return -errno;
+ return r;
}
- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
@@
expression f, buf, sz;
@@
- f = open_memstream(&buf, &sz);
+ f = open_memstream_unlocked(&buf, &sz);
if (!f)
return ...;
- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);

View File

@ -6,7 +6,6 @@
#include <limits.h>
#include <signal.h>
#include <stddef.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
@ -1016,11 +1015,11 @@ int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
}
fs = procfs_file_alloca(pid, "cgroup");
f = fopen(fs, "re");
if (!f)
return errno == ENOENT ? -ESRCH : -errno;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
r = fopen_unlocked(fs, "re", &f);
if (r == -ENOENT)
return -ESRCH;
if (r < 0)
return r;
for (;;) {
_cleanup_free_ char *line = NULL;
@ -2442,17 +2441,13 @@ int cg_kernel_controllers(Set **ret) {
if (!controllers)
return -ENOMEM;
f = fopen("/proc/cgroups", "re");
if (!f) {
if (errno == ENOENT) {
*ret = NULL;
return 0;
}
return -errno;
r = fopen_unlocked("/proc/cgroups", "re", &f);
if (r == -ENOENT) {
*ret = NULL;
return 0;
}
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
if (r < 0)
return r;
/* Ignore the header line */
(void) read_line(f, (size_t) -1, NULL);

View File

@ -1,7 +1,5 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <stdio_ext.h>
#include "alloc-util.h"
#include "env-file.h"
#include "env-util.h"
@ -545,7 +543,6 @@ int write_env_file(const char *fname, char **l) {
if (r < 0)
return r;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod_umask(fileno(f), 0644);
STRV_FOREACH(i, l)

View File

@ -29,6 +29,52 @@
#define READ_FULL_BYTES_MAX (4U*1024U*1024U)
int fopen_unlocked(const char *path, const char *options, FILE **ret) {
assert(ret);
FILE *f = fopen(path, options);
if (!f)
return -errno;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
*ret = f;
return 0;
}
int fdopen_unlocked(int fd, const char *options, FILE **ret) {
assert(ret);
FILE *f = fdopen(fd, options);
if (!f)
return -errno;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
*ret = f;
return 0;
}
FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc) {
FILE *f = open_memstream(ptr, sizeloc);
if (!f)
return NULL;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
return f;
}
FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode) {
FILE *f = fmemopen(buf, size, mode);
if (!f)
return NULL;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
return f;
}
int write_string_stream_ts(
FILE *f,
const char *line,
@ -95,7 +141,6 @@ static int write_string_file_atomic(
if (r < 0)
return r;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod_umask(fileno(f), 0644);
r = write_string_stream_ts(f, line, flags, ts);
@ -141,11 +186,9 @@ int write_string_file_ts(
assert(!ts);
if (flags & WRITE_STRING_FILE_CREATE) {
f = fopen(fn, "we");
if (!f) {
r = -errno;
r = fopen_unlocked(fn, "we", &f);
if (r < 0)
goto fail;
}
} else {
int fd;
@ -157,16 +200,13 @@ int write_string_file_ts(
goto fail;
}
f = fdopen(fd, "w");
if (!f) {
r = -errno;
r = fdopen_unlocked(fd, "w", &f);
if (r < 0) {
safe_close(fd);
goto fail;
}
}
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
if (flags & WRITE_STRING_FILE_DISABLE_BUFFER)
setvbuf(f, NULL, _IONBF, 0);
@ -213,15 +253,14 @@ int write_string_filef(
int read_one_line_file(const char *fn, char **line) {
_cleanup_fclose_ FILE *f = NULL;
int r;
assert(fn);
assert(line);
f = fopen(fn, "re");
if (!f)
return -errno;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
r = fopen_unlocked(fn, "re", &f);
if (r < 0)
return r;
return read_line(f, LONG_LINE_MAX, line);
}
@ -230,6 +269,7 @@ int verify_file(const char *fn, const char *blob, bool accept_extra_nl) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *buf = NULL;
size_t l, k;
int r;
assert(fn);
assert(blob);
@ -243,11 +283,9 @@ int verify_file(const char *fn, const char *blob, bool accept_extra_nl) {
if (!buf)
return -ENOMEM;
f = fopen(fn, "re");
if (!f)
return -errno;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
r = fopen_unlocked(fn, "re", &f);
if (r < 0)
return r;
/* We try to read one byte more than we need, so that we know whether we hit eof */
errno = 0;
@ -390,15 +428,14 @@ finalize:
int read_full_file_full(const char *filename, ReadFullFileFlags flags, char **contents, size_t *size) {
_cleanup_fclose_ FILE *f = NULL;
int r;
assert(filename);
assert(contents);
f = fopen(filename, "re");
if (!f)
return -errno;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
r = fopen_unlocked(filename, "re", &f);
if (r < 0)
return r;
return read_full_stream_full(f, filename, flags, contents, size);
}

View File

@ -33,6 +33,11 @@ typedef enum {
READ_FULL_FILE_UNBASE64 = 1 << 1,
} ReadFullFileFlags;
int fopen_unlocked(const char *path, const char *options, FILE **ret);
int fdopen_unlocked(int fd, const char *options, FILE **ret);
FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc);
FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode);
int write_string_stream_ts(FILE *f, const char *line, WriteStringFileFlags flags, struct timespec *ts);
static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) {
return write_string_stream_ts(f, line, flags, NULL);

View File

@ -2,7 +2,6 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio_ext.h>
#include <sys/mount.h>
#include "alloc-util.h"
@ -378,11 +377,9 @@ int dev_is_devtmpfs(void) {
if (r < 0)
return r;
proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
if (!proc_self_mountinfo)
return -errno;
(void) __fsetlocking(proc_self_mountinfo, FSETLOCKING_BYCALLER);
r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo);
if (r < 0)
return r;
for (;;) {
_cleanup_free_ char *line = NULL;

View File

@ -8,7 +8,6 @@
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
@ -106,7 +105,7 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
char *k;
_cleanup_free_ char *ans = NULL;
const char *p;
int c;
int c, r;
assert(line);
assert(pid >= 0);
@ -121,15 +120,11 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
* comm_fallback is false). Returns 0 and sets *line otherwise. */
p = procfs_file_alloca(pid, "cmdline");
f = fopen(p, "re");
if (!f) {
if (errno == ENOENT)
return -ESRCH;
return -errno;
}
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
r = fopen_unlocked(p, "re", &f);
if (r == -ENOENT)
return -ESRCH;
if (r < 0)
return r;
if (max_length == 0) {
/* This is supposed to be a safety guard against runaway command lines. */
@ -513,14 +508,11 @@ static int get_process_id(pid_t pid, const char *field, uid_t *uid) {
return -EINVAL;
p = procfs_file_alloca(pid, "status");
f = fopen(p, "re");
if (!f) {
if (errno == ENOENT)
return -ESRCH;
return -errno;
}
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
r = fopen_unlocked(p, "re", &f);
if (r == -ENOENT)
return -ESRCH;
if (r < 0)
return r;
for (;;) {
_cleanup_free_ char *line = NULL;
@ -602,14 +594,11 @@ int get_process_environ(pid_t pid, char **env) {
p = procfs_file_alloca(pid, "environ");
f = fopen(p, "re");
if (!f) {
if (errno == ENOENT)
return -ESRCH;
return -errno;
}
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
r = fopen_unlocked(p, "re", &f);
if (r == -ENOENT)
return -ESRCH;
if (r < 0)
return r;
for (;;) {
char c;
@ -895,15 +884,11 @@ int getenv_for_pid(pid_t pid, const char *field, char **ret) {
path = procfs_file_alloca(pid, "environ");
f = fopen(path, "re");
if (!f) {
if (errno == ENOENT)
return -ESRCH;
return -errno;
}
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
r = fopen_unlocked(path, "re", &f);
if (r == -ENOENT)
return -ESRCH;
if (r < 0)
return r;
l = strlen(field);
for (;;) {

View File

@ -4,7 +4,6 @@
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
@ -761,23 +760,20 @@ char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) {
* 2. Strips ANSI color sequences (a subset of CSI), i.e. ESC '[' 'm' sequences
* 3. Strips ANSI operating system sequences (CSO), i.e. ESC ']' BEL sequences
*
* Everything else will be left as it is. In particular other ANSI sequences are left as they are, as are any
* other special characters. Truncated ANSI sequences are left-as is too. This call is supposed to suppress the
* most basic formatting noise, but nothing else.
* Everything else will be left as it is. In particular other ANSI sequences are left as they are, as
* are any other special characters. Truncated ANSI sequences are left-as is too. This call is
* supposed to suppress the most basic formatting noise, but nothing else.
*
* Why care for CSO sequences? Well, to undo what terminal_urlify() and friends generate. */
isz = _isz ? *_isz : strlen(*ibuf);
f = open_memstream(&obuf, &osz);
/* Note we turn off internal locking on f for performance reasons. It's safe to do so since we
* created f here and it doesn't leave our scope. */
f = open_memstream_unlocked(&obuf, &osz);
if (!f)
return NULL;
/* Note we turn off internal locking on f for performance reasons. It's safe to do so since we created f here
* and it doesn't leave our scope. */
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
for (i = *ibuf; i < *ibuf + isz + 1; i++) {
switch (state) {

View File

@ -1,9 +1,11 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <stdio.h>
#include <sys/mman.h>
#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "hexdecoct.h"
#include "macro.h"
@ -37,12 +39,15 @@ int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
return -errno;
}
f = fdopen(fd, "w");
if (!f) {
unlink_noerrno(t);
/* This assumes that returned FILE object is short-lived and used within the same single-threaded
* context and never shared externally, hence locking is not necessary. */
r = fdopen_unlocked(fd, "w", &f);
if (r < 0) {
unlink(t);
free(t);
safe_close(fd);
return -errno;
return r;
}
*_f = f;

View File

@ -1,7 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <getopt.h>
#include <stdio_ext.h>
#include "sd-bus.h"
@ -997,12 +996,10 @@ static int introspect(int argc, char **argv, void *userdata) {
if (r < 0)
return bus_log_parse_error(r);
mf = open_memstream(&buf, &sz);
mf = open_memstream_unlocked(&buf, &sz);
if (!mf)
return log_oom();
(void) __fsetlocking(mf, FSETLOCKING_BYCALLER);
r = format_cmdline(reply, mf, false);
if (r < 0)
return bus_log_parse_error(r);

View File

@ -1,7 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <arpa/inet.h>
#include <stdio_ext.h>
#include "af-list.h"
#include "alloc-util.h"
@ -821,12 +820,10 @@ int bus_cgroup_set_property(
unit_invalidate_cgroup(u, CGROUP_MASK_IO);
f = open_memstream(&buf, &size);
f = open_memstream_unlocked(&buf, &size);
if (!f)
return -ENOMEM;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
fprintf(f, "%s=\n", name);
LIST_FOREACH(device_limits, a, c->io_device_limits)
if (a->limits[iol_type] != cgroup_io_limit_defaults[iol_type])
@ -903,12 +900,10 @@ int bus_cgroup_set_property(
unit_invalidate_cgroup(u, CGROUP_MASK_IO);
f = open_memstream(&buf, &size);
f = open_memstream_unlocked(&buf, &size);
if (!f)
return -ENOMEM;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
fputs("IODeviceWeight=\n", f);
LIST_FOREACH(device_weights, a, c->io_device_weights)
fprintf(f, "IODeviceWeight=%s %" PRIu64 "\n", a->path, a->weight);
@ -982,12 +977,10 @@ int bus_cgroup_set_property(
unit_invalidate_cgroup(u, CGROUP_MASK_IO);
f = open_memstream(&buf, &size);
f = open_memstream_unlocked(&buf, &size);
if (!f)
return -ENOMEM;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
fputs("IODeviceLatencyTargetSec=\n", f);
LIST_FOREACH(device_latencies, a, c->io_device_latencies)
fprintf(f, "IODeviceLatencyTargetSec=%s %s\n",
@ -1077,12 +1070,10 @@ int bus_cgroup_set_property(
unit_invalidate_cgroup(u, CGROUP_MASK_BLKIO);
f = open_memstream(&buf, &size);
f = open_memstream_unlocked(&buf, &size);
if (!f)
return -ENOMEM;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
if (read) {
fputs("BlockIOReadBandwidth=\n", f);
LIST_FOREACH(device_bandwidths, a, c->blockio_device_bandwidths)
@ -1167,12 +1158,10 @@ int bus_cgroup_set_property(
unit_invalidate_cgroup(u, CGROUP_MASK_BLKIO);
f = open_memstream(&buf, &size);
f = open_memstream_unlocked(&buf, &size);
if (!f)
return -ENOMEM;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
fputs("BlockIODeviceWeight=\n", f);
LIST_FOREACH(device_weights, a, c->blockio_device_weights)
fprintf(f, "BlockIODeviceWeight=%s %" PRIu64 "\n", a->path, a->weight);
@ -1275,12 +1264,10 @@ int bus_cgroup_set_property(
unit_invalidate_cgroup(u, CGROUP_MASK_DEVICES);
f = open_memstream(&buf, &size);
f = open_memstream_unlocked(&buf, &size);
if (!f)
return -ENOMEM;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
fputs("DeviceAllow=\n", f);
LIST_FOREACH(device_allow, a, c->device_allow)
fprintf(f, "DeviceAllow=%s %s%s%s\n", a->path, a->r ? "r" : "", a->w ? "w" : "", a->m ? "m" : "");
@ -1390,12 +1377,10 @@ int bus_cgroup_set_property(
*list = ip_address_access_free_all(*list);
unit_invalidate_cgroup_bpf(u);
f = open_memstream(&buf, &size);
f = open_memstream_unlocked(&buf, &size);
if (!f)
return -ENOMEM;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
fputs(name, f);
fputs("=\n", f);

View File

@ -2,7 +2,6 @@
#include <sys/mount.h>
#include <sys/prctl.h>
#include <stdio_ext.h>
#if HAVE_SECCOMP
#include <seccomp.h>
@ -958,12 +957,10 @@ int bus_set_transient_exec_command(
if (n == 0)
*exec_command = exec_command_free_list(*exec_command);
f = open_memstream(&buf, &size);
f = open_memstream_unlocked(&buf, &size);
if (!f)
return -ENOMEM;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
fputs("ExecStart=\n", f);
LIST_FOREACH(command, c, *exec_command) {
@ -2015,12 +2012,10 @@ int bus_exec_context_set_transient_property(
if (r < 0)
return r;
f = open_memstream(&joined, &size);
f = open_memstream_unlocked(&joined, &size);
if (!f)
return -ENOMEM;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
fputs("EnvironmentFile=\n", f);
STRV_FOREACH(i, c->environment_files) {

View File

@ -1,6 +1,5 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <stdio_ext.h>
#include <fcntl.h>
#include "alloc-util.h"

View File

@ -4,7 +4,6 @@
#include <fcntl.h>
#include <linux/kd.h>
#include <signal.h>
#include <stdio_ext.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/inotify.h>
@ -2111,12 +2110,10 @@ int manager_get_dump_string(Manager *m, char **ret) {
assert(m);
assert(ret);
f = open_memstream(&dump, &size);
f = open_memstream_unlocked(&dump, &size);
if (!f)
return -errno;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
manager_dump(m, f, NULL);
r = fflush_and_check(f);

View File

@ -9,7 +9,6 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@ -26,12 +25,36 @@
#if ENABLE_SMACK
static int write_access2_rules(const char* srcdir) {
static int fdopen_unlocked_at(int dfd, const char *dir, const char *name, int *status, FILE **ret_file) {
int fd, r;
FILE *f;
fd = openat(dfd, name, O_RDONLY|O_CLOEXEC);
if (fd < 0) {
if (*status == 0)
*status = -errno;
return log_warning_errno(errno, "Failed to open \"%s/%s\": %m", dir, name);
}
r = fdopen_unlocked(fd, "r", &f);
if (r < 0) {
if (*status == 0)
*status = r;
safe_close(fd);
return log_error_errno(r, "Failed to open \"%s/%s\": %m", dir, name);
}
*ret_file = f;
return 0;
}
static int write_access2_rules(const char *srcdir) {
_cleanup_close_ int load2_fd = -1, change_fd = -1;
_cleanup_closedir_ DIR *dir = NULL;
struct dirent *entry;
int dfd = -1;
int r = 0;
int dfd = -1, r = 0;
load2_fd = open("/sys/fs/smackfs/load2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (load2_fd < 0) {
@ -59,28 +82,13 @@ static int write_access2_rules(const char* srcdir) {
assert(dfd >= 0);
FOREACH_DIRENT(entry, dir, return 0) {
int fd;
_cleanup_fclose_ FILE *policy = NULL;
if (!dirent_is_file(entry))
continue;
fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
if (fd < 0) {
if (r == 0)
r = -errno;
log_warning_errno(errno, "Failed to open '%s': %m", entry->d_name);
if (fdopen_unlocked_at(dfd, srcdir, entry->d_name, &r, &policy) < 0)
continue;
}
policy = fdopen(fd, "r");
if (!policy) {
if (r == 0)
r = -errno;
safe_close(fd);
log_error_errno(errno, "Failed to open '%s': %m", entry->d_name);
continue;
}
/* load2 write rules in the kernel require a line buffered stream */
for (;;) {
@ -115,12 +123,11 @@ static int write_access2_rules(const char* srcdir) {
return r;
}
static int write_cipso2_rules(const char* srcdir) {
static int write_cipso2_rules(const char *srcdir) {
_cleanup_close_ int cipso2_fd = -1;
_cleanup_closedir_ DIR *dir = NULL;
struct dirent *entry;
int dfd = -1;
int r = 0;
int dfd = -1, r = 0;
cipso2_fd = open("/sys/fs/smackfs/cipso2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (cipso2_fd < 0) {
@ -141,28 +148,13 @@ static int write_cipso2_rules(const char* srcdir) {
assert(dfd >= 0);
FOREACH_DIRENT(entry, dir, return 0) {
int fd;
_cleanup_fclose_ FILE *policy = NULL;
if (!dirent_is_file(entry))
continue;
fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
if (fd < 0) {
if (r == 0)
r = -errno;
log_error_errno(errno, "Failed to open '%s': %m", entry->d_name);
if (fdopen_unlocked_at(dfd, srcdir, entry->d_name, &r, &policy) < 0)
continue;
}
policy = fdopen(fd, "r");
if (!policy) {
if (r == 0)
r = -errno;
safe_close(fd);
log_error_errno(errno, "Failed to open '%s': %m", entry->d_name);
continue;
}
/* cipso2 write rules in the kernel require a line buffered stream */
for (;;) {
@ -191,12 +183,11 @@ static int write_cipso2_rules(const char* srcdir) {
return r;
}
static int write_netlabel_rules(const char* srcdir) {
static int write_netlabel_rules(const char *srcdir) {
_cleanup_fclose_ FILE *dst = NULL;
_cleanup_closedir_ DIR *dir = NULL;
struct dirent *entry;
int dfd = -1;
int r = 0;
int dfd = -1, r = 0;
dst = fopen("/sys/fs/smackfs/netlabel", "we");
if (!dst) {
@ -217,27 +208,10 @@ static int write_netlabel_rules(const char* srcdir) {
assert(dfd >= 0);
FOREACH_DIRENT(entry, dir, return 0) {
int fd;
_cleanup_fclose_ FILE *policy = NULL;
fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
if (fd < 0) {
if (r == 0)
r = -errno;
log_warning_errno(errno, "Failed to open %s: %m", entry->d_name);
if (fdopen_unlocked_at(dfd, srcdir, entry->d_name, &r, &policy) < 0)
continue;
}
policy = fdopen(fd, "r");
if (!policy) {
if (r == 0)
r = -errno;
safe_close(fd);
log_error_errno(errno, "Failed to open %s: %m", entry->d_name);
continue;
}
(void) __fsetlocking(policy, FSETLOCKING_BYCALLER);
/* load2 write rules in the kernel require a line buffered stream */
for (;;) {

View File

@ -2,7 +2,6 @@
#include <errno.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <sys/prctl.h>
#include <sys/xattr.h>
#include <unistd.h>
@ -530,12 +529,10 @@ static int compose_open_fds(pid_t pid, char **open_fds) {
if (proc_fdinfo_fd < 0)
return -errno;
stream = open_memstream(&buffer, &size);
stream = open_memstream_unlocked(&buffer, &size);
if (!stream)
return -ENOMEM;
(void) __fsetlocking(stream, FSETLOCKING_BYCALLER);
FOREACH_DIRENT(dent, proc_fd_dir, return -errno) {
_cleanup_fclose_ FILE *fdinfo = NULL;
_cleanup_free_ char *fdname = NULL;

View File

@ -2,11 +2,11 @@
#include <dwarf.h>
#include <elfutils/libdwfl.h>
#include <stdio_ext.h>
#include <sys/types.h>
#include <unistd.h>
#include "alloc-util.h"
#include "fileio.h"
#include "fd-util.h"
#include "format-util.h"
#include "macro.h"
@ -126,12 +126,10 @@ int coredump_make_stack_trace(int fd, const char *executable, char **ret) {
if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
return -errno;
c.f = open_memstream(&buf, &sz);
c.f = open_memstream_unlocked(&buf, &sz);
if (!c.f)
return -ENOMEM;
(void) __fsetlocking(c.f, FSETLOCKING_BYCALLER);
elf_version(EV_CURRENT);
c.elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);

View File

@ -2,7 +2,6 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio_ext.h>
#include <sys/stat.h>
#include <sys/types.h>
@ -479,15 +478,13 @@ static int add_crypttab_devices(void) {
if (!arg_read_crypttab)
return 0;
f = fopen("/etc/crypttab", "re");
if (!f) {
r = fopen_unlocked("/etc/crypttab", "re", &f);
if (r < 0) {
if (errno != ENOENT)
log_error_errno(errno, "Failed to open /etc/crypttab: %m");
return 0;
}
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
if (fstat(fileno(f), &st) < 0) {
log_error_errno(errno, "Failed to stat /etc/crypttab: %m");
return 0;

View File

@ -5,7 +5,6 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdio_ext.h>
#include "alloc-util.h"
#include "fd-util.h"

View File

@ -1,13 +1,11 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <stdio.h>
#include "alloc-util.h"
#include "bus-dump.h"
#include "bus-message.h"
#include "env-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fuzz.h"
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
@ -36,7 +34,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
TAKE_PTR(buffer);
if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0)
assert_se(g = open_memstream(&out, &out_size));
assert_se(g = open_memstream_unlocked(&out, &out_size));
bus_message_dump(m, g ?: stdout, BUS_MESSAGE_DUMP_WITH_HEADER);

View File

@ -1,9 +1,10 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <stdio.h>
#include "alloc-util.h"
#include "env-file.h"
#include "fileio.h"
#include "fd-util.h"
#include "fuzz.h"
#include "strv.h"
@ -15,7 +16,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (size == 0 || size > 65535)
return 0;
f = fmemopen((char*) data, size, "re");
f = fmemopen_unlocked((char*) data, size, "re");
assert_se(f);
/* We don't want to fill the logs with messages about parse errors.

View File

@ -1,9 +1,8 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fuzz.h"
#include "hostname-util.h"
@ -14,7 +13,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (size == 0)
return 0;
f = fmemopen((char*) data, size, "re");
f = fmemopen_unlocked((char*) data, size, "re");
assert_se(f);
/* We don't want to fill the logs with messages about parse errors.

View File

@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include "alloc-util.h"
#include "fileio.h"
#include "fd-util.h"
#include "fuzz.h"
#include "json.h"
@ -14,13 +15,13 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (size == 0)
return 0;
f = fmemopen((char*) data, size, "re");
f = fmemopen_unlocked((char*) data, size, "re");
assert_se(f);
if (json_parse_file(f, NULL, &v, NULL, NULL) < 0)
return 0;
g = open_memstream(&out, &out_size);
g = open_memstream_unlocked(&out, &out_size);
assert_se(g);
json_variant_dump(v, 0, g, NULL);

View File

@ -1,9 +1,8 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fuzz.h"
#include "nspawn-oci.h"
@ -14,7 +13,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (size == 0)
return 0;
f = fmemopen((char*) data, size, "re");
f = fmemopen_unlocked((char*) data, size, "re");
assert_se(f);
/* We don't want to fill the logs with messages about parse errors.

View File

@ -1,9 +1,8 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fuzz.h"
#include "nspawn-settings.h"
@ -14,7 +13,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (size == 0)
return 0;
f = fmemopen((char*) data, size, "re");
f = fmemopen_unlocked((char*) data, size, "re");
assert_se(f);
/* We don't want to fill the logs with messages about parse errors.

View File

@ -24,7 +24,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (size == 0)
return 0;
f = fmemopen((char*) data, size, "re");
f = fmemopen_unlocked((char*) data, size, "re");
assert_se(f);
if (read_line(f, LINE_MAX, &p) < 0)
@ -75,7 +75,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
config_item_perf_lookup, load_fragment_gperf_lookup,
CONFIG_PARSE_ALLOW_INCLUDE, u);
g = open_memstream(&out, &out_size);
g = open_memstream_unlocked(&out, &out_size);
assert_se(g);
unit_dump(u, g, "");

View File

@ -4,9 +4,9 @@
#include <qrencode.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include "fileio.h"
#include "journal-qrcode.h"
#include "macro.h"
@ -45,12 +45,10 @@ int print_qr_code(
assert(seed);
assert(seed_size > 0);
f = open_memstream(&url, &url_size);
f = open_memstream_unlocked(&url, &url_size);
if (!f)
return -ENOMEM;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
fputs("fss://", f);
for (i = 0; i < seed_size; i++) {

View File

@ -6,7 +6,6 @@
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
@ -832,7 +831,6 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
if (r < 0)
goto fail;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fprintf(f,

View File

@ -1,7 +1,5 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <stdio_ext.h>
#include "bus-internal.h"
#include "bus-introspect.h"
#include "bus-objects.h"
@ -18,12 +16,10 @@ int introspect_begin(struct introspect *i, bool trusted) {
zero(*i);
i->trusted = trusted;
i->f = open_memstream(&i->introspection, &i->size);
i->f = open_memstream_unlocked(&i->introspection, &i->size);
if (!i->f)
return -ENOMEM;
(void) __fsetlocking(i->f, FSETLOCKING_BYCALLER);
fputs(BUS_INTROSPECT_DOCTYPE
"<node>\n", i->f);

View File

@ -1,7 +1,5 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <stdio_ext.h>
#include "alloc-util.h"
#include "bus-internal.h"
#include "bus-match.h"
@ -861,12 +859,10 @@ char *bus_match_to_string(struct bus_match_component *components, unsigned n_com
assert(components);
f = open_memstream(&buffer, &size);
f = open_memstream_unlocked(&buffer, &size);
if (!f)
return NULL;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
for (i = 0; i < n_components; i++) {
char buf[32];

View File

@ -20,6 +20,7 @@
#include "bus-util.h"
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
#include "log.h"
#include "tests.h"
#include "util.h"
@ -189,7 +190,7 @@ int main(int argc, char *argv[]) {
bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
ms = open_memstream(&first, &first_size);
ms = open_memstream_unlocked(&first, &first_size);
bus_message_dump(m, ms, 0);
fflush(ms);
assert_se(!ferror(ms));
@ -245,7 +246,7 @@ int main(int argc, char *argv[]) {
bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
fclose(ms);
ms = open_memstream(&second, &second_size);
ms = open_memstream_unlocked(&second, &second_size);
bus_message_dump(m, ms, 0);
fflush(ms);
assert_se(!ferror(ms));
@ -351,7 +352,7 @@ int main(int argc, char *argv[]) {
assert_se(r >= 0);
fclose(ms);
ms = open_memstream(&third, &third_size);
ms = open_memstream_unlocked(&third, &third_size);
bus_message_dump(copy, ms, 0);
fflush(ms);
assert_se(!ferror(ms));

View File

@ -1,7 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <stdio_ext.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
@ -423,7 +422,6 @@ int x11_write_data(Context *c) {
if (r < 0)
return r;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fputs("# Written by systemd-localed(8), read by systemd-localed and Xorg. It's\n"

View File

@ -2,7 +2,6 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio_ext.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
@ -97,7 +96,6 @@ int seat_save(Seat *s) {
if (r < 0)
goto fail;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fprintf(f,

View File

@ -5,7 +5,6 @@
#include <linux/kd.h>
#include <linux/vt.h>
#include <signal.h>
#include <stdio_ext.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
@ -214,7 +213,6 @@ int session_save(Session *s) {
if (r < 0)
goto fail;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fprintf(f,

View File

@ -3,7 +3,6 @@
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdio_ext.h>
#include "alloc-util.h"
#include "bus-common-errors.h"
@ -162,7 +161,6 @@ static int user_save_internal(User *u) {
if (r < 0)
goto fail;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fprintf(f,

View File

@ -3,7 +3,6 @@
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdio_ext.h>
#include <sys/stat.h>
#include "sd-messages.h"
@ -126,7 +125,6 @@ int machine_save(Machine *m) {
if (r < 0)
goto fail;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fprintf(f,

View File

@ -4,7 +4,6 @@
#include <linux/if.h>
#include <linux/can/netlink.h>
#include <unistd.h>
#include <stdio_ext.h>
#include "alloc-util.h"
#include "bus-util.h"
@ -4034,7 +4033,6 @@ int link_save(Link *link) {
if (r < 0)
goto fail;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fprintf(f,

View File

@ -3,7 +3,6 @@
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/fib_rules.h>
#include <stdio_ext.h>
#include <unistd.h>
#include "sd-daemon.h"
@ -1189,7 +1188,6 @@ static int manager_save(Manager *m) {
if (r < 0)
return r;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fprintf(f,

View File

@ -1,7 +1,5 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <stdio_ext.h>
#include "bus-common-errors.h"
#include "bus-error.h"
#include "conf-files.h"
@ -1091,12 +1089,10 @@ static int test_chroot_dropin(
return log_debug_errno(errno, "Failed to open %s/%s: %m", where, p);
}
f = fdopen(fd, "r");
if (!f)
return log_debug_errno(errno, "Failed to convert file handle: %m");
fd = -1;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
r = fdopen_unlocked(fd, "r", &f);
if (r < 0)
return log_debug_errno(r, "Failed to convert file handle: %m");
TAKE_FD(fd);
r = read_line(f, LONG_LINE_MAX, &line);
if (r < 0)

View File

@ -12,6 +12,7 @@
#include "dirent-util.h"
#include "env-file.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-table.h"
#include "fs-util.h"
#include "locale-util.h"
@ -272,10 +273,9 @@ static int inspect_image(int argc, char *argv[], void *userdata) {
nl = true;
} else {
_cleanup_free_ char *pretty_portable = NULL, *pretty_os = NULL;
_cleanup_fclose_ FILE *f;
f = fmemopen((void*) data, sz, "re");
f = fmemopen_unlocked((void*) data, sz, "re");
if (!f)
return log_error_errno(errno, "Failed to open /etc/os-release buffer: %m");

View File

@ -1,9 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <stdio_ext.h>
#if HAVE_GCRYPT
#include <gcrypt.h>
# include <gcrypt.h>
#endif
#include "alloc-util.h"
@ -803,10 +801,9 @@ int dnssec_verify_rrset(
/* Bring the RRs into canonical order */
typesafe_qsort(list, n, rr_compare);
f = open_memstream(&sig_data, &sig_size);
f = open_memstream_unlocked(&sig_data, &sig_size);
if (!f)
return -ENOMEM;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
fwrite_uint16(f, rrsig->rrsig.type_covered);
fwrite_uint8(f, rrsig->rrsig.algorithm);

View File

@ -2,7 +2,6 @@
#include <net/if.h>
#include <linux/if.h>
#include <stdio_ext.h>
#include <unistd.h>
#include "sd-network.h"
@ -1178,7 +1177,6 @@ int link_save_user(Link *l) {
if (r < 0)
goto fail;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fputs("# This is private data. Do not parse.\n", f);

View File

@ -3,7 +3,6 @@
#include <fcntl.h>
#include <netinet/in.h>
#include <poll.h>
#include <stdio_ext.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
@ -511,12 +510,10 @@ static int manager_sigusr1(sd_event_source *s, const struct signalfd_siginfo *si
assert(si);
assert(m);
f = open_memstream(&buffer, &size);
f = open_memstream_unlocked(&buffer, &size);
if (!f)
return log_oom();
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
LIST_FOREACH(scopes, scope, m->dns_scopes)
dns_scope_dump(scope, f);

View File

@ -1,7 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <resolv.h>
#include <stdio_ext.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
@ -359,14 +358,12 @@ int manager_write_resolv_conf(Manager *m) {
if (r < 0)
return log_warning_errno(r, "Failed to open private resolv.conf file for writing: %m");
(void) __fsetlocking(f_uplink, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f_uplink), 0644);
r = fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF, PRIVATE_STUB_RESOLV_CONF, &f_stub, &temp_path_stub);
if (r < 0)
return log_warning_errno(r, "Failed to open private stub-resolv.conf file for writing: %m");
(void) __fsetlocking(f_stub, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f_stub), 0644);
r = write_uplink_resolv_conf_contents(f_uplink, dns, domains);

View File

@ -298,7 +298,7 @@ static int boot_entry_load_unified(
if (!k)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Path is not below root: %s", path);
f = fmemopen((void*) osrelease, strlen(osrelease), "r");
f = fmemopen_unlocked((void*) osrelease, strlen(osrelease), "r");
if (!f)
return log_error_errno(errno, "Failed to open os-release buffer: %m");

View File

@ -6,7 +6,6 @@
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
@ -330,12 +329,10 @@ int calendar_spec_to_string(const CalendarSpec *c, char **p) {
assert(c);
assert(p);
f = open_memstream(&buf, &sz);
f = open_memstream_unlocked(&buf, &sz);
if (!f)
return -ENOMEM;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
if (c->weekdays_bits > 0 && c->weekdays_bits <= BITS_WEEKDAYS) {
format_weekdays(f, c);
fputc(' ', f);

View File

@ -1,7 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <ctype.h>
#include <stdio_ext.h>
#include "alloc-util.h"
#include "fd-util.h"
@ -1376,12 +1375,10 @@ int table_format(Table *t, char **ret) {
size_t sz = 0;
int r;
f = open_memstream(&buf, &sz);
f = open_memstream_unlocked(&buf, &sz);
if (!f)
return -ENOMEM;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
r = table_print(t, f);
if (r < 0)
return r;

View File

@ -1,7 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <stdio_ext.h>
#include <unistd.h>
#include "alloc-util.h"
@ -30,23 +29,22 @@ int generator_open_unit_file(
const char *unit;
FILE *f;
int r;
unit = strjoina(dest, "/", name);
f = fopen(unit, "wxe");
if (!f) {
if (source && errno == EEXIST)
return log_error_errno(errno,
r = fopen_unlocked(unit, "wxe", &f);
if (r < 0) {
if (source && r == -EEXIST)
return log_error_errno(r,
"Failed to create unit file %s, as it already exists. Duplicate entry in %s?",
unit, source);
else
return log_error_errno(errno,
return log_error_errno(r,
"Failed to create unit file %s: %m",
unit);
}
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
fprintf(f,
"# Automatically generated by %s\n\n",
program_invocation_short_name);

View File

@ -4,7 +4,6 @@
#include <locale.h>
#include <math.h>
#include <stdarg.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
@ -1558,12 +1557,10 @@ int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret) {
{
_cleanup_fclose_ FILE *f = NULL;
f = open_memstream(&s, &sz);
f = open_memstream_unlocked(&s, &sz);
if (!f)
return -ENOMEM;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
json_variant_dump(v, flags, f, NULL);
r = fflush_and_check(f);

View File

@ -1,7 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
@ -40,13 +39,10 @@ int umount_recursive(const char *prefix, int flags) {
_cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
again = false;
r = 0;
proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
if (!proc_self_mountinfo)
return -errno;
(void) __fsetlocking(proc_self_mountinfo, FSETLOCKING_BYCALLER);
r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo);
if (r < 0)
return r;
for (;;) {
_cleanup_free_ char *path = NULL, *p = NULL;
@ -302,12 +298,11 @@ int bind_remount_recursive_with_mountinfo(
int bind_remount_recursive(const char *prefix, unsigned long new_flags, unsigned long flags_mask, char **blacklist) {
_cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
int r;
proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
if (!proc_self_mountinfo)
return -errno;
(void) __fsetlocking(proc_self_mountinfo, FSETLOCKING_BYCALLER);
r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo);
if (r < 0)
return r;
return bind_remount_recursive_with_mountinfo(prefix, new_flags, flags_mask, blacklist, proc_self_mountinfo);
}

View File

@ -637,7 +637,7 @@ static void test_fgetc(void) {
_cleanup_fclose_ FILE *f = NULL;
char c;
f = fmemopen((void*) chars, sizeof(chars), "re");
f = fmemopen_unlocked((void*) chars, sizeof(chars), "re");
assert_se(f);
for (unsigned i = 0; i < sizeof(chars); i++) {
@ -727,7 +727,7 @@ static void test_read_line_one_file(FILE *f) {
static void test_read_line(void) {
_cleanup_fclose_ FILE *f = NULL;
f = fmemopen((void*) buffer, sizeof(buffer), "re");
f = fmemopen_unlocked((void*) buffer, sizeof(buffer), "re");
assert_se(f);
test_read_line_one_file(f);
@ -792,7 +792,7 @@ static void test_read_line4(void) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *s = NULL;
assert_se(f = fmemopen((void*) eof_endings[i].string, eof_endings[i].length, "r"));
assert_se(f = fmemopen_unlocked((void*) eof_endings[i].string, eof_endings[i].length, "r"));
r = read_line(f, (size_t) -1, &s);
assert_se((size_t) r == eof_endings[i].length);
@ -813,7 +813,7 @@ static void test_read_nul_string(void) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *s = NULL;
assert_se(f = fmemopen((void*) test, sizeof(test)-1, "r"));
assert_se(f = fmemopen_unlocked((void*) test, sizeof(test)-1, "r"));
assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 13 && streq_ptr(s, "string nr. 1"));
s = mfree(s);

View File

@ -4,6 +4,7 @@
#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "json-internal.h"
#include "json.h"
#include "string-util.h"
@ -358,7 +359,7 @@ static void test_source(void) {
"%s"
"--- original end ---\n", data);
assert_se(f = fmemopen((void*) data, strlen(data), "r"));
assert_se(f = fmemopen_unlocked((void*) data, strlen(data), "r"));
assert_se(json_parse_file(f, "waldo", &v, NULL, NULL) >= 0);