1 /* win32.h
2 *
3 */
4
5 #ifndef WIN32_H
6 #define WIN32_H
7
8 #undef _WIN32_WINNT
9 #define _WIN32_WINNT 0x0501 /* Needed to resolve getaddrinfo et al. */
10
11 #include <winsock2.h>
12 #include <ws2tcpip.h>
13
14 #include <stdio.h>
15 #include <io.h>
16 #include <time.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <stdint.h>
20 #include <process.h>
21
22 #define EWOULDBLOCK EAGAIN
23 #define EAFNOSUPPORT 47
24 #define EADDRINUSE WSAEADDRINUSE
25 #define ENOTCONN WSAENOTCONN
26 #define ECONNRESET WSAECONNRESET
27 #define EAI_SYSTEM -11
28
29 #ifndef SIGHUP
30 #define SIGHUP -1
31 #endif
32
33 typedef char *caddr_t;
34
35 #define O_BLOCK 0
36 #define O_NONBLOCK 1
37 #define F_GETFL 3
38 #define F_SETFL 4
39
40 #define RUSAGE_SELF 0
41
42 #define IOV_MAX 1024
43
44 struct msghdr {
45 void *msg_name; /* Socket name */
46 int msg_namelen; /* Length of name */
47 struct iovec *msg_iov; /* Data blocks */
48 int msg_iovlen; /* Number of blocks */
49 void *msg_accrights; /* Per protocol magic (eg BSD file descriptor passing) */
50 int msg_accrightslen; /* Length of rights list */
51 };
52
53 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
54 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
55 #else
56 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
57 #endif
58
59 /* Structure which says how much of each resource has been used. */
60 struct rusage {
61 /* Total amount of user time used. */
62 struct timeval ru_utime;
63 /* Total amount of system time used. */
64 struct timeval ru_stime;
65 /* Maximum resident set size (in kilobytes). */
66 long int ru_maxrss;
67 /* Amount of sharing of text segment memory
68 with other processes (kilobyte-seconds). */
69 long int ru_ixrss;
70 /* Amount of data segment memory used (kilobyte-seconds). */
71 long int ru_idrss;
72 /* Amount of stack memory used (kilobyte-seconds). */
73 long int ru_isrss;
74 /* Number of soft page faults (i.e. those serviced by reclaiming
75 a page from the list of pages awaiting reallocation. */
76 long int ru_minflt;
77 /* Number of hard page faults (i.e. those that required I/O). */
78 long int ru_majflt;
79 /* Number of times a process was swapped out of physical memory. */
80 long int ru_nswap;
81 /* Number of input operations via the file system. Note: This
82 and `ru_oublock' do not include operations with the cache. */
83 long int ru_inblock;
84 /* Number of output operations via the file system. */
85 long int ru_oublock;
86 /* Number of IPC messages sent. */
87 long int ru_msgsnd;
88 /* Number of IPC messages received. */
89 long int ru_msgrcv;
90 /* Number of signals delivered. */
91 long int ru_nsignals;
92 /* Number of voluntary context switches, i.e. because the process
93 gave up the process before it had to (usually to wait for some
94 resource to be available). */
95 long int ru_nvcsw;
96 /* Number of involuntary context switches, i.e. a higher priority process
97 became runnable or the current process used up its time slice. */
98 long int ru_nivcsw;
99 };
100
101 int inet_aton(register const char *cp, struct in_addr *addr);
102
103 #define close(s) closesocket(s)
104
105 #if !defined(strtok_r)
106 #define strtok_r( _s, _sep, _lasts ) \
107 ( *(_lasts) = strtok( (_s), (_sep) ) )
108 #endif /* strtok_r */
109
mapErr(int error)110 static inline void mapErr(int error) {
111 switch(error) {
112 default:
113 errno = ECONNRESET;
114 break;
115 case WSAEPFNOSUPPORT:
116 errno = EAFNOSUPPORT;
117 break;
118 case WSA_IO_PENDING:
119 case WSATRY_AGAIN:
120 errno = EAGAIN;
121 break;
122 case WSAEWOULDBLOCK:
123 errno = EWOULDBLOCK;
124 break;
125 case WSAEMSGSIZE:
126 errno = E2BIG;
127 break;
128 case WSAECONNRESET:
129 errno = 0;
130 break;
131 }
132 }
133
134 #define write mem_write
135
mem_write(int s,void * buf,size_t len)136 static inline size_t mem_write(int s, void *buf, size_t len)
137 {
138 DWORD dwBufferCount = 0;
139 int error;
140
141 WSABUF wsabuf = { len, (char *)buf} ;
142 if(WSASend(s, &wsabuf, 1, &dwBufferCount, 0, NULL, NULL) == 0) {
143 return dwBufferCount;
144 }
145 error = WSAGetLastError();
146 if(error == WSAECONNRESET) return 0;
147 mapErr(error);
148 return -1;
149 }
150
151 #define read(a,b,c) mem_recv(a,b,c,0)
152 #define recv(a,b,c,d) mem_recv(a,b,c,d)
153
mem_recv(int s,void * buf,size_t len,int unused)154 static inline size_t mem_recv(int s, void *buf, size_t len, int unused)
155 {
156 DWORD flags = 0;
157 DWORD dwBufferCount;
158 WSABUF wsabuf = { len, (char *)buf };
159 int error;
160
161 if(WSARecv((SOCKET)s, &wsabuf, 1, &dwBufferCount, &flags,
162 NULL, NULL) == 0) {
163 return dwBufferCount;
164 }
165 error = WSAGetLastError();
166 if (error == WSAECONNRESET) {
167 return 0;
168 }
169 mapErr(error);
170 return -1;
171 }
172
sendmsg(int s,const struct msghdr * msg,int flags)173 static inline int sendmsg(int s, const struct msghdr *msg, int flags)
174 {
175 DWORD dwBufferCount;
176 int error;
177
178 if(WSASendTo((SOCKET) s,
179 (LPWSABUF)msg->msg_iov,
180 (DWORD)msg->msg_iovlen,
181 &dwBufferCount,
182 flags,
183 msg->msg_name,
184 msg->msg_namelen,
185 NULL,
186 NULL
187 ) == 0) {
188 return dwBufferCount;
189 }
190 error = WSAGetLastError();
191 if (error == WSAECONNRESET) return 0;
192 mapErr(error);
193 return -1;
194 }
195
196 int getrusage(int who, struct rusage *usage);
197 int kill(int pid, int sig);
198 int sleep(int seconds);
199
200 struct sigaction {
201 void (*sa_handler)(int);
202 int sa_mask;
203 int sa_flags;
204 };
205
206 #define sigemptyset(a) 0
207 extern int sigaction(int sig, struct sigaction *act, struct sigaction *oact);
208 #define daemonize(a,b) spawn_memcached(argc, argv)
209 extern int spawn_memcached(int argc, char **argv);
210 #endif
211