This might make things marginially faster. I didn't benchmark though.
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,
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) {
/* 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) {
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);
/* 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"
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.
/* 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"
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.
/* SPDX-License-Identifier: LGPL-2.1+ */
#include "alloc-util.h"
+#include "fileio.h"
#include "fd-util.h"
#include "fuzz.h"
#include "json.h"
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);
/* 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"
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.
/* 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"
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.
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)
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, "");
#include "bus-util.h"
#include "escape.h"
#include "fd-util.h"
+#include "fileio.h"
#include "log.h"
#include "tests.h"
#include "util.h"
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));
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));
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));
#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"
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");
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");
_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++) {
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);
_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);
_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);
#include "alloc-util.h"
#include "fd-util.h"
+#include "fileio.h"
#include "json-internal.h"
#include "json.h"
#include "string-util.h"
"%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);