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

 

Introduction

Documentation

Support

Miscellaneous