From 2fe21124a6560fcf1ce3b0a3004baa9bb45d1e0b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 4 Apr 2019 11:46:44 +0200 Subject: [PATCH] Add open_memstream_unlocked() wrapper --- coccinelle/fopen-unlocked.cocci | 8 +++++++ src/basic/fileio.c | 10 +++++++++ src/basic/fileio.h | 1 + src/basic/string-util.c | 16 ++++++-------- src/basic/tmpfile-util.c | 1 - src/busctl/busctl.c | 5 +---- src/core/dbus-cgroup.c | 29 +++++++------------------- src/core/dbus-execute.c | 9 ++------ src/core/manager.c | 5 +---- src/core/smack-setup.c | 1 - src/coredump/coredump.c | 5 +---- src/coredump/stacktrace.c | 6 ++---- src/journal/journal-qrcode.c | 6 ++---- src/libsystemd/sd-bus/bus-introspect.c | 6 +----- src/libsystemd/sd-bus/bus-match.c | 6 +----- src/portable/portable.c | 2 -- src/resolve/resolved-dns-dnssec.c | 7 ++----- src/resolve/resolved-manager.c | 5 +---- src/shared/calendarspec.c | 5 +---- src/shared/format-table.c | 5 +---- src/shared/json.c | 5 +---- 21 files changed, 49 insertions(+), 94 deletions(-) diff --git a/coccinelle/fopen-unlocked.cocci b/coccinelle/fopen-unlocked.cocci index bbd70a6338d..7870f8ccea0 100644 --- a/coccinelle/fopen-unlocked.cocci +++ b/coccinelle/fopen-unlocked.cocci @@ -61,3 +61,11 @@ expression f, fd, options; + 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); diff --git a/src/basic/fileio.c b/src/basic/fileio.c index af22ec91106..99efc2410c9 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -55,6 +55,16 @@ int fdopen_unlocked(int fd, const char *options, FILE **ret) { 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; +} + int write_string_stream_ts( FILE *f, const char *line, diff --git a/src/basic/fileio.h b/src/basic/fileio.h index 7903b57c80e..fe5c8277de6 100644 --- a/src/basic/fileio.h +++ b/src/basic/fileio.h @@ -35,6 +35,7 @@ typedef enum { 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); 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) { diff --git a/src/basic/string-util.c b/src/basic/string-util.c index 5001a2be3a1..779048904a0 100644 --- a/src/basic/string-util.c +++ b/src/basic/string-util.c @@ -4,7 +4,6 @@ #include #include #include -#include #include #include @@ -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) { diff --git a/src/basic/tmpfile-util.c b/src/basic/tmpfile-util.c index 3ed91520b98..e77af7659f0 100644 --- a/src/basic/tmpfile-util.c +++ b/src/basic/tmpfile-util.c @@ -1,7 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include -#include #include #include "alloc-util.h" diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c index c8125c66067..02f12dc7013 100644 --- a/src/busctl/busctl.c +++ b/src/busctl/busctl.c @@ -1,7 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include -#include #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); diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c index 4615aeaf666..7a5e440e9cc 100644 --- a/src/core/dbus-cgroup.c +++ b/src/core/dbus-cgroup.c @@ -1,7 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include -#include #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); diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c index 5532d1ada93..48a2ebc5efb 100644 --- a/src/core/dbus-execute.c +++ b/src/core/dbus-execute.c @@ -2,7 +2,6 @@ #include #include -#include #if HAVE_SECCOMP #include @@ -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) { diff --git a/src/core/manager.c b/src/core/manager.c index 4154aec12f5..15df7e5f35c 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -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); diff --git a/src/core/smack-setup.c b/src/core/smack-setup.c index 931e8a52270..b95e6239d4b 100644 --- a/src/core/smack-setup.c +++ b/src/core/smack-setup.c @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index 023701646b6..ac7b972026f 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -2,7 +2,6 @@ #include #include -#include #include #include #include @@ -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; diff --git a/src/coredump/stacktrace.c b/src/coredump/stacktrace.c index ab1ac12a23a..66b2ec8cca3 100644 --- a/src/coredump/stacktrace.c +++ b/src/coredump/stacktrace.c @@ -2,11 +2,11 @@ #include #include -#include #include #include #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); diff --git a/src/journal/journal-qrcode.c b/src/journal/journal-qrcode.c index 35d6ec45931..678654f773e 100644 --- a/src/journal/journal-qrcode.c +++ b/src/journal/journal-qrcode.c @@ -4,9 +4,9 @@ #include #include #include -#include #include +#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++) { diff --git a/src/libsystemd/sd-bus/bus-introspect.c b/src/libsystemd/sd-bus/bus-introspect.c index 29a5ef715e4..022eddb10f6 100644 --- a/src/libsystemd/sd-bus/bus-introspect.c +++ b/src/libsystemd/sd-bus/bus-introspect.c @@ -1,7 +1,5 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -#include - #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 "\n", i->f); diff --git a/src/libsystemd/sd-bus/bus-match.c b/src/libsystemd/sd-bus/bus-match.c index 266dd7f1df9..14204eeb6b2 100644 --- a/src/libsystemd/sd-bus/bus-match.c +++ b/src/libsystemd/sd-bus/bus-match.c @@ -1,7 +1,5 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -#include - #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]; diff --git a/src/portable/portable.c b/src/portable/portable.c index 4aa879801f8..1017864b375 100644 --- a/src/portable/portable.c +++ b/src/portable/portable.c @@ -1,7 +1,5 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -#include - #include "bus-common-errors.h" #include "bus-error.h" #include "conf-files.h" diff --git a/src/resolve/resolved-dns-dnssec.c b/src/resolve/resolved-dns-dnssec.c index a5ded5ada20..18e253bea3c 100644 --- a/src/resolve/resolved-dns-dnssec.c +++ b/src/resolve/resolved-dns-dnssec.c @@ -1,9 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -#include - #if HAVE_GCRYPT -#include +# include #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); diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c index 9b8239bd094..24a752e3bb1 100644 --- a/src/resolve/resolved-manager.c +++ b/src/resolve/resolved-manager.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -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); diff --git a/src/shared/calendarspec.c b/src/shared/calendarspec.c index d83e7962a65..6e318fa265f 100644 --- a/src/shared/calendarspec.c +++ b/src/shared/calendarspec.c @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include @@ -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); diff --git a/src/shared/format-table.c b/src/shared/format-table.c index a5c0a99b08a..74379e8c642 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -1,7 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include -#include #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; diff --git a/src/shared/json.c b/src/shared/json.c index db003a41a45..a1559bda4e8 100644 --- a/src/shared/json.c +++ b/src/shared/json.c @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -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); -- 2.39.2