]>
Commit | Line | Data |
---|---|---|
b5d712b5 | 1 | /* |
262a0e14 | 2 | * $Id$ |
b5d712b5 | 3 | * |
4 | * DEBUG: section 54 Windows Interprocess Communication | |
1b52df9a | 5 | * AUTHOR: Andrey Shorin <tolsty@tushino.com> |
6 | * AUTHOR: Guido Serassio <serassio@squid-cache.org> | |
b5d712b5 | 7 | * |
8 | * SQUID Web Proxy Cache http://www.squid-cache.org/ | |
9 | * ---------------------------------------------------------- | |
10 | * | |
11 | * Squid is the result of efforts by numerous individuals from | |
12 | * the Internet community; see the CONTRIBUTORS file for full | |
13 | * details. Many organizations have provided support for Squid's | |
14 | * development; see the SPONSORS file for full details. Squid is | |
15 | * Copyrighted (C) 2001 by the Regents of the University of | |
16 | * California; see the COPYRIGHT file for full details. Squid | |
17 | * incorporates software developed and/or copyrighted by other | |
18 | * sources; see the CREDITS file for full details. | |
19 | * | |
20 | * This program is free software; you can redistribute it and/or modify | |
21 | * it under the terms of the GNU General Public License as published by | |
22 | * the Free Software Foundation; either version 2 of the License, or | |
23 | * (at your option) any later version. | |
26ac0430 | 24 | * |
b5d712b5 | 25 | * This program is distributed in the hope that it will be useful, |
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
28 | * GNU General Public License for more details. | |
26ac0430 | 29 | * |
b5d712b5 | 30 | * You should have received a copy of the GNU General Public License |
31 | * along with this program; if not, write to the Free Software | |
32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
33 | * | |
34 | */ | |
35 | ||
36 | #include "squid.h" | |
37 | #include "comm.h" | |
38 | #include "fde.h" | |
39 | #include "SquidTime.h" | |
40 | ||
41 | #ifndef _MSWSOCK_ | |
42 | #include <mswsock.h> | |
43 | #endif | |
44 | #include <process.h> | |
45 | ||
26ac0430 | 46 | struct ipc_params { |
b5d712b5 | 47 | int type; |
48 | int crfd; | |
49 | int cwfd; | |
b7ac5457 | 50 | Ip::Address local_addr; |
cc192b50 | 51 | struct addrinfo PS; |
b5d712b5 | 52 | const char *prog; |
53 | char **args; | |
54 | }; | |
55 | ||
26ac0430 | 56 | struct thread_params { |
b5d712b5 | 57 | int type; |
58 | int rfd; | |
59 | int send_fd; | |
60 | const char *prog; | |
61 | pid_t pid; | |
62 | }; | |
63 | ||
64 | static unsigned int __stdcall ipc_thread_1(void *params); | |
65 | static unsigned int __stdcall ipc_thread_2(void *params); | |
66 | ||
67 | static const char *ok_string = "OK\n"; | |
68 | static const char *err_string = "ERR\n"; | |
69 | static const char *shutdown_string = "$shutdown\n"; | |
70 | ||
71 | static const char *hello_string = "hi there\n"; | |
72 | #define HELLO_BUF_SZ 32 | |
73 | static char hello_buf[HELLO_BUF_SZ]; | |
74 | ||
75 | static int | |
76 | ipcCloseAllFD(int prfd, int pwfd, int crfd, int cwfd) | |
77 | { | |
78 | if (prfd >= 0) | |
79 | comm_close(prfd); | |
80 | ||
81 | if (prfd != pwfd) | |
82 | if (pwfd >= 0) | |
83 | comm_close(pwfd); | |
84 | ||
85 | if (crfd >= 0) | |
86 | comm_close(crfd); | |
87 | ||
88 | if (crfd != cwfd) | |
89 | if (cwfd >= 0) | |
90 | comm_close(cwfd); | |
91 | ||
92 | return -1; | |
93 | } | |
94 | ||
95 | static void | |
96 | PutEnvironment() | |
97 | { | |
98 | #if HAVE_PUTENV | |
99 | char *env_str; | |
100 | int tmp_s; | |
62493678 AJ |
101 | env_str = (char *)xcalloc((tmp_s = strlen(Debug::debugOptions) + 32), 1); |
102 | snprintf(env_str, tmp_s, "SQUID_DEBUG=%s", Debug::debugOptions); | |
b5d712b5 | 103 | putenv(env_str); |
104 | #endif | |
105 | } | |
106 | ||
107 | pid_t | |
b7ac5457 | 108 | ipcCreate(int type, const char *prog, const char *const args[], const char *name, Ip::Address &local_addr, int *rfd, int *wfd, void **hIpc) |
b5d712b5 | 109 | { |
110 | unsigned long thread; | |
111 | ||
112 | struct ipc_params params; | |
113 | int opt; | |
114 | int optlen = sizeof(opt); | |
115 | DWORD ecode = 0; | |
116 | pid_t pid; | |
117 | ||
b7ac5457 | 118 | Ip::Address tmp_addr; |
cc192b50 | 119 | struct addrinfo *aiCS = NULL; |
120 | struct addrinfo *aiPS = NULL; | |
b5d712b5 | 121 | |
b5d712b5 | 122 | int crfd = -1; |
123 | int prfd = -1; | |
124 | int cwfd = -1; | |
125 | int pwfd = -1; | |
b5d712b5 | 126 | int x; |
127 | ||
128 | requirePathnameExists(name, prog); | |
129 | ||
130 | if (rfd) | |
131 | *rfd = -1; | |
132 | ||
133 | if (wfd) | |
134 | *wfd = -1; | |
135 | ||
136 | if (hIpc) | |
137 | *hIpc = NULL; | |
138 | ||
139 | if (WIN32_OS_version != _WIN_OS_WINNT) { | |
140 | getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *) &opt, &optlen); | |
141 | opt = opt & ~(SO_SYNCHRONOUS_NONALERT | SO_SYNCHRONOUS_ALERT); | |
142 | setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *) &opt, sizeof(opt)); | |
143 | } | |
144 | ||
145 | if (type == IPC_TCP_SOCKET) { | |
146 | crfd = cwfd = comm_open(SOCK_STREAM, | |
147 | IPPROTO_TCP, | |
148 | local_addr, | |
b5d712b5 | 149 | COMM_NOCLOEXEC, |
150 | name); | |
151 | prfd = pwfd = comm_open(SOCK_STREAM, | |
152 | IPPROTO_TCP, /* protocol */ | |
153 | local_addr, | |
b5d712b5 | 154 | 0, /* blocking */ |
155 | name); | |
156 | } else if (type == IPC_UDP_SOCKET) { | |
157 | crfd = cwfd = comm_open(SOCK_DGRAM, | |
158 | IPPROTO_UDP, | |
159 | local_addr, | |
b5d712b5 | 160 | COMM_NOCLOEXEC, |
161 | name); | |
162 | prfd = pwfd = comm_open(SOCK_DGRAM, | |
163 | IPPROTO_UDP, | |
164 | local_addr, | |
165 | 0, | |
b5d712b5 | 166 | name); |
167 | } else if (type == IPC_FIFO) { | |
bf8fe701 | 168 | debugs(54, 0, "ipcCreate: " << prog << ": use IPC_TCP_SOCKET instead of IP_FIFO on Windows"); |
b5d712b5 | 169 | assert(0); |
170 | } else { | |
171 | assert(IPC_NONE); | |
172 | } | |
173 | ||
bf8fe701 | 174 | debugs(54, 3, "ipcCreate: prfd FD " << prfd); |
175 | debugs(54, 3, "ipcCreate: pwfd FD " << pwfd); | |
176 | debugs(54, 3, "ipcCreate: crfd FD " << crfd); | |
177 | debugs(54, 3, "ipcCreate: cwfd FD " << cwfd); | |
b5d712b5 | 178 | |
179 | if (WIN32_OS_version != _WIN_OS_WINNT) { | |
180 | getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *) &opt, &optlen); | |
181 | opt = opt | SO_SYNCHRONOUS_NONALERT; | |
182 | setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *) &opt, optlen); | |
183 | } | |
184 | ||
185 | if (crfd < 0) { | |
bf8fe701 | 186 | debugs(54, 0, "ipcCreate: Failed to create child FD."); |
b5d712b5 | 187 | return ipcCloseAllFD(prfd, pwfd, crfd, cwfd); |
188 | } | |
189 | ||
190 | if (pwfd < 0) { | |
bf8fe701 | 191 | debugs(54, 0, "ipcCreate: Failed to create server FD."); |
b5d712b5 | 192 | return ipcCloseAllFD(prfd, pwfd, crfd, cwfd); |
193 | } | |
194 | ||
cc192b50 | 195 | // AYJ: these flags should be neutral, but if not IPv6 version needs adding |
b5d712b5 | 196 | if (type == IPC_TCP_SOCKET || type == IPC_UDP_SOCKET) { |
b5d712b5 | 197 | |
cc192b50 | 198 | tmp_addr.InitAddrInfo(aiPS); |
199 | ||
200 | if (getsockname(pwfd, aiPS->ai_addr, &(aiPS->ai_addrlen) ) < 0) { | |
bf8fe701 | 201 | debugs(54, 0, "ipcCreate: getsockname: " << xstrerror()); |
b5d712b5 | 202 | return ipcCloseAllFD(prfd, pwfd, crfd, cwfd); |
203 | } | |
204 | ||
cc192b50 | 205 | tmp_addr = *aiPS; |
206 | ||
207 | debugs(54, 3, "ipcCreate: FD " << pwfd << " sockaddr " << tmp_addr ); | |
b5d712b5 | 208 | |
cc192b50 | 209 | tmp_addr.InitAddrInfo(aiCS); |
210 | ||
211 | if (getsockname(crfd, aiCS->ai_addr, &(aiCS->ai_addrlen) ) < 0) { | |
bf8fe701 | 212 | debugs(54, 0, "ipcCreate: getsockname: " << xstrerror()); |
b5d712b5 | 213 | return ipcCloseAllFD(prfd, pwfd, crfd, cwfd); |
214 | } | |
215 | ||
cc192b50 | 216 | tmp_addr.SetEmpty(); |
217 | tmp_addr = *aiCS; | |
218 | ||
219 | debugs(54, 3, "ipcCreate: FD " << crfd << " sockaddr " << tmp_addr ); | |
b5d712b5 | 220 | } |
221 | ||
222 | if (type == IPC_TCP_SOCKET) { | |
223 | if (listen(crfd, 1) < 0) { | |
bf8fe701 | 224 | debugs(54, 1, "ipcCreate: listen FD " << crfd << ": " << xstrerror()); |
b5d712b5 | 225 | return ipcCloseAllFD(prfd, pwfd, crfd, cwfd); |
226 | } | |
227 | ||
bf8fe701 | 228 | debugs(54, 3, "ipcCreate: FD " << crfd << " listening..."); |
b5d712b5 | 229 | } |
230 | ||
231 | /* flush or else we get dup data if unbuffered_logs is set */ | |
232 | logsFlush(); | |
233 | ||
234 | params.type = type; | |
235 | ||
236 | params.crfd = crfd; | |
237 | ||
238 | params.cwfd = cwfd; | |
239 | ||
cc192b50 | 240 | params.PS = *aiPS; |
26ac0430 | 241 | |
cc192b50 | 242 | params.local_addr = local_addr; |
b5d712b5 | 243 | |
244 | params.prog = prog; | |
245 | ||
246 | params.args = (char **) args; | |
247 | ||
248 | thread = _beginthreadex(NULL, 0, ipc_thread_1, ¶ms, 0, NULL); | |
249 | ||
250 | if (thread == 0) { | |
bf8fe701 | 251 | debugs(54, 1, "ipcCreate: _beginthread: " << xstrerror()); |
b5d712b5 | 252 | return ipcCloseAllFD(prfd, pwfd, crfd, cwfd); |
253 | } | |
254 | ||
b7ac5457 | 255 | /* NP: tmp_addr was left with eiether empty or aiCS in Ip::Address format */ |
cc192b50 | 256 | if (comm_connect_addr(pwfd, tmp_addr) == COMM_ERROR) { |
b5d712b5 | 257 | CloseHandle((HANDLE) thread); |
258 | return ipcCloseAllFD(prfd, pwfd, -1, -1); | |
259 | } | |
260 | ||
261 | memset(hello_buf, '\0', HELLO_BUF_SZ); | |
262 | x = recv(prfd, (void *)hello_buf, HELLO_BUF_SZ - 1, 0); | |
263 | ||
264 | if (x < 0) { | |
bf8fe701 | 265 | debugs(54, 0, "ipcCreate: PARENT: hello read test failed"); |
266 | debugs(54, 0, "--> read: " << xstrerror()); | |
b5d712b5 | 267 | CloseHandle((HANDLE) thread); |
268 | return ipcCloseAllFD(prfd, pwfd, -1, -1); | |
269 | } else if (strcmp(hello_buf, hello_string)) { | |
bf8fe701 | 270 | debugs(54, 0, "ipcCreate: PARENT: hello read test failed"); |
271 | debugs(54, 0, "--> read returned " << x); | |
272 | debugs(54, 0, "--> got '" << rfc1738_escape(hello_buf) << "'"); | |
b5d712b5 | 273 | CloseHandle((HANDLE) thread); |
274 | return ipcCloseAllFD(prfd, pwfd, -1, -1); | |
275 | } | |
276 | ||
277 | x = send(pwfd, (const void *)ok_string, strlen(ok_string), 0); | |
278 | ||
279 | if (x < 0) { | |
bf8fe701 | 280 | debugs(54, 0, "ipcCreate: PARENT: OK write test failed"); |
281 | debugs(54, 0, "--> read: " << xstrerror()); | |
b5d712b5 | 282 | CloseHandle((HANDLE) thread); |
283 | return ipcCloseAllFD(prfd, pwfd, -1, -1); | |
284 | } | |
285 | ||
286 | memset(hello_buf, '\0', HELLO_BUF_SZ); | |
287 | x = recv(prfd, (void *)hello_buf, HELLO_BUF_SZ - 1, 0); | |
288 | ||
289 | if (x < 0) { | |
bf8fe701 | 290 | debugs(54, 0, "ipcCreate: PARENT: OK read test failed"); |
291 | debugs(54, 0, "--> read: " << xstrerror()); | |
b5d712b5 | 292 | CloseHandle((HANDLE) thread); |
293 | return ipcCloseAllFD(prfd, pwfd, -1, -1); | |
294 | } else if (!strcmp(hello_buf, err_string)) { | |
bf8fe701 | 295 | debugs(54, 0, "ipcCreate: PARENT: OK read test failed"); |
296 | debugs(54, 0, "--> read returned " << x); | |
297 | debugs(54, 0, "--> got '" << rfc1738_escape(hello_buf) << "'"); | |
b5d712b5 | 298 | CloseHandle((HANDLE) thread); |
299 | return ipcCloseAllFD(prfd, pwfd, -1, -1); | |
300 | } | |
301 | ||
302 | hello_buf[x] = '\0'; | |
303 | pid = atol(hello_buf); | |
304 | commSetTimeout(prfd, -1, NULL, NULL); | |
305 | commSetNonBlocking(prfd); | |
306 | commSetNonBlocking(pwfd); | |
307 | commSetCloseOnExec(prfd); | |
308 | commSetCloseOnExec(pwfd); | |
309 | ||
310 | if (rfd) | |
311 | *rfd = prfd; | |
312 | ||
313 | if (wfd) | |
314 | *wfd = pwfd; | |
315 | ||
316 | fd_table[prfd].flags.ipc = 1; | |
317 | ||
318 | fd_table[pwfd].flags.ipc = 1; | |
319 | ||
320 | fd_table[crfd].flags.ipc = 1; | |
321 | ||
322 | fd_table[cwfd].flags.ipc = 1; | |
323 | ||
324 | if (Config.sleep_after_fork) { | |
325 | /* XXX emulation of usleep() */ | |
326 | DWORD sl; | |
327 | sl = Config.sleep_after_fork / 1000; | |
328 | ||
329 | if (sl == 0) | |
330 | sl = 1; | |
331 | ||
332 | Sleep(sl); | |
333 | } | |
334 | ||
335 | if (GetExitCodeThread((HANDLE) thread, &ecode) && ecode == STILL_ACTIVE) { | |
336 | if (hIpc) | |
337 | *hIpc = (HANDLE) thread; | |
338 | ||
339 | return pid; | |
340 | } else { | |
341 | CloseHandle((HANDLE) thread); | |
342 | return ipcCloseAllFD(prfd, pwfd, -1, -1); | |
343 | } | |
344 | } | |
345 | ||
346 | static int | |
347 | ipcSend(int cwfd, const char *buf, int len) | |
348 | { | |
349 | int x; | |
350 | ||
351 | x = send(cwfd, (const void *)buf, len, 0); | |
352 | ||
353 | if (x < 0) { | |
bf8fe701 | 354 | debugs(54, 0, "sendto FD " << cwfd << ": " << xstrerror()); |
355 | debugs(54, 0, "ipcCreate: CHILD: hello write test failed"); | |
b5d712b5 | 356 | } |
357 | ||
358 | return x; | |
359 | } | |
360 | ||
361 | static unsigned int __stdcall | |
362 | ipc_thread_1(void *in_params) | |
363 | { | |
364 | int t1, t2, t3, retval = -1; | |
26ac0430 AJ |
365 | int p2c[2] = {-1, -1}; |
366 | int c2p[2] = {-1, -1}; | |
b5d712b5 | 367 | HANDLE hProcess = NULL, thread = NULL; |
368 | pid_t pid = -1; | |
369 | ||
370 | struct thread_params thread_params; | |
371 | ssize_t x; | |
cc192b50 | 372 | int fd = -1; |
b5d712b5 | 373 | char *str; |
374 | STARTUPINFO si; | |
375 | PROCESS_INFORMATION pi; | |
376 | long F; | |
377 | int prfd_ipc = -1, pwfd_ipc = -1, crfd_ipc = -1, cwfd_ipc = -1; | |
378 | char *prog = NULL, *buf1 = NULL; | |
379 | ||
b7ac5457 AJ |
380 | Ip::Address PS_ipc; |
381 | Ip::Address CS_ipc; | |
cc192b50 | 382 | struct addrinfo *aiPS_ipc = NULL; |
383 | struct addrinfo *aiCS_ipc = NULL; | |
b5d712b5 | 384 | |
385 | struct ipc_params *params = (struct ipc_params *) in_params; | |
386 | int type = params->type; | |
387 | int crfd = params->crfd; | |
388 | int cwfd = params->cwfd; | |
389 | char **args = params->args; | |
390 | ||
b7ac5457 AJ |
391 | Ip::Address PS = params->PS; |
392 | Ip::Address local_addr = params->local_addr; | |
26ac0430 | 393 | |
b5d712b5 | 394 | buf1 = (char *)xcalloc(1, 8192); |
395 | strcpy(buf1, params->prog); | |
396 | prog = strtok(buf1, w_space); | |
397 | ||
398 | if ((str = strrchr(prog, '/'))) | |
399 | prog = ++str; | |
400 | ||
401 | if ((str = strrchr(prog, '\\'))) | |
402 | prog = ++str; | |
403 | ||
404 | prog = xstrdup(prog); | |
405 | ||
406 | if (type == IPC_TCP_SOCKET) { | |
bf8fe701 | 407 | debugs(54, 3, "ipcCreate: calling accept on FD " << crfd); |
b5d712b5 | 408 | |
409 | if ((fd = accept(crfd, NULL, NULL)) < 0) { | |
bf8fe701 | 410 | debugs(54, 0, "ipcCreate: FD " << crfd << " accept: " << xstrerror()); |
b5d712b5 | 411 | goto cleanup; |
412 | } | |
413 | ||
bf8fe701 | 414 | debugs(54, 3, "ipcCreate: CHILD accepted new FD " << fd); |
b5d712b5 | 415 | comm_close(crfd); |
416 | snprintf(buf1, 8191, "%s CHILD socket", prog); | |
417 | fd_open(fd, FD_SOCKET, buf1); | |
418 | fd_table[fd].flags.ipc = 1; | |
419 | cwfd = crfd = fd; | |
420 | } else if (type == IPC_UDP_SOCKET) { | |
cc192b50 | 421 | if (comm_connect_addr(crfd, params->PS) == COMM_ERROR) |
b5d712b5 | 422 | goto cleanup; |
423 | } | |
424 | ||
425 | x = send(cwfd, (const void *)hello_string, strlen(hello_string) + 1, 0); | |
426 | ||
427 | if (x < 0) { | |
bf8fe701 | 428 | debugs(54, 0, "sendto FD " << cwfd << ": " << xstrerror()); |
429 | debugs(54, 0, "ipcCreate: CHILD: hello write test failed"); | |
b5d712b5 | 430 | goto cleanup; |
431 | } | |
432 | ||
433 | PutEnvironment(); | |
434 | memset(buf1, '\0', sizeof(buf1)); | |
435 | x = recv(crfd, (void *)buf1, 8191, 0); | |
436 | ||
437 | if (x < 0) { | |
bf8fe701 | 438 | debugs(54, 0, "ipcCreate: CHILD: OK read test failed"); |
439 | debugs(54, 0, "--> read: " << xstrerror()); | |
b5d712b5 | 440 | goto cleanup; |
441 | } else if (strcmp(buf1, ok_string)) { | |
bf8fe701 | 442 | debugs(54, 0, "ipcCreate: CHILD: OK read test failed"); |
443 | debugs(54, 0, "--> read returned " << x); | |
444 | debugs(54, 0, "--> got '" << rfc1738_escape(hello_buf) << "'"); | |
b5d712b5 | 445 | goto cleanup; |
446 | } | |
447 | ||
448 | /* assign file descriptors to child process */ | |
449 | if (_pipe(p2c, 1024, _O_BINARY | _O_NOINHERIT) < 0) { | |
bf8fe701 | 450 | debugs(54, 0, "ipcCreate: CHILD: pipe: " << xstrerror()); |
b5d712b5 | 451 | ipcSend(cwfd, err_string, strlen(err_string)); |
452 | goto cleanup; | |
453 | } | |
454 | ||
455 | if (_pipe(c2p, 1024, _O_BINARY | _O_NOINHERIT) < 0) { | |
bf8fe701 | 456 | debugs(54, 0, "ipcCreate: CHILD: pipe: " << xstrerror()); |
b5d712b5 | 457 | ipcSend(cwfd, err_string, strlen(err_string)); |
458 | goto cleanup; | |
459 | } | |
460 | ||
461 | if (type == IPC_UDP_SOCKET) { | |
462 | snprintf(buf1, 8192, "%s(%ld) <-> ipc CHILD socket", prog, -1L); | |
cc192b50 | 463 | crfd_ipc = cwfd_ipc = comm_open(SOCK_DGRAM, IPPROTO_UDP, local_addr, 0, buf1); |
b5d712b5 | 464 | |
465 | if (crfd_ipc < 0) { | |
bf8fe701 | 466 | debugs(54, 0, "ipcCreate: CHILD: Failed to create child FD for " << prog << "."); |
b5d712b5 | 467 | ipcSend(cwfd, err_string, strlen(err_string)); |
468 | goto cleanup; | |
469 | } | |
470 | ||
471 | snprintf(buf1, 8192, "%s(%ld) <-> ipc PARENT socket", prog, -1L); | |
cc192b50 | 472 | prfd_ipc = pwfd_ipc = comm_open(SOCK_DGRAM, IPPROTO_UDP, local_addr, 0, buf1); |
b5d712b5 | 473 | |
474 | if (pwfd_ipc < 0) { | |
bf8fe701 | 475 | debugs(54, 0, "ipcCreate: CHILD: Failed to create server FD for " << prog << "."); |
b5d712b5 | 476 | ipcSend(cwfd, err_string, strlen(err_string)); |
477 | goto cleanup; | |
478 | } | |
479 | ||
cc192b50 | 480 | PS_ipc.InitAddrInfo(aiPS_ipc); |
b5d712b5 | 481 | |
cc192b50 | 482 | if (getsockname(pwfd_ipc, aiPS_ipc->ai_addr, &(aiPS_ipc->ai_addrlen)) < 0) { |
bf8fe701 | 483 | debugs(54, 0, "ipcCreate: getsockname: " << xstrerror()); |
b5d712b5 | 484 | ipcSend(cwfd, err_string, strlen(err_string)); |
485 | goto cleanup; | |
486 | } | |
487 | ||
cc192b50 | 488 | PS_ipc = *aiPS_ipc; |
489 | ||
490 | debugs(54, 3, "ipcCreate: FD " << pwfd_ipc << " sockaddr " << PS_ipc); | |
bf8fe701 | 491 | |
cc192b50 | 492 | CS_ipc.InitAddrInfo(aiCS_ipc); |
b5d712b5 | 493 | |
cc192b50 | 494 | if (getsockname(crfd_ipc, aiCS_ipc->ai_addr, &(aiCS_ipc->ai_addrlen)) < 0) { |
bf8fe701 | 495 | debugs(54, 0, "ipcCreate: getsockname: " << xstrerror()); |
b5d712b5 | 496 | ipcSend(cwfd, err_string, strlen(err_string)); |
497 | goto cleanup; | |
498 | } | |
499 | ||
cc192b50 | 500 | CS_ipc = *aiCS_ipc; |
501 | ||
502 | debugs(54, 3, "ipcCreate: FD " << crfd_ipc << " sockaddr " << CS_ipc); | |
b5d712b5 | 503 | |
cc192b50 | 504 | if (comm_connect_addr(pwfd_ipc, CS_ipc) == COMM_ERROR) { |
b5d712b5 | 505 | ipcSend(cwfd, err_string, strlen(err_string)); |
506 | goto cleanup; | |
507 | } | |
508 | ||
509 | fd = crfd; | |
510 | ||
cc192b50 | 511 | if (comm_connect_addr(crfd_ipc, PS_ipc) == COMM_ERROR) { |
b5d712b5 | 512 | ipcSend(cwfd, err_string, strlen(err_string)); |
513 | goto cleanup; | |
514 | } | |
515 | } /* IPC_UDP_SOCKET */ | |
516 | ||
517 | t1 = dup(0); | |
518 | ||
519 | t2 = dup(1); | |
520 | ||
521 | t3 = dup(2); | |
522 | ||
523 | dup2(c2p[0], 0); | |
524 | ||
525 | dup2(p2c[1], 1); | |
526 | ||
527 | dup2(fileno(debug_log), 2); | |
528 | ||
529 | close(c2p[0]); | |
530 | ||
531 | close(p2c[1]); | |
532 | ||
533 | commUnsetNonBlocking(fd); | |
534 | ||
535 | memset(&si, 0, sizeof(STARTUPINFO)); | |
536 | ||
537 | si.cb = sizeof(STARTUPINFO); | |
538 | ||
539 | si.hStdInput = (HANDLE) _get_osfhandle(0); | |
540 | ||
541 | si.hStdOutput = (HANDLE) _get_osfhandle(1); | |
542 | ||
543 | si.hStdError = (HANDLE) _get_osfhandle(2); | |
544 | ||
545 | si.dwFlags = STARTF_USESTDHANDLES; | |
546 | ||
547 | /* Make sure all other valid handles are not inerithable */ | |
548 | for (x = 3; x < Squid_MaxFD; x++) { | |
549 | if ((F = _get_osfhandle(x)) == -1) | |
550 | continue; | |
551 | ||
552 | SetHandleInformation((HANDLE) F, HANDLE_FLAG_INHERIT, 0); | |
553 | } | |
554 | ||
555 | *buf1 = '\0'; | |
556 | strcpy(buf1 + 4096, params->prog); | |
557 | str = strtok(buf1 + 4096, w_space); | |
558 | ||
559 | do { | |
560 | strcat(buf1, str); | |
561 | strcat(buf1, " "); | |
562 | } while ((str = strtok(NULL, w_space))); | |
563 | ||
564 | x = 1; | |
565 | ||
566 | while (args[x]) { | |
567 | strcat(buf1, args[x++]); | |
568 | strcat(buf1, " "); | |
569 | } | |
570 | ||
571 | if (CreateProcess(buf1 + 4096, buf1, NULL, NULL, TRUE, CREATE_NO_WINDOW, | |
572 | NULL, NULL, &si, &pi)) { | |
573 | pid = pi.dwProcessId; | |
574 | hProcess = pi.hProcess; | |
575 | } else { | |
576 | pid = -1; | |
577 | WIN32_maperror(GetLastError()); | |
578 | x = errno; | |
579 | } | |
580 | ||
581 | dup2(t1, 0); | |
582 | dup2(t2, 1); | |
583 | dup2(t3, 2); | |
584 | close(t1); | |
585 | close(t2); | |
586 | close(t3); | |
587 | ||
588 | if (pid == -1) { | |
589 | errno = x; | |
bf8fe701 | 590 | debugs(54, 0, "ipcCreate: CHILD: " << params->prog << ": " << xstrerror()); |
591 | ||
b5d712b5 | 592 | ipcSend(cwfd, err_string, strlen(err_string)); |
593 | goto cleanup; | |
594 | } | |
595 | ||
596 | if (type == IPC_UDP_SOCKET) { | |
597 | WSAPROTOCOL_INFO wpi; | |
598 | ||
599 | memset(&wpi, 0, sizeof(wpi)); | |
600 | ||
601 | if (SOCKET_ERROR == WSADuplicateSocket(crfd_ipc, pid, &wpi)) { | |
bf8fe701 | 602 | debugs(54, 0, "ipcCreate: CHILD: WSADuplicateSocket: " << xstrerror()); |
603 | ||
b5d712b5 | 604 | ipcSend(cwfd, err_string, strlen(err_string)); |
605 | goto cleanup; | |
606 | } | |
607 | ||
608 | x = write(c2p[1], (const char *) &wpi, sizeof(wpi)); | |
609 | ||
610 | if (x < (ssize_t)sizeof(wpi)) { | |
bf8fe701 | 611 | debugs(54, 0, "ipcCreate: CHILD: write FD " << c2p[1] << ": " << xstrerror()); |
612 | debugs(54, 0, "ipcCreate: CHILD: " << prog << ": socket exchange failed"); | |
613 | ||
b5d712b5 | 614 | ipcSend(cwfd, err_string, strlen(err_string)); |
615 | goto cleanup; | |
616 | } | |
617 | ||
618 | x = read(p2c[0], buf1, 8192); | |
619 | ||
620 | if (x < 0) { | |
bf8fe701 | 621 | debugs(54, 0, "ipcCreate: CHILD: read FD " << p2c[0] << ": " << xstrerror()); |
622 | debugs(54, 0, "ipcCreate: CHILD: " << prog << ": socket exchange failed"); | |
623 | ||
b5d712b5 | 624 | ipcSend(cwfd, err_string, strlen(err_string)); |
625 | goto cleanup; | |
626 | } else if (strncmp(buf1, ok_string, strlen(ok_string))) { | |
bf8fe701 | 627 | debugs(54, 0, "ipcCreate: CHILD: " << prog << ": socket exchange failed"); |
628 | debugs(54, 0, "--> read returned " << x); | |
b5d712b5 | 629 | buf1[x] = '\0'; |
bf8fe701 | 630 | debugs(54, 0, "--> got '" << rfc1738_escape(buf1) << "'"); |
b5d712b5 | 631 | ipcSend(cwfd, err_string, strlen(err_string)); |
632 | goto cleanup; | |
633 | } | |
634 | ||
635 | x = write(c2p[1], (const char *) &PS_ipc, sizeof(PS_ipc)); | |
636 | ||
637 | if (x < (ssize_t)sizeof(PS_ipc)) { | |
bf8fe701 | 638 | debugs(54, 0, "ipcCreate: CHILD: write FD " << c2p[1] << ": " << xstrerror()); |
639 | debugs(54, 0, "ipcCreate: CHILD: " << prog << ": socket exchange failed"); | |
640 | ||
b5d712b5 | 641 | ipcSend(cwfd, err_string, strlen(err_string)); |
642 | goto cleanup; | |
643 | } | |
644 | ||
645 | x = read(p2c[0], buf1, 8192); | |
646 | ||
647 | if (x < 0) { | |
bf8fe701 | 648 | debugs(54, 0, "ipcCreate: CHILD: read FD " << p2c[0] << ": " << xstrerror()); |
649 | debugs(54, 0, "ipcCreate: CHILD: " << prog << ": socket exchange failed"); | |
650 | ||
b5d712b5 | 651 | ipcSend(cwfd, err_string, strlen(err_string)); |
652 | goto cleanup; | |
653 | } else if (strncmp(buf1, ok_string, strlen(ok_string))) { | |
bf8fe701 | 654 | debugs(54, 0, "ipcCreate: CHILD: " << prog << ": socket exchange failed"); |
655 | debugs(54, 0, "--> read returned " << x); | |
b5d712b5 | 656 | buf1[x] = '\0'; |
bf8fe701 | 657 | debugs(54, 0, "--> got '" << rfc1738_escape(buf1) << "'"); |
b5d712b5 | 658 | ipcSend(cwfd, err_string, strlen(err_string)); |
659 | goto cleanup; | |
660 | } | |
661 | ||
662 | x = send(pwfd_ipc, (const void *)ok_string, strlen(ok_string), 0); | |
663 | x = recv(prfd_ipc, (void *)(buf1 + 200), 8191 - 200, 0); | |
664 | assert((size_t) x == strlen(ok_string) | |
665 | && !strncmp(ok_string, buf1 + 200, strlen(ok_string))); | |
666 | } /* IPC_UDP_SOCKET */ | |
667 | ||
668 | snprintf(buf1, 8191, "%s(%ld) CHILD socket", prog, (long int) pid); | |
669 | ||
670 | fd_note(fd, buf1); | |
671 | ||
672 | if (prfd_ipc != -1) { | |
673 | snprintf(buf1, 8191, "%s(%ld) <-> ipc CHILD socket", prog, (long int) pid); | |
674 | fd_note(crfd_ipc, buf1); | |
675 | snprintf(buf1, 8191, "%s(%ld) <-> ipc PARENT socket", prog, (long int) pid); | |
676 | fd_note(prfd_ipc, buf1); | |
677 | } | |
678 | ||
679 | /* else { IPC_TCP_SOCKET */ | |
680 | /* commSetNoLinger(fd); */ | |
681 | /* } */ | |
682 | thread_params.prog = prog; | |
683 | ||
684 | thread_params.send_fd = cwfd; | |
685 | ||
686 | thread_params.pid = pid; | |
687 | ||
688 | if ((thread_params.type = type) == IPC_TCP_SOCKET) | |
689 | thread_params.rfd = p2c[0]; | |
690 | else | |
691 | thread_params.rfd = prfd_ipc; | |
692 | ||
6ca34f6f | 693 | thread = (HANDLE)_beginthreadex(NULL, 0, ipc_thread_2, &thread_params, 0, NULL); |
b5d712b5 | 694 | |
695 | if (!thread) { | |
bf8fe701 | 696 | debugs(54, 0, "ipcCreate: CHILD: _beginthreadex: " << xstrerror()); |
b5d712b5 | 697 | ipcSend(cwfd, err_string, strlen(err_string)); |
698 | goto cleanup; | |
699 | } | |
700 | ||
701 | snprintf(buf1, 8191, "%ld\n", (long int) pid); | |
702 | ||
703 | if (-1 == ipcSend(cwfd, buf1, strlen(buf1))) | |
704 | goto cleanup; | |
705 | ||
26ac0430 | 706 | debugs(54, 2, "ipc(" << prog << "," << pid << "): started successfully"); |
b5d712b5 | 707 | |
708 | /* cycle */ | |
709 | for (;;) { | |
710 | x = recv(crfd, (void *)buf1, 8192, 0); | |
711 | ||
712 | if (x <= 0) { | |
bf8fe701 | 713 | debugs(54, 3, "ipc(" << prog << "," << pid << "): " << x << " bytes received from parent. Exiting..."); |
b5d712b5 | 714 | break; |
715 | } | |
716 | ||
717 | buf1[x] = '\0'; | |
718 | ||
719 | if (type == IPC_UDP_SOCKET && !strcmp(buf1, shutdown_string)) { | |
bf8fe701 | 720 | debugs(54, 3, "ipc(" << prog << "," << pid << "): request for shutdown received from parent. Exiting..."); |
721 | ||
b5d712b5 | 722 | TerminateProcess(hProcess, 0); |
723 | break; | |
724 | } | |
725 | ||
bf8fe701 | 726 | debugs(54, 5, "ipc(" << prog << "," << pid << "): received from parent: " << rfc1738_escape_unescaped(buf1)); |
b5d712b5 | 727 | |
728 | if (type == IPC_TCP_SOCKET) | |
729 | x = write(c2p[1], buf1, x); | |
730 | else | |
731 | x = send(pwfd_ipc, (const void *)buf1, x, 0); | |
732 | ||
733 | if (x <= 0) { | |
bf8fe701 | 734 | debugs(54, 3, "ipc(" << prog << "," << pid << "): " << x << " bytes written to " << prog << ". Exiting..."); |
735 | ||
b5d712b5 | 736 | break; |
737 | } | |
738 | } | |
739 | ||
740 | retval = 0; | |
741 | ||
742 | cleanup: | |
743 | ||
744 | if (c2p[1] != -1) | |
745 | close(c2p[1]); | |
746 | ||
747 | if (fd_table[crfd].flags.open) | |
748 | ipcCloseAllFD(-1, -1, crfd, cwfd); | |
749 | ||
750 | if (prfd_ipc != -1) { | |
751 | send(crfd_ipc, (const void *)shutdown_string, strlen(shutdown_string), 0); | |
752 | shutdown(crfd_ipc, SD_BOTH); | |
753 | shutdown(prfd_ipc, SD_BOTH); | |
754 | } | |
755 | ||
756 | ipcCloseAllFD(prfd_ipc, pwfd_ipc, crfd_ipc, cwfd_ipc); | |
757 | ||
758 | if (hProcess && WAIT_OBJECT_0 != | |
759 | WaitForSingleObject(hProcess, type == IPC_UDP_SOCKET ? 12000 : 5000)) { | |
760 | ||
761 | getCurrentTime(); | |
bf8fe701 | 762 | debugs(54, 0, "ipc(" << prog << "," << pid << "): WARNING: " << prog << |
763 | " didn't exit in " << (type == IPC_UDP_SOCKET ? 12 : 5) << " seconds."); | |
764 | ||
b5d712b5 | 765 | } |
766 | ||
767 | if (thread && WAIT_OBJECT_0 != WaitForSingleObject(thread, 3000)) { | |
768 | getCurrentTime(); | |
bf8fe701 | 769 | debugs(54, 0, "ipc(" << prog << "," << pid << "): WARNING: ipc_thread_2 didn't exit in 3 seconds."); |
770 | ||
b5d712b5 | 771 | } |
772 | ||
773 | getCurrentTime(); | |
774 | ||
775 | if (!retval) | |
bf8fe701 | 776 | debugs(54, 2, "ipc(" << prog << "," << pid << "): normal exit"); |
b5d712b5 | 777 | |
778 | if (buf1) | |
779 | xfree(buf1); | |
780 | ||
781 | if (prog) | |
782 | xfree(prog); | |
783 | ||
784 | if (thread) | |
785 | CloseHandle(thread); | |
786 | ||
787 | if (hProcess) | |
788 | CloseHandle(hProcess); | |
789 | ||
790 | if (p2c[0] != -1) | |
791 | close(p2c[0]); | |
792 | ||
793 | return retval; | |
794 | } | |
795 | ||
796 | static unsigned int __stdcall | |
797 | ipc_thread_2(void *in_params) | |
798 | { | |
799 | int x; | |
800 | ||
801 | struct thread_params *params = (struct thread_params *) in_params; | |
802 | int type = params->type; | |
803 | int rfd = params->rfd; | |
804 | int send_fd = params->send_fd; | |
805 | char *prog = xstrdup(params->prog); | |
806 | pid_t pid = params->pid; | |
807 | char *buf2 = (char *)xcalloc(1, 8192); | |
808 | ||
809 | for (;;) { | |
810 | if (type == IPC_TCP_SOCKET) | |
811 | x = read(rfd, buf2, 8192); | |
812 | else | |
813 | x = recv(rfd, (void *)buf2, 8192, 0); | |
814 | ||
815 | if ((x <= 0 && type == IPC_TCP_SOCKET) || | |
816 | (x < 0 && type == IPC_UDP_SOCKET)) { | |
bf8fe701 | 817 | debugs(54, 3, "ipc(" << prog << "," << pid << "): " << x << " bytes read from " << prog << ". Exiting..."); |
818 | ||
b5d712b5 | 819 | break; |
820 | } | |
821 | ||
822 | buf2[x] = '\0'; | |
823 | ||
824 | if (type == IPC_UDP_SOCKET && !strcmp(buf2, shutdown_string)) { | |
bf8fe701 | 825 | debugs(54, 3, "ipc(" << prog << "," << pid << "): request for shutdown received. Exiting..."); |
826 | ||
b5d712b5 | 827 | break; |
828 | } | |
829 | ||
830 | if (x >= 2) { | |
831 | if ((buf2[x - 1] == '\n') && (buf2[x - 2] == '\r')) { | |
832 | buf2[x - 2] = '\n'; | |
833 | buf2[x - 1] = '\0'; | |
834 | x--; | |
835 | } | |
836 | } | |
837 | ||
bf8fe701 | 838 | debugs(54, 5, "ipc(" << prog << "," << pid << "): received from child : " << rfc1738_escape_unescaped(buf2)); |
839 | ||
b5d712b5 | 840 | x = send(send_fd, (const void *)buf2, x, 0); |
841 | ||
842 | if ((x <= 0 && type == IPC_TCP_SOCKET) || | |
843 | (x < 0 && type == IPC_UDP_SOCKET)) { | |
bf8fe701 | 844 | debugs(54, 3, "ipc(" << prog << "," << pid << "): " << x << " bytes sent to parent. Exiting..."); |
845 | ||
b5d712b5 | 846 | break; |
847 | } | |
848 | } | |
849 | ||
850 | xfree(prog); | |
851 | xfree(buf2); | |
852 | return 0; | |
853 | } |