network: use unlink_and_freep() cleanup functions
This also makes state files not removed on failure.
This commit is contained in:
@@ -470,14 +470,21 @@ static int manager_connect_rtnl(Manager *m) {
|
||||
static int manager_dirty_handler(sd_event_source *s, void *userdata) {
|
||||
Manager *m = userdata;
|
||||
Link *link;
|
||||
int r;
|
||||
|
||||
assert(m);
|
||||
|
||||
if (m->dirty)
|
||||
manager_save(m);
|
||||
if (m->dirty) {
|
||||
r = manager_save(m);
|
||||
if (r < 0)
|
||||
log_warning_errno(r, "Failed to update state file %s, ignoring: %m", m->state_file);
|
||||
}
|
||||
|
||||
SET_FOREACH(link, m->dirty_links)
|
||||
(void) link_save_and_clean(link);
|
||||
SET_FOREACH(link, m->dirty_links) {
|
||||
r = link_save_and_clean(link);
|
||||
if (r < 0)
|
||||
log_link_warning_errno(link, r, "Failed to update link state file %s, ignoring: %m", link->state_file);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -647,10 +654,15 @@ int manager_start(Manager *m) {
|
||||
/* The dirty handler will deal with future serialization, but the first one
|
||||
must be done explicitly. */
|
||||
|
||||
manager_save(m);
|
||||
r = manager_save(m);
|
||||
if (r < 0)
|
||||
log_warning_errno(r, "Failed to update state file %s, ignoring: %m", m->state_file);
|
||||
|
||||
HASHMAP_FOREACH(link, m->links)
|
||||
(void) link_save(link);
|
||||
HASHMAP_FOREACH(link, m->links) {
|
||||
r = link_save(link);
|
||||
if (r < 0)
|
||||
log_link_warning_errno(link, r, "Failed to update link state file %s, ignoring: %m", link->state_file);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ int manager_save(Manager *m) {
|
||||
LinkOperationalState operstate = LINK_OPERSTATE_OFF;
|
||||
LinkCarrierState carrier_state = LINK_CARRIER_STATE_OFF;
|
||||
LinkAddressState address_state = LINK_ADDRESS_STATE_OFF;
|
||||
_cleanup_free_ char *temp_path = NULL;
|
||||
_cleanup_(unlink_and_freep) char *temp_path = NULL;
|
||||
_cleanup_strv_free_ char **p = NULL;
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
Link *link;
|
||||
@@ -261,11 +261,13 @@ int manager_save(Manager *m) {
|
||||
|
||||
r = fflush_and_check(f);
|
||||
if (r < 0)
|
||||
goto fail;
|
||||
return r;
|
||||
|
||||
r = conservative_rename(temp_path, m->state_file);
|
||||
if (r < 0)
|
||||
goto fail;
|
||||
return r;
|
||||
|
||||
temp_path = mfree(temp_path);
|
||||
|
||||
if (m->operational_state != operstate) {
|
||||
m->operational_state = operstate;
|
||||
@@ -288,18 +290,12 @@ int manager_save(Manager *m) {
|
||||
if (p) {
|
||||
r = manager_send_changed_strv(m, p);
|
||||
if (r < 0)
|
||||
log_error_errno(r, "Could not emit changed properties: %m");
|
||||
log_warning_errno(r, "Could not emit changed properties, ignoring: %m");
|
||||
}
|
||||
|
||||
m->dirty = false;
|
||||
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
(void) unlink(m->state_file);
|
||||
(void) unlink(temp_path);
|
||||
|
||||
return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file);
|
||||
}
|
||||
|
||||
static void print_link_hashmap(FILE *f, const char *prefix, Hashmap* h) {
|
||||
@@ -394,7 +390,7 @@ static void serialize_addresses(
|
||||
|
||||
int link_save(Link *link) {
|
||||
const char *admin_state, *oper_state, *carrier_state, *address_state;
|
||||
_cleanup_free_ char *temp_path = NULL;
|
||||
_cleanup_(unlink_and_freep) char *temp_path = NULL;
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
int r;
|
||||
|
||||
@@ -403,10 +399,8 @@ int link_save(Link *link) {
|
||||
assert(link->lease_file);
|
||||
assert(link->manager);
|
||||
|
||||
if (link->state == LINK_STATE_LINGER) {
|
||||
(void) unlink(link->state_file);
|
||||
if (link->state == LINK_STATE_LINGER)
|
||||
return 0;
|
||||
}
|
||||
|
||||
link_lldp_save(link);
|
||||
|
||||
@@ -424,7 +418,7 @@ int link_save(Link *link) {
|
||||
|
||||
r = fopen_temporary(link->state_file, &f, &temp_path);
|
||||
if (r < 0)
|
||||
goto fail;
|
||||
return r;
|
||||
|
||||
(void) fchmod(fileno(f), 0644);
|
||||
|
||||
@@ -621,7 +615,7 @@ int link_save(Link *link) {
|
||||
if (link->dhcp_lease) {
|
||||
r = dhcp_lease_save(link->dhcp_lease, link->lease_file);
|
||||
if (r < 0)
|
||||
goto fail;
|
||||
return r;
|
||||
|
||||
fprintf(f,
|
||||
"DHCP_LEASE=%s\n",
|
||||
@@ -631,24 +625,19 @@ int link_save(Link *link) {
|
||||
|
||||
r = link_serialize_dhcp6_client(link, f);
|
||||
if (r < 0)
|
||||
goto fail;
|
||||
return r;
|
||||
|
||||
r = fflush_and_check(f);
|
||||
if (r < 0)
|
||||
goto fail;
|
||||
return r;
|
||||
|
||||
r = conservative_rename(temp_path, link->state_file);
|
||||
if (r < 0)
|
||||
goto fail;
|
||||
return r;
|
||||
|
||||
temp_path = mfree(temp_path);
|
||||
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
(void) unlink(link->state_file);
|
||||
if (temp_path)
|
||||
(void) unlink(temp_path);
|
||||
|
||||
return log_link_error_errno(link, r, "Failed to save link data to %s: %m", link->state_file);
|
||||
}
|
||||
|
||||
void link_dirty(Link *link) {
|
||||
|
||||
Reference in New Issue
Block a user