From 5896e03c6f0a39a0fe75ddc3cfe54af1f7dc4113 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 23 May 2019 19:59:06 +0900 Subject: [PATCH 1/9] table: add teble_add_string_cell() --- src/shared/format-table.c | 14 ++++++++++++++ src/shared/format-table.h | 1 + 2 files changed, 15 insertions(+) diff --git a/src/shared/format-table.c b/src/shared/format-table.c index 4f4a84d749..de72bf02f3 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -380,6 +380,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; diff --git a/src/shared/format-table.h b/src/shared/format-table.h index 85e3afce4c..5a52c43e25 100644 --- a/src/shared/format-table.h +++ b/src/shared/format-table.h @@ -36,6 +36,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); From d553832698c2dfa7e86ffc0755652f0f18853bd8 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 24 May 2019 18:01:04 +0900 Subject: [PATCH 2/9] table: add more basic types --- src/shared/format-table.c | 104 ++++++++++++++++++++++++++++++++++++++ src/shared/format-table.h | 4 ++ 2 files changed, 108 insertions(+) diff --git a/src/shared/format-table.c b/src/shared/format-table.c index de72bf02f3..09357694dd 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -73,6 +73,10 @@ 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() */ @@ -226,12 +230,16 @@ 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: return sizeof(int); @@ -678,6 +686,10 @@ 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; @@ -710,6 +722,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; @@ -845,6 +877,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); @@ -966,6 +1010,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; @@ -1505,6 +1597,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); diff --git a/src/shared/format-table.h b/src/shared/format-table.h index 5a52c43e25..c5cf5708d4 100644 --- a/src/shared/format-table.h +++ b/src/shared/format-table.h @@ -15,6 +15,10 @@ typedef enum TableDataType { TABLE_TIMESTAMP, TABLE_TIMESPAN, TABLE_SIZE, + TABLE_INT, + TABLE_INT32, + TABLE_INT64, + TABLE_UINT, TABLE_UINT32, TABLE_UINT64, TABLE_PERCENT, From 8390d39165c38e9a155b7a4b5bf0ccef99126a0a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 24 May 2019 18:11:13 +0900 Subject: [PATCH 3/9] table: add TABLE_IFINDEX type --- src/shared/format-table.c | 32 ++++++++++++++++++++++++++++++++ src/shared/format-table.h | 1 + 2 files changed, 33 insertions(+) diff --git a/src/shared/format-table.c b/src/shared/format-table.c index 09357694dd..4f562be81a 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include +#include #include "alloc-util.h" #include "fd-util.h" @@ -80,6 +81,7 @@ typedef struct TableData { 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; @@ -241,6 +243,7 @@ static size_t table_data_size(TableDataType type, const void *data) { case TABLE_INT: case TABLE_UINT: case TABLE_PERCENT: + case TABLE_IFINDEX: return sizeof(int); default: @@ -693,6 +696,7 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) { uint32_t uint32; uint64_t uint64; int percent; + int ifindex; bool b; } buffer; @@ -757,6 +761,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); @@ -898,6 +907,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: ; } @@ -1094,6 +1106,23 @@ static const char *table_data_format(TableData *d) { break; } + case TABLE_IFINDEX: { + _cleanup_free_ char *p; + char name[IF_NAMESIZE + 1] = {}; + + if (if_indextoname(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?"); } @@ -1618,6 +1647,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; } diff --git a/src/shared/format-table.h b/src/shared/format-table.h index c5cf5708d4..ec2bbba292 100644 --- a/src/shared/format-table.h +++ b/src/shared/format-table.h @@ -22,6 +22,7 @@ typedef enum TableDataType { TABLE_UINT32, TABLE_UINT64, TABLE_PERCENT, + TABLE_IFINDEX, _TABLE_DATA_TYPE_MAX, _TABLE_DATA_TYPE_INVALID = -1, } TableDataType; From ff7c88a2f39a3784a2439d9d0f5f66ad60b88e82 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 23 May 2019 02:21:17 +0900 Subject: [PATCH 4/9] networkctl: use Table for 'list' command --- src/network/networkctl.c | 62 +++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/src/network/networkctl.c b/src/network/networkctl.c index 321fe92f09..688aa31bd8 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -19,6 +19,7 @@ #include "device-util.h" #include "ether-addr-util.h" #include "fd-util.h" +#include "format-table.h" #include "hwdb-util.h" #include "local-addresses.h" #include "locale-util.h" @@ -228,6 +229,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 +243,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 +288,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); From 98d5bef3c5e699e09015471fe8d28db69ba72158 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 23 May 2019 01:43:21 +0900 Subject: [PATCH 5/9] networkctl: use Table for 'status' command --- src/network/networkctl.c | 450 ++++++++++++++++++++++++++++----------- 1 file changed, 321 insertions(+), 129 deletions(-) diff --git a/src/network/networkctl.c b/src/network/networkctl.c index 688aa31bd8..04d87562a8 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -110,8 +110,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; @@ -167,13 +165,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 && @@ -464,20 +459,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) @@ -487,27 +490,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]; + 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 (if_indextoname(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; @@ -515,14 +516,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) @@ -531,26 +532,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]; + 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 (if_indextoname(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; @@ -674,20 +678,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) @@ -695,20 +703,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++; } @@ -716,42 +732,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( @@ -766,8 +794,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); @@ -809,26 +839,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; @@ -836,75 +950,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) { From 0232beed9c8531c55972ae9ffa4ff8ddf48adc52 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 23 May 2019 05:03:42 +0900 Subject: [PATCH 6/9] networkctl: use Table for 'label' command --- src/network/networkctl.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/network/networkctl.c b/src/network/networkctl.c index 04d87562a8..bd1da76f3f 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -562,7 +562,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); @@ -579,7 +581,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; @@ -611,10 +625,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) { From 658e9106408feb2fe1fa94dd8a6f88f42973392e Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 23 May 2019 05:13:13 +0900 Subject: [PATCH 7/9] networkctl: use Table for 'lldp' command --- src/network/networkctl.c | 58 +++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/src/network/networkctl.c b/src/network/networkctl.c index bd1da76f3f..6eb1b64a98 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -1209,8 +1209,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) @@ -1222,14 +1224,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; @@ -1290,18 +1312,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); From 7f686722f7ffba4634b9a531cb3e8fdd4addfae8 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 29 May 2019 14:03:18 +0900 Subject: [PATCH 8/9] util: introduce format_ifname() --- src/basic/format-util.c | 10 ++++++++++ src/basic/format-util.h | 3 +++ src/basic/meson.build | 1 + 3 files changed, 14 insertions(+) create mode 100644 src/basic/format-util.c diff --git a/src/basic/format-util.c b/src/basic/format-util.c new file mode 100644 index 0000000000..39ef2fceef --- /dev/null +++ b/src/basic/format-util.c @@ -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); +} diff --git a/src/basic/format-util.h b/src/basic/format-util.h index dece5d3a67..9925a5e991 100644 --- a/src/basic/format-util.h +++ b/src/basic/format-util.h @@ -2,6 +2,7 @@ #pragma once #include +#include #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]); diff --git a/src/basic/meson.build b/src/basic/meson.build index 7d281c8841..d6caf28f14 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -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 From 518a66ec082d977b753acaca289e1a2bd9278f6b Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 29 May 2019 14:18:41 +0900 Subject: [PATCH 9/9] tree-wide: replace if_indextoname() with format_ifname() --- src/basic/socket-util.c | 4 ++-- src/machine/machinectl.c | 4 ++-- src/network/networkctl.c | 14 ++++++++------ src/resolve/resolvectl.c | 13 +++++++------ src/resolve/resolved-dns-cache.c | 6 ++++-- src/shared/format-table.c | 5 +++-- src/test/test-nss.c | 5 +++-- 7 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index 32a0d9c5d0..e1636bca6e 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -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); diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index 12de188239..dcfda07ce3 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -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); diff --git a/src/network/networkctl.c b/src/network/networkctl.c index 6eb1b64a98..a7a1a1c62f 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -20,6 +20,7 @@ #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" @@ -499,9 +500,9 @@ static int dump_gateways( /* Show interface name for the entry if we show * entries for all interfaces */ if (ifindex <= 0) { - char name[IF_NAMESIZE+1] = {}; + char name[IF_NAMESIZE+1]; - if (if_indextoname(local[i].ifindex, name)) + 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); @@ -545,9 +546,9 @@ static int dump_addresses( return r; if (ifindex <= 0) { - char name[IF_NAMESIZE+1] = {}; + char name[IF_NAMESIZE+1]; - if (if_indextoname(local[i].ifindex, name)) + 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); @@ -1358,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; @@ -1383,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); diff --git a/src/resolve/resolvectl.c b/src/resolve/resolvectl.c index 8e4315d86a..6c5b589944 100644 --- a/src/resolve/resolvectl.c +++ b/src/resolve/resolvectl.c @@ -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) { diff --git a/src/resolve/resolved-dns-cache.c b/src/resolve/resolved-dns-cache.c index 99dec28a46..524d5cdb53 100644 --- a/src/resolve/resolved-dns-cache.c +++ b/src/resolve/resolved-dns-cache.c @@ -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)); } diff --git a/src/shared/format-table.c b/src/shared/format-table.c index 4f562be81a..d6612a76d3 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -7,6 +7,7 @@ #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" @@ -1108,9 +1109,9 @@ static const char *table_data_format(TableData *d) { case TABLE_IFINDEX: { _cleanup_free_ char *p; - char name[IF_NAMESIZE + 1] = {}; + char name[IF_NAMESIZE + 1]; - if (if_indextoname(d->ifindex, name)) { + if (format_ifname(d->ifindex, name)) { p = strdup(name); if (!p) return NULL; diff --git a/src/test/test-nss.c b/src/test/test-nss.c index d0f9898378..6ba0f6887e 100644 --- a/src/test/test-nss.c +++ b/src/test/test-nss.c @@ -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);