]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: port everything over to fflush_and_check() 779/head
authorLennart Poettering <lennart@poettering.net>
Wed, 29 Jul 2015 18:31:07 +0000 (20:31 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 29 Jul 2015 18:31:07 +0000 (20:31 +0200)
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.

25 files changed:
src/basic/calendarspec.c
src/basic/fileio.c
src/boot/bootctl.c
src/core/dbus-manager.c
src/core/manager.c
src/gpt-auto-generator/gpt-auto-generator.c
src/journal-remote/journal-upload.c
src/journal/catalog.c
src/journal/journald-stream.c
src/libsystemd-network/sd-dhcp-lease.c
src/libsystemd-network/sd-lldp.c
src/libsystemd/sd-bus/bus-dump.c
src/libsystemd/sd-bus/bus-introspect.c
src/libsystemd/sd-bus/bus-match.c
src/libsystemd/sd-device/device-private.c
src/locale/localed.c
src/login/logind-dbus.c
src/login/logind-inhibit.c
src/login/logind-seat.c
src/login/logind-session.c
src/login/logind-user.c
src/machine/machine.c
src/network/networkd-link.c
src/network/networkd-manager.c
src/shared/ask-password-api.c

index 2fde3e107e0a81345cb39350290376554ec52eb3..2dcc9c55751acd4d81b8dd8b42247fc2599f87bc 100644 (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);
index 2216853777d164646dc32c6f01c899556fc7cce1..4a9105f421d3449ed120729d95621f55c6562cb4 100644 (file)
 #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) {
index 091ea375d32a77c43b241c463c031045cfbe429f..359fde999874d0288a70d3c689f3a2c2210b1784 100644 (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;
 }
 
index d8b39bdf5fe897a72879db421a936fef9fee37c4..5722e3c2bbf37798898d05a0be821197aa114bc9 100644 (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);
 }
index a1f37bbbb3058d9e03ad77d5c71c3a11688a412b..ba107d461568d1f48f41ca9603f6a08f901476c7 100644 (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;
                         }
 
index 22dfd5496da0462cb3f0bb6edff386a5f0f4e2e1..9fc576fb457c29755be3517762f7807a48c1f0eb 100644 (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)
index 5d23639ee81b2eb2b3402fdd7e92f614eb7ffcd3..172fd80a12ad17680c125e353c6d7d1c7c0b64ae 100644 (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) {
index 0801e135990c10c2fb51189043448f01c79c520f..33b05393159910a48dc85dc1545af80765b2e2bc 100644 (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;
 }
 
index db2f5819722c9791d0bffce1a470f24da0aa9133..69e2d41863748c521742110a6f7ae97f5d940c97 100644 (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:
-        if (temp_path)
-                unlink(temp_path);
+        return 0;
 
-        if (r < 0)
-                log_error_errno(r, "Failed to save stream data %s: %m", s->state_file);
+fail:
+        (void) unlink(s->state_file);
+
+        if (temp_path)
+                (void) unlink(temp_path);
 
-        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) {
index 54417b3af38d04638242d2669d624531f898634d..febf9f87f308875eba9f5e6c7753805d3e4081e4 100644 (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;
-
-        fflush(f);
+        r = fflush_and_check(f);
+        if (r < 0)
+                goto fail;
 
-        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;
+
+fail:
+        if (temp_path)
+                (void) unlink(temp_path);
 
-        return r;
+        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) {
index 6a2c05185dbf277c2517c2b0d8ac61c3d4febb7d..034163eb9ec22cdeb20a72169341ec3069f05f9e 100644 (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;
+
+ fail:
+        if (temp_path)
+                (void) unlink(temp_path);
 
-        return r;
+        return log_error_errno(r, "Failed to save lldp data %s: %m", lldp_file);
 }
 
 int sd_lldp_start(sd_lldp *lldp) {
index 9db86adb7f4320ca8d62ad4c6aa9060af5f68f05..a6b05eb88d31248844d82353e95fae8b68fe2fed 100644 (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);
 }
index e2f4550c7ec942e6879df7c664068b73885244a3..c2233d0cf33ca62a60bd47fd63239c53b0acae95 100644 (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);
 }
index 132b37526eb12ba93f7d0c1e6a7bc1f2abb71861..e4cbd793ed2ea976f1d2db89ea3951e370309077 100644 (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;
index 2e604332467d048e8c182ce6f1900b37f433c7e2..0ec9667744ce0e2f021dec691dbf31ba34dc8c29 100644 (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) {
index 88756542fdb1d90b76f84c5f2e01d2019a4926b4..e8a8f17d864ce30c87b853c8cf6040f3026d544a 100644 (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) {
index e6371ff04d9a323e4af123047097c02e94102be0..397952e7e5e0680c38de09edbed7570e52cbd69d 100644 (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(
index 855c85402c17d5ee14a9feb0b441eeb73e18b352..a261e6a71949b5cbee7034895c760d9f9aa71221 100644 (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) {
index 495ec50be06d21e1da86505f5c394b01c78b53af..8d13a63688180d7b029cab930e272259a4e9e64d 100644 (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) {
index 45f4c09d3dc2ee6b069e871c4eb55a03b18e1c4b..2537d0284577383b157ebdbe51b3642457414941 100644 (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,
index 21d7268120a9b115ce621951d8447cc86370a3c1..5d8a7571cd2ccb4f31d587879db471cf2c6ea91d 100644 (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) {
index 05fc4f849f86ae7b44dd60b0f2e47baf683c3b69..ab26803683ec9b2dc433ff9ea4aafebf52cdb191 100644 (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:
-        if (temp_path)
-                unlink(temp_path);
+        return 0;
 
-        if (r < 0)
-                log_error_errno(r, "Failed to save machine data %s: %m", m->state_file);
+fail:
+        (void) unlink(m->state_file);
 
-        return r;
+        if (temp_path)
+                (void) unlink(temp_path);
+
+        return log_error_errno(r, "Failed to save machine data %s: %m", m->state_file);
 }
 
 static void machine_unlink(Machine *m) {
index f20f68b482fb2cc5cf88556853621feca3898e22..9d570e0146b3b75cf7a7f85e5eb1951a94a708d6 100644 (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);
-
         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] = {
index a5c2351cf9299092a68799f1c94a11be18f6bd78..e718c4840a18914a79b9339bdd321bf578b6401f 100644 (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) {
index 3941605ceceda64730943f5dcee30f49af84ef32..ca4c24ebde6f844f1095efcda5ccf33faa4faa18 100644 (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;
         }