v3-4-ctdb: Work around the 1024-fd limit in select
[obnox/samba-ctdb.git] / lib / replace / replace.h
1 /*
2    Unix SMB/CIFS implementation.
3
4    macros to go along with the lib/replace/ portability layer code
5
6    Copyright (C) Andrew Tridgell 2005
7    Copyright (C) Jelmer Vernooij 2006-2008
8    Copyright (C) Jeremy Allison 2007.
9
10      ** NOTE! The following LGPL license applies to the replace
11      ** library. This does NOT imply that all of Samba is released
12      ** under the LGPL
13
14    This library is free software; you can redistribute it and/or
15    modify it under the terms of the GNU Lesser General Public
16    License as published by the Free Software Foundation; either
17    version 3 of the License, or (at your option) any later version.
18
19    This library is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22    Lesser General Public License for more details.
23
24    You should have received a copy of the GNU Lesser General Public
25    License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 */
27
28 #ifndef _LIBREPLACE_REPLACE_H
29 #define _LIBREPLACE_REPLACE_H
30
31 #ifndef NO_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <features.h>
36 #if (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
37 # include <bits/types.h>
38 # undef __FD_SETSIZE
39 # define __FD_SETSIZE 16384
40 #endif
41
42 #ifdef HAVE_STANDARDS_H
43 #include <standards.h>
44 #endif
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <stdarg.h>
49 #include <errno.h>
50
51 #if defined(_MSC_VER) || defined(__MINGW32__)
52 #include "win32_replace.h"
53 #endif
54
55
56 #ifdef HAVE_STDINT_H
57 #include <stdint.h>
58 /* force off HAVE_INTTYPES_H so that roken doesn't try to include both,
59    which causes a warning storm on irix */
60 #undef HAVE_INTTYPES_H
61 #elif HAVE_INTTYPES_H
62 #define __STDC_FORMAT_MACROS
63 #include <inttypes.h>
64 #endif
65
66 #ifndef __PRI64_PREFIX
67 # if __WORDSIZE == 64
68 #  define __PRI64_PREFIX        "l"
69 # else
70 #  define __PRI64_PREFIX        "ll"
71 # endif
72 #endif
73
74 /* Decimal notation.  */
75 #ifndef PRId8
76 # define PRId8          "d"
77 #endif
78 #ifndef PRId16
79 # define PRId16         "d"
80 #endif
81 #ifndef PRId32
82 # define PRId32         "d"
83 #endif
84 #ifndef PRId64
85 # define PRId64         __PRI64_PREFIX "d"
86 #endif
87
88 #ifndef PRIi8
89 # define PRIi8          "i"
90 #endif
91 #ifndef PRIi8
92 # define PRIi16         "i"
93 #endif
94 #ifndef PRIi8
95 # define PRIi32         "i"
96 #endif
97 #ifndef PRIi8
98 # define PRIi64         __PRI64_PREFIX "i"
99 #endif
100
101 #ifndef PRIu8
102 # define PRIu8          "u"
103 #endif
104 #ifndef PRIu16
105 # define PRIu16         "u"
106 #endif
107 #ifndef PRIu32
108 # define PRIu32         "u"
109 #endif
110 #ifndef PRIu64
111 # define PRIu64         __PRI64_PREFIX "u"
112 #endif
113
114 #ifdef HAVE_STRING_H
115 #include <string.h>
116 #endif
117
118 #ifdef HAVE_STRINGS_H
119 #include <strings.h>
120 #endif
121
122 #ifdef HAVE_SYS_TYPES_H
123 #include <sys/types.h>
124 #endif
125
126 #if STDC_HEADERS
127 #include <stdlib.h>
128 #include <stddef.h>
129 #endif
130
131 #ifndef HAVE_STRERROR
132 extern char *sys_errlist[];
133 #define strerror(i) sys_errlist[i]
134 #endif
135
136 #ifndef HAVE_ERRNO_DECL
137 extern int errno;
138 #endif
139
140 #ifndef HAVE_STRDUP
141 #define strdup rep_strdup
142 char *rep_strdup(const char *s);
143 #endif
144
145 #ifndef HAVE_MEMMOVE
146 #define memmove rep_memmove
147 void *rep_memmove(void *dest,const void *src,int size);
148 #endif
149
150 #ifndef HAVE_MKTIME
151 #define mktime rep_mktime
152 /* prototype is in "system/time.h" */
153 #endif
154
155 #ifndef HAVE_TIMEGM
156 #define timegm rep_timegm
157 /* prototype is in "system/time.h" */
158 #endif
159
160 #ifndef HAVE_UTIME
161 #define utime rep_utime
162 /* prototype is in "system/time.h" */
163 #endif
164
165 #ifndef HAVE_UTIMES
166 #define utimes rep_utimes
167 /* prototype is in "system/time.h" */
168 #endif
169
170 #ifndef HAVE_STRLCPY
171 #define strlcpy rep_strlcpy
172 size_t rep_strlcpy(char *d, const char *s, size_t bufsize);
173 #endif
174
175 #ifndef HAVE_STRLCAT
176 #define strlcat rep_strlcat
177 size_t rep_strlcat(char *d, const char *s, size_t bufsize);
178 #endif
179
180 #if (defined(BROKEN_STRNDUP) || !defined(HAVE_STRNDUP))
181 #undef HAVE_STRNDUP
182 #define strndup rep_strndup
183 char *rep_strndup(const char *s, size_t n);
184 #endif
185
186 #if (defined(BROKEN_STRNLEN) || !defined(HAVE_STRNLEN))
187 #undef HAVE_STRNLEN
188 #define strnlen rep_strnlen
189 size_t rep_strnlen(const char *s, size_t n);
190 #endif
191
192 #if !HAVE_DECL_ENVIRON
193 #ifdef __APPLE__
194 #include <crt_externs.h>
195 #define environ (*_NSGetEnviron())
196 #else
197 extern char **environ;
198 #endif
199 #endif
200
201 #ifndef HAVE_SETENV
202 #define setenv rep_setenv
203 int rep_setenv(const char *name, const char *value, int overwrite);
204 #else
205 #ifndef HAVE_SETENV_DECL
206 int setenv(const char *name, const char *value, int overwrite);
207 #endif
208 #endif
209
210 #ifndef HAVE_UNSETENV
211 #define unsetenv rep_unsetenv
212 int rep_unsetenv(const char *name);
213 #endif
214
215 #ifndef HAVE_SETEUID
216 #define seteuid rep_seteuid
217 int rep_seteuid(uid_t);
218 #endif
219
220 #ifndef HAVE_SETEGID
221 #define setegid rep_setegid
222 int rep_setegid(gid_t);
223 #endif
224
225 #if (defined(USE_SETRESUID) && !defined(HAVE_SETRESUID_DECL))
226 /* stupid glibc */
227 int setresuid(uid_t ruid, uid_t euid, uid_t suid);
228 #endif
229 #if (defined(USE_SETRESUID) && !defined(HAVE_SETRESGID_DECL))
230 int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
231 #endif
232
233 #ifndef HAVE_CHOWN
234 #define chown rep_chown
235 int rep_chown(const char *path, uid_t uid, gid_t gid);
236 #endif
237
238 #ifndef HAVE_CHROOT
239 #define chroot rep_chroot
240 int rep_chroot(const char *dirname);
241 #endif
242
243 #ifndef HAVE_LINK
244 #define link rep_link
245 int rep_link(const char *oldpath, const char *newpath);
246 #endif
247
248 #ifndef HAVE_READLINK
249 #define readlink rep_readlink
250 ssize_t rep_readlink(const char *path, char *buf, size_t bufsize);
251 #endif
252
253 #ifndef HAVE_SYMLINK
254 #define symlink rep_symlink
255 int rep_symlink(const char *oldpath, const char *newpath);
256 #endif
257
258 #ifndef HAVE_REALPATH
259 #define realpath rep_realpath
260 char *rep_realpath(const char *path, char *resolved_path);
261 #endif
262
263 #ifndef HAVE_LCHOWN
264 #define lchown rep_lchown
265 int rep_lchown(const char *fname,uid_t uid,gid_t gid);
266 #endif
267
268 #ifndef HAVE_SETLINEBUF
269 #define setlinebuf rep_setlinebuf
270 void rep_setlinebuf(FILE *);
271 #endif
272
273 #ifndef HAVE_STRCASESTR
274 #define strcasestr rep_strcasestr
275 char *rep_strcasestr(const char *haystack, const char *needle);
276 #endif
277
278 #ifndef HAVE_STRTOK_R
279 #define strtok_r rep_strtok_r
280 char *rep_strtok_r(char *s, const char *delim, char **save_ptr);
281 #endif
282
283 #ifndef HAVE_STRTOLL
284 #define strtoll rep_strtoll
285 long long int rep_strtoll(const char *str, char **endptr, int base);
286 #endif
287
288 #ifndef HAVE_STRTOULL
289 #define strtoull rep_strtoull
290 unsigned long long int rep_strtoull(const char *str, char **endptr, int base);
291 #endif
292
293 #ifndef HAVE_FTRUNCATE
294 #define ftruncate rep_ftruncate
295 int rep_ftruncate(int,off_t);
296 #endif
297
298 #ifndef HAVE_INITGROUPS
299 #define initgroups rep_initgroups
300 int rep_initgroups(char *name, gid_t id);
301 #endif
302
303 #if !defined(HAVE_BZERO) && defined(HAVE_MEMSET)
304 #define bzero(a,b) memset((a),'\0',(b))
305 #endif
306
307 #ifndef HAVE_DLERROR
308 #define dlerror rep_dlerror
309 char *rep_dlerror(void);
310 #endif
311
312 #ifndef HAVE_DLOPEN
313 #define dlopen rep_dlopen
314 #ifdef DLOPEN_TAKES_UNSIGNED_FLAGS
315 void *rep_dlopen(const char *name, unsigned int flags);
316 #else
317 void *rep_dlopen(const char *name, int flags);
318 #endif
319 #endif
320
321 #ifndef HAVE_DLSYM
322 #define dlsym rep_dlsym
323 void *rep_dlsym(void *handle, const char *symbol);
324 #endif
325
326 #ifndef HAVE_DLCLOSE
327 #define dlclose rep_dlclose
328 int rep_dlclose(void *handle);
329 #endif
330
331 #ifndef HAVE_SOCKETPAIR
332 #define socketpair rep_socketpair
333 /* prototype is in system/network.h */
334 #endif
335
336 #ifndef PRINTF_ATTRIBUTE
337 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
338 /** Use gcc attribute to check printf fns.  a1 is the 1-based index of
339  * the parameter containing the format, and a2 the index of the first
340  * argument. Note that some gcc 2.x versions don't handle this
341  * properly **/
342 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
343 #else
344 #define PRINTF_ATTRIBUTE(a1, a2)
345 #endif
346 #endif
347
348 #ifndef _DEPRECATED_
349 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
350 #define _DEPRECATED_ __attribute__ ((deprecated))
351 #else
352 #define _DEPRECATED_
353 #endif
354 #endif
355
356 #ifndef HAVE_VASPRINTF
357 #define vasprintf rep_vasprintf
358 int rep_vasprintf(char **ptr, const char *format, va_list ap) PRINTF_ATTRIBUTE(2,0);
359 #endif
360
361 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
362 #define snprintf rep_snprintf
363 int rep_snprintf(char *,size_t ,const char *, ...) PRINTF_ATTRIBUTE(3,4);
364 #endif
365
366 #if !defined(HAVE_VSNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
367 #define vsnprintf rep_vsnprintf
368 int rep_vsnprintf(char *,size_t ,const char *, va_list ap) PRINTF_ATTRIBUTE(3,0);
369 #endif
370
371 #ifndef HAVE_ASPRINTF
372 #define asprintf rep_asprintf
373 int rep_asprintf(char **,const char *, ...) PRINTF_ATTRIBUTE(2,3);
374 #endif
375
376 #ifndef HAVE_VSYSLOG
377 #ifdef HAVE_SYSLOG
378 #define vsyslog rep_vsyslog
379 void rep_vsyslog (int facility_priority, const char *format, va_list arglist) PRINTF_ATTRIBUTE(2,0);
380 #endif
381 #endif
382
383 /* we used to use these fns, but now we have good replacements
384    for snprintf and vsnprintf */
385 #define slprintf snprintf
386
387
388 #ifndef HAVE_VA_COPY
389 #undef va_copy
390 #ifdef HAVE___VA_COPY
391 #define va_copy(dest, src) __va_copy(dest, src)
392 #else
393 #define va_copy(dest, src) (dest) = (src)
394 #endif
395 #endif
396
397 #ifndef HAVE_VOLATILE
398 #define volatile
399 #endif
400
401 #ifndef HAVE_COMPARISON_FN_T
402 typedef int (*comparison_fn_t)(const void *, const void *);
403 #endif
404
405 #ifdef REPLACE_STRPTIME
406 #define strptime rep_strptime
407 struct tm;
408 char *rep_strptime(const char *buf, const char *format, struct tm *tm);
409 #endif
410
411 #ifndef HAVE_DUP2
412 #define dup2 rep_dup2
413 int rep_dup2(int oldfd, int newfd);
414 #endif
415
416 /* Load header file for dynamic linking stuff */
417 #ifdef HAVE_DLFCN_H
418 #include <dlfcn.h>
419 #endif
420
421 #ifndef RTLD_LAZY
422 #define RTLD_LAZY 0
423 #endif
424 #ifndef RTLD_NOW
425 #define RTLD_NOW 0
426 #endif
427 #ifndef RTLD_GLOBAL
428 #define RTLD_GLOBAL 0
429 #endif
430
431 #ifndef HAVE_SECURE_MKSTEMP
432 #define mkstemp(path) rep_mkstemp(path)
433 int rep_mkstemp(char *temp);
434 #endif
435
436 #ifndef HAVE_MKDTEMP
437 #define mkdtemp rep_mkdtemp
438 char *rep_mkdtemp(char *template);
439 #endif
440
441 #ifndef HAVE_PREAD
442 #define pread rep_pread
443 ssize_t rep_pread(int __fd, void *__buf, size_t __nbytes, off_t __offset);
444 #define LIBREPLACE_PREAD_REPLACED 1
445 #else
446 #define LIBREPLACE_PREAD_NOT_REPLACED 1
447 #endif
448
449 #ifndef HAVE_PWRITE
450 #define pwrite rep_pwrite
451 ssize_t rep_pwrite(int __fd, const void *__buf, size_t __nbytes, off_t __offset);
452 #define LIBREPLACE_PWRITE_REPLACED 1
453 #else
454 #define LIBREPLACE_PWRITE_NOT_REPLACED 1
455 #endif
456
457 #if !defined(HAVE_INET_NTOA) || defined(REPLACE_INET_NTOA)
458 #define inet_ntoa rep_inet_ntoa
459 /* prototype is in "system/network.h" */
460 #endif
461
462 #ifndef HAVE_INET_PTON
463 #define inet_pton rep_inet_pton
464 /* prototype is in "system/network.h" */
465 #endif
466
467 #ifndef HAVE_INET_NTOP
468 #define inet_ntop rep_inet_ntop
469 /* prototype is in "system/network.h" */
470 #endif
471
472 #ifndef HAVE_INET_ATON
473 #define inet_aton rep_inet_aton
474 /* prototype is in "system/network.h" */
475 #endif
476
477 #ifndef HAVE_CONNECT
478 #define connect rep_connect
479 /* prototype is in "system/network.h" */
480 #endif
481
482 #ifndef HAVE_GETHOSTBYNAME
483 #define gethostbyname rep_gethostbyname
484 /* prototype is in "system/network.h" */
485 #endif
486
487 #ifndef HAVE_GETIFADDRS
488 #define getifaddrs rep_getifaddrs
489 /* prototype is in "system/network.h" */
490 #endif
491
492 #ifndef HAVE_FREEIFADDRS
493 #define freeifaddrs rep_freeifaddrs
494 /* prototype is in "system/network.h" */
495 #endif
496
497 #ifdef HAVE_LIMITS_H
498 #include <limits.h>
499 #endif
500
501 #ifdef HAVE_SYS_PARAM_H
502 #include <sys/param.h>
503 #endif
504
505 /* The extra casts work around common compiler bugs.  */
506 #define _TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
507 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
508    It is necessary at least when t == time_t.  */
509 #define _TYPE_MINIMUM(t) ((t) (_TYPE_SIGNED (t) \
510                               ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
511 #define _TYPE_MAXIMUM(t) ((t) (~ (t) 0 - _TYPE_MINIMUM (t)))
512
513 #ifndef UINT16_MAX
514 #define UINT16_MAX 65535
515 #endif
516
517 #ifndef UINT32_MAX
518 #define UINT32_MAX (4294967295U)
519 #endif
520
521 #ifndef UINT64_MAX
522 #define UINT64_MAX ((uint64_t)-1)
523 #endif
524
525 #ifndef CHAR_BIT
526 #define CHAR_BIT 8
527 #endif
528
529 #ifndef INT32_MAX
530 #define INT32_MAX _TYPE_MAXIMUM(int32_t)
531 #endif
532
533 #ifdef HAVE_STDBOOL_H
534 #include <stdbool.h>
535 #endif
536
537 #if !defined(HAVE_BOOL)
538 #ifdef HAVE__Bool
539 #define bool _Bool
540 #else
541 typedef int bool;
542 #endif
543 #endif
544
545 /*
546  * to prevent <rpcsvc/yp_prot.h> from doing a redefine of 'bool'
547  *
548  * IRIX, HPUX, MacOS 10 and Solaris need BOOL_DEFINED
549  * Tru64 needs _BOOL_EXISTS
550  * AIX needs _BOOL,_TRUE,_FALSE
551  */
552 #ifndef BOOL_DEFINED
553 #define BOOL_DEFINED
554 #endif
555 #ifndef _BOOL_EXISTS
556 #define _BOOL_EXISTS
557 #endif
558 #ifndef _BOOL
559 #define _BOOL
560 #endif
561
562 #ifndef __bool_true_false_are_defined
563 #define __bool_true_false_are_defined
564 #endif
565
566 #ifndef true
567 #define true (1)
568 #endif
569 #ifndef false
570 #define false (0)
571 #endif
572
573 #ifndef _TRUE
574 #define _TRUE true
575 #endif
576 #ifndef _FALSE
577 #define _FALSE false
578 #endif
579
580 #ifndef HAVE_FUNCTION_MACRO
581 #ifdef HAVE_func_MACRO
582 #define __FUNCTION__ __func__
583 #else
584 #define __FUNCTION__ ("")
585 #endif
586 #endif
587
588
589 #ifndef MIN
590 #define MIN(a,b) ((a)<(b)?(a):(b))
591 #endif
592
593 #ifndef MAX
594 #define MAX(a,b) ((a)>(b)?(a):(b))
595 #endif
596
597 #if !defined(HAVE_VOLATILE)
598 #define volatile
599 #endif
600
601 /**
602   this is a warning hack. The idea is to use this everywhere that we
603   get the "discarding const" warning from gcc. That doesn't actually
604   fix the problem of course, but it means that when we do get to
605   cleaning them up we can do it by searching the code for
606   discard_const.
607
608   It also means that other error types aren't as swamped by the noise
609   of hundreds of const warnings, so we are more likely to notice when
610   we get new errors.
611
612   Please only add more uses of this macro when you find it
613   _really_ hard to fix const warnings. Our aim is to eventually use
614   this function in only a very few places.
615
616   Also, please call this via the discard_const_p() macro interface, as that
617   makes the return type safe.
618 */
619 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
620
621 /** Type-safe version of discard_const */
622 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
623
624 #ifndef __STRING
625 #define __STRING(x)    #x
626 #endif
627
628 #ifndef __STRINGSTRING
629 #define __STRINGSTRING(x) __STRING(x)
630 #endif
631
632 #ifndef __LINESTR__
633 #define __LINESTR__ __STRINGSTRING(__LINE__)
634 #endif
635
636 #ifndef __location__
637 #define __location__ __FILE__ ":" __LINESTR__
638 #endif
639
640 /** 
641  * zero a structure 
642  */
643 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
644
645 /** 
646  * zero a structure given a pointer to the structure 
647  */
648 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
649
650 /** 
651  * zero a structure given a pointer to the structure - no zero check 
652  */
653 #define ZERO_STRUCTPN(x) memset((char *)(x), 0, sizeof(*(x)))
654
655 /* zero an array - note that sizeof(array) must work - ie. it must not be a
656    pointer */
657 #define ZERO_ARRAY(x) memset((char *)(x), 0, sizeof(x))
658
659 /**
660  * work out how many elements there are in a static array 
661  */
662 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
663
664 /** 
665  * pointer difference macro 
666  */
667 #define PTR_DIFF(p1,p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
668
669 #if MMAP_BLACKLIST
670 #undef HAVE_MMAP
671 #endif
672
673 #ifdef __COMPAR_FN_T
674 #define QSORT_CAST (__compar_fn_t)
675 #endif
676
677 #ifndef QSORT_CAST
678 #define QSORT_CAST (int (*)(const void *, const void *))
679 #endif
680
681 #ifndef PATH_MAX
682 #define PATH_MAX 1024
683 #endif
684
685 #ifndef MAX_DNS_NAME_LENGTH
686 #define MAX_DNS_NAME_LENGTH 256 /* Actually 255 but +1 for terminating null. */
687 #endif
688
689 #ifndef HAVE_CRYPT
690 char *ufc_crypt(const char *key, const char *salt);
691 #define crypt ufc_crypt
692 #else
693 #ifdef HAVE_CRYPT_H
694 #include <crypt.h>
695 #endif
696 #endif
697
698 #endif /* _LIBREPLACE_REPLACE_H */