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