]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Make fopen_temporary and fopen_temporary_label unlocked
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 4 Apr 2019 09:02:11 +0000 (11:02 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 12 Apr 2019 09:44:56 +0000 (11:44 +0200)
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.

21 files changed:
coccinelle/fopen-unlocked.cocci
src/basic/cgroup-util.c
src/basic/env-file.c
src/basic/fileio.c
src/basic/mountpoint-util.c
src/basic/process-util.c
src/basic/tmpfile-util.c
src/core/dbus-service.c
src/fstab-generator/fstab-generator.c
src/libsystemd-network/sd-dhcp-lease.c
src/locale/keymap-util.c
src/login/logind-seat.c
src/login/logind-session.c
src/login/logind-user.c
src/machine/machine.c
src/network/networkd-link.c
src/network/networkd-manager.c
src/resolve/resolved-link.c
src/resolve/resolved-resolv-conf.c
src/shared/generator.c
src/shared/mount-util.c

index 93b993dd556ce1efe3dde9941ba4cec79927ff1f..e6f2bc5681fd4172c8a3dbd43d653cdf7e77067e 100644 (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);
index 11b4e3fce1a5777570225ce3bbd6d7d79994ba5a..210089688b0d969c3450fdd78ecb4bd8e487304a 100644 (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>
index a1f1308a54afe519a7352ed6be17ef034e0c0bcb..83767b0a24a5769f4754bf175a09d6d21673c628 100644 (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)
index 4599440b45eb009945ee8909dea0393b7eb7d610..2318f407b871acbc497e43817a9ee9ebf6d76eb3 100644 (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);
index 48494320fdb55123e5e9ffa1ba308d728ec82828..5ac9293167153824722093e9dcb3a38f023e803e 100644 (file)
@@ -2,7 +2,6 @@
 
 #include <errno.h>
 #include <fcntl.h>
-#include <stdio_ext.h>
 #include <sys/mount.h>
 
 #include "alloc-util.h"
index 568f400d9753718c3de810ef63e656d3d3051bfe..3dc3534e1ab21556b2bc81918345ed93c3876ad5 100644 (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>
index bc92d6a6de2bc07d163a9fe76fe93458a3e2be35..260443a1d66ef5f85ef9ef6a2cc45468a8faf01a 100644 (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;
 
index 0f563a662581e21a98b6c70903b90266b530caf4..4e6709c42e22a7c05c2aa012dcaa402e5a84db5b 100644 (file)
@@ -1,6 +1,5 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
-#include <stdio_ext.h>
 #include <fcntl.h>
 
 #include "alloc-util.h"
index d35f2f993e98827d6b061431552b24b7e8de1a7f..28ae48d551a87f476a3848b59337c87350fbe700 100644 (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"
index a16314a9d37988005536fdae71680c931f43beca..c089b4278b17851ef203a4dd4dd806e37c26c021 100644 (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,
index b8bd181c16a85d640a21b1d2505a3b8b0f51e6b1..e238e5a124c2df6f5778a5186c397e12a4b440a6 100644 (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"
index b4904c37d5d8b9baf35acc09d4edccf99e99083c..f5ffb68238edacf21798cfd39b2e77f34d3d0650 100644 (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,
index 3d3bc8ab1cfb7bc79285ed853aec69a1ccaafdc3..f1efeb0e017c7b3aa897aa1544b829764e7f756f 100644 (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,
index 045b6f0e17d37cef8917fda70ca03ce169715a14..8356a9089a94dbb539779ac691abb3fc44545162 100644 (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,
index 84454ddd864568703f15ebe0be99d59f08054f41..b916d038d7a1c61c10e789aa84b2fb6be433c4fc 100644 (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,
index 3e334c8d29cccce8cbe558e47292acaeec8e22c1..4846b13a8b8d19c2efead25967b75b6b2e0aafca 100644 (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,
index 9075b0a14bd400546e76c10a3538510e1008a6d5..677f66a478be2af779b89f4afc9ce105bf6ad4a9 100644 (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,
index 50f9309f10572bffa3eb7559decff5e3630c12e3..f65ce64d17887917f109c412fbe32f7c66aad227 100644 (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);
index 0435791ea0e17b1f0d9f131c656e55ec62f97272..dfc9a948e34d19b2aeaf4e8a7b7cc8733a582d7c 100644 (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);
index ed7f037e918f30fcf1dc157abb2f16903f6caa7c..403c2a6737be8f1d308c02ef5e4211bb7f6a84e1 100644 (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);
index 1b50716731e962024118521ab361a2c035435820..680c4522ad611de607781c9d4217025a79f83f93 100644 (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>