chiark / gitweb /
Use initalization instead of explicit zeroing
[elogind.git] / src / shared / util.h
index d1cdd901a04fc2a31603351f788eb09dec7585a0..4c4aed583e9afafc159b7da9a4c40eeadb9d1d59 100644 (file)
@@ -36,6 +36,7 @@
 #include <dirent.h>
 #include <sys/resource.h>
 #include <stddef.h>
+#include <unistd.h>
 #include <systemd/sd-journal.h>
 
 #include "macro.h"
@@ -615,3 +616,31 @@ char *strextend(char **x, ...);
 char *strrep(const char *s, unsigned n);
 
 void* greedy_realloc(void **p, size_t *allocated, size_t need);
+
+static inline void _reset_errno_(int *saved_errno) {
+        errno = *saved_errno;
+}
+
+#define PROTECT_ERRNO __attribute__((cleanup(_reset_errno_))) int _saved_errno_ = errno
+
+struct umask_struct {
+        mode_t mask;
+        bool quit;
+};
+
+static inline void _reset_umask_(struct umask_struct *s) {
+        umask(s->mask);
+};
+
+#define RUN_WITH_UMASK(mask)                                            \
+        for (__attribute__((cleanup(_reset_umask_))) struct umask_struct _saved_umask_ = { umask(mask), false }; \
+             !_saved_umask_.quit ;                                      \
+             _saved_umask_.quit = true)
+
+static inline unsigned u64log2(uint64_t n) {
+        return (n > 1) ? __builtin_clzll(n) ^ 63U : 0;
+}
+
+static inline bool logind_running(void) {
+        return access("/run/systemd/seats/", F_OK) >= 0;
+}