2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 54 Windows Interprocess Communication */
14 #include "comm/Connection.h"
18 #include "ip/Address.h"
20 #include "SquidConfig.h"
22 #include "SquidTime.h"
35 Ip::Address local_addr
;
41 struct thread_params
{
49 static unsigned int __stdcall
ipc_thread_1(void *params
);
50 static unsigned int __stdcall
ipc_thread_2(void *params
);
52 static const char *ok_string
= "OK\n";
53 static const char *err_string
= "ERR\n";
54 static const char *shutdown_string
= "$shutdown\n";
56 static const char *hello_string
= "hi there\n";
57 #define HELLO_BUF_SZ 32
58 static char hello_buf
[HELLO_BUF_SZ
];
61 ipcCloseAllFD(int prfd
, int pwfd
, int crfd
, int cwfd
)
86 env_str
= (char *)xcalloc((tmp_s
= strlen(Debug::debugOptions
) + 32), 1);
87 snprintf(env_str
, tmp_s
, "SQUID_DEBUG=%s", Debug::debugOptions
);
93 ipcCreate(int type
, const char *prog
, const char *const args
[], const char *name
, Ip::Address
&local_addr
, int *rfd
, int *wfd
, void **hIpc
)
97 struct ipc_params params
;
99 int optlen
= sizeof(opt
);
103 Ip::Address tmp_addr
;
104 struct addrinfo
*aiCS
= NULL
;
105 struct addrinfo
*aiPS
= NULL
;
113 requirePathnameExists(name
, prog
);
124 if (WIN32_OS_version
!= _WIN_OS_WINNT
) {
125 getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, &optlen
);
126 opt
= opt
& ~(SO_SYNCHRONOUS_NONALERT
| SO_SYNCHRONOUS_ALERT
);
127 setsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, sizeof(opt
));
130 if (type
== IPC_TCP_SOCKET
) {
131 crfd
= cwfd
= comm_open(SOCK_STREAM
,
136 prfd
= pwfd
= comm_open(SOCK_STREAM
,
137 IPPROTO_TCP
, /* protocol */
141 } else if (type
== IPC_UDP_SOCKET
) {
142 crfd
= cwfd
= comm_open(SOCK_DGRAM
,
147 prfd
= pwfd
= comm_open(SOCK_DGRAM
,
152 } else if (type
== IPC_FIFO
) {
153 debugs(54, DBG_CRITICAL
, "ipcCreate: " << prog
<< ": use IPC_TCP_SOCKET instead of IP_FIFO on Windows");
159 debugs(54, 3, "ipcCreate: prfd FD " << prfd
);
160 debugs(54, 3, "ipcCreate: pwfd FD " << pwfd
);
161 debugs(54, 3, "ipcCreate: crfd FD " << crfd
);
162 debugs(54, 3, "ipcCreate: cwfd FD " << cwfd
);
164 if (WIN32_OS_version
!= _WIN_OS_WINNT
) {
165 getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, &optlen
);
166 opt
= opt
| SO_SYNCHRONOUS_NONALERT
;
167 setsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, optlen
);
171 debugs(54, DBG_CRITICAL
, "ipcCreate: Failed to create child FD.");
172 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
176 debugs(54, DBG_CRITICAL
, "ipcCreate: Failed to create server FD.");
177 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
180 // AYJ: these flags should be neutral, but if not IPv6 version needs adding
181 if (type
== IPC_TCP_SOCKET
|| type
== IPC_UDP_SOCKET
) {
183 Ip::Address::InitAddr(aiPS
);
185 if (getsockname(pwfd
, aiPS
->ai_addr
, &(aiPS
->ai_addrlen
) ) < 0) {
187 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerr(xerrno
));
188 Ip::Address::FreeAddr(aiPS
);
189 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
193 Ip::Address::FreeAddr(aiPS
);
195 debugs(54, 3, "ipcCreate: FD " << pwfd
<< " sockaddr " << tmp_addr
);
197 Ip::Address::InitAddr(aiCS
);
199 if (getsockname(crfd
, aiCS
->ai_addr
, &(aiCS
->ai_addrlen
) ) < 0) {
201 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerr(xerrno
));
202 Ip::Address::FreeAddr(aiCS
);
203 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
208 Ip::Address::FreeAddr(aiCS
);
210 debugs(54, 3, "ipcCreate: FD " << crfd
<< " sockaddr " << tmp_addr
);
213 if (type
== IPC_TCP_SOCKET
) {
214 if (listen(crfd
, 1) < 0) {
216 debugs(54, DBG_IMPORTANT
, "ipcCreate: listen FD " << crfd
<< ": " << xstrerr(xerrno
));
217 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
220 debugs(54, 3, "ipcCreate: FD " << crfd
<< " listening...");
223 /* flush or else we get dup data if unbuffered_logs is set */
234 params
.local_addr
= local_addr
;
238 params
.args
= (char **) args
;
240 thread
= _beginthreadex(NULL
, 0, ipc_thread_1
, ¶ms
, 0, NULL
);
244 debugs(54, DBG_IMPORTANT
, "ipcCreate: _beginthread: " << xstrerr(xerrno
));
245 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
248 /* NP: tmp_addr was left with eiether empty or aiCS in Ip::Address format */
249 if (comm_connect_addr(pwfd
, tmp_addr
) == Comm::COMM_ERROR
) {
250 CloseHandle((HANDLE
) thread
);
251 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
254 memset(hello_buf
, '\0', HELLO_BUF_SZ
);
255 x
= recv(prfd
, (void *)hello_buf
, HELLO_BUF_SZ
- 1, 0);
259 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: hello read test failed");
260 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerr(xerrno
));
261 CloseHandle((HANDLE
) thread
);
262 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
263 } else if (strcmp(hello_buf
, hello_string
)) {
264 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: hello read test failed");
265 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
266 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
267 CloseHandle((HANDLE
) thread
);
268 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
271 x
= send(pwfd
, (const void *)ok_string
, strlen(ok_string
), 0);
275 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: OK write test failed");
276 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerr(xerrno
));
277 CloseHandle((HANDLE
) thread
);
278 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
281 memset(hello_buf
, '\0', HELLO_BUF_SZ
);
282 x
= recv(prfd
, (void *)hello_buf
, HELLO_BUF_SZ
- 1, 0);
286 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: OK read test failed");
287 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerr(xerrno
));
288 CloseHandle((HANDLE
) thread
);
289 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
290 } else if (!strcmp(hello_buf
, err_string
)) {
291 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: OK read test failed");
292 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
293 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
294 CloseHandle((HANDLE
) thread
);
295 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
299 pid
= atol(hello_buf
);
300 commUnsetFdTimeout(prfd
);
301 commSetNonBlocking(prfd
);
302 commSetNonBlocking(pwfd
);
303 commSetCloseOnExec(prfd
);
304 commSetCloseOnExec(pwfd
);
312 fd_table
[prfd
].flags
.ipc
= true;
313 fd_table
[pwfd
].flags
.ipc
= true;
314 fd_table
[crfd
].flags
.ipc
= true;
315 fd_table
[cwfd
].flags
.ipc
= true;
317 if (Config
.sleep_after_fork
) {
318 /* XXX emulation of usleep() */
320 sl
= Config
.sleep_after_fork
/ 1000;
328 if (GetExitCodeThread((HANDLE
) thread
, &ecode
) && ecode
== STILL_ACTIVE
) {
330 *hIpc
= (HANDLE
) thread
;
334 CloseHandle((HANDLE
) thread
);
335 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
340 ipcSend(int cwfd
, const char *buf
, int len
)
342 int x
= send(cwfd
, (const void *)buf
, len
, 0);
346 debugs(54, DBG_CRITICAL
, "sendto FD " << cwfd
<< ": " << xstrerr(xerrno
));
347 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: hello write test failed");
353 static unsigned int __stdcall
354 ipc_thread_1(void *in_params
)
356 int t1
, t2
, t3
, retval
= -1;
357 int p2c
[2] = {-1, -1};
358 int c2p
[2] = {-1, -1};
359 HANDLE hProcess
= NULL
, thread
= NULL
;
362 struct thread_params thread_params
;
367 PROCESS_INFORMATION pi
;
369 int prfd_ipc
= -1, pwfd_ipc
= -1, crfd_ipc
= -1, cwfd_ipc
= -1;
370 char *prog
= NULL
, *buf1
= NULL
;
374 struct addrinfo
*aiPS_ipc
= NULL
;
375 struct addrinfo
*aiCS_ipc
= NULL
;
377 struct ipc_params
*params
= (struct ipc_params
*) in_params
;
378 int type
= params
->type
;
379 int crfd
= params
->crfd
;
380 int cwfd
= params
->cwfd
;
381 char **args
= params
->args
;
383 Ip::Address PS
= params
->PS
;
384 Ip::Address local_addr
= params
->local_addr
;
386 const size_t bufSz
= 8192;
387 buf1
= (char *)xcalloc(1, bufSz
);
388 strcpy(buf1
, params
->prog
);
389 prog
= strtok(buf1
, w_space
);
391 if ((str
= strrchr(prog
, '/')))
394 if ((str
= strrchr(prog
, '\\')))
397 prog
= xstrdup(prog
);
399 if (type
== IPC_TCP_SOCKET
) {
400 debugs(54, 3, "ipcCreate: calling accept on FD " << crfd
);
402 if ((fd
= accept(crfd
, NULL
, NULL
)) < 0) {
404 debugs(54, DBG_CRITICAL
, "ipcCreate: FD " << crfd
<< " accept: " << xstrerr(xerrno
));
408 debugs(54, 3, "ipcCreate: CHILD accepted new FD " << fd
);
410 snprintf(buf1
, bufSz
-1, "%s CHILD socket", prog
);
411 fd_open(fd
, FD_SOCKET
, buf1
);
412 fd_table
[fd
].flags
.ipc
= 1;
414 } else if (type
== IPC_UDP_SOCKET
) {
415 if (comm_connect_addr(crfd
, params
->PS
) == Comm::COMM_ERROR
)
419 x
= send(cwfd
, (const void *)hello_string
, strlen(hello_string
) + 1, 0);
423 debugs(54, DBG_CRITICAL
, "sendto FD " << cwfd
<< ": " << xstrerr(xerrno
));
424 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: hello write test failed");
429 memset(buf1
, '\0', bufSz
);
430 x
= recv(crfd
, (void *)buf1
, bufSz
-1, 0);
434 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: OK read test failed");
435 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerr(xerrno
));
437 } else if (strcmp(buf1
, ok_string
)) {
438 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: OK read test failed");
439 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
440 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
444 /* assign file descriptors to child process */
445 if (_pipe(p2c
, 1024, _O_BINARY
| _O_NOINHERIT
) < 0) {
447 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: pipe: " << xstrerr(xerrno
));
448 ipcSend(cwfd
, err_string
, strlen(err_string
));
452 if (_pipe(c2p
, 1024, _O_BINARY
| _O_NOINHERIT
) < 0) {
454 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: pipe: " << xstrerr(xerrno
));
455 ipcSend(cwfd
, err_string
, strlen(err_string
));
459 if (type
== IPC_UDP_SOCKET
) {
460 snprintf(buf1
, bufSz
, "%s(%ld) <-> ipc CHILD socket", prog
, -1L);
461 crfd_ipc
= cwfd_ipc
= comm_open(SOCK_DGRAM
, IPPROTO_UDP
, local_addr
, 0, buf1
);
464 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: Failed to create child FD for " << prog
<< ".");
465 ipcSend(cwfd
, err_string
, strlen(err_string
));
469 snprintf(buf1
, bufSz
, "%s(%ld) <-> ipc PARENT socket", prog
, -1L);
470 prfd_ipc
= pwfd_ipc
= comm_open(SOCK_DGRAM
, IPPROTO_UDP
, local_addr
, 0, buf1
);
473 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: Failed to create server FD for " << prog
<< ".");
474 ipcSend(cwfd
, err_string
, strlen(err_string
));
478 Ip::Address::InitAddr(aiPS_ipc
);
480 if (getsockname(pwfd_ipc
, aiPS_ipc
->ai_addr
, &(aiPS_ipc
->ai_addrlen
)) < 0) {
482 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerr(xerrno
));
483 ipcSend(cwfd
, err_string
, strlen(err_string
));
484 Ip::Address::FreeAddr(aiPS_ipc
);
489 Ip::Address::FreeAddr(aiPS_ipc
);
491 debugs(54, 3, "ipcCreate: FD " << pwfd_ipc
<< " sockaddr " << PS_ipc
);
493 Ip::Address::InitAddr(aiCS_ipc
);
495 if (getsockname(crfd_ipc
, aiCS_ipc
->ai_addr
, &(aiCS_ipc
->ai_addrlen
)) < 0) {
497 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerr(xerrno
));
498 ipcSend(cwfd
, err_string
, strlen(err_string
));
499 Ip::Address::FreeAddr(aiCS_ipc
);
504 Ip::Address::FreeAddr(aiCS_ipc
);
506 debugs(54, 3, "ipcCreate: FD " << crfd_ipc
<< " sockaddr " << CS_ipc
);
508 if (comm_connect_addr(pwfd_ipc
, CS_ipc
) == Comm::COMM_ERROR
) {
509 ipcSend(cwfd
, err_string
, strlen(err_string
));
515 if (comm_connect_addr(crfd_ipc
, PS_ipc
) == Comm::COMM_ERROR
) {
516 ipcSend(cwfd
, err_string
, strlen(err_string
));
519 } /* IPC_UDP_SOCKET */
531 dup2(fileno(debug_log
), 2);
537 commUnsetNonBlocking(fd
);
539 memset(&si
, 0, sizeof(STARTUPINFO
));
541 si
.cb
= sizeof(STARTUPINFO
);
543 si
.hStdInput
= (HANDLE
) _get_osfhandle(0);
545 si
.hStdOutput
= (HANDLE
) _get_osfhandle(1);
547 si
.hStdError
= (HANDLE
) _get_osfhandle(2);
549 si
.dwFlags
= STARTF_USESTDHANDLES
;
551 /* Make sure all other valid handles are not inerithable */
552 for (x
= 3; x
< Squid_MaxFD
; ++x
) {
553 if ((F
= _get_osfhandle(x
)) == -1)
556 SetHandleInformation((HANDLE
) F
, HANDLE_FLAG_INHERIT
, 0);
560 strcpy(buf1
+ 4096, params
->prog
);
561 str
= strtok(buf1
+ 4096, w_space
);
566 } while ((str
= strtok(NULL
, w_space
)));
571 strcat(buf1
, args
[x
]);
576 if (CreateProcess(buf1
+ 4096, buf1
, NULL
, NULL
, TRUE
, CREATE_NO_WINDOW
,
577 NULL
, NULL
, &si
, &pi
)) {
578 pid
= pi
.dwProcessId
;
579 hProcess
= pi
.hProcess
;
593 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << params
->prog
<< ": " << xstrerr(x
));
595 ipcSend(cwfd
, err_string
, strlen(err_string
));
599 if (type
== IPC_UDP_SOCKET
) {
600 WSAPROTOCOL_INFO wpi
;
602 memset(&wpi
, 0, sizeof(wpi
));
604 if (SOCKET_ERROR
== WSADuplicateSocket(crfd_ipc
, pid
, &wpi
)) {
606 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: WSADuplicateSocket: " << xstrerr(xerrno
));
607 ipcSend(cwfd
, err_string
, strlen(err_string
));
611 x
= write(c2p
[1], (const char *) &wpi
, sizeof(wpi
));
613 if (x
< (ssize_t
)sizeof(wpi
)) {
615 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: write FD " << c2p
[1] << ": " << xstrerr(xerrno
));
616 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
617 ipcSend(cwfd
, err_string
, strlen(err_string
));
621 x
= read(p2c
[0], buf1
, bufSz
-1);
625 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: read FD " << p2c
[0] << ": " << xstrerr(xerrno
));
626 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
627 ipcSend(cwfd
, err_string
, strlen(err_string
));
629 } else if (strncmp(buf1
, ok_string
, strlen(ok_string
))) {
630 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
631 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
633 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(buf1
) << "'");
634 ipcSend(cwfd
, err_string
, strlen(err_string
));
638 x
= write(c2p
[1], (const char *) &PS_ipc
, sizeof(PS_ipc
));
640 if (x
< (ssize_t
)sizeof(PS_ipc
)) {
642 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: write FD " << c2p
[1] << ": " << xstrerr(xerrno
));
643 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
644 ipcSend(cwfd
, err_string
, strlen(err_string
));
648 x
= read(p2c
[0], buf1
, bufSz
-1);
652 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: read FD " << p2c
[0] << ": " << xstrerr(xerrno
));
653 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
654 ipcSend(cwfd
, err_string
, strlen(err_string
));
656 } else if (strncmp(buf1
, ok_string
, strlen(ok_string
))) {
657 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
658 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
660 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(buf1
) << "'");
661 ipcSend(cwfd
, err_string
, strlen(err_string
));
665 x
= send(pwfd_ipc
, (const void *)ok_string
, strlen(ok_string
), 0);
666 x
= recv(prfd_ipc
, (void *)(buf1
+ 200), bufSz
-1 - 200, 0);
667 assert((size_t) x
== strlen(ok_string
)
668 && !strncmp(ok_string
, buf1
+ 200, strlen(ok_string
)));
669 } /* IPC_UDP_SOCKET */
671 snprintf(buf1
, bufSz
-1, "%s(%ld) CHILD socket", prog
, (long int) pid
);
675 if (prfd_ipc
!= -1) {
676 snprintf(buf1
, bufSz
-1, "%s(%ld) <-> ipc CHILD socket", prog
, (long int) pid
);
677 fd_note(crfd_ipc
, buf1
);
678 snprintf(buf1
, bufSz
-1, "%s(%ld) <-> ipc PARENT socket", prog
, (long int) pid
);
679 fd_note(prfd_ipc
, buf1
);
682 /* else { IPC_TCP_SOCKET */
683 /* commSetNoLinger(fd); */
685 thread_params
.prog
= prog
;
687 thread_params
.send_fd
= cwfd
;
689 thread_params
.pid
= pid
;
691 if ((thread_params
.type
= type
) == IPC_TCP_SOCKET
)
692 thread_params
.rfd
= p2c
[0];
694 thread_params
.rfd
= prfd_ipc
;
696 thread
= (HANDLE
)_beginthreadex(NULL
, 0, ipc_thread_2
, &thread_params
, 0, NULL
);
700 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: _beginthreadex: " << xstrerr(xerrno
));
701 ipcSend(cwfd
, err_string
, strlen(err_string
));
705 snprintf(buf1
, bufSz
-1, "%ld\n", (long int) pid
);
707 if (-1 == ipcSend(cwfd
, buf1
, strlen(buf1
)))
710 debugs(54, 2, "ipc(" << prog
<< "," << pid
<< "): started successfully");
714 x
= recv(crfd
, (void *)buf1
, bufSz
-1, 0);
717 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes received from parent. Exiting...");
723 if (type
== IPC_UDP_SOCKET
&& !strcmp(buf1
, shutdown_string
)) {
724 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): request for shutdown received from parent. Exiting...");
726 TerminateProcess(hProcess
, 0);
730 debugs(54, 5, "ipc(" << prog
<< "," << pid
<< "): received from parent: " << rfc1738_escape_unescaped(buf1
));
732 if (type
== IPC_TCP_SOCKET
)
733 x
= write(c2p
[1], buf1
, x
);
735 x
= send(pwfd_ipc
, (const void *)buf1
, x
, 0);
738 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes written to " << prog
<< ". Exiting...");
751 if (fd_table
[crfd
].flags
.open
)
752 ipcCloseAllFD(-1, -1, crfd
, cwfd
);
754 if (prfd_ipc
!= -1) {
755 send(crfd_ipc
, (const void *)shutdown_string
, strlen(shutdown_string
), 0);
756 shutdown(crfd_ipc
, SD_BOTH
);
757 shutdown(prfd_ipc
, SD_BOTH
);
760 ipcCloseAllFD(prfd_ipc
, pwfd_ipc
, crfd_ipc
, cwfd_ipc
);
762 if (hProcess
&& WAIT_OBJECT_0
!=
763 WaitForSingleObject(hProcess
, type
== IPC_UDP_SOCKET
? 12000 : 5000)) {
766 debugs(54, DBG_CRITICAL
, "ipc(" << prog
<< "," << pid
<< "): WARNING: " << prog
<<
767 " didn't exit in " << (type
== IPC_UDP_SOCKET
? 12 : 5) << " seconds.");
771 if (thread
&& WAIT_OBJECT_0
!= WaitForSingleObject(thread
, 3000)) {
773 debugs(54, DBG_CRITICAL
, "ipc(" << prog
<< "," << pid
<< "): WARNING: ipc_thread_2 didn't exit in 3 seconds.");
780 debugs(54, 2, "ipc(" << prog
<< "," << pid
<< "): normal exit");
789 CloseHandle(hProcess
);
797 static unsigned int __stdcall
798 ipc_thread_2(void *in_params
)
802 struct thread_params
*params
= (struct thread_params
*) in_params
;
803 int type
= params
->type
;
804 int rfd
= params
->rfd
;
805 int send_fd
= params
->send_fd
;
806 char *prog
= xstrdup(params
->prog
);
807 pid_t pid
= params
->pid
;
808 const size_t bufSz
= 8192;
809 char *buf2
= (char *)xcalloc(1, bufSz
);
812 if (type
== IPC_TCP_SOCKET
)
813 x
= read(rfd
, buf2
, bufSz
-1);
815 x
= recv(rfd
, (void *)buf2
, bufSz
-1, 0);
817 if ((x
<= 0 && type
== IPC_TCP_SOCKET
) ||
818 (x
< 0 && type
== IPC_UDP_SOCKET
)) {
819 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes read from " << prog
<< ". Exiting...");
826 if (type
== IPC_UDP_SOCKET
&& !strcmp(buf2
, shutdown_string
)) {
827 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): request for shutdown received. Exiting...");
832 if ((buf2
[x
- 1] == '\n') && (buf2
[x
- 2] == '\r')) {
839 debugs(54, 5, "ipc(" << prog
<< "," << pid
<< "): received from child : " << rfc1738_escape_unescaped(buf2
));
841 x
= send(send_fd
, (const void *)buf2
, x
, 0);
843 if ((x
<= 0 && type
== IPC_TCP_SOCKET
) ||
844 (x
< 0 && type
== IPC_UDP_SOCKET
)) {
845 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes sent to parent. Exiting...");