1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-10-28 11:55:23 +03:00

tree-wide: port everything over to fflush_and_check()

Some places invoked fflush() directly with their own manual error
checking, let's unify all that by using fflush_and_check().

This also unifies the general error paths of fflush()+rename() file
writers.
This commit is contained in:
Lennart Poettering 2015-07-29 20:31:07 +02:00
parent 8388607b58
commit dacd6cee76
25 changed files with 248 additions and 215 deletions

View File

@ -253,6 +253,7 @@ int calendar_spec_to_string(const CalendarSpec *c, char **p) {
char *buf = NULL;
size_t sz = 0;
FILE *f;
int r;
assert(c);
assert(p);
@ -278,12 +279,11 @@ int calendar_spec_to_string(const CalendarSpec *c, char **p) {
fputc(':', f);
format_chain(f, 2, c->second);
fflush(f);
if (ferror(f)) {
r = fflush_and_check(f);
if (r < 0) {
free(buf);
fclose(f);
return -ENOMEM;
return r;
}
fclose(f);

View File

@ -28,21 +28,15 @@
#include "fileio.h"
int write_string_stream(FILE *f, const char *line, bool enforce_newline) {
assert(f);
assert(line);
errno = 0;
fputs(line, f);
if (enforce_newline && !endswith(line, "\n"))
fputc('\n', f);
fflush(f);
if (ferror(f))
return errno ? -errno : -EIO;
return 0;
return fflush_and_check(f);
}
static int write_string_file_atomic(const char *fn, const char *line, bool enforce_newline) {

View File

@ -489,9 +489,9 @@ static int copy_file(const char *from, const char *to, bool force) {
}
} while (!feof(f));
fflush(g);
if (ferror(g)) {
r = log_error_errno(EIO, "Failed to write \"%s\": %m", to);
r = fflush_and_check(g);
if (r < 0) {
log_error_errno(r, "Failed to write \"%s\": %m", to);
goto error;
}
@ -519,7 +519,7 @@ static int copy_file(const char *from, const char *to, bool force) {
return 0;
error:
unlink(p);
(void) unlink(p);
return r;
}

View File

@ -1069,10 +1069,9 @@ static int method_dump(sd_bus_message *message, void *userdata, sd_bus_error *er
manager_dump_units(m, f, NULL);
manager_dump_jobs(m, f, NULL);
fflush(f);
if (ferror(f))
return -ENOMEM;
r = fflush_and_check(f);
if (r < 0)
return r;
return sd_bus_reply_method_return(message, "s", dump);
}

View File

@ -1701,6 +1701,7 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
ssize_t n;
struct signalfd_siginfo sfsi;
bool sigchld = false;
int r;
assert(m);
assert(m->signal_fd == fd);
@ -1809,20 +1810,16 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
f = open_memstream(&dump, &size);
if (!f) {
log_warning("Failed to allocate memory stream.");
log_warning_errno(errno, "Failed to allocate memory stream: %m");
break;
}
manager_dump_units(m, f, "\t");
manager_dump_jobs(m, f, "\t");
if (ferror(f)) {
log_warning("Failed to write status stream");
break;
}
if (fflush(f)) {
log_warning("Failed to flush status stream");
r = fflush_and_check(f);
if (r < 0) {
log_warning_errno(r, "Failed to write status stream: %m");
break;
}

View File

@ -97,9 +97,9 @@ static int add_cryptsetup(const char *id, const char *what, bool rw, char **devi
id, what, rw ? "" : "read-only",
id);
fflush(f);
if (ferror(f))
return log_error_errno(errno, "Failed to write file %s: %m", p);
r = fflush_and_check(f);
if (r < 0)
return log_error_errno(r, "Failed to write file %s: %m", p);
from = strjoina("../", n);
@ -223,9 +223,9 @@ static int add_mount(
else
fprintf(f, "Options=%s\n", rw ? "rw" : "ro");
fflush(f);
if (ferror(f))
return log_error_errno(errno, "Failed to write unit file %s: %m", p);
r = fflush_and_check(f);
if (r < 0)
return log_error_errno(r, "Failed to write unit file %s: %m", p);
if (post) {
lnk = strjoin(arg_dest, "/", post, ".requires/", unit, NULL);
@ -301,9 +301,9 @@ static int add_automount(
where,
(unsigned long long)timeout / USEC_PER_SEC);
fflush(f);
if (ferror(f))
return log_error_errno(errno, "Failed to write unit file %s: %m", p);
r = fflush_and_check(f);
if (r < 0)
return log_error_errno(r, "Failed to write unit file %s: %m", p);
lnk = strjoin(arg_dest, "/" SPECIAL_LOCAL_FS_TARGET ".wants/", unit, NULL);
if (!lnk)
@ -426,9 +426,9 @@ static int add_swap(const char *path) {
"What=%s\n",
path);
fflush(f);
if (ferror(f))
return log_error_errno(errno, "Failed to write unit file %s: %m", unit);
r = fflush_and_check(f);
if (r < 0)
return log_error_errno(r, "Failed to write unit file %s: %m", unit);
lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET ".wants/", name, NULL);
if (!lnk)

View File

@ -126,26 +126,31 @@ static int update_cursor_state(Uploader *u) {
r = fopen_temporary(u->state_file, &f, &temp_path);
if (r < 0)
goto finish;
goto fail;
fprintf(f,
"# This is private data. Do not parse.\n"
"LAST_CURSOR=%s\n",
u->last_cursor);
fflush(f);
r = fflush_and_check(f);
if (r < 0)
goto fail;
if (ferror(f) || rename(temp_path, u->state_file) < 0) {
if (rename(temp_path, u->state_file) < 0) {
r = -errno;
unlink(u->state_file);
unlink(temp_path);
goto fail;
}
finish:
if (r < 0)
log_error_errno(r, "Failed to save state %s: %m", u->state_file);
return 0;
return r;
fail:
if (temp_path)
(void) unlink(temp_path);
(void) unlink(u->state_file);
return log_error_errno(r, "Failed to save state %s: %m", u->state_file);
}
static int load_cursor_state(Uploader *u) {

View File

@ -371,25 +371,23 @@ static long write_catalog(const char *database, Hashmap *h, struct strbuf *sb,
goto error;
}
fflush(w);
if (ferror(w)) {
log_error("%s: failed to write database.", p);
r = fflush_and_check(w);
if (r < 0) {
log_error_errno(r, "%s: failed to write database: %m", p);
goto error;
}
fchmod(fileno(w), 0644);
if (rename(p, database) < 0) {
log_error_errno(errno, "rename (%s -> %s) failed: %m", p, database);
r = -errno;
r = log_error_errno(errno, "rename (%s -> %s) failed: %m", p, database);
goto error;
}
return ftell(w);
error:
unlink(p);
(void) unlink(p);
return r;
}

View File

@ -142,7 +142,7 @@ static int stdout_stream_save(StdoutStream *s) {
r = fopen_temporary(s->state_file, &f, &temp_path);
if (r < 0)
goto finish;
goto fail;
fprintf(f,
"# This is private data. Do not parse\n"
@ -163,7 +163,7 @@ static int stdout_stream_save(StdoutStream *s) {
escaped = cescape(s->identifier);
if (!escaped) {
r = -ENOMEM;
goto finish;
goto fail;
}
fprintf(f, "IDENTIFIER=%s\n", escaped);
@ -175,7 +175,7 @@ static int stdout_stream_save(StdoutStream *s) {
escaped = cescape(s->unit_id);
if (!escaped) {
r = -ENOMEM;
goto finish;
goto fail;
}
fprintf(f, "UNIT=%s\n", escaped);
@ -183,16 +183,13 @@ static int stdout_stream_save(StdoutStream *s) {
r = fflush_and_check(f);
if (r < 0)
goto finish;
goto fail;
if (rename(temp_path, s->state_file) < 0) {
r = -errno;
goto finish;
goto fail;
}
free(temp_path);
temp_path = NULL;
/* Store the connection fd in PID 1, so that we get it passed
* in again on next start */
if (!s->fdstore) {
@ -200,14 +197,15 @@ static int stdout_stream_save(StdoutStream *s) {
s->fdstore = true;
}
finish:
return 0;
fail:
(void) unlink(s->state_file);
if (temp_path)
unlink(temp_path);
(void) unlink(temp_path);
if (r < 0)
log_error_errno(r, "Failed to save stream data %s: %m", s->state_file);
return r;
return log_error_errno(r, "Failed to save stream data %s: %m", s->state_file);
}
static int stdout_stream_log(StdoutStream *s, const char *p) {

View File

@ -643,13 +643,13 @@ int sd_dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
r = fopen_temporary(lease_file, &f, &temp_path);
if (r < 0)
goto finish;
goto fail;
fchmod(fileno(f), 0644);
r = sd_dhcp_lease_get_address(lease, &address);
if (r < 0)
goto finish;
goto fail;
fprintf(f,
"# This is private data. Do not parse.\n"
@ -657,7 +657,7 @@ int sd_dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
r = sd_dhcp_lease_get_netmask(lease, &address);
if (r < 0)
goto finish;
goto fail;
fprintf(f, "NETMASK=%s\n", inet_ntoa(address));
@ -713,7 +713,7 @@ int sd_dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
client_id_hex = hexmem(client_id, client_id_len);
if (!client_id_hex) {
r = -ENOMEM;
goto finish;
goto fail;
}
fprintf(f, "CLIENTID=%s\n", client_id_hex);
}
@ -725,26 +725,27 @@ int sd_dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
option_hex = hexmem(data, data_len);
if (!option_hex) {
r = -ENOMEM;
goto finish;
goto fail;
}
fprintf(f, "VENDOR_SPECIFIC=%s\n", option_hex);
}
r = 0;
r = fflush_and_check(f);
if (r < 0)
goto fail;
fflush(f);
if (ferror(f) || rename(temp_path, lease_file) < 0) {
if (rename(temp_path, lease_file) < 0) {
r = -errno;
unlink(lease_file);
unlink(temp_path);
goto fail;
}
finish:
if (r < 0)
log_error_errno(r, "Failed to save lease data %s: %m", lease_file);
return 0;
return r;
fail:
if (temp_path)
(void) unlink(temp_path);
return log_error_errno(r, "Failed to save lease data %s: %m", lease_file);
}
int sd_dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file) {

View File

@ -440,7 +440,7 @@ int sd_lldp_save(sd_lldp *lldp, const char *lldp_file) {
r = fopen_temporary(lldp_file, &f, &temp_path);
if (r < 0)
goto finish;
goto fail;
fchmod(fileno(f), 0644);
@ -457,8 +457,10 @@ int sd_lldp_save(sd_lldp *lldp, const char *lldp_file) {
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], type);
s = strdup(buf);
if (!s)
return -ENOMEM;
if (!s) {
r = -ENOMEM;
goto fail;
}
r = lldp_read_port_id(p->packet, &type, &length, &port_id);
if (r < 0)
@ -466,8 +468,10 @@ int sd_lldp_save(sd_lldp *lldp, const char *lldp_file) {
if (type != LLDP_PORT_SUBTYPE_MAC_ADDRESS) {
k = strndup((char *) port_id, length -1);
if (!k)
return -ENOMEM;
if (!k) {
r = -ENOMEM;
goto fail;
}
sprintf(buf, "'_Port=%s' '_PType=%d' ", k , type);
free(k);
@ -478,8 +482,10 @@ int sd_lldp_save(sd_lldp *lldp, const char *lldp_file) {
}
k = strappend(s, buf);
if (!k)
return -ENOMEM;
if (!k) {
r = -ENOMEM;
goto fail;
}
free(s);
s = k;
@ -493,8 +499,10 @@ int sd_lldp_save(sd_lldp *lldp, const char *lldp_file) {
sprintf(buf, "'_TTL="USEC_FMT"' ", p->until);
k = strappend(s, buf);
if (!k)
return -ENOMEM;
if (!k) {
r = -ENOMEM;
goto fail;
}
free(s);
s = k;
@ -504,15 +512,19 @@ int sd_lldp_save(sd_lldp *lldp, const char *lldp_file) {
k = strappend(s, "'_NAME=N/A' ");
else {
t = strndup(k, length);
if (!t)
return -ENOMEM;
if (!t) {
r = -ENOMEM;
goto fail;
}
k = strjoin(s, "'_NAME=", t, "' ", NULL);
free(t);
}
if (!k)
return -ENOMEM;
if (!k) {
r = -ENOMEM;
goto fail;
}
free(s);
s = k;
@ -522,8 +534,10 @@ int sd_lldp_save(sd_lldp *lldp, const char *lldp_file) {
sprintf(buf, "'_CAP=%x'", data);
k = strappend(s, buf);
if (!k)
return -ENOMEM;
if (!k) {
r = -ENOMEM;
goto fail;
}
free(s);
s = k;
@ -531,21 +545,23 @@ int sd_lldp_save(sd_lldp *lldp, const char *lldp_file) {
fprintf(f, "%s\n", s);
}
}
r = 0;
fflush(f);
r = fflush_and_check(f);
if (r < 0)
goto fail;
if (ferror(f) || rename(temp_path, lldp_file) < 0) {
if (rename(temp_path, lldp_file) < 0) {
r = -errno;
unlink(lldp_file);
unlink(temp_path);
goto fail;
}
finish:
if (r < 0)
log_error("Failed to save lldp data %s: %s", lldp_file, strerror(-r));
return 0;
return r;
fail:
if (temp_path)
(void) unlink(temp_path);
return log_error_errno(r, "Failed to save lldp data %s: %m", lldp_file);
}
int sd_lldp_start(sd_lldp *lldp) {

View File

@ -551,9 +551,8 @@ int bus_pcap_header(size_t snaplen, FILE *f) {
hdr.snaplen = (uint32_t) snaplen;
fwrite(&hdr, 1, sizeof(hdr), f);
fflush(f);
return 0;
return fflush_and_check(f);
}
int bus_message_pcap_frame(sd_bus_message *m, size_t snaplen, FILE *f) {
@ -598,7 +597,5 @@ int bus_message_pcap_frame(sd_bus_message *m, size_t snaplen, FILE *f) {
snaplen -= w;
}
fflush(f);
return 0;
return fflush_and_check(f);
}

View File

@ -179,10 +179,10 @@ int introspect_finish(struct introspect *i, sd_bus *bus, sd_bus_message *m, sd_b
assert(reply);
fputs("</node>\n", i->f);
fflush(i->f);
if (ferror(i->f))
return -ENOMEM;
r = fflush_and_check(i->f);
if (r < 0)
return r;
r = sd_bus_message_new_method_return(m, &q);
if (r < 0)
@ -204,8 +204,6 @@ void introspect_free(struct introspect *i) {
if (i->f)
fclose(i->f);
if (i->introspection)
free(i->introspection);
free(i->introspection);
zero(*i);
}

View File

@ -914,6 +914,7 @@ char *bus_match_to_string(struct bus_match_component *components, unsigned n_com
char *buffer = NULL;
size_t size = 0;
unsigned i;
int r;
if (n_components <= 0)
return strdup("");
@ -942,8 +943,8 @@ char *bus_match_to_string(struct bus_match_component *components, unsigned n_com
fputc('\'', f);
}
fflush(f);
if (ferror(f))
r = fflush_and_check(f);
if (r < 0)
return NULL;
return buffer;

View File

@ -1082,12 +1082,10 @@ int device_update_db(sd_device *device) {
return 0;
fail:
log_error_errno(r, "failed to create %s file '%s' for '%s'", has_info ? "db" : "empty",
path, device->devpath);
unlink(path);
unlink(path_tmp);
(void) unlink(path);
(void) unlink(path_tmp);
return r;
return log_error_errno(r, "failed to create %s file '%s' for '%s'", has_info ? "db" : "empty", path, device->devpath);
}
int device_delete_db(sd_device *device) {

View File

@ -476,15 +476,25 @@ static int x11_write_data(Context *c) {
fprintf(f, " Option \"XkbOptions\" \"%s\"\n", c->x11_options);
fputs("EndSection\n", f);
fflush(f);
if (ferror(f) || rename(temp_path, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) {
r = fflush_and_check(f);
if (r < 0)
goto fail;
if (rename(temp_path, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) {
r = -errno;
unlink("/etc/X11/xorg.conf.d/00-keyboard.conf");
unlink(temp_path);
return r;
} else
return 0;
goto fail;
}
return 0;
fail:
(void) unlink("/etc/X11/xorg.conf.d/00-keyboard.conf");
if (temp_path)
(void) unlink(temp_path);
return r;
}
static int vconsole_reload(sd_bus *bus) {

View File

@ -1816,17 +1816,22 @@ static int update_schedule_file(Manager *m) {
if (!isempty(m->wall_message))
fprintf(f, "WALL_MESSAGE=%s\n", t);
(void) fflush_and_check(f);
r = fflush_and_check(f);
if (r < 0)
goto fail;
if (ferror(f) || rename(temp_path, "/run/systemd/shutdown/scheduled") < 0) {
log_error_errno(errno, "Failed to write information about scheduled shutdowns: %m");
if (rename(temp_path, "/run/systemd/shutdown/scheduled") < 0) {
r = -errno;
(void) unlink(temp_path);
(void) unlink("/run/systemd/shutdown/scheduled");
goto fail;
}
return r;
return 0;
fail:
(void) unlink(temp_path);
(void) unlink("/run/systemd/shutdown/scheduled");
return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m");
}
static int manager_scheduled_shutdown_handler(

View File

@ -86,11 +86,11 @@ int inhibitor_save(Inhibitor *i) {
r = mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0);
if (r < 0)
goto finish;
goto fail;
r = fopen_temporary(i->state_file, &f, &temp_path);
if (r < 0)
goto finish;
goto fail;
fchmod(fileno(f), 0644);
@ -128,19 +128,24 @@ int inhibitor_save(Inhibitor *i) {
if (i->fifo_path)
fprintf(f, "FIFO=%s\n", i->fifo_path);
fflush(f);
r = fflush_and_check(f);
if (r < 0)
goto fail;
if (ferror(f) || rename(temp_path, i->state_file) < 0) {
if (rename(temp_path, i->state_file) < 0) {
r = -errno;
unlink(i->state_file);
unlink(temp_path);
goto fail;
}
finish:
if (r < 0)
log_error_errno(r, "Failed to save inhibit data %s: %m", i->state_file);
return 0;
return r;
fail:
(void) unlink(i->state_file);
if (temp_path)
(void) unlink(temp_path);
return log_error_errno(r, "Failed to save inhibit data %s: %m", i->state_file);
}
int inhibitor_start(Inhibitor *i) {

View File

@ -93,11 +93,11 @@ int seat_save(Seat *s) {
r = mkdir_safe_label("/run/systemd/seats", 0755, 0, 0);
if (r < 0)
goto finish;
goto fail;
r = fopen_temporary(s->state_file, &f, &temp_path);
if (r < 0)
goto finish;
goto fail;
fchmod(fileno(f), 0644);
@ -141,19 +141,24 @@ int seat_save(Seat *s) {
i->sessions_by_seat_next ? ' ' : '\n');
}
fflush(f);
r = fflush_and_check(f);
if (r < 0)
goto fail;
if (ferror(f) || rename(temp_path, s->state_file) < 0) {
if (rename(temp_path, s->state_file) < 0) {
r = -errno;
unlink(s->state_file);
unlink(temp_path);
goto fail;
}
finish:
if (r < 0)
log_error_errno(r, "Failed to save seat data %s: %m", s->state_file);
return 0;
return r;
fail:
(void) unlink(s->state_file);
if (temp_path)
(void) unlink(temp_path);
return log_error_errno(r, "Failed to save seat data %s: %m", s->state_file);
}
int seat_load(Seat *s) {

View File

@ -165,11 +165,11 @@ int session_save(Session *s) {
r = mkdir_safe_label("/run/systemd/sessions", 0755, 0, 0);
if (r < 0)
goto finish;
goto fail;
r = fopen_temporary(s->state_file, &f, &temp_path);
if (r < 0)
goto finish;
goto fail;
assert(s->user);
@ -217,7 +217,7 @@ int session_save(Session *s) {
escaped = cescape(s->remote_host);
if (!escaped) {
r = -ENOMEM;
goto finish;
goto fail;
}
fprintf(f, "REMOTE_HOST=%s\n", escaped);
@ -229,7 +229,7 @@ int session_save(Session *s) {
escaped = cescape(s->remote_user);
if (!escaped) {
r = -ENOMEM;
goto finish;
goto fail;
}
fprintf(f, "REMOTE_USER=%s\n", escaped);
@ -241,7 +241,7 @@ int session_save(Session *s) {
escaped = cescape(s->service);
if (!escaped) {
r = -ENOMEM;
goto finish;
goto fail;
}
fprintf(f, "SERVICE=%s\n", escaped);
@ -254,7 +254,7 @@ int session_save(Session *s) {
escaped = cescape(s->desktop);
if (!escaped) {
r = -ENOMEM;
goto finish;
goto fail;
}
fprintf(f, "DESKTOP=%s\n", escaped);
@ -282,21 +282,27 @@ int session_save(Session *s) {
if (s->controller)
fprintf(f, "CONTROLLER=%s\n", s->controller);
fflush(f);
r = fflush_and_check(f);
if (r < 0)
goto fail;
if (ferror(f) || rename(temp_path, s->state_file) < 0) {
if (rename(temp_path, s->state_file) < 0) {
r = -errno;
unlink(s->state_file);
unlink(temp_path);
goto fail;
}
finish:
if (r < 0)
log_error_errno(r, "Failed to save session data %s: %m", s->state_file);
return 0;
return r;
fail:
(void) unlink(s->state_file);
if (temp_path)
(void) unlink(temp_path);
return log_error_errno(r, "Failed to save session data %s: %m", s->state_file);
}
int session_load(Session *s) {
_cleanup_free_ char *remote = NULL,
*seat = NULL,

View File

@ -116,11 +116,11 @@ static int user_save_internal(User *u) {
r = mkdir_safe_label("/run/systemd/users", 0755, 0, 0);
if (r < 0)
goto finish;
goto fail;
r = fopen_temporary(u->state_file, &f, &temp_path);
if (r < 0)
goto finish;
goto fail;
fchmod(fileno(f), 0644);
@ -241,19 +241,24 @@ static int user_save_internal(User *u) {
fputc('\n', f);
}
fflush(f);
r = fflush_and_check(f);
if (r < 0)
goto fail;
if (ferror(f) || rename(temp_path, u->state_file) < 0) {
if (rename(temp_path, u->state_file) < 0) {
r = -errno;
unlink(u->state_file);
unlink(temp_path);
goto fail;
}
finish:
if (r < 0)
log_error_errno(r, "Failed to save user data %s: %m", u->state_file);
return 0;
return r;
fail:
(void) unlink(u->state_file);
if (temp_path)
(void) unlink(temp_path);
return log_error_errno(r, "Failed to save user data %s: %m", u->state_file);
}
int user_save(User *u) {

View File

@ -112,13 +112,13 @@ int machine_save(Machine *m) {
r = mkdir_safe_label("/run/systemd/machines", 0755, 0, 0);
if (r < 0)
goto finish;
goto fail;
r = fopen_temporary(m->state_file, &f, &temp_path);
if (r < 0)
goto finish;
goto fail;
fchmod(fileno(f), 0644);
(void) fchmod(fileno(f), 0644);
fprintf(f,
"# This is private data. Do not parse.\n"
@ -131,7 +131,7 @@ int machine_save(Machine *m) {
escaped = cescape(m->unit);
if (!escaped) {
r = -ENOMEM;
goto finish;
goto fail;
}
fprintf(f, "SCOPE=%s\n", escaped); /* We continue to call this "SCOPE=" because it is internal only, and we want to stay compatible with old files */
@ -146,7 +146,7 @@ int machine_save(Machine *m) {
escaped = cescape(m->service);
if (!escaped) {
r = -ENOMEM;
goto finish;
goto fail;
}
fprintf(f, "SERVICE=%s\n", escaped);
}
@ -157,7 +157,7 @@ int machine_save(Machine *m) {
escaped = cescape(m->root_directory);
if (!escaped) {
r = -ENOMEM;
goto finish;
goto fail;
}
fprintf(f, "ROOT=%s\n", escaped);
}
@ -195,16 +195,13 @@ int machine_save(Machine *m) {
r = fflush_and_check(f);
if (r < 0)
goto finish;
goto fail;
if (rename(temp_path, m->state_file) < 0) {
r = -errno;
goto finish;
goto fail;
}
free(temp_path);
temp_path = NULL;
if (m->unit) {
char *sl;
@ -215,14 +212,15 @@ int machine_save(Machine *m) {
(void) symlink(m->name, sl);
}
finish:
return 0;
fail:
(void) unlink(m->state_file);
if (temp_path)
unlink(temp_path);
(void) unlink(temp_path);
if (r < 0)
log_error_errno(r, "Failed to save machine data %s: %m", m->state_file);
return r;
return log_error_errno(r, "Failed to save machine data %s: %m", m->state_file);
}
static void machine_unlink(Machine *m) {

View File

@ -2388,14 +2388,13 @@ int link_save(Link *link) {
}
return 0;
fail:
log_link_error_errno(link, r, "Failed to save link data to %s: %m", link->state_file);
(void) unlink(link->state_file);
fail:
(void) unlink(link->state_file);
if (temp_path)
(void) unlink(temp_path);
return r;
return log_link_error_errno(link, r, "Failed to save link data to %s: %m", link->state_file);
}
static const char* const link_state_table[_LINK_STATE_MAX] = {

View File

@ -818,10 +818,10 @@ int manager_save(Manager *m) {
return 0;
fail:
log_error_errno(r, "Failed to save network state to %s: %m", m->state_file);
unlink(m->state_file);
unlink(temp_path);
return r;
(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);
}
int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) {

View File

@ -382,11 +382,9 @@ int ask_password_agent(
if (id)
fprintf(f, "Id=%s\n", id);
fflush(f);
if (ferror(f)) {
log_error_errno(errno, "Failed to write query file: %m");
r = -errno;
r = fflush_and_check(f);
if (r < 0) {
log_error_errno(r, "Failed to write query file: %m");
goto finish;
}