udev: String substitutions can be done in ENV, too
[systemd_ALT.git] / src / core / manager-dump.c
blobe39aff5d899de0aeba38c2c40c157fdd5b6e6475
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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"
11 void manager_dump_jobs(Manager *s, FILE *f, char **patterns, const char *prefix) {
12 Job *j;
14 assert(s);
15 assert(f);
17 HASHMAP_FOREACH(j, s->jobs) {
19 if (!strv_fnmatch_or_empty(patterns, j->unit->id, FNM_NOESCAPE))
20 continue;
22 job_dump(j, f, prefix);
26 void manager_dump_units(Manager *s, FILE *f, char **patterns, const char *prefix) {
27 Unit *u;
28 const char *t;
30 assert(s);
31 assert(f);
33 HASHMAP_FOREACH_KEY(u, t, s->units) {
34 if (u->id != t)
35 continue;
37 if (!strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE))
38 continue;
40 unit_dump(u, f, prefix);
44 static void manager_dump_header(Manager *m, FILE *f, const char *prefix) {
46 /* NB: this is a debug interface for developers. It's not supposed to be machine readable or be
47 * stable between versions. We take the liberty to restructure it entirely between versions and
48 * add/remove fields at will. */
50 fprintf(f, "%sManager: systemd " STRINGIFY(PROJECT_VERSION) " (" GIT_VERSION ")\n", strempty(prefix));
51 fprintf(f, "%sFeatures: %s\n", strempty(prefix), systemd_features);
53 for (ManagerTimestamp q = 0; q < _MANAGER_TIMESTAMP_MAX; q++) {
54 const dual_timestamp *t = m->timestamps + q;
56 if (dual_timestamp_is_set(t))
57 fprintf(f, "%sTimestamp %s: %s\n",
58 strempty(prefix),
59 manager_timestamp_to_string(q),
60 timestamp_is_set(t->realtime) ? FORMAT_TIMESTAMP(t->realtime) :
61 FORMAT_TIMESPAN(t->monotonic, 1));
65 void manager_dump(Manager *m, FILE *f, char **patterns, const char *prefix) {
66 assert(m);
67 assert(f);
69 /* If no pattern is provided, dump the full manager state including the manager version, features and
70 * so on. Otherwise limit the dump to the units/jobs matching the specified patterns. */
71 if (!patterns)
72 manager_dump_header(m, f, prefix);
74 manager_dump_units(m, f, patterns, prefix);
75 manager_dump_jobs(m, f, patterns, prefix);
78 int manager_get_dump_string(Manager *m, char **patterns, char **ret) {
79 _cleanup_(memstream_done) MemStream ms = {};
80 FILE *f;
82 assert(m);
83 assert(ret);
85 f = memstream_init(&ms);
86 if (!f)
87 return -errno;
89 manager_dump(m, f, patterns, NULL);
91 return memstream_finalize(&ms, ret, NULL);
94 void manager_test_summary(Manager *m) {
95 assert(m);
97 printf("-> By units:\n");
98 manager_dump_units(m, stdout, /* patterns= */ NULL, "\t");
100 printf("-> By jobs:\n");
101 manager_dump_jobs(m, stdout, /* patterns= */ NULL, "\t");