summaryrefslogtreecommitdiff
path: root/src/shared/musl_missing.h
blob: 15f5f8473d9cd580d51f804aaa664080827d9398 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#pragma once
#ifndef ELOGIND_BASIC_MUSL_MISSING_H_INCLUDED
#define ELOGIND_BASIC_MUSL_MISSING_H_INCLUDED


/****************************************************************
 * musl_missing.h - work around glibc extensions for musl libc.
 *
 * Implements glibc functions missing in musl libc as macros.
 * Is to be included where these functions are used.
 * Also defines some glibc only constants as either 0 or
 * as found in the corresponding glibc header file.
 *
 * Juergen Buchmueller <pullmoll@t-online.de> for Void Linux
 * Public Domain; no warranties whatsoever. Thank you Mr. P.
 *
 ****************************************************************/


void elogind_set_program_name(const char* pcall);

#if !defined(__GLIBC__)
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h> /* for pthread_atfork */

#define strerror_r(e, m, k) (strerror_r(e, m, k) < 0 ? strdup("strerror_r() failed") : m);

#ifndef _ERRNO_H
extern char *program_invocation_name;
extern char *program_invocation_short_name;
#endif // errno.h included beforehand

/*
 * Possibly TODO according to http://man7.org/linux/man-pages/man3/getenv.3.html
 * + test if the process's effective user ID does not match its real user ID or
 *   the process's effective group ID does not match its real group ID;
 *   typically this is the result of executing a set-user-ID or set-
 *   group-ID program. Is calling issetugid() sufficient here?
 * + test if the effective capability bit was set on the executable file
 * + test if the process has a nonempty permitted capability set
 */
#if ! HAVE_SECURE_GETENV && ! HAVE___SECURE_GETENV
#  define secure_getenv(name) \
        (issetugid() ? NULL : getenv(name))
#  undef HAVE_SECURE_GETENV
#  define HAVE_SECURE_GETENV 1
#endif // HAVE_[__]SECURE_GETENV

/* Poor man's basename */
#define basename(path) \
        (strrchr(path, '/') ? strrchr(path, '/')+1 : path)

/* strndupa may already be defined in another compatibility header */
#if !defined(strndupa)
#define strndupa(x_src, x_n) \
        (__extension__ ( {   \
                const char* x_in  = (x_src);                  \
                size_t      x_len = strnlen(x_in, (x_n)) + 1; \
                char*       x_out = (char *) alloca(x_len);   \
                x_out[x_len-1] = '\0';                        \
                (char *) memcpy(x_out, x_in, x_len-1);        \
        } ) )
#endif

/* See http://man7.org/linux/man-pages/man3/canonicalize_file_name.3.html */
#define canonicalize_file_name(path) \
        realpath(path, NULL)

/* GLOB_BRACE is another glibc extension - ignore it for musl libc */
#define GLOB_BRACE 0

/* getnameinfo(3) glibc extensions are undefined in musl libc */
#define NI_IDN 0
#define NI_IDN_USE_STD3_ASCII_RULES 0

/* Taken from glibc's net/if_arp.h */
#if !defined(ARPHRD_IEEE802154_PHY)
#define ARPHRD_IEEE802154_PHY 805        /* IEEE 802.15.4 PHY header.  */
#endif

/* Shorthand for type of comparison functions. */
#ifndef __COMPAR_FN_T
# define __COMPAR_FN_T
typedef int (*__compar_fn_t) (const void *, const void *);
typedef __compar_fn_t comparison_fn_t;
#endif

/* Make musl utmp/wtmp stubs visible if needed. */
#if ENABLE_UTMP
# include <paths.h>
# include <utmp.h>
# include <utmpx.h>
# if defined(_PATH_UTMP) && !defined(_PATH_UTMPX)
#   define _PATH_UTMPX _PATH_UTMP
# endif
# if defined(_PATH_WTMP) && !defined(_PATH_WTMPX)
#   define _PATH_WTMPX _PATH_WTMP
# endif
#endif // ENABLE_UTMP

/*
 * Systemd makes use of undeclared glibc-specific __register_atfork to avoid
 * a depednency on libpthread, __register_atfork is roughly equivalent to
 * pthread_atfork so define __register_atfork to pthread_atfork.
 */
#define __register_atfork(prepare,parent,child,dso) pthread_atfork(prepare,parent,child)

/* 
 * Missing FTW macros in musl, define them if not defined
 * taken from
 * https://git.yoctoproject.org/cgit.cgi/poky/plain/meta/recipes-core/systemd/systemd/0028-add-missing-FTW_-macros-for-musl.patch
 */
#ifndef FTW_ACTIONRETVAL
#define FTW_ACTIONRETVAL 16
#endif

#ifndef FTW_CONTINUE
#define FTW_CONTINUE 0
#endif

#ifndef FTW_STOP
#define FTW_STOP 1
#endif

#ifndef FTW_SKIP_SUBTREE
#define FTW_SKIP_SUBTREE 2
#endif

#endif // !defined(__GLIBC__)

#endif // ELOGIND_BASIC_MUSL_MISSING_H_INCLUDED