chiark / gitweb /
shared/musl_missing.h: replace ifdef with if on HAVE_[__]SECURE_GETENV
[elogind.git] / src / shared / musl_missing.h
1 #pragma once
2 #ifndef ELOGIND_BASIC_MUSL_MISSING_H_INCLUDED
3 #define ELOGIND_BASIC_MUSL_MISSING_H_INCLUDED
4
5
6 /****************************************************************
7  * musl_missing.h - work around glibc extensions for musl libc.
8  *
9  * Implements glibc functions missing in musl libc as macros.
10  * Is to be included where these functions are used.
11  * Also defines some glibc only constants as either 0 or
12  * as found in the corresponding glibc header file.
13  *
14  * Juergen Buchmueller <pullmoll@t-online.de> for Void Linux
15  * Public Domain; no warranties whatsoever. Thank you Mr. P.
16  *
17  ****************************************************************/
18
19
20 void elogind_set_program_name(const char* pcall);
21
22 #if !defined(__GLIBC__)
23 #include "config.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #define strerror_r(e, m, k) (strerror_r(e, m, k) < 0 ? strdup("strerror_r() failed") : m);
29
30 #ifndef _ERRNO_H
31 extern char *program_invocation_name;
32 extern char *program_invocation_short_name;
33 #endif // errno.h included beforehand
34
35 /*
36  * Possibly TODO according to http://man7.org/linux/man-pages/man3/getenv.3.html
37  * + test if the process's effective user ID does not match its real user ID or
38  *   the process's effective group ID does not match its real group ID;
39  *   typically this is the result of executing a set-user-ID or set-
40  *   group-ID program. Is calling issetugid() sufficient here?
41  * + test if the effective capability bit was set on the executable file
42  * + test if the process has a nonempty permitted capability set
43  */
44 #if ! HAVE_SECURE_GETENV && ! HAVE___SECURE_GETENV
45 #  define secure_getenv(name) \
46         (issetugid() ? NULL : getenv(name))
47 #  undef HAVE_SECURE_GETENV
48 #  define HAVE_SECURE_GETENV 1
49 #endif // HAVE_[__]SECURE_GETENV
50
51 /* Poor man's basename */
52 #define basename(path) \
53         (strrchr(path, '/') ? strrchr(path, '/')+1 : path)
54
55 /* strndupa may already be defined in another compatibility header */
56 #if !defined(strndupa)
57 #define strndupa(src, n) \
58         (__extension__ ({const char *in = (src);   \
59                 size_t len = strnlen(in, (n)) + 1; \
60                 char *out = (char *) alloca(len);  \
61                 out[len-1] = '\0';                 \
62                 (char *) memcpy(out, in, len-1);}) \
63         )
64 #endif
65
66 /* See http://man7.org/linux/man-pages/man3/canonicalize_file_name.3.html */
67 #define canonicalize_file_name(path) \
68         realpath(path, NULL)
69
70 /* GLOB_BRACE is another glibc extension - ignore it for musl libc */
71 #define GLOB_BRACE 0
72
73 /* getnameinfo(3) glibc extensions are undefined in musl libc */
74 #define NI_IDN 0
75 #define NI_IDN_USE_STD3_ASCII_RULES 0
76
77 /* Taken from glibc's net/if_arp.h */
78 #if !defined(ARPHRD_IEEE802154_PHY)
79 #define ARPHRD_IEEE802154_PHY 805        /* IEEE 802.15.4 PHY header.  */
80 #endif
81
82 /* Shorthand for type of comparison functions. */
83 #ifndef __COMPAR_FN_T
84 # define __COMPAR_FN_T
85 typedef int (*__compar_fn_t) (const void *, const void *);
86 typedef __compar_fn_t comparison_fn_t;
87 #endif
88
89 /* Make musl utmp/wtmp stubs visible if needed. */
90 #if ENABLE_UTMP
91 # include <paths.h>
92 # include <utmp.h>
93 # include <utmpx.h>
94 # if defined(_PATH_UTMP) && !defined(_PATH_UTMPX)
95 #   define _PATH_UTMPX _PATH_UTMP
96 # endif
97 # if defined(_PATH_WTMP) && !defined(_PATH_WTMPX)
98 #   define _PATH_WTMPX _PATH_WTMP
99 # endif
100 #endif // ENABLE_UTMP
101
102 #endif // !defined(__GLIBC__)
103
104 #endif // ELOGIND_BASIC_MUSL_MISSING_H_INCLUDED
105