]> jfr.im git - solanum.git/blame - librb/include/rb_lib.h
librb: add rb_{set,clear}_cloexec
[solanum.git] / librb / include / rb_lib.h
CommitLineData
db137867
AC
1#ifndef RB_LIB_H
2#define RB_LIB_H 1
3
4#include <librb-config.h>
5#include <stdio.h>
6#include <limits.h>
7#include <stdarg.h>
8#include <assert.h>
9#include <fcntl.h>
10#include <signal.h>
11#include <ctype.h>
12
13#ifdef __GNUC__
14#undef alloca
15#define alloca __builtin_alloca
16#else
17# ifdef _MSC_VER
18# include <malloc.h>
19# define alloca _alloca
20# else
21# if RB_HAVE_ALLOCA_H
22# include <alloca.h>
23# else
24# ifdef _AIX
25#pragma alloca
26# else
27# ifndef alloca /* predefined by HP cc +Olibcalls */
28char *alloca();
29# endif
30# endif
31# endif
32# endif
33#endif /* __GNUC__ */
34
35#ifdef __GNUC__
36
c2ac22cc
VY
37#ifdef rb_likely
38#undef rb_likely
db137867 39#endif
c2ac22cc
VY
40#ifdef rb_unlikely
41#undef rb_unlikely
db137867
AC
42#endif
43
44#if __GNUC__ == 2 && __GNUC_MINOR__ < 96
45# define __builtin_expect(x, expected_value) (x)
46#endif
47
c2ac22cc
VY
48#define rb_likely(x) __builtin_expect(!!(x), 1)
49#define rb_unlikely(x) __builtin_expect(!!(x), 0)
db137867
AC
50
51#else /* !__GNUC__ */
52
53#define UNUSED(x) x
54
c2ac22cc
VY
55#ifdef rb_likely
56#undef rb_likely
db137867 57#endif
c2ac22cc
VY
58#ifdef rb_unlikely
59#undef rb_unlikely
db137867 60#endif
c2ac22cc
VY
61#define rb_likely(x) (x)
62#define rb_unlikely(x) (x)
db137867
AC
63#endif
64
17e4e6af
AC
65#ifdef _WIN32
66#define rb_get_errno() do { errno = WSAGetLastError(); WSASetLastError(errno); } while(0)
67typedef SOCKET rb_platform_fd_t;
ac2f2189 68#define RB_PATH_SEPARATOR '\\'
17e4e6af
AC
69#else
70#define rb_get_errno()
71typedef int rb_platform_fd_t;
ac2f2189 72#define RB_PATH_SEPARATOR '/'
17e4e6af 73#endif
db137867 74
3202e249 75#ifdef _WIN32
db137867
AC
76#include <process.h>
77
d74fa5b5
JT
78#ifndef PATH_MAX
79#define PATH_MAX 128
db137867
AC
80#endif
81
db137867
AC
82#ifdef strerror
83#undef strerror
84#endif
85
3202e249
VY
86#define strerror(x) rb_strerror(x)
87char *rb_strerror(int error);
db137867 88
38b68802 89#undef ENOBUFS
db137867 90#define ENOBUFS WSAENOBUFS
b68dee9b 91
38b68802 92#undef EINPROGRESS
db137867 93#define EINPROGRESS WSAEINPROGRESS
b68dee9b 94
38b68802 95#undef EWOULDBLOCK
db137867 96#define EWOULDBLOCK WSAEWOULDBLOCK
b68dee9b 97
38b68802 98#undef EMSGSIZE
db137867 99#define EMSGSIZE WSAEMSGSIZE
b68dee9b 100
38b68802 101#undef EALREADY
db137867 102#define EALREADY WSAEALREADY
b68dee9b 103
38b68802 104#undef EISCONN
db137867 105#define EISCONN WSAEISCONN
b68dee9b 106
38b68802 107#undef EADDRINUSE
db137867 108#define EADDRINUSE WSAEADDRINUSE
b68dee9b 109
38b68802 110#undef EAFNOSUPPORT
db137867
AC
111#define EAFNOSUPPORT WSAEAFNOSUPPORT
112
113#define pipe(x) _pipe(x, 1024, O_BINARY)
491b3b39 114#define ioctl(x,y,z) ioctlsocket(x,y, (unsigned long *)z)
db137867 115
db137867 116#define WNOHANG 1
db137867
AC
117
118#ifndef SIGKILL
119#define SIGKILL SIGTERM
120#endif
121
3202e249 122#endif /* _WIN32 */
db137867
AC
123
124
125
126#ifndef HOSTIPLEN
127#define HOSTIPLEN 53
128#endif
129
7a06833f
SA
130
131/* For those unfamiliar with GNU format attributes, a is the 1 based
132 * argument number of the format string, and b is the 1 based argument
133 * number of the variadic ... */
134#ifdef __GNUC__
135#define AFP(a,b) __attribute__((format (printf, a, b)))
136#else
137#define AFP(a,b)
138#endif
139
140
db137867 141#ifdef __GNUC__
b5ad4cdf
SA
142#define slrb_assert(expr) ( \
143 rb_likely((expr)) || ( \
c2ac22cc 144 rb_lib_log( \
db137867 145 "file: %s line: %d (%s): Assertion failed: (%s)", \
b5ad4cdf
SA
146 __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr), 0) \
147 )
db137867 148#else
b5ad4cdf
SA
149#define slrb_assert(expr) ( \
150 rb_likely((expr)) || ( \
29c92cf9 151 rb_lib_log( \
db137867 152 "file: %s line: %d: Assertion failed: (%s)", \
b5ad4cdf
SA
153 __FILE__, __LINE__, #expr), 0) \
154 )
db137867 155#endif
3202e249 156
b5ad4cdf 157/* evaluates to true if assertion fails */
3202e249 158#ifdef SOFT_ASSERT
b5ad4cdf 159#define lrb_assert(expr) (!slrb_assert(expr))
db137867 160#else
b5ad4cdf 161#define lrb_assert(expr) (assert(slrb_assert(expr)), 0)
db137867
AC
162#endif
163
9d9a4f60 164#ifdef RB_SOCKADDR_HAS_SA_LEN
db137867
AC
165#define ss_len sa_len
166#endif
167
3202e249
VY
168#define GET_SS_FAMILY(x) (((const struct sockaddr *)(x))->sa_family)
169#define SET_SS_FAMILY(x, y) ((((struct sockaddr *)(x))->sa_family) = y)
9d9a4f60 170#ifdef RB_SOCKADDR_HAS_SA_LEN
db137867 171#define SET_SS_LEN(x, y) do { \
d86692fa
EM
172 struct sockaddr *_storage; \
173 _storage = ((struct sockaddr *)(x));\
174 _storage->sa_len = (y); \
db137867
AC
175 } while (0)
176#define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_len)
9d9a4f60 177#else /* !RB_SOCKADDR_HAS_SA_LEN */
db137867 178#define SET_SS_LEN(x, y) (((struct sockaddr *)(x))->sa_family = ((struct sockaddr *)(x))->sa_family)
db137867 179#define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
db137867
AC
180#endif
181
d86692fa
EM
182#define GET_SS_PORT(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? ((struct sockaddr_in *)(x))->sin_port : ((struct sockaddr_in6 *)(x))->sin6_port)
183#define SET_SS_PORT(x, y) do { \
184 if(((struct sockaddr *)(x))->sa_family == AF_INET) { \
185 ((struct sockaddr_in *)(x))->sin_port = (y); \
186 } else { \
187 ((struct sockaddr_in6 *)(x))->sin6_port = (y); \
188 } \
189 } while (0)
d86692fa 190
db137867
AC
191#ifndef INADDRSZ
192#define INADDRSZ 4
193#endif
194
195#ifndef IN6ADDRSZ
196#define IN6ADDRSZ 16
197#endif
198
199#ifndef INT16SZ
200#define INT16SZ 2
201#endif
202
41aed6bb
SA
203#ifndef UINT16_MAX
204#define UINT16_MAX (65535U)
205#endif
206
7c7cf006
SA
207#ifndef UINT32_MAX
208#define UINT32_MAX (4294967295U)
209#endif
210
db137867
AC
211
212typedef void log_cb(const char *buffer);
213typedef void restart_cb(const char *buffer);
214typedef void die_cb(const char *buffer);
215
216char *rb_ctime(const time_t, char *, size_t);
217char *rb_date(const time_t, char *, size_t);
218void rb_lib_log(const char *, ...);
92706fd5 219void rb_lib_restart(const char *, ...) __attribute__((noreturn));
db137867
AC
220void rb_lib_die(const char *, ...);
221void rb_set_time(void);
222const char *rb_lib_version(void);
223
224void rb_lib_init(log_cb * xilog, restart_cb * irestart, die_cb * idie, int closeall, int maxfds,
225 size_t dh_size, size_t fd_heap_size);
92706fd5 226void rb_lib_loop(long delay) __attribute__((noreturn));
db137867
AC
227
228time_t rb_current_time(void);
229const struct timeval *rb_current_time_tv(void);
230pid_t rb_spawn_process(const char *, const char **);
231
232char *rb_strtok_r(char *, const char *, char **);
233
234int rb_gettimeofday(struct timeval *, void *);
235
236void rb_sleep(unsigned int seconds, unsigned int useconds);
237char *rb_crypt(const char *, const char *);
238
239unsigned char *rb_base64_encode(const unsigned char *str, int length);
240unsigned char *rb_base64_decode(const unsigned char *str, int length, int *ret);
3202e249
VY
241int rb_kill(pid_t, int);
242char *rb_strerror(int);
db137867 243
3202e249 244int rb_setenv(const char *, const char *, int);
db137867 245
3202e249
VY
246pid_t rb_waitpid(pid_t pid, int *status, int options);
247pid_t rb_getpid(void);
248//unsigned int rb_geteuid(void);
db137867 249
030272f3 250
db137867
AC
251#include <rb_tools.h>
252#include <rb_memory.h>
253#include <rb_commio.h>
254#include <rb_balloc.h>
255#include <rb_linebuf.h>
db137867
AC
256#include <rb_event.h>
257#include <rb_helper.h>
258#include <rb_rawbuf.h>
259#include <rb_patricia.h>
260
261#endif