summaryrefslogtreecommitdiffstats
path: root/src/basic/tmpfile-util.c
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/tmpfile-util.c
parentAdd fopen_unlocked() wrapper (diff)
downloadsystemd-41f6e627d7cfdf1ea50d5adbd7e118589dbcf8db.tar.xz
systemd-41f6e627d7cfdf1ea50d5adbd7e118589dbcf8db.zip
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/tmpfile-util.c')
-rw-r--r--src/basic/tmpfile-util.c7
1 files changed, 7 insertions, 0 deletions
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;