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