2 * DEBUG: section 54 Windows Interprocess Communication
3 * AUTHOR: Andrey Shorin <tolsty@tushino.com>
4 * AUTHOR: Guido Serassio <serassio@squid-cache.org>
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
39 #include "ip/Address.h"
41 #include "SquidConfig.h"
43 #include "SquidTime.h"
58 Ip::Address local_addr
;
64 struct thread_params
{
72 static unsigned int __stdcall
ipc_thread_1(void *params
);
73 static unsigned int __stdcall
ipc_thread_2(void *params
);
75 static const char *ok_string
= "OK\n";
76 static const char *err_string
= "ERR\n";
77 static const char *shutdown_string
= "$shutdown\n";
79 static const char *hello_string
= "hi there\n";
80 #define HELLO_BUF_SZ 32
81 static char hello_buf
[HELLO_BUF_SZ
];
84 ipcCloseAllFD(int prfd
, int pwfd
, int crfd
, int cwfd
)
109 env_str
= (char *)xcalloc((tmp_s
= strlen(Debug::debugOptions
) + 32), 1);
110 snprintf(env_str
, tmp_s
, "SQUID_DEBUG=%s", Debug::debugOptions
);
116 ipcCreate(int type
, const char *prog
, const char *const args
[], const char *name
, Ip::Address
&local_addr
, int *rfd
, int *wfd
, void **hIpc
)
118 unsigned long thread
;
120 struct ipc_params params
;
122 int optlen
= sizeof(opt
);
126 Ip::Address tmp_addr
;
127 struct addrinfo
*aiCS
= NULL
;
128 struct addrinfo
*aiPS
= NULL
;
136 requirePathnameExists(name
, prog
);
147 if (WIN32_OS_version
!= _WIN_OS_WINNT
) {
148 getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, &optlen
);
149 opt
= opt
& ~(SO_SYNCHRONOUS_NONALERT
| SO_SYNCHRONOUS_ALERT
);
150 setsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, sizeof(opt
));
153 if (type
== IPC_TCP_SOCKET
) {
154 crfd
= cwfd
= comm_open(SOCK_STREAM
,
159 prfd
= pwfd
= comm_open(SOCK_STREAM
,
160 IPPROTO_TCP
, /* protocol */
164 } else if (type
== IPC_UDP_SOCKET
) {
165 crfd
= cwfd
= comm_open(SOCK_DGRAM
,
170 prfd
= pwfd
= comm_open(SOCK_DGRAM
,
175 } else if (type
== IPC_FIFO
) {
176 debugs(54, DBG_CRITICAL
, "ipcCreate: " << prog
<< ": use IPC_TCP_SOCKET instead of IP_FIFO on Windows");
182 debugs(54, 3, "ipcCreate: prfd FD " << prfd
);
183 debugs(54, 3, "ipcCreate: pwfd FD " << pwfd
);
184 debugs(54, 3, "ipcCreate: crfd FD " << crfd
);
185 debugs(54, 3, "ipcCreate: cwfd FD " << cwfd
);
187 if (WIN32_OS_version
!= _WIN_OS_WINNT
) {
188 getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, &optlen
);
189 opt
= opt
| SO_SYNCHRONOUS_NONALERT
;
190 setsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, optlen
);
194 debugs(54, DBG_CRITICAL
, "ipcCreate: Failed to create child FD.");
195 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
199 debugs(54, DBG_CRITICAL
, "ipcCreate: Failed to create server FD.");
200 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
203 // AYJ: these flags should be neutral, but if not IPv6 version needs adding
204 if (type
== IPC_TCP_SOCKET
|| type
== IPC_UDP_SOCKET
) {
206 tmp_addr
.InitAddrInfo(aiPS
);
208 if (getsockname(pwfd
, aiPS
->ai_addr
, &(aiPS
->ai_addrlen
) ) < 0) {
209 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
210 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
215 debugs(54, 3, "ipcCreate: FD " << pwfd
<< " sockaddr " << tmp_addr
);
217 tmp_addr
.InitAddrInfo(aiCS
);
219 if (getsockname(crfd
, aiCS
->ai_addr
, &(aiCS
->ai_addrlen
) ) < 0) {
220 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
221 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
227 debugs(54, 3, "ipcCreate: FD " << crfd
<< " sockaddr " << tmp_addr
);
230 if (type
== IPC_TCP_SOCKET
) {
231 if (listen(crfd
, 1) < 0) {
232 debugs(54, DBG_IMPORTANT
, "ipcCreate: listen FD " << crfd
<< ": " << xstrerror());
233 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
236 debugs(54, 3, "ipcCreate: FD " << crfd
<< " listening...");
239 /* flush or else we get dup data if unbuffered_logs is set */
250 params
.local_addr
= local_addr
;
254 params
.args
= (char **) args
;
256 thread
= _beginthreadex(NULL
, 0, ipc_thread_1
, ¶ms
, 0, NULL
);
259 debugs(54, DBG_IMPORTANT
, "ipcCreate: _beginthread: " << xstrerror());
260 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
263 /* NP: tmp_addr was left with eiether empty or aiCS in Ip::Address format */
264 if (comm_connect_addr(pwfd
, tmp_addr
) == COMM_ERROR
) {
265 CloseHandle((HANDLE
) thread
);
266 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
269 memset(hello_buf
, '\0', HELLO_BUF_SZ
);
270 x
= recv(prfd
, (void *)hello_buf
, HELLO_BUF_SZ
- 1, 0);
273 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: hello read test failed");
274 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
275 CloseHandle((HANDLE
) thread
);
276 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
277 } else if (strcmp(hello_buf
, hello_string
)) {
278 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: hello read test failed");
279 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
280 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
281 CloseHandle((HANDLE
) thread
);
282 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
285 x
= send(pwfd
, (const void *)ok_string
, strlen(ok_string
), 0);
288 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: OK write test failed");
289 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
290 CloseHandle((HANDLE
) thread
);
291 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
294 memset(hello_buf
, '\0', HELLO_BUF_SZ
);
295 x
= recv(prfd
, (void *)hello_buf
, HELLO_BUF_SZ
- 1, 0);
298 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: OK read test failed");
299 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
300 CloseHandle((HANDLE
) thread
);
301 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
302 } else if (!strcmp(hello_buf
, err_string
)) {
303 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: OK read test failed");
304 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
305 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
306 CloseHandle((HANDLE
) thread
);
307 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
311 pid
= atol(hello_buf
);
312 commUnsetFdTimeout(prfd
);
313 commSetNonBlocking(prfd
);
314 commSetNonBlocking(pwfd
);
315 commSetCloseOnExec(prfd
);
316 commSetCloseOnExec(pwfd
);
324 fd_table
[prfd
].flags
.ipc
= 1;
326 fd_table
[pwfd
].flags
.ipc
= 1;
328 fd_table
[crfd
].flags
.ipc
= 1;
330 fd_table
[cwfd
].flags
.ipc
= 1;
332 if (Config
.sleep_after_fork
) {
333 /* XXX emulation of usleep() */
335 sl
= Config
.sleep_after_fork
/ 1000;
343 if (GetExitCodeThread((HANDLE
) thread
, &ecode
) && ecode
== STILL_ACTIVE
) {
345 *hIpc
= (HANDLE
) thread
;
349 CloseHandle((HANDLE
) thread
);
350 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
355 ipcSend(int cwfd
, const char *buf
, int len
)
359 x
= send(cwfd
, (const void *)buf
, len
, 0);
362 debugs(54, DBG_CRITICAL
, "sendto FD " << cwfd
<< ": " << xstrerror());
363 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: hello write test failed");
369 static unsigned int __stdcall
370 ipc_thread_1(void *in_params
)
372 int t1
, t2
, t3
, retval
= -1;
373 int p2c
[2] = {-1, -1};
374 int c2p
[2] = {-1, -1};
375 HANDLE hProcess
= NULL
, thread
= NULL
;
378 struct thread_params thread_params
;
383 PROCESS_INFORMATION pi
;
385 int prfd_ipc
= -1, pwfd_ipc
= -1, crfd_ipc
= -1, cwfd_ipc
= -1;
386 char *prog
= NULL
, *buf1
= NULL
;
390 struct addrinfo
*aiPS_ipc
= NULL
;
391 struct addrinfo
*aiCS_ipc
= NULL
;
393 struct ipc_params
*params
= (struct ipc_params
*) in_params
;
394 int type
= params
->type
;
395 int crfd
= params
->crfd
;
396 int cwfd
= params
->cwfd
;
397 char **args
= params
->args
;
399 Ip::Address PS
= params
->PS
;
400 Ip::Address local_addr
= params
->local_addr
;
402 buf1
= (char *)xcalloc(1, 8192);
403 strcpy(buf1
, params
->prog
);
404 prog
= strtok(buf1
, w_space
);
406 if ((str
= strrchr(prog
, '/')))
409 if ((str
= strrchr(prog
, '\\')))
412 prog
= xstrdup(prog
);
414 if (type
== IPC_TCP_SOCKET
) {
415 debugs(54, 3, "ipcCreate: calling accept on FD " << crfd
);
417 if ((fd
= accept(crfd
, NULL
, NULL
)) < 0) {
418 debugs(54, DBG_CRITICAL
, "ipcCreate: FD " << crfd
<< " accept: " << xstrerror());
422 debugs(54, 3, "ipcCreate: CHILD accepted new FD " << fd
);
424 snprintf(buf1
, 8191, "%s CHILD socket", prog
);
425 fd_open(fd
, FD_SOCKET
, buf1
);
426 fd_table
[fd
].flags
.ipc
= 1;
428 } else if (type
== IPC_UDP_SOCKET
) {
429 if (comm_connect_addr(crfd
, params
->PS
) == COMM_ERROR
)
433 x
= send(cwfd
, (const void *)hello_string
, strlen(hello_string
) + 1, 0);
436 debugs(54, DBG_CRITICAL
, "sendto FD " << cwfd
<< ": " << xstrerror());
437 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: hello write test failed");
442 memset(buf1
, '\0', sizeof(buf1
));
443 x
= recv(crfd
, (void *)buf1
, 8191, 0);
446 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: OK read test failed");
447 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
449 } else if (strcmp(buf1
, ok_string
)) {
450 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: OK read test failed");
451 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
452 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
456 /* assign file descriptors to child process */
457 if (_pipe(p2c
, 1024, _O_BINARY
| _O_NOINHERIT
) < 0) {
458 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: pipe: " << xstrerror());
459 ipcSend(cwfd
, err_string
, strlen(err_string
));
463 if (_pipe(c2p
, 1024, _O_BINARY
| _O_NOINHERIT
) < 0) {
464 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: pipe: " << xstrerror());
465 ipcSend(cwfd
, err_string
, strlen(err_string
));
469 if (type
== IPC_UDP_SOCKET
) {
470 snprintf(buf1
, 8192, "%s(%ld) <-> ipc CHILD socket", prog
, -1L);
471 crfd_ipc
= cwfd_ipc
= comm_open(SOCK_DGRAM
, IPPROTO_UDP
, local_addr
, 0, buf1
);
474 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: Failed to create child FD for " << prog
<< ".");
475 ipcSend(cwfd
, err_string
, strlen(err_string
));
479 snprintf(buf1
, 8192, "%s(%ld) <-> ipc PARENT socket", prog
, -1L);
480 prfd_ipc
= pwfd_ipc
= comm_open(SOCK_DGRAM
, IPPROTO_UDP
, local_addr
, 0, buf1
);
483 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: Failed to create server FD for " << prog
<< ".");
484 ipcSend(cwfd
, err_string
, strlen(err_string
));
488 PS_ipc
.InitAddrInfo(aiPS_ipc
);
490 if (getsockname(pwfd_ipc
, aiPS_ipc
->ai_addr
, &(aiPS_ipc
->ai_addrlen
)) < 0) {
491 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
492 ipcSend(cwfd
, err_string
, strlen(err_string
));
498 debugs(54, 3, "ipcCreate: FD " << pwfd_ipc
<< " sockaddr " << PS_ipc
);
500 CS_ipc
.InitAddrInfo(aiCS_ipc
);
502 if (getsockname(crfd_ipc
, aiCS_ipc
->ai_addr
, &(aiCS_ipc
->ai_addrlen
)) < 0) {
503 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
504 ipcSend(cwfd
, err_string
, strlen(err_string
));
510 debugs(54, 3, "ipcCreate: FD " << crfd_ipc
<< " sockaddr " << CS_ipc
);
512 if (comm_connect_addr(pwfd_ipc
, CS_ipc
) == COMM_ERROR
) {
513 ipcSend(cwfd
, err_string
, strlen(err_string
));
519 if (comm_connect_addr(crfd_ipc
, PS_ipc
) == COMM_ERROR
) {
520 ipcSend(cwfd
, err_string
, strlen(err_string
));
523 } /* IPC_UDP_SOCKET */
535 dup2(fileno(debug_log
), 2);
541 commUnsetNonBlocking(fd
);
543 memset(&si
, 0, sizeof(STARTUPINFO
));
545 si
.cb
= sizeof(STARTUPINFO
);
547 si
.hStdInput
= (HANDLE
) _get_osfhandle(0);
549 si
.hStdOutput
= (HANDLE
) _get_osfhandle(1);
551 si
.hStdError
= (HANDLE
) _get_osfhandle(2);
553 si
.dwFlags
= STARTF_USESTDHANDLES
;
555 /* Make sure all other valid handles are not inerithable */
556 for (x
= 3; x
< Squid_MaxFD
; ++x
) {
557 if ((F
= _get_osfhandle(x
)) == -1)
560 SetHandleInformation((HANDLE
) F
, HANDLE_FLAG_INHERIT
, 0);
564 strcpy(buf1
+ 4096, params
->prog
);
565 str
= strtok(buf1
+ 4096, w_space
);
570 } while ((str
= strtok(NULL
, w_space
)));
575 strcat(buf1
, args
[x
]);
580 if (CreateProcess(buf1
+ 4096, buf1
, NULL
, NULL
, TRUE
, CREATE_NO_WINDOW
,
581 NULL
, NULL
, &si
, &pi
)) {
582 pid
= pi
.dwProcessId
;
583 hProcess
= pi
.hProcess
;
598 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << params
->prog
<< ": " << xstrerror());
600 ipcSend(cwfd
, err_string
, strlen(err_string
));
604 if (type
== IPC_UDP_SOCKET
) {
605 WSAPROTOCOL_INFO wpi
;
607 memset(&wpi
, 0, sizeof(wpi
));
609 if (SOCKET_ERROR
== WSADuplicateSocket(crfd_ipc
, pid
, &wpi
)) {
610 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: WSADuplicateSocket: " << xstrerror());
612 ipcSend(cwfd
, err_string
, strlen(err_string
));
616 x
= write(c2p
[1], (const char *) &wpi
, sizeof(wpi
));
618 if (x
< (ssize_t
)sizeof(wpi
)) {
619 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: write FD " << c2p
[1] << ": " << xstrerror());
620 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
622 ipcSend(cwfd
, err_string
, strlen(err_string
));
626 x
= read(p2c
[0], buf1
, 8192);
629 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: read FD " << p2c
[0] << ": " << xstrerror());
630 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
632 ipcSend(cwfd
, err_string
, strlen(err_string
));
634 } else if (strncmp(buf1
, ok_string
, strlen(ok_string
))) {
635 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
636 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
638 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(buf1
) << "'");
639 ipcSend(cwfd
, err_string
, strlen(err_string
));
643 x
= write(c2p
[1], (const char *) &PS_ipc
, sizeof(PS_ipc
));
645 if (x
< (ssize_t
)sizeof(PS_ipc
)) {
646 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: write FD " << c2p
[1] << ": " << xstrerror());
647 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
649 ipcSend(cwfd
, err_string
, strlen(err_string
));
653 x
= read(p2c
[0], buf1
, 8192);
656 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: read FD " << p2c
[0] << ": " << xstrerror());
657 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
659 ipcSend(cwfd
, err_string
, strlen(err_string
));
661 } else if (strncmp(buf1
, ok_string
, strlen(ok_string
))) {
662 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
663 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
665 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(buf1
) << "'");
666 ipcSend(cwfd
, err_string
, strlen(err_string
));
670 x
= send(pwfd_ipc
, (const void *)ok_string
, strlen(ok_string
), 0);
671 x
= recv(prfd_ipc
, (void *)(buf1
+ 200), 8191 - 200, 0);
672 assert((size_t) x
== strlen(ok_string
)
673 && !strncmp(ok_string
, buf1
+ 200, strlen(ok_string
)));
674 } /* IPC_UDP_SOCKET */
676 snprintf(buf1
, 8191, "%s(%ld) CHILD socket", prog
, (long int) pid
);
680 if (prfd_ipc
!= -1) {
681 snprintf(buf1
, 8191, "%s(%ld) <-> ipc CHILD socket", prog
, (long int) pid
);
682 fd_note(crfd_ipc
, buf1
);
683 snprintf(buf1
, 8191, "%s(%ld) <-> ipc PARENT socket", prog
, (long int) pid
);
684 fd_note(prfd_ipc
, buf1
);
687 /* else { IPC_TCP_SOCKET */
688 /* commSetNoLinger(fd); */
690 thread_params
.prog
= prog
;
692 thread_params
.send_fd
= cwfd
;
694 thread_params
.pid
= pid
;
696 if ((thread_params
.type
= type
) == IPC_TCP_SOCKET
)
697 thread_params
.rfd
= p2c
[0];
699 thread_params
.rfd
= prfd_ipc
;
701 thread
= (HANDLE
)_beginthreadex(NULL
, 0, ipc_thread_2
, &thread_params
, 0, NULL
);
704 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: _beginthreadex: " << xstrerror());
705 ipcSend(cwfd
, err_string
, strlen(err_string
));
709 snprintf(buf1
, 8191, "%ld\n", (long int) pid
);
711 if (-1 == ipcSend(cwfd
, buf1
, strlen(buf1
)))
714 debugs(54, 2, "ipc(" << prog
<< "," << pid
<< "): started successfully");
718 x
= recv(crfd
, (void *)buf1
, 8192, 0);
721 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes received from parent. Exiting...");
727 if (type
== IPC_UDP_SOCKET
&& !strcmp(buf1
, shutdown_string
)) {
728 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): request for shutdown received from parent. Exiting...");
730 TerminateProcess(hProcess
, 0);
734 debugs(54, 5, "ipc(" << prog
<< "," << pid
<< "): received from parent: " << rfc1738_escape_unescaped(buf1
));
736 if (type
== IPC_TCP_SOCKET
)
737 x
= write(c2p
[1], buf1
, x
);
739 x
= send(pwfd_ipc
, (const void *)buf1
, x
, 0);
742 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes written to " << prog
<< ". Exiting...");
755 if (fd_table
[crfd
].flags
.open
)
756 ipcCloseAllFD(-1, -1, crfd
, cwfd
);
758 if (prfd_ipc
!= -1) {
759 send(crfd_ipc
, (const void *)shutdown_string
, strlen(shutdown_string
), 0);
760 shutdown(crfd_ipc
, SD_BOTH
);
761 shutdown(prfd_ipc
, SD_BOTH
);
764 ipcCloseAllFD(prfd_ipc
, pwfd_ipc
, crfd_ipc
, cwfd_ipc
);
766 if (hProcess
&& WAIT_OBJECT_0
!=
767 WaitForSingleObject(hProcess
, type
== IPC_UDP_SOCKET
? 12000 : 5000)) {
770 debugs(54, DBG_CRITICAL
, "ipc(" << prog
<< "," << pid
<< "): WARNING: " << prog
<<
771 " didn't exit in " << (type
== IPC_UDP_SOCKET
? 12 : 5) << " seconds.");
775 if (thread
&& WAIT_OBJECT_0
!= WaitForSingleObject(thread
, 3000)) {
777 debugs(54, DBG_CRITICAL
, "ipc(" << prog
<< "," << pid
<< "): WARNING: ipc_thread_2 didn't exit in 3 seconds.");
784 debugs(54, 2, "ipc(" << prog
<< "," << pid
<< "): normal exit");
796 CloseHandle(hProcess
);
804 static unsigned int __stdcall
805 ipc_thread_2(void *in_params
)
809 struct thread_params
*params
= (struct thread_params
*) in_params
;
810 int type
= params
->type
;
811 int rfd
= params
->rfd
;
812 int send_fd
= params
->send_fd
;
813 char *prog
= xstrdup(params
->prog
);
814 pid_t pid
= params
->pid
;
815 char *buf2
= (char *)xcalloc(1, 8192);
818 if (type
== IPC_TCP_SOCKET
)
819 x
= read(rfd
, buf2
, 8192);
821 x
= recv(rfd
, (void *)buf2
, 8192, 0);
823 if ((x
<= 0 && type
== IPC_TCP_SOCKET
) ||
824 (x
< 0 && type
== IPC_UDP_SOCKET
)) {
825 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes read from " << prog
<< ". Exiting...");
832 if (type
== IPC_UDP_SOCKET
&& !strcmp(buf2
, shutdown_string
)) {
833 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): request for shutdown received. Exiting...");
839 if ((buf2
[x
- 1] == '\n') && (buf2
[x
- 2] == '\r')) {
846 debugs(54, 5, "ipc(" << prog
<< "," << pid
<< "): received from child : " << rfc1738_escape_unescaped(buf2
));
848 x
= send(send_fd
, (const void *)buf2
, x
, 0);
850 if ((x
<= 0 && type
== IPC_TCP_SOCKET
) ||
851 (x
< 0 && type
== IPC_UDP_SOCKET
)) {
852 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes sent to parent. Exiting...");