2 * AUTHOR: Andrey Shorin <tolsty@tushino.com>
3 * AUTHOR: Guido Serassio <serassio@squid-cache.org>
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 #ifndef SQUID_OS_MSWINDOWS_H
33 #define SQUID_OS_MSWINDOWS_H
37 #define ACL WindowsACL
38 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
40 #define _CRT_SECURE_NO_DEPRECATE
41 #pragma warning( disable : 4290 )
42 #pragma warning( disable : 4996 )
46 /* Some MinGW version defines min() and max() as macros
47 causing the fail of the build process. The following
48 #define will disable that definition
54 #if defined _FILE_OFFSET_BITS && _FILE_OFFSET_BITS == 64
55 # define __USE_FILE_OFFSET64 1
58 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
60 #if defined __USE_FILE_OFFSET64
61 typedef uint64_t ino_t
;
63 typedef unsigned long ino_t
;
66 #define INT64_MAX _I64_MAX
67 #define INT64_MIN _I64_MIN
69 #include "default_config_file.h"
70 /* Some tricks for MS Compilers */
72 #define THREADLOCAL __declspec(thread)
74 #elif defined(__GNUC__) /* gcc environment */
76 #define THREADLOCAL __attribute__((section(".tls")))
80 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
81 #define alloca _alloca
86 #define fdopen _fdopen
87 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
88 #define fileno _fileno
89 #define fstat _fstati64
91 #if !defined(_SQUID_MINGW_) // MinGW defines these properly
92 SQUIDCEXTERN
int WIN32_ftruncate(int fd
, off_t size
);
93 #define ftruncate WIN32_ftruncate
94 SQUIDCEXTERN
int WIN32_truncate(const char *pathname
, off_t length
);
95 #define truncate WIN32_truncate
97 #define getcwd _getcwd
98 #define getpid _getpid
99 #define getrusage WIN32_getrusage
100 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
101 #define lseek _lseeki64
102 #define memccpy _memccpy
103 #define mkdir(p,F) _mkdir((p))
104 #define mktemp _mktemp
107 #define mkdir(p,F) mkdir((p))
109 #define pclose _pclose
111 #define putenv _putenv
112 #define setmode _setmode
113 #define sleep(t) Sleep((t)*1000)
114 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
115 #define snprintf _snprintf
116 #define stat _stati64
117 #define strcasecmp _stricmp
118 #define strdup _strdup
119 #define strlwr _strlwr
120 #define strncasecmp _strnicmp
121 #define tempnam _tempnam
124 #define unlink _unlink
125 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
126 #define vsnprintf _vsnprintf
129 #define O_RDONLY _O_RDONLY
130 #define O_WRONLY _O_WRONLY
131 #define O_RDWR _O_RDWR
132 #define O_APPEND _O_APPEND
134 #define O_CREAT _O_CREAT
135 #define O_TRUNC _O_TRUNC
136 #define O_EXCL _O_EXCL
138 #define O_TEXT _O_TEXT
139 #define O_BINARY _O_BINARY
140 #define O_RAW _O_BINARY
141 #define O_TEMPORARY _O_TEMPORARY
142 #define O_NOINHERIT _O_NOINHERIT
143 #define O_SEQUENTIAL _O_SEQUENTIAL
144 #define O_RANDOM _O_RANDOM
147 #define S_IFMT _S_IFMT
148 #define S_IFDIR _S_IFDIR
149 #define S_IFCHR _S_IFCHR
150 #define S_IFREG _S_IFREG
151 #define S_IREAD _S_IREAD
152 #define S_IWRITE _S_IWRITE
153 #define S_IEXEC _S_IEXEC
156 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
157 #define S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR)
160 #define SIGHUP 1 /* hangup */
161 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
162 #define SIGBUS 10 /* bus error */
163 #define SIGPIPE 13 /* write on a pipe with no one to read it */
164 #define SIGCHLD 20 /* to parent on child stop or exit */
165 #define SIGUSR1 30 /* user defined signal 1 */
166 #define SIGUSR2 31 /* user defined signal 2 */
174 char *pw_name
; /* user name */
175 char *pw_passwd
; /* user password */
176 uid_t pw_uid
; /* user id */
177 gid_t pw_gid
; /* group id */
178 char *pw_gecos
; /* real name */
179 char *pw_dir
; /* home directory */
180 char *pw_shell
; /* shell program */
184 char *gr_name
; /* group name */
185 char *gr_passwd
; /* group password */
186 gid_t gr_gid
; /* group id */
187 char **gr_mem
; /* group members */
191 long f_type
; /* type of filesystem (see below) */
192 long f_bsize
; /* optimal transfer block size */
193 long f_blocks
; /* total data blocks in file system */
194 long f_bfree
; /* free blocks in fs */
195 long f_bavail
; /* free blocks avail to non-superuser */
196 long f_files
; /* total file nodes in file system */
197 long f_ffree
; /* free file nodes in fs */
198 long f_fsid
; /* file system id */
199 long f_namelen
; /* maximum length of filenames */
200 long f_spare
[6]; /* spare for later */
203 #if !HAVE_GETTIMEOFDAY
205 int tz_minuteswest
; /* minutes west of Greenwich */
206 int tz_dsttime
; /* type of dst correction */
210 #define CHANGE_FD_SETSIZE 1
211 #if CHANGE_FD_SETSIZE && SQUID_MAXFD > DEFAULT_FD_SETSIZE
212 #define FD_SETSIZE SQUID_MAXFD
217 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
218 #include <winsock2.h>
220 #include <ws2tcpip.h>
221 #if (EAI_NODATA == EAI_NONAME)
223 #define EAI_NODATA WSANO_DATA
225 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
226 /* Hack to suppress compiler warnings on FD_SET() & FD_CLR() */
227 #pragma warning (push)
228 #pragma warning (disable:4142)
230 /* prevent inclusion of wingdi.h */
233 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
234 #pragma warning (pop)
238 typedef char * caddr_t
;
240 #ifndef _PATH_DEVNULL
241 #define _PATH_DEVNULL "NUL"
250 #define EISCONN WSAEISCONN
253 #define EINPROGRESS WSAEINPROGRESS
256 #define EWOULDBLOCK WSAEWOULDBLOCK
259 #define EALREADY WSAEALREADY
262 #define ETIMEDOUT WSAETIMEDOUT
265 #define ECONNREFUSED WSAECONNREFUSED
268 #define ECONNRESET WSAECONNRESET
271 #define ENOTCONN WSAENOTCONN
274 #define ERESTART WSATRY_AGAIN
277 #define EAFNOSUPPORT WSAEAFNOSUPPORT
280 #define ENETUNREACH WSAENETUNREACH
284 #define h_errno errno /* we'll set it ourselves */
287 #define FD_CLR(fd, set) do { \
289 SOCKET __sock = _get_osfhandle(fd); \
290 for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count ; __i++) { \
291 if (((fd_set FAR *)(set))->fd_array[__i] == __sock) { \
292 while (__i < ((fd_set FAR *)(set))->fd_count-1) { \
293 ((fd_set FAR *)(set))->fd_array[__i] = \
294 ((fd_set FAR *)(set))->fd_array[__i+1]; \
297 ((fd_set FAR *)(set))->fd_count--; \
304 #define FD_SET(fd, set) do { \
306 SOCKET __sock = _get_osfhandle(fd); \
307 for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count; __i++) { \
308 if (((fd_set FAR *)(set))->fd_array[__i] == (__sock)) { \
312 if (__i == ((fd_set FAR *)(set))->fd_count) { \
313 if (((fd_set FAR *)(set))->fd_count < FD_SETSIZE) { \
314 ((fd_set FAR *)(set))->fd_array[__i] = (__sock); \
315 ((fd_set FAR *)(set))->fd_count++; \
321 #define FD_ISSET(fd, set) Win32__WSAFDIsSet(fd, (fd_set FAR *)(set))
323 /* internal to Microsoft CRTLIB */
325 long osfhnd
; /* underlying OS file HANDLE */
326 char osfile
; /* attributes of file (e.g., open in text mode?) */
327 char pipech
; /* one char buffer for handles opened on pipes */
330 CRITICAL_SECTION lock
;
334 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
335 #define _pioinfo(i) ( __pioinfo[(i) >> IOINFO_L2E] + ((i) & (IOINFO_ARRAY_ELTS - 1)) )
336 #define _osfile(i) ( _pioinfo(i)->osfile )
337 #define _osfhnd(i) ( _pioinfo(i)->osfhnd )
338 #define FOPEN 0x01 /* file handle open */
340 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
342 SQUIDCEXTERN _CRTIMP ioinfo
* __pioinfo
[];
343 SQUIDCEXTERN
int __cdecl
_free_osfhnd(int);
345 #elif _SQUID_MINGW_ /* MinGW environment */
347 __MINGW_IMPORT ioinfo
* __pioinfo
[];
348 SQUIDCEXTERN
int _free_osfhnd(int);
352 SQUIDCEXTERN THREADLOCAL
int ws32_result
;
354 #define strerror(e) WIN32_strerror(e)
355 #define HAVE_STRERROR 1
362 char l_so_type
[sizeof(int)];
363 int l_so_type_siz
= sizeof(l_so_type
);
364 SOCKET sock
= _get_osfhandle(fd
);
366 if (::getsockopt(sock
, SOL_SOCKET
, SO_TYPE
, l_so_type
, &l_so_type_siz
) == 0) {
368 if (closesocket(sock
) == SOCKET_ERROR
) {
369 errno
= WSAGetLastError();
379 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
382 #define _S_IREAD 0x0100
386 #define _S_IWRITE 0x0080
390 int open(const char *filename
, int oflag
, int pmode
= 0)
392 return _open(filename
, oflag
, pmode
& (_S_IREAD
| _S_IWRITE
));
397 int read(int fd
, void * buf
, size_t siz
)
399 char l_so_type
[sizeof(int)];
400 int l_so_type_siz
= sizeof(l_so_type
);
401 SOCKET sock
= _get_osfhandle(fd
);
403 if (::getsockopt(sock
, SOL_SOCKET
, SO_TYPE
, l_so_type
, &l_so_type_siz
) == 0)
404 return ::recv(sock
, (char FAR
*) buf
, (int)siz
, 0);
406 return _read(fd
, buf
, (unsigned int)siz
);
410 int write(int fd
, const void * buf
, size_t siz
)
412 char l_so_type
[sizeof(int)];
413 int l_so_type_siz
= sizeof(l_so_type
);
414 SOCKET sock
= _get_osfhandle(fd
);
416 if (::getsockopt(sock
, SOL_SOCKET
, SO_TYPE
, l_so_type
, &l_so_type_siz
) == 0)
417 return ::send(sock
, (char FAR
*) buf
, siz
, 0);
419 return _write(fd
, buf
, siz
);
423 char *index(const char *s
, int c
)
425 return (char *)strchr(s
,c
);
428 /** \cond AUTODOCS-IGNORE */
434 int accept(int s
, struct sockaddr
* a
, size_t * l
)
437 if ((result
= ::accept(_get_osfhandle(s
), a
, (int *)l
)) == INVALID_SOCKET
) {
438 if (WSAEMFILE
== (errno
= WSAGetLastError()))
442 return _open_osfhandle(result
, 0);
446 int bind(int s
, struct sockaddr
* n
, int l
)
448 if (::bind(_get_osfhandle(s
),n
,l
) == SOCKET_ERROR
) {
449 errno
= WSAGetLastError();
456 int connect(int s
, const struct sockaddr
* n
, int l
)
458 if (::connect(_get_osfhandle(s
),n
,l
) == SOCKET_ERROR
) {
459 if (WSAEMFILE
== (errno
= WSAGetLastError()))
467 struct hostent
* gethostbyname (const char *n
) {
468 HOSTENT FAR
* result
;
469 if ((result
= ::gethostbyname(n
)) == NULL
)
470 errno
= WSAGetLastError();
473 #define gethostbyname(n) Squid::gethostbyname(n)
476 SERVENT FAR
* getservbyname (const char * n
, const char * p
)
478 SERVENT FAR
* result
;
479 if ((result
= ::getservbyname(n
, p
)) == NULL
)
480 errno
= WSAGetLastError();
483 #define getservbyname(n,p) Squid::getservbyname(n,p)
486 HOSTENT FAR
* gethostbyaddr(const char * a
, int l
, int t
)
488 HOSTENT FAR
* result
;
489 if ((result
= ::gethostbyaddr(a
, l
, t
)) == NULL
)
490 errno
= WSAGetLastError();
493 #define gethostbyaddr(a,l,t) Squid::gethostbyaddr(a,l,t)
496 int getsockname(int s
, struct sockaddr
* n
, size_t * l
)
498 if ((::getsockname(_get_osfhandle(s
), n
, (int *)l
)) == SOCKET_ERROR
) {
499 errno
= WSAGetLastError();
506 int gethostname(char * n
, size_t l
)
508 if ((::gethostname(n
, l
)) == SOCKET_ERROR
) {
509 errno
= WSAGetLastError();
514 #define gethostname(n,l) Squid::gethostname(n,l)
517 int getsockopt(int s
, int l
, int o
, void * v
, int * n
)
520 if ((::getsockopt(_get_osfhandle(s
), l
, o
,(char *) v
, n
)) == SOCKET_ERROR
) {
521 errno
= WSAGetLastError();
527 /* Simple ioctl() emulation */
529 int ioctl(int s
, int c
, void * a
)
531 if ((::ioctlsocket(_get_osfhandle(s
), c
, (u_long FAR
*)a
)) == SOCKET_ERROR
) {
532 errno
= WSAGetLastError();
539 int ioctlsocket(int s
, long c
, u_long FAR
* a
)
541 if ((::ioctlsocket(_get_osfhandle(s
), c
, a
)) == SOCKET_ERROR
) {
542 errno
= WSAGetLastError();
549 int listen(int s
, int b
)
551 if (::listen(_get_osfhandle(s
), b
) == SOCKET_ERROR
) {
552 if (WSAEMFILE
== (errno
= WSAGetLastError()))
558 #define listen(s,b) Squid::listen(s,b)
561 int recv(int s
, void * b
, size_t l
, int f
)
564 if ((result
= ::recv(_get_osfhandle(s
), (char *)b
, l
, f
)) == SOCKET_ERROR
) {
565 errno
= WSAGetLastError();
572 int recvfrom(int s
, void * b
, size_t l
, int f
, struct sockaddr
* fr
, size_t * fl
)
575 if ((result
= ::recvfrom(_get_osfhandle(s
), (char *)b
, l
, f
, fr
, (int *)fl
)) == SOCKET_ERROR
) {
576 errno
= WSAGetLastError();
583 int select(int n
, fd_set
* r
, fd_set
* w
, fd_set
* e
, struct timeval
* t
)
586 if ((result
= ::select(n
,r
,w
,e
,t
)) == SOCKET_ERROR
) {
587 errno
= WSAGetLastError();
592 #define select(n,r,w,e,t) Squid::select(n,r,w,e,t)
595 int send(int s
, const void * b
, size_t l
, int f
)
598 if ((result
= ::send(_get_osfhandle(s
), (char *)b
, l
, f
)) == SOCKET_ERROR
) {
599 errno
= WSAGetLastError();
606 int sendto(int s
, const void * b
, size_t l
, int f
, const struct sockaddr
* t
, int tl
)
609 if ((result
= ::sendto(_get_osfhandle(s
), (char *)b
, l
, f
, t
, tl
)) == SOCKET_ERROR
) {
610 errno
= WSAGetLastError();
617 int setsockopt(SOCKET s
, int l
, int o
, const char * v
, int n
)
621 socket
= ((s
== INVALID_SOCKET
) ? s
: (SOCKET
)_get_osfhandle((int)s
));
623 if (::setsockopt(socket
, l
, o
, v
, n
) == SOCKET_ERROR
) {
624 errno
= WSAGetLastError();
629 #define setsockopt(s,l,o,v,n) Squid::setsockopt(s,l,o,v,n)
632 int shutdown(int s
, int h
)
634 if (::shutdown(_get_osfhandle(s
),h
) == SOCKET_ERROR
) {
635 errno
= WSAGetLastError();
642 int socket(int f
, int t
, int p
)
645 if ((result
= ::socket(f
, t
, p
)) == INVALID_SOCKET
) {
646 if (WSAEMFILE
== (errno
= WSAGetLastError()))
650 return _open_osfhandle(result
, 0);
652 #define socket(f,t,p) Squid::socket(f,t,p)
657 return _pipe(pipefd
,4096,_O_BINARY
);
662 int WSAAsyncSelect(int s
, HWND h
, unsigned int w
, long e
)
664 if (::WSAAsyncSelect(_get_osfhandle(s
), h
, w
, e
) == SOCKET_ERROR
) {
665 errno
= WSAGetLastError();
671 #undef WSADuplicateSocket
673 int WSADuplicateSocket(int s
, DWORD n
, LPWSAPROTOCOL_INFO l
)
676 if (::WSADuplicateSocketW(_get_osfhandle(s
), n
, l
) == SOCKET_ERROR
) {
678 if (::WSADuplicateSocketA(_get_osfhandle(s
), n
, l
) == SOCKET_ERROR
) {
680 errno
= WSAGetLastError();
688 int WSASocket(int a
, int t
, int p
, LPWSAPROTOCOL_INFO i
, GROUP g
, DWORD f
)
692 if ((result
= ::WSASocketW(a
, t
, p
, i
, g
, f
)) == INVALID_SOCKET
) {
694 if ((result
= ::WSASocketA(a
, t
, p
, i
, g
, f
)) == INVALID_SOCKET
) {
696 if (WSAEMFILE
== (errno
= WSAGetLastError()))
700 return _open_osfhandle(result
, 0);
703 } /* namespace Squid */
705 #else /* #ifdef __cplusplus */
706 #define connect(s,n,l) \
707 (SOCKET_ERROR == connect(_get_osfhandle(s),n,l) ? \
708 (WSAEMFILE == (errno = WSAGetLastError()) ? errno = EMFILE : -1, -1) : 0)
709 #define gethostbyname(n) \
710 (NULL == ((HOSTENT FAR*)(ws32_result = (int)gethostbyname(n))) ? \
711 (errno = WSAGetLastError()), (HOSTENT FAR*)NULL : (HOSTENT FAR*)ws32_result)
712 #define gethostname(n,l) \
713 (SOCKET_ERROR == gethostname(n,l) ? \
714 (errno = WSAGetLastError()), -1 : 0)
715 #define recv(s,b,l,f) \
716 (SOCKET_ERROR == (ws32_result = recv(_get_osfhandle(s),b,l,f)) ? \
717 (errno = WSAGetLastError()), -1 : ws32_result)
718 #define sendto(s,b,l,f,t,tl) \
719 (SOCKET_ERROR == (ws32_result = sendto(_get_osfhandle(s),b,l,f,t,tl)) ? \
720 (errno = WSAGetLastError()), -1 : ws32_result)
721 #define select(n,r,w,e,t) \
722 (SOCKET_ERROR == (ws32_result = select(n,r,w,e,t)) ? \
723 (errno = WSAGetLastError()), -1 : ws32_result)
724 #define socket(f,t,p) \
725 (INVALID_SOCKET == ((SOCKET)(ws32_result = (int)socket(f,t,p))) ? \
726 ((WSAEMFILE == (errno = WSAGetLastError()) ? errno = EMFILE : -1), -1) : \
727 (SOCKET)_open_osfhandle(ws32_result,0))
728 #define write _write /* Needed in util.c */
729 #define open _open /* Needed in win32lib.c */
730 #endif /* #ifdef __cplusplus */
732 #if HAVE_SYS_RESOURCE_H
733 #include <sys/resource.h>
735 #define RUSAGE_SELF 0 /* calling process */
736 #define RUSAGE_CHILDREN -1 /* terminated child processes */
739 struct timeval ru_utime
; /* user time used */
740 struct timeval ru_stime
; /* system time used */
741 long ru_maxrss
; /* integral max resident set size */
742 long ru_ixrss
; /* integral shared text memory size */
743 long ru_idrss
; /* integral unshared data size */
744 long ru_isrss
; /* integral unshared stack size */
745 long ru_minflt
; /* page reclaims */
746 long ru_majflt
; /* page faults */
747 long ru_nswap
; /* swaps */
748 long ru_inblock
; /* block input operations */
749 long ru_oublock
; /* block output operations */
750 long ru_msgsnd
; /* messages sent */
751 long ru_msgrcv
; /* messages received */
752 long ru_nsignals
; /* signals received */
753 long ru_nvcsw
; /* voluntary context switches */
754 long ru_nivcsw
; /* involuntary context switches */
756 #endif /* HAVE_SYS_RESOURCE_H */
760 #if !defined(getpagesize)
761 /* Windows may lack getpagesize() prototype */
762 SQUIDCEXTERN
size_t getpagesize(void);
765 /* gcc doesn't recognize the Windows native 64 bit formatting tags causing
766 * the compile fail, so we must disable the check on native Windows.
769 #define PRINTF_FORMAT_ARG1
770 #define PRINTF_FORMAT_ARG2
771 #define PRINTF_FORMAT_ARG3
774 /* XXX: the logic around this is a bit warped:
775 * we #define ACL unconditionally at the top of this file,
776 * then #undef ACL unconditionally hafway down,
777 * then here re-define ACL to the same value as at the top,
778 * then include windows.h and #undef ACL again.
781 #define ACL WindowsACL
782 #define _MSWIN_ACL_WAS_NOT_DEFINED 1
785 #if _MSWIN_ACL_WAS_NOT_DEFINED
787 #undef _MSWIN_ACL_WAS_NOT_DEFINED
790 #endif /* _SQUID_WINDOWS_ */
791 #endif /* SQUID_OS_MSWINDOWS_H */