]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/manager-dump.c
core: move pid watch/unwatch logic of the service manager to pidfd
[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 int manager_get_dump_jobs_string(Manager *m, char **patterns, const char *prefix, char **ret) {
28 _cleanup_(memstream_done) MemStream ms = {};
29 FILE *f;
30
31 assert(m);
32 assert(ret);
33
34 f = memstream_init(&ms);
35 if (!f)
36 return -errno;
37
38 manager_dump_jobs(m, f, patterns, prefix);
39
40 return memstream_finalize(&ms, ret, NULL);
41 }
42
43 void manager_dump_units(Manager *s, FILE *f, char **patterns, const char *prefix) {
44 Unit *u;
45 const char *t;
46
47 assert(s);
48 assert(f);
49
50 HASHMAP_FOREACH_KEY(u, t, s->units) {
51 if (u->id != t)
52 continue;
53
54 if (!strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE))
55 continue;
56
57 unit_dump(u, f, prefix);
58 }
59 }
60
61 static void manager_dump_header(Manager *m, FILE *f, const char *prefix) {
62
63 /* NB: this is a debug interface for developers. It's not supposed to be machine readable or be
64 * stable between versions. We take the liberty to restructure it entirely between versions and
65 * add/remove fields at will. */
66
67 fprintf(f, "%sManager: systemd " STRINGIFY(PROJECT_VERSION) " (" GIT_VERSION ")\n", strempty(prefix));
68 fprintf(f, "%sFeatures: %s\n", strempty(prefix), systemd_features);
69
70 for (ManagerTimestamp q = 0; q < _MANAGER_TIMESTAMP_MAX; q++) {
71 const dual_timestamp *t = m->timestamps + q;
72
73 if (dual_timestamp_is_set(t))
74 fprintf(f, "%sTimestamp %s: %s\n",
75 strempty(prefix),
76 manager_timestamp_to_string(q),
77 timestamp_is_set(t->realtime) ? FORMAT_TIMESTAMP(t->realtime) :
78 FORMAT_TIMESPAN(t->monotonic, 1));
79 }
80 }
81
82 void manager_dump(Manager *m, FILE *f, char **patterns, const char *prefix) {
83 assert(m);
84 assert(f);
85
86 /* If no pattern is provided, dump the full manager state including the manager version, features and
87 * so on. Otherwise limit the dump to the units/jobs matching the specified patterns. */
88 if (!patterns)
89 manager_dump_header(m, f, prefix);
90
91 manager_dump_units(m, f, patterns, prefix);
92 manager_dump_jobs(m, f, patterns, prefix);
93 }
94
95 int manager_get_dump_string(Manager *m, char **patterns, char **ret) {
96 _cleanup_(memstream_done) MemStream ms = {};
97 FILE *f;
98
99 assert(m);
100 assert(ret);
101
102 f = memstream_init(&ms);
103 if (!f)
104 return -errno;
105
106 manager_dump(m, f, patterns, NULL);
107
108 return memstream_finalize(&ms, ret, NULL);
109 }
110
111 void manager_test_summary(Manager *m) {
112 assert(m);
113
114 printf("-> By units:\n");
115 manager_dump_units(m, stdout, /* patterns= */ NULL, "\t");
116
117 printf("-> By jobs:\n");
118 manager_dump_jobs(m, stdout, /* patterns= */ NULL, "\t");
119 }