1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "build.h"
4 #include "fd-util.h"
5 #include "fileio.h"
6 #include "hashmap.h"
7 #include "manager-dump.h"
8 #include "unit-serialize.h"
9 
manager_dump_jobs(Manager * s,FILE * f,const char * prefix)10 void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
11         Job *j;
12 
13         assert(s);
14         assert(f);
15 
16         HASHMAP_FOREACH(j, s->jobs)
17                 job_dump(j, f, prefix);
18 }
19 
manager_dump_units(Manager * s,FILE * f,const char * prefix)20 void manager_dump_units(Manager *s, FILE *f, const char *prefix) {
21         Unit *u;
22         const char *t;
23 
24         assert(s);
25         assert(f);
26 
27         HASHMAP_FOREACH_KEY(u, t, s->units)
28                 if (u->id == t)
29                         unit_dump(u, f, prefix);
30 }
31 
manager_dump(Manager * m,FILE * f,const char * prefix)32 void manager_dump(Manager *m, FILE *f, const char *prefix) {
33         assert(m);
34         assert(f);
35 
36         fprintf(f, "%sManager: systemd " STRINGIFY(PROJECT_VERSION) " (" GIT_VERSION ")\n", strempty(prefix));
37         fprintf(f, "%sFeatures: %s\n", strempty(prefix), systemd_features);
38 
39         for (ManagerTimestamp q = 0; q < _MANAGER_TIMESTAMP_MAX; q++) {
40                 const dual_timestamp *t = m->timestamps + q;
41 
42                 if (dual_timestamp_is_set(t))
43                         fprintf(f, "%sTimestamp %s: %s\n",
44                                 strempty(prefix),
45                                 manager_timestamp_to_string(q),
46                                 timestamp_is_set(t->realtime) ? FORMAT_TIMESTAMP(t->realtime) :
47                                                                 FORMAT_TIMESPAN(t->monotonic, 1));
48         }
49 
50         manager_dump_units(m, f, prefix);
51         manager_dump_jobs(m, f, prefix);
52 }
53 
manager_get_dump_string(Manager * m,char ** ret)54 int manager_get_dump_string(Manager *m, char **ret) {
55         _cleanup_free_ char *dump = NULL;
56         _cleanup_fclose_ FILE *f = NULL;
57         size_t size;
58         int r;
59 
60         assert(m);
61         assert(ret);
62 
63         f = open_memstream_unlocked(&dump, &size);
64         if (!f)
65                 return -errno;
66 
67         manager_dump(m, f, NULL);
68 
69         r = fflush_and_check(f);
70         if (r < 0)
71                 return r;
72 
73         f = safe_fclose(f);
74 
75         *ret = TAKE_PTR(dump);
76 
77         return 0;
78 }
79 
manager_test_summary(Manager * m)80 void manager_test_summary(Manager *m) {
81         assert(m);
82 
83         printf("-> By units:\n");
84         manager_dump_units(m, stdout, "\t");
85 
86         printf("-> By jobs:\n");
87         manager_dump_jobs(m, stdout, "\t");
88 }
89