| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-network.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "format-table.h" |
| 7 |
#include "fs-util.h" |
| 8 |
#include "glyph-util.h" |
| 9 |
#include "log.h" |
| 10 |
#include "networkctl.h" |
| 11 |
#include "networkctl-dump-util.h" |
| 12 |
#include "networkctl-journal.h" |
| 13 |
#include "networkctl-status-system.h" |
| 14 |
#include "networkctl-util.h" |
| 15 |
#include "sort-util.h" |
| 16 |
#include "string-util.h" |
| 17 |
#include "strv.h" |
| 18 |
|
| 19 |
static int ifindex_str_compare_func(char * const *a, char * const *b) { |
| 20 |
size_t al, bl; |
| 21 |
int r; |
| 22 |
|
| 23 |
assert(a); |
| 24 |
assert(b); |
| 25 |
|
| 26 |
al = strlen_ptr(*a); |
| 27 |
bl = strlen_ptr(*b); |
| 28 |
|
| 29 |
r = CMP(al, bl); |
| 30 |
if (r != 0) |
| 31 |
return r; |
| 32 |
|
| 33 |
return strcmp_ptr(*a, *b); |
| 34 |
} |
| 35 |
|
| 36 |
static int get_netifs(char **ret) { |
| 37 |
_cleanup_strv_free_ char **netifs = NULL; |
| 38 |
_cleanup_free_ char *joined = NULL; |
| 39 |
int r; |
| 40 |
|
| 41 |
assert(ret); |
| 42 |
|
| 43 |
r = get_files_in_directory("/run/systemd/netif/links/", &netifs); |
| 44 |
if (IN_SET(r, 0, -ENOENT)) { |
| 45 |
*ret = NULL; |
| 46 |
return 0; |
| 47 |
} |
| 48 |
if (r < 0) |
| 49 |
return log_error_errno(r, "Failed to list network interfaces: %m"); |
| 50 |
|
| 51 |
typesafe_qsort(netifs, strv_length(netifs), ifindex_str_compare_func); |
| 52 |
|
| 53 |
joined = strv_join(netifs, ", "); |
| 54 |
if (!joined) |
| 55 |
return log_oom(); |
| 56 |
|
| 57 |
*ret = TAKE_PTR(joined); |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
int system_status(sd_netlink *rtnl, sd_hwdb *hwdb) { |
| 62 |
_cleanup_free_ char *operational_state = NULL, *online_state = NULL, *netifs_joined = NULL; |
| 63 |
_cleanup_strv_free_ char **dns = NULL, **ntp = NULL, **search_domains = NULL, **route_domains = NULL; |
| 64 |
const char *on_color_operational, *off_color_operational, *on_color_online; |
| 65 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 66 |
int r; |
| 67 |
|
| 68 |
assert(rtnl); |
| 69 |
|
| 70 |
(void) sd_network_get_operational_state(&operational_state); |
| 71 |
operational_state_to_color(NULL, operational_state, &on_color_operational, &off_color_operational); |
| 72 |
|
| 73 |
(void) sd_network_get_online_state(&online_state); |
| 74 |
online_state_to_color(online_state, &on_color_online, NULL); |
| 75 |
|
| 76 |
table = table_new_vertical(); |
| 77 |
if (!table) |
| 78 |
return log_oom(); |
| 79 |
|
| 80 |
if (arg_full) |
| 81 |
table_set_width(table, 0); |
| 82 |
|
| 83 |
r = get_netifs(&netifs_joined); |
| 84 |
if (r < 0) |
| 85 |
return r; |
| 86 |
|
| 87 |
r = table_add_many(table, |
| 88 |
TABLE_FIELD, "State", |
| 89 |
TABLE_STRING, strna(operational_state), |
| 90 |
TABLE_SET_COLOR, on_color_operational, |
| 91 |
TABLE_FIELD, "Online state", |
| 92 |
TABLE_STRING, online_state ?: "unknown", |
| 93 |
TABLE_SET_COLOR, on_color_online); |
| 94 |
if (r < 0) |
| 95 |
return table_log_add_error(r); |
| 96 |
|
| 97 |
r = dump_addresses(rtnl, /* message= */ NULL, table, /* ifindex= */ 0); |
| 98 |
if (r < 0) |
| 99 |
return r; |
| 100 |
|
| 101 |
r = dump_gateways(rtnl, hwdb, table, 0); |
| 102 |
if (r < 0) |
| 103 |
return r; |
| 104 |
|
| 105 |
(void) sd_network_get_dns(&dns); |
| 106 |
r = dump_list(table, "DNS", dns); |
| 107 |
if (r < 0) |
| 108 |
return r; |
| 109 |
|
| 110 |
(void) sd_network_get_search_domains(&search_domains); |
| 111 |
r = dump_list(table, "Search Domains", search_domains); |
| 112 |
if (r < 0) |
| 113 |
return r; |
| 114 |
|
| 115 |
(void) sd_network_get_route_domains(&route_domains); |
| 116 |
r = dump_list(table, "Route Domains", route_domains); |
| 117 |
if (r < 0) |
| 118 |
return r; |
| 119 |
|
| 120 |
(void) sd_network_get_ntp(&ntp); |
| 121 |
r = dump_list(table, "NTP", ntp); |
| 122 |
if (r < 0) |
| 123 |
return r; |
| 124 |
|
| 125 |
printf("%s%s%s Interfaces: %s\n", |
| 126 |
on_color_operational, glyph(GLYPH_BLACK_CIRCLE), off_color_operational, |
| 127 |
strna(netifs_joined)); |
| 128 |
|
| 129 |
r = table_print_or_warn(table); |
| 130 |
if (r < 0) |
| 131 |
return r; |
| 132 |
|
| 133 |
return show_logs(0, NULL); |
| 134 |
} |