]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
umask-util: add helper that resets umask until end of current code block
authorLennart Poettering <lennart@poettering.net>
Fri, 12 Nov 2021 10:06:46 +0000 (11:06 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 12 Nov 2021 15:01:40 +0000 (16:01 +0100)
src/basic/umask-util.h
src/nspawn/nspawn.c
src/shared/dev-setup.c
src/test/test-fs-util.c

index bd7c2bdb8c7c1e4b52fc85f4e6042e03a8c5c615..90d18f70ba441d5085aa17b31f34a1f562a55400 100644 (file)
@@ -24,3 +24,6 @@ assert_cc((S_IFMT & 0777) == 0);
         for (_cleanup_umask_ mode_t _saved_umask_ = umask(mask) | S_IFMT; \
              FLAGS_SET(_saved_umask_, S_IFMT);                          \
              _saved_umask_ &= 0777)
+
+#define BLOCK_WITH_UMASK(mask) \
+        _unused_ _cleanup_umask_ mode_t _saved_umask_ = umask(mask);
index 66daeb4f649cd927d13e9993566d85fd5fe327d4..b85b8c3d43cd8511ba34bd0ab75ae52adf5c865b 100644 (file)
@@ -2216,13 +2216,12 @@ static int copy_devnodes(const char *dest) {
                 "tty\0"
                 "net/tun\0";
 
-        _unused_ _cleanup_umask_ mode_t u;
         const char *d;
         int r = 0;
 
         assert(dest);
 
-        u = umask(0000);
+        BLOCK_WITH_UMASK(0000);
 
         /* Create /dev/net, so that we can create /dev/net/tun in it */
         if (userns_mkdir(dest, "/dev/net", 0755, 0, 0) < 0)
@@ -2299,11 +2298,10 @@ static int copy_devnodes(const char *dest) {
 }
 
 static int make_extra_nodes(const char *dest) {
-        _unused_ _cleanup_umask_ mode_t u;
         size_t i;
         int r;
 
-        u = umask(0000);
+        BLOCK_WITH_UMASK(0000);
 
         for (i = 0; i < arg_n_extra_nodes; i++) {
                 _cleanup_free_ char *path = NULL;
@@ -2500,12 +2498,11 @@ static int setup_kmsg(int kmsg_socket) {
         _cleanup_(unlink_and_freep) char *from = NULL;
         _cleanup_free_ char *fifo = NULL;
         _cleanup_close_ int fd = -1;
-        _unused_ _cleanup_umask_ mode_t u;
         int r;
 
         assert(kmsg_socket >= 0);
 
-        u = umask(0000);
+        BLOCK_WITH_UMASK(0000);
 
         /* We create the kmsg FIFO as as temporary file in /run, but immediately delete it after bind mounting it to
          * /proc/kmsg. While FIFOs on the reading side behave very similar to /proc/kmsg, their writing side behaves
index c3e717ae11e63e0238c208b9d3aee6a070ff53f4..0390abbfdc38b0b103c40f9d813ec719dc5c2a43 100644 (file)
@@ -81,13 +81,12 @@ int make_inaccessible_nodes(
                 { "inaccessible/blk",  S_IFBLK  | 0000 },
         };
 
-        _unused_ _cleanup_umask_ mode_t u;
         int r;
 
         if (!parent_dir)
                 parent_dir = "/run/systemd";
 
-        u = umask(0000);
+        BLOCK_WITH_UMASK(0000);
 
         /* Set up inaccessible (and empty) file nodes of all types. This are used to as mount sources for over-mounting
          * ("masking") file nodes that shall become inaccessible and empty for specific containers or services. We try
index 41ddec4783a462ef3a6d9e02a7ca8565497a7530..d8273bc846e651c5096c922499ebb1dde3233ecc 100644 (file)
@@ -765,7 +765,6 @@ static void test_rename_noreplace(void) {
 
 static void test_chmod_and_chown(void) {
         _cleanup_(rm_rf_physical_and_freep) char *d = NULL;
-        _unused_ _cleanup_umask_ mode_t u = umask(0000);
         struct stat st;
         const char *p;
 
@@ -774,6 +773,8 @@ static void test_chmod_and_chown(void) {
 
         log_info("/* %s */", __func__);
 
+        BLOCK_WITH_UMASK(0000);
+
         assert_se(mkdtemp_malloc(NULL, &d) >= 0);
 
         p = strjoina(d, "/reg");