| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-netlink.h" |
| 4 |
#include "sd-network.h" |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "format-table.h" |
| 8 |
#include "netif-util.h" |
| 9 |
#include "networkctl.h" |
| 10 |
#include "networkctl-description.h" |
| 11 |
#include "networkctl-link-info.h" |
| 12 |
#include "networkctl-list.h" |
| 13 |
#include "networkctl-util.h" |
| 14 |
|
| 15 |
int verb_list_links(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 16 |
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| 17 |
_cleanup_(link_info_array_freep) LinkInfo *links = NULL; |
| 18 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 19 |
TableCell *cell; |
| 20 |
int c, r; |
| 21 |
|
| 22 |
r = dump_description(argc, argv); |
| 23 |
if (r != 0) |
| 24 |
return r; |
| 25 |
|
| 26 |
r = sd_netlink_open(&rtnl); |
| 27 |
if (r < 0) |
| 28 |
return log_error_errno(r, "Failed to connect to netlink: %m"); |
| 29 |
|
| 30 |
c = acquire_link_info(NULL, rtnl, argc > 1 ? argv + 1 : NULL, &links); |
| 31 |
if (c < 0) |
| 32 |
return c; |
| 33 |
|
| 34 |
pager_open(arg_pager_flags); |
| 35 |
|
| 36 |
table = table_new("idx", "link", "type", "operational", "setup"); |
| 37 |
if (!table) |
| 38 |
return log_oom(); |
| 39 |
|
| 40 |
if (arg_full) |
| 41 |
table_set_width(table, 0); |
| 42 |
|
| 43 |
table_set_header(table, arg_legend); |
| 44 |
table_set_ersatz_string(table, TABLE_ERSATZ_DASH); |
| 45 |
|
| 46 |
assert_se(cell = table_get_cell(table, 0, 0)); |
| 47 |
(void) table_set_minimum_width(table, cell, 3); |
| 48 |
(void) table_set_weight(table, cell, 0); |
| 49 |
(void) table_set_ellipsize_percent(table, cell, 100); |
| 50 |
(void) table_set_align_percent(table, cell, 100); |
| 51 |
|
| 52 |
assert_se(cell = table_get_cell(table, 0, 1)); |
| 53 |
(void) table_set_ellipsize_percent(table, cell, 100); |
| 54 |
|
| 55 |
FOREACH_ARRAY(link, links, c) { |
| 56 |
_cleanup_free_ char *setup_state = NULL, *operational_state = NULL; |
| 57 |
_cleanup_free_ char *t = NULL; |
| 58 |
const char *on_color_operational, *on_color_setup; |
| 59 |
|
| 60 |
(void) sd_network_link_get_operational_state(link->ifindex, &operational_state); |
| 61 |
operational_state_to_color(link->name, operational_state, &on_color_operational, NULL); |
| 62 |
|
| 63 |
(void) sd_network_link_get_setup_state(link->ifindex, &setup_state); |
| 64 |
setup_state_to_color(setup_state, &on_color_setup, NULL); |
| 65 |
|
| 66 |
r = net_get_type_string(link->sd_device, link->iftype, &t); |
| 67 |
if (r == -ENOMEM) |
| 68 |
return log_oom(); |
| 69 |
|
| 70 |
r = table_add_many(table, |
| 71 |
TABLE_INT, link->ifindex, |
| 72 |
TABLE_STRING, link->name, |
| 73 |
TABLE_STRING, t, |
| 74 |
TABLE_STRING, operational_state, |
| 75 |
TABLE_SET_COLOR, on_color_operational, |
| 76 |
TABLE_STRING, setup_state ?: "unmanaged", |
| 77 |
TABLE_SET_COLOR, on_color_setup); |
| 78 |
if (r < 0) |
| 79 |
return table_log_add_error(r); |
| 80 |
} |
| 81 |
|
| 82 |
r = table_print_or_warn(table); |
| 83 |
if (r < 0) |
| 84 |
return r; |
| 85 |
|
| 86 |
if (arg_legend) |
| 87 |
printf("\n%i links listed.\n", c); |
| 88 |
|
| 89 |
return 0; |
| 90 |
} |