Skip to content

Commit 41f6e62

Browse files
committed
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.
1 parent fdeea3f commit 41f6e62

File tree

21 files changed

+31
-45
lines changed

21 files changed

+31
-45
lines changed

coccinelle/fopen-unlocked.cocci

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,17 @@ expression f, path, options;
3535
+ return -ESRCH;
3636
+ if (r < 0)
3737
+ return r;
38+
@@
39+
expression f, path, p;
40+
@@
41+
r = fopen_temporary(path, &f, &p);
42+
if (r < 0)
43+
return ...;
44+
- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
45+
@@
46+
expression f, g, path, p;
47+
@@
48+
r = fopen_temporary_label(path, g, &f, &p);
49+
if (r < 0)
50+
return ...;
51+
- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);

src/basic/cgroup-util.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <limits.h>
77
#include <signal.h>
88
#include <stddef.h>
9-
#include <stdio_ext.h>
109
#include <stdlib.h>
1110
#include <string.h>
1211
#include <sys/stat.h>

src/basic/env-file.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/* SPDX-License-Identifier: LGPL-2.1+ */
22

3-
#include <stdio_ext.h>
4-
53
#include "alloc-util.h"
64
#include "env-file.h"
75
#include "env-util.h"
@@ -545,7 +543,6 @@ int write_env_file(const char *fname, char **l) {
545543
if (r < 0)
546544
return r;
547545

548-
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
549546
(void) fchmod_umask(fileno(f), 0644);
550547

551548
STRV_FOREACH(i, l)

src/basic/fileio.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ static int write_string_file_atomic(
108108
if (r < 0)
109109
return r;
110110

111-
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
112111
(void) fchmod_umask(fileno(f), 0644);
113112

114113
r = write_string_stream_ts(f, line, flags, ts);
@@ -154,11 +153,9 @@ int write_string_file_ts(
154153
assert(!ts);
155154

156155
if (flags & WRITE_STRING_FILE_CREATE) {
157-
f = fopen(fn, "we");
158-
if (!f) {
159-
r = -errno;
156+
r = fopen_unlocked(fn, "we", &f);
157+
if (r < 0)
160158
goto fail;
161-
}
162159
} else {
163160
int fd;
164161

@@ -176,9 +173,9 @@ int write_string_file_ts(
176173
safe_close(fd);
177174
goto fail;
178175
}
179-
}
180176

181-
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
177+
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
178+
}
182179

183180
if (flags & WRITE_STRING_FILE_DISABLE_BUFFER)
184181
setvbuf(f, NULL, _IONBF, 0);

src/basic/mountpoint-util.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <errno.h>
44
#include <fcntl.h>
5-
#include <stdio_ext.h>
65
#include <sys/mount.h>
76

87
#include "alloc-util.h"

src/basic/process-util.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <signal.h>
99
#include <stdbool.h>
1010
#include <stdio.h>
11-
#include <stdio_ext.h>
1211
#include <stdlib.h>
1312
#include <string.h>
1413
#include <sys/mman.h>

src/basic/tmpfile-util.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* SPDX-License-Identifier: LGPL-2.1+ */
22

3+
#include <stdio.h>
4+
#include <stdio_ext.h>
35
#include <sys/mman.h>
46

57
#include "alloc-util.h"
@@ -37,6 +39,9 @@ int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
3739
return -errno;
3840
}
3941

42+
/* This assumes that returned FILE object is short-lived and used within the same single-threaded
43+
* context and never shared externally, hence locking is not necessary. */
44+
4045
f = fdopen(fd, "w");
4146
if (!f) {
4247
unlink_noerrno(t);
@@ -45,6 +50,8 @@ int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
4550
return -errno;
4651
}
4752

53+
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
54+
4855
*_f = f;
4956
*_temp_path = t;
5057

src/core/dbus-service.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* SPDX-License-Identifier: LGPL-2.1+ */
22

3-
#include <stdio_ext.h>
43
#include <fcntl.h>
54

65
#include "alloc-util.h"

src/fstab-generator/fstab-generator.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <stdio.h>
66
#include <string.h>
77
#include <unistd.h>
8-
#include <stdio_ext.h>
98

109
#include "alloc-util.h"
1110
#include "fd-util.h"

src/libsystemd-network/sd-dhcp-lease.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <arpa/inet.h>
77
#include <errno.h>
88
#include <stdio.h>
9-
#include <stdio_ext.h>
109
#include <stdlib.h>
1110
#include <string.h>
1211
#include <sys/stat.h>
@@ -832,7 +831,6 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
832831
if (r < 0)
833832
goto fail;
834833

835-
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
836834
(void) fchmod(fileno(f), 0644);
837835

838836
fprintf(f,

0 commit comments

Comments
 (0)