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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors