diff options
Diffstat (limited to 'src/utils/include/c.h')
-rw-r--r-- | src/utils/include/c.h | 54 |
1 files changed, 53 insertions, 1 deletions
diff --git a/src/utils/include/c.h b/src/utils/include/c.h index ae08131..354b59e 100644 --- a/src/utils/include/c.h +++ b/src/utils/include/c.h @@ -16,6 +16,8 @@ #include <stdlib.h> #include <string.h> #include <errno.h> +#include <sys/types.h> +#include <grp.h> #include <assert.h> @@ -64,6 +66,21 @@ # define ignore_result(x) ((void) (x)) #endif /* !__GNUC__ */ + +/* "restrict" keyword fallback */ +#if __STDC__ != 1 +# define restrict __restrict /* use implementation __ format */ +#else +# ifndef __STDC_VERSION__ +# define restrict __restrict /* use implementation __ format */ +# else +# if __STDC_VERSION__ < 199901L +# define restrict __restrict /* use implementation __ format */ +# endif +# endif +#endif + + /* * It evaluates to 1 if the attribute/feature is supported by the current * compilation target. Fallback for old compilers. @@ -202,7 +219,7 @@ prog_inv_sh_nm_from_file(char *f, char stripext) #ifndef HAVE_ERR_H -static inline void +static inline void __attribute__ ((__format__ (__printf__, 4, 5))) errmsg(char doexit, int excode, char adderr, const char *fmt, ...) { fprintf(stderr, "%s: ", program_invocation_short_name); @@ -320,6 +337,24 @@ static inline size_t get_hostname_max(void) return 64; } + +static inline int drop_permissions(void) +{ + errno = 0; + + /* drop GID */ + if (setgid(getgid()) < 0) + goto fail; + + /* drop UID */ + if (setuid(getuid()) < 0) + goto fail; + + return 0; +fail: + return errno ? -errno : -1; +} + /* * The usleep function was marked obsolete in POSIX.1-2001 and was removed * in POSIX.1-2008. It was replaced with nanosleep() that provides more @@ -396,6 +431,23 @@ static inline int xusleep(useconds_t usec) #define stringify_value(s) stringify(s) #define stringify(s) #s +/* Detect if we're compiled with Address Sanitizer + * - gcc (__SANITIZE_ADDRESS__) + * - clang (__has_feature(address_sanitizer)) + */ +#if !defined(HAS_FEATURE_ADDRESS_SANITIZER) +# ifdef __SANITIZE_ADDRESS__ +# define HAS_FEATURE_ADDRESS_SANITIZER 1 +# elif defined(__has_feature) +# if __has_feature(address_sanitizer) +# define HAS_FEATURE_ADDRESS_SANITIZER 1 +# endif +# endif +# if !defined(HAS_FEATURE_ADDRESS_SANITIZER) +# define HAS_FEATURE_ADDRESS_SANITIZER 0 +# endif +#endif + /* * UL_ASAN_BLACKLIST is a macro to tell AddressSanitizer (a compile-time * instrumentation shipped with Clang and GCC) to not instrument the |