Merge pull request #12646 from yuwata/networkctl-use-table

networkctl: use Table
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2019-05-30 11:02:03 +02:00
committed by GitHub
11 changed files with 631 additions and 172 deletions
+10
View File
@@ -0,0 +1,10 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include "format-util.h"
#include "memory-util.h"
char *format_ifname(int ifindex, char buf[static IF_NAMESIZE + 1]) {
/* Buffer is always cleared */
memzero(buf, IF_NAMESIZE + 1);
return if_indextoname(ifindex, buf);
}
+3
View File
@@ -2,6 +2,7 @@
#pragma once
#include <inttypes.h>
#include <net/if.h>
#if SIZEOF_PID_T == 4
# define PID_PRI PRIi32
@@ -65,3 +66,5 @@
#else
# error Unknown ino_t size
#endif
char *format_ifname(int ifindex, char buf[static IF_NAMESIZE + 1]);
+1
View File
@@ -56,6 +56,7 @@ basic_sources = files('''
fd-util.h
fileio.c
fileio.h
format-util.c
format-util.h
fs-util.c
fs-util.h
+2 -2
View File
@@ -1389,7 +1389,7 @@ int socket_bind_to_ifname(int fd, const char *ifname) {
}
int socket_bind_to_ifindex(int fd, int ifindex) {
char ifname[IFNAMSIZ] = "";
char ifname[IF_NAMESIZE + 1];
assert(fd >= 0);
@@ -1407,7 +1407,7 @@ int socket_bind_to_ifindex(int fd, int ifindex) {
return -errno;
/* Fall back to SO_BINDTODEVICE on kernels < 5.0 which didn't have SO_BINDTOIFINDEX */
if (!if_indextoname(ifindex, ifname))
if (!format_ifname(ifindex, ifname))
return -errno;
return socket_bind_to_ifname(fd, ifname);
+2 -2
View File
@@ -606,9 +606,9 @@ static void print_machine_status_info(sd_bus *bus, MachineStatusInfo *i) {
fputs("\t Iface:", stdout);
for (c = 0; c < i->n_netif; c++) {
char name[IF_NAMESIZE+1] = "";
char name[IF_NAMESIZE+1];
if (if_indextoname(i->netif[c], name)) {
if (format_ifname(i->netif[c], name)) {
fputc(' ', stdout);
fputs(name, stdout);
+442 -158
View File
@@ -19,6 +19,8 @@
#include "device-util.h"
#include "ether-addr-util.h"
#include "fd-util.h"
#include "format-table.h"
#include "format-util.h"
#include "hwdb-util.h"
#include "local-addresses.h"
#include "locale-util.h"
@@ -109,8 +111,6 @@ typedef struct LinkInfo {
bool has_mac_address:1;
bool has_mtu:1;
bool has_min_mtu:1;
bool has_max_mtu:1;
bool has_tx_queues:1;
bool has_rx_queues:1;
} LinkInfo;
@@ -166,13 +166,10 @@ static int decode_link(sd_netlink_message *m, LinkInfo *info, char **patterns) {
sd_netlink_message_read_u32(m, IFLA_MTU, &info->mtu) >= 0 &&
info->mtu > 0;
info->has_min_mtu =
sd_netlink_message_read_u32(m, IFLA_MIN_MTU, &info->min_mtu) >= 0 &&
info->min_mtu > 0;
info->has_max_mtu =
sd_netlink_message_read_u32(m, IFLA_MAX_MTU, &info->max_mtu) >= 0 &&
info->min_mtu > 0;
if (info->has_mtu) {
(void) sd_netlink_message_read_u32(m, IFLA_MIN_MTU, &info->min_mtu);
(void) sd_netlink_message_read_u32(m, IFLA_MAX_MTU, &info->max_mtu);
}
info->has_rx_queues =
sd_netlink_message_read_u32(m, IFLA_NUM_RX_QUEUES, &info->rx_queues) >= 0 &&
@@ -228,6 +225,8 @@ static int acquire_link_info(sd_netlink *rtnl, char **patterns, LinkInfo **ret)
static int list_links(int argc, char *argv[], void *userdata) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_free_ LinkInfo *links = NULL;
_cleanup_(table_unrefp) Table *table = NULL;
TableCell *cell;
int c, i, r;
r = sd_netlink_open(&rtnl);
@@ -240,13 +239,29 @@ static int list_links(int argc, char *argv[], void *userdata) {
(void) pager_open(arg_pager_flags);
if (arg_legend)
printf("%3s %-16s %-18s %-16s %-10s\n",
"IDX",
"LINK",
"TYPE",
"OPERATIONAL",
"SETUP");
table = table_new("IDX", "LINK", "TYPE", "OPERATIONAL", "SETUP");
if (!table)
return log_oom();
table_set_header(table, arg_legend);
assert_se(cell = table_get_cell(table, 0, 0));
(void) table_set_minimum_width(table, cell, 3);
(void) table_set_weight(table, cell, 0);
(void) table_set_ellipsize_percent(table, cell, 0);
(void) table_set_align_percent(table, cell, 100);
assert_se(cell = table_get_cell(table, 0, 1));
(void) table_set_minimum_width(table, cell, 16);
assert_se(cell = table_get_cell(table, 0, 2));
(void) table_set_minimum_width(table, cell, 18);
assert_se(cell = table_get_cell(table, 0, 3));
(void) table_set_minimum_width(table, cell, 16);
assert_se(cell = table_get_cell(table, 0, 4));
(void) table_set_minimum_width(table, cell, 10);
for (i = 0; i < c; i++) {
_cleanup_free_ char *setup_state = NULL, *operational_state = NULL;
@@ -269,12 +284,33 @@ static int list_links(int argc, char *argv[], void *userdata) {
t = link_get_type_string(links[i].iftype, d);
printf("%3i %-16s %-18s %s%-16s%s %s%-10s%s\n",
links[i].ifindex, links[i].name, strna(t),
on_color_operational, strna(operational_state), off_color_operational,
on_color_setup, strna(setup_state), off_color_setup);
r = table_add_cell_full(table, NULL, TABLE_INT, &links[i].ifindex, SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
r = table_add_many(table,
TABLE_STRING, links[i].name,
TABLE_STRING, strna(t));
if (r < 0)
return r;
r = table_add_cell(table, &cell, TABLE_STRING, strna(operational_state));
if (r < 0)
return r;
(void) table_set_color(table, cell, on_color_operational);
r = table_add_cell(table, &cell, TABLE_STRING, strna(setup_state));
if (r < 0)
return r;
(void) table_set_color(table, cell, on_color_setup);
}
r = table_print(table, NULL);
if (r < 0)
return log_error_errno(r, "Failed to print table: %m");
if (arg_legend)
printf("\n%i links listed.\n", c);
@@ -424,20 +460,28 @@ static int get_gateway_description(
static int dump_gateways(
sd_netlink *rtnl,
sd_hwdb *hwdb,
const char *prefix,
Table *table,
int ifindex) {
_cleanup_free_ struct local_address *local = NULL;
int r, n, i;
assert(rtnl);
assert(prefix);
assert(table);
n = local_gateways(rtnl, ifindex, AF_UNSPEC, &local);
if (n < 0)
return n;
for (i = 0; i < n; i++) {
_cleanup_free_ char *gateway = NULL, *description = NULL;
_cleanup_free_ char *gateway = NULL, *description = NULL, *with_description = NULL;
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return r;
r = table_add_cell_full(table, NULL, TABLE_STRING, i == 0 ? "Gateway:" : "", SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
r = in_addr_to_string(local[i].family, &local[i].address, &gateway);
if (r < 0)
@@ -447,27 +491,25 @@ static int dump_gateways(
if (r < 0)
log_debug_errno(r, "Could not get description of gateway: %m");
printf("%*s%s",
(int) strlen(prefix),
i == 0 ? prefix : "",
gateway);
if (description)
printf(" (%s)", description);
if (description) {
with_description = strjoin(gateway, " (", description, ")");
if (!with_description)
return -ENOMEM;
}
/* Show interface name for the entry if we show
* entries for all interfaces */
if (ifindex <= 0) {
char name[IF_NAMESIZE+1];
if (if_indextoname(local[i].ifindex, name)) {
fputs(" on ", stdout);
fputs(name, stdout);
} else
printf(" on %%%i", local[i].ifindex);
}
fputc('\n', stdout);
if (format_ifname(local[i].ifindex, name))
r = table_add_cell_stringf(table, NULL, "%s on %s", with_description ?: gateway, name);
else
r = table_add_cell_stringf(table, NULL, "%s on %%%i", with_description ?: gateway, local[i].ifindex);
} else
r = table_add_cell(table, NULL, TABLE_STRING, with_description ?: gateway);
if (r < 0)
return r;
}
return 0;
@@ -475,14 +517,14 @@ static int dump_gateways(
static int dump_addresses(
sd_netlink *rtnl,
const char *prefix,
Table *table,
int ifindex) {
_cleanup_free_ struct local_address *local = NULL;
int r, n, i;
assert(rtnl);
assert(prefix);
assert(table);
n = local_addresses(rtnl, ifindex, AF_UNSPEC, &local);
if (n < 0)
@@ -491,26 +533,29 @@ static int dump_addresses(
for (i = 0; i < n; i++) {
_cleanup_free_ char *pretty = NULL;
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return r;
r = table_add_cell_full(table, NULL, TABLE_STRING, i == 0 ? "Address:" : "", SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
r = in_addr_to_string(local[i].family, &local[i].address, &pretty);
if (r < 0)
return r;
printf("%*s%s",
(int) strlen(prefix),
i == 0 ? prefix : "",
pretty);
if (ifindex <= 0) {
char name[IF_NAMESIZE+1];
if (if_indextoname(local[i].ifindex, name)) {
fputs(" on ", stdout);
fputs(name, stdout);
} else
printf(" on %%%i", local[i].ifindex);
}
fputc('\n', stdout);
if (format_ifname(local[i].ifindex, name))
r = table_add_cell_stringf(table, NULL, "%s on %s", pretty, name);
else
r = table_add_cell_stringf(table, NULL, "%s on %%%i", pretty, local[i].ifindex);
} else
r = table_add_cell(table, NULL, TABLE_STRING, pretty);
if (r < 0)
return r;
}
return 0;
@@ -518,7 +563,9 @@ static int dump_addresses(
static int dump_address_labels(sd_netlink *rtnl) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
_cleanup_(table_unrefp) Table *table = NULL;
sd_netlink_message *m;
TableCell *cell;
int r;
assert(rtnl);
@@ -535,7 +582,19 @@ static int dump_address_labels(sd_netlink *rtnl) {
if (r < 0)
return r;
printf("%10s/%s %30s\n", "Prefix", "Prefixlen", "Label");
table = table_new("Label", "Prefix/Prefixlen");
if (!table)
return -ENOMEM;
r = table_set_sort(table, 0, SIZE_MAX);
if (r < 0)
return r;
assert_se(cell = table_get_cell(table, 0, 0));
(void) table_set_align_percent(table, cell, 100);
assert_se(cell = table_get_cell(table, 0, 1));
(void) table_set_align_percent(table, cell, 100);
for (m = reply; m; m = sd_netlink_message_next(m)) {
_cleanup_free_ char *pretty = NULL;
@@ -567,10 +626,18 @@ static int dump_address_labels(sd_netlink *rtnl) {
if (r < 0)
continue;
printf("%10s/%-5u %30u\n", pretty, prefixlen, label);
r = table_add_cell_full(table, NULL, TABLE_UINT32, &label, SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
r = table_add_cell_stringf(table, &cell, "%s/%u", pretty, prefixlen);
if (r < 0)
return r;
(void) table_set_align_percent(table, cell, 100);
}
return 0;
return table_print(table, NULL);
}
static int list_address_labels(int argc, char *argv[], void *userdata) {
@@ -634,20 +701,24 @@ static int next_lldp_neighbor(FILE *f, sd_lldp_neighbor **ret) {
return 1;
}
static int dump_lldp_neighbors(const char *prefix, int ifindex) {
static int dump_lldp_neighbors(Table *table, const char *prefix, int ifindex) {
_cleanup_fclose_ FILE *f = NULL;
int r, c = 0;
assert(table);
assert(prefix);
assert(ifindex > 0);
r = open_lldp_neighbors(ifindex, &f);
if (r == -ENOENT)
return 0;
if (r < 0)
return r;
for (;;) {
const char *system_name = NULL, *port_id = NULL, *port_description = NULL;
_cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
_cleanup_free_ char *str = NULL;
r = next_lldp_neighbor(f, &n);
if (r < 0)
@@ -655,20 +726,28 @@ static int dump_lldp_neighbors(const char *prefix, int ifindex) {
if (r == 0)
break;
printf("%*s",
(int) strlen(prefix),
c == 0 ? prefix : "");
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return r;
r = table_add_cell_full(table, NULL, TABLE_STRING, c == 0 ? prefix : "", SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
(void) sd_lldp_neighbor_get_system_name(n, &system_name);
(void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id);
(void) sd_lldp_neighbor_get_port_description(n, &port_description);
printf("%s on port %s", strna(system_name), strna(port_id));
if (asprintf(&str, "%s on port %s%s%s%s",
strna(system_name), strna(port_id),
isempty(port_description) ? "" : " (",
port_description,
isempty(port_description) ? "" : ")") < 0)
return -ENOMEM;
if (!isempty(port_description))
printf(" (%s)", port_description);
putchar('\n');
r = table_add_cell(table, NULL, TABLE_STRING, str);
if (r < 0)
return r;
c++;
}
@@ -676,42 +755,54 @@ static int dump_lldp_neighbors(const char *prefix, int ifindex) {
return c;
}
static void dump_ifindexes(const char *prefix, const int *ifindexes) {
static int dump_ifindexes(Table *table, const char *prefix, const int *ifindexes) {
unsigned c;
int r;
assert(prefix);
if (!ifindexes || ifindexes[0] <= 0)
return;
return 0;
for (c = 0; ifindexes[c] > 0; c++) {
char name[IF_NAMESIZE+1];
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return r;
printf("%*s",
(int) strlen(prefix),
c == 0 ? prefix : "");
r = table_add_cell_full(table, NULL, TABLE_STRING, c == 0 ? prefix : "", SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
if (if_indextoname(ifindexes[c], name))
fputs(name, stdout);
else
printf("%i", ifindexes[c]);
fputc('\n', stdout);
r = table_add_cell(table, NULL, TABLE_IFINDEX, ifindexes + c);
if (r < 0)
return r;
}
return 0;
}
static void dump_list(const char *prefix, char **l) {
static int dump_list(Table *table, const char *prefix, char **l) {
char **i;
int r;
if (strv_isempty(l))
return;
return 0;
STRV_FOREACH(i, l) {
printf("%*s%s\n",
(int) strlen(prefix),
i == l ? prefix : "",
*i);
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return r;
r = table_add_cell_full(table, NULL, TABLE_STRING, i == l ? prefix : "", SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
r = table_add_cell(table, NULL, TABLE_STRING, *i);
if (r < 0)
return r;
}
return 0;
}
static int link_status_one(
@@ -726,8 +817,10 @@ static int link_status_one(
_cleanup_free_ char *t = NULL, *network = NULL;
const char *driver = NULL, *path = NULL, *vendor = NULL, *model = NULL, *link = NULL;
const char *on_color_operational, *off_color_operational,
*on_color_setup, *off_color_setup;
*on_color_setup, *off_color_setup;
_cleanup_free_ int *carrier_bound_to = NULL, *carrier_bound_by = NULL;
_cleanup_(table_unrefp) Table *table = NULL;
TableCell *cell;
int r;
assert(rtnl);
@@ -769,26 +862,110 @@ static int link_status_one(
(void) sd_network_link_get_carrier_bound_to(info->ifindex, &carrier_bound_to);
(void) sd_network_link_get_carrier_bound_by(info->ifindex, &carrier_bound_by);
printf("%s%s%s %i: %s\n", on_color_operational, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off_color_operational, info->ifindex, info->name);
table = table_new("DOT", "KEY", "VALUE");
if (!table)
return -ENOMEM;
printf(" Link File: %s\n"
" Network File: %s\n"
" Type: %s\n"
" State: %s%s%s (%s%s%s)\n",
strna(link),
strna(network),
strna(t),
on_color_operational, strna(operational_state), off_color_operational,
on_color_setup, strna(setup_state), off_color_setup);
table_set_header(table, false);
if (path)
printf(" Path: %s\n", path);
if (driver)
printf(" Driver: %s\n", driver);
if (vendor)
printf(" Vendor: %s\n", vendor);
if (model)
printf(" Model: %s\n", model);
r = table_add_cell(table, &cell, TABLE_STRING, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE));
if (r < 0)
return r;
(void) table_set_ellipsize_percent(table, cell, 0);
(void) table_set_color(table, cell, on_color_operational);
r = table_add_cell_stringf(table, NULL, "%i: %s", info->ifindex, info->name);
if (r < 0)
return r;
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return r;
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return r;
r = table_add_cell_full(table, NULL, TABLE_STRING, "Link File:", SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
r = table_add_cell(table, NULL, TABLE_STRING, strna(link));
if (r < 0)
return r;
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return r;
r = table_add_cell_full(table, NULL, TABLE_STRING, "Network File:", SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
r = table_add_cell(table, NULL, TABLE_STRING, strna(network));
if (r < 0)
return r;
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return r;
r = table_add_cell_full(table, NULL, TABLE_STRING, "Type:", SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
r = table_add_cell(table, NULL, TABLE_STRING, strna(t));
if (r < 0)
return r;
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return r;
r = table_add_cell_full(table, NULL, TABLE_STRING, "State:", SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
r = table_add_cell_stringf(table, NULL, "%s%s%s (%s%s%s)",
on_color_operational, strna(operational_state), off_color_operational,
on_color_setup, strna(setup_state), off_color_setup);
if (r < 0)
return r;
if (path) {
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return r;
r = table_add_cell_full(table, NULL, TABLE_STRING, "Path:", SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
r = table_add_cell(table, NULL, TABLE_STRING, path);
if (r < 0)
return r;
}
if (driver) {
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return r;
r = table_add_cell_full(table, NULL, TABLE_STRING, "Driver:", SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
r = table_add_cell(table, NULL, TABLE_STRING, driver);
if (r < 0)
return r;
}
if (vendor) {
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return r;
r = table_add_cell_full(table, NULL, TABLE_STRING, "Vendor:", SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
r = table_add_cell(table, NULL, TABLE_STRING, vendor);
if (r < 0)
return r;
}
if (model) {
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return r;
r = table_add_cell_full(table, NULL, TABLE_STRING, "Model:", SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
r = table_add_cell(table, NULL, TABLE_STRING, model);
if (r < 0)
return r;
}
if (info->has_mac_address) {
_cleanup_free_ char *description = NULL;
@@ -796,75 +973,153 @@ static int link_status_one(
(void) ieee_oui(hwdb, &info->mac_address, &description);
if (description)
printf(" HW Address: %s (%s)\n", ether_addr_to_string(&info->mac_address, ea), description);
else
printf(" HW Address: %s\n", ether_addr_to_string(&info->mac_address, ea));
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return r;
r = table_add_cell_full(table, NULL, TABLE_STRING, "HW Address:", SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
r = table_add_cell_stringf(table, NULL, "%s%s%s%s",
ether_addr_to_string(&info->mac_address, ea),
description ? " (" : "",
description,
description ? ")" : "");
if (r < 0)
return r;
}
if (info->has_mtu)
printf(" MTU: %" PRIu32 "\n", info->mtu);
if (info->has_min_mtu)
printf(" Minimum MTU: %" PRIu32 "\n", info->min_mtu);
if (info->has_max_mtu)
printf(" Maximum MTU: %" PRIu32 "\n", info->max_mtu);
if (info->has_mtu) {
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return r;
r = table_add_cell_full(table, NULL, TABLE_STRING, "MTU:", SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
r = table_add_cell_stringf(table, NULL, "%" PRIu32 " (Minimum: %" PRIu32 ", Maximum: %" PRIu32 ")",
info->mtu, info->min_mtu, info->max_mtu);
if (r < 0)
return r;
}
if (info->has_tx_queues)
printf("Transmit Queue Length: %" PRIu32 "\n", info->tx_queues);
if (info->has_rx_queues)
printf(" Receive Queue Length: %" PRIu32 "\n", info->rx_queues);
if (info->has_tx_queues || info->has_rx_queues) {
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return r;
r = table_add_cell_full(table, NULL, TABLE_STRING, "Queue Length (Tx/Rx):", SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
r = table_add_cell_stringf(table, NULL, "%" PRIu32 "/%" PRIu32, info->tx_queues, info->rx_queues);
if (r < 0)
return r;
}
(void) dump_addresses(rtnl, " Address: ", info->ifindex);
(void) dump_gateways(rtnl, hwdb, " Gateway: ", info->ifindex);
dump_list(" DNS: ", dns);
dump_list(" Search Domains: ", search_domains);
dump_list(" Route Domains: ", route_domains);
dump_list(" NTP: ", ntp);
dump_ifindexes("Carrier Bound To: ", carrier_bound_to);
dump_ifindexes("Carrier Bound By: ", carrier_bound_by);
r = dump_addresses(rtnl, table, info->ifindex);
if (r < 0)
return r;
r = dump_gateways(rtnl, hwdb, table, info->ifindex);
if (r < 0)
return r;
r = dump_list(table, "DNS:", dns);
if (r < 0)
return r;
r = dump_list(table, "Search Domains:", search_domains);
if (r < 0)
return r;
r = dump_list(table, "Route Domains:", route_domains);
if (r < 0)
return r;
r = dump_list(table, "NTP:", ntp);
if (r < 0)
return r;
r = dump_ifindexes(table, "Carrier Bound To:", carrier_bound_to);
if (r < 0)
return r;
r = dump_ifindexes(table, "Carrier Bound By:", carrier_bound_by);
if (r < 0)
return r;
(void) sd_network_link_get_timezone(info->ifindex, &tz);
if (tz)
printf(" Time Zone: %s\n", tz);
if (tz) {
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return r;
r = table_add_cell_full(table, NULL, TABLE_STRING, "Time Zone:", SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
r = table_add_cell(table, NULL, TABLE_STRING, tz);
if (r < 0)
return r;
}
(void) dump_lldp_neighbors(" Connected To: ", info->ifindex);
r = dump_lldp_neighbors(table, "Connected To:", info->ifindex);
if (r < 0)
return r;
return 0;
return table_print(table, NULL);
}
static int system_status(sd_netlink *rtnl, sd_hwdb *hwdb) {
_cleanup_free_ char *operational_state = NULL;
_cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **search_domains = NULL, **route_domains = NULL;
const char *on_color_operational, *off_color_operational;
_cleanup_(table_unrefp) Table *table = NULL;
TableCell *cell;
int r;
assert(rtnl);
(void) sd_network_get_operational_state(&operational_state);
operational_state_to_color(operational_state, &on_color_operational, &off_color_operational);
printf("%s%s%s State: %s%s%s\n",
on_color_operational, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off_color_operational,
on_color_operational, strna(operational_state), off_color_operational);
table = table_new("DOT", "KEY", "VALUE");
if (!table)
return -ENOMEM;
(void) dump_addresses(rtnl, " Address: ", 0);
(void) dump_gateways(rtnl, hwdb, " Gateway: ", 0);
table_set_header(table, false);
r = table_add_cell(table, &cell, TABLE_STRING, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE));
if (r < 0)
return r;
(void) table_set_color(table, cell, on_color_operational);
(void) table_set_ellipsize_percent(table, cell, 0);
r = table_add_cell_full(table, NULL, TABLE_STRING, "State:", SIZE_MAX, SIZE_MAX, 0, 100, 0);
if (r < 0)
return r;
r = table_add_cell(table, &cell, TABLE_STRING, strna(operational_state));
if (r < 0)
return r;
(void) table_set_color(table, cell, on_color_operational);
r = dump_addresses(rtnl, table, 0);
if (r < 0)
return r;
r = dump_gateways(rtnl, hwdb, table, 0);
if (r < 0)
return r;
(void) sd_network_get_dns(&dns);
dump_list(" DNS: ", dns);
r = dump_list(table, "DNS:", dns);
if (r < 0)
return r;
(void) sd_network_get_search_domains(&search_domains);
dump_list("Search Domains: ", search_domains);
r = dump_list(table, "Search Domains:", search_domains);
if (r < 0)
return r;
(void) sd_network_get_route_domains(&route_domains);
dump_list(" Route Domains: ", route_domains);
r = dump_list(table, "Route Domains:", route_domains);
if (r < 0)
return r;
(void) sd_network_get_ntp(&ntp);
dump_list(" NTP: ", ntp);
r = dump_list(table, "NTP:", ntp);
if (r < 0)
return r;
return 0;
return table_print(table, NULL);
}
static int link_status(int argc, char *argv[], void *userdata) {
@@ -955,8 +1210,10 @@ static void lldp_capabilities_legend(uint16_t x) {
static int link_lldp_status(int argc, char *argv[], void *userdata) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_free_ LinkInfo *links = NULL;
_cleanup_(table_unrefp) Table *table = NULL;
int i, r, c, m = 0;
uint16_t all = 0;
TableCell *cell;
r = sd_netlink_open(&rtnl);
if (r < 0)
@@ -968,14 +1225,34 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) {
(void) pager_open(arg_pager_flags);
if (arg_legend)
printf("%-16s %-17s %-16s %-11s %-17s %-16s\n",
"LINK",
"CHASSIS ID",
"SYSTEM NAME",
"CAPS",
"PORT ID",
"PORT DESCRIPTION");
table = table_new("LINK",
"CHASSIS ID",
"SYSTEM NAME",
"CAPS",
"PORT ID",
"PORT DESCRIPTION");
if (!table)
return -ENOMEM;
table_set_header(table, arg_legend);
assert_se(cell = table_get_cell(table, 0, 0));
table_set_minimum_width(table, cell, 16);
assert_se(cell = table_get_cell(table, 0, 1));
table_set_minimum_width(table, cell, 17);
assert_se(cell = table_get_cell(table, 0, 2));
table_set_minimum_width(table, cell, 16);
assert_se(cell = table_get_cell(table, 0, 3));
table_set_minimum_width(table, cell, 11);
assert_se(cell = table_get_cell(table, 0, 4));
table_set_minimum_width(table, cell, 17);
assert_se(cell = table_get_cell(table, 0, 5));
table_set_minimum_width(table, cell, 16);
for (i = 0; i < c; i++) {
_cleanup_fclose_ FILE *f = NULL;
@@ -1036,18 +1313,24 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) {
all |= cc;
}
printf("%-16s %-17s %-16s %-11s %-17s %-16s\n",
links[i].name,
strna(chassis_id),
strna(system_name),
strna(capabilities),
strna(port_id),
strna(port_description));
r = table_add_many(table,
TABLE_STRING, links[i].name,
TABLE_STRING, strna(chassis_id),
TABLE_STRING, strna(system_name),
TABLE_STRING, strna(capabilities),
TABLE_STRING, strna(port_id),
TABLE_STRING, strna(port_description));
if (r < 0)
return r;
m++;
}
}
r = table_print(table, NULL);
if (r < 0)
return r;
if (arg_legend) {
lldp_capabilities_legend(all);
printf("\n%i neighbors listed.\n", m);
@@ -1076,7 +1359,6 @@ static int link_delete_send_message(sd_netlink *rtnl, int index) {
static int link_delete(int argc, char *argv[], void *userdata) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_set_free_ Set *indexes = NULL;
char ifname[IFNAMSIZ] = "";
int index, r, i;
Iterator j;
@@ -1101,7 +1383,9 @@ static int link_delete(int argc, char *argv[], void *userdata) {
SET_FOREACH(index, indexes, j) {
r = link_delete_send_message(rtnl, index);
if (r < 0) {
if (if_indextoname(index, ifname))
char ifname[IF_NAMESIZE + 1];
if (format_ifname(index, ifname))
return log_error_errno(r, "Failed to delete interface %s: %m", ifname);
else
return log_error_errno(r, "Failed to delete interface %d: %m", index);
+7 -6
View File
@@ -14,6 +14,7 @@
#include "bus-util.h"
#include "dns-domain.h"
#include "escape.h"
#include "format-util.h"
#include "gcrypt-util.h"
#include "in-addr-util.h"
#include "main-func.h"
@@ -140,12 +141,12 @@ static void print_source(uint64_t flags, usec_t rtt) {
}
static void print_ifindex_comment(int printed_so_far, int ifindex) {
char ifname[IF_NAMESIZE];
char ifname[IF_NAMESIZE + 1];
if (ifindex <= 0)
return;
if (!if_indextoname(ifindex, ifname))
if (!format_ifname(ifindex, ifname))
log_warning_errno(errno, "Failed to resolve interface name for index %i, ignoring: %m", ifindex);
else
printf("%*s%s-- link: %s%s",
@@ -1382,7 +1383,7 @@ static int status_ifindex(sd_bus *bus, int ifindex, const char *name, StatusMode
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
_cleanup_(link_info_clear) struct link_info link_info = {};
_cleanup_free_ char *ifi = NULL, *p = NULL;
char ifname[IF_NAMESIZE] = "";
char ifname[IF_NAMESIZE + 1] = "";
char **i;
int r;
@@ -1390,7 +1391,7 @@ static int status_ifindex(sd_bus *bus, int ifindex, const char *name, StatusMode
assert(ifindex > 0);
if (!name) {
if (!if_indextoname(ifindex, ifname))
if (!format_ifname(ifindex, ifname))
return log_error_errno(errno, "Failed to resolve interface name for %i: %m", ifindex);
name = ifname;
@@ -1844,12 +1845,12 @@ static int verb_status(int argc, char **argv, void *userdata) {
}
static int log_interface_is_managed(int r, int ifindex) {
char ifname[IFNAMSIZ];
char ifname[IF_NAMESIZE + 1];
return log_error_errno(r,
"The specified interface %s is managed by systemd-networkd. Operation refused.\n"
"Please configure DNS settings for systemd-networkd managed interfaces directly in their .network files.",
strna(if_indextoname(ifindex, ifname)));
strna(format_ifname(ifindex, ifname)));
}
static int verb_dns(int argc, char **argv, void *userdata) {
+4 -2
View File
@@ -5,6 +5,7 @@
#include "af-list.h"
#include "alloc-util.h"
#include "dns-domain.h"
#include "format-util.h"
#include "resolved-dns-answer.h"
#include "resolved-dns-cache.h"
#include "resolved-dns-packet.h"
@@ -390,7 +391,7 @@ static int dns_cache_put_positive(
_cleanup_(dns_cache_item_freep) DnsCacheItem *i = NULL;
DnsCacheItem *existing;
char key_str[DNS_RESOURCE_KEY_STRING_MAX], ifname[IF_NAMESIZE];
char key_str[DNS_RESOURCE_KEY_STRING_MAX];
int r, k;
assert(c);
@@ -456,6 +457,7 @@ static int dns_cache_put_positive(
if (DEBUG_LOGGING) {
_cleanup_free_ char *t = NULL;
char ifname[IF_NAMESIZE + 1];
(void) in_addr_to_string(i->owner_family, &i->owner_address, &t);
@@ -464,7 +466,7 @@ static int dns_cache_put_positive(
i->shared_owner ? " shared" : "",
dns_resource_key_to_string(i->key, key_str, sizeof key_str),
(i->until - timestamp) / USEC_PER_SEC,
i->ifindex == 0 ? "*" : strna(if_indextoname(i->ifindex, ifname)),
i->ifindex == 0 ? "*" : strna(format_ifname(i->ifindex, ifname)),
af_to_name_short(i->owner_family),
strna(t));
}
+151
View File
@@ -1,11 +1,13 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <ctype.h>
#include <net/if.h>
#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-table.h"
#include "format-util.h"
#include "gunicode.h"
#include "memory-util.h"
#include "pager.h"
@@ -73,9 +75,14 @@ typedef struct TableData {
usec_t timespan;
uint64_t size;
char string[0];
int int_val;
int32_t int32;
int64_t int64;
unsigned uint_val;
uint32_t uint32;
uint64_t uint64;
int percent; /* we use 'int' as datatype for percent values in order to match the result of parse_percent() */
int ifindex;
/* … add more here as we start supporting more cell data types … */
};
} TableData;
@@ -226,13 +233,18 @@ static size_t table_data_size(TableDataType type, const void *data) {
return sizeof(usec_t);
case TABLE_SIZE:
case TABLE_INT64:
case TABLE_UINT64:
return sizeof(uint64_t);
case TABLE_INT32:
case TABLE_UINT32:
return sizeof(uint32_t);
case TABLE_INT:
case TABLE_UINT:
case TABLE_PERCENT:
case TABLE_IFINDEX:
return sizeof(int);
default:
@@ -380,6 +392,20 @@ int table_add_cell_full(
return 0;
}
int table_add_cell_stringf(Table *t, TableCell **ret_cell, const char *format, ...) {
_cleanup_free_ char *buffer = NULL;
va_list ap;
int r;
va_start(ap, format);
r = vasprintf(&buffer, format, ap);
va_end(ap);
if (r < 0)
return -ENOMEM;
return table_add_cell(t, ret_cell, TABLE_STRING, buffer);
}
int table_dup_cell(Table *t, TableCell *cell) {
size_t i;
@@ -664,9 +690,14 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
union {
uint64_t size;
usec_t usec;
int int_val;
int32_t int32;
int64_t int64;
unsigned uint_val;
uint32_t uint32;
uint64_t uint64;
int percent;
int ifindex;
bool b;
} buffer;
@@ -696,6 +727,26 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
data = &buffer.size;
break;
case TABLE_INT:
buffer.int_val = va_arg(ap, int);
data = &buffer.int_val;
break;
case TABLE_INT32:
buffer.int32 = va_arg(ap, int32_t);
data = &buffer.int32;
break;
case TABLE_INT64:
buffer.int64 = va_arg(ap, int64_t);
data = &buffer.int64;
break;
case TABLE_UINT:
buffer.uint_val = va_arg(ap, unsigned);
data = &buffer.uint_val;
break;
case TABLE_UINT32:
buffer.uint32 = va_arg(ap, uint32_t);
data = &buffer.uint32;
@@ -711,6 +762,11 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
data = &buffer.percent;
break;
case TABLE_IFINDEX:
buffer.ifindex = va_arg(ap, int);
data = &buffer.ifindex;
break;
case _TABLE_DATA_TYPE_MAX:
/* Used as end marker */
va_end(ap);
@@ -831,6 +887,18 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
case TABLE_SIZE:
return CMP(a->size, b->size);
case TABLE_INT:
return CMP(a->int_val, b->int_val);
case TABLE_INT32:
return CMP(a->int32, b->int32);
case TABLE_INT64:
return CMP(a->int64, b->int64);
case TABLE_UINT:
return CMP(a->uint_val, b->uint_val);
case TABLE_UINT32:
return CMP(a->uint32, b->uint32);
@@ -840,6 +908,9 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
case TABLE_PERCENT:
return CMP(a->percent, b->percent);
case TABLE_IFINDEX:
return CMP(a->ifindex, b->ifindex);
default:
;
}
@@ -952,6 +1023,54 @@ static const char *table_data_format(TableData *d) {
break;
}
case TABLE_INT: {
_cleanup_free_ char *p;
p = new(char, DECIMAL_STR_WIDTH(d->int_val) + 1);
if (!p)
return NULL;
sprintf(p, "%i", d->int_val);
d->formatted = TAKE_PTR(p);
break;
}
case TABLE_INT32: {
_cleanup_free_ char *p;
p = new(char, DECIMAL_STR_WIDTH(d->int32) + 1);
if (!p)
return NULL;
sprintf(p, "%" PRIi32, d->int32);
d->formatted = TAKE_PTR(p);
break;
}
case TABLE_INT64: {
_cleanup_free_ char *p;
p = new(char, DECIMAL_STR_WIDTH(d->int64) + 1);
if (!p)
return NULL;
sprintf(p, "%" PRIi64, d->int64);
d->formatted = TAKE_PTR(p);
break;
}
case TABLE_UINT: {
_cleanup_free_ char *p;
p = new(char, DECIMAL_STR_WIDTH(d->uint_val) + 1);
if (!p)
return NULL;
sprintf(p, "%u", d->uint_val);
d->formatted = TAKE_PTR(p);
break;
}
case TABLE_UINT32: {
_cleanup_free_ char *p;
@@ -988,6 +1107,23 @@ static const char *table_data_format(TableData *d) {
break;
}
case TABLE_IFINDEX: {
_cleanup_free_ char *p;
char name[IF_NAMESIZE + 1];
if (format_ifname(d->ifindex, name)) {
p = strdup(name);
if (!p)
return NULL;
} else {
if (asprintf(&p, "%i" , d->ifindex) < 0)
return NULL;
}
d->formatted = TAKE_PTR(p);
break;
}
default:
assert_not_reached("Unexpected type?");
}
@@ -1491,6 +1627,18 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) {
return json_variant_new_unsigned(ret, d->size);
case TABLE_INT:
return json_variant_new_integer(ret, d->int_val);
case TABLE_INT32:
return json_variant_new_integer(ret, d->int32);
case TABLE_INT64:
return json_variant_new_integer(ret, d->int64);
case TABLE_UINT:
return json_variant_new_unsigned(ret, d->uint_val);
case TABLE_UINT32:
return json_variant_new_unsigned(ret, d->uint32);
@@ -1500,6 +1648,9 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) {
case TABLE_PERCENT:
return json_variant_new_integer(ret, d->percent);
case TABLE_IFINDEX:
return json_variant_new_integer(ret, d->ifindex);
default:
return -EINVAL;
}
+6
View File
@@ -15,9 +15,14 @@ typedef enum TableDataType {
TABLE_TIMESTAMP,
TABLE_TIMESPAN,
TABLE_SIZE,
TABLE_INT,
TABLE_INT32,
TABLE_INT64,
TABLE_UINT,
TABLE_UINT32,
TABLE_UINT64,
TABLE_PERCENT,
TABLE_IFINDEX,
_TABLE_DATA_TYPE_MAX,
_TABLE_DATA_TYPE_INVALID = -1,
} TableDataType;
@@ -36,6 +41,7 @@ int table_add_cell_full(Table *t, TableCell **ret_cell, TableDataType type, cons
static inline int table_add_cell(Table *t, TableCell **ret_cell, TableDataType type, const void *data) {
return table_add_cell_full(t, ret_cell, type, data, (size_t) -1, (size_t) -1, (unsigned) -1, (unsigned) -1, (unsigned) -1);
}
int table_add_cell_stringf(Table *t, TableCell **ret_cell, const char *format, ...) _printf_(3, 4);
int table_dup_cell(Table *t, TableCell *cell);
+3 -2
View File
@@ -8,6 +8,7 @@
#include "af-list.h"
#include "alloc-util.h"
#include "errno-list.h"
#include "format-util.h"
#include "hexdecoct.h"
#include "hostname-util.h"
#include "in-addr-util.h"
@@ -77,7 +78,7 @@ static int print_gaih_addrtuples(const struct gaih_addrtuple *tuples) {
union in_addr_union u;
int r;
char family_name[DECIMAL_STR_MAX(int)];
char ifname[IF_NAMESIZE];
char ifname[IF_NAMESIZE + 1];
memcpy(&u, it->addr, 16);
r = in_addr_to_string(it->family, &u, &a);
@@ -88,7 +89,7 @@ static int print_gaih_addrtuples(const struct gaih_addrtuple *tuples) {
if (it->scopeid == 0)
goto numerical_index;
if (!if_indextoname(it->scopeid, ifname)) {
if (!format_ifname(it->scopeid, ifname)) {
log_warning_errno(errno, "if_indextoname(%d) failed: %m", it->scopeid);
numerical_index:
xsprintf(ifname, "%i", it->scopeid);