mswindows.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 /*
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 _PATH_DEVNULL
284 #define _PATH_DEVNULL "NUL"
285 #endif
286 
287 #ifndef EISCONN
288 #define EISCONN WSAEISCONN
289 #endif
290 #ifndef EINPROGRESS
291 #define EINPROGRESS WSAEINPROGRESS
292 #endif
293 #ifndef EWOULDBLOCK
294 #define EWOULDBLOCK WSAEWOULDBLOCK
295 #endif
296 #ifndef EALREADY
297 #define EALREADY WSAEALREADY
298 #endif
299 #ifndef ETIMEDOUT
300 #define ETIMEDOUT WSAETIMEDOUT
301 #endif
302 #ifndef ECONNREFUSED
303 #define ECONNREFUSED WSAECONNREFUSED
304 #endif
305 #ifndef ECONNRESET
306 #define ECONNRESET WSAECONNRESET
307 #endif
308 #ifndef ENOTCONN
309 #define ENOTCONN WSAENOTCONN
310 #endif
311 #ifndef ERESTART
312 #define ERESTART WSATRY_AGAIN
313 #endif
314 #ifndef EAFNOSUPPORT
315 #define EAFNOSUPPORT WSAEAFNOSUPPORT
316 #endif
317 #ifndef ENETUNREACH
318 #define ENETUNREACH WSAENETUNREACH
319 #endif
320 #ifndef ENOTSUP
321 #define ENOTSUP WSAEOPNOTSUPP
322 #endif
323 #ifndef ECONNABORTED
324 #define ECONNABORTED WSAECONNABORTED
325 #endif
326 
327 #undef h_errno
328 #define h_errno errno /* we'll set it ourselves */
329 
330 #undef FD_CLR
331 #define FD_CLR(fd, set) do { \
332  u_int __i; \
333  SOCKET __sock = _get_osfhandle(fd); \
334  for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count ; __i++) { \
335  if (((fd_set FAR *)(set))->fd_array[__i] == __sock) { \
336  while (__i < ((fd_set FAR *)(set))->fd_count-1) { \
337  ((fd_set FAR *)(set))->fd_array[__i] = \
338  ((fd_set FAR *)(set))->fd_array[__i+1]; \
339  __i++; \
340  } \
341  ((fd_set FAR *)(set))->fd_count--; \
342  break; \
343  } \
344  } \
345 } while(0)
346 
347 #undef FD_SET
348 #define FD_SET(fd, set) do { \
349  u_int __i; \
350  SOCKET __sock = _get_osfhandle(fd); \
351  for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count; __i++) { \
352  if (((fd_set FAR *)(set))->fd_array[__i] == (__sock)) { \
353  break; \
354  } \
355  } \
356  if (__i == ((fd_set FAR *)(set))->fd_count) { \
357  if (((fd_set FAR *)(set))->fd_count < FD_SETSIZE) { \
358  ((fd_set FAR *)(set))->fd_array[__i] = (__sock); \
359  ((fd_set FAR *)(set))->fd_count++; \
360  } \
361  } \
362 } while(0)
363 
364 #undef FD_ISSET
365 #define FD_ISSET(fd, set) Win32__WSAFDIsSet(fd, (fd_set FAR *)(set))
366 
367 /* internal to Microsoft CRTLIB */
368 typedef struct {
369  long osfhnd; /* underlying OS file HANDLE */
370  char osfile; /* attributes of file (e.g., open in text mode?) */
371  char pipech; /* one char buffer for handles opened on pipes */
372 #ifdef _MT
373  int lockinitflag;
374  CRITICAL_SECTION lock;
375 #endif /* _MT */
376 } ioinfo;
377 #define IOINFO_L2E 5
378 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
379 #define _pioinfo(i) ( __pioinfo[(i) >> IOINFO_L2E] + ((i) & (IOINFO_ARRAY_ELTS - 1)) )
380 #define _osfile(i) ( _pioinfo(i)->osfile )
381 #define _osfhnd(i) ( _pioinfo(i)->osfhnd )
382 #if !defined(FOPEN)
383 #define FOPEN 0x01 /* file handle open */
384 #endif
385 
386 #if defined(_MSC_VER)
387 SQUIDCEXTERN _CRTIMP ioinfo * __pioinfo[];
388 SQUIDCEXTERN int __cdecl _free_osfhnd(int);
389 #endif
390 
391 SQUIDCEXTERN THREADLOCAL int ws32_result;
392 
393 #if defined(__cplusplus)
394 
395 inline int
396 close(int fd)
397 {
398  char l_so_type[sizeof(int)];
399  int l_so_type_siz = sizeof(l_so_type);
400  SOCKET sock = _get_osfhandle(fd);
401 
402  if (::getsockopt(sock, SOL_SOCKET, SO_TYPE, l_so_type, &l_so_type_siz) == 0) {
403  int result = 0;
404  if (closesocket(sock) == SOCKET_ERROR) {
405  errno = WSAGetLastError();
406  result = 1;
407  }
408  _free_osfhnd(fd);
409  _osfile(fd) = 0;
410  return result;
411  } else
412  return _close(fd);
413 }
414 
415 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
416 
417 #ifndef _S_IREAD
418 #define _S_IREAD 0x0100
419 #endif
420 
421 #ifndef _S_IWRITE
422 #define _S_IWRITE 0x0080
423 #endif
424 
425 inline int
426 open(const char *filename, int oflag, int pmode = 0)
427 {
428  return _open(filename, oflag, pmode & (_S_IREAD | _S_IWRITE));
429 }
430 #endif
431 
432 inline int
433 read(int fd, void * buf, size_t siz)
434 {
435  char l_so_type[sizeof(int)];
436  int l_so_type_siz = sizeof(l_so_type);
437  SOCKET sock = _get_osfhandle(fd);
438 
439  if (::getsockopt(sock, SOL_SOCKET, SO_TYPE, l_so_type, &l_so_type_siz) == 0)
440  return ::recv(sock, (char FAR *) buf, (int)siz, 0);
441  else
442  return _read(fd, buf, (unsigned int)siz);
443 }
444 
445 inline int
446 write(int fd, const void * buf, size_t siz)
447 {
448  char l_so_type[sizeof(int)];
449  int l_so_type_siz = sizeof(l_so_type);
450  SOCKET sock = _get_osfhandle(fd);
451 
452  if (::getsockopt(sock, SOL_SOCKET, SO_TYPE, l_so_type, &l_so_type_siz) == 0)
453  return ::send(sock, (char FAR *) buf, siz, 0);
454  else
455  return _write(fd, buf, siz);
456 }
457 
458 // stdlib <functional> definitions are required before std API redefinitions.
459 #include <functional>
460 
462 namespace Squid
463 {
466 /*
467  * Each of these functions is defined in the Squid namespace so as not to
468  * clash with the winsock2.h definitions.
469  * It is then paired with a #define to cause these wrappers to be used by
470  * the main code instead of those system definitions.
471  *
472  * We do this wrapper in order to:
473  * - cast the parameter types in only one place, and
474  * - record errors in POSIX errno variable, and
475  * - map the FD value used by Squid to the socket handes used by Windows.
476  */
477 
478 inline int
479 accept(int s, struct sockaddr * a, socklen_t * l)
480 {
481  SOCKET result;
482  if ((result = ::accept(_get_osfhandle(s), a, l)) == INVALID_SOCKET) {
483  if (WSAEMFILE == (errno = WSAGetLastError()))
484  errno = EMFILE;
485  return -1;
486  } else
487  return _open_osfhandle(result, 0);
488 }
489 #define accept(s,a,l) Squid::accept(s,a,reinterpret_cast<socklen_t*>(l))
490 
491 inline int
492 bind(int s, const struct sockaddr * n, socklen_t l)
493 {
494  if (::bind(_get_osfhandle(s),n,l) == SOCKET_ERROR) {
495  errno = WSAGetLastError();
496  return -1;
497  } else
498  return 0;
499 }
500 #define bind(s,n,l) Squid::bind(s,n,l)
501 
502 inline int
503 connect(int s, const struct sockaddr * n, socklen_t l)
504 {
505  if (::connect(_get_osfhandle(s),n,l) == SOCKET_ERROR) {
506  if (WSAEMFILE == (errno = WSAGetLastError()))
507  errno = EMFILE;
508  return -1;
509  } else
510  return 0;
511 }
512 #define connect(s,n,l) Squid::connect(s,n,l)
513 
514 inline struct hostent *
515 gethostbyname(const char *n) {
516  HOSTENT FAR * result;
517  if ((result = ::gethostbyname(n)) == NULL)
518  errno = WSAGetLastError();
519  return result;
520 }
521 #define gethostbyname(n) Squid::gethostbyname(n)
522 
523 inline SERVENT FAR *
524 getservbyname(const char * n, const char * p)
525 {
526  SERVENT FAR * result;
527  if ((result = ::getservbyname(n, p)) == NULL)
528  errno = WSAGetLastError();
529  return result;
530 }
531 #define getservbyname(n,p) Squid::getservbyname(n,p)
532 
533 inline HOSTENT FAR *
534 gethostbyaddr(const void * a, size_t l, int t)
535 {
536  HOSTENT FAR * result;
537  if ((result = ::gethostbyaddr((const char*)a, l, t)) == NULL)
538  errno = WSAGetLastError();
539  return result;
540 }
541 #define gethostbyaddr(a,l,t) Squid::gethostbyaddr(a,l,t)
542 
543 inline int
544 getsockname(int s, struct sockaddr * n, socklen_t * l)
545 {
546  int i=*l;
547  if (::getsockname(_get_osfhandle(s), n, &i) == SOCKET_ERROR) {
548  errno = WSAGetLastError();
549  return -1;
550  } else
551  return 0;
552 }
553 #define getsockname(s,a,l) Squid::getsockname(s,a,reinterpret_cast<socklen_t*>(l))
554 
555 inline int
556 gethostname(char * n, size_t l)
557 {
558  if ((::gethostname(n, l)) == SOCKET_ERROR) {
559  errno = WSAGetLastError();
560  return -1;
561  } else
562  return 0;
563 }
564 #define gethostname(n,l) Squid::gethostname(n,l)
565 
566 inline int
567 getsockopt(int s, int l, int o, void * v, socklen_t * n)
568 {
569  Sleep(1);
570  if ((::getsockopt(_get_osfhandle(s), l, o,(char *) v, n)) == SOCKET_ERROR) {
571  errno = WSAGetLastError();
572  return -1;
573  } else
574  return 0;
575 }
576 #define getsockopt(s,l,o,v,n) Squid::getsockopt(s,l,o,v,n)
577 
578 /* Simple ioctl() emulation */
579 inline int
580 ioctl(int s, int c, void * a)
581 {
582  if ((::ioctlsocket(_get_osfhandle(s), c, (u_long FAR *)a)) == SOCKET_ERROR) {
583  errno = WSAGetLastError();
584  return -1;
585  } else
586  return 0;
587 }
588 #define ioctl(s,c,a) Squid::ioctl(s,c,a)
589 
590 inline int
591 ioctlsocket(int s, long c, u_long FAR * a)
592 {
593  if ((::ioctlsocket(_get_osfhandle(s), c, a)) == SOCKET_ERROR) {
594  errno = WSAGetLastError();
595  return -1;
596  } else
597  return 0;
598 }
599 #define ioctlsocket(s,c,a) Squid::ioctlsocket(s,c,a)
600 
601 inline int
602 listen(int s, int b)
603 {
604  if (::listen(_get_osfhandle(s), b) == SOCKET_ERROR) {
605  if (WSAEMFILE == (errno = WSAGetLastError()))
606  errno = EMFILE;
607  return -1;
608  } else
609  return 0;
610 }
611 #define listen(s,b) Squid::listen(s,b)
612 
613 inline ssize_t
614 recv(int s, void * b, size_t l, int f)
615 {
616  ssize_t result;
617  if ((result = ::recv(_get_osfhandle(s), (char *)b, l, f)) == SOCKET_ERROR) {
618  errno = WSAGetLastError();
619  return -1;
620  } else
621  return result;
622 }
623 #define recv(s,b,l,f) Squid::recv(s,b,l,f)
624 
625 inline ssize_t
626 recvfrom(int s, void * b, size_t l, int f, struct sockaddr * fr, socklen_t * fl)
627 {
628  ssize_t result;
629  int ifl=*fl;
630  if ((result = ::recvfrom(_get_osfhandle(s), (char *)b, l, f, fr, &ifl)) == SOCKET_ERROR) {
631  errno = WSAGetLastError();
632  return -1;
633  } else
634  return result;
635 }
636 #define recvfrom(s,b,l,f,r,n) Squid::recvfrom(s,b,l,f,r,reinterpret_cast<socklen_t*>(n))
637 
638 inline int
639 select(int n, fd_set * r, fd_set * w, fd_set * e, struct timeval * t)
640 {
641  int result;
642  if ((result = ::select(n,r,w,e,t)) == SOCKET_ERROR) {
643  errno = WSAGetLastError();
644  return -1;
645  } else
646  return result;
647 }
648 #define select(n,r,w,e,t) Squid::select(n,r,w,e,t)
649 
650 inline ssize_t
651 send(int s, const char * b, size_t l, int f)
652 {
653  ssize_t result;
654  if ((result = ::send(_get_osfhandle(s), b, l, f)) == SOCKET_ERROR) {
655  errno = WSAGetLastError();
656  return -1;
657  } else
658  return result;
659 }
660 #define send(s,b,l,f) Squid::send(s,reinterpret_cast<const char*>(b),l,f)
661 
662 inline ssize_t
663 sendto(int s, const void * b, size_t l, int f, const struct sockaddr * t, socklen_t tl)
664 {
665  ssize_t result;
666  if ((result = ::sendto(_get_osfhandle(s), (char *)b, l, f, t, tl)) == SOCKET_ERROR) {
667  errno = WSAGetLastError();
668  return -1;
669  } else
670  return result;
671 }
672 #define sendto(a,b,l,f,t,n) Squid::sendto(a,b,l,f,t,n)
673 
674 inline int
675 setsockopt(SOCKET s, int l, int o, const void * v, socklen_t n)
676 {
677  SOCKET socket;
678 
679  socket = ((s == INVALID_SOCKET) ? s : (SOCKET)_get_osfhandle((int)s));
680 
681  if (::setsockopt(socket, l, o, (const char *)v, n) == SOCKET_ERROR) {
682  errno = WSAGetLastError();
683  return -1;
684  } else
685  return 0;
686 }
687 #define setsockopt(s,l,o,v,n) Squid::setsockopt(s,l,o,v,n)
688 
689 inline int
690 shutdown(int s, int h)
691 {
692  if (::shutdown(_get_osfhandle(s),h) == SOCKET_ERROR) {
693  errno = WSAGetLastError();
694  return -1;
695  } else
696  return 0;
697 }
698 #define shutdown(s,h) Squid::shutdown(s,h)
699 
700 inline int
701 socket(int f, int t, int p)
702 {
703  SOCKET result;
704  if ((result = ::socket(f, t, p)) == INVALID_SOCKET) {
705  if (WSAEMFILE == (errno = WSAGetLastError()))
706  errno = EMFILE;
707  return -1;
708  } else
709  return _open_osfhandle(result, 0);
710 }
711 #define socket(f,t,p) Squid::socket(f,t,p)
712 
713 inline int
714 pipe(int pipefd[2])
715 {
716  return _pipe(pipefd,4096,_O_BINARY);
717 }
718 #define pipe(a) Squid::pipe(a)
719 
720 inline int
721 WSAAsyncSelect(int s, HWND h, unsigned int w, long e)
722 {
723  if (::WSAAsyncSelect(_get_osfhandle(s), h, w, e) == SOCKET_ERROR) {
724  errno = WSAGetLastError();
725  return -1;
726  } else
727  return 0;
728 }
729 #define WSAAsyncSelect(s,h,w,e) Squid::WSAAsyncSelect(s,h,w,e)
730 
731 #undef WSADuplicateSocket
732 inline int
733 WSADuplicateSocket(int s, DWORD n, LPWSAPROTOCOL_INFO l)
734 {
735 #ifdef UNICODE
736  if (::WSADuplicateSocketW(_get_osfhandle(s), n, l) == SOCKET_ERROR) {
737 #else
738  if (::WSADuplicateSocketA(_get_osfhandle(s), n, l) == SOCKET_ERROR) {
739 #endif
740  errno = WSAGetLastError();
741  return -1;
742  } else
743  return 0;
744 }
745 #define WSADuplicateSocket(s,n,l) Squid::WSADuplicateSocket(s,n,l)
746 
747 #undef WSASocket
748 inline int
749 WSASocket(int a, int t, int p, LPWSAPROTOCOL_INFO i, GROUP g, DWORD f)
750 {
751  SOCKET result;
752 #ifdef UNICODE
753  if ((result = ::WSASocketW(a, t, p, i, g, f)) == INVALID_SOCKET) {
754 #else
755  if ((result = ::WSASocketA(a, t, p, i, g, f)) == INVALID_SOCKET) {
756 #endif
757  if (WSAEMFILE == (errno = WSAGetLastError()))
758  errno = EMFILE;
759  return -1;
760  } else
761  return _open_osfhandle(result, 0);
762 }
763 #define WSASocket(a,t,p,i,g,f) Squid::WSASocket(a,t,p,i,g,f)
764 
765 } /* namespace Squid */
766 
767 #else /* #ifdef __cplusplus */
768 #define connect(s,n,l) \
769  (SOCKET_ERROR == connect(_get_osfhandle(s),n,l) ? \
770  (WSAEMFILE == (errno = WSAGetLastError()) ? errno = EMFILE : -1, -1) : 0)
771 #define gethostbyname(n) \
772  (NULL == ((HOSTENT FAR*)(ws32_result = (int)gethostbyname(n))) ? \
773  (errno = WSAGetLastError()), (HOSTENT FAR*)NULL : (HOSTENT FAR*)ws32_result)
774 #define gethostname(n,l) \
775  (SOCKET_ERROR == gethostname(n,l) ? \
776  (errno = WSAGetLastError()), -1 : 0)
777 #define recv(s,b,l,f) \
778  (SOCKET_ERROR == (ws32_result = recv(_get_osfhandle(s),b,l,f)) ? \
779  (errno = WSAGetLastError()), -1 : ws32_result)
780 #define sendto(s,b,l,f,t,tl) \
781  (SOCKET_ERROR == (ws32_result = sendto(_get_osfhandle(s),b,l,f,t,tl)) ? \
782  (errno = WSAGetLastError()), -1 : ws32_result)
783 #define select(n,r,w,e,t) \
784  (SOCKET_ERROR == (ws32_result = select(n,r,w,e,t)) ? \
785  (errno = WSAGetLastError()), -1 : ws32_result)
786 #define socket(f,t,p) \
787  (INVALID_SOCKET == ((SOCKET)(ws32_result = (int)socket(f,t,p))) ? \
788  ((WSAEMFILE == (errno = WSAGetLastError()) ? errno = EMFILE : -1), -1) : \
789  (SOCKET)_open_osfhandle(ws32_result,0))
790 #define write _write /* Needed in util.c */
791 #define open _open /* Needed in win32lib.c */
792 #endif /* #ifdef __cplusplus */
793 
794 /* provide missing definitions from resoruce.h */
795 /* NP: sys/resource.h and sys/time.h are apparently order-dependant. */
796 #if HAVE_SYS_TIME_H
797 #include <sys/time.h>
798 #endif
799 #if HAVE_SYS_RESOURCE_H
800 #include <sys/resource.h>
801 #else
802 #define RUSAGE_SELF 0 /* calling process */
803 #define RUSAGE_CHILDREN -1 /* terminated child processes */
804 
805 struct rusage {
806  struct timeval ru_utime; /* user time used */
807  struct timeval ru_stime; /* system time used */
808  long ru_maxrss; /* integral max resident set size */
809  long ru_ixrss; /* integral shared text memory size */
810  long ru_idrss; /* integral unshared data size */
811  long ru_isrss; /* integral unshared stack size */
812  long ru_minflt; /* page reclaims */
813  long ru_majflt; /* page faults */
814  long ru_nswap; /* swaps */
815  long ru_inblock; /* block input operations */
816  long ru_oublock; /* block output operations */
817  long ru_msgsnd; /* messages sent */
818  long ru_msgrcv; /* messages received */
819  long ru_nsignals; /* signals received */
820  long ru_nvcsw; /* voluntary context switches */
821  long ru_nivcsw; /* involuntary context switches */
822 };
823 #endif /* HAVE_SYS_RESOURCE_H */
824 
825 #undef ACL
826 
827 SQUIDCEXTERN int chroot(const char *dirname);
828 SQUIDCEXTERN int kill(pid_t, int);
829 SQUIDCEXTERN struct passwd * getpwnam(char *unused);
830 SQUIDCEXTERN struct group * getgrnam(char *unused);
831 
832 static inline uid_t
833 geteuid(void)
834 {
835  return 100;
836 }
837 static inline int
838 seteuid (uid_t euid)
839 {
840  return 0;
841 }
842 static inline uid_t
843 getuid(void)
844 {
845  return 100;
846 }
847 static inline int
848 setuid (uid_t uid)
849 {
850  return 0;
851 }
852 static inline gid_t
853 getegid(void)
854 {
855  return 100;
856 }
857 static inline int
858 setegid (gid_t egid)
859 {
860  return 0;
861 }
862 static inline int
863 getgid(void)
864 {
865  return 100;
866 }
867 static inline int
868 setgid (gid_t gid)
869 {
870  return 0;
871 }
872 
873 #if !HAVE_GETPAGESIZE
874 /* And now we define a compatibility layer */
875 size_t getpagesize();
876 #define HAVE_GETPAGESIZE 2
877 #endif
878 
879 SQUIDCEXTERN void WIN32_ExceptionHandlerInit(void);
880 SQUIDCEXTERN int Win32__WSAFDIsSet(int fd, fd_set* set);
882 
883 /* XXX: the logic around this is a bit warped:
884  * we #define ACL unconditionally at the top of this file,
885  * then #undef ACL unconditionally hafway down,
886  * then here re-define ACL to the same value as at the top,
887  * then include windows.h and #undef ACL again.
888  */
889 #ifndef ACL
890 #define ACL WindowsACL
891 #define _MSWIN_ACL_WAS_NOT_DEFINED 1
892 #endif
893 #include <windows.h>
894 #if _MSWIN_ACL_WAS_NOT_DEFINED
895 #undef ACL
896 #undef _MSWIN_ACL_WAS_NOT_DEFINED
897 #endif
898 
899 #if !HAVE_SYSLOG
900 /* syslog compatibility layer derives from git */
901 #define LOG_PID 0x01
902 #define LOG_EMERG 0
903 #define LOG_ALERT 1
904 #define LOG_CRIT 2
905 #define LOG_ERR 3
906 #define LOG_WARNING 4
907 #define LOG_NOTICE 5
908 #define LOG_INFO 6
909 #define LOG_DEBUG 7
910 #define LOG_DAEMON (3<<3)
911 
912 void openlog(const char *ident, int logopt, int facility);
913 void syslog(int priority, const char *fmt, ...);
914 #endif
915 
916 /* prototypes */
917 void WIN32_maperror(unsigned long WIN32_oserrno);
918 
919 #endif /* _SQUID_WINDOWS_ */
920 #endif /* SQUID_COMPAT_OS_MSWINDOWS_H */
921 
DWORD WIN32_IpAddrChangeMonitorInit()
int socklen_t
Definition: types.h:137
int size
Definition: ModDevPoll.cc:69
#define NULL
Definition: types.h:145
#define SQUIDCEXTERN
Definition: squid.h:21
int unsigned int
Definition: stub_fd.cc:19

 

Introduction

Documentation

Support

Miscellaneous