4 * DEBUG: section 54 Windows Interprocess Communication
5 * AUTHOR: Andrey Shorin <tolsty@tushino.com>
6 * AUTHOR: Guido Serassio <serassio@squid-cache.org>
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
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.
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.
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.
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.
41 #include "ip/Address.h"
43 #include "SquidTime.h"
57 Ip::Address local_addr
;
63 struct thread_params
{
71 static unsigned int __stdcall
ipc_thread_1(void *params
);
72 static unsigned int __stdcall
ipc_thread_2(void *params
);
74 static const char *ok_string
= "OK\n";
75 static const char *err_string
= "ERR\n";
76 static const char *shutdown_string
= "$shutdown\n";
78 static const char *hello_string
= "hi there\n";
79 #define HELLO_BUF_SZ 32
80 static char hello_buf
[HELLO_BUF_SZ
];
83 ipcCloseAllFD(int prfd
, int pwfd
, int crfd
, int cwfd
)
108 env_str
= (char *)xcalloc((tmp_s
= strlen(Debug::debugOptions
) + 32), 1);
109 snprintf(env_str
, tmp_s
, "SQUID_DEBUG=%s", Debug::debugOptions
);
115 ipcCreate(int type
, const char *prog
, const char *const args
[], const char *name
, Ip::Address
&local_addr
, int *rfd
, int *wfd
, void **hIpc
)
117 unsigned long thread
;
119 struct ipc_params params
;
121 int optlen
= sizeof(opt
);
125 Ip::Address tmp_addr
;
126 struct addrinfo
*aiCS
= NULL
;
127 struct addrinfo
*aiPS
= NULL
;
135 requirePathnameExists(name
, prog
);
146 if (WIN32_OS_version
!= _WIN_OS_WINNT
) {
147 getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, &optlen
);
148 opt
= opt
& ~(SO_SYNCHRONOUS_NONALERT
| SO_SYNCHRONOUS_ALERT
);
149 setsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, sizeof(opt
));
152 if (type
== IPC_TCP_SOCKET
) {
153 crfd
= cwfd
= comm_open(SOCK_STREAM
,
158 prfd
= pwfd
= comm_open(SOCK_STREAM
,
159 IPPROTO_TCP
, /* protocol */
163 } else if (type
== IPC_UDP_SOCKET
) {
164 crfd
= cwfd
= comm_open(SOCK_DGRAM
,
169 prfd
= pwfd
= comm_open(SOCK_DGRAM
,
174 } else if (type
== IPC_FIFO
) {
175 debugs(54, DBG_CRITICAL
, "ipcCreate: " << prog
<< ": use IPC_TCP_SOCKET instead of IP_FIFO on Windows");
181 debugs(54, 3, "ipcCreate: prfd FD " << prfd
);
182 debugs(54, 3, "ipcCreate: pwfd FD " << pwfd
);
183 debugs(54, 3, "ipcCreate: crfd FD " << crfd
);
184 debugs(54, 3, "ipcCreate: cwfd FD " << cwfd
);
186 if (WIN32_OS_version
!= _WIN_OS_WINNT
) {
187 getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, &optlen
);
188 opt
= opt
| SO_SYNCHRONOUS_NONALERT
;
189 setsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, optlen
);
193 debugs(54, DBG_CRITICAL
, "ipcCreate: Failed to create child FD.");
194 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
198 debugs(54, DBG_CRITICAL
, "ipcCreate: Failed to create server FD.");
199 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
202 // AYJ: these flags should be neutral, but if not IPv6 version needs adding
203 if (type
== IPC_TCP_SOCKET
|| type
== IPC_UDP_SOCKET
) {
205 tmp_addr
.InitAddrInfo(aiPS
);
207 if (getsockname(pwfd
, aiPS
->ai_addr
, &(aiPS
->ai_addrlen
) ) < 0) {
208 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
209 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
214 debugs(54, 3, "ipcCreate: FD " << pwfd
<< " sockaddr " << tmp_addr
);
216 tmp_addr
.InitAddrInfo(aiCS
);
218 if (getsockname(crfd
, aiCS
->ai_addr
, &(aiCS
->ai_addrlen
) ) < 0) {
219 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
220 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
226 debugs(54, 3, "ipcCreate: FD " << crfd
<< " sockaddr " << tmp_addr
);
229 if (type
== IPC_TCP_SOCKET
) {
230 if (listen(crfd
, 1) < 0) {
231 debugs(54, DBG_IMPORTANT
, "ipcCreate: listen FD " << crfd
<< ": " << xstrerror());
232 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
235 debugs(54, 3, "ipcCreate: FD " << crfd
<< " listening...");
238 /* flush or else we get dup data if unbuffered_logs is set */
249 params
.local_addr
= local_addr
;
253 params
.args
= (char **) args
;
255 thread
= _beginthreadex(NULL
, 0, ipc_thread_1
, ¶ms
, 0, NULL
);
258 debugs(54, DBG_IMPORTANT
, "ipcCreate: _beginthread: " << xstrerror());
259 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
262 /* NP: tmp_addr was left with eiether empty or aiCS in Ip::Address format */
263 if (comm_connect_addr(pwfd
, tmp_addr
) == COMM_ERROR
) {
264 CloseHandle((HANDLE
) thread
);
265 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
268 memset(hello_buf
, '\0', HELLO_BUF_SZ
);
269 x
= recv(prfd
, (void *)hello_buf
, HELLO_BUF_SZ
- 1, 0);
272 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: hello read test failed");
273 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
274 CloseHandle((HANDLE
) thread
);
275 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
276 } else if (strcmp(hello_buf
, hello_string
)) {
277 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: hello read test failed");
278 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
279 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
280 CloseHandle((HANDLE
) thread
);
281 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
284 x
= send(pwfd
, (const void *)ok_string
, strlen(ok_string
), 0);
287 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: OK write test failed");
288 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
289 CloseHandle((HANDLE
) thread
);
290 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
293 memset(hello_buf
, '\0', HELLO_BUF_SZ
);
294 x
= recv(prfd
, (void *)hello_buf
, HELLO_BUF_SZ
- 1, 0);
297 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: OK read test failed");
298 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
299 CloseHandle((HANDLE
) thread
);
300 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
301 } else if (!strcmp(hello_buf
, err_string
)) {
302 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: OK read test failed");
303 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
304 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
305 CloseHandle((HANDLE
) thread
);
306 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
310 pid
= atol(hello_buf
);
311 commUnsetFdTimeout(prfd
);
312 commSetNonBlocking(prfd
);
313 commSetNonBlocking(pwfd
);
314 commSetCloseOnExec(prfd
);
315 commSetCloseOnExec(pwfd
);
323 fd_table
[prfd
].flags
.ipc
= 1;
325 fd_table
[pwfd
].flags
.ipc
= 1;
327 fd_table
[crfd
].flags
.ipc
= 1;
329 fd_table
[cwfd
].flags
.ipc
= 1;
331 if (Config
.sleep_after_fork
) {
332 /* XXX emulation of usleep() */
334 sl
= Config
.sleep_after_fork
/ 1000;
342 if (GetExitCodeThread((HANDLE
) thread
, &ecode
) && ecode
== STILL_ACTIVE
) {
344 *hIpc
= (HANDLE
) thread
;
348 CloseHandle((HANDLE
) thread
);
349 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
354 ipcSend(int cwfd
, const char *buf
, int len
)
358 x
= send(cwfd
, (const void *)buf
, len
, 0);
361 debugs(54, DBG_CRITICAL
, "sendto FD " << cwfd
<< ": " << xstrerror());
362 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: hello write test failed");
368 static unsigned int __stdcall
369 ipc_thread_1(void *in_params
)
371 int t1
, t2
, t3
, retval
= -1;
372 int p2c
[2] = {-1, -1};
373 int c2p
[2] = {-1, -1};
374 HANDLE hProcess
= NULL
, thread
= NULL
;
377 struct thread_params thread_params
;
382 PROCESS_INFORMATION pi
;
384 int prfd_ipc
= -1, pwfd_ipc
= -1, crfd_ipc
= -1, cwfd_ipc
= -1;
385 char *prog
= NULL
, *buf1
= NULL
;
389 struct addrinfo
*aiPS_ipc
= NULL
;
390 struct addrinfo
*aiCS_ipc
= NULL
;
392 struct ipc_params
*params
= (struct ipc_params
*) in_params
;
393 int type
= params
->type
;
394 int crfd
= params
->crfd
;
395 int cwfd
= params
->cwfd
;
396 char **args
= params
->args
;
398 Ip::Address PS
= params
->PS
;
399 Ip::Address local_addr
= params
->local_addr
;
401 buf1
= (char *)xcalloc(1, 8192);
402 strcpy(buf1
, params
->prog
);
403 prog
= strtok(buf1
, w_space
);
405 if ((str
= strrchr(prog
, '/')))
408 if ((str
= strrchr(prog
, '\\')))
411 prog
= xstrdup(prog
);
413 if (type
== IPC_TCP_SOCKET
) {
414 debugs(54, 3, "ipcCreate: calling accept on FD " << crfd
);
416 if ((fd
= accept(crfd
, NULL
, NULL
)) < 0) {
417 debugs(54, DBG_CRITICAL
, "ipcCreate: FD " << crfd
<< " accept: " << xstrerror());
421 debugs(54, 3, "ipcCreate: CHILD accepted new FD " << fd
);
423 snprintf(buf1
, 8191, "%s CHILD socket", prog
);
424 fd_open(fd
, FD_SOCKET
, buf1
);
425 fd_table
[fd
].flags
.ipc
= 1;
427 } else if (type
== IPC_UDP_SOCKET
) {
428 if (comm_connect_addr(crfd
, params
->PS
) == COMM_ERROR
)
432 x
= send(cwfd
, (const void *)hello_string
, strlen(hello_string
) + 1, 0);
435 debugs(54, DBG_CRITICAL
, "sendto FD " << cwfd
<< ": " << xstrerror());
436 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: hello write test failed");
441 memset(buf1
, '\0', sizeof(buf1
));
442 x
= recv(crfd
, (void *)buf1
, 8191, 0);
445 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: OK read test failed");
446 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
448 } else if (strcmp(buf1
, ok_string
)) {
449 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: OK read test failed");
450 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
451 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
455 /* assign file descriptors to child process */
456 if (_pipe(p2c
, 1024, _O_BINARY
| _O_NOINHERIT
) < 0) {
457 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: pipe: " << xstrerror());
458 ipcSend(cwfd
, err_string
, strlen(err_string
));
462 if (_pipe(c2p
, 1024, _O_BINARY
| _O_NOINHERIT
) < 0) {
463 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: pipe: " << xstrerror());
464 ipcSend(cwfd
, err_string
, strlen(err_string
));
468 if (type
== IPC_UDP_SOCKET
) {
469 snprintf(buf1
, 8192, "%s(%ld) <-> ipc CHILD socket", prog
, -1L);
470 crfd_ipc
= cwfd_ipc
= comm_open(SOCK_DGRAM
, IPPROTO_UDP
, local_addr
, 0, buf1
);
473 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: Failed to create child FD for " << prog
<< ".");
474 ipcSend(cwfd
, err_string
, strlen(err_string
));
478 snprintf(buf1
, 8192, "%s(%ld) <-> ipc PARENT socket", prog
, -1L);
479 prfd_ipc
= pwfd_ipc
= comm_open(SOCK_DGRAM
, IPPROTO_UDP
, local_addr
, 0, buf1
);
482 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: Failed to create server FD for " << prog
<< ".");
483 ipcSend(cwfd
, err_string
, strlen(err_string
));
487 PS_ipc
.InitAddrInfo(aiPS_ipc
);
489 if (getsockname(pwfd_ipc
, aiPS_ipc
->ai_addr
, &(aiPS_ipc
->ai_addrlen
)) < 0) {
490 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
491 ipcSend(cwfd
, err_string
, strlen(err_string
));
497 debugs(54, 3, "ipcCreate: FD " << pwfd_ipc
<< " sockaddr " << PS_ipc
);
499 CS_ipc
.InitAddrInfo(aiCS_ipc
);
501 if (getsockname(crfd_ipc
, aiCS_ipc
->ai_addr
, &(aiCS_ipc
->ai_addrlen
)) < 0) {
502 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
503 ipcSend(cwfd
, err_string
, strlen(err_string
));
509 debugs(54, 3, "ipcCreate: FD " << crfd_ipc
<< " sockaddr " << CS_ipc
);
511 if (comm_connect_addr(pwfd_ipc
, CS_ipc
) == COMM_ERROR
) {
512 ipcSend(cwfd
, err_string
, strlen(err_string
));
518 if (comm_connect_addr(crfd_ipc
, PS_ipc
) == COMM_ERROR
) {
519 ipcSend(cwfd
, err_string
, strlen(err_string
));
522 } /* IPC_UDP_SOCKET */
534 dup2(fileno(debug_log
), 2);
540 commUnsetNonBlocking(fd
);
542 memset(&si
, 0, sizeof(STARTUPINFO
));
544 si
.cb
= sizeof(STARTUPINFO
);
546 si
.hStdInput
= (HANDLE
) _get_osfhandle(0);
548 si
.hStdOutput
= (HANDLE
) _get_osfhandle(1);
550 si
.hStdError
= (HANDLE
) _get_osfhandle(2);
552 si
.dwFlags
= STARTF_USESTDHANDLES
;
554 /* Make sure all other valid handles are not inerithable */
555 for (x
= 3; x
< Squid_MaxFD
; ++x
) {
556 if ((F
= _get_osfhandle(x
)) == -1)
559 SetHandleInformation((HANDLE
) F
, HANDLE_FLAG_INHERIT
, 0);
563 strcpy(buf1
+ 4096, params
->prog
);
564 str
= strtok(buf1
+ 4096, w_space
);
569 } while ((str
= strtok(NULL
, w_space
)));
574 strcat(buf1
, args
[x
]);
579 if (CreateProcess(buf1
+ 4096, buf1
, NULL
, NULL
, TRUE
, CREATE_NO_WINDOW
,
580 NULL
, NULL
, &si
, &pi
)) {
581 pid
= pi
.dwProcessId
;
582 hProcess
= pi
.hProcess
;
597 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << params
->prog
<< ": " << xstrerror());
599 ipcSend(cwfd
, err_string
, strlen(err_string
));
603 if (type
== IPC_UDP_SOCKET
) {
604 WSAPROTOCOL_INFO wpi
;
606 memset(&wpi
, 0, sizeof(wpi
));
608 if (SOCKET_ERROR
== WSADuplicateSocket(crfd_ipc
, pid
, &wpi
)) {
609 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: WSADuplicateSocket: " << xstrerror());
611 ipcSend(cwfd
, err_string
, strlen(err_string
));
615 x
= write(c2p
[1], (const char *) &wpi
, sizeof(wpi
));
617 if (x
< (ssize_t
)sizeof(wpi
)) {
618 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: write FD " << c2p
[1] << ": " << xstrerror());
619 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
621 ipcSend(cwfd
, err_string
, strlen(err_string
));
625 x
= read(p2c
[0], buf1
, 8192);
628 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: read FD " << p2c
[0] << ": " << xstrerror());
629 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
631 ipcSend(cwfd
, err_string
, strlen(err_string
));
633 } else if (strncmp(buf1
, ok_string
, strlen(ok_string
))) {
634 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
635 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
637 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(buf1
) << "'");
638 ipcSend(cwfd
, err_string
, strlen(err_string
));
642 x
= write(c2p
[1], (const char *) &PS_ipc
, sizeof(PS_ipc
));
644 if (x
< (ssize_t
)sizeof(PS_ipc
)) {
645 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: write FD " << c2p
[1] << ": " << xstrerror());
646 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
648 ipcSend(cwfd
, err_string
, strlen(err_string
));
652 x
= read(p2c
[0], buf1
, 8192);
655 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: read FD " << p2c
[0] << ": " << xstrerror());
656 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
658 ipcSend(cwfd
, err_string
, strlen(err_string
));
660 } else if (strncmp(buf1
, ok_string
, strlen(ok_string
))) {
661 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
662 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
664 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(buf1
) << "'");
665 ipcSend(cwfd
, err_string
, strlen(err_string
));
669 x
= send(pwfd_ipc
, (const void *)ok_string
, strlen(ok_string
), 0);
670 x
= recv(prfd_ipc
, (void *)(buf1
+ 200), 8191 - 200, 0);
671 assert((size_t) x
== strlen(ok_string
)
672 && !strncmp(ok_string
, buf1
+ 200, strlen(ok_string
)));
673 } /* IPC_UDP_SOCKET */
675 snprintf(buf1
, 8191, "%s(%ld) CHILD socket", prog
, (long int) pid
);
679 if (prfd_ipc
!= -1) {
680 snprintf(buf1
, 8191, "%s(%ld) <-> ipc CHILD socket", prog
, (long int) pid
);
681 fd_note(crfd_ipc
, buf1
);
682 snprintf(buf1
, 8191, "%s(%ld) <-> ipc PARENT socket", prog
, (long int) pid
);
683 fd_note(prfd_ipc
, buf1
);
686 /* else { IPC_TCP_SOCKET */
687 /* commSetNoLinger(fd); */
689 thread_params
.prog
= prog
;
691 thread_params
.send_fd
= cwfd
;
693 thread_params
.pid
= pid
;
695 if ((thread_params
.type
= type
) == IPC_TCP_SOCKET
)
696 thread_params
.rfd
= p2c
[0];
698 thread_params
.rfd
= prfd_ipc
;
700 thread
= (HANDLE
)_beginthreadex(NULL
, 0, ipc_thread_2
, &thread_params
, 0, NULL
);
703 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: _beginthreadex: " << xstrerror());
704 ipcSend(cwfd
, err_string
, strlen(err_string
));
708 snprintf(buf1
, 8191, "%ld\n", (long int) pid
);
710 if (-1 == ipcSend(cwfd
, buf1
, strlen(buf1
)))
713 debugs(54, 2, "ipc(" << prog
<< "," << pid
<< "): started successfully");
717 x
= recv(crfd
, (void *)buf1
, 8192, 0);
720 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes received from parent. Exiting...");
726 if (type
== IPC_UDP_SOCKET
&& !strcmp(buf1
, shutdown_string
)) {
727 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): request for shutdown received from parent. Exiting...");
729 TerminateProcess(hProcess
, 0);
733 debugs(54, 5, "ipc(" << prog
<< "," << pid
<< "): received from parent: " << rfc1738_escape_unescaped(buf1
));
735 if (type
== IPC_TCP_SOCKET
)
736 x
= write(c2p
[1], buf1
, x
);
738 x
= send(pwfd_ipc
, (const void *)buf1
, x
, 0);
741 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes written to " << prog
<< ". Exiting...");
754 if (fd_table
[crfd
].flags
.open
)
755 ipcCloseAllFD(-1, -1, crfd
, cwfd
);
757 if (prfd_ipc
!= -1) {
758 send(crfd_ipc
, (const void *)shutdown_string
, strlen(shutdown_string
), 0);
759 shutdown(crfd_ipc
, SD_BOTH
);
760 shutdown(prfd_ipc
, SD_BOTH
);
763 ipcCloseAllFD(prfd_ipc
, pwfd_ipc
, crfd_ipc
, cwfd_ipc
);
765 if (hProcess
&& WAIT_OBJECT_0
!=
766 WaitForSingleObject(hProcess
, type
== IPC_UDP_SOCKET
? 12000 : 5000)) {
769 debugs(54, DBG_CRITICAL
, "ipc(" << prog
<< "," << pid
<< "): WARNING: " << prog
<<
770 " didn't exit in " << (type
== IPC_UDP_SOCKET
? 12 : 5) << " seconds.");
774 if (thread
&& WAIT_OBJECT_0
!= WaitForSingleObject(thread
, 3000)) {
776 debugs(54, DBG_CRITICAL
, "ipc(" << prog
<< "," << pid
<< "): WARNING: ipc_thread_2 didn't exit in 3 seconds.");
783 debugs(54, 2, "ipc(" << prog
<< "," << pid
<< "): normal exit");
795 CloseHandle(hProcess
);
803 static unsigned int __stdcall
804 ipc_thread_2(void *in_params
)
808 struct thread_params
*params
= (struct thread_params
*) in_params
;
809 int type
= params
->type
;
810 int rfd
= params
->rfd
;
811 int send_fd
= params
->send_fd
;
812 char *prog
= xstrdup(params
->prog
);
813 pid_t pid
= params
->pid
;
814 char *buf2
= (char *)xcalloc(1, 8192);
817 if (type
== IPC_TCP_SOCKET
)
818 x
= read(rfd
, buf2
, 8192);
820 x
= recv(rfd
, (void *)buf2
, 8192, 0);
822 if ((x
<= 0 && type
== IPC_TCP_SOCKET
) ||
823 (x
< 0 && type
== IPC_UDP_SOCKET
)) {
824 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes read from " << prog
<< ". Exiting...");
831 if (type
== IPC_UDP_SOCKET
&& !strcmp(buf2
, shutdown_string
)) {
832 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): request for shutdown received. Exiting...");
838 if ((buf2
[x
- 1] == '\n') && (buf2
[x
- 2] == '\r')) {
845 debugs(54, 5, "ipc(" << prog
<< "," << pid
<< "): received from child : " << rfc1738_escape_unescaped(buf2
));
847 x
= send(send_fd
, (const void *)buf2
, x
, 0);
849 if ((x
<= 0 && type
== IPC_TCP_SOCKET
) ||
850 (x
< 0 && type
== IPC_UDP_SOCKET
)) {
851 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes sent to parent. Exiting...");