}
static int inhibitor_save(Inhibitor *i) {
- _cleanup_(unlink_and_freep) char *temp_path = NULL;
- _cleanup_fclose_ FILE *f = NULL;
int r;
assert(i);
r = mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0, MKDIR_WARN_MODE);
if (r < 0)
- goto fail;
+ return log_error_errno(r, "Failed to create /run/systemd/inhibit/: %m");
- r = fopen_temporary(i->state_file, &f, &temp_path);
+ _cleanup_(unlink_and_freep) char *temp_path = NULL;
+ _cleanup_fclose_ FILE *f = NULL;
+ r = fopen_tmpfile_linkable(i->state_file, O_WRONLY|O_CLOEXEC, &temp_path, &f);
if (r < 0)
- goto fail;
+ return log_error_errno(r, "Failed to create state file '%s': %m", i->state_file);
- (void) fchmod(fileno(f), 0644);
+ if (fchmod(fileno(f), 0644) < 0)
+ return log_error_errno(errno, "Failed to set access mode for state file '%s' to 0644: %m", i->state_file);
fprintf(f,
"# This is private data. Do not parse.\n"
_cleanup_free_ char *cc = NULL;
cc = cescape(i->who);
- if (!cc) {
- r = -ENOMEM;
- goto fail;
- }
+ if (!cc)
+ return log_oom();
fprintf(f, "WHO=%s\n", cc);
}
_cleanup_free_ char *cc = NULL;
cc = cescape(i->why);
- if (!cc) {
- r = -ENOMEM;
- goto fail;
- }
+ if (!cc)
+ return log_oom();
fprintf(f, "WHY=%s\n", cc);
}
if (i->fifo_path)
fprintf(f, "FIFO=%s\n", i->fifo_path);
- r = fflush_and_check(f);
+ r = flink_tmpfile(f, temp_path, i->state_file, LINK_TMPFILE_REPLACE);
if (r < 0)
- goto fail;
+ return log_error_errno(r, "Failed to move '%s' into place: %m", i->state_file);
- if (rename(temp_path, i->state_file) < 0) {
- r = -errno;
- goto fail;
- }
-
- temp_path = mfree(temp_path);
+ temp_path = mfree(temp_path); /* disarm auto-destroy: temporary file does not exist anymore */
return 0;
-
-fail:
- (void) unlink(i->state_file);
-
- return log_error_errno(r, "Failed to save inhibit data %s: %m", i->state_file);
}
static int bus_manager_send_inhibited_change(Inhibitor *i) {
}
int seat_save(Seat *s) {
- _cleanup_(unlink_and_freep) char *temp_path = NULL;
- _cleanup_fclose_ FILE *f = NULL;
int r;
assert(s);
r = mkdir_safe_label("/run/systemd/seats", 0755, 0, 0, MKDIR_WARN_MODE);
if (r < 0)
- goto fail;
+ return log_error_errno(r, "Failed to create /run/systemd/seats/: %m");
- r = fopen_temporary(s->state_file, &f, &temp_path);
+ _cleanup_(unlink_and_freep) char *temp_path = NULL;
+ _cleanup_fclose_ FILE *f = NULL;
+ r = fopen_tmpfile_linkable(s->state_file, O_WRONLY|O_CLOEXEC, &temp_path, &f);
if (r < 0)
- goto fail;
+ return log_error_errno(r, "Failed to create state file '%s': %m", s->state_file);
- (void) fchmod(fileno(f), 0644);
+ if (fchmod(fileno(f), 0644) < 0)
+ return log_error_errno(errno, "Failed to set access mode for state file '%s' to 0644: %m", s->state_file);
fprintf(f,
"# This is private data. Do not parse.\n"
i->sessions_by_seat_next ? ' ' : '\n');
}
- r = fflush_and_check(f);
+ r = flink_tmpfile(f, temp_path, s->state_file, LINK_TMPFILE_REPLACE);
if (r < 0)
- goto fail;
+ return log_error_errno(r, "Failed to move '%s' into place: %m", s->state_file);
- if (rename(temp_path, s->state_file) < 0) {
- r = -errno;
- goto fail;
- }
-
- temp_path = mfree(temp_path);
+ temp_path = mfree(temp_path); /* disarm auto-destroy: temporary file does not exist anymore */
return 0;
-
-fail:
- (void) unlink(s->state_file);
- return log_error_errno(r, "Failed to save seat data %s: %m", s->state_file);
}
int seat_load(Seat *s) {
}
int session_save(Session *s) {
- _cleanup_(unlink_and_freep) char *temp_path = NULL;
- _cleanup_fclose_ FILE *f = NULL;
int r;
assert(s);
r = mkdir_safe_label("/run/systemd/sessions", 0755, 0, 0, MKDIR_WARN_MODE);
if (r < 0)
- goto fail;
+ return log_error_errno(r, "Failed to create /run/systemd/sessions/: %m");
- r = fopen_temporary(s->state_file, &f, &temp_path);
+ _cleanup_(unlink_and_freep) char *temp_path = NULL;
+ _cleanup_fclose_ FILE *f = NULL;
+ r = fopen_tmpfile_linkable(s->state_file, O_WRONLY|O_CLOEXEC, &temp_path, &f);
if (r < 0)
- goto fail;
+ return log_error_errno(r, "Failed to create state file '%s': %m", s->state_file);
- (void) fchmod(fileno(f), 0644);
+ if (fchmod(fileno(f), 0644) < 0)
+ return log_error_errno(errno, "Failed to set access mode for state file '%s' to 0644: %m", s->state_file);
fprintf(f,
"# This is private data. Do not parse.\n"
_cleanup_free_ char *escaped = NULL;
escaped = cescape(s->remote_host);
- if (!escaped) {
- r = -ENOMEM;
- goto fail;
- }
+ if (!escaped)
+ return log_oom();
fprintf(f, "REMOTE_HOST=%s\n", escaped);
}
_cleanup_free_ char *escaped = NULL;
escaped = cescape(s->remote_user);
- if (!escaped) {
- r = -ENOMEM;
- goto fail;
- }
+ if (!escaped)
+ return log_oom();
fprintf(f, "REMOTE_USER=%s\n", escaped);
}
_cleanup_free_ char *escaped = NULL;
escaped = cescape(s->service);
- if (!escaped) {
- r = -ENOMEM;
- goto fail;
- }
+ if (!escaped)
+ return log_oom();
fprintf(f, "SERVICE=%s\n", escaped);
}
_cleanup_free_ char *escaped = NULL;
escaped = cescape(s->desktop);
- if (!escaped) {
- r = -ENOMEM;
- goto fail;
- }
+ if (!escaped)
+ return log_oom();
fprintf(f, "DESKTOP=%s\n", escaped);
}
session_save_devices(s, f);
}
- r = fflush_and_check(f);
+ r = flink_tmpfile(f, temp_path, s->state_file, LINK_TMPFILE_REPLACE);
if (r < 0)
- goto fail;
-
- if (rename(temp_path, s->state_file) < 0) {
- r = -errno;
- goto fail;
- }
+ return log_error_errno(r, "Failed to move '%s' into place: %m", s->state_file);
- temp_path = mfree(temp_path);
+ temp_path = mfree(temp_path); /* disarm auto-destroy: temporary file does not exist anymore */
return 0;
-
-fail:
- (void) unlink(s->state_file);
-
- return log_error_errno(r, "Failed to save session data %s: %m", s->state_file);
}
static int session_load_devices(Session *s, const char *devices) {
}
static int user_save_internal(User *u) {
- _cleanup_(unlink_and_freep) char *temp_path = NULL;
- _cleanup_fclose_ FILE *f = NULL;
int r;
assert(u);
r = mkdir_safe_label("/run/systemd/users", 0755, 0, 0, MKDIR_WARN_MODE);
if (r < 0)
- goto fail;
+ return log_error_errno(r, "Failed to create /run/systemd/users/: %m");
- r = fopen_temporary(u->state_file, &f, &temp_path);
+ _cleanup_(unlink_and_freep) char *temp_path = NULL;
+ _cleanup_fclose_ FILE *f = NULL;
+ r = fopen_tmpfile_linkable(u->state_file, O_WRONLY|O_CLOEXEC, &temp_path, &f);
if (r < 0)
- goto fail;
+ return log_error_errno(r, "Failed to create state file '%s': %m", u->state_file);
- (void) fchmod(fileno(f), 0644);
+ if (fchmod(fileno(f), 0644) < 0)
+ return log_error_errno(errno, "Failed to set access mode for state file '%s' to 0644: %m", u->state_file);
fprintf(f,
"# This is private data. Do not parse.\n"
fputc('\n', f);
}
- r = fflush_and_check(f);
+ r = flink_tmpfile(f, temp_path, u->state_file, LINK_TMPFILE_REPLACE);
if (r < 0)
- goto fail;
+ return log_error_errno(r, "Failed to move '%s' into place: %m", u->state_file);
- if (rename(temp_path, u->state_file) < 0) {
- r = -errno;
- goto fail;
- }
-
- temp_path = mfree(temp_path);
+ temp_path = mfree(temp_path); /* disarm auto-destroy: temporary file does not exist anymore */
return 0;
-
-fail:
- (void) unlink(u->state_file);
-
- return log_error_errno(r, "Failed to save user data %s: %m", u->state_file);
}
int user_save(User *u) {