Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser_port.h revision 1.27
      1 /*	$NetBSD: rumpuser_port.h,v 1.27 2014/01/15 16:53:15 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_STRSUFTOLL
     25 #define PLATFORM_HAS_SETGETPROGNAME
     26 
     27 #if __NetBSD_Prereq__(5,99,48)
     28 #define PLATFORM_HAS_NBQUOTA
     29 #endif
     30 
     31 #if __NetBSD_Prereq__(6,99,16)
     32 #define HAVE_CLOCK_NANOSLEEP
     33 #endif
     34 
     35 /*
     36  * This includes also statvfs1() and fstatvfs1().  They could be
     37  * reasonably easily emulated on other platforms.
     38  */
     39 #define PLATFORM_HAS_NBVFSSTAT
     40 #endif /* __NetBSD__ */
     41 
     42 /* might not be 100% accurate, maybe need to revisit later */
     43 #if defined(__linux__) || defined(__sun__)
     44 #define HAVE_CLOCK_NANOSLEEP
     45 #endif
     46 
     47 #ifdef __linux__
     48 #define _XOPEN_SOURCE 600
     49 #define _BSD_SOURCE
     50 #define _FILE_OFFSET_BITS 64
     51 #define _GNU_SOURCE
     52 #include <features.h>
     53 #endif
     54 
     55 #if defined(__sun__)
     56 #  if defined(RUMPUSER_NO_FILE_OFFSET_BITS)
     57 #    undef _FILE_OFFSET_BITS
     58 #  else
     59 #    define _FILE_OFFSET_BITS 64
     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 #define __printflike(a,b)
    168 #endif
    169 
    170 #ifndef __noinline
    171 #ifdef __GNUC__
    172 #define __noinline __attribute__((__noinline__))
    173 #else
    174 #define __noinline
    175 #endif
    176 #endif
    177 
    178 #ifndef __arraycount
    179 #define __arraycount(_ar_) (sizeof(_ar_)/sizeof(_ar_[0]))
    180 #endif
    181 
    182 #ifndef __UNCONST
    183 #define __UNCONST(_a_) ((void *)(unsigned long)(const void *)(_a_))
    184 #endif
    185 
    186 #ifndef __CONCAT
    187 #define __CONCAT(x,y)	x ## y
    188 #endif
    189 
    190 #ifndef __STRING
    191 #define __STRING(x)	#x
    192 #endif
    193 
    194 #if defined(__linux__) || defined(__sun__) || defined (__CYGWIN__)
    195 #define RUMPUSER_RANDOM() random()
    196 #define RUMPUSER_USE_DEVRANDOM
    197 #else
    198 #define RUMPUSER_RANDOM() arc4random()
    199 #endif
    200 
    201 #ifndef __NetBSD_Prereq__
    202 #define __NetBSD_Prereq__(a,b,c) 0
    203 #endif
    204 
    205 #include <sys/socket.h>
    206 
    207 #if !defined(__CMSG_ALIGN)
    208 #ifdef CMSG_ALIGN
    209 #define __CMSG_ALIGN(a) CMSG_ALIGN(a)
    210 #endif
    211 #endif
    212 
    213 #ifndef PF_LOCAL
    214 #define PF_LOCAL PF_UNIX
    215 #endif
    216 #ifndef AF_LOCAL
    217 #define AF_LOCAL AF_UNIX
    218 #endif
    219 
    220 /* pfft, but what are you going to do? */
    221 #ifndef MSG_NOSIGNAL
    222 #define MSG_NOSIGNAL 0
    223 #endif
    224 
    225 #if defined(__sun__) && !defined(RUMP_REGISTER_T)
    226 #define RUMP_REGISTER_T long
    227 typedef RUMP_REGISTER_T register_t;
    228 #endif
    229 
    230 #include <sys/time.h>
    231 
    232 #ifndef TIMEVAL_TO_TIMESPEC
    233 #define TIMEVAL_TO_TIMESPEC(tv, ts)		\
    234 do {						\
    235 	(ts)->tv_sec  = (tv)->tv_sec;		\
    236 	(ts)->tv_nsec = (tv)->tv_usec * 1000;	\
    237 } while (/*CONSTCOND*/0)
    238 #endif
    239 
    240 #ifndef PLATFORM_HAS_SETGETPROGNAME
    241 #define setprogname(a)
    242 #endif
    243 
    244 #endif /* _LIB_LIBRUMPUSER_RUMPUSER_PORT_H_ */
    245