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