]> git.ipfire.org Git - thirdparty/squid.git/blob - src/win32.cc
Completed protos.h split and code refactoring
[thirdparty/squid.git] / src / win32.cc
1 /*
2 * Windows support
3 * AUTHOR: Guido Serassio <serassio@squid-cache.org>
4 * inspired by previous work by Romeo Anghelache & Eric Stern.
5 *
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
8 *
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.
17 *
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.
22 *
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.
27 *
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.
31 *
32 */
33
34 #include "squid.h"
35 #include "squid_windows.h"
36 #include "win32.h"
37
38 #if _SQUID_MSWIN_
39 #if HAVE_WIN32_PSAPI
40 #include <psapi.h>
41 #endif
42 #ifndef _MSWSOCK_
43 #include <mswsock.h>
44 #endif
45 #include <fde.h>
46
47 SQUIDCEXTERN LPCRITICAL_SECTION dbg_mutex;
48 void WIN32_ExceptionHandlerCleanup(void);
49 static LPTOP_LEVEL_EXCEPTION_FILTER Win32_Old_ExceptionHandler = NULL;
50
51 int WIN32_pipe(int handles[2])
52 {
53 int new_socket;
54 fde *F = NULL;
55
56 Ip::Address localhost;
57 Ip::Address handle0;
58 Ip::Address handle1;
59 struct addrinfo *AI = NULL;
60
61 localhost.SetLocalhost();
62
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.
66 */
67 localhost.SetIPv4();
68
69 handles[0] = handles[1] = -1;
70
71 ++statCounter.syscalls.sock.sockets;
72
73 handle0 = localhost;
74 handle0.SetPort(0);
75 handle0.GetAddrInfo(AI);
76
77 if ((new_socket = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol)) < 0)
78 return -1;
79
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);
84 return -1;
85 }
86
87 handle0 = *AI; // retrieve the new details returned by connect()
88
89 handle1.SetPort(handle1.GetPort());
90 handle1.GetAddrInfo(AI);
91
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]);
95 handles[1] = -1;
96 closesocket(new_socket);
97 return -1;
98 }
99
100 closesocket(new_socket);
101
102 F = &fd_table[handles[0]];
103 F->local_addr = handle0;
104
105 F = &fd_table[handles[1]];
106 F->local_addr = localhost;
107 handle1.NtoA(F->ipaddr, MAX_IPSTRLEN);
108 F->remote_port = handle1.GetPort();
109
110 return 0;
111 }
112
113 int WIN32_getrusage(int who, struct rusage *usage)
114 {
115 #if HAVE_WIN32_PSAPI
116
117 if (WIN32_OS_version >= _WIN_OS_WINNT) {
118 /* On Windows NT and later call PSAPI.DLL for process Memory */
119 /* informations -- Guido Serassio */
120 HANDLE hProcess;
121 PROCESS_MEMORY_COUNTERS pmc;
122 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
123 PROCESS_VM_READ,
124 FALSE, GetCurrentProcessId());
125 {
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;
129
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);
139 } else {
140 CloseHandle( hProcess );
141 return -1;
142 }
143 }
144
145 if (GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc))) {
146 usage->ru_maxrss=(DWORD)(pmc.WorkingSetSize / getpagesize());
147 usage->ru_majflt=pmc.PageFaultCount;
148 } else {
149 CloseHandle( hProcess );
150 return -1;
151 }
152
153 CloseHandle( hProcess );
154 }
155
156 #endif
157 return 0;
158 }
159
160 int
161 Win32__WSAFDIsSet(int fd, fd_set FAR * set)
162 {
163 fde *F = &fd_table[fd];
164 SOCKET s = F->win32.handle;
165
166 return __WSAFDIsSet(s, set);
167 }
168
169 LONG CALLBACK WIN32_ExceptionHandler(EXCEPTION_POINTERS* ep)
170 {
171 EXCEPTION_RECORD* er;
172
173 er = ep->ExceptionRecord;
174
175 switch (er->ExceptionCode) {
176
177 case EXCEPTION_ACCESS_VIOLATION:
178 raise(SIGSEGV);
179 break;
180
181 case EXCEPTION_DATATYPE_MISALIGNMENT:
182
183 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
184
185 case EXCEPTION_IN_PAGE_ERROR:
186 death(SIGBUS);
187 break;
188
189 default:
190 break;
191 }
192
193 return EXCEPTION_CONTINUE_SEARCH;
194 }
195
196 void WIN32_ExceptionHandlerInit()
197 {
198 #if !defined(_DEBUG)
199
200 if (Win32_Old_ExceptionHandler == NULL)
201 Win32_Old_ExceptionHandler = SetUnhandledExceptionFilter(WIN32_ExceptionHandler);
202
203 #endif
204 }
205
206 void WIN32_ExceptionHandlerCleanup()
207 {
208 if (Win32_Old_ExceptionHandler != NULL)
209 SetUnhandledExceptionFilter(Win32_Old_ExceptionHandler);
210 }
211
212 #endif /* SQUID_MSWIN_ */