From: Lennart Poettering Date: Mon, 20 Nov 2017 20:20:44 +0000 (+0100) Subject: manager: add manager_get_dump_string() X-Git-Tag: v236~148^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=713f6f901db285602ff88ebf1dcd4631293e85c1;p=thirdparty%2Fsystemd.git manager: add manager_get_dump_string() It's like manager_dump(), but returns a string. This allows us to reduce some duplicate code. Also, while we are at it, turn off stdio locking while we write to the memory FILE *f. --- diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c index 3261e1e86eb..5029f49a012 100644 --- a/src/core/dbus-manager.c +++ b/src/core/dbus-manager.c @@ -1289,9 +1289,7 @@ static int method_unsubscribe(sd_bus_message *message, void *userdata, sd_bus_er static int method_dump(sd_bus_message *message, void *userdata, sd_bus_error *error) { _cleanup_free_ char *dump = NULL; - _cleanup_fclose_ FILE *f = NULL; Manager *m = userdata; - size_t size; int r; assert(message); @@ -1303,13 +1301,7 @@ static int method_dump(sd_bus_message *message, void *userdata, sd_bus_error *er if (r < 0) return r; - f = open_memstream(&dump, &size); - if (!f) - return -ENOMEM; - - manager_dump(m, f, NULL); - - r = fflush_and_check(f); + r = manager_get_dump_string(m, &dump); if (r < 0) return r; diff --git a/src/core/manager.c b/src/core/manager.c index b99bf22e238..97ca77c9fa4 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -1731,6 +1732,35 @@ void manager_dump(Manager *m, FILE *f, const char *prefix) { manager_dump_jobs(m, f, prefix); } +int manager_get_dump_string(Manager *m, char **ret) { + _cleanup_free_ char *dump = NULL; + _cleanup_fclose_ FILE *f = NULL; + size_t size; + int r; + + assert(m); + assert(ret); + + f = open_memstream(&dump, &size); + if (!f) + return -errno; + + __fsetlocking(f, FSETLOCKING_BYCALLER); + + manager_dump(m, f, NULL); + + r = fflush_and_check(f); + if (r < 0) + return r; + + f = safe_fclose(f); + + *ret = dump; + dump = NULL; + + return 0; +} + void manager_clear_jobs(Manager *m) { Job *j; @@ -2179,20 +2209,10 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t case SIGUSR2: { _cleanup_free_ char *dump = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t size; - - f = open_memstream(&dump, &size); - if (!f) { - log_warning_errno(errno, "Failed to allocate memory stream: %m"); - break; - } - - manager_dump(m, f, NULL); - r = fflush_and_check(f); + r = manager_get_dump_string(m, &dump); if (r < 0) { - log_warning_errno(r, "Failed to write status stream: %m"); + log_warning_errno(errno, "Failed to acquire manager dump: %m"); break; } diff --git a/src/core/manager.h b/src/core/manager.h index eec063e4656..4271e0c6c0d 100644 --- a/src/core/manager.h +++ b/src/core/manager.h @@ -375,6 +375,7 @@ int manager_propagate_reload(Manager *m, Unit *unit, JobMode mode, sd_bus_error void manager_dump_units(Manager *s, FILE *f, const char *prefix); void manager_dump_jobs(Manager *s, FILE *f, const char *prefix); void manager_dump(Manager *s, FILE *f, const char *prefix); +int manager_get_dump_string(Manager *m, char **ret); void manager_clear_jobs(Manager *m);