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.
39 #include "ip/Address.h"
41 #include "SquidTime.h"
55 Ip::Address local_addr
;
61 struct thread_params
{
69 static unsigned int __stdcall
ipc_thread_1(void *params
);
70 static unsigned int __stdcall
ipc_thread_2(void *params
);
72 static const char *ok_string
= "OK\n";
73 static const char *err_string
= "ERR\n";
74 static const char *shutdown_string
= "$shutdown\n";
76 static const char *hello_string
= "hi there\n";
77 #define HELLO_BUF_SZ 32
78 static char hello_buf
[HELLO_BUF_SZ
];
81 ipcCloseAllFD(int prfd
, int pwfd
, int crfd
, int cwfd
)
106 env_str
= (char *)xcalloc((tmp_s
= strlen(Debug::debugOptions
) + 32), 1);
107 snprintf(env_str
, tmp_s
, "SQUID_DEBUG=%s", Debug::debugOptions
);
113 ipcCreate(int type
, const char *prog
, const char *const args
[], const char *name
, Ip::Address
&local_addr
, int *rfd
, int *wfd
, void **hIpc
)
115 unsigned long thread
;
117 struct ipc_params params
;
119 int optlen
= sizeof(opt
);
123 Ip::Address tmp_addr
;
124 struct addrinfo
*aiCS
= NULL
;
125 struct addrinfo
*aiPS
= NULL
;
133 requirePathnameExists(name
, prog
);
144 if (WIN32_OS_version
!= _WIN_OS_WINNT
) {
145 getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, &optlen
);
146 opt
= opt
& ~(SO_SYNCHRONOUS_NONALERT
| SO_SYNCHRONOUS_ALERT
);
147 setsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, sizeof(opt
));
150 if (type
== IPC_TCP_SOCKET
) {
151 crfd
= cwfd
= comm_open(SOCK_STREAM
,
156 prfd
= pwfd
= comm_open(SOCK_STREAM
,
157 IPPROTO_TCP
, /* protocol */
161 } else if (type
== IPC_UDP_SOCKET
) {
162 crfd
= cwfd
= comm_open(SOCK_DGRAM
,
167 prfd
= pwfd
= comm_open(SOCK_DGRAM
,
172 } else if (type
== IPC_FIFO
) {
173 debugs(54, DBG_CRITICAL
, "ipcCreate: " << prog
<< ": use IPC_TCP_SOCKET instead of IP_FIFO on Windows");
179 debugs(54, 3, "ipcCreate: prfd FD " << prfd
);
180 debugs(54, 3, "ipcCreate: pwfd FD " << pwfd
);
181 debugs(54, 3, "ipcCreate: crfd FD " << crfd
);
182 debugs(54, 3, "ipcCreate: cwfd FD " << cwfd
);
184 if (WIN32_OS_version
!= _WIN_OS_WINNT
) {
185 getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, &optlen
);
186 opt
= opt
| SO_SYNCHRONOUS_NONALERT
;
187 setsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, optlen
);
191 debugs(54, DBG_CRITICAL
, "ipcCreate: Failed to create child FD.");
192 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
196 debugs(54, DBG_CRITICAL
, "ipcCreate: Failed to create server FD.");
197 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
200 // AYJ: these flags should be neutral, but if not IPv6 version needs adding
201 if (type
== IPC_TCP_SOCKET
|| type
== IPC_UDP_SOCKET
) {
203 tmp_addr
.InitAddrInfo(aiPS
);
205 if (getsockname(pwfd
, aiPS
->ai_addr
, &(aiPS
->ai_addrlen
) ) < 0) {
206 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
207 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
212 debugs(54, 3, "ipcCreate: FD " << pwfd
<< " sockaddr " << tmp_addr
);
214 tmp_addr
.InitAddrInfo(aiCS
);
216 if (getsockname(crfd
, aiCS
->ai_addr
, &(aiCS
->ai_addrlen
) ) < 0) {
217 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
218 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
224 debugs(54, 3, "ipcCreate: FD " << crfd
<< " sockaddr " << tmp_addr
);
227 if (type
== IPC_TCP_SOCKET
) {
228 if (listen(crfd
, 1) < 0) {
229 debugs(54, DBG_IMPORTANT
, "ipcCreate: listen FD " << crfd
<< ": " << xstrerror());
230 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
233 debugs(54, 3, "ipcCreate: FD " << crfd
<< " listening...");
236 /* flush or else we get dup data if unbuffered_logs is set */
247 params
.local_addr
= local_addr
;
251 params
.args
= (char **) args
;
253 thread
= _beginthreadex(NULL
, 0, ipc_thread_1
, ¶ms
, 0, NULL
);
256 debugs(54, DBG_IMPORTANT
, "ipcCreate: _beginthread: " << xstrerror());
257 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
260 /* NP: tmp_addr was left with eiether empty or aiCS in Ip::Address format */
261 if (comm_connect_addr(pwfd
, tmp_addr
) == COMM_ERROR
) {
262 CloseHandle((HANDLE
) thread
);
263 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
266 memset(hello_buf
, '\0', HELLO_BUF_SZ
);
267 x
= recv(prfd
, (void *)hello_buf
, HELLO_BUF_SZ
- 1, 0);
270 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: hello read test failed");
271 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
272 CloseHandle((HANDLE
) thread
);
273 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
274 } else if (strcmp(hello_buf
, hello_string
)) {
275 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: hello read test failed");
276 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
277 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
278 CloseHandle((HANDLE
) thread
);
279 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
282 x
= send(pwfd
, (const void *)ok_string
, strlen(ok_string
), 0);
285 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: OK write test failed");
286 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
287 CloseHandle((HANDLE
) thread
);
288 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
291 memset(hello_buf
, '\0', HELLO_BUF_SZ
);
292 x
= recv(prfd
, (void *)hello_buf
, HELLO_BUF_SZ
- 1, 0);
295 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: OK read test failed");
296 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
297 CloseHandle((HANDLE
) thread
);
298 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
299 } else if (!strcmp(hello_buf
, err_string
)) {
300 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: OK read test failed");
301 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
302 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
303 CloseHandle((HANDLE
) thread
);
304 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
308 pid
= atol(hello_buf
);
309 commUnsetFdTimeout(prfd
);
310 commSetNonBlocking(prfd
);
311 commSetNonBlocking(pwfd
);
312 commSetCloseOnExec(prfd
);
313 commSetCloseOnExec(pwfd
);
321 fd_table
[prfd
].flags
.ipc
= 1;
323 fd_table
[pwfd
].flags
.ipc
= 1;
325 fd_table
[crfd
].flags
.ipc
= 1;
327 fd_table
[cwfd
].flags
.ipc
= 1;
329 if (Config
.sleep_after_fork
) {
330 /* XXX emulation of usleep() */
332 sl
= Config
.sleep_after_fork
/ 1000;
340 if (GetExitCodeThread((HANDLE
) thread
, &ecode
) && ecode
== STILL_ACTIVE
) {
342 *hIpc
= (HANDLE
) thread
;
346 CloseHandle((HANDLE
) thread
);
347 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
352 ipcSend(int cwfd
, const char *buf
, int len
)
356 x
= send(cwfd
, (const void *)buf
, len
, 0);
359 debugs(54, DBG_CRITICAL
, "sendto FD " << cwfd
<< ": " << xstrerror());
360 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: hello write test failed");
366 static unsigned int __stdcall
367 ipc_thread_1(void *in_params
)
369 int t1
, t2
, t3
, retval
= -1;
370 int p2c
[2] = {-1, -1};
371 int c2p
[2] = {-1, -1};
372 HANDLE hProcess
= NULL
, thread
= NULL
;
375 struct thread_params thread_params
;
380 PROCESS_INFORMATION pi
;
382 int prfd_ipc
= -1, pwfd_ipc
= -1, crfd_ipc
= -1, cwfd_ipc
= -1;
383 char *prog
= NULL
, *buf1
= NULL
;
387 struct addrinfo
*aiPS_ipc
= NULL
;
388 struct addrinfo
*aiCS_ipc
= NULL
;
390 struct ipc_params
*params
= (struct ipc_params
*) in_params
;
391 int type
= params
->type
;
392 int crfd
= params
->crfd
;
393 int cwfd
= params
->cwfd
;
394 char **args
= params
->args
;
396 Ip::Address PS
= params
->PS
;
397 Ip::Address local_addr
= params
->local_addr
;
399 buf1
= (char *)xcalloc(1, 8192);
400 strcpy(buf1
, params
->prog
);
401 prog
= strtok(buf1
, w_space
);
403 if ((str
= strrchr(prog
, '/')))
406 if ((str
= strrchr(prog
, '\\')))
409 prog
= xstrdup(prog
);
411 if (type
== IPC_TCP_SOCKET
) {
412 debugs(54, 3, "ipcCreate: calling accept on FD " << crfd
);
414 if ((fd
= accept(crfd
, NULL
, NULL
)) < 0) {
415 debugs(54, DBG_CRITICAL
, "ipcCreate: FD " << crfd
<< " accept: " << xstrerror());
419 debugs(54, 3, "ipcCreate: CHILD accepted new FD " << fd
);
421 snprintf(buf1
, 8191, "%s CHILD socket", prog
);
422 fd_open(fd
, FD_SOCKET
, buf1
);
423 fd_table
[fd
].flags
.ipc
= 1;
425 } else if (type
== IPC_UDP_SOCKET
) {
426 if (comm_connect_addr(crfd
, params
->PS
) == COMM_ERROR
)
430 x
= send(cwfd
, (const void *)hello_string
, strlen(hello_string
) + 1, 0);
433 debugs(54, DBG_CRITICAL
, "sendto FD " << cwfd
<< ": " << xstrerror());
434 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: hello write test failed");
439 memset(buf1
, '\0', sizeof(buf1
));
440 x
= recv(crfd
, (void *)buf1
, 8191, 0);
443 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: OK read test failed");
444 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
446 } else if (strcmp(buf1
, ok_string
)) {
447 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: OK read test failed");
448 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
449 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
453 /* assign file descriptors to child process */
454 if (_pipe(p2c
, 1024, _O_BINARY
| _O_NOINHERIT
) < 0) {
455 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: pipe: " << xstrerror());
456 ipcSend(cwfd
, err_string
, strlen(err_string
));
460 if (_pipe(c2p
, 1024, _O_BINARY
| _O_NOINHERIT
) < 0) {
461 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: pipe: " << xstrerror());
462 ipcSend(cwfd
, err_string
, strlen(err_string
));
466 if (type
== IPC_UDP_SOCKET
) {
467 snprintf(buf1
, 8192, "%s(%ld) <-> ipc CHILD socket", prog
, -1L);
468 crfd_ipc
= cwfd_ipc
= comm_open(SOCK_DGRAM
, IPPROTO_UDP
, local_addr
, 0, buf1
);
471 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: Failed to create child FD for " << prog
<< ".");
472 ipcSend(cwfd
, err_string
, strlen(err_string
));
476 snprintf(buf1
, 8192, "%s(%ld) <-> ipc PARENT socket", prog
, -1L);
477 prfd_ipc
= pwfd_ipc
= comm_open(SOCK_DGRAM
, IPPROTO_UDP
, local_addr
, 0, buf1
);
480 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: Failed to create server FD for " << prog
<< ".");
481 ipcSend(cwfd
, err_string
, strlen(err_string
));
485 PS_ipc
.InitAddrInfo(aiPS_ipc
);
487 if (getsockname(pwfd_ipc
, aiPS_ipc
->ai_addr
, &(aiPS_ipc
->ai_addrlen
)) < 0) {
488 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
489 ipcSend(cwfd
, err_string
, strlen(err_string
));
495 debugs(54, 3, "ipcCreate: FD " << pwfd_ipc
<< " sockaddr " << PS_ipc
);
497 CS_ipc
.InitAddrInfo(aiCS_ipc
);
499 if (getsockname(crfd_ipc
, aiCS_ipc
->ai_addr
, &(aiCS_ipc
->ai_addrlen
)) < 0) {
500 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
501 ipcSend(cwfd
, err_string
, strlen(err_string
));
507 debugs(54, 3, "ipcCreate: FD " << crfd_ipc
<< " sockaddr " << CS_ipc
);
509 if (comm_connect_addr(pwfd_ipc
, CS_ipc
) == COMM_ERROR
) {
510 ipcSend(cwfd
, err_string
, strlen(err_string
));
516 if (comm_connect_addr(crfd_ipc
, PS_ipc
) == COMM_ERROR
) {
517 ipcSend(cwfd
, err_string
, strlen(err_string
));
520 } /* IPC_UDP_SOCKET */
532 dup2(fileno(debug_log
), 2);
538 commUnsetNonBlocking(fd
);
540 memset(&si
, 0, sizeof(STARTUPINFO
));
542 si
.cb
= sizeof(STARTUPINFO
);
544 si
.hStdInput
= (HANDLE
) _get_osfhandle(0);
546 si
.hStdOutput
= (HANDLE
) _get_osfhandle(1);
548 si
.hStdError
= (HANDLE
) _get_osfhandle(2);
550 si
.dwFlags
= STARTF_USESTDHANDLES
;
552 /* Make sure all other valid handles are not inerithable */
553 for (x
= 3; x
< Squid_MaxFD
; ++x
) {
554 if ((F
= _get_osfhandle(x
)) == -1)
557 SetHandleInformation((HANDLE
) F
, HANDLE_FLAG_INHERIT
, 0);
561 strcpy(buf1
+ 4096, params
->prog
);
562 str
= strtok(buf1
+ 4096, w_space
);
567 } while ((str
= strtok(NULL
, w_space
)));
572 strcat(buf1
, args
[x
]);
577 if (CreateProcess(buf1
+ 4096, buf1
, NULL
, NULL
, TRUE
, CREATE_NO_WINDOW
,
578 NULL
, NULL
, &si
, &pi
)) {
579 pid
= pi
.dwProcessId
;
580 hProcess
= pi
.hProcess
;
595 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << params
->prog
<< ": " << xstrerror());
597 ipcSend(cwfd
, err_string
, strlen(err_string
));
601 if (type
== IPC_UDP_SOCKET
) {
602 WSAPROTOCOL_INFO wpi
;
604 memset(&wpi
, 0, sizeof(wpi
));
606 if (SOCKET_ERROR
== WSADuplicateSocket(crfd_ipc
, pid
, &wpi
)) {
607 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: WSADuplicateSocket: " << xstrerror());
609 ipcSend(cwfd
, err_string
, strlen(err_string
));
613 x
= write(c2p
[1], (const char *) &wpi
, sizeof(wpi
));
615 if (x
< (ssize_t
)sizeof(wpi
)) {
616 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: write FD " << c2p
[1] << ": " << xstrerror());
617 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
619 ipcSend(cwfd
, err_string
, strlen(err_string
));
623 x
= read(p2c
[0], buf1
, 8192);
626 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: read FD " << p2c
[0] << ": " << xstrerror());
627 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
629 ipcSend(cwfd
, err_string
, strlen(err_string
));
631 } else if (strncmp(buf1
, ok_string
, strlen(ok_string
))) {
632 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
633 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
635 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(buf1
) << "'");
636 ipcSend(cwfd
, err_string
, strlen(err_string
));
640 x
= write(c2p
[1], (const char *) &PS_ipc
, sizeof(PS_ipc
));
642 if (x
< (ssize_t
)sizeof(PS_ipc
)) {
643 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: write FD " << c2p
[1] << ": " << xstrerror());
644 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
646 ipcSend(cwfd
, err_string
, strlen(err_string
));
650 x
= read(p2c
[0], buf1
, 8192);
653 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: read FD " << p2c
[0] << ": " << xstrerror());
654 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
656 ipcSend(cwfd
, err_string
, strlen(err_string
));
658 } else if (strncmp(buf1
, ok_string
, strlen(ok_string
))) {
659 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
660 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
662 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(buf1
) << "'");
663 ipcSend(cwfd
, err_string
, strlen(err_string
));
667 x
= send(pwfd_ipc
, (const void *)ok_string
, strlen(ok_string
), 0);
668 x
= recv(prfd_ipc
, (void *)(buf1
+ 200), 8191 - 200, 0);
669 assert((size_t) x
== strlen(ok_string
)
670 && !strncmp(ok_string
, buf1
+ 200, strlen(ok_string
)));
671 } /* IPC_UDP_SOCKET */
673 snprintf(buf1
, 8191, "%s(%ld) CHILD socket", prog
, (long int) pid
);
677 if (prfd_ipc
!= -1) {
678 snprintf(buf1
, 8191, "%s(%ld) <-> ipc CHILD socket", prog
, (long int) pid
);
679 fd_note(crfd_ipc
, buf1
);
680 snprintf(buf1
, 8191, "%s(%ld) <-> ipc PARENT socket", prog
, (long int) pid
);
681 fd_note(prfd_ipc
, buf1
);
684 /* else { IPC_TCP_SOCKET */
685 /* commSetNoLinger(fd); */
687 thread_params
.prog
= prog
;
689 thread_params
.send_fd
= cwfd
;
691 thread_params
.pid
= pid
;
693 if ((thread_params
.type
= type
) == IPC_TCP_SOCKET
)
694 thread_params
.rfd
= p2c
[0];
696 thread_params
.rfd
= prfd_ipc
;
698 thread
= (HANDLE
)_beginthreadex(NULL
, 0, ipc_thread_2
, &thread_params
, 0, NULL
);
701 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: _beginthreadex: " << xstrerror());
702 ipcSend(cwfd
, err_string
, strlen(err_string
));
706 snprintf(buf1
, 8191, "%ld\n", (long int) pid
);
708 if (-1 == ipcSend(cwfd
, buf1
, strlen(buf1
)))
711 debugs(54, 2, "ipc(" << prog
<< "," << pid
<< "): started successfully");
715 x
= recv(crfd
, (void *)buf1
, 8192, 0);
718 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes received from parent. Exiting...");
724 if (type
== IPC_UDP_SOCKET
&& !strcmp(buf1
, shutdown_string
)) {
725 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): request for shutdown received from parent. Exiting...");
727 TerminateProcess(hProcess
, 0);
731 debugs(54, 5, "ipc(" << prog
<< "," << pid
<< "): received from parent: " << rfc1738_escape_unescaped(buf1
));
733 if (type
== IPC_TCP_SOCKET
)
734 x
= write(c2p
[1], buf1
, x
);
736 x
= send(pwfd_ipc
, (const void *)buf1
, x
, 0);
739 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes written to " << prog
<< ". Exiting...");
752 if (fd_table
[crfd
].flags
.open
)
753 ipcCloseAllFD(-1, -1, crfd
, cwfd
);
755 if (prfd_ipc
!= -1) {
756 send(crfd_ipc
, (const void *)shutdown_string
, strlen(shutdown_string
), 0);
757 shutdown(crfd_ipc
, SD_BOTH
);
758 shutdown(prfd_ipc
, SD_BOTH
);
761 ipcCloseAllFD(prfd_ipc
, pwfd_ipc
, crfd_ipc
, cwfd_ipc
);
763 if (hProcess
&& WAIT_OBJECT_0
!=
764 WaitForSingleObject(hProcess
, type
== IPC_UDP_SOCKET
? 12000 : 5000)) {
767 debugs(54, DBG_CRITICAL
, "ipc(" << prog
<< "," << pid
<< "): WARNING: " << prog
<<
768 " didn't exit in " << (type
== IPC_UDP_SOCKET
? 12 : 5) << " seconds.");
772 if (thread
&& WAIT_OBJECT_0
!= WaitForSingleObject(thread
, 3000)) {
774 debugs(54, DBG_CRITICAL
, "ipc(" << prog
<< "," << pid
<< "): WARNING: ipc_thread_2 didn't exit in 3 seconds.");
781 debugs(54, 2, "ipc(" << prog
<< "," << pid
<< "): normal exit");
793 CloseHandle(hProcess
);
801 static unsigned int __stdcall
802 ipc_thread_2(void *in_params
)
806 struct thread_params
*params
= (struct thread_params
*) in_params
;
807 int type
= params
->type
;
808 int rfd
= params
->rfd
;
809 int send_fd
= params
->send_fd
;
810 char *prog
= xstrdup(params
->prog
);
811 pid_t pid
= params
->pid
;
812 char *buf2
= (char *)xcalloc(1, 8192);
815 if (type
== IPC_TCP_SOCKET
)
816 x
= read(rfd
, buf2
, 8192);
818 x
= recv(rfd
, (void *)buf2
, 8192, 0);
820 if ((x
<= 0 && type
== IPC_TCP_SOCKET
) ||
821 (x
< 0 && type
== IPC_UDP_SOCKET
)) {
822 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes read from " << prog
<< ". Exiting...");
829 if (type
== IPC_UDP_SOCKET
&& !strcmp(buf2
, shutdown_string
)) {
830 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): request for shutdown received. Exiting...");
836 if ((buf2
[x
- 1] == '\n') && (buf2
[x
- 2] == '\r')) {
843 debugs(54, 5, "ipc(" << prog
<< "," << pid
<< "): received from child : " << rfc1738_escape_unescaped(buf2
));
845 x
= send(send_fd
, (const void *)buf2
, x
, 0);
847 if ((x
<= 0 && type
== IPC_TCP_SOCKET
) ||
848 (x
< 0 && type
== IPC_UDP_SOCKET
)) {
849 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes sent to parent. Exiting...");