]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc.cc
2 * DEBUG: section 54 Interprocess Communication
3 * AUTHOR: Duane Wessels
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 #include "comm/Connection.h"
38 #include "ip/Address.h"
40 #include "SquidConfig.h"
45 static const char *hello_string
= "hi there\n";
46 #define HELLO_BUF_SZ 32
47 static char hello_buf
[HELLO_BUF_SZ
];
50 ipcCloseAllFD(int prfd
, int pwfd
, int crfd
, int cwfd
)
75 env_str
= (char *)xcalloc((tmp_s
= strlen(Debug::debugOptions
) + 32), 1);
76 snprintf(env_str
, tmp_s
, "SQUID_DEBUG=%s", Debug::debugOptions
);
82 ipcCreate(int type
, const char *prog
, const char *const args
[], const char *name
, Ip::Address
&local_addr
, int *rfd
, int *wfd
, void **hIpc
)
87 struct addrinfo
*AI
= NULL
;
96 #if USE_POLL && _SQUID_OSF_
97 assert(type
!= IPC_FIFO
);
109 // NP: no wrapping around d and c usage since we *want* code expansion
110 #define IPC_CHECK_FAIL(f,d,c) \
112 debugs(54, DBG_CRITICAL, "ERROR: Failed to create helper " d " FD: " << c); \
113 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd); \
116 if (type
== IPC_TCP_SOCKET
) {
117 crfd
= cwfd
= comm_open(SOCK_STREAM
,
122 prfd
= pwfd
= comm_open(SOCK_STREAM
,
127 IPC_CHECK_FAIL(crfd
, "child read", "TCP " << local_addr
);
128 IPC_CHECK_FAIL(prfd
, "parent read", "TCP " << local_addr
);
129 } else if (type
== IPC_UDP_SOCKET
) {
130 crfd
= cwfd
= comm_open(SOCK_DGRAM
,
135 prfd
= pwfd
= comm_open(SOCK_DGRAM
,
140 IPC_CHECK_FAIL(crfd
, "child read", "UDP" << local_addr
);
141 IPC_CHECK_FAIL(prfd
, "parent read", "UDP" << local_addr
);
142 } else if (type
== IPC_FIFO
) {
147 debugs(54, DBG_CRITICAL
, "ipcCreate: pipe: " << xstrerror());
148 return -1; // maybe ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
150 fd_open(prfd
= p2c
[0], FD_PIPE
, "IPC FIFO Parent Read");
151 fd_open(cwfd
= p2c
[1], FD_PIPE
, "IPC FIFO Child Write");
154 debugs(54, DBG_CRITICAL
, "ipcCreate: pipe: " << xstrerror());
155 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
157 fd_open(crfd
= c2p
[0], FD_PIPE
, "IPC FIFO Child Read");
158 fd_open(pwfd
= c2p
[1], FD_PIPE
, "IPC FIFO Parent Write");
160 IPC_CHECK_FAIL(crfd
, "child read", "FIFO pipe");
161 IPC_CHECK_FAIL(prfd
, "parent read", "FIFO pipe");
163 #if HAVE_SOCKETPAIR && defined(AF_UNIX)
165 } else if (type
== IPC_UNIX_STREAM
) {
169 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
) < 0) {
170 debugs(54, DBG_CRITICAL
, "ipcCreate: socketpair: " << xstrerror());
174 setsockopt(fds
[0], SOL_SOCKET
, SO_SNDBUF
, (void *) &buflen
, sizeof(buflen
));
175 setsockopt(fds
[0], SOL_SOCKET
, SO_RCVBUF
, (void *) &buflen
, sizeof(buflen
));
176 setsockopt(fds
[1], SOL_SOCKET
, SO_SNDBUF
, (void *) &buflen
, sizeof(buflen
));
177 setsockopt(fds
[1], SOL_SOCKET
, SO_RCVBUF
, (void *) &buflen
, sizeof(buflen
));
178 fd_open(prfd
= pwfd
= fds
[0], FD_PIPE
, "IPC UNIX STREAM Parent");
179 fd_open(crfd
= cwfd
= fds
[1], FD_PIPE
, "IPC UNIX STREAM Parent");
180 IPC_CHECK_FAIL(crfd
, "child read", "UDS socket");
181 IPC_CHECK_FAIL(prfd
, "parent read", "UDS socket");
183 } else if (type
== IPC_UNIX_DGRAM
) {
186 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, fds
) < 0) {
187 debugs(54, DBG_CRITICAL
, "ipcCreate: socketpair: " << xstrerror());
191 fd_open(prfd
= pwfd
= fds
[0], FD_PIPE
, "IPC UNIX DGRAM Parent");
192 fd_open(crfd
= cwfd
= fds
[1], FD_PIPE
, "IPC UNIX DGRAM Parent");
194 IPC_CHECK_FAIL(crfd
, "child read", "UDS datagram");
195 IPC_CHECK_FAIL(prfd
, "parent read", "UDS datagram");
202 debugs(54, 3, "ipcCreate: prfd FD " << prfd
);
203 debugs(54, 3, "ipcCreate: pwfd FD " << pwfd
);
204 debugs(54, 3, "ipcCreate: crfd FD " << crfd
);
205 debugs(54, 3, "ipcCreate: cwfd FD " << cwfd
);
207 if (type
== IPC_TCP_SOCKET
|| type
== IPC_UDP_SOCKET
) {
208 PaS
.InitAddrInfo(AI
);
210 if (getsockname(pwfd
, AI
->ai_addr
, &AI
->ai_addrlen
) < 0) {
211 PaS
.FreeAddrInfo(AI
);
212 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
213 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
218 debugs(54, 3, "ipcCreate: FD " << pwfd
<< " sockaddr " << PaS
);
220 PaS
.FreeAddrInfo(AI
);
222 ChS
.InitAddrInfo(AI
);
224 if (getsockname(crfd
, AI
->ai_addr
, &AI
->ai_addrlen
) < 0) {
225 ChS
.FreeAddrInfo(AI
);
226 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
227 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
232 ChS
.FreeAddrInfo(AI
);
234 debugs(54, 3, "ipcCreate: FD " << crfd
<< " sockaddr " << ChS
);
238 if (type
== IPC_TCP_SOCKET
) {
239 if (listen(crfd
, 1) < 0) {
240 debugs(54, DBG_IMPORTANT
, "ipcCreate: listen FD " << crfd
<< ": " << xstrerror());
241 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
244 debugs(54, 3, "ipcCreate: FD " << crfd
<< " listening...");
247 /* flush or else we get dup data if unbuffered_logs is set */
250 if ((pid
= fork()) < 0) {
251 debugs(54, DBG_IMPORTANT
, "ipcCreate: fork: " << xstrerror());
252 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
255 if (pid
> 0) { /* parent */
256 /* close shared socket with child */
264 if (type
== IPC_TCP_SOCKET
|| type
== IPC_UDP_SOCKET
) {
265 if (comm_connect_addr(pwfd
, ChS
) == COMM_ERROR
)
266 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
269 memset(hello_buf
, '\0', HELLO_BUF_SZ
);
271 if (type
== IPC_UDP_SOCKET
)
272 x
= comm_udp_recv(prfd
, hello_buf
, HELLO_BUF_SZ
- 1, 0);
274 x
= read(prfd
, hello_buf
, HELLO_BUF_SZ
- 1);
277 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: hello read test failed");
278 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
279 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
280 } else if (strcmp(hello_buf
, hello_string
)) {
281 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: hello read test failed");
282 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
283 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
284 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
287 commUnsetFdTimeout(prfd
);
288 commSetNonBlocking(prfd
);
289 commSetNonBlocking(pwfd
);
297 fd_table
[prfd
].flags
.ipc
= 1;
299 fd_table
[pwfd
].flags
.ipc
= 1;
301 if (Config
.sleep_after_fork
) {
302 /* XXX emulation of usleep() */
305 sl
.tv_sec
= Config
.sleep_after_fork
/ 1000000;
306 sl
.tv_usec
= Config
.sleep_after_fork
% 1000000;
307 select(0, NULL
, NULL
, NULL
, &sl
);
314 TheProcessKind
= pkHelper
;
315 no_suid(); /* give up extra priviliges */
317 /* close shared socket with parent */
325 if (type
== IPC_TCP_SOCKET
) {
326 debugs(54, 3, "ipcCreate: calling accept on FD " << crfd
);
328 if ((fd
= accept(crfd
, NULL
, NULL
)) < 0) {
329 debugs(54, DBG_CRITICAL
, "ipcCreate: FD " << crfd
<< " accept: " << xstrerror());
333 debugs(54, 3, "ipcCreate: CHILD accepted new FD " << fd
);
336 } else if (type
== IPC_UDP_SOCKET
) {
337 if (comm_connect_addr(crfd
, PaS
) == COMM_ERROR
)
338 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
341 if (type
== IPC_UDP_SOCKET
) {
342 x
= comm_udp_send(cwfd
, hello_string
, strlen(hello_string
) + 1, 0);
345 debugs(54, DBG_CRITICAL
, "sendto FD " << cwfd
<< ": " << xstrerror());
346 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: hello write test failed");
350 if (write(cwfd
, hello_string
, strlen(hello_string
) + 1) < 0) {
351 debugs(54, DBG_CRITICAL
, "write FD " << cwfd
<< ": " << xstrerror());
352 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: hello write test failed");
359 * This double-dup stuff avoids problems when one of
360 * crfd, cwfd, or debug_log are in the rage 0-2.
364 /* First make sure 0-2 is occupied by something. Gets cleaned up later */
367 } while (x
< 3 && x
> -1);
375 t3
= dup(fileno(debug_log
));
377 assert(t1
> 2 && t2
> 2 && t3
> 2);
383 close(fileno(debug_log
));
397 /* Make sure all other filedescriptors are closed */
398 for (x
= 3; x
< SQUID_MAXFD
; ++x
)
406 execvp(prog
, (char *const *) args
);
408 debug_log
= fdopen(2, "a+");
410 debugs(54, DBG_CRITICAL
, "ipcCreate: " << prog
<< ": " << xstrerror());