File: compat_shared.h

package info (click to toggle)
squid3 3.4.8-6
  • links: PTS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 31,084 kB
  • sloc: cpp: 165,325; ansic: 21,998; sh: 12,166; makefile: 5,964; perl: 2,153; sql: 322; awk: 118
file content (260 lines) | stat: -rw-r--r-- 6,691 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#ifndef _SQUID_COMPAT_SHARED_H
#define _SQUID_COMPAT_SHARED_H

/*
 * This file contains all the compatibility and portability hacks
 * Which are general-case and shared between all OS and support programs.
 *
 * If an OS-specific hack is needed there are per-OS files for that in
 * the os/ sub-directory here.
 *
 * These hacks should be platform and location agnostic.
 * A quick look-over of the code already here should give you an idea
 * of the requirements for wrapping your hack for safe portability.
 */

#ifdef __cplusplus
/*
 * Define an error display handler override.
 * If error_notify is set by the linked program it will be used by the local
 * portability functions. Otherwise perror() will be used.
 */
extern void (*failure_notify) (const char *);
#endif

/*
 * sys/resource.h and sys/time.h are apparently order-dependant.
 */
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#if HAVE_SYS_RESOURCE_H
#include <sys/resource.h>       /* needs sys/time.h above it */
#endif

/*
 * DIRENT functionality can apparently come from many places.
 * With various complaints by different compilers
 */
#if HAVE_DIRENT_H
#include <dirent.h>
#define NAMLEN(dirent) strlen((dirent)->d_name)
#else /* if not HAVE_DIRENT_H */
#define dirent direct
#define NAMLEN(dirent) (dirent)->d_namlen
#if HAVE_SYS_NDIR_H
#include <sys/ndir.h>
#endif /* HAVE_SYS_NDIR_H */
#if HAVE_SYS_DIR_H
#include <sys/dir.h>
#endif /* HAVE_SYS_DIR_H */
#if HAVE_NDIR_H
#include <ndir.h>
#endif /* HAVE_NDIR_H */
#endif /* HAVE_DIRENT_H */

/* The structure dirent also varies between 64-bit and 32-bit environments.
 * Define our own dirent_t type for consistent simple internal use.
 * NP: GCC seems not to care about the type naming differences.
 */
#if defined(__USE_FILE_OFFSET64) && !defined(__GNUC__)
#define dirent_t struct dirent64
#else
#define dirent_t struct dirent
#endif

/*
 * Filedescriptor limits in the different select loops
 *
 * NP: FreeBSD 7 defines FD_SETSIZE as unsigned but Squid needs
 *     it to be signed to compare it with signed values.
 *     Linux and others including FreeBSD <7, define it as signed.
 *     If this causes any issues please contact squid-dev@squid-cache.org
 */
#if defined(USE_SELECT) || defined(USE_SELECT_WIN32)
/* Limited by design */
# define SQUID_MAXFD_LIMIT    ((signed int)FD_SETSIZE)

#elif defined(USE_POLL)
/* Limited due to delay pools */
# define SQUID_MAXFD_LIMIT    ((signed int)FD_SETSIZE)

#elif defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
# define SQUID_FDSET_NOUSE 1

#else
# error Unknown select loop model!
#endif

#if !HAVE_STRUCT_RUSAGE
/**
 * If we don't have getrusage() then we create a fake structure
 * with only the fields Squid cares about.  This just makes the
 * source code cleaner, so we don't need lots of ifdefs in other
 * places
 */
struct rusage {
    struct timeval ru_stime;
    struct timeval ru_utime;
    int ru_maxrss;
    int ru_majflt;
};
#endif /* !HAVE_STRUCT_RUSAGE */

#ifndef min
#ifdef __cplusplus
/**
 * min() comparison may not always be provided.
 * Squid bundles this template for when its needed.
 * May be used on any type which provides operator '<'
 */
template<class A>
inline A const &
min(A const & lhs, A const & rhs)
{
    if (rhs < lhs)
        return rhs;
    return lhs;
}
#else /* !__cplusplus */
/* for non-C++ we are stuck with the < and ? operator */
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif /* __cplusplus */
#endif /* min */

#ifndef max
#ifdef __cplusplus
/**
 * max() comparison may not always be provided.
 * Squid bundles this template for when its needed.
 * May be used on any type which provides operator '>'
 */
template<class A>
inline A const &
max(A const & lhs, A const & rhs)
{
    if (rhs > lhs)
        return rhs;
    return lhs;
}
#else /* !__cplusplus */
/* for non-C++ we are stuck with the < and ? operator */
#define max(a,b) ((a) < (b) ? (b) : (a))
#endif /* __cplusplus */
#endif /* max */

/**
 * Common shared definition of what whitespace consists of for string tests
 */
#define w_space     " \t\n\r"

#ifndef SQUID_NONBLOCK
/* REQUIRED for the below logics. If they move this needs to as well */
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#if defined(O_NONBLOCK)
/**
 * We used to assume O_NONBLOCK was broken on Solaris, but evidence
 * now indicates that its fine on Solaris 8, and in fact required for
 * properly detecting EOF on FIFOs.  So now we assume that if
 * its defined, it works correctly on all operating systems.
 */
#define SQUID_NONBLOCK O_NONBLOCK
#else
/** O_NDELAY is our fallback. */
#define SQUID_NONBLOCK O_NDELAY
#endif
#endif

/**
 * Signalling flags are apparently not always provided.
 * TODO find out if these can be moved into specific OS portability files.
 */
#if HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifndef SA_RESTART
#define SA_RESTART 0
#endif
#ifndef SA_NODEFER
#define SA_NODEFER 0
#endif
#ifndef SA_RESETHAND
#define SA_RESETHAND 0
#endif
#if SA_RESETHAND == 0 && defined(SA_ONESHOT)
#undef SA_RESETHAND
#define SA_RESETHAND SA_ONESHOT
#endif

/**
 * com_err.h is a C header and needs explicit shielding, but not
 * all other system headers including this care to do so.
 */
#ifdef __cplusplus
#if HAVE_ET_COM_ERR_H
extern "C" {
#include <et/com_err.h>
}
#elif HAVE_COM_ERR_H
extern "C" {
#include <com_err.h>
}
#endif
#endif

/*
 * Several function definitions which we provide for security and code safety.
 */
#include "compat/xalloc.h"
#include "compat/xstrerror.h"
#include "compat/xstring.h"
#include "compat/xstrto.h"
#include "compat/xis.h"

/*
 * strtoll() is needed. Squid provides a portable definition.
 */
#include "compat/strtoll.h"

#if !HAVE_MEMCPY
#if HAVE_BCOPY
#define memcpy(d,s,n) bcopy((s),(d),(n))
#elif HAVE_MEMMOVE
#define memcpy(d,s,n) memmove((d),(s),(n))
#endif
#endif

#if !HAVE_MEMMOVE && HAVE_BCOPY
#define memmove(d,s,n) bcopy((s),(d),(n))
#endif

/*
 * strnstr() is needed. The OS may not provide a working copy.
 */
#if HAVE_STRNSTR
/* If strnstr exists and is usable we do so. */
#define squid_strnstr(a,b,c)    strnstr(a,b,c)
#else
/* If not we have our own copy imported from FreeBSD */
const char * squid_strnstr(const char *s, const char *find, size_t slen);
#endif

#if __GNUC__
#if !defined(PRINTF_FORMAT_ARG1)
#define PRINTF_FORMAT_ARG1 __attribute__ ((format (printf, 1, 2)))
#endif
#if !defined(PRINTF_FORMAT_ARG2)
#define PRINTF_FORMAT_ARG2 __attribute__ ((format (printf, 2, 3)))
#endif
#if !defined(PRINTF_FORMAT_ARG3)
#define PRINTF_FORMAT_ARG3 __attribute__ ((format (printf, 3, 4)))
#endif
#else /* !__GNU__ */
#define PRINTF_FORMAT_ARG1
#define PRINTF_FORMAT_ARG2
#define PRINTF_FORMAT_ARG3
#endif

#endif /* _SQUID_COMPAT_SHARED_H */