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);
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;
#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>
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;
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;
}
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);