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.
40 #include "ip/Address.h"
42 #include "SquidTime.h"
56 Ip::Address local_addr
;
62 struct thread_params
{
70 static unsigned int __stdcall
ipc_thread_1(void *params
);
71 static unsigned int __stdcall
ipc_thread_2(void *params
);
73 static const char *ok_string
= "OK\n";
74 static const char *err_string
= "ERR\n";
75 static const char *shutdown_string
= "$shutdown\n";
77 static const char *hello_string
= "hi there\n";
78 #define HELLO_BUF_SZ 32
79 static char hello_buf
[HELLO_BUF_SZ
];
82 ipcCloseAllFD(int prfd
, int pwfd
, int crfd
, int cwfd
)
107 env_str
= (char *)xcalloc((tmp_s
= strlen(Debug::debugOptions
) + 32), 1);
108 snprintf(env_str
, tmp_s
, "SQUID_DEBUG=%s", Debug::debugOptions
);
114 ipcCreate(int type
, const char *prog
, const char *const args
[], const char *name
, Ip::Address
&local_addr
, int *rfd
, int *wfd
, void **hIpc
)
116 unsigned long thread
;
118 struct ipc_params params
;
120 int optlen
= sizeof(opt
);
124 Ip::Address tmp_addr
;
125 struct addrinfo
*aiCS
= NULL
;
126 struct addrinfo
*aiPS
= NULL
;
134 requirePathnameExists(name
, prog
);
145 if (WIN32_OS_version
!= _WIN_OS_WINNT
) {
146 getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, &optlen
);
147 opt
= opt
& ~(SO_SYNCHRONOUS_NONALERT
| SO_SYNCHRONOUS_ALERT
);
148 setsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, sizeof(opt
));
151 if (type
== IPC_TCP_SOCKET
) {
152 crfd
= cwfd
= comm_open(SOCK_STREAM
,
157 prfd
= pwfd
= comm_open(SOCK_STREAM
,
158 IPPROTO_TCP
, /* protocol */
162 } else if (type
== IPC_UDP_SOCKET
) {
163 crfd
= cwfd
= comm_open(SOCK_DGRAM
,
168 prfd
= pwfd
= comm_open(SOCK_DGRAM
,
173 } else if (type
== IPC_FIFO
) {
174 debugs(54, DBG_CRITICAL
, "ipcCreate: " << prog
<< ": use IPC_TCP_SOCKET instead of IP_FIFO on Windows");
180 debugs(54, 3, "ipcCreate: prfd FD " << prfd
);
181 debugs(54, 3, "ipcCreate: pwfd FD " << pwfd
);
182 debugs(54, 3, "ipcCreate: crfd FD " << crfd
);
183 debugs(54, 3, "ipcCreate: cwfd FD " << cwfd
);
185 if (WIN32_OS_version
!= _WIN_OS_WINNT
) {
186 getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, &optlen
);
187 opt
= opt
| SO_SYNCHRONOUS_NONALERT
;
188 setsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, optlen
);
192 debugs(54, DBG_CRITICAL
, "ipcCreate: Failed to create child FD.");
193 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
197 debugs(54, DBG_CRITICAL
, "ipcCreate: Failed to create server FD.");
198 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
201 // AYJ: these flags should be neutral, but if not IPv6 version needs adding
202 if (type
== IPC_TCP_SOCKET
|| type
== IPC_UDP_SOCKET
) {
204 tmp_addr
.InitAddrInfo(aiPS
);
206 if (getsockname(pwfd
, aiPS
->ai_addr
, &(aiPS
->ai_addrlen
) ) < 0) {
207 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
208 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
213 debugs(54, 3, "ipcCreate: FD " << pwfd
<< " sockaddr " << tmp_addr
);
215 tmp_addr
.InitAddrInfo(aiCS
);
217 if (getsockname(crfd
, aiCS
->ai_addr
, &(aiCS
->ai_addrlen
) ) < 0) {
218 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
219 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
225 debugs(54, 3, "ipcCreate: FD " << crfd
<< " sockaddr " << tmp_addr
);
228 if (type
== IPC_TCP_SOCKET
) {
229 if (listen(crfd
, 1) < 0) {
230 debugs(54, DBG_IMPORTANT
, "ipcCreate: listen FD " << crfd
<< ": " << xstrerror());
231 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
234 debugs(54, 3, "ipcCreate: FD " << crfd
<< " listening...");
237 /* flush or else we get dup data if unbuffered_logs is set */
248 params
.local_addr
= local_addr
;
252 params
.args
= (char **) args
;
254 thread
= _beginthreadex(NULL
, 0, ipc_thread_1
, ¶ms
, 0, NULL
);
257 debugs(54, DBG_IMPORTANT
, "ipcCreate: _beginthread: " << xstrerror());
258 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
261 /* NP: tmp_addr was left with eiether empty or aiCS in Ip::Address format */
262 if (comm_connect_addr(pwfd
, tmp_addr
) == COMM_ERROR
) {
263 CloseHandle((HANDLE
) thread
);
264 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
267 memset(hello_buf
, '\0', HELLO_BUF_SZ
);
268 x
= recv(prfd
, (void *)hello_buf
, HELLO_BUF_SZ
- 1, 0);
271 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: hello read test failed");
272 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
273 CloseHandle((HANDLE
) thread
);
274 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
275 } else if (strcmp(hello_buf
, hello_string
)) {
276 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: hello read test failed");
277 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
278 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
279 CloseHandle((HANDLE
) thread
);
280 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
283 x
= send(pwfd
, (const void *)ok_string
, strlen(ok_string
), 0);
286 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: OK write test failed");
287 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
288 CloseHandle((HANDLE
) thread
);
289 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
292 memset(hello_buf
, '\0', HELLO_BUF_SZ
);
293 x
= recv(prfd
, (void *)hello_buf
, HELLO_BUF_SZ
- 1, 0);
296 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: OK read test failed");
297 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
298 CloseHandle((HANDLE
) thread
);
299 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
300 } else if (!strcmp(hello_buf
, err_string
)) {
301 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: OK read test failed");
302 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
303 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
304 CloseHandle((HANDLE
) thread
);
305 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
309 pid
= atol(hello_buf
);
310 commUnsetFdTimeout(prfd
);
311 commSetNonBlocking(prfd
);
312 commSetNonBlocking(pwfd
);
313 commSetCloseOnExec(prfd
);
314 commSetCloseOnExec(pwfd
);
322 fd_table
[prfd
].flags
.ipc
= 1;
324 fd_table
[pwfd
].flags
.ipc
= 1;
326 fd_table
[crfd
].flags
.ipc
= 1;
328 fd_table
[cwfd
].flags
.ipc
= 1;
330 if (Config
.sleep_after_fork
) {
331 /* XXX emulation of usleep() */
333 sl
= Config
.sleep_after_fork
/ 1000;
341 if (GetExitCodeThread((HANDLE
) thread
, &ecode
) && ecode
== STILL_ACTIVE
) {
343 *hIpc
= (HANDLE
) thread
;
347 CloseHandle((HANDLE
) thread
);
348 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
353 ipcSend(int cwfd
, const char *buf
, int len
)
357 x
= send(cwfd
, (const void *)buf
, len
, 0);
360 debugs(54, DBG_CRITICAL
, "sendto FD " << cwfd
<< ": " << xstrerror());
361 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: hello write test failed");
367 static unsigned int __stdcall
368 ipc_thread_1(void *in_params
)
370 int t1
, t2
, t3
, retval
= -1;
371 int p2c
[2] = {-1, -1};
372 int c2p
[2] = {-1, -1};
373 HANDLE hProcess
= NULL
, thread
= NULL
;
376 struct thread_params thread_params
;
381 PROCESS_INFORMATION pi
;
383 int prfd_ipc
= -1, pwfd_ipc
= -1, crfd_ipc
= -1, cwfd_ipc
= -1;
384 char *prog
= NULL
, *buf1
= NULL
;
388 struct addrinfo
*aiPS_ipc
= NULL
;
389 struct addrinfo
*aiCS_ipc
= NULL
;
391 struct ipc_params
*params
= (struct ipc_params
*) in_params
;
392 int type
= params
->type
;
393 int crfd
= params
->crfd
;
394 int cwfd
= params
->cwfd
;
395 char **args
= params
->args
;
397 Ip::Address PS
= params
->PS
;
398 Ip::Address local_addr
= params
->local_addr
;
400 buf1
= (char *)xcalloc(1, 8192);
401 strcpy(buf1
, params
->prog
);
402 prog
= strtok(buf1
, w_space
);
404 if ((str
= strrchr(prog
, '/')))
407 if ((str
= strrchr(prog
, '\\')))
410 prog
= xstrdup(prog
);
412 if (type
== IPC_TCP_SOCKET
) {
413 debugs(54, 3, "ipcCreate: calling accept on FD " << crfd
);
415 if ((fd
= accept(crfd
, NULL
, NULL
)) < 0) {
416 debugs(54, DBG_CRITICAL
, "ipcCreate: FD " << crfd
<< " accept: " << xstrerror());
420 debugs(54, 3, "ipcCreate: CHILD accepted new FD " << fd
);
422 snprintf(buf1
, 8191, "%s CHILD socket", prog
);
423 fd_open(fd
, FD_SOCKET
, buf1
);
424 fd_table
[fd
].flags
.ipc
= 1;
426 } else if (type
== IPC_UDP_SOCKET
) {
427 if (comm_connect_addr(crfd
, params
->PS
) == COMM_ERROR
)
431 x
= send(cwfd
, (const void *)hello_string
, strlen(hello_string
) + 1, 0);
434 debugs(54, DBG_CRITICAL
, "sendto FD " << cwfd
<< ": " << xstrerror());
435 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: hello write test failed");
440 memset(buf1
, '\0', sizeof(buf1
));
441 x
= recv(crfd
, (void *)buf1
, 8191, 0);
444 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: OK read test failed");
445 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
447 } else if (strcmp(buf1
, ok_string
)) {
448 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: OK read test failed");
449 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
450 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
454 /* assign file descriptors to child process */
455 if (_pipe(p2c
, 1024, _O_BINARY
| _O_NOINHERIT
) < 0) {
456 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: pipe: " << xstrerror());
457 ipcSend(cwfd
, err_string
, strlen(err_string
));
461 if (_pipe(c2p
, 1024, _O_BINARY
| _O_NOINHERIT
) < 0) {
462 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: pipe: " << xstrerror());
463 ipcSend(cwfd
, err_string
, strlen(err_string
));
467 if (type
== IPC_UDP_SOCKET
) {
468 snprintf(buf1
, 8192, "%s(%ld) <-> ipc CHILD socket", prog
, -1L);
469 crfd_ipc
= cwfd_ipc
= comm_open(SOCK_DGRAM
, IPPROTO_UDP
, local_addr
, 0, buf1
);
472 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: Failed to create child FD for " << prog
<< ".");
473 ipcSend(cwfd
, err_string
, strlen(err_string
));
477 snprintf(buf1
, 8192, "%s(%ld) <-> ipc PARENT socket", prog
, -1L);
478 prfd_ipc
= pwfd_ipc
= comm_open(SOCK_DGRAM
, IPPROTO_UDP
, local_addr
, 0, buf1
);
481 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: Failed to create server FD for " << prog
<< ".");
482 ipcSend(cwfd
, err_string
, strlen(err_string
));
486 PS_ipc
.InitAddrInfo(aiPS_ipc
);
488 if (getsockname(pwfd_ipc
, aiPS_ipc
->ai_addr
, &(aiPS_ipc
->ai_addrlen
)) < 0) {
489 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
490 ipcSend(cwfd
, err_string
, strlen(err_string
));
496 debugs(54, 3, "ipcCreate: FD " << pwfd_ipc
<< " sockaddr " << PS_ipc
);
498 CS_ipc
.InitAddrInfo(aiCS_ipc
);
500 if (getsockname(crfd_ipc
, aiCS_ipc
->ai_addr
, &(aiCS_ipc
->ai_addrlen
)) < 0) {
501 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
502 ipcSend(cwfd
, err_string
, strlen(err_string
));
508 debugs(54, 3, "ipcCreate: FD " << crfd_ipc
<< " sockaddr " << CS_ipc
);
510 if (comm_connect_addr(pwfd_ipc
, CS_ipc
) == COMM_ERROR
) {
511 ipcSend(cwfd
, err_string
, strlen(err_string
));
517 if (comm_connect_addr(crfd_ipc
, PS_ipc
) == COMM_ERROR
) {
518 ipcSend(cwfd
, err_string
, strlen(err_string
));
521 } /* IPC_UDP_SOCKET */
533 dup2(fileno(debug_log
), 2);
539 commUnsetNonBlocking(fd
);
541 memset(&si
, 0, sizeof(STARTUPINFO
));
543 si
.cb
= sizeof(STARTUPINFO
);
545 si
.hStdInput
= (HANDLE
) _get_osfhandle(0);
547 si
.hStdOutput
= (HANDLE
) _get_osfhandle(1);
549 si
.hStdError
= (HANDLE
) _get_osfhandle(2);
551 si
.dwFlags
= STARTF_USESTDHANDLES
;
553 /* Make sure all other valid handles are not inerithable */
554 for (x
= 3; x
< Squid_MaxFD
; ++x
) {
555 if ((F
= _get_osfhandle(x
)) == -1)
558 SetHandleInformation((HANDLE
) F
, HANDLE_FLAG_INHERIT
, 0);
562 strcpy(buf1
+ 4096, params
->prog
);
563 str
= strtok(buf1
+ 4096, w_space
);
568 } while ((str
= strtok(NULL
, w_space
)));
573 strcat(buf1
, args
[x
]);
578 if (CreateProcess(buf1
+ 4096, buf1
, NULL
, NULL
, TRUE
, CREATE_NO_WINDOW
,
579 NULL
, NULL
, &si
, &pi
)) {
580 pid
= pi
.dwProcessId
;
581 hProcess
= pi
.hProcess
;
596 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << params
->prog
<< ": " << xstrerror());
598 ipcSend(cwfd
, err_string
, strlen(err_string
));
602 if (type
== IPC_UDP_SOCKET
) {
603 WSAPROTOCOL_INFO wpi
;
605 memset(&wpi
, 0, sizeof(wpi
));
607 if (SOCKET_ERROR
== WSADuplicateSocket(crfd_ipc
, pid
, &wpi
)) {
608 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: WSADuplicateSocket: " << xstrerror());
610 ipcSend(cwfd
, err_string
, strlen(err_string
));
614 x
= write(c2p
[1], (const char *) &wpi
, sizeof(wpi
));
616 if (x
< (ssize_t
)sizeof(wpi
)) {
617 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: write FD " << c2p
[1] << ": " << xstrerror());
618 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
620 ipcSend(cwfd
, err_string
, strlen(err_string
));
624 x
= read(p2c
[0], buf1
, 8192);
627 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: read FD " << p2c
[0] << ": " << xstrerror());
628 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
630 ipcSend(cwfd
, err_string
, strlen(err_string
));
632 } else if (strncmp(buf1
, ok_string
, strlen(ok_string
))) {
633 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
634 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
636 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(buf1
) << "'");
637 ipcSend(cwfd
, err_string
, strlen(err_string
));
641 x
= write(c2p
[1], (const char *) &PS_ipc
, sizeof(PS_ipc
));
643 if (x
< (ssize_t
)sizeof(PS_ipc
)) {
644 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: write FD " << c2p
[1] << ": " << xstrerror());
645 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
647 ipcSend(cwfd
, err_string
, strlen(err_string
));
651 x
= read(p2c
[0], buf1
, 8192);
654 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: read FD " << p2c
[0] << ": " << xstrerror());
655 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
657 ipcSend(cwfd
, err_string
, strlen(err_string
));
659 } else if (strncmp(buf1
, ok_string
, strlen(ok_string
))) {
660 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
661 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
663 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(buf1
) << "'");
664 ipcSend(cwfd
, err_string
, strlen(err_string
));
668 x
= send(pwfd_ipc
, (const void *)ok_string
, strlen(ok_string
), 0);
669 x
= recv(prfd_ipc
, (void *)(buf1
+ 200), 8191 - 200, 0);
670 assert((size_t) x
== strlen(ok_string
)
671 && !strncmp(ok_string
, buf1
+ 200, strlen(ok_string
)));
672 } /* IPC_UDP_SOCKET */
674 snprintf(buf1
, 8191, "%s(%ld) CHILD socket", prog
, (long int) pid
);
678 if (prfd_ipc
!= -1) {
679 snprintf(buf1
, 8191, "%s(%ld) <-> ipc CHILD socket", prog
, (long int) pid
);
680 fd_note(crfd_ipc
, buf1
);
681 snprintf(buf1
, 8191, "%s(%ld) <-> ipc PARENT socket", prog
, (long int) pid
);
682 fd_note(prfd_ipc
, buf1
);
685 /* else { IPC_TCP_SOCKET */
686 /* commSetNoLinger(fd); */
688 thread_params
.prog
= prog
;
690 thread_params
.send_fd
= cwfd
;
692 thread_params
.pid
= pid
;
694 if ((thread_params
.type
= type
) == IPC_TCP_SOCKET
)
695 thread_params
.rfd
= p2c
[0];
697 thread_params
.rfd
= prfd_ipc
;
699 thread
= (HANDLE
)_beginthreadex(NULL
, 0, ipc_thread_2
, &thread_params
, 0, NULL
);
702 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: _beginthreadex: " << xstrerror());
703 ipcSend(cwfd
, err_string
, strlen(err_string
));
707 snprintf(buf1
, 8191, "%ld\n", (long int) pid
);
709 if (-1 == ipcSend(cwfd
, buf1
, strlen(buf1
)))
712 debugs(54, 2, "ipc(" << prog
<< "," << pid
<< "): started successfully");
716 x
= recv(crfd
, (void *)buf1
, 8192, 0);
719 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes received from parent. Exiting...");
725 if (type
== IPC_UDP_SOCKET
&& !strcmp(buf1
, shutdown_string
)) {
726 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): request for shutdown received from parent. Exiting...");
728 TerminateProcess(hProcess
, 0);
732 debugs(54, 5, "ipc(" << prog
<< "," << pid
<< "): received from parent: " << rfc1738_escape_unescaped(buf1
));
734 if (type
== IPC_TCP_SOCKET
)
735 x
= write(c2p
[1], buf1
, x
);
737 x
= send(pwfd_ipc
, (const void *)buf1
, x
, 0);
740 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes written to " << prog
<< ". Exiting...");
753 if (fd_table
[crfd
].flags
.open
)
754 ipcCloseAllFD(-1, -1, crfd
, cwfd
);
756 if (prfd_ipc
!= -1) {
757 send(crfd_ipc
, (const void *)shutdown_string
, strlen(shutdown_string
), 0);
758 shutdown(crfd_ipc
, SD_BOTH
);
759 shutdown(prfd_ipc
, SD_BOTH
);
762 ipcCloseAllFD(prfd_ipc
, pwfd_ipc
, crfd_ipc
, cwfd_ipc
);
764 if (hProcess
&& WAIT_OBJECT_0
!=
765 WaitForSingleObject(hProcess
, type
== IPC_UDP_SOCKET
? 12000 : 5000)) {
768 debugs(54, DBG_CRITICAL
, "ipc(" << prog
<< "," << pid
<< "): WARNING: " << prog
<<
769 " didn't exit in " << (type
== IPC_UDP_SOCKET
? 12 : 5) << " seconds.");
773 if (thread
&& WAIT_OBJECT_0
!= WaitForSingleObject(thread
, 3000)) {
775 debugs(54, DBG_CRITICAL
, "ipc(" << prog
<< "," << pid
<< "): WARNING: ipc_thread_2 didn't exit in 3 seconds.");
782 debugs(54, 2, "ipc(" << prog
<< "," << pid
<< "): normal exit");
794 CloseHandle(hProcess
);
802 static unsigned int __stdcall
803 ipc_thread_2(void *in_params
)
807 struct thread_params
*params
= (struct thread_params
*) in_params
;
808 int type
= params
->type
;
809 int rfd
= params
->rfd
;
810 int send_fd
= params
->send_fd
;
811 char *prog
= xstrdup(params
->prog
);
812 pid_t pid
= params
->pid
;
813 char *buf2
= (char *)xcalloc(1, 8192);
816 if (type
== IPC_TCP_SOCKET
)
817 x
= read(rfd
, buf2
, 8192);
819 x
= recv(rfd
, (void *)buf2
, 8192, 0);
821 if ((x
<= 0 && type
== IPC_TCP_SOCKET
) ||
822 (x
< 0 && type
== IPC_UDP_SOCKET
)) {
823 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes read from " << prog
<< ". Exiting...");
830 if (type
== IPC_UDP_SOCKET
&& !strcmp(buf2
, shutdown_string
)) {
831 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): request for shutdown received. Exiting...");
837 if ((buf2
[x
- 1] == '\n') && (buf2
[x
- 2] == '\r')) {
844 debugs(54, 5, "ipc(" << prog
<< "," << pid
<< "): received from child : " << rfc1738_escape_unescaped(buf2
));
846 x
= send(send_fd
, (const void *)buf2
, x
, 0);
848 if ((x
<= 0 && type
== IPC_TCP_SOCKET
) ||
849 (x
< 0 && type
== IPC_UDP_SOCKET
)) {
850 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes sent to parent. Exiting...");