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