mswindows.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 /*
10  * AUTHOR: Andrey Shorin <tolsty@tushino.com>
11  * AUTHOR: Guido Serassio <serassio@squid-cache.org>
12  */
13 
14 #ifndef SQUID_OS_MSWINDOWS_H
15 #define SQUID_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 /* we target Windows XP and later - some API are missing otherwise */
26 #if _SQUID_MINGW_
27 #if WINVER < 0x0501
28 #undef WINVER
29 #define WINVER 0x0501
30 #undef _WIN32_WINNT
31 #define _WIN32_WINNT WINVER
32 #endif
33 #endif /* _SQUID_MINGW_ */
34 
35 #include "compat/initgroups.h"
36 
37 #if HAVE_DIRECT_H
38 #include <direct.h>
39 #endif
40 #if HAVE_FCNTL_H
41 #include <fcntl.h>
42 #endif /* HAVE_FCNTL_H */
43 #if HAVE_STRING_H
44 #include <string.h>
45 #endif /* HAVE_FCNTL_H */
46 #if HAVE_SYS_STAT_H
47 #include <sys/stat.h>
48 #endif /* HAVE_SYS_STAT_H */
49 
50 #define ACL WindowsACL
51 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
52 #if _MSC_VER == 1400
53 #define _CRT_SECURE_NO_DEPRECATE
54 #pragma warning( disable : 4290 )
55 #pragma warning( disable : 4996 )
56 #endif
57 #endif
58 
59 /* Some MinGW version defines min() and max() as macros
60  causing the fail of the build process. The following
61  #define will disable that definition
62  */
63 #if defined(__GNUC__) && !NOMINMAX
64 #define NOMINMAX
65 #endif
66 
68 #if !defined(IPV6_V6ONLY)
69 #define IPV6_V6ONLY 27
70 #endif
71 
72 #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
73 # define __USE_FILE_OFFSET64 1
74 #endif
75 
76 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
77 
78 #if defined(__USE_FILE_OFFSET64)
79 typedef uint64_t ino_t;
80 #else
81 typedef unsigned long ino_t;
82 #endif
83 
84 #define INT64_MAX _I64_MAX
85 #define INT64_MIN _I64_MIN
86 
87 #include "default_config_file.h"
88 /* Some tricks for MS Compilers */
89 #define __STDC__ 1
90 #define THREADLOCAL __declspec(thread)
91 
92 #elif defined(__GNUC__) /* gcc environment */
93 
94 #define THREADLOCAL __attribute__((section(".tls")))
95 
96 #endif /* _MSC_VER */
97 
98 /* ONLY Microsoft C Compiler needs these: */
99 #if defined(_MSC_VER)
100 #define alloca _alloca
101 #define fileno _fileno
102 #define fstat _fstati64
103 #define lseek _lseeki64
104 #define memccpy _memccpy
105 #define mktemp _mktemp
106 #define snprintf _snprintf
107 #define stat _stati64
108 #define strcasecmp _stricmp
109 #define strlwr _strlwr
110 #define strncasecmp _strnicmp
111 #define tempnam _tempnam
112 #define vsnprintf _vsnprintf
113 #endif
114 
115 /* Microsoft C Compiler and CygWin need these. MinGW does not */
116 #if defined(_MSC_VER) || _SQUID_CYGWIN_
117 SQUIDCEXTERN int WIN32_ftruncate(int fd, off_t size);
118 #define ftruncate WIN32_ftruncate
119 SQUIDCEXTERN int WIN32_truncate(const char *pathname, off_t length);
120 #define truncate WIN32_truncate
121 #define chdir _chdir
122 #endif
123 
124 /* All three compiler systems need these: */
125 #define dup _dup
126 #define dup2 _dup2
127 #define fdopen _fdopen
128 #define getcwd _getcwd
129 #define getpid _getpid
130 #define mkdir(p,F) mkdir((p))
131 #define pclose _pclose
132 #define popen _popen
133 #define putenv _putenv
134 #define setmode _setmode
135 #define sleep(t) Sleep((t)*1000)
136 #define umask _umask
137 #define unlink _unlink
138 
139 #ifndef O_RDONLY
140 #define O_RDONLY _O_RDONLY
141 #endif
142 #ifndef O_WRONLY
143 #define O_WRONLY _O_WRONLY
144 #endif
145 #ifndef O_RDWR
146 #define O_RDWR _O_RDWR
147 #endif
148 #ifndef O_APPEND
149 #define O_APPEND _O_APPEND
150 #endif
151 #ifndef O_CREAT
152 #define O_CREAT _O_CREAT
153 #endif
154 #ifndef O_TRUNC
155 #define O_TRUNC _O_TRUNC
156 #endif
157 #ifndef O_EXCL
158 #define O_EXCL _O_EXCL
159 #endif
160 #ifndef O_TEXT
161 #define O_TEXT _O_TEXT
162 #endif
163 #ifndef O_BINARY
164 #define O_BINARY _O_BINARY
165 #endif
166 #ifndef O_RAW
167 #define O_RAW _O_BINARY
168 #endif
169 #ifndef O_TEMPORARY
170 #define O_TEMPORARY _O_TEMPORARY
171 #endif
172 #ifndef O_NOINHERIT
173 #define O_NOINHERIT _O_NOINHERIT
174 #endif
175 #ifndef O_SEQUENTIAL
176 #define O_SEQUENTIAL _O_SEQUENTIAL
177 #endif
178 #ifndef O_RANDOM
179 #define O_RANDOM _O_RANDOM
180 #endif
181 #ifndef O_NDELAY
182 #define O_NDELAY 0
183 #endif
184 
185 #ifndef S_IFMT
186 #define S_IFMT _S_IFMT
187 #endif
188 #ifndef S_IFDIR
189 #define S_IFDIR _S_IFDIR
190 #endif
191 #ifndef S_IFCHR
192 #define S_IFCHR _S_IFCHR
193 #endif
194 #ifndef S_IFREG
195 #define S_IFREG _S_IFREG
196 #endif
197 #ifndef S_IREAD
198 #define S_IREAD _S_IREAD
199 #endif
200 #ifndef S_IWRITE
201 #define S_IWRITE _S_IWRITE
202 #endif
203 #ifndef S_IEXEC
204 #define S_IEXEC _S_IEXEC
205 #endif
206 #ifndef S_IRWXO
207 #define S_IRWXO 007
208 #endif
209 
210 /* There are no group protection bits like these in Windows.
211  * The values are used by umask() to remove permissions so
212  * mapping to user permission bits will break file accesses.
213  * Map group permissions to harmless zero instead.
214  */
215 #ifndef S_IXGRP
216 #define S_IXGRP 0
217 #endif
218 #ifndef S_IWGRP
219 #define S_IWGRP 0
220 #endif
221 #ifndef S_IWOTH
222 #define S_IWOTH 0
223 #endif
224 #ifndef S_IXOTH
225 #define S_IXOTH 0
226 #endif
227 
228 #if defined(_MSC_VER)
229 #define S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR)
230 #endif
231 
232 #define SIGHUP 1 /* hangup */
233 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
234 #define SIGBUS 10 /* bus error */
235 #define SIGPIPE 13 /* write on a pipe with no one to read it */
236 #define SIGCHLD 20 /* to parent on child stop or exit */
237 #define SIGUSR1 30 /* user defined signal 1 */
238 #define SIGUSR2 31 /* user defined signal 2 */
239 
240 #if _SQUID_MINGW_
241 typedef unsigned char boolean;
242 typedef unsigned char u_char;
243 typedef unsigned int u_int;
244 #endif
245 
246 #if defined(_MSC_VER)
247 typedef int uid_t;
248 typedef int gid_t;
249 #endif
250 
251 struct passwd {
252  char *pw_name; /* user name */
253  char *pw_passwd; /* user password */
254  uid_t pw_uid; /* user id */
255  gid_t pw_gid; /* group id */
256  char *pw_gecos; /* real name */
257  char *pw_dir; /* home directory */
258  char *pw_shell; /* shell program */
259 };
260 
261 struct group {
262  char *gr_name; /* group name */
263  char *gr_passwd; /* group password */
264  gid_t gr_gid; /* group id */
265  char **gr_mem; /* group members */
266 };
267 
268 #if !HAVE_GETTIMEOFDAY
269 struct timezone {
270  int tz_minuteswest; /* minutes west of Greenwich */
271  int tz_dsttime; /* type of dst correction */
272 };
273 #endif
274 
275 #define CHANGE_FD_SETSIZE 1
276 #if CHANGE_FD_SETSIZE && SQUID_MAXFD > DEFAULT_FD_SETSIZE
277 #define FD_SETSIZE SQUID_MAXFD
278 #endif
279 
280 #include <process.h>
281 #include <errno.h>
282 #if HAVE_WINSOCK2_H
283 #include <winsock2.h>
284 #elif HAVE_WINSOCK_H
285 #include <winsock.h>
286 #endif
287 
288 #if !_SQUID_CYGWIN_
289 #undef IN_ADDR
290 #include <ws2tcpip.h>
291 #endif
292 
293 #if (EAI_NODATA == EAI_NONAME)
294 #undef EAI_NODATA
295 #define EAI_NODATA WSANO_DATA
296 #endif
297 
298 #if defined(_MSC_VER)
299 /* Hack to suppress compiler warnings on FD_SET() & FD_CLR() */
300 #pragma warning (push)
301 #pragma warning (disable:4142)
302 #endif
303 
304 /* prevent inclusion of wingdi.h */
305 #define NOGDI
306 #include <ws2spi.h>
307 
308 #if defined(_MSC_VER)
309 #pragma warning (pop)
310 #endif
311 
312 #include <io.h>
313 
314 typedef char * caddr_t;
315 
316 #ifndef _PATH_DEVNULL
317 #define _PATH_DEVNULL "NUL"
318 #endif
319 
320 #undef FD_CLOSE
321 #undef FD_OPEN
322 #undef FD_READ
323 #undef FD_WRITE
324 
325 #ifndef EISCONN
326 #define EISCONN WSAEISCONN
327 #endif
328 #ifndef EINPROGRESS
329 #define EINPROGRESS WSAEINPROGRESS
330 #endif
331 #ifndef EWOULDBLOCK
332 #define EWOULDBLOCK WSAEWOULDBLOCK
333 #endif
334 #ifndef EALREADY
335 #define EALREADY WSAEALREADY
336 #endif
337 #ifndef ETIMEDOUT
338 #define ETIMEDOUT WSAETIMEDOUT
339 #endif
340 #ifndef ECONNREFUSED
341 #define ECONNREFUSED WSAECONNREFUSED
342 #endif
343 #ifndef ECONNRESET
344 #define ECONNRESET WSAECONNRESET
345 #endif
346 #ifndef ENOTCONN
347 #define ENOTCONN WSAENOTCONN
348 #endif
349 #ifndef ERESTART
350 #define ERESTART WSATRY_AGAIN
351 #endif
352 #ifndef EAFNOSUPPORT
353 #define EAFNOSUPPORT WSAEAFNOSUPPORT
354 #endif
355 #ifndef ENETUNREACH
356 #define ENETUNREACH WSAENETUNREACH
357 #endif
358 #ifndef ENOTSUP
359 #define ENOTSUP WSAEOPNOTSUPP
360 #endif
361 #ifndef ECONNABORTED
362 #define ECONNABORTED WSAECONNABORTED
363 #endif
364 
365 #undef h_errno
366 #define h_errno errno /* we'll set it ourselves */
367 
368 #undef FD_CLR
369 #define FD_CLR(fd, set) do { \
370  u_int __i; \
371  SOCKET __sock = _get_osfhandle(fd); \
372  for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count ; __i++) { \
373  if (((fd_set FAR *)(set))->fd_array[__i] == __sock) { \
374  while (__i < ((fd_set FAR *)(set))->fd_count-1) { \
375  ((fd_set FAR *)(set))->fd_array[__i] = \
376  ((fd_set FAR *)(set))->fd_array[__i+1]; \
377  __i++; \
378  } \
379  ((fd_set FAR *)(set))->fd_count--; \
380  break; \
381  } \
382  } \
383 } while(0)
384 
385 #undef FD_SET
386 #define FD_SET(fd, set) do { \
387  u_int __i; \
388  SOCKET __sock = _get_osfhandle(fd); \
389  for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count; __i++) { \
390  if (((fd_set FAR *)(set))->fd_array[__i] == (__sock)) { \
391  break; \
392  } \
393  } \
394  if (__i == ((fd_set FAR *)(set))->fd_count) { \
395  if (((fd_set FAR *)(set))->fd_count < FD_SETSIZE) { \
396  ((fd_set FAR *)(set))->fd_array[__i] = (__sock); \
397  ((fd_set FAR *)(set))->fd_count++; \
398  } \
399  } \
400 } while(0)
401 
402 #undef FD_ISSET
403 #define FD_ISSET(fd, set) Win32__WSAFDIsSet(fd, (fd_set FAR *)(set))
404 
405 /* internal to Microsoft CRTLIB */
406 typedef struct {
407  long osfhnd; /* underlying OS file HANDLE */
408  char osfile; /* attributes of file (e.g., open in text mode?) */
409  char pipech; /* one char buffer for handles opened on pipes */
410 #ifdef _MT
411  int lockinitflag;
412  CRITICAL_SECTION lock;
413 #endif /* _MT */
414 } ioinfo;
415 #define IOINFO_L2E 5
416 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
417 #define _pioinfo(i) ( __pioinfo[(i) >> IOINFO_L2E] + ((i) & (IOINFO_ARRAY_ELTS - 1)) )
418 #define _osfile(i) ( _pioinfo(i)->osfile )
419 #define _osfhnd(i) ( _pioinfo(i)->osfhnd )
420 #if !defined(FOPEN)
421 #define FOPEN 0x01 /* file handle open */
422 #endif
423 
424 #if defined(_MSC_VER)
425 SQUIDCEXTERN _CRTIMP ioinfo * __pioinfo[];
426 SQUIDCEXTERN int __cdecl _free_osfhnd(int);
427 #endif
428 
429 #if _SQUID_MINGW_
430 __MINGW_IMPORT ioinfo * __pioinfo[];
431 SQUIDCEXTERN int _free_osfhnd(int);
432 #endif
433 
434 SQUIDCEXTERN THREADLOCAL int ws32_result;
435 
436 #if defined(__cplusplus)
437 
438 inline int
439 close(int fd)
440 {
441  char l_so_type[sizeof(int)];
442  int l_so_type_siz = sizeof(l_so_type);
443  SOCKET sock = _get_osfhandle(fd);
444 
445  if (::getsockopt(sock, SOL_SOCKET, SO_TYPE, l_so_type, &l_so_type_siz) == 0) {
446  int result = 0;
447  if (closesocket(sock) == SOCKET_ERROR) {
448  errno = WSAGetLastError();
449  result = 1;
450  }
451  _free_osfhnd(fd);
452  _osfile(fd) = 0;
453  return result;
454  } else
455  return _close(fd);
456 }
457 
458 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
459 
460 #ifndef _S_IREAD
461 #define _S_IREAD 0x0100
462 #endif
463 
464 #ifndef _S_IWRITE
465 #define _S_IWRITE 0x0080
466 #endif
467 
468 inline int
469 open(const char *filename, int oflag, int pmode = 0)
470 {
471  return _open(filename, oflag, pmode & (_S_IREAD | _S_IWRITE));
472 }
473 #endif
474 
475 inline int
476 read(int fd, void * buf, size_t siz)
477 {
478  char l_so_type[sizeof(int)];
479  int l_so_type_siz = sizeof(l_so_type);
480  SOCKET sock = _get_osfhandle(fd);
481 
482  if (::getsockopt(sock, SOL_SOCKET, SO_TYPE, l_so_type, &l_so_type_siz) == 0)
483  return ::recv(sock, (char FAR *) buf, (int)siz, 0);
484  else
485  return _read(fd, buf, (unsigned int)siz);
486 }
487 
488 inline int
489 write(int fd, const void * buf, size_t siz)
490 {
491  char l_so_type[sizeof(int)];
492  int l_so_type_siz = sizeof(l_so_type);
493  SOCKET sock = _get_osfhandle(fd);
494 
495  if (::getsockopt(sock, SOL_SOCKET, SO_TYPE, l_so_type, &l_so_type_siz) == 0)
496  return ::send(sock, (char FAR *) buf, siz, 0);
497  else
498  return _write(fd, buf, siz);
499 }
500 
501 inline char *
502 index(const char *s, int c)
503 {
504  return (char *)strchr(s,c);
505 }
506 
507 // stdlib <functional> definitions are required before std API redefinitions.
508 #include <functional>
509 
511 namespace Squid
512 {
515 /*
516  * Each of these functions is defined in the Squid namespace so as not to
517  * clash with the winsock.h and winsock2.h definitions.
518  * It is then paired with a #define to cause these wrappers to be used by
519  * the main code instead of those system definitions.
520  *
521  * We do this wrapper in order to:
522  * - cast the parameter types in only one place, and
523  * - record errors in POSIX errno variable, and
524  * - map the FD value used by Squid to the socket handes used by Windows.
525  */
526 
527 inline int
528 accept(int s, struct sockaddr * a, socklen_t * l)
529 {
530  SOCKET result;
531  if ((result = ::accept(_get_osfhandle(s), a, l)) == INVALID_SOCKET) {
532  if (WSAEMFILE == (errno = WSAGetLastError()))
533  errno = EMFILE;
534  return -1;
535  } else
536  return _open_osfhandle(result, 0);
537 }
538 #define accept(s,a,l) Squid::accept(s,a,reinterpret_cast<socklen_t*>(l))
539 
540 inline int
541 bind(int s, const struct sockaddr * n, socklen_t l)
542 {
543  if (::bind(_get_osfhandle(s),n,l) == SOCKET_ERROR) {
544  errno = WSAGetLastError();
545  return -1;
546  } else
547  return 0;
548 }
549 #define bind(s,n,l) Squid::bind(s,n,l)
550 
551 inline int
552 connect(int s, const struct sockaddr * n, socklen_t l)
553 {
554  if (::connect(_get_osfhandle(s),n,l) == SOCKET_ERROR) {
555  if (WSAEMFILE == (errno = WSAGetLastError()))
556  errno = EMFILE;
557  return -1;
558  } else
559  return 0;
560 }
561 #define connect(s,n,l) Squid::connect(s,n,l)
562 
563 inline struct hostent *
564 gethostbyname(const char *n) {
565  HOSTENT FAR * result;
566  if ((result = ::gethostbyname(n)) == NULL)
567  errno = WSAGetLastError();
568  return result;
569 }
570 #define gethostbyname(n) Squid::gethostbyname(n)
571 
572 inline SERVENT FAR *
573 getservbyname(const char * n, const char * p)
574 {
575  SERVENT FAR * result;
576  if ((result = ::getservbyname(n, p)) == NULL)
577  errno = WSAGetLastError();
578  return result;
579 }
580 #define getservbyname(n,p) Squid::getservbyname(n,p)
581 
582 inline HOSTENT FAR *
583 gethostbyaddr(const void * a, size_t l, int t)
584 {
585  HOSTENT FAR * result;
586  if ((result = ::gethostbyaddr((const char*)a, l, t)) == NULL)
587  errno = WSAGetLastError();
588  return result;
589 }
590 #define gethostbyaddr(a,l,t) Squid::gethostbyaddr(a,l,t)
591 
592 inline int
593 getsockname(int s, struct sockaddr * n, socklen_t * l)
594 {
595  int i=*l;
596  if (::getsockname(_get_osfhandle(s), n, &i) == SOCKET_ERROR) {
597  errno = WSAGetLastError();
598  return -1;
599  } else
600  return 0;
601 }
602 #define getsockname(s,a,l) Squid::getsockname(s,a,reinterpret_cast<socklen_t*>(l))
603 
604 inline int
605 gethostname(char * n, size_t l)
606 {
607  if ((::gethostname(n, l)) == SOCKET_ERROR) {
608  errno = WSAGetLastError();
609  return -1;
610  } else
611  return 0;
612 }
613 #define gethostname(n,l) Squid::gethostname(n,l)
614 
615 inline int
616 getsockopt(int s, int l, int o, void * v, socklen_t * n)
617 {
618  Sleep(1);
619  if ((::getsockopt(_get_osfhandle(s), l, o,(char *) v, n)) == SOCKET_ERROR) {
620  errno = WSAGetLastError();
621  return -1;
622  } else
623  return 0;
624 }
625 #define getsockopt(s,l,o,v,n) Squid::getsockopt(s,l,o,v,n)
626 
627 inline char *
628 inet_ntop(int af, const void *src, char *dst, size_t size)
629 {
630 #if HAVE_DECL_INETNTOPA
631  return (char*)InetNtopA(af, const_cast<void*>(src), dst, size);
632 #else
633  return ::inet_ntop(af, src, dst, size);
634 #endif
635 }
636 #define inet_ntop(a,s,d,l) Squid::inet_ntop(a,s,d,l)
637 
638 inline char *
639 inet_pton(int af, const void *src, char *dst)
640 {
641 #if HAVE_DECL_INETPTONA
642  return (char*)InetPtonA(af, const_cast<void*>(src), dst);
643 #else
644  return ::inet_pton(af, src, dst);
645 #endif
646 }
647 #define inet_pton(a,s,d) Squid::inet_pton(a,s,d)
648 
649 /* Simple ioctl() emulation */
650 inline int
651 ioctl(int s, int c, void * a)
652 {
653  if ((::ioctlsocket(_get_osfhandle(s), c, (u_long FAR *)a)) == SOCKET_ERROR) {
654  errno = WSAGetLastError();
655  return -1;
656  } else
657  return 0;
658 }
659 #define ioctl(s,c,a) Squid::ioctl(s,c,a)
660 
661 inline int
662 ioctlsocket(int s, long c, u_long FAR * a)
663 {
664  if ((::ioctlsocket(_get_osfhandle(s), c, a)) == SOCKET_ERROR) {
665  errno = WSAGetLastError();
666  return -1;
667  } else
668  return 0;
669 }
670 #define ioctlsocket(s,c,a) Squid::ioctlsocket(s,c,a)
671 
672 inline int
673 listen(int s, int b)
674 {
675  if (::listen(_get_osfhandle(s), b) == SOCKET_ERROR) {
676  if (WSAEMFILE == (errno = WSAGetLastError()))
677  errno = EMFILE;
678  return -1;
679  } else
680  return 0;
681 }
682 #define listen(s,b) Squid::listen(s,b)
683 
684 inline ssize_t
685 recv(int s, void * b, size_t l, int f)
686 {
687  ssize_t result;
688  if ((result = ::recv(_get_osfhandle(s), (char *)b, l, f)) == SOCKET_ERROR) {
689  errno = WSAGetLastError();
690  return -1;
691  } else
692  return result;
693 }
694 #define recv(s,b,l,f) Squid::recv(s,b,l,f)
695 
696 inline ssize_t
697 recvfrom(int s, void * b, size_t l, int f, struct sockaddr * fr, socklen_t * fl)
698 {
699  ssize_t result;
700  int ifl=*fl;
701  if ((result = ::recvfrom(_get_osfhandle(s), (char *)b, l, f, fr, &ifl)) == SOCKET_ERROR) {
702  errno = WSAGetLastError();
703  return -1;
704  } else
705  return result;
706 }
707 #define recvfrom(s,b,l,f,r,n) Squid::recvfrom(s,b,l,f,r,reinterpret_cast<socklen_t*>(n))
708 
709 inline int
710 select(int n, fd_set * r, fd_set * w, fd_set * e, struct timeval * t)
711 {
712  int result;
713  if ((result = ::select(n,r,w,e,t)) == SOCKET_ERROR) {
714  errno = WSAGetLastError();
715  return -1;
716  } else
717  return result;
718 }
719 #define select(n,r,w,e,t) Squid::select(n,r,w,e,t)
720 
721 inline ssize_t
722 send(int s, const char * b, size_t l, int f)
723 {
724  ssize_t result;
725  if ((result = ::send(_get_osfhandle(s), b, l, f)) == SOCKET_ERROR) {
726  errno = WSAGetLastError();
727  return -1;
728  } else
729  return result;
730 }
731 #define send(s,b,l,f) Squid::send(s,reinterpret_cast<const char*>(b),l,f)
732 
733 inline ssize_t
734 sendto(int s, const void * b, size_t l, int f, const struct sockaddr * t, socklen_t tl)
735 {
736  ssize_t result;
737  if ((result = ::sendto(_get_osfhandle(s), (char *)b, l, f, t, tl)) == SOCKET_ERROR) {
738  errno = WSAGetLastError();
739  return -1;
740  } else
741  return result;
742 }
743 #define sendto(a,b,l,f,t,n) Squid::sendto(a,b,l,f,t,n)
744 
745 inline int
746 setsockopt(SOCKET s, int l, int o, const void * v, socklen_t n)
747 {
748  SOCKET socket;
749 
750  socket = ((s == INVALID_SOCKET) ? s : (SOCKET)_get_osfhandle((int)s));
751 
752  if (::setsockopt(socket, l, o, (const char *)v, n) == SOCKET_ERROR) {
753  errno = WSAGetLastError();
754  return -1;
755  } else
756  return 0;
757 }
758 #define setsockopt(s,l,o,v,n) Squid::setsockopt(s,l,o,v,n)
759 
760 inline int
761 shutdown(int s, int h)
762 {
763  if (::shutdown(_get_osfhandle(s),h) == SOCKET_ERROR) {
764  errno = WSAGetLastError();
765  return -1;
766  } else
767  return 0;
768 }
769 #define shutdown(s,h) Squid::shutdown(s,h)
770 
771 inline int
772 socket(int f, int t, int p)
773 {
774  SOCKET result;
775  if ((result = ::socket(f, t, p)) == INVALID_SOCKET) {
776  if (WSAEMFILE == (errno = WSAGetLastError()))
777  errno = EMFILE;
778  return -1;
779  } else
780  return _open_osfhandle(result, 0);
781 }
782 #define socket(f,t,p) Squid::socket(f,t,p)
783 
784 inline int
785 pipe(int pipefd[2])
786 {
787  return _pipe(pipefd,4096,_O_BINARY);
788 }
789 #define pipe(a) Squid::pipe(a)
790 
791 inline int
792 WSAAsyncSelect(int s, HWND h, unsigned int w, long e)
793 {
794  if (::WSAAsyncSelect(_get_osfhandle(s), h, w, e) == SOCKET_ERROR) {
795  errno = WSAGetLastError();
796  return -1;
797  } else
798  return 0;
799 }
800 #define WSAAsyncSelect(s,h,w,e) Squid::WSAAsyncSelect(s,h,w,e)
801 
802 #undef WSADuplicateSocket
803 inline int
804 WSADuplicateSocket(int s, DWORD n, LPWSAPROTOCOL_INFO l)
805 {
806 #ifdef UNICODE
807  if (::WSADuplicateSocketW(_get_osfhandle(s), n, l) == SOCKET_ERROR) {
808 #else
809  if (::WSADuplicateSocketA(_get_osfhandle(s), n, l) == SOCKET_ERROR) {
810 #endif
811  errno = WSAGetLastError();
812  return -1;
813  } else
814  return 0;
815 }
816 #define WSADuplicateSocket(s,n,l) Squid::WSADuplicateSocket(s,n,l)
817 
818 #undef WSASocket
819 inline int
820 WSASocket(int a, int t, int p, LPWSAPROTOCOL_INFO i, GROUP g, DWORD f)
821 {
822  SOCKET result;
823 #ifdef UNICODE
824  if ((result = ::WSASocketW(a, t, p, i, g, f)) == INVALID_SOCKET) {
825 #else
826  if ((result = ::WSASocketA(a, t, p, i, g, f)) == INVALID_SOCKET) {
827 #endif
828  if (WSAEMFILE == (errno = WSAGetLastError()))
829  errno = EMFILE;
830  return -1;
831  } else
832  return _open_osfhandle(result, 0);
833 }
834 #define WSASocket(a,t,p,i,g,f) Squid::WSASocket(a,t,p,i,g,f)
835 
836 } /* namespace Squid */
837 
838 #else /* #ifdef __cplusplus */
839 #define connect(s,n,l) \
840  (SOCKET_ERROR == connect(_get_osfhandle(s),n,l) ? \
841  (WSAEMFILE == (errno = WSAGetLastError()) ? errno = EMFILE : -1, -1) : 0)
842 #define gethostbyname(n) \
843  (NULL == ((HOSTENT FAR*)(ws32_result = (int)gethostbyname(n))) ? \
844  (errno = WSAGetLastError()), (HOSTENT FAR*)NULL : (HOSTENT FAR*)ws32_result)
845 #define gethostname(n,l) \
846  (SOCKET_ERROR == gethostname(n,l) ? \
847  (errno = WSAGetLastError()), -1 : 0)
848 #define recv(s,b,l,f) \
849  (SOCKET_ERROR == (ws32_result = recv(_get_osfhandle(s),b,l,f)) ? \
850  (errno = WSAGetLastError()), -1 : ws32_result)
851 #define sendto(s,b,l,f,t,tl) \
852  (SOCKET_ERROR == (ws32_result = sendto(_get_osfhandle(s),b,l,f,t,tl)) ? \
853  (errno = WSAGetLastError()), -1 : ws32_result)
854 #define select(n,r,w,e,t) \
855  (SOCKET_ERROR == (ws32_result = select(n,r,w,e,t)) ? \
856  (errno = WSAGetLastError()), -1 : ws32_result)
857 #define socket(f,t,p) \
858  (INVALID_SOCKET == ((SOCKET)(ws32_result = (int)socket(f,t,p))) ? \
859  ((WSAEMFILE == (errno = WSAGetLastError()) ? errno = EMFILE : -1), -1) : \
860  (SOCKET)_open_osfhandle(ws32_result,0))
861 #define write _write /* Needed in util.c */
862 #define open _open /* Needed in win32lib.c */
863 #endif /* #ifdef __cplusplus */
864 
865 /* provide missing definitions from resoruce.h */
866 /* NP: sys/resource.h and sys/time.h are apparently order-dependant. */
867 #if HAVE_SYS_TIME_H
868 #include <sys/time.h>
869 #endif
870 #if HAVE_SYS_RESOURCE_H
871 #include <sys/resource.h>
872 #else
873 #define RUSAGE_SELF 0 /* calling process */
874 #define RUSAGE_CHILDREN -1 /* terminated child processes */
875 
876 struct rusage {
877  struct timeval ru_utime; /* user time used */
878  struct timeval ru_stime; /* system time used */
879  long ru_maxrss; /* integral max resident set size */
880  long ru_ixrss; /* integral shared text memory size */
881  long ru_idrss; /* integral unshared data size */
882  long ru_isrss; /* integral unshared stack size */
883  long ru_minflt; /* page reclaims */
884  long ru_majflt; /* page faults */
885  long ru_nswap; /* swaps */
886  long ru_inblock; /* block input operations */
887  long ru_oublock; /* block output operations */
888  long ru_msgsnd; /* messages sent */
889  long ru_msgrcv; /* messages received */
890  long ru_nsignals; /* signals received */
891  long ru_nvcsw; /* voluntary context switches */
892  long ru_nivcsw; /* involuntary context switches */
893 };
894 #endif /* HAVE_SYS_RESOURCE_H */
895 
896 #undef ACL
897 
898 SQUIDCEXTERN int chroot(const char *dirname);
899 SQUIDCEXTERN int kill(pid_t, int);
900 SQUIDCEXTERN struct passwd * getpwnam(char *unused);
901 SQUIDCEXTERN struct group * getgrnam(char *unused);
902 
903 static inline uid_t
904 geteuid(void)
905 {
906  return 100;
907 }
908 static inline int
909 seteuid (uid_t euid)
910 {
911  return 0;
912 }
913 static inline uid_t
914 getuid(void)
915 {
916  return 100;
917 }
918 static inline int
919 setuid (uid_t uid)
920 {
921  return 0;
922 }
923 static inline gid_t
924 getegid(void)
925 {
926  return 100;
927 }
928 static inline int
929 setegid (gid_t egid)
930 {
931  return 0;
932 }
933 static inline int
934 getgid(void)
935 {
936  return 100;
937 }
938 static inline int
939 setgid (gid_t gid)
940 {
941  return 0;
942 }
943 
944 /* for some reason autoconf misdetects getpagesize.. */
945 #if HAVE_GETPAGESIZE && _SQUID_MINGW_
946 #undef HAVE_GETPAGESIZE
947 #endif
948 
949 #if !HAVE_GETPAGESIZE
950 /* And now we define a compatibility layer */
951 size_t getpagesize();
952 #define HAVE_GETPAGESIZE 2
953 #endif
954 
955 SQUIDCEXTERN void WIN32_ExceptionHandlerInit(void);
956 SQUIDCEXTERN int Win32__WSAFDIsSet(int fd, fd_set* set);
958 
959 /* gcc doesn't recognize the Windows native 64 bit formatting tags causing
960  * the compile fail, so we must disable the check on native Windows.
961  */
962 #if __GNUC__
963 #define PRINTF_FORMAT_ARG1
964 #define PRINTF_FORMAT_ARG2
965 #define PRINTF_FORMAT_ARG3
966 #endif
967 
968 /* XXX: the logic around this is a bit warped:
969  * we #define ACL unconditionally at the top of this file,
970  * then #undef ACL unconditionally hafway down,
971  * then here re-define ACL to the same value as at the top,
972  * then include windows.h and #undef ACL again.
973  */
974 #ifndef ACL
975 #define ACL WindowsACL
976 #define _MSWIN_ACL_WAS_NOT_DEFINED 1
977 #endif
978 #include <windows.h>
979 #if _MSWIN_ACL_WAS_NOT_DEFINED
980 #undef ACL
981 #undef _MSWIN_ACL_WAS_NOT_DEFINED
982 #endif
983 
984 #if !HAVE_SYSLOG
985 /* syslog compatibility layer derives from git */
986 #define LOG_PID 0x01
987 #define LOG_EMERG 0
988 #define LOG_ALERT 1
989 #define LOG_CRIT 2
990 #define LOG_ERR 3
991 #define LOG_WARNING 4
992 #define LOG_NOTICE 5
993 #define LOG_INFO 6
994 #define LOG_DEBUG 7
995 #define LOG_DAEMON (3<<3)
996 
997 void openlog(const char *ident, int logopt, int facility);
998 void syslog(int priority, const char *fmt, ...);
999 #endif
1000 
1001 #if _SQUID_MINGW_
1002 /* MinGW missing bits from sys/wait.h */
1003 /* A status looks like:
1004  * <2 bytes info> <2 bytes code>
1005  *
1006  * <code> == 0, child has exited, info is the exit value
1007  * <code> == 1..7e, child has exited, info is the signal number.
1008  * <code> == 7f, child has stopped, info was the signal number.
1009  * <code> == 80, there was a core dump.
1010  */
1011 #define WIFEXITED(w) (((w) & 0xff) == 0)
1012 #define WIFSIGNALED(w) (((w) & 0x7f) > 0 && (((w) & 0x7f) < 0x7f))
1013 #define WIFSTOPPED(w) (((w) & 0xff) == 0x7f)
1014 #define WEXITSTATUS(w) (((w) >> 8) & 0xff)
1015 #define WTERMSIG(w) ((w) & 0x7f)
1016 #define WSTOPSIG WEXITSTATUS
1017 #endif
1018 
1019 /* prototypes */
1020 void WIN32_maperror(unsigned long WIN32_oserrno);
1021 
1022 #endif /* _SQUID_WINDOWS_ */
1023 #endif /* SQUID_OS_MSWINDOWS_H */
1024 
int socklen_t
Definition: types.h:158
struct hostent * gethostbyaddr()
#define SQUIDCEXTERN
Definition: squid.h:26
#define inet_pton
Definition: inet_pton.h:28
struct servent * getservbyname()
#define inet_ntop
Definition: inet_ntop.h:25
char boolean
Definition: GnuRegex.c:231
bool SIGHDLR int STUB void int
Definition: stub_tools.cc:68
struct hostent * gethostbyname()
DWORD WIN32_IpAddrChangeMonitorInit()
Definition: WinSvc.cc:441
#define NULL
Definition: types.h:166

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors