]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/systemctl/systemctl-list-jobs.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include "bus-locator.h"
5 #include "locale-util.h"
6 #include "systemctl-list-jobs.h"
7 #include "systemctl-util.h"
9 #include "terminal-util.h"
11 static int output_waiting_jobs(sd_bus
*bus
, Table
*table
, uint32_t id
, const char *method
, const char *prefix
) {
12 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
13 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
14 const char *name
, *type
;
20 r
= bus_call_method(bus
, bus_systemd_mgr
, method
, &error
, &reply
, "u", id
);
22 return log_debug_errno(r
, "Failed to get waiting jobs for job %" PRIu32
, id
);
24 r
= sd_bus_message_enter_container(reply
, 'a', "(usssoo)");
26 return bus_log_parse_error(r
);
28 while ((r
= sd_bus_message_read(reply
, "(usssoo)", &other_id
, &name
, &type
, NULL
, NULL
, NULL
)) > 0) {
29 _cleanup_free_
char *row
= NULL
;
32 if (asprintf(&row
, "%s %u (%s/%s)", prefix
, other_id
, name
, type
) < 0)
35 rc
= table_add_many(table
,
36 TABLE_STRING
, special_glyph(SPECIAL_GLYPH_TREE_RIGHT
),
41 return table_log_add_error(r
);
45 return bus_log_parse_error(r
);
47 r
= sd_bus_message_exit_container(reply
);
49 return bus_log_parse_error(r
);
56 const char *name
, *type
, *state
;
59 static int output_jobs_list(sd_bus
*bus
, const struct job_info
* jobs
, unsigned n
, bool skipped
) {
60 _cleanup_(table_unrefp
) Table
*table
= NULL
;
64 assert(n
== 0 || jobs
);
67 if (arg_legend
!= 0) {
68 on
= ansi_highlight_green();
71 printf("%sNo jobs %s.%s\n", on
, skipped
? "listed" : "running", off
);
76 (void) pager_open(arg_pager_flags
);
78 table
= table_new("job", "unit", "type", "state");
82 table_set_header(table
, arg_legend
!= 0);
84 table_set_width(table
, 0);
86 (void) table_set_empty_string(table
, "-");
88 for (const struct job_info
*j
= jobs
; j
< jobs
+ n
; j
++) {
89 if (streq(j
->state
, "running"))
90 on
= ansi_highlight();
94 r
= table_add_many(table
,
96 TABLE_STRING
, j
->name
,
98 TABLE_STRING
, j
->type
,
99 TABLE_STRING
, j
->state
,
100 TABLE_SET_COLOR
, on
);
102 return table_log_add_error(r
);
105 output_waiting_jobs(bus
, table
, j
->id
, "GetJobAfter", "\twaiting for job");
107 output_waiting_jobs(bus
, table
, j
->id
, "GetJobBefore", "\tblocking job");
110 r
= table_print(table
, NULL
);
112 return log_error_errno(r
, "Failed to print the table: %m");
114 if (arg_legend
!= 0) {
115 on
= ansi_highlight();
118 printf("\n%s%u jobs listed%s.\n", on
, n
, off
);
124 static bool output_show_job(struct job_info
*job
, char **patterns
) {
125 return strv_fnmatch_or_empty(patterns
, job
->name
, FNM_NOESCAPE
);
128 int list_jobs(int argc
, char *argv
[], void *userdata
) {
129 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
130 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
131 _cleanup_free_
struct job_info
*jobs
= NULL
;
132 const char *name
, *type
, *state
;
133 bool skipped
= false;
140 r
= acquire_bus(BUS_MANAGER
, &bus
);
144 r
= bus_call_method(bus
, bus_systemd_mgr
, "ListJobs", &error
, &reply
, NULL
);
146 return log_error_errno(r
, "Failed to list jobs: %s", bus_error_message(&error
, r
));
148 r
= sd_bus_message_enter_container(reply
, 'a', "(usssoo)");
150 return bus_log_parse_error(r
);
152 while ((r
= sd_bus_message_read(reply
, "(usssoo)", &id
, &name
, &type
, &state
, NULL
, NULL
)) > 0) {
153 struct job_info job
= { id
, name
, type
, state
};
155 if (!output_show_job(&job
, strv_skip(argv
, 1))) {
160 if (!GREEDY_REALLOC(jobs
, size
, c
+ 1))
166 return bus_log_parse_error(r
);
168 r
= sd_bus_message_exit_container(reply
);
170 return bus_log_parse_error(r
);
172 (void) pager_open(arg_pager_flags
);
174 return output_jobs_list(bus
, jobs
, c
, skipped
);