1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <stdbool.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 
8 #include "macro.h"
9 
umaskp(mode_t * u)10 static inline void umaskp(mode_t *u) {
11         umask(*u & 0777);
12 }
13 
14 #define _cleanup_umask_ _cleanup_(umaskp)
15 
16 /* We make use of the fact here that the umask() concept is using only the lower 9 bits of mode_t, although
17  * mode_t has space for the file type in the bits further up. We simply OR in the file type mask S_IFMT to
18  * distinguish the first and the second iteration of the RUN_WITH_UMASK() loop, so that we can run the first
19  * one, and exit on the second. */
20 
21 assert_cc((S_IFMT & 0777) == 0);
22 
23 #define RUN_WITH_UMASK(mask)                                            \
24         for (_cleanup_umask_ mode_t _saved_umask_ = umask(mask) | S_IFMT; \
25              FLAGS_SET(_saved_umask_, S_IFMT);                          \
26              _saved_umask_ &= 0777)
27 
28 #define BLOCK_WITH_UMASK(mask) \
29         _unused_ _cleanup_umask_ mode_t _saved_umask_ = umask(mask);
30