]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/manager-dump.c
tree-wide: drop unnecessary inclusion of version.h
[thirdparty/systemd.git] / src / core / manager-dump.c
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 "memstream-util.h"
9 #include "unit-serialize.h"
10 #include "version.h"
11
12 void manager_dump_jobs(Manager *s, FILE *f, char **patterns, const char *prefix) {
13 Job *j;
14
15 assert(s);
16 assert(f);
17
18 HASHMAP_FOREACH(j, s->jobs) {
19
20 if (!strv_fnmatch_or_empty(patterns, j->unit->id, FNM_NOESCAPE))
21 continue;
22
23 job_dump(j, f, prefix);
24 }
25 }
26
27 void manager_dump_units(Manager *s, FILE *f, char **patterns, const char *prefix) {
28 Unit *u;
29 const char *t;
30
31 assert(s);
32 assert(f);
33
34 HASHMAP_FOREACH_KEY(u, t, s->units) {
35 if (u->id != t)
36 continue;
37
38 if (!strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE))
39 continue;
40
41 unit_dump(u, f, prefix);
42 }
43 }
44
45 static void manager_dump_header(Manager *m, FILE *f, const char *prefix) {
46
47 /* NB: this is a debug interface for developers. It's not supposed to be machine readable or be
48 * stable between versions. We take the liberty to restructure it entirely between versions and
49 * add/remove fields at will. */
50
51 fprintf(f, "%sManager: systemd " STRINGIFY(PROJECT_VERSION) " (" GIT_VERSION ")\n", strempty(prefix));
52 fprintf(f, "%sFeatures: %s\n", strempty(prefix), systemd_features);
53
54 for (ManagerTimestamp q = 0; q < _MANAGER_TIMESTAMP_MAX; q++) {
55 const dual_timestamp *t = m->timestamps + q;
56
57 if (dual_timestamp_is_set(t))
58 fprintf(f, "%sTimestamp %s: %s\n",
59 strempty(prefix),
60 manager_timestamp_to_string(q),
61 timestamp_is_set(t->realtime) ? FORMAT_TIMESTAMP(t->realtime) :
62 FORMAT_TIMESPAN(t->monotonic, 1));
63 }
64 }
65
66 void manager_dump(Manager *m, FILE *f, char **patterns, const char *prefix) {
67 assert(m);
68 assert(f);
69
70 /* If no pattern is provided, dump the full manager state including the manager version, features and
71 * so on. Otherwise limit the dump to the units/jobs matching the specified patterns. */
72 if (!patterns)
73 manager_dump_header(m, f, prefix);
74
75 manager_dump_units(m, f, patterns, prefix);
76 manager_dump_jobs(m, f, patterns, prefix);
77 }
78
79 int manager_get_dump_string(Manager *m, char **patterns, char **ret) {
80 _cleanup_(memstream_done) MemStream ms = {};
81 FILE *f;
82
83 assert(m);
84 assert(ret);
85
86 f = memstream_init(&ms);
87 if (!f)
88 return -errno;
89
90 manager_dump(m, f, patterns, NULL);
91
92 return memstream_finalize(&ms, ret, NULL);
93 }
94
95 void manager_test_summary(Manager *m) {
96 assert(m);
97
98 printf("-> By units:\n");
99 manager_dump_units(m, stdout, /* patterns= */ NULL, "\t");
100
101 printf("-> By jobs:\n");
102 manager_dump_jobs(m, stdout, /* patterns= */ NULL, "\t");
103 }