sd-network: add API to output all collected system-wide NTP and DNS servers

Also, output the collected information in "networkctl".
This commit is contained in:
Lennart Poettering 2014-08-12 15:05:21 +02:00
parent f01e5736f1
commit 03cc0fd143
3 changed files with 96 additions and 45 deletions

View File

@ -34,6 +34,64 @@
#include "sd-network.h"
#include "network-internal.h"
_public_ int sd_network_get_operational_state(char **state) {
_cleanup_free_ char *s = NULL;
int r;
assert_return(state, -EINVAL);
r = parse_env_file("/run/systemd/netif/state", NEWLINE, "OPER_STATE", &s, NULL);
if (r == -ENOENT)
return -ENODATA;
if (r < 0)
return r;
if (isempty(s))
return -ENODATA;
*state = s;
s = NULL;
return 0;
}
static int network_get_strv(const char *key, char ***ret) {
_cleanup_strv_free_ char **a = NULL;
_cleanup_free_ char *s = NULL;
int r;
assert_return(ret, -EINVAL);
r = parse_env_file("/run/systemd/netif/state", NEWLINE, key, &s, NULL);
if (r == -ENOENT)
return -ENODATA;
if (r < 0)
return r;
if (isempty(s)) {
*ret = NULL;
return 0;
}
a = strv_split(s, " ");
if (!a)
return -ENOMEM;
strv_uniq(a);
r = strv_length(a);
*ret = a;
a = NULL;
return r;
}
_public_ int sd_network_get_dns(char ***ret) {
return network_get_strv("DNS", ret);
}
_public_ int sd_network_get_ntp(char ***ret) {
return network_get_strv("NTP", ret);
}
_public_ int sd_network_get_link_state(int ifindex, char **state) {
_cleanup_free_ char *s = NULL, *p = NULL;
int r;
@ -47,11 +105,10 @@ _public_ int sd_network_get_link_state(int ifindex, char **state) {
r = parse_env_file(p, NEWLINE, "ADMIN_STATE", &s, NULL);
if (r == -ENOENT)
return -ENODATA;
else if (r < 0)
if (r < 0)
return r;
else if (!s)
return -EIO;
if (isempty(s))
return -ENODATA;
if (streq(s, "initializing"))
return -EBUSY;
@ -61,27 +118,6 @@ _public_ int sd_network_get_link_state(int ifindex, char **state) {
return 0;
}
_public_ int sd_network_get_operational_state(char **state) {
_cleanup_free_ char *s = NULL;
int r;
assert_return(state, -EINVAL);
r = parse_env_file("/run/systemd/netif/state", NEWLINE, "OPER_STATE",
&s, NULL);
if (r == -ENOENT)
return -ENODATA;
else if (r < 0)
return r;
else if (!s)
return -EIO;
*state = s;
s = NULL;
return 0;
}
_public_ int sd_network_get_link_operational_state(int ifindex, char **state) {
_cleanup_free_ char *s = NULL, *p = NULL;
int r;
@ -95,10 +131,10 @@ _public_ int sd_network_get_link_operational_state(int ifindex, char **state) {
r = parse_env_file(p, NEWLINE, "OPER_STATE", &s, NULL);
if (r == -ENOENT)
return -ENODATA;
else if (r < 0)
if (r < 0)
return r;
else if (!s)
return -EIO;
if (isempty(s))
return -ENODATA;
*state = s;
s = NULL;
@ -119,9 +155,9 @@ _public_ int sd_network_get_link_llmnr(int ifindex, char **llmnr) {
r = parse_env_file(p, NEWLINE, "LLMNR", &s, NULL);
if (r == -ENOENT)
return -ENODATA;
else if (r < 0)
if (r < 0)
return r;
else if (!s)
if (isempty(s))
return -ENODATA;
*llmnr = s;
@ -142,11 +178,12 @@ static int network_get_link_strv(const char *key, int ifindex, char ***ret) {
return -ENOMEM;
r = parse_env_file(p, NEWLINE, key, &s, NULL);
if (r == -ENOENT)
return -ENODATA;
if (r < 0)
return r;
else if (!s) {
if (isempty(s)) {
*ret = NULL;
return 0;
}

View File

@ -216,14 +216,20 @@ static int link_status(char **args, unsigned n) {
if (n <= 1) {
_cleanup_free_ char *operational_state = NULL;
_cleanup_strv_free_ char **dns = NULL, **ntp = NULL;
r = sd_network_get_operational_state(&operational_state);
if (r < 0) {
log_error("Failed to get operational state: %s", strerror(-r));
return r;
}
sd_network_get_operational_state(&operational_state);
if (operational_state)
printf(" State: %s\n", operational_state);
sd_network_get_dns(&dns);
if (!strv_isempty(dns))
dump_list(" DNS: ", dns);
sd_network_get_dns(&ntp);
if (!strv_isempty(ntp))
dump_list(" NTP: ", ntp);
printf("State: %s\n", operational_state);
return 0;
}
@ -312,7 +318,7 @@ static int link_status(char **args, unsigned n) {
sd_network_get_link_operational_state(canonical_ifindex, &operational_state);
sd_network_get_link_dns(canonical_ifindex, &dns);
sd_network_get_link_dns(canonical_ifindex, &ntp);
sd_network_get_link_ntp(canonical_ifindex, &ntp);
sprintf(devid, "n%i", canonical_ifindex);
d = udev_device_new_from_device_id(udev, devid);

View File

@ -56,6 +56,14 @@ _SD_BEGIN_DECLARATIONS;
*/
int sd_network_get_operational_state(char **state);
/* Get DNS entries for all links. These are string representations of
* IP addresses */
int sd_network_get_dns(char ***addr);
/* Get NTP entries for all links. These are domain names or string
* reperesentations of IP addresses */
int sd_network_get_ntp(char ***addr);
/* Get state from ifindex.
* Possible states: failed, configuring, configured, unmanaged
* Possible return codes:
@ -71,12 +79,6 @@ int sd_network_get_link_state(int ifindex, char **state);
*/
int sd_network_get_link_operational_state(int ifindex, char **state);
/* Indicates whether or not LLMNR should be enabled for the link
* Possible levels of support: yes, no, resolve
* Possible return codes:
* -ENODATA: networkd is not aware of the link*/
int sd_network_get_link_llmnr(int ifindex, char **llmnr);
/* Get DNS entries for a given link. These are string representations of
* IP addresses */
int sd_network_get_link_dns(int ifindex, char ***addr);
@ -85,6 +87,12 @@ int sd_network_get_link_dns(int ifindex, char ***addr);
* reperesentations of IP addresses */
int sd_network_get_link_ntp(int ifindex, char ***addr);
/* Indicates whether or not LLMNR should be enabled for the link
* Possible levels of support: yes, no, resolve
* Possible return codes:
* -ENODATA: networkd is not aware of the link*/
int sd_network_get_link_llmnr(int ifindex, char **llmnr);
/* Monitor object */
typedef struct sd_network_monitor sd_network_monitor;