client_login_timeout: check wait_for_welcome
[pgbouncer.git] / win32 / win32support.h
blob4a5f9b224dc2d66504be87f6247b308a1d3c6e14
1 #ifndef _CONFIG_WIN32_
2 #define _CONFIG_WIN32_
4 #define WIN32_LEAN_AND_MEAN
6 #include <errno.h>
7 #include <stdint.h>
8 #include <winsock2.h>
9 #include <ws2tcpip.h>
10 #include <time.h>
12 #define ECONNABORTED WSAECONNABORTED
13 #define EMSGSIZE WSAEMSGSIZE
14 #define EINPROGRESS WSAEWOULDBLOCK // WSAEINPROGRESS
16 #undef EAGAIN
17 #define EAGAIN WSAEWOULDBLOCK // WSAEAGAIN
19 /* dummy types / functions */
20 #define uid_t int
21 #define gid_t int
22 #define hstrerror strerror
23 #define getuid() (6667)
24 #define setsid() getpid()
25 #define setgid(x) (-1)
26 #define setuid(x) (-1)
27 #define fork() (-1)
28 #define geteuid() getuid()
29 #define setgroups(s, p) (-1)
31 #define srandom(s) srand(s)
32 #define random() rand()
34 typedef enum
36 LOG_CRIT = -4,
37 LOG_ERR,
38 LOG_WARNING,
39 LOG_INFO,
40 LOG_DEBUG
41 } Log_Level;
43 #define in_addr_t uint32_t
46 * make recvmsg/sendmsg and fd related code compile
49 struct iovec {
50 void *iov_base; /* Base address. */
51 size_t iov_len; /* Length. */
54 struct msghdr {
55 void *msg_name;
56 socklen_t msg_namelen;
57 struct iovec *msg_iov;
58 int msg_iovlen;
59 void *msg_control;
60 socklen_t msg_controllen;
61 int msg_flags;
64 struct cmsghdr {
65 socklen_t cmsg_len;
66 int cmsg_level;
67 int cmsg_type;
71 #define SCM_RIGHTS 1
73 #define CMSG_DATA(cmsg) ((unsigned char *) ((struct cmsghdr *) (cmsg) + 1))
74 #define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) \
75 & ~(sizeof (size_t) - 1))
76 #define CMSG_LEN(len) ((int)(CMSG_ALIGN(sizeof(struct cmsghdr))+(len)))
77 #define CMSG_FIRSTHDR(mhdr) \
78 ((mhdr)->msg_controllen >= (int)sizeof(struct cmsghdr) ? \
79 (struct cmsghdr *)(mhdr)->msg_control : \
80 (struct cmsghdr *)NULL)
81 #define CMSG_NXTHDR(mhdr, cmsg) \
82 (((cmsg) == NULL) ? CMSG_FIRSTHDR(mhdr) : \
83 (((u_char *)(cmsg) + CMSG_ALIGN((cmsg)->cmsg_len) \
84 + CMSG_ALIGN(sizeof(struct cmsghdr)) > \
85 (u_char *)((mhdr)->msg_control) + (mhdr)->msg_controllen) ? \
86 (struct cmsghdr *)NULL : \
87 (struct cmsghdr *)((u_char *)(cmsg) + CMSG_ALIGN((cmsg)->cmsg_len))))
88 #define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr))+CMSG_ALIGN(len))
91 * unify WSAGetLastError() with errno.
94 static inline int ewrap(int res) {
95 if (res < 0)
96 errno = WSAGetLastError();
97 return res;
100 /* proper signature for setsockopt */
101 static inline int w_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
103 return ewrap(setsockopt(fd, level, optname, optval, optlen));
105 #define setsockopt(a,b,c,d,e) w_setsockopt(a,b,c,d,e)
107 #define getsockopt(a,b,c,d,e) ewrap(getsockopt(a,b,c,d,e))
108 #define connect(a,b,c) ewrap(connect(a,b,c))
109 #define recv(a,b,c,d) ewrap(recv(a,b,c,d))
110 #define send(a,b,c,d) ewrap(send(a,b,c,d))
111 #define socket(a,b,c) ewrap(socket(a,b,c))
112 #define bind(a,b,c) ewrap(bind(a,b,c))
113 #define listen(a,b) ewrap(listen(a,b))
114 #define accept(a,b,c) ewrap(accept(a,b,c))
115 #define getpeername(a,b,c) ewrap(getpeername(a,b,c))
116 #define getsockname(a,b,c) ewrap(getsockname(a,b,c))
117 #define select(a,b,c,d,e) ewrap(select(a,b,c,d,e))
119 static inline struct hostent *w_gethostbyname(const char *n) {
120 struct hostent *res = gethostbyname(n);
121 if (!res) errno = WSAGetLastError();
122 return res;
124 #define gethostbyname(a) w_gethostbyname(a)
126 const char *win32_strerror(int e);
128 static inline const char *w_strerror(int e) {
129 if (e > 900)
130 return win32_strerror(e);
131 return strerror(e);
133 #define strerror(x) w_strerror(x)
136 /* gettimeoutday() */
137 static inline int win32_gettimeofday(struct timeval * tp, void * tzp)
139 FILETIME file_time;
140 SYSTEMTIME system_time;
141 ULARGE_INTEGER ularge;
142 __int64 epoch = 116444736000000000LL;
144 GetSystemTime(&system_time);
145 SystemTimeToFileTime(&system_time, &file_time);
146 ularge.LowPart = file_time.dwLowDateTime;
147 ularge.HighPart = file_time.dwHighDateTime;
149 tp->tv_sec = (long) ((ularge.QuadPart - epoch) / 10000000L);
150 tp->tv_usec = (long) (system_time.wMilliseconds * 1000);
152 return 0;
154 #define gettimeofday win32_gettimeofday
156 /* make unix socket related code compile */
157 struct sockaddr_un {
158 int sun_family;
159 char sun_path[128];
162 /* getrlimit() */
163 #define RLIMIT_NOFILE -1
164 struct rlimit {
165 int rlim_cur;
166 int rlim_max;
168 static inline int getrlimit(int res, struct rlimit *dst)
170 dst->rlim_cur = dst->rlim_max = -1;
171 return 0;
174 /* kill is only used to detect if process is running (ESRCH->not) */
175 static inline int kill(int pid, int sig)
177 HANDLE hProcess;
178 DWORD exitCode;
179 int ret = 0;
181 if (sig != 0) {
182 errno = EINVAL;
183 return -1;
186 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
187 if (hProcess == NULL) {
188 if (GetLastError() == ERROR_INVALID_PARAMETER)
189 ret = ESRCH;
190 else
191 ret = EPERM;
192 } else {
193 /* OpenProcess may succed for exited processes */
194 if (GetExitCodeProcess(hProcess, &exitCode)) {
195 if (exitCode != STILL_ACTIVE)
196 ret = ESRCH;
198 CloseHandle(hProcess);
201 if (ret) {
202 errno = ret;
203 return -1;
204 } else
205 return 0;
208 /* sendmsg is not used */
209 static inline int sendmsg(int s, const struct msghdr *m, int flags)
211 if (m->msg_iovlen != 1) {
212 errno = EINVAL;
213 return -1;
215 return send(s, m->msg_iov[0].iov_base,
216 m->msg_iov[0].iov_len, flags);
219 /* recvmsg() is, but only with one iov */
220 static inline int recvmsg(int s, struct msghdr *m, int flags)
222 if (m->msg_iovlen != 1) {
223 errno = EINVAL;
224 return -1;
226 if (m->msg_controllen)
227 m->msg_controllen = 0;
228 return recv(s, m->msg_iov[0].iov_base,
229 m->msg_iov[0].iov_len, flags);
232 /* dummy getpwnam() */
233 struct passwd {
234 char *pw_name;
235 char *pw_passwd;
236 int pw_uid;
237 int pw_gid;
239 static inline const struct passwd * getpwnam(const char *u) { return NULL; }
242 * fcntl
245 #define F_GETFD 1
246 #define F_SETFD 2
247 #define F_GETFL 3
248 #define F_SETFL 4
249 #define O_NONBLOCK 1
250 #define FD_CLOEXEC HANDLE_FLAG_INHERIT
252 static inline int fcntl(int fd, int cmd, long arg)
254 ULONG lval;
255 DWORD dval;
256 switch (cmd) {
257 case F_GETFD:
258 if (GetHandleInformation((HANDLE)fd, &dval))
259 return dval;
260 errno = EINVAL;
261 return -1;
262 case F_SETFD:
263 /* set FD_CLOEXEC */
264 if (SetHandleInformation((HANDLE)fd, FD_CLOEXEC, arg))
265 return 0;
266 errno = EINVAL;
267 return -1;
268 case F_GETFL:
269 /* O_NONBLOCK? */
270 return 0;
271 case F_SETFL:
272 /* set O_NONBLOCK */
273 lval = (arg & O_NONBLOCK) ? 1 : 0;
274 if (ioctlsocket(fd, FIONBIO, &lval) == SOCKET_ERROR) {
275 errno = WSAGetLastError();
276 return -1;
278 return 0;
279 default:
280 errno = EINVAL;
281 return -1;
286 * syslog
289 #define LOG_EMERG 0
290 #define LOG_ALERT 1
291 #define LOG_CRIT 2
292 #define LOG_ERR 3
293 #define LOG_WARNING 4
294 #define LOG_NOTICE 5
295 #define LOG_INFO 6
296 #define LOG_DEBUG 7
298 #define LOG_PID 0
300 #define LOG_KERN 0
301 #define LOG_USER 0
302 #define LOG_MAIL 0
303 #define LOG_DAEMON 0
304 #define LOG_AUTH 0
305 #define LOG_SYSLOG 0
306 #define LOG_LPR 0
307 #define LOG_NEWS 0
308 #define LOG_UUCP 0
309 #define LOG_CRON 0
310 #define LOG_AUTHPRIV 0
311 #define LOG_FTP 0
312 #define LOG_LOCAL0 0
313 #define LOG_LOCAL1 0
314 #define LOG_LOCAL2 0
315 #define LOG_LOCAL3 0
316 #define LOG_LOCAL4 0
317 #define LOG_LOCAL5 0
318 #define LOG_LOCAL6 0
319 #define LOG_LOCAL7 0
321 static inline void openlog(const char *ident, int option, int facility) {}
322 static inline void closelog(void) {}
323 void win32_eventlog(int priority, const char *format, ...);
324 #define syslog win32_eventlog
326 /* redirect main() */
327 #define main(a,b) real_main(a,b)
328 int real_main(int argc, char *argv[]);
330 #endif /* _CONFIG_WIN32_ */