1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-31 16:21:26 +03:00

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.
This commit is contained in:
Lennart Poettering 2017-11-20 21:20:44 +01:00
parent ad75b9e765
commit 713f6f901d
3 changed files with 34 additions and 21 deletions

View File

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

View File

@ -22,6 +22,7 @@
#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>
@ -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;
}

View File

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