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