diff options
author | Lennart Poettering <lennart@poettering.net> | 2014-08-12 15:05:21 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2014-08-12 15:06:51 +0200 |
commit | 03cc0fd1431b82e59c11ae12a274c1f2df23169d (patch) | |
tree | eae515ef2bcdb6cd8b1c19d1dee5668a43202c66 /src/libsystemd/sd-network | |
parent | f01e5736f1555eec9183e48448fa3454f265b73e (diff) |
sd-network: add API to output all collected system-wide NTP and DNS servers
Also, output the collected information in "networkctl".
Diffstat (limited to 'src/libsystemd/sd-network')
-rw-r--r-- | src/libsystemd/sd-network/sd-network.c | 89 |
1 files changed, 63 insertions, 26 deletions
diff --git a/src/libsystemd/sd-network/sd-network.c b/src/libsystemd/sd-network/sd-network.c index 6375788c95..d2e608c79d 100644 --- a/src/libsystemd/sd-network/sd-network.c +++ b/src/libsystemd/sd-network/sd-network.c @@ -34,26 +34,19 @@ #include "sd-network.h" #include "network-internal.h" -_public_ int sd_network_get_link_state(int ifindex, char **state) { - _cleanup_free_ char *s = NULL, *p = NULL; +_public_ int sd_network_get_operational_state(char **state) { + _cleanup_free_ char *s = NULL; int r; - assert_return(ifindex > 0, -EINVAL); assert_return(state, -EINVAL); - if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0) - return -ENOMEM; - - r = parse_env_file(p, NEWLINE, "ADMIN_STATE", &s, NULL); + r = parse_env_file("/run/systemd/netif/state", 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 (streq(s, "initializing")) - return -EBUSY; + if (isempty(s)) + return -ENODATA; *state = s; s = NULL; @@ -61,20 +54,63 @@ _public_ int sd_network_get_link_state(int ifindex, char **state) { return 0; } -_public_ int sd_network_get_operational_state(char **state) { +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; + + assert_return(ifindex > 0, -EINVAL); assert_return(state, -EINVAL); - r = parse_env_file("/run/systemd/netif/state", NEWLINE, "OPER_STATE", - &s, NULL); + if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0) + return -ENOMEM; + + 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; *state = s; s = NULL; @@ -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; } |