]> git.ipfire.org Git - thirdparty/squid.git/blob - compat/os/mswindows.h
Merged from trunk r12948.
[thirdparty/squid.git] / compat / os / mswindows.h
1 /*
2 * AUTHOR: Andrey Shorin <tolsty@tushino.com>
3 * AUTHOR: Guido Serassio <serassio@squid-cache.org>
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
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.
16 *
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.
21 *
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.
26 *
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.
30 *
31 */
32 #ifndef SQUID_OS_MSWINDOWS_H
33 #define SQUID_OS_MSWINDOWS_H
34
35 #if _SQUID_WINDOWS_
36
37 /* we target Windows XP and later - some API are missing otherwise */
38 #if _SQUID_MINGW_
39 #if WINVER < 0x0501
40 #undef WINVER
41 #define WINVER 0x0501
42 #undef _WIN32_WINNT
43 #define _WIN32_WINNT WINVER
44 #endif
45 #endif /* _SQUID_MINGW_ */
46
47 #if HAVE_FCNTL_H
48 #include <fcntl.h>
49 #endif /* HAVE_FCNTL_H */
50 #if HAVE_STRING_H
51 #include <string.h>
52 #endif /* HAVE_FCNTL_H */
53 #if HAVE_SYS_STAT_H
54 #include <sys/stat.h>
55 #endif /* HAVE_SYS_STAT_H */
56
57 #define ACL WindowsACL
58 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
59 #if _MSC_VER == 1400
60 #define _CRT_SECURE_NO_DEPRECATE
61 #pragma warning( disable : 4290 )
62 #pragma warning( disable : 4996 )
63 #endif
64 #endif
65
66 /* Some MinGW version defines min() and max() as macros
67 causing the fail of the build process. The following
68 #define will disable that definition
69 */
70 #if defined(__GNUC__) && !NOMINMAX
71 #define NOMINMAX
72 #endif
73
74 #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
75 # define __USE_FILE_OFFSET64 1
76 #endif
77
78 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
79
80 #if defined(__USE_FILE_OFFSET64)
81 typedef uint64_t ino_t;
82 #else
83 typedef unsigned long ino_t;
84 #endif
85
86 #define INT64_MAX _I64_MAX
87 #define INT64_MIN _I64_MIN
88
89 #include "default_config_file.h"
90 /* Some tricks for MS Compilers */
91 #define __STDC__ 1
92 #define THREADLOCAL __declspec(thread)
93
94 #elif defined(__GNUC__) /* gcc environment */
95
96 #define THREADLOCAL __attribute__((section(".tls")))
97
98 #endif /* _MSC_VER */
99
100 /* ONLY Microsoft C Compiler needs these: */
101 #if defined(_MSC_VER)
102 #define alloca _alloca
103 #define fileno _fileno
104 #define fstat _fstati64
105 #define lseek _lseeki64
106 #define memccpy _memccpy
107 #define mkdir(p,F) _mkdir((p))
108 #define mktemp _mktemp
109 #define snprintf _snprintf
110 #define stat _stati64
111 #define strcasecmp _stricmp
112 #define strdup _strdup
113 #define strlwr _strlwr
114 #define strncasecmp _strnicmp
115 #define tempnam _tempnam
116 #define vsnprintf _vsnprintf
117 #endif
118
119 /* CygWin and MinGW compilers need these. Microsoft C Compiler does not. */
120 #if _SQUID_MINGW_ || _SQUID_CYGWIN_
121 #define mkdir(p,F) mkdir((p))
122 #endif
123
124 /* Microsoft C Compiler and CygWin need these. MinGW does not */
125 #if defined(_MSC_VER) || _SQUID_CYGWIN_
126 SQUIDCEXTERN int WIN32_ftruncate(int fd, off_t size);
127 #define ftruncate WIN32_ftruncate
128 SQUIDCEXTERN int WIN32_truncate(const char *pathname, off_t length);
129 #define truncate WIN32_truncate
130 #endif
131
132 /* All three compiler systems need these: */
133 #define chdir _chdir
134 #define dup _dup
135 #define dup2 _dup2
136 #define fdopen _fdopen
137 #define getcwd _getcwd
138 #define getpid _getpid
139 #define pclose _pclose
140 #define popen _popen
141 #define putenv _putenv
142 #define setmode _setmode
143 #define sleep(t) Sleep((t)*1000)
144 #define umask _umask
145 #define unlink _unlink
146
147 #define O_RDONLY _O_RDONLY
148 #define O_WRONLY _O_WRONLY
149 #define O_RDWR _O_RDWR
150 #define O_APPEND _O_APPEND
151
152 #define O_CREAT _O_CREAT
153 #define O_TRUNC _O_TRUNC
154 #define O_EXCL _O_EXCL
155
156 #define O_TEXT _O_TEXT
157 #define O_BINARY _O_BINARY
158 #define O_RAW _O_BINARY
159 #define O_TEMPORARY _O_TEMPORARY
160 #define O_NOINHERIT _O_NOINHERIT
161 #define O_SEQUENTIAL _O_SEQUENTIAL
162 #define O_RANDOM _O_RANDOM
163 #define O_NDELAY 0
164
165 #define S_IFMT _S_IFMT
166 #define S_IFDIR _S_IFDIR
167 #define S_IFCHR _S_IFCHR
168 #define S_IFREG _S_IFREG
169 #define S_IREAD _S_IREAD
170 #define S_IWRITE _S_IWRITE
171 #define S_IEXEC _S_IEXEC
172
173 #define S_IRWXO 007
174
175 #if defined(_MSC_VER)
176 #define S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR)
177 #endif
178
179 #define SIGHUP 1 /* hangup */
180 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
181 #define SIGBUS 10 /* bus error */
182 #define SIGPIPE 13 /* write on a pipe with no one to read it */
183 #define SIGCHLD 20 /* to parent on child stop or exit */
184 #define SIGUSR1 30 /* user defined signal 1 */
185 #define SIGUSR2 31 /* user defined signal 2 */
186
187 #if _SQUID_MINGW_
188 typedef unsigned char boolean;
189 typedef unsigned char u_char;
190 typedef unsigned int u_int;
191 #endif
192
193 #if defined(_MSC_VER)
194 typedef int uid_t;
195 typedef int gid_t;
196 #endif
197
198 struct passwd {
199 char *pw_name; /* user name */
200 char *pw_passwd; /* user password */
201 uid_t pw_uid; /* user id */
202 gid_t pw_gid; /* group id */
203 char *pw_gecos; /* real name */
204 char *pw_dir; /* home directory */
205 char *pw_shell; /* shell program */
206 };
207
208 struct group {
209 char *gr_name; /* group name */
210 char *gr_passwd; /* group password */
211 gid_t gr_gid; /* group id */
212 char **gr_mem; /* group members */
213 };
214
215 #if !_SQUID_MINGW_
216 struct statfs {
217 long f_type; /* type of filesystem (see below) */
218 long f_bsize; /* optimal transfer block size */
219 long f_blocks; /* total data blocks in file system */
220 long f_bfree; /* free blocks in fs */
221 long f_bavail; /* free blocks avail to non-superuser */
222 long f_files; /* total file nodes in file system */
223 long f_ffree; /* free file nodes in fs */
224 long f_fsid; /* file system id */
225 long f_namelen; /* maximum length of filenames */
226 long f_spare[6]; /* spare for later */
227 };
228 #endif
229
230 #if !HAVE_GETTIMEOFDAY
231 struct timezone {
232 int tz_minuteswest; /* minutes west of Greenwich */
233 int tz_dsttime; /* type of dst correction */
234 };
235 #endif
236
237 #define CHANGE_FD_SETSIZE 1
238 #if CHANGE_FD_SETSIZE && SQUID_MAXFD > DEFAULT_FD_SETSIZE
239 #define FD_SETSIZE SQUID_MAXFD
240 #endif
241
242 #include <process.h>
243 #include <errno.h>
244 #if HAVE_WINSOCK2_H
245 #include <winsock2.h>
246 #elif HAVE_WINSOCK_H
247 #include <winsock.h>
248 #endif
249
250 #if !_SQUID_CYGWIN_
251 #undef IN_ADDR
252 #include <ws2tcpip.h>
253 #endif
254
255 #if (EAI_NODATA == EAI_NONAME)
256 #undef EAI_NODATA
257 #define EAI_NODATA WSANO_DATA
258 #endif
259
260 #if defined(_MSC_VER)
261 /* Hack to suppress compiler warnings on FD_SET() & FD_CLR() */
262 #pragma warning (push)
263 #pragma warning (disable:4142)
264 #endif
265
266 /* prevent inclusion of wingdi.h */
267 #define NOGDI
268 #include <ws2spi.h>
269
270 #if defined(_MSC_VER)
271 #pragma warning (pop)
272 #endif
273
274 #include <io.h>
275
276 typedef char * caddr_t;
277
278 #ifndef _PATH_DEVNULL
279 #define _PATH_DEVNULL "NUL"
280 #endif
281
282 #undef FD_CLOSE
283 #undef FD_OPEN
284 #undef FD_READ
285 #undef FD_WRITE
286
287 #ifndef EISCONN
288 #define EISCONN WSAEISCONN
289 #endif
290 #ifndef EINPROGRESS
291 #define EINPROGRESS WSAEINPROGRESS
292 #endif
293 #ifndef EWOULDBLOCK
294 #define EWOULDBLOCK WSAEWOULDBLOCK
295 #endif
296 #ifndef EALREADY
297 #define EALREADY WSAEALREADY
298 #endif
299 #ifndef ETIMEDOUT
300 #define ETIMEDOUT WSAETIMEDOUT
301 #endif
302 #ifndef ECONNREFUSED
303 #define ECONNREFUSED WSAECONNREFUSED
304 #endif
305 #ifndef ECONNRESET
306 #define ECONNRESET WSAECONNRESET
307 #endif
308 #ifndef ENOTCONN
309 #define ENOTCONN WSAENOTCONN
310 #endif
311 #ifndef ERESTART
312 #define ERESTART WSATRY_AGAIN
313 #endif
314 #ifndef EAFNOSUPPORT
315 #define EAFNOSUPPORT WSAEAFNOSUPPORT
316 #endif
317 #ifndef ENETUNREACH
318 #define ENETUNREACH WSAENETUNREACH
319 #endif
320 #ifndef ENOTSUP
321 #define ENOTSUP WSAEOPNOTSUPP
322 #endif
323
324 #undef h_errno
325 #define h_errno errno /* we'll set it ourselves */
326
327 #undef FD_CLR
328 #define FD_CLR(fd, set) do { \
329 u_int __i; \
330 SOCKET __sock = _get_osfhandle(fd); \
331 for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count ; __i++) { \
332 if (((fd_set FAR *)(set))->fd_array[__i] == __sock) { \
333 while (__i < ((fd_set FAR *)(set))->fd_count-1) { \
334 ((fd_set FAR *)(set))->fd_array[__i] = \
335 ((fd_set FAR *)(set))->fd_array[__i+1]; \
336 __i++; \
337 } \
338 ((fd_set FAR *)(set))->fd_count--; \
339 break; \
340 } \
341 } \
342 } while(0)
343
344 #undef FD_SET
345 #define FD_SET(fd, set) do { \
346 u_int __i; \
347 SOCKET __sock = _get_osfhandle(fd); \
348 for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count; __i++) { \
349 if (((fd_set FAR *)(set))->fd_array[__i] == (__sock)) { \
350 break; \
351 } \
352 } \
353 if (__i == ((fd_set FAR *)(set))->fd_count) { \
354 if (((fd_set FAR *)(set))->fd_count < FD_SETSIZE) { \
355 ((fd_set FAR *)(set))->fd_array[__i] = (__sock); \
356 ((fd_set FAR *)(set))->fd_count++; \
357 } \
358 } \
359 } while(0)
360
361 #undef FD_ISSET
362 #define FD_ISSET(fd, set) Win32__WSAFDIsSet(fd, (fd_set FAR *)(set))
363
364 /* internal to Microsoft CRTLIB */
365 typedef struct {
366 long osfhnd; /* underlying OS file HANDLE */
367 char osfile; /* attributes of file (e.g., open in text mode?) */
368 char pipech; /* one char buffer for handles opened on pipes */
369 #ifdef _MT
370 int lockinitflag;
371 CRITICAL_SECTION lock;
372 #endif /* _MT */
373 } ioinfo;
374 #define IOINFO_L2E 5
375 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
376 #define _pioinfo(i) ( __pioinfo[(i) >> IOINFO_L2E] + ((i) & (IOINFO_ARRAY_ELTS - 1)) )
377 #define _osfile(i) ( _pioinfo(i)->osfile )
378 #define _osfhnd(i) ( _pioinfo(i)->osfhnd )
379 #define FOPEN 0x01 /* file handle open */
380
381 #if defined(_MSC_VER)
382 SQUIDCEXTERN _CRTIMP ioinfo * __pioinfo[];
383 SQUIDCEXTERN int __cdecl _free_osfhnd(int);
384 #endif
385
386 #if _SQUID_MINGW_
387 __MINGW_IMPORT ioinfo * __pioinfo[];
388 SQUIDCEXTERN int _free_osfhnd(int);
389 #endif
390
391 SQUIDCEXTERN THREADLOCAL int ws32_result;
392
393 #if defined(__cplusplus)
394
395 inline int
396 close(int fd)
397 {
398 char l_so_type[sizeof(int)];
399 int l_so_type_siz = sizeof(l_so_type);
400 SOCKET sock = _get_osfhandle(fd);
401
402 if (::getsockopt(sock, SOL_SOCKET, SO_TYPE, l_so_type, &l_so_type_siz) == 0) {
403 int result = 0;
404 if (closesocket(sock) == SOCKET_ERROR) {
405 errno = WSAGetLastError();
406 result = 1;
407 }
408 _free_osfhnd(fd);
409 _osfile(fd) = 0;
410 return result;
411 } else
412 return _close(fd);
413 }
414
415 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
416
417 #ifndef _S_IREAD
418 #define _S_IREAD 0x0100
419 #endif
420
421 #ifndef _S_IWRITE
422 #define _S_IWRITE 0x0080
423 #endif
424
425 inline int
426 open(const char *filename, int oflag, int pmode = 0)
427 {
428 return _open(filename, oflag, pmode & (_S_IREAD | _S_IWRITE));
429 }
430 #endif
431
432 inline int
433 read(int fd, void * buf, size_t siz)
434 {
435 char l_so_type[sizeof(int)];
436 int l_so_type_siz = sizeof(l_so_type);
437 SOCKET sock = _get_osfhandle(fd);
438
439 if (::getsockopt(sock, SOL_SOCKET, SO_TYPE, l_so_type, &l_so_type_siz) == 0)
440 return ::recv(sock, (char FAR *) buf, (int)siz, 0);
441 else
442 return _read(fd, buf, (unsigned int)siz);
443 }
444
445 inline int
446 write(int fd, const void * buf, size_t siz)
447 {
448 char l_so_type[sizeof(int)];
449 int l_so_type_siz = sizeof(l_so_type);
450 SOCKET sock = _get_osfhandle(fd);
451
452 if (::getsockopt(sock, SOL_SOCKET, SO_TYPE, l_so_type, &l_so_type_siz) == 0)
453 return ::send(sock, (char FAR *) buf, siz, 0);
454 else
455 return _write(fd, buf, siz);
456 }
457
458 inline char *
459 index(const char *s, int c)
460 {
461 return (char *)strchr(s,c);
462 }
463
464 // stdlib <functional> definitions are required before std API redefinitions.
465 #include <functional>
466
467 /** \cond AUTODOCS-IGNORE */
468 namespace Squid
469 {
470 /** \endcond */
471
472 inline int
473 accept(int s, struct sockaddr * a, socklen_t * l)
474 {
475 SOCKET result;
476 if ((result = ::accept(_get_osfhandle(s), a, l)) == INVALID_SOCKET) {
477 if (WSAEMFILE == (errno = WSAGetLastError()))
478 errno = EMFILE;
479 return -1;
480 } else
481 return _open_osfhandle(result, 0);
482 }
483 #define accept(s,a,l) Squid::accept(s,a,l)
484
485 inline int
486 bind(int s, const struct sockaddr * n, socklen_t l)
487 {
488 if (::bind(_get_osfhandle(s),n,l) == SOCKET_ERROR) {
489 errno = WSAGetLastError();
490 return -1;
491 } else
492 return 0;
493 }
494 #define bind(s,n,l) Squid::bind(s,n,l)
495
496 inline int
497 connect(int s, const struct sockaddr * n, socklen_t l)
498 {
499 if (::connect(_get_osfhandle(s),n,l) == SOCKET_ERROR) {
500 if (WSAEMFILE == (errno = WSAGetLastError()))
501 errno = EMFILE;
502 return -1;
503 } else
504 return 0;
505 }
506 #define connect(s,n,l) Squid::connect(s,n,l)
507
508 inline struct hostent *
509 gethostbyname(const char *n) {
510 HOSTENT FAR * result;
511 if ((result = ::gethostbyname(n)) == NULL)
512 errno = WSAGetLastError();
513 return result;
514 }
515 #define gethostbyname(n) Squid::gethostbyname(n)
516
517 inline SERVENT FAR *
518 getservbyname(const char * n, const char * p)
519 {
520 SERVENT FAR * result;
521 if ((result = ::getservbyname(n, p)) == NULL)
522 errno = WSAGetLastError();
523 return result;
524 }
525 #define getservbyname(n,p) Squid::getservbyname(n,p)
526
527 inline HOSTENT FAR *
528 gethostbyaddr(const void * a, size_t l, int t)
529 {
530 HOSTENT FAR * result;
531 if ((result = ::gethostbyaddr((const char*)a, l, t)) == NULL)
532 errno = WSAGetLastError();
533 return result;
534 }
535 #define gethostbyaddr(a,l,t) Squid::gethostbyaddr(a,l,t)
536
537 inline int
538 getsockname(int s, struct sockaddr * n, socklen_t * l)
539 {
540 int i=*l;
541 if (::getsockname(_get_osfhandle(s), n, &i) == SOCKET_ERROR) {
542 errno = WSAGetLastError();
543 return -1;
544 } else
545 return 0;
546 }
547 #define getsockname(s,a,l) Squid::getsockname(s,a,l)
548
549 inline int
550 gethostname(char * n, size_t l)
551 {
552 if ((::gethostname(n, l)) == SOCKET_ERROR) {
553 errno = WSAGetLastError();
554 return -1;
555 } else
556 return 0;
557 }
558 #define gethostname(n,l) Squid::gethostname(n,l)
559
560 inline int
561 getsockopt(int s, int l, int o, void * v, socklen_t * n)
562 {
563 Sleep(1);
564 if ((::getsockopt(_get_osfhandle(s), l, o,(char *) v, n)) == SOCKET_ERROR) {
565 errno = WSAGetLastError();
566 return -1;
567 } else
568 return 0;
569 }
570 #define getsockopt(s,l,o,v,n) Squid::getsockopt(s,l,o,v,n)
571
572 /* Simple ioctl() emulation */
573 inline int
574 ioctl(int s, int c, void * a)
575 {
576 if ((::ioctlsocket(_get_osfhandle(s), c, (u_long FAR *)a)) == SOCKET_ERROR) {
577 errno = WSAGetLastError();
578 return -1;
579 } else
580 return 0;
581 }
582 #define ioctl(s,c,a) Squid::ioctl(s,c,a)
583
584 inline int
585 ioctlsocket(int s, long c, u_long FAR * a)
586 {
587 if ((::ioctlsocket(_get_osfhandle(s), c, a)) == SOCKET_ERROR) {
588 errno = WSAGetLastError();
589 return -1;
590 } else
591 return 0;
592 }
593 #define ioctlsocket(s,c,a) Squid::ioctlsocket(s,c,a)
594
595 inline int
596 listen(int s, int b)
597 {
598 if (::listen(_get_osfhandle(s), b) == SOCKET_ERROR) {
599 if (WSAEMFILE == (errno = WSAGetLastError()))
600 errno = EMFILE;
601 return -1;
602 } else
603 return 0;
604 }
605 #define listen(s,b) Squid::listen(s,b)
606
607 inline ssize_t
608 recv(int s, void * b, size_t l, int f)
609 {
610 ssize_t result;
611 if ((result = ::recv(_get_osfhandle(s), (char *)b, l, f)) == SOCKET_ERROR) {
612 errno = WSAGetLastError();
613 return -1;
614 } else
615 return result;
616 }
617 #define recv(s,b,l,f) Squid::recv(s,b,l,f)
618
619 inline ssize_t
620 recvfrom(int s, void * b, size_t l, int f, struct sockaddr * fr, socklen_t * fl)
621 {
622 ssize_t result;
623 int ifl=*fl;
624 if ((result = ::recvfrom(_get_osfhandle(s), (char *)b, l, f, fr, &ifl)) == SOCKET_ERROR) {
625 errno = WSAGetLastError();
626 return -1;
627 } else
628 return result;
629 }
630 #define recvfrom(s,b,l,f,r,n) Squid::recvfrom(s,b,l,f,r,n)
631
632 inline int
633 select(int n, fd_set * r, fd_set * w, fd_set * e, struct timeval * t)
634 {
635 int result;
636 if ((result = ::select(n,r,w,e,t)) == SOCKET_ERROR) {
637 errno = WSAGetLastError();
638 return -1;
639 } else
640 return result;
641 }
642 #define select(n,r,w,e,t) Squid::select(n,r,w,e,t)
643
644 inline ssize_t
645 send(int s, const char * b, size_t l, int f)
646 {
647 ssize_t result;
648 if ((result = ::send(_get_osfhandle(s), b, l, f)) == SOCKET_ERROR) {
649 errno = WSAGetLastError();
650 return -1;
651 } else
652 return result;
653 }
654 #define send(s,b,l,f) Squid::send(s,b,l,f)
655
656 inline ssize_t
657 sendto(int s, const void * b, size_t l, int f, const struct sockaddr * t, socklen_t tl)
658 {
659 ssize_t result;
660 if ((result = ::sendto(_get_osfhandle(s), (char *)b, l, f, t, tl)) == SOCKET_ERROR) {
661 errno = WSAGetLastError();
662 return -1;
663 } else
664 return result;
665 }
666 #define sendto(a,b,l,f,t,n) Squid::sendto(a,b,l,f,t,n)
667
668 inline int
669 setsockopt(SOCKET s, int l, int o, const void * v, socklen_t n)
670 {
671 SOCKET socket;
672
673 socket = ((s == INVALID_SOCKET) ? s : (SOCKET)_get_osfhandle((int)s));
674
675 if (::setsockopt(socket, l, o, (const char *)v, n) == SOCKET_ERROR) {
676 errno = WSAGetLastError();
677 return -1;
678 } else
679 return 0;
680 }
681 #define setsockopt(s,l,o,v,n) Squid::setsockopt(s,l,o,v,n)
682
683 inline int
684 shutdown(int s, int h)
685 {
686 if (::shutdown(_get_osfhandle(s),h) == SOCKET_ERROR) {
687 errno = WSAGetLastError();
688 return -1;
689 } else
690 return 0;
691 }
692 #define shutdown(s,h) Squid::shutdown(s,h)
693
694 inline int
695 socket(int f, int t, int p)
696 {
697 SOCKET result;
698 if ((result = ::socket(f, t, p)) == INVALID_SOCKET) {
699 if (WSAEMFILE == (errno = WSAGetLastError()))
700 errno = EMFILE;
701 return -1;
702 } else
703 return _open_osfhandle(result, 0);
704 }
705 #define socket(f,t,p) Squid::socket(f,t,p)
706
707 inline int
708 pipe(int pipefd[2])
709 {
710 return _pipe(pipefd,4096,_O_BINARY);
711 }
712 #define pipe(a) Squid::pipe(a)
713
714 inline int
715 WSAAsyncSelect(int s, HWND h, unsigned int w, long e)
716 {
717 if (::WSAAsyncSelect(_get_osfhandle(s), h, w, e) == SOCKET_ERROR) {
718 errno = WSAGetLastError();
719 return -1;
720 } else
721 return 0;
722 }
723
724 #undef WSADuplicateSocket
725 inline int
726 WSADuplicateSocket(int s, DWORD n, LPWSAPROTOCOL_INFO l)
727 {
728 #ifdef UNICODE
729 if (::WSADuplicateSocketW(_get_osfhandle(s), n, l) == SOCKET_ERROR) {
730 #else
731 if (::WSADuplicateSocketA(_get_osfhandle(s), n, l) == SOCKET_ERROR) {
732 #endif
733 errno = WSAGetLastError();
734 return -1;
735 } else
736 return 0;
737 }
738
739 #undef WSASocket
740 inline int
741 WSASocket(int a, int t, int p, LPWSAPROTOCOL_INFO i, GROUP g, DWORD f)
742 {
743 SOCKET result;
744 #ifdef UNICODE
745 if ((result = ::WSASocketW(a, t, p, i, g, f)) == INVALID_SOCKET) {
746 #else
747 if ((result = ::WSASocketA(a, t, p, i, g, f)) == INVALID_SOCKET) {
748 #endif
749 if (WSAEMFILE == (errno = WSAGetLastError()))
750 errno = EMFILE;
751 return -1;
752 } else
753 return _open_osfhandle(result, 0);
754 }
755
756 } /* namespace Squid */
757
758 #else /* #ifdef __cplusplus */
759 #define connect(s,n,l) \
760 (SOCKET_ERROR == connect(_get_osfhandle(s),n,l) ? \
761 (WSAEMFILE == (errno = WSAGetLastError()) ? errno = EMFILE : -1, -1) : 0)
762 #define gethostbyname(n) \
763 (NULL == ((HOSTENT FAR*)(ws32_result = (int)gethostbyname(n))) ? \
764 (errno = WSAGetLastError()), (HOSTENT FAR*)NULL : (HOSTENT FAR*)ws32_result)
765 #define gethostname(n,l) \
766 (SOCKET_ERROR == gethostname(n,l) ? \
767 (errno = WSAGetLastError()), -1 : 0)
768 #define recv(s,b,l,f) \
769 (SOCKET_ERROR == (ws32_result = recv(_get_osfhandle(s),b,l,f)) ? \
770 (errno = WSAGetLastError()), -1 : ws32_result)
771 #define sendto(s,b,l,f,t,tl) \
772 (SOCKET_ERROR == (ws32_result = sendto(_get_osfhandle(s),b,l,f,t,tl)) ? \
773 (errno = WSAGetLastError()), -1 : ws32_result)
774 #define select(n,r,w,e,t) \
775 (SOCKET_ERROR == (ws32_result = select(n,r,w,e,t)) ? \
776 (errno = WSAGetLastError()), -1 : ws32_result)
777 #define socket(f,t,p) \
778 (INVALID_SOCKET == ((SOCKET)(ws32_result = (int)socket(f,t,p))) ? \
779 ((WSAEMFILE == (errno = WSAGetLastError()) ? errno = EMFILE : -1), -1) : \
780 (SOCKET)_open_osfhandle(ws32_result,0))
781 #define write _write /* Needed in util.c */
782 #define open _open /* Needed in win32lib.c */
783 #endif /* #ifdef __cplusplus */
784
785 #if HAVE_SYS_RESOURCE_H
786 #include <sys/resource.h>
787 #else
788 #define RUSAGE_SELF 0 /* calling process */
789 #define RUSAGE_CHILDREN -1 /* terminated child processes */
790
791 struct rusage {
792 struct timeval ru_utime; /* user time used */
793 struct timeval ru_stime; /* system time used */
794 long ru_maxrss; /* integral max resident set size */
795 long ru_ixrss; /* integral shared text memory size */
796 long ru_idrss; /* integral unshared data size */
797 long ru_isrss; /* integral unshared stack size */
798 long ru_minflt; /* page reclaims */
799 long ru_majflt; /* page faults */
800 long ru_nswap; /* swaps */
801 long ru_inblock; /* block input operations */
802 long ru_oublock; /* block output operations */
803 long ru_msgsnd; /* messages sent */
804 long ru_msgrcv; /* messages received */
805 long ru_nsignals; /* signals received */
806 long ru_nvcsw; /* voluntary context switches */
807 long ru_nivcsw; /* involuntary context switches */
808 };
809 #endif /* HAVE_SYS_RESOURCE_H */
810
811 #undef ACL
812
813 SQUIDCEXTERN int chroot(const char *dirname);
814 SQUIDCEXTERN int kill(pid_t, int);
815 #if !_SQUID_MINGW_
816 SQUIDCEXTERN int statfs(const char *, struct statfs *);
817 #endif
818 SQUIDCEXTERN struct passwd * getpwnam(char *unused);
819 SQUIDCEXTERN struct group * getgrnam(char *unused);
820
821 static inline uid_t
822 geteuid(void)
823 {
824 return 100;
825 }
826 static inline int
827 seteuid (uid_t euid)
828 {
829 return 0;
830 }
831 static inline uid_t
832 getuid(void)
833 {
834 return 100;
835 }
836 static inline int
837 setuid (uid_t uid)
838 {
839 return 0;
840 }
841 static inline gid_t
842 getegid(void)
843 {
844 return 100;
845 }
846 static inline int
847 setegid (gid_t egid)
848 {
849 return 0;
850 }
851 static inline int
852 getgid(void)
853 {
854 return 100;
855 }
856 static inline int
857 setgid (gid_t gid)
858 {
859 return 0;
860 }
861
862 /* for some reason autoconf misdetects getpagesize.. */
863 #if HAVE_GETPAGESIZE && _SQUID_MINGW_
864 #undef HAVE_GETPAGESIZE
865 #endif
866
867 #if !HAVE_GETPAGESIZE
868 /* And now we define a compatibility layer */
869 size_t getpagesize();
870 #define HAVE_GETPAGESIZE 2
871 #endif
872
873 SQUIDCEXTERN void WIN32_ExceptionHandlerInit(void);
874 SQUIDCEXTERN int Win32__WSAFDIsSet(int fd, fd_set* set);
875 SQUIDCEXTERN DWORD WIN32_IpAddrChangeMonitorInit();
876
877 /* gcc doesn't recognize the Windows native 64 bit formatting tags causing
878 * the compile fail, so we must disable the check on native Windows.
879 */
880 #if __GNUC__
881 #define PRINTF_FORMAT_ARG1
882 #define PRINTF_FORMAT_ARG2
883 #define PRINTF_FORMAT_ARG3
884 #endif
885
886 /* XXX: the logic around this is a bit warped:
887 * we #define ACL unconditionally at the top of this file,
888 * then #undef ACL unconditionally hafway down,
889 * then here re-define ACL to the same value as at the top,
890 * then include windows.h and #undef ACL again.
891 */
892 #ifndef ACL
893 #define ACL WindowsACL
894 #define _MSWIN_ACL_WAS_NOT_DEFINED 1
895 #endif
896 #include <windows.h>
897 #if _MSWIN_ACL_WAS_NOT_DEFINED
898 #undef ACL
899 #undef _MSWIN_ACL_WAS_NOT_DEFINED
900 #endif
901
902 #if !HAVE_SYSLOG
903 /* syslog compatibility layer derives from git */
904 #define LOG_PID 0x01
905 #define LOG_EMERG 0
906 #define LOG_ALERT 1
907 #define LOG_CRIT 2
908 #define LOG_ERR 3
909 #define LOG_WARNING 4
910 #define LOG_NOTICE 5
911 #define LOG_INFO 6
912 #define LOG_DEBUG 7
913 #define LOG_DAEMON (3<<3)
914
915 void openlog(const char *ident, int logopt, int facility);
916 void syslog(int priority, const char *fmt, ...);
917 #endif
918
919 #if _SQUID_MINGW_
920 /* MinGW missing bits from sys/wait.h */
921 /* A status looks like:
922 * <2 bytes info> <2 bytes code>
923 *
924 * <code> == 0, child has exited, info is the exit value
925 * <code> == 1..7e, child has exited, info is the signal number.
926 * <code> == 7f, child has stopped, info was the signal number.
927 * <code> == 80, there was a core dump.
928 */
929 #define WIFEXITED(w) (((w) & 0xff) == 0)
930 #define WIFSIGNALED(w) (((w) & 0x7f) > 0 && (((w) & 0x7f) < 0x7f))
931 #define WIFSTOPPED(w) (((w) & 0xff) == 0x7f)
932 #define WEXITSTATUS(w) (((w) >> 8) & 0xff)
933 #define WTERMSIG(w) ((w) & 0x7f)
934 #define WSTOPSIG WEXITSTATUS
935 #endif
936
937 /* prototypes */
938 void WIN32_maperror(unsigned long WIN32_oserrno);
939
940 #include "compat/strsep.h"
941
942 #endif /* _SQUID_WINDOWS_ */
943 #endif /* SQUID_OS_MSWINDOWS_H */