Blame shared/systemd/src/basic/umask-util.h

Packit Service 87a54e
/* SPDX-License-Identifier: LGPL-2.1-or-later */
Packit 5756e2
#pragma once
Packit 5756e2
Packit 5756e2
#include <stdbool.h>
Packit 5756e2
#include <sys/stat.h>
Packit 5756e2
#include <sys/types.h>
Packit 5756e2
Packit 5756e2
#include "macro.h"
Packit 5756e2
Packit 5756e2
static inline void umaskp(mode_t *u) {
Packit 5756e2
        umask(*u & 0777);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
#define _cleanup_umask_ _cleanup_(umaskp)
Packit 5756e2
Packit 5756e2
/* We make use of the fact here that the umask() concept is using only the lower 9 bits of mode_t, although
Packit 5756e2
 * mode_t has space for the file type in the bits further up. We simply OR in the file type mask S_IFMT to
Packit 5756e2
 * distinguish the first and the second iteration of the RUN_WITH_UMASK() loop, so that we can run the first
Packit 5756e2
 * one, and exit on the second. */
Packit 5756e2
Packit 5756e2
assert_cc((S_IFMT & 0777) == 0);
Packit 5756e2
Packit 5756e2
#define RUN_WITH_UMASK(mask)                                            \
Packit 5756e2
        for (_cleanup_umask_ mode_t _saved_umask_ = umask(mask) | S_IFMT; \
Packit 5756e2
             FLAGS_SET(_saved_umask_, S_IFMT);                          \
Packit 5756e2
             _saved_umask_ &= 0777)