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