summaryrefslogtreecommitdiff
path: root/src/network/networkd-manager.c
diff options
context:
space:
mode:
authorTom Gundersen <teg@jklm.no>2014-05-08 17:21:37 +0200
committerTom Gundersen <teg@jklm.no>2014-05-08 17:21:37 +0200
commitbbf7c04821a71fec67eaf0e7a34d17afc5913c13 (patch)
treec713e0a13d98e87c2462947c508b025c25dd827c /src/network/networkd-manager.c
parente0e5ce237b11f2d97189cd7725bf339b4b8a78de (diff)
sd-network: expose global operational state
Diffstat (limited to 'src/network/networkd-manager.c')
-rw-r--r--src/network/networkd-manager.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c
index 28de7919da..4b29553330 100644
--- a/src/network/networkd-manager.c
+++ b/src/network/networkd-manager.c
@@ -20,6 +20,7 @@
***/
#include <resolv.h>
+#include <linux/if.h>
#include "path-util.h"
#include "networkd.h"
@@ -80,6 +81,10 @@ int manager_new(Manager **ret) {
if (!m)
return -ENOMEM;
+ m->state_file = strdup("/run/systemd/network/state");
+ if (!m->state_file)
+ return -ENOMEM;
+
r = sd_event_default(&m->event);
if (r < 0)
return r;
@@ -135,6 +140,8 @@ void manager_free(Manager *m) {
if (!m)
return;
+ free(m->state_file);
+
udev_monitor_unref(m->udev_monitor);
udev_unref(m->udev);
sd_bus_unref(m->bus);
@@ -457,3 +464,55 @@ int manager_update_resolv_conf(Manager *m) {
return 0;
}
+
+int manager_save(Manager *m) {
+ Link *link;
+ Iterator i;
+ _cleanup_free_ char *temp_path = NULL;
+ _cleanup_fclose_ FILE *f = NULL;
+ const char *oper_state = "unknown";
+ bool dormant, carrier;
+ int r;
+
+ assert(m);
+ assert(m->state_file);
+
+ HASHMAP_FOREACH(link, m->links, i) {
+ if (link->flags & IFF_LOOPBACK)
+ continue;
+
+ if (link_has_carrier(link->flags, link->operstate))
+ carrier = true;
+ else if (link->operstate == IF_OPER_DORMANT)
+ dormant = true;
+ }
+
+ if (carrier)
+ oper_state = "carrier";
+ else if (dormant)
+ oper_state = "dormant";
+
+ r = fopen_temporary(m->state_file, &f, &temp_path);
+ if (r < 0)
+ goto finish;
+
+ fchmod(fileno(f), 0644);
+
+ fprintf(f,
+ "# This is private data. Do not parse.\n"
+ "OPER_STATE=%s\n", oper_state);
+
+ fflush(f);
+
+ if (ferror(f) || rename(temp_path, m->state_file) < 0) {
+ r = -errno;
+ unlink(m->state_file);
+ unlink(temp_path);
+ }
+
+finish:
+ if (r < 0)
+ log_error("Failed to save network state to %s: %s", m->state_file, strerror(-r));
+
+ return r;
+}