]> git.ipfire.org Git - thirdparty/squid.git/blob - compat/compat_shared.h
SourceFormat Enforcement
[thirdparty/squid.git] / compat / compat_shared.h
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef _SQUID_COMPAT_SHARED_H
10 #define _SQUID_COMPAT_SHARED_H
11
12 /*
13 * This file contains all the compatibility and portability hacks
14 * Which are general-case and shared between all OS and support programs.
15 *
16 * If an OS-specific hack is needed there are per-OS files for that in
17 * the os/ sub-directory here.
18 *
19 * These hacks should be platform and location agnostic.
20 * A quick look-over of the code already here should give you an idea
21 * of the requirements for wrapping your hack for safe portability.
22 */
23
24 #ifdef __cplusplus
25 /*
26 * Define an error display handler override.
27 * If error_notify is set by the linked program it will be used by the local
28 * portability functions. Otherwise perror() will be used.
29 */
30 extern void (*failure_notify) (const char *);
31 #endif
32
33 /*
34 * sys/resource.h and sys/time.h are apparently order-dependant.
35 */
36 #if HAVE_SYS_TIME_H
37 #include <sys/time.h>
38 #endif
39 #if HAVE_SYS_RESOURCE_H
40 #include <sys/resource.h> /* needs sys/time.h above it */
41 #endif
42
43 /*
44 * DIRENT functionality can apparently come from many places.
45 * With various complaints by different compilers
46 */
47 #if HAVE_DIRENT_H
48 #include <dirent.h>
49 #define NAMLEN(dirent) strlen((dirent)->d_name)
50 #else /* if not HAVE_DIRENT_H */
51 #define dirent direct
52 #define NAMLEN(dirent) (dirent)->d_namlen
53 #if HAVE_SYS_NDIR_H
54 #include <sys/ndir.h>
55 #endif /* HAVE_SYS_NDIR_H */
56 #if HAVE_SYS_DIR_H
57 #include <sys/dir.h>
58 #endif /* HAVE_SYS_DIR_H */
59 #if HAVE_NDIR_H
60 #include <ndir.h>
61 #endif /* HAVE_NDIR_H */
62 #endif /* HAVE_DIRENT_H */
63
64 /* The structure dirent also varies between 64-bit and 32-bit environments.
65 * Define our own dirent_t type for consistent simple internal use.
66 * NP: GCC seems not to care about the type naming differences.
67 */
68 #if defined(__USE_FILE_OFFSET64) && !defined(__GNUC__)
69 #define dirent_t struct dirent64
70 #else
71 #define dirent_t struct dirent
72 #endif
73
74 /*
75 * Filedescriptor limits in the different select loops
76 *
77 * NP: FreeBSD 7 defines FD_SETSIZE as unsigned but Squid needs
78 * it to be signed to compare it with signed values.
79 * Linux and others including FreeBSD <7, define it as signed.
80 * If this causes any issues please contact squid-dev@squid-cache.org
81 */
82 #if defined(USE_SELECT) || defined(USE_SELECT_WIN32)
83 /* Limited by design */
84 # define SQUID_MAXFD_LIMIT ((signed int)FD_SETSIZE)
85
86 #elif defined(USE_POLL)
87 /* Limited due to delay pools */
88 # define SQUID_MAXFD_LIMIT ((signed int)FD_SETSIZE)
89
90 #elif defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
91 # define SQUID_FDSET_NOUSE 1
92
93 #else
94 # error Unknown select loop model!
95 #endif
96
97 #if !HAVE_STRUCT_RUSAGE
98 /**
99 * If we don't have getrusage() then we create a fake structure
100 * with only the fields Squid cares about. This just makes the
101 * source code cleaner, so we don't need lots of ifdefs in other
102 * places
103 */
104 struct rusage {
105 struct timeval ru_stime;
106 struct timeval ru_utime;
107 int ru_maxrss;
108 int ru_majflt;
109 };
110 #endif /* !HAVE_STRUCT_RUSAGE */
111
112 #ifndef min
113 #ifdef __cplusplus
114 /**
115 * min() comparison may not always be provided.
116 * Squid bundles this template for when its needed.
117 * May be used on any type which provides operator '<'
118 */
119 template<class A>
120 inline A const &
121 min(A const & lhs, A const & rhs)
122 {
123 if (rhs < lhs)
124 return rhs;
125 return lhs;
126 }
127 #else /* !__cplusplus */
128 /* for non-C++ we are stuck with the < and ? operator */
129 #define min(a,b) ((a) < (b) ? (a) : (b))
130 #endif /* __cplusplus */
131 #endif /* min */
132
133 #ifndef max
134 #ifdef __cplusplus
135 /**
136 * max() comparison may not always be provided.
137 * Squid bundles this template for when its needed.
138 * May be used on any type which provides operator '>'
139 */
140 template<class A>
141 inline A const &
142 max(A const & lhs, A const & rhs)
143 {
144 if (rhs > lhs)
145 return rhs;
146 return lhs;
147 }
148 #else /* !__cplusplus */
149 /* for non-C++ we are stuck with the < and ? operator */
150 #define max(a,b) ((a) < (b) ? (b) : (a))
151 #endif /* __cplusplus */
152 #endif /* max */
153
154 /**
155 * Common shared definition of what whitespace consists of for string tests
156 */
157 #define w_space " \t\n\r"
158
159 #ifndef SQUID_NONBLOCK
160 /* REQUIRED for the below logics. If they move this needs to as well */
161 #if HAVE_FCNTL_H
162 #include <fcntl.h>
163 #endif
164 #if defined(O_NONBLOCK)
165 /**
166 * We used to assume O_NONBLOCK was broken on Solaris, but evidence
167 * now indicates that its fine on Solaris 8, and in fact required for
168 * properly detecting EOF on FIFOs. So now we assume that if
169 * its defined, it works correctly on all operating systems.
170 */
171 #define SQUID_NONBLOCK O_NONBLOCK
172 #else
173 /** O_NDELAY is our fallback. */
174 #define SQUID_NONBLOCK O_NDELAY
175 #endif
176 #endif
177
178 /**
179 * Signalling flags are apparently not always provided.
180 * TODO find out if these can be moved into specific OS portability files.
181 */
182 #if defined(__cplusplus)
183 #include <csignal>
184 #else
185 #if HAVE_SIGNAL_H
186 #include <signal.h>
187 #endif
188 #endif
189 #ifndef SA_RESTART
190 #define SA_RESTART 0
191 #endif
192 #ifndef SA_NODEFER
193 #define SA_NODEFER 0
194 #endif
195 #ifndef SA_RESETHAND
196 #define SA_RESETHAND 0
197 #endif
198 #if SA_RESETHAND == 0 && defined(SA_ONESHOT)
199 #undef SA_RESETHAND
200 #define SA_RESETHAND SA_ONESHOT
201 #endif
202
203 /**
204 * com_err.h is a C header and needs explicit shielding, but not
205 * all other system headers including this care to do so.
206 */
207 #ifdef __cplusplus
208 #if HAVE_ET_COM_ERR_H
209 extern "C" {
210 #include <et/com_err.h>
211 }
212 #elif HAVE_COM_ERR_H
213 extern "C" {
214 #include <com_err.h>
215 }
216 #endif
217 #endif
218
219 /*
220 * Several function definitions which we provide for security and code safety.
221 */
222 #include "compat/xalloc.h"
223 #include "compat/xis.h"
224 #include "compat/xstrerror.h"
225 #include "compat/xstring.h"
226 #include "compat/xstrto.h"
227
228 /*
229 * strtoll() is needed. Squid provides a portable definition.
230 */
231 #include "compat/strtoll.h"
232
233 // memrchr() is a GNU extension. MinGW in particular does not define it.
234 #include "compat/memrchr.h"
235
236 #if !HAVE_MEMCPY
237 #if HAVE_BCOPY
238 #define memcpy(d,s,n) bcopy((s),(d),(n))
239 #elif HAVE_MEMMOVE
240 #define memcpy(d,s,n) memmove((d),(s),(n))
241 #endif
242 #endif
243
244 #if !HAVE_MEMMOVE && HAVE_BCOPY
245 #define memmove(d,s,n) bcopy((s),(d),(n))
246 #endif
247
248 /*
249 * strnstr() is needed. The OS may not provide a working copy.
250 */
251 #if HAVE_STRNSTR
252 /* If strnstr exists and is usable we do so. */
253 #define squid_strnstr(a,b,c) strnstr(a,b,c)
254 #else
255 /* If not we have our own copy imported from FreeBSD */
256 const char * squid_strnstr(const char *s, const char *find, size_t slen);
257 #endif
258
259 #if __GNUC__
260 #if !defined(PRINTF_FORMAT_ARG1)
261 #define PRINTF_FORMAT_ARG1 __attribute__ ((format (printf, 1, 2)))
262 #endif
263 #if !defined(PRINTF_FORMAT_ARG2)
264 #define PRINTF_FORMAT_ARG2 __attribute__ ((format (printf, 2, 3)))
265 #endif
266 #if !defined(PRINTF_FORMAT_ARG3)
267 #define PRINTF_FORMAT_ARG3 __attribute__ ((format (printf, 3, 4)))
268 #endif
269 #else /* !__GNU__ */
270 #define PRINTF_FORMAT_ARG1
271 #define PRINTF_FORMAT_ARG2
272 #define PRINTF_FORMAT_ARG3
273 #endif
274
275 #endif /* _SQUID_COMPAT_SHARED_H */
276