compat_shared.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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 */
30extern 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/* The structure dirent also varies between 64-bit and 32-bit environments.
44 * Define our own dirent_t type for consistent simple internal use.
45 * NP: GCC seems not to care about the type naming differences.
46 */
47#if HAVE_DIRENT_H
48#include <dirent.h>
49#if defined(__USE_FILE_OFFSET64) && !defined(__GNUC__)
50#define dirent_t struct dirent64
51#else
52#define dirent_t struct dirent
53#endif
54#endif /* HAVE_DIRENT_H */
55
56/*
57 * Filedescriptor limits in the different select loops
58 *
59 * NP: FreeBSD 7 defines FD_SETSIZE as unsigned but Squid needs
60 * it to be signed to compare it with signed values.
61 * Linux and others including FreeBSD <7, define it as signed.
62 * If this causes any issues please contact squid-dev mailing list.
63 */
64#if defined(USE_SELECT)
65/* Limited by design */
66# define SQUID_MAXFD_LIMIT ((signed int)FD_SETSIZE)
67
68#elif defined(USE_POLL)
69/* Limited due to delay pools */
70# define SQUID_MAXFD_LIMIT ((signed int)FD_SETSIZE)
71
72#elif defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
73# define SQUID_FDSET_NOUSE 1
74
75#else
76# error Unknown select loop model!
77#endif
78
79#if !HAVE_STRUCT_RUSAGE
86struct rusage {
87 struct timeval ru_stime;
88 struct timeval ru_utime;
91};
92#endif /* !HAVE_STRUCT_RUSAGE */
93
94#ifndef min
95#ifdef __cplusplus
101template<class A>
102inline A const &
103min(A const & lhs, A const & rhs)
104{
105 if (rhs < lhs)
106 return rhs;
107 return lhs;
108}
109#else /* !__cplusplus */
110/* for non-C++ we are stuck with the < and ? operator */
111#define min(a,b) ((a) < (b) ? (a) : (b))
112#endif /* __cplusplus */
113#endif /* min */
114
115#ifndef max
116#ifdef __cplusplus
122template<class A>
123inline A const &
124max(A const & lhs, A const & rhs)
125{
126 if (rhs > lhs)
127 return rhs;
128 return lhs;
129}
130#else /* !__cplusplus */
131/* for non-C++ we are stuck with the < and ? operator */
132#define max(a,b) ((a) < (b) ? (b) : (a))
133#endif /* __cplusplus */
134#endif /* max */
135
139#define w_space " \t\n\r"
140
141#ifndef SQUID_NONBLOCK
142/* REQUIRED for the below logics. If they move this needs to as well */
143#if HAVE_FCNTL_H
144#include <fcntl.h>
145#endif
146#if defined(O_NONBLOCK)
153#define SQUID_NONBLOCK O_NONBLOCK
154#else
156#define SQUID_NONBLOCK O_NDELAY
157#endif
158#endif
159
164#if defined(__cplusplus)
165#include <csignal>
166#else
167#if HAVE_SIGNAL_H
168#include <signal.h>
169#endif
170#endif
171#ifndef SA_RESTART
172#define SA_RESTART 0
173#endif
174#ifndef SA_NODEFER
175#define SA_NODEFER 0
176#endif
177#ifndef SA_RESETHAND
178#define SA_RESETHAND 0
179#endif
180#if SA_RESETHAND == 0 && defined(SA_ONESHOT)
181#undef SA_RESETHAND
182#define SA_RESETHAND SA_ONESHOT
183#endif
184
189#ifdef __cplusplus
190#if HAVE_ET_COM_ERR_H
191extern "C" {
192#include <et/com_err.h>
193}
194#elif HAVE_COM_ERR_H
195extern "C" {
196#include <com_err.h>
197}
198#endif
199#endif
200
201/*
202 * Several function definitions which we provide for security and code safety.
203 */
204#include "compat/xalloc.h"
205#include "compat/xis.h"
206#include "compat/xstrerror.h"
207#include "compat/xstring.h"
208#include "compat/xstrto.h"
209
210/*
211 * strtoll() is needed. Squid provides a portable definition.
212 */
213#include "compat/strtoll.h"
214
215// memrchr() is a GNU extension. MinGW in particular does not define it.
216#include "compat/memrchr.h"
217
218#if !HAVE_MEMCPY
219#if HAVE_BCOPY
220#define memcpy(d,s,n) bcopy((s),(d),(n))
221#elif HAVE_MEMMOVE
222#define memcpy(d,s,n) memmove((d),(s),(n))
223#endif
224#endif
225
226#if !HAVE_MEMMOVE && HAVE_BCOPY
227#define memmove(d,s,n) bcopy((s),(d),(n))
228#endif
229
230/*
231 * strnstr() is needed. The OS may not provide a working copy.
232 */
233#if HAVE_STRNSTR
234/* If strnstr exists and is usable we do so. */
235#define squid_strnstr(a,b,c) strnstr(a,b,c)
236#else
237/* If not we have our own copy imported from FreeBSD */
238const char * squid_strnstr(const char *s, const char *find, size_t slen);
239#endif
240
241#if __GNUC__
242#if _SQUID_MINGW_
243#define PRINTF_FORMAT_ARG1 __attribute__ ((format (gnu_printf, 1, 2)))
244#define PRINTF_FORMAT_ARG2 __attribute__ ((format (gnu_printf, 2, 3)))
245#define PRINTF_FORMAT_ARG3 __attribute__ ((format (gnu_printf, 3, 4)))
246#else
247#define PRINTF_FORMAT_ARG1 __attribute__ ((format (printf, 1, 2)))
248#define PRINTF_FORMAT_ARG2 __attribute__ ((format (printf, 2, 3)))
249#define PRINTF_FORMAT_ARG3 __attribute__ ((format (printf, 3, 4)))
250#endif /* !_SQUID_MINGW_ */
251#else /* !__GNU__ */
252#define PRINTF_FORMAT_ARG1
253#define PRINTF_FORMAT_ARG2
254#define PRINTF_FORMAT_ARG3
255#endif
256
257#endif /* _SQUID_COMPAT_SHARED_H */
258
void(* failure_notify)(const char *)
Definition: compat.cc:12
A const & max(A const &lhs, A const &rhs)
A const & min(A const &lhs, A const &rhs)
const char * squid_strnstr(const char *s, const char *find, size_t slen)
Definition: strnstr.cc:77
static uint32 A
Definition: md4.c:43
struct timeval ru_utime
Definition: compat_shared.h:88
struct timeval ru_stime
Definition: compat_shared.h:87
int ru_majflt
Definition: compat_shared.h:90
int ru_maxrss
Definition: compat_shared.h:89

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors