| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "analyze.h" |
| 4 |
#include "analyze-capability.h" |
| 5 |
#include "capability-list.h" |
| 6 |
#include "capability-util.h" |
| 7 |
#include "format-table.h" |
| 8 |
#include "log.h" |
| 9 |
#include "parse-util.h" |
| 10 |
|
| 11 |
static int table_add_capability(Table *table, int c) { |
| 12 |
int r; |
| 13 |
|
| 14 |
r = table_add_many(table, |
| 15 |
TABLE_STRING, capability_to_name(c) ?: "cap_???", |
| 16 |
TABLE_UINT, c); |
| 17 |
if (r < 0) |
| 18 |
return table_log_add_error(r); |
| 19 |
return 0; |
| 20 |
} |
| 21 |
|
| 22 |
int verb_capabilities(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 23 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 24 |
unsigned last_cap; |
| 25 |
int r; |
| 26 |
|
| 27 |
table = table_new("name", "number"); |
| 28 |
if (!table) |
| 29 |
return log_oom(); |
| 30 |
|
| 31 |
(void) table_set_align_percent(table, table_get_cell(table, 0, 1), 100); |
| 32 |
|
| 33 |
/* Determine the maximum of the last cap known by the kernel and by us */ |
| 34 |
last_cap = MAX((unsigned) CAP_LAST_CAP, cap_last_cap()); |
| 35 |
|
| 36 |
if (arg_capability == CAPABILITY_MASK) { |
| 37 |
uint64_t cap_mask; |
| 38 |
|
| 39 |
if (argc != 2) |
| 40 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Exactly 1 positional argument expected."); |
| 41 |
|
| 42 |
r = safe_atoux64(argv[1], &cap_mask); |
| 43 |
if (r < 0) |
| 44 |
return log_error_errno(r, "Capability mask \"%s\" is not valid.", argv[1]); |
| 45 |
|
| 46 |
for (unsigned c = 0; cap_mask != 0; ) { |
| 47 |
if (cap_mask & 0b1) { |
| 48 |
if (c > last_cap) |
| 49 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Capability %u is not known.", c); |
| 50 |
|
| 51 |
r = table_add_capability(table, c); |
| 52 |
if (r < 0) |
| 53 |
return r; |
| 54 |
} |
| 55 |
++c; |
| 56 |
cap_mask >>= 1; |
| 57 |
} |
| 58 |
|
| 59 |
(void) table_set_sort(table, (size_t) 1); |
| 60 |
|
| 61 |
} else if (argc == 1) { |
| 62 |
for (unsigned c = 0; c <= last_cap; c++) { |
| 63 |
r = table_add_capability(table, c); |
| 64 |
if (r < 0) |
| 65 |
return r; |
| 66 |
} |
| 67 |
} else { |
| 68 |
for (int i = 1; i < argc; i++) { |
| 69 |
int c; |
| 70 |
|
| 71 |
c = capability_from_name(argv[i]); |
| 72 |
if (c < 0 || (unsigned) c > last_cap) |
| 73 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Capability \"%s\" is not known.", argv[i]); |
| 74 |
|
| 75 |
r = table_add_capability(table, c); |
| 76 |
if (r < 0) |
| 77 |
return r; |
| 78 |
} |
| 79 |
|
| 80 |
(void) table_set_sort(table, (size_t) 1); |
| 81 |
} |
| 82 |
|
| 83 |
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend); |
| 84 |
if (r < 0) |
| 85 |
return r; |
| 86 |
|
| 87 |
return 0; |
| 88 |
} |