Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser_port.h revision 1.32.2.1
      1 /*	$NetBSD: rumpuser_port.h,v 1.32.2.1 2014/08/10 06:52:26 tls 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(__NetBSD__) && __NetBSD_Version__ > 600000000) || \
    221   defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
    222 #define PLATFORM_HAS_ARC4RANDOM_BUF
    223 #endif
    224 
    225 #ifndef __NetBSD_Prereq__
    226 #define __NetBSD_Prereq__(a,b,c) 0
    227 #endif
    228 
    229 #include <sys/socket.h>
    230 
    231 #if !defined(__CMSG_ALIGN)
    232 #ifdef CMSG_ALIGN
    233 #define __CMSG_ALIGN(a) CMSG_ALIGN(a)
    234 #endif
    235 #endif
    236 
    237 #ifndef PF_LOCAL
    238 #define PF_LOCAL PF_UNIX
    239 #endif
    240 #ifndef AF_LOCAL
    241 #define AF_LOCAL AF_UNIX
    242 #endif
    243 
    244 /* pfft, but what are you going to do? */
    245 #ifndef MSG_NOSIGNAL
    246 #define MSG_NOSIGNAL 0
    247 #endif
    248 
    249 #if (defined(__sun__) || defined(__ANDROID__)) && !defined(RUMP_REGISTER_T)
    250 #define RUMP_REGISTER_T long
    251 typedef RUMP_REGISTER_T register_t;
    252 #endif
    253 
    254 #include <sys/time.h>
    255 
    256 #ifndef TIMEVAL_TO_TIMESPEC
    257 #define TIMEVAL_TO_TIMESPEC(tv, ts)		\
    258 do {						\
    259 	(ts)->tv_sec  = (tv)->tv_sec;		\
    260 	(ts)->tv_nsec = (tv)->tv_usec * 1000;	\
    261 } while (/*CONSTCOND*/0)
    262 #endif
    263 
    264 #ifndef PLATFORM_HAS_SETGETPROGNAME
    265 #define setprogname(a)
    266 #endif
    267 
    268 #endif /* _LIB_LIBRUMPUSER_RUMPUSER_PORT_H_ */
    269