summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-04-04 11:02:11 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-04-12 11:44:56 +0200
commit41f6e627d7cfdf1ea50d5adbd7e118589dbcf8db (patch)
tree979f9a42211a4abbad46950b6e069d3f109f68de /src/basic
parentfdeea3f4f1c0f78f1014582135d047265098fb82 (diff)
downloadsystemd-41f6e627d7cfdf1ea50d5adbd7e118589dbcf8db.tar.gz
Make fopen_temporary and fopen_temporary_label unlocked
This is partially a refactoring, but also makes many more places use unlocked operations implicitly, i.e. all users of fopen_temporary(). AFAICT, the uses are always for short-lived files which are not shared externally, and are just used within the same context. Locking is not necessary.
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/cgroup-util.c1
-rw-r--r--src/basic/env-file.c3
-rw-r--r--src/basic/fileio.c11
-rw-r--r--src/basic/mountpoint-util.c1
-rw-r--r--src/basic/process-util.c1
-rw-r--r--src/basic/tmpfile-util.c7
6 files changed, 11 insertions, 13 deletions
diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c
index 11b4e3fce1..210089688b 100644
--- a/src/basic/cgroup-util.c
+++ b/src/basic/cgroup-util.c
@@ -6,7 +6,6 @@
#include <limits.h>
#include <signal.h>
#include <stddef.h>
-#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
diff --git a/src/basic/env-file.c b/src/basic/env-file.c
index a1f1308a54..83767b0a24 100644
--- a/src/basic/env-file.c
+++ b/src/basic/env-file.c
@@ -1,7 +1,5 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
-#include <stdio_ext.h>
-
#include "alloc-util.h"
#include "env-file.h"
#include "env-util.h"
@@ -545,7 +543,6 @@ int write_env_file(const char *fname, char **l) {
if (r < 0)
return r;
- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod_umask(fileno(f), 0644);
STRV_FOREACH(i, l)
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index 4599440b45..2318f407b8 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -108,7 +108,6 @@ 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);
@@ -154,11 +153,9 @@ int write_string_file_ts(
assert(!ts);
if (flags & WRITE_STRING_FILE_CREATE) {
- f = fopen(fn, "we");
- if (!f) {
- r = -errno;
+ r = fopen_unlocked(fn, "we", &f);
+ if (r < 0)
goto fail;
- }
} else {
int fd;
@@ -176,9 +173,9 @@ int write_string_file_ts(
safe_close(fd);
goto fail;
}
- }
- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+ (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+ }
if (flags & WRITE_STRING_FILE_DISABLE_BUFFER)
setvbuf(f, NULL, _IONBF, 0);
diff --git a/src/basic/mountpoint-util.c b/src/basic/mountpoint-util.c
index 48494320fd..5ac9293167 100644
--- a/src/basic/mountpoint-util.c
+++ b/src/basic/mountpoint-util.c
@@ -2,7 +2,6 @@
#include <errno.h>
#include <fcntl.h>
-#include <stdio_ext.h>
#include <sys/mount.h>
#include "alloc-util.h"
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
index 568f400d97..3dc3534e1a 100644
--- a/src/basic/process-util.c
+++ b/src/basic/process-util.c
@@ -8,7 +8,6 @@
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
-#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
diff --git a/src/basic/tmpfile-util.c b/src/basic/tmpfile-util.c
index bc92d6a6de..260443a1d6 100644
--- a/src/basic/tmpfile-util.c
+++ b/src/basic/tmpfile-util.c
@@ -1,5 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
+#include <stdio.h>
+#include <stdio_ext.h>
#include <sys/mman.h>
#include "alloc-util.h"
@@ -37,6 +39,9 @@ int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
return -errno;
}
+ /* This assumes that returned FILE object is short-lived and used within the same single-threaded
+ * context and never shared externally, hence locking is not necessary. */
+
f = fdopen(fd, "w");
if (!f) {
unlink_noerrno(t);
@@ -45,6 +50,8 @@ int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
return -errno;
}
+ (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+
*_f = f;
*_temp_path = t;