]> git.ipfire.org Git - thirdparty/squid.git/blame - compat/mswindows.cc
Fix ipv6 enabled squidclient. Was failing with assertion failure due to ipv6 support...
[thirdparty/squid.git] / compat / mswindows.cc
CommitLineData
1b52df9a 1
62045984 2/*
1b52df9a 3 * Windows support
4 * AUTHOR: Guido Serassio <serassio@squid-cache.org>
5 * inspired by previous work by Romeo Anghelache & Eric Stern.
62045984 6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
26ac0430 23 *
62045984 24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
26ac0430 28 *
62045984 29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 */
34
f7f3304a 35#include "squid.h"
62045984 36#include "util.h"
37
38/* The following code section is part of an EXPERIMENTAL native */
0a1238e7 39/* Windows NT/2000 Squid port - Compiles only on MS Visual C++ or MinGW */
7aa9bb3e 40#if _SQUID_WINDOWS_ && !_SQUID_CYGWIN_
62045984 41
42#undef strerror
43#define sys_nerr _sys_nerr
44
45#undef assert
46#include <assert.h>
47#include <stdio.h>
48#include <fcntl.h>
62045984 49#include <string.h>
50#include <sys/timeb.h>
51#if HAVE_WIN32_PSAPI
52#include <psapi.h>
53#endif
54
55THREADLOCAL int ws32_result;
56LPCRITICAL_SECTION dbg_mutex = NULL;
57
58void GetProcessName(pid_t, char *);
59
60#if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
26ac0430 61size_t
62045984 62getpagesize()
63{
97fee6b1
GS
64 static DWORD system_pagesize = 0;
65 if (!system_pagesize) {
26ac0430
AJ
66 SYSTEM_INFO system_info;
67 GetSystemInfo(&system_info);
68 system_pagesize = system_info.dwPageSize;
97fee6b1
GS
69 }
70 return system_pagesize;
62045984 71}
72#endif
73
26ac0430 74uid_t
62045984 75geteuid(void)
76{
77 return 100;
78}
79
26ac0430 80uid_t
62045984 81getuid(void)
82{
83 return 100;
84}
85
26ac0430 86int
62045984 87setuid(uid_t uid)
88{
89 return 0;
90}
91
26ac0430 92int
62045984 93seteuid(uid_t euid)
94{
95 return 0;
96}
97
26ac0430 98gid_t
62045984 99getegid(void)
100{
101 return 100;
102}
103
26ac0430 104gid_t
62045984 105getgid(void)
106{
107 return 100;
108}
109
26ac0430 110int
62045984 111setgid(gid_t gid)
112{
113 return 0;
114}
115
26ac0430 116int
62045984 117setegid(gid_t egid)
118{
119 return 0;
120}
121
26ac0430 122int
62045984 123chroot(const char *dirname)
124{
125 if (SetCurrentDirectory(dirname))
26ac0430 126 return 0;
62045984 127 else
26ac0430 128 return GetLastError();
62045984 129}
130
26ac0430 131void
62045984 132GetProcessName(pid_t pid, char *ProcessName)
133{
134 HANDLE hProcess;
135
136 strcpy(ProcessName, "unknown");
137#if HAVE_WIN32_PSAPI
138 /* Get a handle to the process. */
139 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
26ac0430
AJ
140 PROCESS_VM_READ,
141 FALSE, pid);
62045984 142 /* Get the process name. */
143 if (NULL != hProcess) {
26ac0430
AJ
144 HMODULE hMod;
145 DWORD cbNeeded;
146
147 if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))
148 GetModuleBaseName(hProcess, hMod, ProcessName, sizeof(ProcessName));
149 else {
150 CloseHandle(hProcess);
151 return;
152 }
62045984 153 } else
26ac0430 154 return;
62045984 155 CloseHandle(hProcess);
156#endif
157}
158
26ac0430 159int
62045984 160kill(pid_t pid, int sig)
161{
162 HANDLE hProcess;
163 char MyProcessName[MAX_PATH];
164 char ProcessNameToCheck[MAX_PATH];
165
166 if (sig == 0) {
26ac0430
AJ
167 if ((hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
168 PROCESS_VM_READ,
169 FALSE, pid)) == NULL)
170 return -1;
171 else {
172 CloseHandle(hProcess);
173 GetProcessName(getpid(), MyProcessName);
174 GetProcessName(pid, ProcessNameToCheck);
175 if (strcmp(MyProcessName, ProcessNameToCheck) == 0)
176 return 0;
177 return -1;
178 }
62045984 179 } else
26ac0430 180 return 0;
62045984 181}
182
32d002cb 183#if !HAVE_GETTIMEOFDAY
26ac0430 184int
62045984 185gettimeofday(struct timeval *pcur_time, void *tzp)
186{
187 struct _timeb current;
188 struct timezone *tz = (struct timezone *) tzp;
189
190 _ftime(&current);
191
192 pcur_time->tv_sec = current.time;
193 pcur_time->tv_usec = current.millitm * 1000L;
194 if (tz) {
26ac0430
AJ
195 tz->tz_minuteswest = current.timezone; /* minutes west of Greenwich */
196 tz->tz_dsttime = current.dstflag; /* type of dst correction */
62045984 197 }
198 return 0;
199}
379d5751 200#endif
62045984 201
26ac0430 202int
62045984 203statfs(const char *path, struct statfs *sfs)
204{
205 char drive[4];
206 DWORD spc, bps, freec, totalc;
207 DWORD vsn, maxlen, flags;
208
209 if (!sfs) {
26ac0430
AJ
210 errno = EINVAL;
211 return -1;
62045984 212 }
213 strncpy(drive, path, 2);
214 drive[2] = '\0';
215 strcat(drive, "\\");
216
217 if (!GetDiskFreeSpace(drive, &spc, &bps, &freec, &totalc)) {
26ac0430
AJ
218 errno = ENOENT;
219 return -1;
62045984 220 }
221 if (!GetVolumeInformation(drive, NULL, 0, &vsn, &maxlen, &flags, NULL, 0)) {
26ac0430
AJ
222 errno = ENOENT;
223 return -1;
62045984 224 }
225 sfs->f_type = flags;
226 sfs->f_bsize = spc * bps;
227 sfs->f_blocks = totalc;
228 sfs->f_bfree = sfs->f_bavail = freec;
229 sfs->f_files = -1;
230 sfs->f_ffree = -1;
231 sfs->f_fsid = vsn;
232 sfs->f_namelen = maxlen;
233 return 0;
234}
235
3bfe9ee7 236#if !_SQUID_MINGW_
62045984 237int
238WIN32_ftruncate(int fd, off_t size)
239{
240 HANDLE hfile;
241 unsigned int curpos;
242
243 if (fd < 0)
26ac0430 244 return -1;
62045984 245
246 hfile = (HANDLE) _get_osfhandle(fd);
247 curpos = SetFilePointer(hfile, 0, NULL, FILE_CURRENT);
248 if (curpos == 0xFFFFFFFF
26ac0430
AJ
249 || SetFilePointer(hfile, size, NULL, FILE_BEGIN) == 0xFFFFFFFF
250 || !SetEndOfFile(hfile)) {
251 int error = GetLastError();
252
253 switch (error) {
254 case ERROR_INVALID_HANDLE:
255 errno = EBADF;
256 break;
257 default:
258 errno = EIO;
259 break;
260 }
261
262 return -1;
62045984 263 }
264 return 0;
265}
266
26ac0430 267int
62045984 268WIN32_truncate(const char *pathname, off_t length)
269{
270 int fd;
271 int res = -1;
272
273 fd = open(pathname, O_RDWR);
274
275 if (fd == -1)
26ac0430 276 errno = EBADF;
62045984 277 else {
26ac0430
AJ
278 res = WIN32_ftruncate(fd, length);
279 _close(fd);
62045984 280 }
281
282 return res;
283}
3bfe9ee7 284#endif /* !_SQUID_MINGW_ */
62045984 285
62045984 286struct passwd *
e1381638 287getpwnam(char *unused) {
26ac0430 288 static struct passwd pwd = {NULL, NULL, 100, 100, NULL, NULL, NULL};
62045984 289 return &pwd;
290}
291
292struct group *
e1381638 293getgrnam(char *unused) {
26ac0430 294 static struct group grp = {NULL, NULL, 100, NULL};
62045984 295 return &grp;
296}
297
d305c51d 298#if _SQUID_MINGW_
26ac0430 299int
62045984 300_free_osfhnd(int filehandle)
301{
302 if (((unsigned) filehandle < SQUID_MAXFD) &&
26ac0430
AJ
303 (_osfile(filehandle) & FOPEN) &&
304 (_osfhnd(filehandle) != (long) INVALID_HANDLE_VALUE)) {
305 switch (filehandle) {
306 case 0:
307 SetStdHandle(STD_INPUT_HANDLE, NULL);
308 break;
309 case 1:
310 SetStdHandle(STD_OUTPUT_HANDLE, NULL);
311 break;
312 case 2:
313 SetStdHandle(STD_ERROR_HANDLE, NULL);
314 break;
315 }
316 _osfhnd(filehandle) = (long) INVALID_HANDLE_VALUE;
317 return (0);
62045984 318 } else {
26ac0430
AJ
319 errno = EBADF; /* bad handle */
320 _doserrno = 0L; /* not an OS error */
321 return -1;
62045984 322 }
323}
324#endif
325
326struct errorentry {
327 unsigned long WIN32_code;
328 int POSIX_errno;
329};
330
26ac0430 331static struct errorentry errortable[] = {
62045984 332 {ERROR_INVALID_FUNCTION, EINVAL},
333 {ERROR_FILE_NOT_FOUND, ENOENT},
334 {ERROR_PATH_NOT_FOUND, ENOENT},
335 {ERROR_TOO_MANY_OPEN_FILES, EMFILE},
336 {ERROR_ACCESS_DENIED, EACCES},
337 {ERROR_INVALID_HANDLE, EBADF},
338 {ERROR_ARENA_TRASHED, ENOMEM},
339 {ERROR_NOT_ENOUGH_MEMORY, ENOMEM},
340 {ERROR_INVALID_BLOCK, ENOMEM},
341 {ERROR_BAD_ENVIRONMENT, E2BIG},
342 {ERROR_BAD_FORMAT, ENOEXEC},
343 {ERROR_INVALID_ACCESS, EINVAL},
344 {ERROR_INVALID_DATA, EINVAL},
345 {ERROR_INVALID_DRIVE, ENOENT},
346 {ERROR_CURRENT_DIRECTORY, EACCES},
347 {ERROR_NOT_SAME_DEVICE, EXDEV},
348 {ERROR_NO_MORE_FILES, ENOENT},
349 {ERROR_LOCK_VIOLATION, EACCES},
350 {ERROR_BAD_NETPATH, ENOENT},
351 {ERROR_NETWORK_ACCESS_DENIED, EACCES},
352 {ERROR_BAD_NET_NAME, ENOENT},
353 {ERROR_FILE_EXISTS, EEXIST},
354 {ERROR_CANNOT_MAKE, EACCES},
355 {ERROR_FAIL_I24, EACCES},
356 {ERROR_INVALID_PARAMETER, EINVAL},
357 {ERROR_NO_PROC_SLOTS, EAGAIN},
358 {ERROR_DRIVE_LOCKED, EACCES},
359 {ERROR_BROKEN_PIPE, EPIPE},
360 {ERROR_DISK_FULL, ENOSPC},
361 {ERROR_INVALID_TARGET_HANDLE, EBADF},
362 {ERROR_INVALID_HANDLE, EINVAL},
363 {ERROR_WAIT_NO_CHILDREN, ECHILD},
364 {ERROR_CHILD_NOT_COMPLETE, ECHILD},
365 {ERROR_DIRECT_ACCESS_HANDLE, EBADF},
366 {ERROR_NEGATIVE_SEEK, EINVAL},
367 {ERROR_SEEK_ON_DEVICE, EACCES},
368 {ERROR_DIR_NOT_EMPTY, ENOTEMPTY},
369 {ERROR_NOT_LOCKED, EACCES},
370 {ERROR_BAD_PATHNAME, ENOENT},
371 {ERROR_MAX_THRDS_REACHED, EAGAIN},
372 {ERROR_LOCK_FAILED, EACCES},
373 {ERROR_ALREADY_EXISTS, EEXIST},
374 {ERROR_FILENAME_EXCED_RANGE, ENOENT},
375 {ERROR_NESTING_NOT_ALLOWED, EAGAIN},
376 {ERROR_NOT_ENOUGH_QUOTA, ENOMEM}
377};
378
379#define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
380#define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
381
382#define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
383#define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
384
26ac0430 385void
62045984 386WIN32_maperror(unsigned long WIN32_oserrno)
387{
388 int i;
389
390 _doserrno = WIN32_oserrno;
391 for (i = 0; i < (sizeof(errortable) / sizeof(struct errorentry)); ++i) {
26ac0430
AJ
392 if (WIN32_oserrno == errortable[i].WIN32_code) {
393 errno = errortable[i].POSIX_errno;
394 return;
395 }
62045984 396 }
397 if (WIN32_oserrno >= MIN_EACCES_RANGE && WIN32_oserrno <= MAX_EACCES_RANGE)
26ac0430 398 errno = EACCES;
62045984 399 else if (WIN32_oserrno >= MIN_EXEC_ERROR && WIN32_oserrno <= MAX_EXEC_ERROR)
26ac0430 400 errno = ENOEXEC;
62045984 401 else
26ac0430 402 errno = EINVAL;
62045984 403}
404#endif