compat_shared.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2019 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 mailing list.
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 
104 struct rusage {
105  struct timeval ru_stime;
106  struct timeval ru_utime;
109 };
110 #endif /* !HAVE_STRUCT_RUSAGE */
111 
112 #ifndef min
113 #ifdef __cplusplus
114 
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 
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 
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 
171 #define SQUID_NONBLOCK O_NONBLOCK
172 #else
173 
174 #define SQUID_NONBLOCK O_NDELAY
175 #endif
176 #endif
177 
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 
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 
void(* failure_notify)(const char *)
Definition: compat.cc:12
struct timeval ru_stime
const char * squid_strnstr(const char *s, const char *find, size_t slen)
Definition: strnstr.cc:77
A const & max(A const &lhs, A const &rhs)
int ru_majflt
struct timeval ru_utime
static uint32 A
Definition: md4.c:43
int ru_maxrss
A const & min(A const &lhs, A const &rhs)

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors