summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-04-04 11:46:44 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-04-12 11:44:57 +0200
commit2fe21124a6560fcf1ce3b0a3004baa9bb45d1e0b (patch)
tree35448e5d3a8042bb36bada1fedee63ae24ea7b97
parentb636d78aeeea584b7828df18cb636693af365e2f (diff)
downloadsystemd-2fe21124a6560fcf1ce3b0a3004baa9bb45d1e0b.tar.gz
Add open_memstream_unlocked() wrapper
-rw-r--r--coccinelle/fopen-unlocked.cocci8
-rw-r--r--src/basic/fileio.c10
-rw-r--r--src/basic/fileio.h1
-rw-r--r--src/basic/string-util.c16
-rw-r--r--src/basic/tmpfile-util.c1
-rw-r--r--src/busctl/busctl.c5
-rw-r--r--src/core/dbus-cgroup.c29
-rw-r--r--src/core/dbus-execute.c9
-rw-r--r--src/core/manager.c5
-rw-r--r--src/core/smack-setup.c1
-rw-r--r--src/coredump/coredump.c5
-rw-r--r--src/coredump/stacktrace.c6
-rw-r--r--src/journal/journal-qrcode.c6
-rw-r--r--src/libsystemd/sd-bus/bus-introspect.c6
-rw-r--r--src/libsystemd/sd-bus/bus-match.c6
-rw-r--r--src/portable/portable.c2
-rw-r--r--src/resolve/resolved-dns-dnssec.c7
-rw-r--r--src/resolve/resolved-manager.c5
-rw-r--r--src/shared/calendarspec.c5
-rw-r--r--src/shared/format-table.c5
-rw-r--r--src/shared/json.c5
21 files changed, 49 insertions, 94 deletions
diff --git a/coccinelle/fopen-unlocked.cocci b/coccinelle/fopen-unlocked.cocci
index bbd70a6338..7870f8ccea 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 af22ec9110..99efc2410c 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 7903b57c80..fe5c8277de 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 5001a2be3a..779048904a 100644
--- a/src/basic/string-util.c
+++ b/src/basic/string-util.c
@@ -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) {
diff --git a/src/basic/tmpfile-util.c b/src/basic/tmpfile-util.c
index 3ed91520b9..e77af7659f 100644
--- a/src/basic/tmpfile-util.c
+++ b/src/basic/tmpfile-util.c
@@ -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"
diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c
index c8125c6606..02f12dc701 100644
--- a/src/busctl/busctl.c
+++ b/src/busctl/busctl.c
@@ -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);
diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c
index 4615aeaf66..7a5e440e9c 100644
--- a/src/core/dbus-cgroup.c
+++ b/src/core/dbus-cgroup.c
@@ -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);
diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c
index 5532d1ada9..48a2ebc5ef 100644
--- a/src/core/dbus-execute.c
+++ b/src/core/dbus-execute.c
@@ -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) {
diff --git a/src/core/manager.c b/src/core/manager.c
index 4154aec12f..15df7e5f35 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -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);
diff --git a/src/core/smack-setup.c b/src/core/smack-setup.c
index 931e8a5227..b95e6239d4 100644
--- a/src/core/smack-setup.c
+++ b/src/core/smack-setup.c
@@ -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>
diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c
index 023701646b..ac7b972026 100644
--- a/src/coredump/coredump.c
+++ b/src/coredump/coredump.c
@@ -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;
diff --git a/src/coredump/stacktrace.c b/src/coredump/stacktrace.c
index ab1ac12a23..66b2ec8cca 100644
--- a/src/coredump/stacktrace.c
+++ b/src/coredump/stacktrace.c
@@ -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);
diff --git a/src/journal/journal-qrcode.c b/src/journal/journal-qrcode.c
index 35d6ec4593..678654f773 100644
--- a/src/journal/journal-qrcode.c
+++ b/src/journal/journal-qrcode.c
@@ -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++) {
diff --git a/src/libsystemd/sd-bus/bus-introspect.c b/src/libsystemd/sd-bus/bus-introspect.c
index 29a5ef715e..022eddb10f 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 <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);
diff --git a/src/libsystemd/sd-bus/bus-match.c b/src/libsystemd/sd-bus/bus-match.c
index 266dd7f1df..14204eeb6b 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 <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];
diff --git a/src/portable/portable.c b/src/portable/portable.c
index 4aa879801f..1017864b37 100644
--- a/src/portable/portable.c
+++ b/src/portable/portable.c
@@ -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"
diff --git a/src/resolve/resolved-dns-dnssec.c b/src/resolve/resolved-dns-dnssec.c
index a5ded5ada2..18e253bea3 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 <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);
diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c
index 9b8239bd09..24a752e3bb 100644
--- a/src/resolve/resolved-manager.c
+++ b/src/resolve/resolved-manager.c
@@ -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);
diff --git a/src/shared/calendarspec.c b/src/shared/calendarspec.c
index d83e7962a6..6e318fa265 100644
--- a/src/shared/calendarspec.c
+++ b/src/shared/calendarspec.c
@@ -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);
diff --git a/src/shared/format-table.c b/src/shared/format-table.c
index a5c0a99b08..74379e8c64 100644
--- a/src/shared/format-table.c
+++ b/src/shared/format-table.c
@@ -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;
diff --git a/src/shared/json.c b/src/shared/json.c
index db003a41a4..a1559bda4e 100644
--- a/src/shared/json.c
+++ b/src/shared/json.c
@@ -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);