summaryrefslogtreecommitdiff
path: root/src/systemd/src/basic/fileio.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/systemd/src/basic/fileio.c')
-rw-r--r--src/systemd/src/basic/fileio.c25
1 files changed, 21 insertions, 4 deletions
diff --git a/src/systemd/src/basic/fileio.c b/src/systemd/src/basic/fileio.c
index 3ab50bca2f..71c404bdd0 100644
--- a/src/systemd/src/basic/fileio.c
+++ b/src/systemd/src/basic/fileio.c
@@ -23,6 +23,7 @@
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
+#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
@@ -64,9 +65,15 @@ int write_string_stream_ts(
assert(f);
assert(line);
- fputs(line, f);
+ if (ferror(f))
+ return -EIO;
+
+ if (fputs(line, f) == EOF)
+ return -errno;
+
if (!(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n"))
- fputc('\n', f);
+ if (fputc('\n', f) == EOF)
+ return -errno;
if (ts) {
struct timespec twice[2] = {*ts, *ts};
@@ -98,6 +105,7 @@ static int write_string_file_atomic(
if (r < 0)
return r;
+ (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod_umask(fileno(f), 0644);
r = write_string_stream_ts(f, line, flags, ts);
@@ -167,6 +175,8 @@ int write_string_file_ts(
}
}
+ (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+
if (flags & WRITE_STRING_FILE_DISABLE_BUFFER)
setvbuf(f, NULL, _IONBF, 0);
@@ -203,6 +213,8 @@ int read_one_line_file(const char *fn, char **line) {
if (!f)
return -errno;
+ (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+
r = read_line(f, LONG_LINE_MAX, line);
return r < 0 ? r : 0;
}
@@ -228,6 +240,8 @@ int verify_file(const char *fn, const char *blob, bool accept_extra_nl) {
if (!f)
return -errno;
+ (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+
/* We try to read one byte more than we need, so that we know whether we hit eof */
errno = 0;
k = fread(buf, 1, l + accept_extra_nl + 1, f);
@@ -323,6 +337,8 @@ int read_full_file(const char *fn, char **contents, size_t *size) {
if (!f)
return -errno;
+ (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+
return read_full_stream(f, contents, size);
}
@@ -879,7 +895,8 @@ int write_env_file(const char *fname, char **l) {
if (r < 0)
return r;
- fchmod_umask(fileno(f), 0644);
+ (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+ (void) fchmod_umask(fileno(f), 0644);
STRV_FOREACH(i, l)
write_env_var(f, *i);
@@ -1461,7 +1478,7 @@ int link_tmpfile(int fd, const char *path, const char *target) {
if (rename_noreplace(AT_FDCWD, path, AT_FDCWD, target) < 0)
return -errno;
} else {
- char proc_fd_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(fd) + 1];
+ char proc_fd_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(fd) + 1];
xsprintf(proc_fd_path, "/proc/self/fd/%i", fd);