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