Debian Codegraph @

src/systemctl/systemctl-list-jobs.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fnmatch.h>
4
5 #include "sd-bus.h"
6
7 #include "alloc-util.h"
8 #include "ansi-color.h"
9 #include "bus-error.h"
10 #include "bus-locator.h"
11 #include "bus-util.h"
12 #include "format-table.h"
13 #include "glyph-util.h"
14 #include "string-util.h"
15 #include "strv.h"
16 #include "systemctl.h"
17 #include "systemctl-list-jobs.h"
18 #include "systemctl-util.h"
19
20 static int output_waiting_jobs(sd_bus *bus, Table *table, uint32_t id, const char *method, const char *prefix) {
21 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
22 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
23 const char *name, *type;
24 uint32_t other_id;
25 int r;
26
27 assert(bus);
28
29 r = bus_call_method(bus, bus_systemd_mgr, method, &error, &reply, "u", id);
30 if (r < 0)
31 return log_debug_errno(r, "Failed to get waiting jobs for job %" PRIu32, id);
32
33 r = sd_bus_message_enter_container(reply, 'a', "(usssoo)");
34 if (r < 0)
35 return bus_log_parse_error(r);
36
37 while ((r = sd_bus_message_read(reply, "(usssoo)", &other_id, &name, &type, NULL, NULL, NULL)) > 0) {
38 _cleanup_free_ char *row = NULL;
39
40 if (asprintf(&row, "%s %u (%s/%s)", prefix, other_id, name, type) < 0)
41 return log_oom();
42
43 r = table_add_many(table,
44 TABLE_STRING, glyph(GLYPH_TREE_RIGHT),
45 TABLE_STRING, row,
46 TABLE_EMPTY,
47 TABLE_EMPTY);
48 if (r < 0)
49 return table_log_add_error(r);
50 }
51
52 if (r < 0)
53 return bus_log_parse_error(r);
54
55 r = sd_bus_message_exit_container(reply);
56 if (r < 0)
57 return bus_log_parse_error(r);
58
59 return 0;
60 }
61
62 struct job_info {
63 uint32_t id;
64 const char *name, *type, *state;
65 };
66
67 static int output_jobs_list(sd_bus *bus, const struct job_info* jobs, unsigned n, bool skipped) {
68 _cleanup_(table_unrefp) Table *table = NULL;
69 const char *on, *off;
70 int r;
71
72 assert(n == 0 || jobs);
73
74 if (n == 0) {
75 if (arg_legend != 0) {
76 on = ansi_highlight_green();
77 off = ansi_normal();
78
79 printf("%sNo jobs %s.%s\n", on, skipped ? "listed" : "running", off);
80 }
81 return 0;
82 }
83
84 pager_open(arg_pager_flags);
85
86 table = table_new("job", "unit", "type", "state");
87 if (!table)
88 return log_oom();
89
90 table_set_header(table, arg_legend != 0);
91 if (arg_full)
92 table_set_width(table, 0);
93
94 table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
95
96 for (const struct job_info *j = jobs; j < jobs + n; j++) {
97 if (streq(j->state, "running"))
98 on = ansi_highlight();
99 else
100 on = "";
101
102 r = table_add_many(table,
103 TABLE_UINT, j->id,
104 TABLE_STRING, j->name,
105 TABLE_SET_COLOR, on,
106 TABLE_STRING, j->type,
107 TABLE_STRING, j->state,
108 TABLE_SET_COLOR, on);
109 if (r < 0)
110 return table_log_add_error(r);
111
112 if (arg_jobs_after)
113 output_waiting_jobs(bus, table, j->id, "GetJobAfter", "\tblocking job");
114 if (arg_jobs_before)
115 output_waiting_jobs(bus, table, j->id, "GetJobBefore", "\twaiting for job");
116 }
117
118 r = table_print_or_warn(table);
119 if (r < 0)
120 return r;
121
122 if (arg_legend != 0) {
123 on = ansi_highlight();
124 off = ansi_normal();
125
126 printf("\n%s%u jobs listed%s.\n", on, n, off);
127 }
128
129 return 0;
130 }
131
132 static bool output_show_job(struct job_info *job, char **patterns) {
133 return strv_fnmatch_or_empty(patterns, job->name, FNM_NOESCAPE);
134 }
135
136 int verb_list_jobs(int argc, char *argv[], uintptr_t _data, void *userdata) {
137 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
138 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
139 _cleanup_free_ struct job_info *jobs = NULL;
140 const char *name, *type, *state;
141 bool skipped = false;
142 unsigned c = 0;
143 sd_bus *bus;
144 uint32_t id;
145 int r;
146
147 r = acquire_bus(BUS_MANAGER, &bus);
148 if (r < 0)
149 return r;
150
151 r = bus_call_method(bus, bus_systemd_mgr, "ListJobs", &error, &reply, NULL);
152 if (r < 0)
153 return log_error_errno(r, "Failed to list jobs: %s", bus_error_message(&error, r));
154
155 r = sd_bus_message_enter_container(reply, 'a', "(usssoo)");
156 if (r < 0)
157 return bus_log_parse_error(r);
158
159 while ((r = sd_bus_message_read(reply, "(usssoo)", &id, &name, &type, &state, NULL, NULL)) > 0) {
160 struct job_info job = { id, name, type, state };
161
162 if (!output_show_job(&job, strv_skip(argv, 1))) {
163 skipped = true;
164 continue;
165 }
166
167 if (!GREEDY_REALLOC(jobs, c + 1))
168 return log_oom();
169
170 jobs[c++] = job;
171 }
172 if (r < 0)
173 return bus_log_parse_error(r);
174
175 r = sd_bus_message_exit_container(reply);
176 if (r < 0)
177 return bus_log_parse_error(r);
178
179 pager_open(arg_pager_flags);
180
181 return output_jobs_list(bus, jobs, c, skipped);
182 }