affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering#pragma once
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering/***
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering This file is part of systemd.
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering Copyright 2010 Lennart Poettering
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering systemd is free software; you can redistribute it and/or modify it
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering under the terms of the GNU Lesser General Public License as published by
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering (at your option) any later version.
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering systemd is distributed in the hope that it will be useful, but
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering Lesser General Public License for more details.
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering You should have received a copy of the GNU Lesser General Public License
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering***/
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering#include <stdbool.h>
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering#include <sys/stat.h>
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering#include <sys/types.h>
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering#include "macro.h"
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poetteringstatic inline void umaskp(mode_t *u) {
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering umask(*u);
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering}
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering#define _cleanup_umask_ _cleanup_(umaskp)
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poetteringstruct _umask_struct_ {
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering mode_t mask;
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering bool quit;
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering};
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poetteringstatic inline void _reset_umask_(struct _umask_struct_ *s) {
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering umask(s->mask);
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering};
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering#define RUN_WITH_UMASK(mask) \
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering for (_cleanup_(_reset_umask_) struct _umask_struct_ _saved_umask_ = { umask(mask), false }; \
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering !_saved_umask_.quit ; \
affb60b1ef0cb48388d23360c6f3b043c50f6b8cLennart Poettering _saved_umask_.quit = true)