1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

Add open_memstream_unlocked() wrapper

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2019-04-04 11:46:44 +02:00
parent b636d78aee
commit 2fe21124a6
21 changed files with 49 additions and 94 deletions

View 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);

View 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,

View 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) {

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,7 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <stdio.h>
#include <stdio_ext.h>
#include <sys/mman.h>
#include "alloc-util.h"

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

@ -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>

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

@ -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

@ -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

@ -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"

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

@ -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

@ -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

@ -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);