2 * DEBUG: section 54 Windows Interprocess Communication
3 * AUTHOR: Andrey Shorin <tolsty@tushino.com>
4 * AUTHOR: Guido Serassio <serassio@squid-cache.org>
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
39 #include "ip/Address.h"
41 #include "SquidConfig.h"
43 #include "SquidTime.h"
58 Ip::Address local_addr
;
64 struct thread_params
{
72 static unsigned int __stdcall
ipc_thread_1(void *params
);
73 static unsigned int __stdcall
ipc_thread_2(void *params
);
75 static const char *ok_string
= "OK\n";
76 static const char *err_string
= "ERR\n";
77 static const char *shutdown_string
= "$shutdown\n";
79 static const char *hello_string
= "hi there\n";
80 #define HELLO_BUF_SZ 32
81 static char hello_buf
[HELLO_BUF_SZ
];
84 ipcCloseAllFD(int prfd
, int pwfd
, int crfd
, int cwfd
)
109 env_str
= (char *)xcalloc((tmp_s
= strlen(Debug::debugOptions
) + 32), 1);
110 snprintf(env_str
, tmp_s
, "SQUID_DEBUG=%s", Debug::debugOptions
);
116 ipcCreate(int type
, const char *prog
, const char *const args
[], const char *name
, Ip::Address
&local_addr
, int *rfd
, int *wfd
, void **hIpc
)
118 unsigned long thread
;
120 struct ipc_params params
;
122 int optlen
= sizeof(opt
);
126 Ip::Address tmp_addr
;
127 struct addrinfo
*aiCS
= NULL
;
128 struct addrinfo
*aiPS
= NULL
;
136 requirePathnameExists(name
, prog
);
147 if (WIN32_OS_version
!= _WIN_OS_WINNT
) {
148 getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, &optlen
);
149 opt
= opt
& ~(SO_SYNCHRONOUS_NONALERT
| SO_SYNCHRONOUS_ALERT
);
150 setsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, sizeof(opt
));
153 if (type
== IPC_TCP_SOCKET
) {
154 crfd
= cwfd
= comm_open(SOCK_STREAM
,
159 prfd
= pwfd
= comm_open(SOCK_STREAM
,
160 IPPROTO_TCP
, /* protocol */
164 } else if (type
== IPC_UDP_SOCKET
) {
165 crfd
= cwfd
= comm_open(SOCK_DGRAM
,
170 prfd
= pwfd
= comm_open(SOCK_DGRAM
,
175 } else if (type
== IPC_FIFO
) {
176 debugs(54, DBG_CRITICAL
, "ipcCreate: " << prog
<< ": use IPC_TCP_SOCKET instead of IP_FIFO on Windows");
182 debugs(54, 3, "ipcCreate: prfd FD " << prfd
);
183 debugs(54, 3, "ipcCreate: pwfd FD " << pwfd
);
184 debugs(54, 3, "ipcCreate: crfd FD " << crfd
);
185 debugs(54, 3, "ipcCreate: cwfd FD " << cwfd
);
187 if (WIN32_OS_version
!= _WIN_OS_WINNT
) {
188 getsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, &optlen
);
189 opt
= opt
| SO_SYNCHRONOUS_NONALERT
;
190 setsockopt(INVALID_SOCKET
, SOL_SOCKET
, SO_OPENTYPE
, (char *) &opt
, optlen
);
194 debugs(54, DBG_CRITICAL
, "ipcCreate: Failed to create child FD.");
195 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
199 debugs(54, DBG_CRITICAL
, "ipcCreate: Failed to create server FD.");
200 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
203 // AYJ: these flags should be neutral, but if not IPv6 version needs adding
204 if (type
== IPC_TCP_SOCKET
|| type
== IPC_UDP_SOCKET
) {
206 Ip::Address::InitAddrInfo(aiPS
);
208 if (getsockname(pwfd
, aiPS
->ai_addr
, &(aiPS
->ai_addrlen
) ) < 0) {
209 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
210 Ip::Address::FreeAddrInfo(aiPS
);
211 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
215 Ip::Address::FreeAddrInfo(aiPS
);
217 debugs(54, 3, "ipcCreate: FD " << pwfd
<< " sockaddr " << tmp_addr
);
219 Ip::Address::InitAddrInfo(aiCS
);
221 if (getsockname(crfd
, aiCS
->ai_addr
, &(aiCS
->ai_addrlen
) ) < 0) {
222 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
223 Ip::Address::FreeAddrInfo(aiCS
);
224 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
229 Ip::Address::FreeAddrInfo(aiCS
);
231 debugs(54, 3, "ipcCreate: FD " << crfd
<< " sockaddr " << tmp_addr
);
234 if (type
== IPC_TCP_SOCKET
) {
235 if (listen(crfd
, 1) < 0) {
236 debugs(54, DBG_IMPORTANT
, "ipcCreate: listen FD " << crfd
<< ": " << xstrerror());
237 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
240 debugs(54, 3, "ipcCreate: FD " << crfd
<< " listening...");
243 /* flush or else we get dup data if unbuffered_logs is set */
254 params
.local_addr
= local_addr
;
258 params
.args
= (char **) args
;
260 thread
= _beginthreadex(NULL
, 0, ipc_thread_1
, ¶ms
, 0, NULL
);
263 debugs(54, DBG_IMPORTANT
, "ipcCreate: _beginthread: " << xstrerror());
264 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
267 /* NP: tmp_addr was left with eiether empty or aiCS in Ip::Address format */
268 if (comm_connect_addr(pwfd
, tmp_addr
) == COMM_ERROR
) {
269 CloseHandle((HANDLE
) thread
);
270 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
273 memset(hello_buf
, '\0', HELLO_BUF_SZ
);
274 x
= recv(prfd
, (void *)hello_buf
, HELLO_BUF_SZ
- 1, 0);
277 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: hello read test failed");
278 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
279 CloseHandle((HANDLE
) thread
);
280 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
281 } else if (strcmp(hello_buf
, hello_string
)) {
282 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: hello read test failed");
283 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
284 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
285 CloseHandle((HANDLE
) thread
);
286 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
289 x
= send(pwfd
, (const void *)ok_string
, strlen(ok_string
), 0);
292 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: OK write test failed");
293 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
294 CloseHandle((HANDLE
) thread
);
295 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
298 memset(hello_buf
, '\0', HELLO_BUF_SZ
);
299 x
= recv(prfd
, (void *)hello_buf
, HELLO_BUF_SZ
- 1, 0);
302 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: OK read test failed");
303 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
304 CloseHandle((HANDLE
) thread
);
305 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
306 } else if (!strcmp(hello_buf
, err_string
)) {
307 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: OK read test failed");
308 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
309 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
310 CloseHandle((HANDLE
) thread
);
311 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
315 pid
= atol(hello_buf
);
316 commUnsetFdTimeout(prfd
);
317 commSetNonBlocking(prfd
);
318 commSetNonBlocking(pwfd
);
319 commSetCloseOnExec(prfd
);
320 commSetCloseOnExec(pwfd
);
328 fd_table
[prfd
].flags
.ipc
= true;
329 fd_table
[pwfd
].flags
.ipc
= true;
330 fd_table
[crfd
].flags
.ipc
= true;
331 fd_table
[cwfd
].flags
.ipc
= true;
333 if (Config
.sleep_after_fork
) {
334 /* XXX emulation of usleep() */
336 sl
= Config
.sleep_after_fork
/ 1000;
344 if (GetExitCodeThread((HANDLE
) thread
, &ecode
) && ecode
== STILL_ACTIVE
) {
346 *hIpc
= (HANDLE
) thread
;
350 CloseHandle((HANDLE
) thread
);
351 return ipcCloseAllFD(prfd
, pwfd
, -1, -1);
356 ipcSend(int cwfd
, const char *buf
, int len
)
360 x
= send(cwfd
, (const void *)buf
, len
, 0);
363 debugs(54, DBG_CRITICAL
, "sendto FD " << cwfd
<< ": " << xstrerror());
364 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: hello write test failed");
370 static unsigned int __stdcall
371 ipc_thread_1(void *in_params
)
373 int t1
, t2
, t3
, retval
= -1;
374 int p2c
[2] = {-1, -1};
375 int c2p
[2] = {-1, -1};
376 HANDLE hProcess
= NULL
, thread
= NULL
;
379 struct thread_params thread_params
;
384 PROCESS_INFORMATION pi
;
386 int prfd_ipc
= -1, pwfd_ipc
= -1, crfd_ipc
= -1, cwfd_ipc
= -1;
387 char *prog
= NULL
, *buf1
= NULL
;
391 struct addrinfo
*aiPS_ipc
= NULL
;
392 struct addrinfo
*aiCS_ipc
= NULL
;
394 struct ipc_params
*params
= (struct ipc_params
*) in_params
;
395 int type
= params
->type
;
396 int crfd
= params
->crfd
;
397 int cwfd
= params
->cwfd
;
398 char **args
= params
->args
;
400 Ip::Address PS
= params
->PS
;
401 Ip::Address local_addr
= params
->local_addr
;
403 buf1
= (char *)xcalloc(1, 8192);
404 strcpy(buf1
, params
->prog
);
405 prog
= strtok(buf1
, w_space
);
407 if ((str
= strrchr(prog
, '/')))
410 if ((str
= strrchr(prog
, '\\')))
413 prog
= xstrdup(prog
);
415 if (type
== IPC_TCP_SOCKET
) {
416 debugs(54, 3, "ipcCreate: calling accept on FD " << crfd
);
418 if ((fd
= accept(crfd
, NULL
, NULL
)) < 0) {
419 debugs(54, DBG_CRITICAL
, "ipcCreate: FD " << crfd
<< " accept: " << xstrerror());
423 debugs(54, 3, "ipcCreate: CHILD accepted new FD " << fd
);
425 snprintf(buf1
, 8191, "%s CHILD socket", prog
);
426 fd_open(fd
, FD_SOCKET
, buf1
);
427 fd_table
[fd
].flags
.ipc
= 1;
429 } else if (type
== IPC_UDP_SOCKET
) {
430 if (comm_connect_addr(crfd
, params
->PS
) == COMM_ERROR
)
434 x
= send(cwfd
, (const void *)hello_string
, strlen(hello_string
) + 1, 0);
437 debugs(54, DBG_CRITICAL
, "sendto FD " << cwfd
<< ": " << xstrerror());
438 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: hello write test failed");
443 memset(buf1
, '\0', sizeof(buf1
));
444 x
= recv(crfd
, (void *)buf1
, 8191, 0);
447 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: OK read test failed");
448 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
450 } else if (strcmp(buf1
, ok_string
)) {
451 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: OK read test failed");
452 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
453 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
457 /* assign file descriptors to child process */
458 if (_pipe(p2c
, 1024, _O_BINARY
| _O_NOINHERIT
) < 0) {
459 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: pipe: " << xstrerror());
460 ipcSend(cwfd
, err_string
, strlen(err_string
));
464 if (_pipe(c2p
, 1024, _O_BINARY
| _O_NOINHERIT
) < 0) {
465 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: pipe: " << xstrerror());
466 ipcSend(cwfd
, err_string
, strlen(err_string
));
470 if (type
== IPC_UDP_SOCKET
) {
471 snprintf(buf1
, 8192, "%s(%ld) <-> ipc CHILD socket", prog
, -1L);
472 crfd_ipc
= cwfd_ipc
= comm_open(SOCK_DGRAM
, IPPROTO_UDP
, local_addr
, 0, buf1
);
475 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: Failed to create child FD for " << prog
<< ".");
476 ipcSend(cwfd
, err_string
, strlen(err_string
));
480 snprintf(buf1
, 8192, "%s(%ld) <-> ipc PARENT socket", prog
, -1L);
481 prfd_ipc
= pwfd_ipc
= comm_open(SOCK_DGRAM
, IPPROTO_UDP
, local_addr
, 0, buf1
);
484 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: Failed to create server FD for " << prog
<< ".");
485 ipcSend(cwfd
, err_string
, strlen(err_string
));
489 Ip::Address::InitAddrInfo(aiPS_ipc
);
491 if (getsockname(pwfd_ipc
, aiPS_ipc
->ai_addr
, &(aiPS_ipc
->ai_addrlen
)) < 0) {
492 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
493 ipcSend(cwfd
, err_string
, strlen(err_string
));
494 Ip::Address::FreeAddrInfo(aiPS_ipc
);
499 Ip::Address::FreeAddrInfo(aiPS_ipc
);
501 debugs(54, 3, "ipcCreate: FD " << pwfd_ipc
<< " sockaddr " << PS_ipc
);
503 Ip::Address::InitAddrInfo(aiCS_ipc
);
505 if (getsockname(crfd_ipc
, aiCS_ipc
->ai_addr
, &(aiCS_ipc
->ai_addrlen
)) < 0) {
506 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
507 ipcSend(cwfd
, err_string
, strlen(err_string
));
508 Ip::Address::FreeAddrInfo(aiCS_ipc
);
513 Ip::Address::FreeAddrInfo(aiCS_ipc
);
515 debugs(54, 3, "ipcCreate: FD " << crfd_ipc
<< " sockaddr " << CS_ipc
);
517 if (comm_connect_addr(pwfd_ipc
, CS_ipc
) == COMM_ERROR
) {
518 ipcSend(cwfd
, err_string
, strlen(err_string
));
524 if (comm_connect_addr(crfd_ipc
, PS_ipc
) == COMM_ERROR
) {
525 ipcSend(cwfd
, err_string
, strlen(err_string
));
528 } /* IPC_UDP_SOCKET */
540 dup2(fileno(debug_log
), 2);
546 commUnsetNonBlocking(fd
);
548 memset(&si
, 0, sizeof(STARTUPINFO
));
550 si
.cb
= sizeof(STARTUPINFO
);
552 si
.hStdInput
= (HANDLE
) _get_osfhandle(0);
554 si
.hStdOutput
= (HANDLE
) _get_osfhandle(1);
556 si
.hStdError
= (HANDLE
) _get_osfhandle(2);
558 si
.dwFlags
= STARTF_USESTDHANDLES
;
560 /* Make sure all other valid handles are not inerithable */
561 for (x
= 3; x
< Squid_MaxFD
; ++x
) {
562 if ((F
= _get_osfhandle(x
)) == -1)
565 SetHandleInformation((HANDLE
) F
, HANDLE_FLAG_INHERIT
, 0);
569 strcpy(buf1
+ 4096, params
->prog
);
570 str
= strtok(buf1
+ 4096, w_space
);
575 } while ((str
= strtok(NULL
, w_space
)));
580 strcat(buf1
, args
[x
]);
585 if (CreateProcess(buf1
+ 4096, buf1
, NULL
, NULL
, TRUE
, CREATE_NO_WINDOW
,
586 NULL
, NULL
, &si
, &pi
)) {
587 pid
= pi
.dwProcessId
;
588 hProcess
= pi
.hProcess
;
603 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << params
->prog
<< ": " << xstrerror());
605 ipcSend(cwfd
, err_string
, strlen(err_string
));
609 if (type
== IPC_UDP_SOCKET
) {
610 WSAPROTOCOL_INFO wpi
;
612 memset(&wpi
, 0, sizeof(wpi
));
614 if (SOCKET_ERROR
== WSADuplicateSocket(crfd_ipc
, pid
, &wpi
)) {
615 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: WSADuplicateSocket: " << xstrerror());
617 ipcSend(cwfd
, err_string
, strlen(err_string
));
621 x
= write(c2p
[1], (const char *) &wpi
, sizeof(wpi
));
623 if (x
< (ssize_t
)sizeof(wpi
)) {
624 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: write FD " << c2p
[1] << ": " << xstrerror());
625 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
627 ipcSend(cwfd
, err_string
, strlen(err_string
));
631 x
= read(p2c
[0], buf1
, 8192);
634 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: read FD " << p2c
[0] << ": " << xstrerror());
635 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
637 ipcSend(cwfd
, err_string
, strlen(err_string
));
639 } else if (strncmp(buf1
, ok_string
, strlen(ok_string
))) {
640 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
641 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
643 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(buf1
) << "'");
644 ipcSend(cwfd
, err_string
, strlen(err_string
));
648 x
= write(c2p
[1], (const char *) &PS_ipc
, sizeof(PS_ipc
));
650 if (x
< (ssize_t
)sizeof(PS_ipc
)) {
651 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: write FD " << c2p
[1] << ": " << xstrerror());
652 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
654 ipcSend(cwfd
, err_string
, strlen(err_string
));
658 x
= read(p2c
[0], buf1
, 8192);
661 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: read FD " << p2c
[0] << ": " << xstrerror());
662 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
664 ipcSend(cwfd
, err_string
, strlen(err_string
));
666 } else if (strncmp(buf1
, ok_string
, strlen(ok_string
))) {
667 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: " << prog
<< ": socket exchange failed");
668 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
670 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(buf1
) << "'");
671 ipcSend(cwfd
, err_string
, strlen(err_string
));
675 x
= send(pwfd_ipc
, (const void *)ok_string
, strlen(ok_string
), 0);
676 x
= recv(prfd_ipc
, (void *)(buf1
+ 200), 8191 - 200, 0);
677 assert((size_t) x
== strlen(ok_string
)
678 && !strncmp(ok_string
, buf1
+ 200, strlen(ok_string
)));
679 } /* IPC_UDP_SOCKET */
681 snprintf(buf1
, 8191, "%s(%ld) CHILD socket", prog
, (long int) pid
);
685 if (prfd_ipc
!= -1) {
686 snprintf(buf1
, 8191, "%s(%ld) <-> ipc CHILD socket", prog
, (long int) pid
);
687 fd_note(crfd_ipc
, buf1
);
688 snprintf(buf1
, 8191, "%s(%ld) <-> ipc PARENT socket", prog
, (long int) pid
);
689 fd_note(prfd_ipc
, buf1
);
692 /* else { IPC_TCP_SOCKET */
693 /* commSetNoLinger(fd); */
695 thread_params
.prog
= prog
;
697 thread_params
.send_fd
= cwfd
;
699 thread_params
.pid
= pid
;
701 if ((thread_params
.type
= type
) == IPC_TCP_SOCKET
)
702 thread_params
.rfd
= p2c
[0];
704 thread_params
.rfd
= prfd_ipc
;
706 thread
= (HANDLE
)_beginthreadex(NULL
, 0, ipc_thread_2
, &thread_params
, 0, NULL
);
709 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: _beginthreadex: " << xstrerror());
710 ipcSend(cwfd
, err_string
, strlen(err_string
));
714 snprintf(buf1
, 8191, "%ld\n", (long int) pid
);
716 if (-1 == ipcSend(cwfd
, buf1
, strlen(buf1
)))
719 debugs(54, 2, "ipc(" << prog
<< "," << pid
<< "): started successfully");
723 x
= recv(crfd
, (void *)buf1
, 8192, 0);
726 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes received from parent. Exiting...");
732 if (type
== IPC_UDP_SOCKET
&& !strcmp(buf1
, shutdown_string
)) {
733 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): request for shutdown received from parent. Exiting...");
735 TerminateProcess(hProcess
, 0);
739 debugs(54, 5, "ipc(" << prog
<< "," << pid
<< "): received from parent: " << rfc1738_escape_unescaped(buf1
));
741 if (type
== IPC_TCP_SOCKET
)
742 x
= write(c2p
[1], buf1
, x
);
744 x
= send(pwfd_ipc
, (const void *)buf1
, x
, 0);
747 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes written to " << prog
<< ". Exiting...");
760 if (fd_table
[crfd
].flags
.open
)
761 ipcCloseAllFD(-1, -1, crfd
, cwfd
);
763 if (prfd_ipc
!= -1) {
764 send(crfd_ipc
, (const void *)shutdown_string
, strlen(shutdown_string
), 0);
765 shutdown(crfd_ipc
, SD_BOTH
);
766 shutdown(prfd_ipc
, SD_BOTH
);
769 ipcCloseAllFD(prfd_ipc
, pwfd_ipc
, crfd_ipc
, cwfd_ipc
);
771 if (hProcess
&& WAIT_OBJECT_0
!=
772 WaitForSingleObject(hProcess
, type
== IPC_UDP_SOCKET
? 12000 : 5000)) {
775 debugs(54, DBG_CRITICAL
, "ipc(" << prog
<< "," << pid
<< "): WARNING: " << prog
<<
776 " didn't exit in " << (type
== IPC_UDP_SOCKET
? 12 : 5) << " seconds.");
780 if (thread
&& WAIT_OBJECT_0
!= WaitForSingleObject(thread
, 3000)) {
782 debugs(54, DBG_CRITICAL
, "ipc(" << prog
<< "," << pid
<< "): WARNING: ipc_thread_2 didn't exit in 3 seconds.");
789 debugs(54, 2, "ipc(" << prog
<< "," << pid
<< "): normal exit");
801 CloseHandle(hProcess
);
809 static unsigned int __stdcall
810 ipc_thread_2(void *in_params
)
814 struct thread_params
*params
= (struct thread_params
*) in_params
;
815 int type
= params
->type
;
816 int rfd
= params
->rfd
;
817 int send_fd
= params
->send_fd
;
818 char *prog
= xstrdup(params
->prog
);
819 pid_t pid
= params
->pid
;
820 char *buf2
= (char *)xcalloc(1, 8192);
823 if (type
== IPC_TCP_SOCKET
)
824 x
= read(rfd
, buf2
, 8192);
826 x
= recv(rfd
, (void *)buf2
, 8192, 0);
828 if ((x
<= 0 && type
== IPC_TCP_SOCKET
) ||
829 (x
< 0 && type
== IPC_UDP_SOCKET
)) {
830 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes read from " << prog
<< ". Exiting...");
837 if (type
== IPC_UDP_SOCKET
&& !strcmp(buf2
, shutdown_string
)) {
838 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): request for shutdown received. Exiting...");
844 if ((buf2
[x
- 1] == '\n') && (buf2
[x
- 2] == '\r')) {
851 debugs(54, 5, "ipc(" << prog
<< "," << pid
<< "): received from child : " << rfc1738_escape_unescaped(buf2
));
853 x
= send(send_fd
, (const void *)buf2
, x
, 0);
855 if ((x
<= 0 && type
== IPC_TCP_SOCKET
) ||
856 (x
< 0 && type
== IPC_UDP_SOCKET
)) {
857 debugs(54, 3, "ipc(" << prog
<< "," << pid
<< "): " << x
<< " bytes sent to parent. Exiting...");