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