Squid Web Cache master
Loading...
Searching...
No Matches
mswindows.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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/*
10 * AUTHOR: Andrey Shorin <tolsty@tushino.com>
11 * AUTHOR: Guido Serassio <serassio@squid-cache.org>
12 */
13
14#ifndef SQUID_COMPAT_OS_MSWINDOWS_H
15#define SQUID_COMPAT_OS_MSWINDOWS_H
16
17#if _SQUID_WINDOWS_
18
19/****************************************************************************
20 *--------------------------------------------------------------------------*
21 * DO *NOT* MAKE ANY CHANGES below here unless you know what you're doing...*
22 *--------------------------------------------------------------------------*
23 ****************************************************************************/
24
25#include "compat/initgroups.h"
26
27#if HAVE_DIRECT_H
28#include <direct.h>
29#endif
30#if HAVE_FCNTL_H
31#include <fcntl.h>
32#endif /* HAVE_FCNTL_H */
33#if HAVE_STRING_H
34#include <string.h>
35#endif /* HAVE_FCNTL_H */
36#if HAVE_SYS_STAT_H
37#include <sys/stat.h>
38#endif /* HAVE_SYS_STAT_H */
39
40#define ACL WindowsACL
41#if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
42#if _MSC_VER == 1400
43#define _CRT_SECURE_NO_DEPRECATE
44#pragma warning( disable : 4290 )
45#pragma warning( disable : 4996 )
46#endif
47#endif
48
49#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
50# define __USE_FILE_OFFSET64 1
51#endif
52
53#if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
54
55#if defined(__USE_FILE_OFFSET64)
56typedef uint64_t ino_t;
57#else
58typedef unsigned long ino_t;
59#endif
60
61#define INT64_MAX _I64_MAX
62#define INT64_MIN _I64_MIN
63
64#include "default_config_file.h"
65/* Some tricks for MS Compilers */
66#define __STDC__ 1
67#define THREADLOCAL __declspec(thread)
68
69#elif defined(__GNUC__) /* gcc environment */
70
71#define THREADLOCAL __attribute__((section(".tls")))
72
73#endif /* _MSC_VER */
74
75/* ONLY Microsoft C Compiler needs these: */
76#if defined(_MSC_VER)
77#define alloca _alloca
78#define fileno _fileno
79#define fstat _fstati64
80#define lseek _lseeki64
81#define memccpy _memccpy
82#define mktemp _mktemp
83#define snprintf _snprintf
84#define stat _stati64
85#define strcasecmp _stricmp
86#define strlwr _strlwr
87#define strncasecmp _strnicmp
88#define tempnam _tempnam
89#define vsnprintf _vsnprintf
90#endif
91
92/* Microsoft C Compiler and CygWin need these. */
93#if defined(_MSC_VER) || _SQUID_CYGWIN_
94SQUIDCEXTERN int WIN32_ftruncate(int fd, off_t size);
95#define ftruncate WIN32_ftruncate
96SQUIDCEXTERN int WIN32_truncate(const char *pathname, off_t length);
97#define truncate WIN32_truncate
98#define chdir _chdir
99#endif
100
101/* All three compiler systems need these: */
102#define dup _dup
103#define dup2 _dup2
104#define fdopen _fdopen
105#define getcwd _getcwd
106#define getpid _getpid
107#define mkdir(p,F) mkdir((p))
108#define pclose _pclose
109#define popen _popen
110
111inline int
112setenv(const char * const name, const char * const value, const int overwrite)
113{
114 if (!overwrite && getenv(name))
115 return 0;
116
117 // overwrite requested or the variable is not set
118
119 // XXX: Unlike POSIX.1 setenv(3) we want to emulate here, _putenv_s() treats
120 // `value` that points to an empty string specially: It removes the named
121 // variable (if any) and does not create a new variable with an empty value.
122 return (_putenv_s(name, value) == 0 ? 0 : -1);
123}
124
125#define setmode _setmode
126#define sleep(t) Sleep((t)*1000)
127#define umask _umask
128#define unlink _unlink
129
130#ifndef O_RDONLY
131#define O_RDONLY _O_RDONLY
132#endif
133#ifndef O_WRONLY
134#define O_WRONLY _O_WRONLY
135#endif
136#ifndef O_RDWR
137#define O_RDWR _O_RDWR
138#endif
139#ifndef O_APPEND
140#define O_APPEND _O_APPEND
141#endif
142#ifndef O_CREAT
143#define O_CREAT _O_CREAT
144#endif
145#ifndef O_TRUNC
146#define O_TRUNC _O_TRUNC
147#endif
148#ifndef O_EXCL
149#define O_EXCL _O_EXCL
150#endif
151#ifndef O_TEXT
152#define O_TEXT _O_TEXT
153#endif
154#ifndef O_BINARY
155#define O_BINARY _O_BINARY
156#endif
157#ifndef O_RAW
158#define O_RAW _O_BINARY
159#endif
160#ifndef O_TEMPORARY
161#define O_TEMPORARY _O_TEMPORARY
162#endif
163#ifndef O_NOINHERIT
164#define O_NOINHERIT _O_NOINHERIT
165#endif
166#ifndef O_SEQUENTIAL
167#define O_SEQUENTIAL _O_SEQUENTIAL
168#endif
169#ifndef O_RANDOM
170#define O_RANDOM _O_RANDOM
171#endif
172#ifndef O_NDELAY
173#define O_NDELAY 0
174#endif
175
176#ifndef S_IFMT
177#define S_IFMT _S_IFMT
178#endif
179#ifndef S_IFDIR
180#define S_IFDIR _S_IFDIR
181#endif
182#ifndef S_IFCHR
183#define S_IFCHR _S_IFCHR
184#endif
185#ifndef S_IFREG
186#define S_IFREG _S_IFREG
187#endif
188#ifndef S_IREAD
189#define S_IREAD _S_IREAD
190#endif
191#ifndef S_IWRITE
192#define S_IWRITE _S_IWRITE
193#endif
194#ifndef S_IEXEC
195#define S_IEXEC _S_IEXEC
196#endif
197#ifndef S_IRWXO
198#define S_IRWXO 007
199#endif
200
201/* There are no group protection bits like these in Windows.
202 * The values are used by umask() to remove permissions so
203 * mapping to user permission bits will break file accesses.
204 * Map group permissions to harmless zero instead.
205 */
206#ifndef S_IXGRP
207#define S_IXGRP 0
208#endif
209#ifndef S_IWGRP
210#define S_IWGRP 0
211#endif
212#ifndef S_IWOTH
213#define S_IWOTH 0
214#endif
215#ifndef S_IXOTH
216#define S_IXOTH 0
217#endif
218
219#if defined(_MSC_VER)
220#define S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR)
221#endif
222
223#define SIGHUP 1 /* hangup */
224#define SIGKILL 9 /* kill (cannot be caught or ignored) */
225#define SIGBUS 10 /* bus error */
226#define SIGPIPE 13 /* write on a pipe with no one to read it */
227#define SIGCHLD 20 /* to parent on child stop or exit */
228#define SIGUSR1 30 /* user defined signal 1 */
229#define SIGUSR2 31 /* user defined signal 2 */
230
231#if defined(_MSC_VER)
232typedef int uid_t;
233typedef int gid_t;
234#endif
235
236struct passwd {
237 char *pw_name; /* user name */
238 char *pw_passwd; /* user password */
239 uid_t pw_uid; /* user id */
240 gid_t pw_gid; /* group id */
241 char *pw_gecos; /* real name */
242 char *pw_dir; /* home directory */
243 char *pw_shell; /* shell program */
244};
245
246struct group {
247 char *gr_name; /* group name */
248 char *gr_passwd; /* group password */
249 gid_t gr_gid; /* group id */
250 char **gr_mem; /* group members */
251};
252
253#if !HAVE_GETTIMEOFDAY
254struct timezone {
255 int tz_minuteswest; /* minutes west of Greenwich */
256 int tz_dsttime; /* type of dst correction */
257};
258#endif
259
260#define CHANGE_FD_SETSIZE 1
261#if CHANGE_FD_SETSIZE && SQUID_MAXFD > DEFAULT_FD_SETSIZE
262#define FD_SETSIZE SQUID_MAXFD
263#endif
264
265#include <process.h>
266#include <errno.h>
267#if HAVE_WINSOCK2_H
268#include <winsock2.h>
269#endif
270
271#if !_SQUID_CYGWIN_
272#undef IN_ADDR
273#include <ws2tcpip.h>
274#endif
275
276#if (EAI_NODATA == EAI_NONAME)
277#undef EAI_NODATA
278#define EAI_NODATA WSANO_DATA
279#endif
280
281#if defined(_MSC_VER)
282/* Hack to suppress compiler warnings on FD_SET() & FD_CLR() */
283#pragma warning (push)
284#pragma warning (disable:4142)
285#endif
286
287/* prevent inclusion of wingdi.h */
288#define NOGDI
289#include <ws2spi.h>
290
291#if defined(_MSC_VER)
292#pragma warning (pop)
293#endif
294
295#include <io.h>
296
297#ifndef EISCONN
298#define EISCONN WSAEISCONN
299#endif
300#ifndef EINPROGRESS
301#define EINPROGRESS WSAEINPROGRESS
302#endif
303#ifndef EWOULDBLOCK
304#define EWOULDBLOCK WSAEWOULDBLOCK
305#endif
306#ifndef EALREADY
307#define EALREADY WSAEALREADY
308#endif
309#ifndef ETIMEDOUT
310#define ETIMEDOUT WSAETIMEDOUT
311#endif
312#ifndef ECONNREFUSED
313#define ECONNREFUSED WSAECONNREFUSED
314#endif
315#ifndef ECONNRESET
316#define ECONNRESET WSAECONNRESET
317#endif
318#ifndef ENOTCONN
319#define ENOTCONN WSAENOTCONN
320#endif
321#ifndef ERESTART
322#define ERESTART WSATRY_AGAIN
323#endif
324#ifndef EAFNOSUPPORT
325#define EAFNOSUPPORT WSAEAFNOSUPPORT
326#endif
327#ifndef ENETUNREACH
328#define ENETUNREACH WSAENETUNREACH
329#endif
330#ifndef ENOTSUP
331#define ENOTSUP WSAEOPNOTSUPP
332#endif
333#ifndef ECONNABORTED
334#define ECONNABORTED WSAECONNABORTED
335#endif
336
337#undef h_errno
338#define h_errno errno /* we'll set it ourselves */
339
340#undef FD_CLR
341#define FD_CLR(fd, set) do { \
342 u_int __i; \
343 SOCKET __sock = _get_osfhandle(fd); \
344 for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count ; __i++) { \
345 if (((fd_set FAR *)(set))->fd_array[__i] == __sock) { \
346 while (__i < ((fd_set FAR *)(set))->fd_count-1) { \
347 ((fd_set FAR *)(set))->fd_array[__i] = \
348 ((fd_set FAR *)(set))->fd_array[__i+1]; \
349 __i++; \
350 } \
351 ((fd_set FAR *)(set))->fd_count--; \
352 break; \
353 } \
354 } \
355} while(0)
356
357#undef FD_SET
358#define FD_SET(fd, set) do { \
359 u_int __i; \
360 SOCKET __sock = _get_osfhandle(fd); \
361 for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count; __i++) { \
362 if (((fd_set FAR *)(set))->fd_array[__i] == (__sock)) { \
363 break; \
364 } \
365 } \
366 if (__i == ((fd_set FAR *)(set))->fd_count) { \
367 if (((fd_set FAR *)(set))->fd_count < FD_SETSIZE) { \
368 ((fd_set FAR *)(set))->fd_array[__i] = (__sock); \
369 ((fd_set FAR *)(set))->fd_count++; \
370 } \
371 } \
372} while(0)
373
374#undef FD_ISSET
375#define FD_ISSET(fd, set) Win32__WSAFDIsSet(fd, (fd_set FAR *)(set))
376
377/* internal to Microsoft CRTLIB */
378typedef struct {
379 long osfhnd; /* underlying OS file HANDLE */
380 char osfile; /* attributes of file (e.g., open in text mode?) */
381 char pipech; /* one char buffer for handles opened on pipes */
382#ifdef _MT
383 int lockinitflag;
384 CRITICAL_SECTION lock;
385#endif /* _MT */
386} ioinfo;
387#define IOINFO_L2E 5
388#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
389#define _pioinfo(i) ( __pioinfo[(i) >> IOINFO_L2E] + ((i) & (IOINFO_ARRAY_ELTS - 1)) )
390#define _osfile(i) ( _pioinfo(i)->osfile )
391#define _osfhnd(i) ( _pioinfo(i)->osfhnd )
392#if !defined(FOPEN)
393#define FOPEN 0x01 /* file handle open */
394#endif
395
396#if defined(_MSC_VER)
397SQUIDCEXTERN _CRTIMP ioinfo * __pioinfo[];
398SQUIDCEXTERN int __cdecl _free_osfhnd(int);
399#endif
400
401SQUIDCEXTERN THREADLOCAL int ws32_result;
402
403#if defined(__cplusplus)
404
405#if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
406
407#endif
408
409// stdlib <functional> definitions are required before std API redefinitions.
410#include <functional>
411
413namespace Squid
414{
417/*
418 * Each of these functions is defined in the Squid namespace so as not to
419 * clash with the winsock2.h definitions.
420 * It is then paired with a #define to cause these wrappers to be used by
421 * the main code instead of those system definitions.
422 *
423 * We do this wrapper in order to:
424 * - cast the parameter types in only one place, and
425 * - record errors in POSIX errno variable, and
426 * - map the FD value used by Squid to the socket handes used by Windows.
427 */
428
429inline int
430ioctl(int s, int c, void * a)
431{
432 if ((::ioctlsocket(_get_osfhandle(s), c, (u_long FAR *)a)) == SOCKET_ERROR) {
433 errno = WSAGetLastError();
434 return -1;
435 } else
436 return 0;
437}
438#define ioctl(s,c,a) Squid::ioctl(s,c,a)
439
440inline int
441ioctlsocket(int s, long c, u_long FAR * a)
442{
443 if ((::ioctlsocket(_get_osfhandle(s), c, a)) == SOCKET_ERROR) {
444 errno = WSAGetLastError();
445 return -1;
446 } else
447 return 0;
448}
449#define ioctlsocket(s,c,a) Squid::ioctlsocket(s,c,a)
450
451inline int
452shutdown(int s, int h)
453{
454 if (::shutdown(_get_osfhandle(s),h) == SOCKET_ERROR) {
455 errno = WSAGetLastError();
456 return -1;
457 } else
458 return 0;
459}
460#define shutdown(s,h) Squid::shutdown(s,h)
461
462#undef WSADuplicateSocket
463inline int
464WSADuplicateSocket(int s, DWORD n, LPWSAPROTOCOL_INFO l)
465{
466#ifdef UNICODE
467 if (::WSADuplicateSocketW(_get_osfhandle(s), n, l) == SOCKET_ERROR) {
468#else
469 if (::WSADuplicateSocketA(_get_osfhandle(s), n, l) == SOCKET_ERROR) {
470#endif
471 errno = WSAGetLastError();
472 return -1;
473 } else
474 return 0;
475}
476#define WSADuplicateSocket(s,n,l) Squid::WSADuplicateSocket(s,n,l)
477
478#undef WSASocket
479inline int
480WSASocket(int a, int t, int p, LPWSAPROTOCOL_INFO i, GROUP g, DWORD f)
481{
482 SOCKET result;
483#ifdef UNICODE
484 if ((result = ::WSASocketW(a, t, p, i, g, f)) == INVALID_SOCKET) {
485#else
486 if ((result = ::WSASocketA(a, t, p, i, g, f)) == INVALID_SOCKET) {
487#endif
488 if (WSAEMFILE == (errno = WSAGetLastError()))
489 errno = EMFILE;
490 return -1;
491 } else
492 return _open_osfhandle(result, 0);
493}
494#define WSASocket(a,t,p,i,g,f) Squid::WSASocket(a,t,p,i,g,f)
495
496} /* namespace Squid */
497
498#else /* #ifdef __cplusplus */
499#define write _write /* Needed in util.c */
500#define open _open /* Needed in win32lib.c */
501#endif /* #ifdef __cplusplus */
502
503/* provide missing definitions from resource.h */
504/* NP: sys/resource.h and sys/time.h are apparently order-dependant. */
505#if HAVE_SYS_TIME_H
506#include <sys/time.h>
507#endif
508#if HAVE_SYS_RESOURCE_H
509#include <sys/resource.h>
510#else
511#define RUSAGE_SELF 0 /* calling process */
512#define RUSAGE_CHILDREN -1 /* terminated child processes */
513
514struct rusage {
515 struct timeval ru_utime; /* user time used */
516 struct timeval ru_stime; /* system time used */
517 long ru_maxrss; /* integral max resident set size */
518 long ru_ixrss; /* integral shared text memory size */
519 long ru_idrss; /* integral unshared data size */
520 long ru_isrss; /* integral unshared stack size */
521 long ru_minflt; /* page reclaims */
522 long ru_majflt; /* page faults */
523 long ru_nswap; /* swaps */
524 long ru_inblock; /* block input operations */
525 long ru_oublock; /* block output operations */
526 long ru_msgsnd; /* messages sent */
527 long ru_msgrcv; /* messages received */
528 long ru_nsignals; /* signals received */
529 long ru_nvcsw; /* voluntary context switches */
530 long ru_nivcsw; /* involuntary context switches */
531};
532#endif /* HAVE_SYS_RESOURCE_H */
533
534#undef ACL
535
536SQUIDCEXTERN int chroot(const char *dirname);
537SQUIDCEXTERN int kill(pid_t, int);
538SQUIDCEXTERN struct passwd * getpwnam(char *unused);
539SQUIDCEXTERN struct group * getgrnam(char *unused);
540
541static inline uid_t
542geteuid(void)
543{
544 return 100;
545}
546static inline int
547seteuid (uid_t euid)
548{
549 return 0;
550}
551static inline uid_t
552getuid(void)
553{
554 return 100;
555}
556static inline int
557setuid (uid_t uid)
558{
559 return 0;
560}
561static inline gid_t
562getegid(void)
563{
564 return 100;
565}
566static inline int
567setegid (gid_t egid)
568{
569 return 0;
570}
571static inline int
572getgid(void)
573{
574 return 100;
575}
576static inline int
577setgid (gid_t gid)
578{
579 return 0;
580}
581
582#if !HAVE_GETPAGESIZE
583/* And now we define a compatibility layer */
584size_t getpagesize();
585#define HAVE_GETPAGESIZE 2
586#endif
587
588SQUIDCEXTERN void WIN32_ExceptionHandlerInit(void);
589SQUIDCEXTERN int Win32__WSAFDIsSet(int fd, fd_set* set);
591
592/* XXX: the logic around this is a bit warped:
593 * we #define ACL unconditionally at the top of this file,
594 * then #undef ACL unconditionally hafway down,
595 * then here re-define ACL to the same value as at the top,
596 * then include windows.h and #undef ACL again.
597 */
598#ifndef ACL
599#define ACL WindowsACL
600#define _MSWIN_ACL_WAS_NOT_DEFINED 1
601#endif
602#include <windows.h>
603#if _MSWIN_ACL_WAS_NOT_DEFINED
604#undef ACL
605#undef _MSWIN_ACL_WAS_NOT_DEFINED
606#endif
607
608#if !HAVE_SYSLOG
609/* syslog compatibility layer derives from git */
610#define LOG_PID 0x01
611#define LOG_EMERG 0
612#define LOG_ALERT 1
613#define LOG_CRIT 2
614#define LOG_ERR 3
615#define LOG_WARNING 4
616#define LOG_NOTICE 5
617#define LOG_INFO 6
618#define LOG_DEBUG 7
619#define LOG_DAEMON (3<<3)
620
621void openlog(const char *ident, int logopt, int facility);
622void syslog(int priority, const char *fmt, ...);
623#endif
624
625#endif /* _SQUID_WINDOWS_ */
626#endif /* SQUID_COMPAT_OS_MSWINDOWS_H */
627
int size
Definition ModDevPoll.cc:70
#define SQUIDCEXTERN
Definition squid.h:21
DWORD WIN32_IpAddrChangeMonitorInit()