]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc_win32.cc
merge from trunk r12441
[thirdparty/squid.git] / src / ipc_win32.cc
1 /*
2 * DEBUG: section 54 Windows Interprocess Communication
3 * AUTHOR: Andrey Shorin <tolsty@tushino.com>
4 * AUTHOR: Guido Serassio <serassio@squid-cache.org>
5 *
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
8 *
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.
17 *
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.
22 *
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.
27 *
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.
31 *
32 */
33
34 #include "squid.h"
35 #include "cache_cf.h"
36 #include "comm.h"
37 #include "fd.h"
38 #include "fde.h"
39 #include "ip/Address.h"
40 #include "rfc1738.h"
41 #include "SquidConfig.h"
42 #include "SquidIpc.h"
43 #include "SquidTime.h"
44 #include "tools.h"
45
46 #ifndef _MSWSOCK_
47 #include <mswsock.h>
48 #endif
49 #include <process.h>
50 #if HAVE_ERRNO_H
51 #include <errno.h>
52 #endif
53
54 struct ipc_params {
55 int type;
56 int crfd;
57 int cwfd;
58 Ip::Address local_addr;
59 struct addrinfo PS;
60 const char *prog;
61 char **args;
62 };
63
64 struct thread_params {
65 int type;
66 int rfd;
67 int send_fd;
68 const char *prog;
69 pid_t pid;
70 };
71
72 static unsigned int __stdcall ipc_thread_1(void *params);
73 static unsigned int __stdcall ipc_thread_2(void *params);
74
75 static const char *ok_string = "OK\n";
76 static const char *err_string = "ERR\n";
77 static const char *shutdown_string = "$shutdown\n";
78
79 static const char *hello_string = "hi there\n";
80 #define HELLO_BUF_SZ 32
81 static char hello_buf[HELLO_BUF_SZ];
82
83 static int
84 ipcCloseAllFD(int prfd, int pwfd, int crfd, int cwfd)
85 {
86 if (prfd >= 0)
87 comm_close(prfd);
88
89 if (prfd != pwfd)
90 if (pwfd >= 0)
91 comm_close(pwfd);
92
93 if (crfd >= 0)
94 comm_close(crfd);
95
96 if (crfd != cwfd)
97 if (cwfd >= 0)
98 comm_close(cwfd);
99
100 return -1;
101 }
102
103 static void
104 PutEnvironment()
105 {
106 #if HAVE_PUTENV
107 char *env_str;
108 int tmp_s;
109 env_str = (char *)xcalloc((tmp_s = strlen(Debug::debugOptions) + 32), 1);
110 snprintf(env_str, tmp_s, "SQUID_DEBUG=%s", Debug::debugOptions);
111 putenv(env_str);
112 #endif
113 }
114
115 pid_t
116 ipcCreate(int type, const char *prog, const char *const args[], const char *name, Ip::Address &local_addr, int *rfd, int *wfd, void **hIpc)
117 {
118 unsigned long thread;
119
120 struct ipc_params params;
121 int opt;
122 int optlen = sizeof(opt);
123 DWORD ecode = 0;
124 pid_t pid;
125
126 Ip::Address tmp_addr;
127 struct addrinfo *aiCS = NULL;
128 struct addrinfo *aiPS = NULL;
129
130 int crfd = -1;
131 int prfd = -1;
132 int cwfd = -1;
133 int pwfd = -1;
134 int x;
135
136 requirePathnameExists(name, prog);
137
138 if (rfd)
139 *rfd = -1;
140
141 if (wfd)
142 *wfd = -1;
143
144 if (hIpc)
145 *hIpc = NULL;
146
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));
151 }
152
153 if (type == IPC_TCP_SOCKET) {
154 crfd = cwfd = comm_open(SOCK_STREAM,
155 IPPROTO_TCP,
156 local_addr,
157 COMM_NOCLOEXEC,
158 name);
159 prfd = pwfd = comm_open(SOCK_STREAM,
160 IPPROTO_TCP, /* protocol */
161 local_addr,
162 0, /* blocking */
163 name);
164 } else if (type == IPC_UDP_SOCKET) {
165 crfd = cwfd = comm_open(SOCK_DGRAM,
166 IPPROTO_UDP,
167 local_addr,
168 COMM_NOCLOEXEC,
169 name);
170 prfd = pwfd = comm_open(SOCK_DGRAM,
171 IPPROTO_UDP,
172 local_addr,
173 0,
174 name);
175 } else if (type == IPC_FIFO) {
176 debugs(54, DBG_CRITICAL, "ipcCreate: " << prog << ": use IPC_TCP_SOCKET instead of IP_FIFO on Windows");
177 assert(0);
178 } else {
179 assert(IPC_NONE);
180 }
181
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);
186
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);
191 }
192
193 if (crfd < 0) {
194 debugs(54, DBG_CRITICAL, "ipcCreate: Failed to create child FD.");
195 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
196 }
197
198 if (pwfd < 0) {
199 debugs(54, DBG_CRITICAL, "ipcCreate: Failed to create server FD.");
200 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
201 }
202
203 // AYJ: these flags should be neutral, but if not IPv6 version needs adding
204 if (type == IPC_TCP_SOCKET || type == IPC_UDP_SOCKET) {
205
206 tmp_addr.InitAddrInfo(aiPS);
207
208 if (getsockname(pwfd, aiPS->ai_addr, &(aiPS->ai_addrlen) ) < 0) {
209 debugs(54, DBG_CRITICAL, "ipcCreate: getsockname: " << xstrerror());
210 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
211 }
212
213 tmp_addr = *aiPS;
214
215 debugs(54, 3, "ipcCreate: FD " << pwfd << " sockaddr " << tmp_addr );
216
217 tmp_addr.InitAddrInfo(aiCS);
218
219 if (getsockname(crfd, aiCS->ai_addr, &(aiCS->ai_addrlen) ) < 0) {
220 debugs(54, DBG_CRITICAL, "ipcCreate: getsockname: " << xstrerror());
221 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
222 }
223
224 tmp_addr.SetEmpty();
225 tmp_addr = *aiCS;
226
227 debugs(54, 3, "ipcCreate: FD " << crfd << " sockaddr " << tmp_addr );
228 }
229
230 if (type == IPC_TCP_SOCKET) {
231 if (listen(crfd, 1) < 0) {
232 debugs(54, DBG_IMPORTANT, "ipcCreate: listen FD " << crfd << ": " << xstrerror());
233 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
234 }
235
236 debugs(54, 3, "ipcCreate: FD " << crfd << " listening...");
237 }
238
239 /* flush or else we get dup data if unbuffered_logs is set */
240 logsFlush();
241
242 params.type = type;
243
244 params.crfd = crfd;
245
246 params.cwfd = cwfd;
247
248 params.PS = *aiPS;
249
250 params.local_addr = local_addr;
251
252 params.prog = prog;
253
254 params.args = (char **) args;
255
256 thread = _beginthreadex(NULL, 0, ipc_thread_1, &params, 0, NULL);
257
258 if (thread == 0) {
259 debugs(54, DBG_IMPORTANT, "ipcCreate: _beginthread: " << xstrerror());
260 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
261 }
262
263 /* NP: tmp_addr was left with eiether empty or aiCS in Ip::Address format */
264 if (comm_connect_addr(pwfd, tmp_addr) == COMM_ERROR) {
265 CloseHandle((HANDLE) thread);
266 return ipcCloseAllFD(prfd, pwfd, -1, -1);
267 }
268
269 memset(hello_buf, '\0', HELLO_BUF_SZ);
270 x = recv(prfd, (void *)hello_buf, HELLO_BUF_SZ - 1, 0);
271
272 if (x < 0) {
273 debugs(54, DBG_CRITICAL, "ipcCreate: PARENT: hello read test failed");
274 debugs(54, DBG_CRITICAL, "--> read: " << xstrerror());
275 CloseHandle((HANDLE) thread);
276 return ipcCloseAllFD(prfd, pwfd, -1, -1);
277 } else if (strcmp(hello_buf, hello_string)) {
278 debugs(54, DBG_CRITICAL, "ipcCreate: PARENT: hello read test failed");
279 debugs(54, DBG_CRITICAL, "--> read returned " << x);
280 debugs(54, DBG_CRITICAL, "--> got '" << rfc1738_escape(hello_buf) << "'");
281 CloseHandle((HANDLE) thread);
282 return ipcCloseAllFD(prfd, pwfd, -1, -1);
283 }
284
285 x = send(pwfd, (const void *)ok_string, strlen(ok_string), 0);
286
287 if (x < 0) {
288 debugs(54, DBG_CRITICAL, "ipcCreate: PARENT: OK write test failed");
289 debugs(54, DBG_CRITICAL, "--> read: " << xstrerror());
290 CloseHandle((HANDLE) thread);
291 return ipcCloseAllFD(prfd, pwfd, -1, -1);
292 }
293
294 memset(hello_buf, '\0', HELLO_BUF_SZ);
295 x = recv(prfd, (void *)hello_buf, HELLO_BUF_SZ - 1, 0);
296
297 if (x < 0) {
298 debugs(54, DBG_CRITICAL, "ipcCreate: PARENT: OK read test failed");
299 debugs(54, DBG_CRITICAL, "--> read: " << xstrerror());
300 CloseHandle((HANDLE) thread);
301 return ipcCloseAllFD(prfd, pwfd, -1, -1);
302 } else if (!strcmp(hello_buf, err_string)) {
303 debugs(54, DBG_CRITICAL, "ipcCreate: PARENT: OK read test failed");
304 debugs(54, DBG_CRITICAL, "--> read returned " << x);
305 debugs(54, DBG_CRITICAL, "--> got '" << rfc1738_escape(hello_buf) << "'");
306 CloseHandle((HANDLE) thread);
307 return ipcCloseAllFD(prfd, pwfd, -1, -1);
308 }
309
310 hello_buf[x] = '\0';
311 pid = atol(hello_buf);
312 commUnsetFdTimeout(prfd);
313 commSetNonBlocking(prfd);
314 commSetNonBlocking(pwfd);
315 commSetCloseOnExec(prfd);
316 commSetCloseOnExec(pwfd);
317
318 if (rfd)
319 *rfd = prfd;
320
321 if (wfd)
322 *wfd = pwfd;
323
324 fd_table[prfd].flags.ipc = 1;
325
326 fd_table[pwfd].flags.ipc = 1;
327
328 fd_table[crfd].flags.ipc = 1;
329
330 fd_table[cwfd].flags.ipc = 1;
331
332 if (Config.sleep_after_fork) {
333 /* XXX emulation of usleep() */
334 DWORD sl;
335 sl = Config.sleep_after_fork / 1000;
336
337 if (sl == 0)
338 sl = 1;
339
340 Sleep(sl);
341 }
342
343 if (GetExitCodeThread((HANDLE) thread, &ecode) && ecode == STILL_ACTIVE) {
344 if (hIpc)
345 *hIpc = (HANDLE) thread;
346
347 return pid;
348 } else {
349 CloseHandle((HANDLE) thread);
350 return ipcCloseAllFD(prfd, pwfd, -1, -1);
351 }
352 }
353
354 static int
355 ipcSend(int cwfd, const char *buf, int len)
356 {
357 int x;
358
359 x = send(cwfd, (const void *)buf, len, 0);
360
361 if (x < 0) {
362 debugs(54, DBG_CRITICAL, "sendto FD " << cwfd << ": " << xstrerror());
363 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: hello write test failed");
364 }
365
366 return x;
367 }
368
369 static unsigned int __stdcall
370 ipc_thread_1(void *in_params)
371 {
372 int t1, t2, t3, retval = -1;
373 int p2c[2] = {-1, -1};
374 int c2p[2] = {-1, -1};
375 HANDLE hProcess = NULL, thread = NULL;
376 pid_t pid = -1;
377
378 struct thread_params thread_params;
379 ssize_t x;
380 int fd = -1;
381 char *str;
382 STARTUPINFO si;
383 PROCESS_INFORMATION pi;
384 long F;
385 int prfd_ipc = -1, pwfd_ipc = -1, crfd_ipc = -1, cwfd_ipc = -1;
386 char *prog = NULL, *buf1 = NULL;
387
388 Ip::Address PS_ipc;
389 Ip::Address CS_ipc;
390 struct addrinfo *aiPS_ipc = NULL;
391 struct addrinfo *aiCS_ipc = NULL;
392
393 struct ipc_params *params = (struct ipc_params *) in_params;
394 int type = params->type;
395 int crfd = params->crfd;
396 int cwfd = params->cwfd;
397 char **args = params->args;
398
399 Ip::Address PS = params->PS;
400 Ip::Address local_addr = params->local_addr;
401
402 buf1 = (char *)xcalloc(1, 8192);
403 strcpy(buf1, params->prog);
404 prog = strtok(buf1, w_space);
405
406 if ((str = strrchr(prog, '/')))
407 prog = ++str;
408
409 if ((str = strrchr(prog, '\\')))
410 prog = ++str;
411
412 prog = xstrdup(prog);
413
414 if (type == IPC_TCP_SOCKET) {
415 debugs(54, 3, "ipcCreate: calling accept on FD " << crfd);
416
417 if ((fd = accept(crfd, NULL, NULL)) < 0) {
418 debugs(54, DBG_CRITICAL, "ipcCreate: FD " << crfd << " accept: " << xstrerror());
419 goto cleanup;
420 }
421
422 debugs(54, 3, "ipcCreate: CHILD accepted new FD " << fd);
423 comm_close(crfd);
424 snprintf(buf1, 8191, "%s CHILD socket", prog);
425 fd_open(fd, FD_SOCKET, buf1);
426 fd_table[fd].flags.ipc = 1;
427 cwfd = crfd = fd;
428 } else if (type == IPC_UDP_SOCKET) {
429 if (comm_connect_addr(crfd, params->PS) == COMM_ERROR)
430 goto cleanup;
431 }
432
433 x = send(cwfd, (const void *)hello_string, strlen(hello_string) + 1, 0);
434
435 if (x < 0) {
436 debugs(54, DBG_CRITICAL, "sendto FD " << cwfd << ": " << xstrerror());
437 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: hello write test failed");
438 goto cleanup;
439 }
440
441 PutEnvironment();
442 memset(buf1, '\0', sizeof(buf1));
443 x = recv(crfd, (void *)buf1, 8191, 0);
444
445 if (x < 0) {
446 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: OK read test failed");
447 debugs(54, DBG_CRITICAL, "--> read: " << xstrerror());
448 goto cleanup;
449 } else if (strcmp(buf1, ok_string)) {
450 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: OK read test failed");
451 debugs(54, DBG_CRITICAL, "--> read returned " << x);
452 debugs(54, DBG_CRITICAL, "--> got '" << rfc1738_escape(hello_buf) << "'");
453 goto cleanup;
454 }
455
456 /* assign file descriptors to child process */
457 if (_pipe(p2c, 1024, _O_BINARY | _O_NOINHERIT) < 0) {
458 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: pipe: " << xstrerror());
459 ipcSend(cwfd, err_string, strlen(err_string));
460 goto cleanup;
461 }
462
463 if (_pipe(c2p, 1024, _O_BINARY | _O_NOINHERIT) < 0) {
464 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: pipe: " << xstrerror());
465 ipcSend(cwfd, err_string, strlen(err_string));
466 goto cleanup;
467 }
468
469 if (type == IPC_UDP_SOCKET) {
470 snprintf(buf1, 8192, "%s(%ld) <-> ipc CHILD socket", prog, -1L);
471 crfd_ipc = cwfd_ipc = comm_open(SOCK_DGRAM, IPPROTO_UDP, local_addr, 0, buf1);
472
473 if (crfd_ipc < 0) {
474 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: Failed to create child FD for " << prog << ".");
475 ipcSend(cwfd, err_string, strlen(err_string));
476 goto cleanup;
477 }
478
479 snprintf(buf1, 8192, "%s(%ld) <-> ipc PARENT socket", prog, -1L);
480 prfd_ipc = pwfd_ipc = comm_open(SOCK_DGRAM, IPPROTO_UDP, local_addr, 0, buf1);
481
482 if (pwfd_ipc < 0) {
483 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: Failed to create server FD for " << prog << ".");
484 ipcSend(cwfd, err_string, strlen(err_string));
485 goto cleanup;
486 }
487
488 PS_ipc.InitAddrInfo(aiPS_ipc);
489
490 if (getsockname(pwfd_ipc, aiPS_ipc->ai_addr, &(aiPS_ipc->ai_addrlen)) < 0) {
491 debugs(54, DBG_CRITICAL, "ipcCreate: getsockname: " << xstrerror());
492 ipcSend(cwfd, err_string, strlen(err_string));
493 goto cleanup;
494 }
495
496 PS_ipc = *aiPS_ipc;
497
498 debugs(54, 3, "ipcCreate: FD " << pwfd_ipc << " sockaddr " << PS_ipc);
499
500 CS_ipc.InitAddrInfo(aiCS_ipc);
501
502 if (getsockname(crfd_ipc, aiCS_ipc->ai_addr, &(aiCS_ipc->ai_addrlen)) < 0) {
503 debugs(54, DBG_CRITICAL, "ipcCreate: getsockname: " << xstrerror());
504 ipcSend(cwfd, err_string, strlen(err_string));
505 goto cleanup;
506 }
507
508 CS_ipc = *aiCS_ipc;
509
510 debugs(54, 3, "ipcCreate: FD " << crfd_ipc << " sockaddr " << CS_ipc);
511
512 if (comm_connect_addr(pwfd_ipc, CS_ipc) == COMM_ERROR) {
513 ipcSend(cwfd, err_string, strlen(err_string));
514 goto cleanup;
515 }
516
517 fd = crfd;
518
519 if (comm_connect_addr(crfd_ipc, PS_ipc) == COMM_ERROR) {
520 ipcSend(cwfd, err_string, strlen(err_string));
521 goto cleanup;
522 }
523 } /* IPC_UDP_SOCKET */
524
525 t1 = dup(0);
526
527 t2 = dup(1);
528
529 t3 = dup(2);
530
531 dup2(c2p[0], 0);
532
533 dup2(p2c[1], 1);
534
535 dup2(fileno(debug_log), 2);
536
537 close(c2p[0]);
538
539 close(p2c[1]);
540
541 commUnsetNonBlocking(fd);
542
543 memset(&si, 0, sizeof(STARTUPINFO));
544
545 si.cb = sizeof(STARTUPINFO);
546
547 si.hStdInput = (HANDLE) _get_osfhandle(0);
548
549 si.hStdOutput = (HANDLE) _get_osfhandle(1);
550
551 si.hStdError = (HANDLE) _get_osfhandle(2);
552
553 si.dwFlags = STARTF_USESTDHANDLES;
554
555 /* Make sure all other valid handles are not inerithable */
556 for (x = 3; x < Squid_MaxFD; ++x) {
557 if ((F = _get_osfhandle(x)) == -1)
558 continue;
559
560 SetHandleInformation((HANDLE) F, HANDLE_FLAG_INHERIT, 0);
561 }
562
563 *buf1 = '\0';
564 strcpy(buf1 + 4096, params->prog);
565 str = strtok(buf1 + 4096, w_space);
566
567 do {
568 strcat(buf1, str);
569 strcat(buf1, " ");
570 } while ((str = strtok(NULL, w_space)));
571
572 x = 1;
573
574 while (args[x]) {
575 strcat(buf1, args[x]);
576 ++x;
577 strcat(buf1, " ");
578 }
579
580 if (CreateProcess(buf1 + 4096, buf1, NULL, NULL, TRUE, CREATE_NO_WINDOW,
581 NULL, NULL, &si, &pi)) {
582 pid = pi.dwProcessId;
583 hProcess = pi.hProcess;
584 } else {
585 pid = -1;
586 x = GetLastError();
587 }
588
589 dup2(t1, 0);
590 dup2(t2, 1);
591 dup2(t3, 2);
592 close(t1);
593 close(t2);
594 close(t3);
595
596 if (pid == -1) {
597 errno = x;
598 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: " << params->prog << ": " << xstrerror());
599
600 ipcSend(cwfd, err_string, strlen(err_string));
601 goto cleanup;
602 }
603
604 if (type == IPC_UDP_SOCKET) {
605 WSAPROTOCOL_INFO wpi;
606
607 memset(&wpi, 0, sizeof(wpi));
608
609 if (SOCKET_ERROR == WSADuplicateSocket(crfd_ipc, pid, &wpi)) {
610 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: WSADuplicateSocket: " << xstrerror());
611
612 ipcSend(cwfd, err_string, strlen(err_string));
613 goto cleanup;
614 }
615
616 x = write(c2p[1], (const char *) &wpi, sizeof(wpi));
617
618 if (x < (ssize_t)sizeof(wpi)) {
619 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: write FD " << c2p[1] << ": " << xstrerror());
620 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: " << prog << ": socket exchange failed");
621
622 ipcSend(cwfd, err_string, strlen(err_string));
623 goto cleanup;
624 }
625
626 x = read(p2c[0], buf1, 8192);
627
628 if (x < 0) {
629 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: read FD " << p2c[0] << ": " << xstrerror());
630 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: " << prog << ": socket exchange failed");
631
632 ipcSend(cwfd, err_string, strlen(err_string));
633 goto cleanup;
634 } else if (strncmp(buf1, ok_string, strlen(ok_string))) {
635 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: " << prog << ": socket exchange failed");
636 debugs(54, DBG_CRITICAL, "--> read returned " << x);
637 buf1[x] = '\0';
638 debugs(54, DBG_CRITICAL, "--> got '" << rfc1738_escape(buf1) << "'");
639 ipcSend(cwfd, err_string, strlen(err_string));
640 goto cleanup;
641 }
642
643 x = write(c2p[1], (const char *) &PS_ipc, sizeof(PS_ipc));
644
645 if (x < (ssize_t)sizeof(PS_ipc)) {
646 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: write FD " << c2p[1] << ": " << xstrerror());
647 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: " << prog << ": socket exchange failed");
648
649 ipcSend(cwfd, err_string, strlen(err_string));
650 goto cleanup;
651 }
652
653 x = read(p2c[0], buf1, 8192);
654
655 if (x < 0) {
656 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: read FD " << p2c[0] << ": " << xstrerror());
657 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: " << prog << ": socket exchange failed");
658
659 ipcSend(cwfd, err_string, strlen(err_string));
660 goto cleanup;
661 } else if (strncmp(buf1, ok_string, strlen(ok_string))) {
662 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: " << prog << ": socket exchange failed");
663 debugs(54, DBG_CRITICAL, "--> read returned " << x);
664 buf1[x] = '\0';
665 debugs(54, DBG_CRITICAL, "--> got '" << rfc1738_escape(buf1) << "'");
666 ipcSend(cwfd, err_string, strlen(err_string));
667 goto cleanup;
668 }
669
670 x = send(pwfd_ipc, (const void *)ok_string, strlen(ok_string), 0);
671 x = recv(prfd_ipc, (void *)(buf1 + 200), 8191 - 200, 0);
672 assert((size_t) x == strlen(ok_string)
673 && !strncmp(ok_string, buf1 + 200, strlen(ok_string)));
674 } /* IPC_UDP_SOCKET */
675
676 snprintf(buf1, 8191, "%s(%ld) CHILD socket", prog, (long int) pid);
677
678 fd_note(fd, buf1);
679
680 if (prfd_ipc != -1) {
681 snprintf(buf1, 8191, "%s(%ld) <-> ipc CHILD socket", prog, (long int) pid);
682 fd_note(crfd_ipc, buf1);
683 snprintf(buf1, 8191, "%s(%ld) <-> ipc PARENT socket", prog, (long int) pid);
684 fd_note(prfd_ipc, buf1);
685 }
686
687 /* else { IPC_TCP_SOCKET */
688 /* commSetNoLinger(fd); */
689 /* } */
690 thread_params.prog = prog;
691
692 thread_params.send_fd = cwfd;
693
694 thread_params.pid = pid;
695
696 if ((thread_params.type = type) == IPC_TCP_SOCKET)
697 thread_params.rfd = p2c[0];
698 else
699 thread_params.rfd = prfd_ipc;
700
701 thread = (HANDLE)_beginthreadex(NULL, 0, ipc_thread_2, &thread_params, 0, NULL);
702
703 if (!thread) {
704 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: _beginthreadex: " << xstrerror());
705 ipcSend(cwfd, err_string, strlen(err_string));
706 goto cleanup;
707 }
708
709 snprintf(buf1, 8191, "%ld\n", (long int) pid);
710
711 if (-1 == ipcSend(cwfd, buf1, strlen(buf1)))
712 goto cleanup;
713
714 debugs(54, 2, "ipc(" << prog << "," << pid << "): started successfully");
715
716 /* cycle */
717 for (;;) {
718 x = recv(crfd, (void *)buf1, 8192, 0);
719
720 if (x <= 0) {
721 debugs(54, 3, "ipc(" << prog << "," << pid << "): " << x << " bytes received from parent. Exiting...");
722 break;
723 }
724
725 buf1[x] = '\0';
726
727 if (type == IPC_UDP_SOCKET && !strcmp(buf1, shutdown_string)) {
728 debugs(54, 3, "ipc(" << prog << "," << pid << "): request for shutdown received from parent. Exiting...");
729
730 TerminateProcess(hProcess, 0);
731 break;
732 }
733
734 debugs(54, 5, "ipc(" << prog << "," << pid << "): received from parent: " << rfc1738_escape_unescaped(buf1));
735
736 if (type == IPC_TCP_SOCKET)
737 x = write(c2p[1], buf1, x);
738 else
739 x = send(pwfd_ipc, (const void *)buf1, x, 0);
740
741 if (x <= 0) {
742 debugs(54, 3, "ipc(" << prog << "," << pid << "): " << x << " bytes written to " << prog << ". Exiting...");
743
744 break;
745 }
746 }
747
748 retval = 0;
749
750 cleanup:
751
752 if (c2p[1] != -1)
753 close(c2p[1]);
754
755 if (fd_table[crfd].flags.open)
756 ipcCloseAllFD(-1, -1, crfd, cwfd);
757
758 if (prfd_ipc != -1) {
759 send(crfd_ipc, (const void *)shutdown_string, strlen(shutdown_string), 0);
760 shutdown(crfd_ipc, SD_BOTH);
761 shutdown(prfd_ipc, SD_BOTH);
762 }
763
764 ipcCloseAllFD(prfd_ipc, pwfd_ipc, crfd_ipc, cwfd_ipc);
765
766 if (hProcess && WAIT_OBJECT_0 !=
767 WaitForSingleObject(hProcess, type == IPC_UDP_SOCKET ? 12000 : 5000)) {
768
769 getCurrentTime();
770 debugs(54, DBG_CRITICAL, "ipc(" << prog << "," << pid << "): WARNING: " << prog <<
771 " didn't exit in " << (type == IPC_UDP_SOCKET ? 12 : 5) << " seconds.");
772
773 }
774
775 if (thread && WAIT_OBJECT_0 != WaitForSingleObject(thread, 3000)) {
776 getCurrentTime();
777 debugs(54, DBG_CRITICAL, "ipc(" << prog << "," << pid << "): WARNING: ipc_thread_2 didn't exit in 3 seconds.");
778
779 }
780
781 getCurrentTime();
782
783 if (!retval)
784 debugs(54, 2, "ipc(" << prog << "," << pid << "): normal exit");
785
786 if (buf1)
787 xfree(buf1);
788
789 if (prog)
790 xfree(prog);
791
792 if (thread)
793 CloseHandle(thread);
794
795 if (hProcess)
796 CloseHandle(hProcess);
797
798 if (p2c[0] != -1)
799 close(p2c[0]);
800
801 return retval;
802 }
803
804 static unsigned int __stdcall
805 ipc_thread_2(void *in_params)
806 {
807 int x;
808
809 struct thread_params *params = (struct thread_params *) in_params;
810 int type = params->type;
811 int rfd = params->rfd;
812 int send_fd = params->send_fd;
813 char *prog = xstrdup(params->prog);
814 pid_t pid = params->pid;
815 char *buf2 = (char *)xcalloc(1, 8192);
816
817 for (;;) {
818 if (type == IPC_TCP_SOCKET)
819 x = read(rfd, buf2, 8192);
820 else
821 x = recv(rfd, (void *)buf2, 8192, 0);
822
823 if ((x <= 0 && type == IPC_TCP_SOCKET) ||
824 (x < 0 && type == IPC_UDP_SOCKET)) {
825 debugs(54, 3, "ipc(" << prog << "," << pid << "): " << x << " bytes read from " << prog << ". Exiting...");
826
827 break;
828 }
829
830 buf2[x] = '\0';
831
832 if (type == IPC_UDP_SOCKET && !strcmp(buf2, shutdown_string)) {
833 debugs(54, 3, "ipc(" << prog << "," << pid << "): request for shutdown received. Exiting...");
834
835 break;
836 }
837
838 if (x >= 2) {
839 if ((buf2[x - 1] == '\n') && (buf2[x - 2] == '\r')) {
840 buf2[x - 2] = '\n';
841 buf2[x - 1] = '\0';
842 --x;
843 }
844 }
845
846 debugs(54, 5, "ipc(" << prog << "," << pid << "): received from child : " << rfc1738_escape_unescaped(buf2));
847
848 x = send(send_fd, (const void *)buf2, x, 0);
849
850 if ((x <= 0 && type == IPC_TCP_SOCKET) ||
851 (x < 0 && type == IPC_UDP_SOCKET)) {
852 debugs(54, 3, "ipc(" << prog << "," << pid << "): " << x << " bytes sent to parent. Exiting...");
853
854 break;
855 }
856 }
857
858 xfree(prog);
859 xfree(buf2);
860 return 0;
861 }