rumpuser_port.h revision 1.3.2.4 1 1.3.2.3 tls /* $NetBSD: rumpuser_port.h,v 1.3.2.4 2014/08/20 00:02:21 tls Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Portability header for non-NetBSD platforms.
5 1.1 pooka * Quick & dirty.
6 1.1 pooka * Maybe should try to use the infrastructure in tools/compat instead?
7 1.1 pooka */
8 1.1 pooka
9 1.1 pooka #ifndef _LIB_LIBRUMPUSER_RUMPUSER_PORT_H_
10 1.1 pooka #define _LIB_LIBRUMPUSER_RUMPUSER_PORT_H_
11 1.1 pooka
12 1.1 pooka #ifdef __NetBSD__
13 1.1 pooka #include <sys/cdefs.h>
14 1.2 pooka #include <sys/param.h>
15 1.2 pooka
16 1.2 pooka #define PLATFORM_HAS_KQUEUE
17 1.2 pooka #define PLATFORM_HAS_CHFLAGS
18 1.2 pooka #define PLATFORM_HAS_NBMOUNT
19 1.2 pooka #define PLATFORM_HAS_NFSSVC
20 1.2 pooka #define PLATFORM_HAS_FSYNC_RANGE
21 1.2 pooka #define PLATFORM_HAS_NBSYSCTL
22 1.3 pooka #define PLATFORM_HAS_NBFILEHANDLE
23 1.3.2.4 tls #ifndef HAVE_PTHREAD_SETNAME_3
24 1.3.2.4 tls #define HAVE_PTHREAD_SETNAME_3
25 1.3.2.4 tls #endif
26 1.3.2.4 tls
27 1.3.2.4 tls #define PLATFORM_HAS_STRSUFTOLL
28 1.3.2.4 tls #define PLATFORM_HAS_SETGETPROGNAME
29 1.2 pooka
30 1.2 pooka #if __NetBSD_Prereq__(5,99,48)
31 1.2 pooka #define PLATFORM_HAS_NBQUOTA
32 1.2 pooka #endif
33 1.2 pooka
34 1.3.2.3 tls #if __NetBSD_Prereq__(6,99,16)
35 1.3.2.3 tls #define HAVE_CLOCK_NANOSLEEP
36 1.3.2.3 tls #endif
37 1.3.2.3 tls
38 1.2 pooka /*
39 1.2 pooka * This includes also statvfs1() and fstatvfs1(). They could be
40 1.2 pooka * reasonably easily emulated on other platforms.
41 1.2 pooka */
42 1.2 pooka #define PLATFORM_HAS_NBVFSSTAT
43 1.3.2.3 tls #endif /* __NetBSD__ */
44 1.3.2.3 tls
45 1.3.2.4 tls #ifndef MIN
46 1.3.2.4 tls #define MIN(a,b) ((/*CONSTCOND*/(a)<(b))?(a):(b))
47 1.3.2.4 tls #endif
48 1.3.2.4 tls #ifndef MAX
49 1.3.2.4 tls #define MAX(a,b) ((/*CONSTCOND*/(a)>(b))?(a):(b))
50 1.3.2.4 tls #endif
51 1.3.2.4 tls
52 1.3.2.3 tls /* might not be 100% accurate, maybe need to revisit later */
53 1.3.2.4 tls #if (defined(__linux__) && !defined(__ANDROID__)) || defined(__sun__)
54 1.3.2.3 tls #define HAVE_CLOCK_NANOSLEEP
55 1.1 pooka #endif
56 1.1 pooka
57 1.1 pooka #ifdef __linux__
58 1.1 pooka #define _XOPEN_SOURCE 600
59 1.1 pooka #define _BSD_SOURCE
60 1.1 pooka #define _GNU_SOURCE
61 1.3.2.4 tls #endif
62 1.3.2.4 tls
63 1.3.2.4 tls #ifdef __ANDROID__
64 1.3.2.4 tls #include <stdint.h>
65 1.3.2.4 tls typedef uint16_t in_port_t;
66 1.3.2.4 tls #include <sys/select.h>
67 1.3.2.4 tls #define atomic_inc_uint(x) __sync_fetch_and_add(x, 1)
68 1.3.2.4 tls #define atomic_dec_uint(x) __sync_fetch_and_sub(x, 1)
69 1.3.2.4 tls static inline int getsubopt(char **optionp, char * const *tokens, char **valuep);
70 1.3.2.4 tls static inline int
71 1.3.2.4 tls getsubopt(char **optionp, char * const *tokens, char **valuep)
72 1.3.2.4 tls {
73 1.3.2.4 tls
74 1.3.2.4 tls /* TODO make a definition */
75 1.3.2.4 tls return -1;
76 1.3.2.4 tls }
77 1.1 pooka #endif
78 1.1 pooka
79 1.3.2.3 tls #if defined(__sun__)
80 1.3.2.3 tls # if defined(RUMPUSER_NO_FILE_OFFSET_BITS)
81 1.3.2.3 tls # undef _FILE_OFFSET_BITS
82 1.3.2.3 tls # endif
83 1.3.2.3 tls #endif
84 1.3.2.3 tls
85 1.3.2.4 tls #if defined(__APPLE__)
86 1.3.2.4 tls #define __dead __attribute__((noreturn))
87 1.3.2.4 tls #include <sys/cdefs.h>
88 1.3.2.4 tls
89 1.3.2.4 tls #include <libkern/OSAtomic.h>
90 1.3.2.4 tls #define atomic_inc_uint(x) OSAtomicIncrement32((volatile int32_t *)(x))
91 1.3.2.4 tls #define atomic_dec_uint(x) OSAtomicDecrement32((volatile int32_t *)(x))
92 1.3.2.4 tls
93 1.3.2.4 tls #include <sys/time.h>
94 1.3.2.4 tls
95 1.3.2.4 tls #define CLOCK_REALTIME 0
96 1.3.2.4 tls typedef int clockid_t;
97 1.3.2.4 tls
98 1.3.2.4 tls static inline int
99 1.3.2.4 tls clock_gettime(clockid_t clk, struct timespec *ts)
100 1.3.2.4 tls {
101 1.3.2.4 tls struct timeval tv;
102 1.3.2.4 tls
103 1.3.2.4 tls if (gettimeofday(&tv, 0) == 0) {
104 1.3.2.4 tls ts->tv_sec = tv.tv_sec;
105 1.3.2.4 tls ts->tv_nsec = tv.tv_usec * 1000;
106 1.3.2.4 tls }
107 1.3.2.4 tls return -1;
108 1.3.2.4 tls }
109 1.3.2.4 tls
110 1.3.2.4 tls #endif
111 1.3.2.4 tls
112 1.1 pooka #include <sys/types.h>
113 1.1 pooka #include <sys/param.h>
114 1.1 pooka
115 1.3.2.4 tls /* NetBSD is the only(?) platform with getenv_r() */
116 1.3.2.4 tls #if !defined(__NetBSD__)
117 1.2 pooka #include <errno.h>
118 1.2 pooka #include <stdlib.h>
119 1.2 pooka #include <string.h>
120 1.3.2.4 tls #include <inttypes.h>
121 1.2 pooka
122 1.2 pooka /* this is inline simply to make this header self-contained */
123 1.2 pooka static inline int
124 1.2 pooka getenv_r(const char *name, char *buf, size_t buflen)
125 1.2 pooka {
126 1.2 pooka char *tmp;
127 1.2 pooka
128 1.2 pooka if ((tmp = getenv(name)) != NULL) {
129 1.2 pooka if (strlen(tmp) >= buflen) {
130 1.2 pooka errno = ERANGE;
131 1.2 pooka return -1;
132 1.2 pooka }
133 1.2 pooka strcpy(buf, tmp);
134 1.2 pooka return 0;
135 1.2 pooka } else {
136 1.2 pooka errno = ENOENT;
137 1.2 pooka return -1;
138 1.2 pooka }
139 1.2 pooka }
140 1.2 pooka #endif
141 1.2 pooka
142 1.3.2.1 tls #if defined(__sun__)
143 1.3.2.2 tls #include <sys/sysmacros.h>
144 1.3.2.2 tls
145 1.3.2.3 tls #if !defined(HAVE_POSIX_MEMALIGN)
146 1.3.2.2 tls /* Solarisa 10 has memalign() but no posix_memalign() */
147 1.3.2.1 tls #include <stdlib.h>
148 1.3.2.1 tls
149 1.3.2.1 tls static inline int
150 1.3.2.1 tls posix_memalign(void **ptr, size_t align, size_t size)
151 1.3.2.1 tls {
152 1.3.2.1 tls
153 1.3.2.1 tls *ptr = memalign(align, size);
154 1.3.2.1 tls if (*ptr == NULL)
155 1.3.2.1 tls return ENOMEM;
156 1.3.2.1 tls return 0;
157 1.3.2.1 tls }
158 1.3.2.3 tls #endif /* !HAVE_POSIX_MEMALIGN */
159 1.3.2.1 tls #endif /* __sun__ */
160 1.3.2.1 tls
161 1.1 pooka #ifndef __RCSID
162 1.1 pooka #define __RCSID(a)
163 1.1 pooka #endif
164 1.1 pooka
165 1.1 pooka #ifndef INFTIM
166 1.1 pooka #define INFTIM (-1)
167 1.1 pooka #endif
168 1.1 pooka
169 1.1 pooka #ifndef _DIAGASSERT
170 1.1 pooka #define _DIAGASSERT(_p_)
171 1.1 pooka #endif
172 1.1 pooka
173 1.3.2.2 tls #if defined(__linux__) || defined(__sun__) || defined(__CYGWIN__)
174 1.3.2.2 tls #define SIN_SETLEN(a,b)
175 1.1 pooka #else /* BSD */
176 1.3.2.2 tls #define SIN_SETLEN(_sin_, _len_) _sin_.sin_len = _len_
177 1.1 pooka #endif
178 1.1 pooka
179 1.1 pooka #ifndef __predict_true
180 1.1 pooka #define __predict_true(a) a
181 1.1 pooka #define __predict_false(a) a
182 1.1 pooka #endif
183 1.1 pooka
184 1.1 pooka #ifndef __dead
185 1.3.2.2 tls #define __dead __attribute__((__noreturn__))
186 1.1 pooka #endif
187 1.1 pooka
188 1.1 pooka #ifndef __printflike
189 1.3.2.4 tls #ifdef __GNUC__
190 1.3.2.4 tls #define __printflike(a,b) __attribute__((__format__ (__printf__,a,b)))
191 1.3.2.4 tls #else
192 1.1 pooka #define __printflike(a,b)
193 1.1 pooka #endif
194 1.3.2.4 tls #endif
195 1.1 pooka
196 1.2 pooka #ifndef __noinline
197 1.2 pooka #ifdef __GNUC__
198 1.2 pooka #define __noinline __attribute__((__noinline__))
199 1.2 pooka #else
200 1.2 pooka #define __noinline
201 1.2 pooka #endif
202 1.2 pooka #endif
203 1.2 pooka
204 1.1 pooka #ifndef __arraycount
205 1.1 pooka #define __arraycount(_ar_) (sizeof(_ar_)/sizeof(_ar_[0]))
206 1.1 pooka #endif
207 1.1 pooka
208 1.1 pooka #ifndef __UNCONST
209 1.1 pooka #define __UNCONST(_a_) ((void *)(unsigned long)(const void *)(_a_))
210 1.1 pooka #endif
211 1.1 pooka
212 1.3.2.4 tls #ifndef __CONCAT
213 1.3.2.4 tls #define __CONCAT(x,y) x ## y
214 1.3.2.4 tls #endif
215 1.3.2.4 tls
216 1.3.2.4 tls #ifndef __STRING
217 1.3.2.4 tls #define __STRING(x) #x
218 1.3.2.4 tls #endif
219 1.3.2.4 tls
220 1.3.2.4 tls #if (defined(__NetBSD__) && __NetBSD_Version__ > 600000000) || \
221 1.3.2.4 tls defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
222 1.3.2.4 tls #define PLATFORM_HAS_ARC4RANDOM_BUF
223 1.1 pooka #endif
224 1.1 pooka
225 1.2 pooka #ifndef __NetBSD_Prereq__
226 1.2 pooka #define __NetBSD_Prereq__(a,b,c) 0
227 1.2 pooka #endif
228 1.2 pooka
229 1.2 pooka #include <sys/socket.h>
230 1.3.2.1 tls
231 1.3.2.1 tls #if !defined(__CMSG_ALIGN)
232 1.2 pooka #ifdef CMSG_ALIGN
233 1.2 pooka #define __CMSG_ALIGN(a) CMSG_ALIGN(a)
234 1.2 pooka #endif
235 1.2 pooka #endif
236 1.2 pooka
237 1.3.2.1 tls #ifndef PF_LOCAL
238 1.3.2.1 tls #define PF_LOCAL PF_UNIX
239 1.3.2.1 tls #endif
240 1.3.2.1 tls #ifndef AF_LOCAL
241 1.3.2.1 tls #define AF_LOCAL AF_UNIX
242 1.3.2.1 tls #endif
243 1.3.2.1 tls
244 1.3.2.1 tls /* pfft, but what are you going to do? */
245 1.3.2.1 tls #ifndef MSG_NOSIGNAL
246 1.3.2.1 tls #define MSG_NOSIGNAL 0
247 1.3.2.1 tls #endif
248 1.3.2.1 tls
249 1.3.2.4 tls #if (defined(__sun__) || defined(__ANDROID__)) && !defined(RUMP_REGISTER_T)
250 1.3.2.2 tls #define RUMP_REGISTER_T long
251 1.3.2.2 tls typedef RUMP_REGISTER_T register_t;
252 1.3.2.2 tls #endif
253 1.3.2.2 tls
254 1.3.2.4 tls #include <sys/time.h>
255 1.3.2.4 tls
256 1.3.2.4 tls #ifndef TIMEVAL_TO_TIMESPEC
257 1.3.2.4 tls #define TIMEVAL_TO_TIMESPEC(tv, ts) \
258 1.3.2.4 tls do { \
259 1.3.2.4 tls (ts)->tv_sec = (tv)->tv_sec; \
260 1.3.2.4 tls (ts)->tv_nsec = (tv)->tv_usec * 1000; \
261 1.3.2.4 tls } while (/*CONSTCOND*/0)
262 1.3.2.4 tls #endif
263 1.3.2.4 tls
264 1.3.2.4 tls #ifndef PLATFORM_HAS_SETGETPROGNAME
265 1.3.2.4 tls #define setprogname(a)
266 1.3.2.4 tls #endif
267 1.3.2.4 tls
268 1.1 pooka #endif /* _LIB_LIBRUMPUSER_RUMPUSER_PORT_H_ */
269