1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2019-04-04 11:02:11 +02:00
parent fdeea3f4f1
commit 41f6e627d7
21 changed files with 31 additions and 45 deletions

View File

@ -35,3 +35,17 @@ expression f, path, options;
+ return -ESRCH;
+ if (r < 0)
+ return r;
@@
expression f, path, p;
@@
r = fopen_temporary(path, &f, &p);
if (r < 0)
return ...;
- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
@@
expression f, g, path, p;
@@
r = fopen_temporary_label(path, g, &f, &p);
if (r < 0)
return ...;
- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);

View File

@ -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>

View File

@ -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)

View File

@ -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);

View File

@ -2,7 +2,6 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio_ext.h>
#include <sys/mount.h>
#include "alloc-util.h"

View File

@ -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>

View File

@ -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;

View File

@ -1,6 +1,5 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <stdio_ext.h>
#include <fcntl.h>
#include "alloc-util.h"

View File

@ -5,7 +5,6 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdio_ext.h>
#include "alloc-util.h"
#include "fd-util.h"

View File

@ -6,7 +6,6 @@
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
@ -832,7 +831,6 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
if (r < 0)
goto fail;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fprintf(f,

View File

@ -1,7 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <stdio_ext.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
@ -423,7 +422,6 @@ int x11_write_data(Context *c) {
if (r < 0)
return r;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fputs("# Written by systemd-localed(8), read by systemd-localed and Xorg. It's\n"

View File

@ -2,7 +2,6 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio_ext.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
@ -97,7 +96,6 @@ int seat_save(Seat *s) {
if (r < 0)
goto fail;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fprintf(f,

View File

@ -5,7 +5,6 @@
#include <linux/kd.h>
#include <linux/vt.h>
#include <signal.h>
#include <stdio_ext.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
@ -214,7 +213,6 @@ int session_save(Session *s) {
if (r < 0)
goto fail;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fprintf(f,

View File

@ -3,7 +3,6 @@
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdio_ext.h>
#include "alloc-util.h"
#include "bus-common-errors.h"
@ -162,7 +161,6 @@ static int user_save_internal(User *u) {
if (r < 0)
goto fail;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fprintf(f,

View File

@ -3,7 +3,6 @@
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdio_ext.h>
#include <sys/stat.h>
#include "sd-messages.h"
@ -126,7 +125,6 @@ int machine_save(Machine *m) {
if (r < 0)
goto fail;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fprintf(f,

View File

@ -4,7 +4,6 @@
#include <linux/if.h>
#include <linux/can/netlink.h>
#include <unistd.h>
#include <stdio_ext.h>
#include "alloc-util.h"
#include "bus-util.h"
@ -4034,7 +4033,6 @@ int link_save(Link *link) {
if (r < 0)
goto fail;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fprintf(f,

View File

@ -3,7 +3,6 @@
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/fib_rules.h>
#include <stdio_ext.h>
#include <unistd.h>
#include "sd-daemon.h"
@ -1189,7 +1188,6 @@ static int manager_save(Manager *m) {
if (r < 0)
return r;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fprintf(f,

View File

@ -2,7 +2,6 @@
#include <net/if.h>
#include <linux/if.h>
#include <stdio_ext.h>
#include <unistd.h>
#include "sd-network.h"
@ -1178,7 +1177,6 @@ int link_save_user(Link *l) {
if (r < 0)
goto fail;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fputs("# This is private data. Do not parse.\n", f);

View File

@ -1,7 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <resolv.h>
#include <stdio_ext.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
@ -359,14 +358,12 @@ int manager_write_resolv_conf(Manager *m) {
if (r < 0)
return log_warning_errno(r, "Failed to open private resolv.conf file for writing: %m");
(void) __fsetlocking(f_uplink, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f_uplink), 0644);
r = fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF, PRIVATE_STUB_RESOLV_CONF, &f_stub, &temp_path_stub);
if (r < 0)
return log_warning_errno(r, "Failed to open private stub-resolv.conf file for writing: %m");
(void) __fsetlocking(f_stub, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f_stub), 0644);
r = write_uplink_resolv_conf_contents(f_uplink, dns, domains);

View File

@ -1,7 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <stdio_ext.h>
#include <unistd.h>
#include "alloc-util.h"
@ -30,23 +29,22 @@ int generator_open_unit_file(
const char *unit;
FILE *f;
int r;
unit = strjoina(dest, "/", name);
f = fopen(unit, "wxe");
if (!f) {
if (source && errno == EEXIST)
return log_error_errno(errno,
r = fopen_unlocked(unit, "wxe", &f);
if (r < 0) {
if (source && r == -EEXIST)
return log_error_errno(r,
"Failed to create unit file %s, as it already exists. Duplicate entry in %s?",
unit, source);
else
return log_error_errno(errno,
return log_error_errno(r,
"Failed to create unit file %s: %m",
unit);
}
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
fprintf(f,
"# Automatically generated by %s\n\n",
program_invocation_short_name);

View File

@ -1,7 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>