From dacd6cee76a08331b8c8616c5f30f70ee49aa2f9 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 29 Jul 2015 20:31:07 +0200 Subject: 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. --- src/login/logind-dbus.c | 19 ++++++++++++------- src/login/logind-inhibit.c | 25 +++++++++++++++---------- src/login/logind-seat.c | 25 +++++++++++++++---------- src/login/logind-session.c | 34 ++++++++++++++++++++-------------- src/login/logind-user.c | 25 +++++++++++++++---------- 5 files changed, 77 insertions(+), 51 deletions(-) (limited to 'src/login') diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index e6371ff04d..397952e7e5 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -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( diff --git a/src/login/logind-inhibit.c b/src/login/logind-inhibit.c index 855c85402c..a261e6a719 100644 --- a/src/login/logind-inhibit.c +++ b/src/login/logind-inhibit.c @@ -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) { diff --git a/src/login/logind-seat.c b/src/login/logind-seat.c index 495ec50be0..8d13a63688 100644 --- a/src/login/logind-seat.c +++ b/src/login/logind-seat.c @@ -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) { diff --git a/src/login/logind-session.c b/src/login/logind-session.c index 45f4c09d3d..2537d02845 100644 --- a/src/login/logind-session.c +++ b/src/login/logind-session.c @@ -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, diff --git a/src/login/logind-user.c b/src/login/logind-user.c index 21d7268120..5d8a7571cd 100644 --- a/src/login/logind-user.c +++ b/src/login/logind-user.c @@ -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) { -- cgit v1.2.3-54-g00ecf