]> git.ipfire.org Git - thirdparty/squid.git/blame - compat/compat_shared.h
SourceFormat Enforcement
[thirdparty/squid.git] / compat / compat_shared.h
CommitLineData
37be9888 1/*
bde978a6 2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
37be9888
AJ
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
f04e1182
AJ
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.
af6a12ee 15 *
f04e1182
AJ
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
2ded5176 24#ifdef __cplusplus
25f98340
AJ
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 */
2ded5176 30extern void (*failure_notify) (const char *);
25f98340 31#endif
f04e1182
AJ
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.
a8d98451 45 * With various complaints by different compilers
f04e1182
AJ
46 */
47#if HAVE_DIRENT_H
48#include <dirent.h>
49#define NAMLEN(dirent) strlen((dirent)->d_name)
f04e1182
AJ
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
a8d98451
AJ
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.
6bbbf111 66 * NP: GCC seems not to care about the type naming differences.
a8d98451 67 */
6bbbf111 68#if defined(__USE_FILE_OFFSET64) && !defined(__GNUC__)
03ff133d 69#define dirent_t struct dirent64
a8d98451 70#else
03ff133d 71#define dirent_t struct dirent
a8d98451
AJ
72#endif
73
f04e1182
AJ
74/*
75 * Filedescriptor limits in the different select loops
af6a12ee 76 *
cc937513
AJ
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
f04e1182
AJ
81 */
82#if defined(USE_SELECT) || defined(USE_SELECT_WIN32)
83/* Limited by design */
cc937513 84# define SQUID_MAXFD_LIMIT ((signed int)FD_SETSIZE)
f04e1182
AJ
85
86#elif defined(USE_POLL)
87/* Limited due to delay pools */
cc937513 88# define SQUID_MAXFD_LIMIT ((signed int)FD_SETSIZE)
f04e1182 89
a1ad2f9b 90#elif defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
f04e1182
AJ
91# define SQUID_FDSET_NOUSE 1
92
93#else
94# error Unknown select loop model!
95#endif
96
f04e1182
AJ
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
d85b8894 101 * source code cleaner, so we don't need lots of ifdefs in other
f04e1182
AJ
102 * places
103 */
af6a12ee 104struct rusage {
f04e1182
AJ
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
f04e1182 112#ifndef min
d85c3078 113#ifdef __cplusplus
f04e1182
AJ
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 */
119template<class A>
120inline A const &
121min(A const & lhs, A const & rhs)
122{
123 if (rhs < lhs)
124 return rhs;
125 return lhs;
126}
d85c3078
AJ
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 */
f04e1182
AJ
132
133#ifndef max
d85c3078 134#ifdef __cplusplus
f04e1182
AJ
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 */
140template<class A>
141inline A const &
142max(A const & lhs, A const & rhs)
143{
144 if (rhs > lhs)
145 return rhs;
146 return lhs;
147}
d85c3078
AJ
148#else /* !__cplusplus */
149/* for non-C++ we are stuck with the < and ? operator */
150#define max(a,b) ((a) < (b) ? (b) : (a))
f04e1182 151#endif /* __cplusplus */
d85c3078 152#endif /* max */
f04e1182 153
f04e1182
AJ
154/**
155 * Common shared definition of what whitespace consists of for string tests
156 */
157#define w_space " \t\n\r"
158
dc47f531 159#ifndef SQUID_NONBLOCK
f04e1182
AJ
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
dc47f531 176#endif
f04e1182
AJ
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 */
074d6a40
AJ
182#if defined(__cplusplus)
183#include <csignal>
184#else
6b6d5344
AJ
185#if HAVE_SIGNAL_H
186#include <signal.h>
187#endif
074d6a40 188#endif
f04e1182
AJ
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
b092339c 203/**
dc47f531
AJ
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.
b092339c
HN
206 */
207#ifdef __cplusplus
208#if HAVE_ET_COM_ERR_H
209extern "C" {
210#include <et/com_err.h>
211}
212#elif HAVE_COM_ERR_H
213extern "C" {
214#include <com_err.h>
215}
216#endif
217#endif
218
25f98340
AJ
219/*
220 * Several function definitions which we provide for security and code safety.
221 */
222#include "compat/xalloc.h"
602d9612 223#include "compat/xis.h"
25f98340
AJ
224#include "compat/xstrerror.h"
225#include "compat/xstring.h"
226#include "compat/xstrto.h"
227
628fed9d
AJ
228/*
229 * strtoll() is needed. Squid provides a portable definition.
230 */
231#include "compat/strtoll.h"
232
ee4d4e85
AJ
233// memrchr() is a GNU extension. MinGW in particular does not define it.
234#include "compat/memrchr.h"
235
41d00cd3
AJ
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 */
256const 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
aea9544a
AJ
269#else /* !__GNU__ */
270#define PRINTF_FORMAT_ARG1
271#define PRINTF_FORMAT_ARG2
272#define PRINTF_FORMAT_ARG3
41d00cd3 273#endif
ef364f64 274
f04e1182 275#endif /* _SQUID_COMPAT_SHARED_H */
f53969cc 276