tree-wide: replace if_indextoname() with format_ifname()

This commit is contained in:
Yu Watanabe 2019-05-29 14:18:41 +09:00
parent 7f686722f7
commit 518a66ec08
7 changed files with 29 additions and 22 deletions

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);

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);

View file

@ -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);

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) {

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));
}

View file

@ -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;

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);