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