1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "analyze.h"
4 #include "analyze-capability.h"
5 #include "cap-list.h"
6 #include "capability-util.h"
7 #include "format-table.h"
8 
verb_capabilities(int argc,char * argv[],void * userdata)9 int verb_capabilities(int argc, char *argv[], void *userdata) {
10         _cleanup_(table_unrefp) Table *table = NULL;
11         unsigned last_cap;
12         int r;
13 
14         table = table_new("name", "number");
15         if (!table)
16                 return log_oom();
17 
18         (void) table_set_align_percent(table, table_get_cell(table, 0, 1), 100);
19 
20         /* Determine the maximum of the last cap known by the kernel and by us */
21         last_cap = MAX((unsigned) CAP_LAST_CAP, cap_last_cap());
22 
23         if (strv_isempty(strv_skip(argv, 1)))
24                 for (unsigned c = 0; c <= last_cap; c++) {
25                         r = table_add_many(table,
26                                            TABLE_STRING, capability_to_name(c) ?: "cap_???",
27                                            TABLE_UINT, c);
28                         if (r < 0)
29                                 return table_log_add_error(r);
30                 }
31         else {
32                 for (int i = 1; i < argc; i++) {
33                         int c;
34 
35                         c = capability_from_name(argv[i]);
36                         if (c < 0 || (unsigned) c > last_cap)
37                                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Capability \"%s\" not known.", argv[i]);
38 
39                         r = table_add_many(table,
40                                            TABLE_STRING, capability_to_name(c) ?: "cap_???",
41                                            TABLE_UINT, (unsigned) c);
42                         if (r < 0)
43                                 return table_log_add_error(r);
44                 }
45 
46                 (void) table_set_sort(table, (size_t) 1);
47         }
48 
49         pager_open(arg_pager_flags);
50 
51         return table_print(table, NULL);
52 }
53