2 * Copyright (C) 1996-2022 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"
34 Ip::Address local_addr
;
40 struct thread_params
{
48 static unsigned int __stdcall
ipc_thread_1(void *params
);
49 static unsigned int __stdcall
ipc_thread_2(void *params
);
51 static const char *ok_string
= "OK\n";
52 static const char *err_string
= "ERR\n";
53 static const char *shutdown_string
= "$shutdown\n";
55 static const char *hello_string
= "hi there\n";
56 #define HELLO_BUF_SZ 32
57 static char hello_buf
[HELLO_BUF_SZ
];
60 ipcCloseAllFD(int prfd
, int pwfd
, int crfd
, int cwfd
)
85 env_str
= (char *)xcalloc((tmp_s
= strlen(Debug::debugOptions
) + 32), 1);
86 snprintf(env_str
, tmp_s
, "SQUID_DEBUG=%s", Debug::debugOptions
);
92 ipcCreate(int type
, const char *prog
, const char *const args
[], const char *name
, Ip::Address
&local_addr
, int *rfd
, int *wfd
, void **hIpc
)
96 struct ipc_params params
;
98 int optlen
= sizeof(opt
);
102 Ip::Address tmp_addr
;
103 struct addrinfo
*aiCS
= NULL
;
104 struct addrinfo
*aiPS
= NULL
;
112 requirePathnameExists(name
, prog
);
123 if (WIN32_OS_version
!= _WIN_OS_WINNT
) {
124 getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, &optlen
);
125 opt
= opt
& ~(SO_SYNCHRONOUS_NONALERT
| SO_SYNCHRONOUS_ALERT
);
126 setsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, sizeof(opt
));
129 if (type
== IPC_TCP_SOCKET
) {
130 crfd
= cwfd
= comm_open_listener(SOCK_STREAM
,
135 prfd
= pwfd
= comm_open(SOCK_STREAM
,
136 IPPROTO_TCP
, /* protocol */
140 } else if (type
== IPC_UDP_SOCKET
) {
141 crfd
= cwfd
= comm_open(SOCK_DGRAM
,
146 prfd
= pwfd
= comm_open(SOCK_DGRAM
,
151 } else if (type
== IPC_FIFO
) {
152 debugs(54, DBG_CRITICAL
, "ipcCreate: " << prog
<< ": use IPC_TCP_SOCKET instead of IP_FIFO on Windows");
158 debugs(54, 3, "ipcCreate: prfd FD " << prfd
);
159 debugs(54, 3, "ipcCreate: pwfd FD " << pwfd
);
160 debugs(54, 3, "ipcCreate: crfd FD " << crfd
);
161 debugs(54, 3, "ipcCreate: cwfd FD " << cwfd
);
163 if (WIN32_OS_version
!= _WIN_OS_WINNT
) {
164 getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, &optlen
);
165 opt
= opt
| SO_SYNCHRONOUS_NONALERT
;
166 setsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, optlen
);
170 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: Failed to create child FD.");
171 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
175 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: Failed to create server FD.");
176 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
179 // AYJ: these flags should be neutral, but if not IPv6 version needs adding
180 if (type
== IPC_TCP_SOCKET
|| type
== IPC_UDP_SOCKET
) {
182 Ip::Address::InitAddr(aiPS
);
184 if (getsockname(pwfd
, aiPS
->ai_addr
, &(aiPS
->ai_addrlen
) ) < 0) {
186 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerr(xerrno
));
187 Ip::Address::FreeAddr(aiPS
);
188 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
192 Ip::Address::FreeAddr(aiPS
);
194 debugs(54, 3, "ipcCreate: FD " << pwfd
<< " sockaddr " << tmp_addr
);
196 Ip::Address::InitAddr(aiCS
);
198 if (getsockname(crfd
, aiCS
->ai_addr
, &(aiCS
->ai_addrlen
) ) < 0) {
200 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerr(xerrno
));
201 Ip::Address::FreeAddr(aiCS
);
202 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
207 Ip::Address::FreeAddr(aiCS
);
209 debugs(54, 3, "ipcCreate: FD " << crfd
<< " sockaddr " << tmp_addr
);
212 if (type
== IPC_TCP_SOCKET
) {
213 if (listen(crfd
, 1) < 0) {
215 debugs(54, DBG_IMPORTANT
, "ipcCreate: listen FD " << crfd
<< ": " << xstrerr(xerrno
));
216 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
219 debugs(54, 3, "ipcCreate: FD " << crfd
<< " listening...");
222 /* flush or else we get dup data if unbuffered_logs is set */
233 params
.local_addr
= local_addr
;
237 params
.args
= (char **) args
;
239 thread
= _beginthreadex(NULL
, 0, ipc_thread_1
, ¶ms
, 0, NULL
);
243 debugs(54, DBG_IMPORTANT
, "ipcCreate: _beginthread: " << xstrerr(xerrno
));
244 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
247 /* NP: tmp_addr was left with eiether empty or aiCS in Ip::Address format */
248 if (comm_connect_addr(pwfd
, tmp_addr
) == Comm::COMM_ERROR
) {
249 CloseHandle((HANDLE
) thread
);
250 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
253 memset(hello_buf
, '\0', HELLO_BUF_SZ
);
254 x
= recv(prfd
, (void *)hello_buf
, HELLO_BUF_SZ
- 1, 0);
258 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: PARENT: hello read test failed");
259 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerr(xerrno
));
260 CloseHandle((HANDLE
) thread
);
261 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
262 } else if (strcmp(hello_buf
, hello_string
)) {
263 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: PARENT: hello read test failed");
264 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
265 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
266 CloseHandle((HANDLE
) thread
);
267 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
270 x
= send(pwfd
, (const void *)ok_string
, strlen(ok_string
), 0);
274 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: PARENT: OK write test failed");
275 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerr(xerrno
));
276 CloseHandle((HANDLE
) thread
);
277 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
280 memset(hello_buf
, '\0', HELLO_BUF_SZ
);
281 x
= recv(prfd
, (void *)hello_buf
, HELLO_BUF_SZ
- 1, 0);
285 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: PARENT: OK read test failed");
286 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerr(xerrno
));
287 CloseHandle((HANDLE
) thread
);
288 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
289 } else if (!strcmp(hello_buf
, err_string
)) {
290 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: PARENT: OK read test failed");
291 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
292 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
293 CloseHandle((HANDLE
) thread
);
294 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
298 pid
= atol(hello_buf
);
299 commUnsetFdTimeout(prfd
);
300 commSetNonBlocking(prfd
);
301 commSetNonBlocking(pwfd
);
302 commSetCloseOnExec(prfd
);
303 commSetCloseOnExec(pwfd
);
311 fd_table
[prfd
].flags
.ipc
= true;
312 fd_table
[pwfd
].flags
.ipc
= true;
313 fd_table
[crfd
].flags
.ipc
= true;
314 fd_table
[cwfd
].flags
.ipc
= true;
316 if (Config
.sleep_after_fork
) {
317 /* XXX emulation of usleep() */
319 sl
= Config
.sleep_after_fork
/ 1000;
327 if (GetExitCodeThread((HANDLE
) thread
, &ecode
) && ecode
== STILL_ACTIVE
) {
329 *hIpc
= (HANDLE
) thread
;
333 CloseHandle((HANDLE
) thread
);
334 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
339 ipcSend(int cwfd
, const char *buf
, int len
)
341 int x
= send(cwfd
, (const void *)buf
, len
, 0);
345 debugs(54, DBG_CRITICAL
, "sendto FD " << cwfd
<< ": " << xstrerr(xerrno
));
346 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: CHILD: hello write test failed");
352 static unsigned int __stdcall
353 ipc_thread_1(void *in_params
)
355 int t1
, t2
, t3
, retval
= -1;
356 int p2c
[2] = {-1, -1};
357 int c2p
[2] = {-1, -1};
358 HANDLE hProcess
= NULL
, thread
= NULL
;
361 struct thread_params thread_params
;
366 PROCESS_INFORMATION pi
;
368 int prfd_ipc
= -1, pwfd_ipc
= -1, crfd_ipc
= -1, cwfd_ipc
= -1;
369 char *prog
= NULL
, *buf1
= NULL
;
373 struct addrinfo
*aiPS_ipc
= NULL
;
374 struct addrinfo
*aiCS_ipc
= NULL
;
376 struct ipc_params
*params
= (struct ipc_params
*) in_params
;
377 int type
= params
->type
;
378 int crfd
= params
->crfd
;
379 int cwfd
= params
->cwfd
;
380 char **args
= params
->args
;
382 Ip::Address PS
= params
->PS
;
383 Ip::Address local_addr
= params
->local_addr
;
385 const size_t bufSz
= 8192;
386 buf1
= (char *)xcalloc(1, bufSz
);
387 strcpy(buf1
, params
->prog
);
388 prog
= strtok(buf1
, w_space
);
390 if ((str
= strrchr(prog
, '/')))
393 if ((str
= strrchr(prog
, '\\')))
396 prog
= xstrdup(prog
);
398 if (type
== IPC_TCP_SOCKET
) {
399 debugs(54, 3, "ipcCreate: calling accept on FD " << crfd
);
401 if ((fd
= accept(crfd
, NULL
, NULL
)) < 0) {
403 debugs(54, DBG_CRITICAL
, "ipcCreate: FD " << crfd
<< " accept: " << xstrerr(xerrno
));
407 debugs(54, 3, "ipcCreate: CHILD accepted new FD " << fd
);
409 snprintf(buf1
, bufSz
-1, "%s CHILD socket", prog
);
410 fd_open(fd
, FD_SOCKET
, buf1
);
411 fd_table
[fd
].flags
.ipc
= 1;
413 } else if (type
== IPC_UDP_SOCKET
) {
414 if (comm_connect_addr(crfd
, params
->PS
) == Comm::COMM_ERROR
)
418 x
= send(cwfd
, (const void *)hello_string
, strlen(hello_string
) + 1, 0);
422 debugs(54, DBG_CRITICAL
, "sendto FD " << cwfd
<< ": " << xstrerr(xerrno
));
423 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: CHILD: hello write test failed");
428 memset(buf1
, '\0', bufSz
);
429 x
= recv(crfd
, (void *)buf1
, bufSz
-1, 0);
433 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: CHILD: OK read test failed");
434 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerr(xerrno
));
436 } else if (strcmp(buf1
, ok_string
)) {
437 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: CHILD: OK read test failed");
438 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
439 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
443 /* assign file descriptors to child process */
444 if (_pipe(p2c
, 1024, _O_BINARY
| _O_NOINHERIT
) < 0) {
446 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: pipe: " << xstrerr(xerrno
));
447 ipcSend(cwfd
, err_string
, strlen(err_string
));
451 if (_pipe(c2p
, 1024, _O_BINARY
| _O_NOINHERIT
) < 0) {
453 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: pipe: " << xstrerr(xerrno
));
454 ipcSend(cwfd
, err_string
, strlen(err_string
));
458 if (type
== IPC_UDP_SOCKET
) {
459 snprintf(buf1
, bufSz
, "%s(%ld) <-> ipc CHILD socket", prog
, -1L);
460 crfd_ipc
= cwfd_ipc
= comm_open(SOCK_DGRAM
, IPPROTO_UDP
, local_addr
, 0, buf1
);
463 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: CHILD: Failed to create child FD for " << prog
<< ".");
464 ipcSend(cwfd
, err_string
, strlen(err_string
));
468 snprintf(buf1
, bufSz
, "%s(%ld) <-> ipc PARENT socket", prog
, -1L);
469 prfd_ipc
= pwfd_ipc
= comm_open(SOCK_DGRAM
, IPPROTO_UDP
, local_addr
, 0, buf1
);
472 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: CHILD: Failed to create server FD for " << prog
<< ".");
473 ipcSend(cwfd
, err_string
, strlen(err_string
));
477 Ip::Address::InitAddr(aiPS_ipc
);
479 if (getsockname(pwfd_ipc
, aiPS_ipc
->ai_addr
, &(aiPS_ipc
->ai_addrlen
)) < 0) {
481 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerr(xerrno
));
482 ipcSend(cwfd
, err_string
, strlen(err_string
));
483 Ip::Address::FreeAddr(aiPS_ipc
);
488 Ip::Address::FreeAddr(aiPS_ipc
);
490 debugs(54, 3, "ipcCreate: FD " << pwfd_ipc
<< " sockaddr " << PS_ipc
);
492 Ip::Address::InitAddr(aiCS_ipc
);
494 if (getsockname(crfd_ipc
, aiCS_ipc
->ai_addr
, &(aiCS_ipc
->ai_addrlen
)) < 0) {
496 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerr(xerrno
));
497 ipcSend(cwfd
, err_string
, strlen(err_string
));
498 Ip::Address::FreeAddr(aiCS_ipc
);
503 Ip::Address::FreeAddr(aiCS_ipc
);
505 debugs(54, 3, "ipcCreate: FD " << crfd_ipc
<< " sockaddr " << CS_ipc
);
507 if (comm_connect_addr(pwfd_ipc
, CS_ipc
) == Comm::COMM_ERROR
) {
508 ipcSend(cwfd
, err_string
, strlen(err_string
));
514 if (comm_connect_addr(crfd_ipc
, PS_ipc
) == Comm::COMM_ERROR
) {
515 ipcSend(cwfd
, err_string
, strlen(err_string
));
518 } /* IPC_UDP_SOCKET */
530 dup2(fileno(debug_log
), 2);
536 commUnsetNonBlocking(fd
);
538 memset(&si
, 0, sizeof(STARTUPINFO
));
540 si
.cb
= sizeof(STARTUPINFO
);
542 si
.hStdInput
= (HANDLE
) _get_osfhandle(0);
544 si
.hStdOutput
= (HANDLE
) _get_osfhandle(1);
546 si
.hStdError
= (HANDLE
) _get_osfhandle(2);
548 si
.dwFlags
= STARTF_USESTDHANDLES
;
550 /* Make sure all other valid handles are not inerithable */
551 for (x
= 3; x
< Squid_MaxFD
; ++x
) {
552 if ((F
= _get_osfhandle(x
)) == -1)
555 SetHandleInformation((HANDLE
) F
, HANDLE_FLAG_INHERIT
, 0);
559 strcpy(buf1
+ 4096, params
->prog
);
560 str
= strtok(buf1
+ 4096, w_space
);
565 } while ((str
= strtok(NULL
, w_space
)));
570 strcat(buf1
, args
[x
]);
575 if (CreateProcess(buf1
+ 4096, buf1
, NULL
, NULL
, TRUE
, CREATE_NO_WINDOW
,
576 NULL
, NULL
, &si
, &pi
)) {
577 pid
= pi
.dwProcessId
;
578 hProcess
= pi
.hProcess
;
592 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << params
->prog
<< ": " << xstrerr(x
));
594 ipcSend(cwfd
, err_string
, strlen(err_string
));
598 if (type
== IPC_UDP_SOCKET
) {
599 WSAPROTOCOL_INFO wpi
;
601 memset(&wpi
, 0, sizeof(wpi
));
603 if (SOCKET_ERROR
== WSADuplicateSocket(crfd_ipc
, pid
, &wpi
)) {
605 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: WSADuplicateSocket: " << xstrerr(xerrno
));
606 ipcSend(cwfd
, err_string
, strlen(err_string
));
610 x
= write(c2p
[1], (const char *) &wpi
, sizeof(wpi
));
612 if (x
< (ssize_t
)sizeof(wpi
)) {
614 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: write FD " << c2p
[1] << ": " << xstrerr(xerrno
));
615 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
616 ipcSend(cwfd
, err_string
, strlen(err_string
));
620 x
= read(p2c
[0], buf1
, bufSz
-1);
624 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: read FD " << p2c
[0] << ": " << xstrerr(xerrno
));
625 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
626 ipcSend(cwfd
, err_string
, strlen(err_string
));
628 } else if (strncmp(buf1
, ok_string
, strlen(ok_string
))) {
629 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
630 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
632 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(buf1
) << "'");
633 ipcSend(cwfd
, err_string
, strlen(err_string
));
637 x
= write(c2p
[1], (const char *) &PS_ipc
, sizeof(PS_ipc
));
639 if (x
< (ssize_t
)sizeof(PS_ipc
)) {
641 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: write FD " << c2p
[1] << ": " << xstrerr(xerrno
));
642 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
643 ipcSend(cwfd
, err_string
, strlen(err_string
));
647 x
= read(p2c
[0], buf1
, bufSz
-1);
651 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: read FD " << p2c
[0] << ": " << xstrerr(xerrno
));
652 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
653 ipcSend(cwfd
, err_string
, strlen(err_string
));
655 } else if (strncmp(buf1
, ok_string
, strlen(ok_string
))) {
656 debugs(54, DBG_CRITICAL
, "ERROR: ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
657 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
659 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(buf1
) << "'");
660 ipcSend(cwfd
, err_string
, strlen(err_string
));
664 x
= send(pwfd_ipc
, (const void *)ok_string
, strlen(ok_string
), 0);
665 x
= recv(prfd_ipc
, (void *)(buf1
+ 200), bufSz
-1 - 200, 0);
666 assert((size_t) x
== strlen(ok_string
)
667 && !strncmp(ok_string
, buf1
+ 200, strlen(ok_string
)));
668 } /* IPC_UDP_SOCKET */
670 snprintf(buf1
, bufSz
-1, "%s(%ld) CHILD socket", prog
, (long int) pid
);
674 if (prfd_ipc
!= -1) {
675 snprintf(buf1
, bufSz
-1, "%s(%ld) <-> ipc CHILD socket", prog
, (long int) pid
);
676 fd_note(crfd_ipc
, buf1
);
677 snprintf(buf1
, bufSz
-1, "%s(%ld) <-> ipc PARENT socket", prog
, (long int) pid
);
678 fd_note(prfd_ipc
, buf1
);
681 /* else { IPC_TCP_SOCKET */
682 /* commSetNoLinger(fd); */
684 thread_params
.prog
= prog
;
686 thread_params
.send_fd
= cwfd
;
688 thread_params
.pid
= pid
;
690 if ((thread_params
.type
= type
) == IPC_TCP_SOCKET
)
691 thread_params
.rfd
= p2c
[0];
693 thread_params
.rfd
= prfd_ipc
;
695 thread
= (HANDLE
)_beginthreadex(NULL
, 0, ipc_thread_2
, &thread_params
, 0, NULL
);
699 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: _beginthreadex: " << xstrerr(xerrno
));
700 ipcSend(cwfd
, err_string
, strlen(err_string
));
704 snprintf(buf1
, bufSz
-1, "%ld\n", (long int) pid
);
706 if (-1 == ipcSend(cwfd
, buf1
, strlen(buf1
)))
709 debugs(54, 2, "ipc(" << prog
<< "," << pid
<< "): started successfully");
713 x
= recv(crfd
, (void *)buf1
, bufSz
-1, 0);
716 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes received from parent. Exiting...");
722 if (type
== IPC_UDP_SOCKET
&& !strcmp(buf1
, shutdown_string
)) {
723 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): request for shutdown received from parent. Exiting...");
725 TerminateProcess(hProcess
, 0);
729 debugs(54, 5, "ipc(" << prog
<< "," << pid
<< "): received from parent: " << rfc1738_escape_unescaped(buf1
));
731 if (type
== IPC_TCP_SOCKET
)
732 x
= write(c2p
[1], buf1
, x
);
734 x
= send(pwfd_ipc
, (const void *)buf1
, x
, 0);
737 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes written to " << prog
<< ". Exiting...");
750 if (fd_table
[crfd
].flags
.open
)
751 ipcCloseAllFD(-1, -1, crfd
, cwfd
);
753 if (prfd_ipc
!= -1) {
754 send(crfd_ipc
, (const void *)shutdown_string
, strlen(shutdown_string
), 0);
755 shutdown(crfd_ipc
, SD_BOTH
);
756 shutdown(prfd_ipc
, SD_BOTH
);
759 ipcCloseAllFD(prfd_ipc
, pwfd_ipc
, crfd_ipc
, cwfd_ipc
);
761 if (hProcess
&& WAIT_OBJECT_0
!=
762 WaitForSingleObject(hProcess
, type
== IPC_UDP_SOCKET
? 12000 : 5000)) {
765 debugs(54, DBG_CRITICAL
, "ipc(" << prog
<< "," << pid
<< "): WARNING: " << prog
<<
766 " didn't exit in " << (type
== IPC_UDP_SOCKET
? 12 : 5) << " seconds.");
770 if (thread
&& WAIT_OBJECT_0
!= WaitForSingleObject(thread
, 3000)) {
772 debugs(54, DBG_CRITICAL
, "ipc(" << prog
<< "," << pid
<< "): WARNING: ipc_thread_2 didn't exit in 3 seconds.");
779 debugs(54, 2, "ipc(" << prog
<< "," << pid
<< "): normal exit");
788 CloseHandle(hProcess
);
796 static unsigned int __stdcall
797 ipc_thread_2(void *in_params
)
801 struct thread_params
*params
= (struct thread_params
*) in_params
;
802 int type
= params
->type
;
803 int rfd
= params
->rfd
;
804 int send_fd
= params
->send_fd
;
805 char *prog
= xstrdup(params
->prog
);
806 pid_t pid
= params
->pid
;
807 const size_t bufSz
= 8192;
808 char *buf2
= (char *)xcalloc(1, bufSz
);
811 if (type
== IPC_TCP_SOCKET
)
812 x
= read(rfd
, buf2
, bufSz
-1);
814 x
= recv(rfd
, (void *)buf2
, bufSz
-1, 0);
816 if ((x
<= 0 && type
== IPC_TCP_SOCKET
) ||
817 (x
< 0 && type
== IPC_UDP_SOCKET
)) {
818 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes read from " << prog
<< ". Exiting...");
825 if (type
== IPC_UDP_SOCKET
&& !strcmp(buf2
, shutdown_string
)) {
826 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): request for shutdown received. Exiting...");
831 if ((buf2
[x
- 1] == '\n') && (buf2
[x
- 2] == '\r')) {
838 debugs(54, 5, "ipc(" << prog
<< "," << pid
<< "): received from child : " << rfc1738_escape_unescaped(buf2
));
840 x
= send(send_fd
, (const void *)buf2
, x
, 0);
842 if ((x
<= 0 && type
== IPC_TCP_SOCKET
) ||
843 (x
< 0 && type
== IPC_UDP_SOCKET
)) {
844 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes sent to parent. Exiting...");