]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Add open_memstream_unlocked() wrapper
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 4 Apr 2019 09:46:44 +0000 (11:46 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 12 Apr 2019 09:44:57 +0000 (11:44 +0200)
21 files changed:
coccinelle/fopen-unlocked.cocci
src/basic/fileio.c
src/basic/fileio.h
src/basic/string-util.c
src/basic/tmpfile-util.c
src/busctl/busctl.c
src/core/dbus-cgroup.c
src/core/dbus-execute.c
src/core/manager.c
src/core/smack-setup.c
src/coredump/coredump.c
src/coredump/stacktrace.c
src/journal/journal-qrcode.c
src/libsystemd/sd-bus/bus-introspect.c
src/libsystemd/sd-bus/bus-match.c
src/portable/portable.c
src/resolve/resolved-dns-dnssec.c
src/resolve/resolved-manager.c
src/shared/calendarspec.c
src/shared/format-table.c
src/shared/json.c

index bbd70a6338dabb5fd41a467c7ebc1777c2059c03..7870f8ccea09bfdfc8f196aec6e00990f6a5c81d 100644 (file)
@@ -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);
index af22ec9110624ed7d157ede1f16e1f60d800142a..99efc2410c9ddcb7ac7050faed2b0964830c47d2 100644 (file)
@@ -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,
index 7903b57c80eb7a4c022be77a333c78ceb1ee4d39..fe5c8277de62bdadce9c5cf5af2132562df84df7 100644 (file)
@@ -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) {
index 5001a2be3a1f1e794e162956f6ccc2627151f306..779048904a08f0ed67b3099a59f7497d1eb8f772 100644 (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) {
index 3ed91520b98ca4910724ff14732cf0d1d206bc89..e77af7659f00a01e0f9496210fddfbd16e54af75 100644 (file)
@@ -1,7 +1,6 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include <stdio.h>
-#include <stdio_ext.h>
 #include <sys/mman.h>
 
 #include "alloc-util.h"
index c8125c660677ed48b74a2f47879e9f68931865d1..02f12dc70138bd466f6caeb6603bf9eb6f98ea37 100644 (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);
index 4615aeaf6667d65ee86973d4ada9a3a668e855af..7a5e440e9cc0aebf2d35f6c5c661f328d79bdba1 100644 (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);
 
index 5532d1ada93f5167502939f0b50545edb824ee60..48a2ebc5efb311995a80b0ec43efda9b155af6ac 100644 (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) {
index 4154aec12f5a4e6b194ef02a97c5fe5891c3ee16..15df7e5f35cc1405818bbcbd0ec847b8abf95048 100644 (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);
index 931e8a52270b8391058c06d354af99b4a1b77b40..b95e6239d4b217c1302f02c611505fc419a88ab7 100644 (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>
index 023701646b65e7bf216b4c661d8dedd5ddb29e2e..ac7b972026f23ed0c2adf0027917b5447c6ac1f9 100644 (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;
index ab1ac12a23a57c30245b25256193df552f0351d5..66b2ec8cca32bac084578b4e90c708bceb407a9f 100644 (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);
index 35d6ec45931142ab15e8935bcdb24d101e6b0651..678654f773e36e7a8d242a5ed48e09865f4fa24f 100644 (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++) {
index 29a5ef715e41545160fb4ba61845dfaf3ad00f16..022eddb10f66a1e90675b6c5b84fafb78dd34e79 100644 (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);
 
index 266dd7f1df928452edf5125190881f2d88e5048a..14204eeb6b2d35ea614515d6467d69df9c393c9f 100644 (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];
 
index 4aa879801f830e58e6e1d5656f1663cbb635d2ca..1017864b375c4a1caa4b17e73837351d01e5386d 100644 (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"
index a5ded5ada2020991bb6bfd6b6e076abc0c09e590..18e253bea3c8184dfe9ce1124684bd21d540acd2 100644 (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);
index 9b8239bd094236ab74764006bf969427fce91395..24a752e3bb1983efd61b95596ae8e997302f06b5 100644 (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);
 
index d83e7962a650503a7d03e08973eda8cdb44d1b17..6e318fa265fa24c5d67bfc73eb0726ba0d45128f 100644 (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);
index a5c0a99b08a50286f997480d92d0281271f29cb5..74379e8c642cf6a569c8c452f89ae25948bd9eef 100644 (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;
index db003a41a45360ae94ebd8bb71978b3ff1b1a4f0..a1559bda4e80531c1c33dc24e47797ebd2d1828c 100644 (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);