3 * AUTHOR: Guido Serassio <serassio@squid-cache.org>
4 * inspired by previous work by Romeo Anghelache & Eric Stern.
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35 #include "squid_windows.h"
47 SQUIDCEXTERN LPCRITICAL_SECTION dbg_mutex
;
48 void WIN32_ExceptionHandlerCleanup(void);
49 static LPTOP_LEVEL_EXCEPTION_FILTER Win32_Old_ExceptionHandler
= NULL
;
51 int WIN32_pipe(int handles
[2])
56 Ip::Address localhost
;
59 struct addrinfo
*AI
= NULL
;
61 localhost
.SetLocalhost();
63 /* INET6: back-compatible: localhost pipes default to IPv4 unless set otherwise.
64 * it is blocked by untested helpers on many admins configs
65 * if this proves to be wrong it can die easily.
69 handles
[0] = handles
[1] = -1;
71 ++statCounter
.syscalls
.sock
.sockets
;
75 handle0
.GetAddrInfo(AI
);
77 if ((new_socket
= socket(AI
->ai_family
, AI
->ai_socktype
, AI
->ai_protocol
)) < 0)
80 if (bind(new_socket
, AI
->ai_addr
, AI
->ai_addrlen
) < 0 ||
81 listen(new_socket
, 1) < 0 || getsockname(new_socket
, AI
->ai_addr
, &(AI
->ai_addrlen
) ) < 0 ||
82 (handles
[1] = socket(AI
->ai_family
, AI
->ai_socktype
, 0)) < 0) {
83 closesocket(new_socket
);
87 handle0
= *AI
; // retrieve the new details returned by connect()
89 handle1
.SetPort(handle1
.GetPort());
90 handle1
.GetAddrInfo(AI
);
92 if (connect(handles
[1], AI
->ai_addr
, AI
->ai_addrlen
) < 0 ||
93 (handles
[0] = accept(new_socket
, AI
->ai_addr
, &(AI
->ai_addrlen
)) ) < 0) {
94 closesocket(handles
[1]);
96 closesocket(new_socket
);
100 closesocket(new_socket
);
102 F
= &fd_table
[handles
[0]];
103 F
->local_addr
= handle0
;
105 F
= &fd_table
[handles
[1]];
106 F
->local_addr
= localhost
;
107 handle1
.NtoA(F
->ipaddr
, MAX_IPSTRLEN
);
108 F
->remote_port
= handle1
.GetPort();
113 int WIN32_getrusage(int who
, struct rusage
*usage
)
117 if (WIN32_OS_version
>= _WIN_OS_WINNT
) {
118 /* On Windows NT and later call PSAPI.DLL for process Memory */
119 /* informations -- Guido Serassio */
121 PROCESS_MEMORY_COUNTERS pmc
;
122 hProcess
= OpenProcess(PROCESS_QUERY_INFORMATION
|
124 FALSE
, GetCurrentProcessId());
126 /* Microsoft CRT doesn't have getrusage function, */
127 /* so we get process CPU time information from PSAPI.DLL. */
128 FILETIME ftCreate
, ftExit
, ftKernel
, ftUser
;
130 if (GetProcessTimes(hProcess
, &ftCreate
, &ftExit
, &ftKernel
, &ftUser
)) {
131 int64_t *ptUser
= (int64_t *)&ftUser
;
132 int64_t tUser64
= *ptUser
/ 10;
133 int64_t *ptKernel
= (int64_t *)&ftKernel
;
134 int64_t tKernel64
= *ptKernel
/ 10;
135 usage
->ru_utime
.tv_sec
=(long)(tUser64
/ 1000000);
136 usage
->ru_stime
.tv_sec
=(long)(tKernel64
/ 1000000);
137 usage
->ru_utime
.tv_usec
=(long)(tUser64
% 1000000);
138 usage
->ru_stime
.tv_usec
=(long)(tKernel64
% 1000000);
140 CloseHandle( hProcess
);
145 if (GetProcessMemoryInfo( hProcess
, &pmc
, sizeof(pmc
))) {
146 usage
->ru_maxrss
=(DWORD
)(pmc
.WorkingSetSize
/ getpagesize());
147 usage
->ru_majflt
=pmc
.PageFaultCount
;
149 CloseHandle( hProcess
);
153 CloseHandle( hProcess
);
161 Win32__WSAFDIsSet(int fd
, fd_set FAR
* set
)
163 fde
*F
= &fd_table
[fd
];
164 SOCKET s
= F
->win32
.handle
;
166 return __WSAFDIsSet(s
, set
);
169 LONG CALLBACK
WIN32_ExceptionHandler(EXCEPTION_POINTERS
* ep
)
171 EXCEPTION_RECORD
* er
;
173 er
= ep
->ExceptionRecord
;
175 switch (er
->ExceptionCode
) {
177 case EXCEPTION_ACCESS_VIOLATION
:
181 case EXCEPTION_DATATYPE_MISALIGNMENT
:
183 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
185 case EXCEPTION_IN_PAGE_ERROR
:
193 return EXCEPTION_CONTINUE_SEARCH
;
196 void WIN32_ExceptionHandlerInit()
200 if (Win32_Old_ExceptionHandler
== NULL
)
201 Win32_Old_ExceptionHandler
= SetUnhandledExceptionFilter(WIN32_ExceptionHandler
);
206 void WIN32_ExceptionHandlerCleanup()
208 if (Win32_Old_ExceptionHandler
!= NULL
)
209 SetUnhandledExceptionFilter(Win32_Old_ExceptionHandler
);
212 #endif /* SQUID_MSWIN_ */