]>
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"
43 static const char *hello_string
= "hi there\n";
44 #define HELLO_BUF_SZ 32
45 static char hello_buf
[HELLO_BUF_SZ
];
48 ipcCloseAllFD(int prfd
, int pwfd
, int crfd
, int cwfd
)
73 env_str
= (char *)xcalloc((tmp_s
= strlen(Debug::debugOptions
) + 32), 1);
74 snprintf(env_str
, tmp_s
, "SQUID_DEBUG=%s", Debug::debugOptions
);
80 ipcCreate(int type
, const char *prog
, const char *const args
[], const char *name
, Ip::Address
&local_addr
, int *rfd
, int *wfd
, void **hIpc
)
85 struct addrinfo
*AI
= NULL
;
94 #if USE_POLL && _SQUID_OSF_
95 assert(type
!= IPC_FIFO
);
107 // NP: no wrapping around d and c usage since we *want* code expansion
108 #define IPC_CHECK_FAIL(f,d,c) \
110 debugs(54, DBG_CRITICAL, "ERROR: Failed to create helper " d " FD: " << c); \
111 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd); \
114 if (type
== IPC_TCP_SOCKET
) {
115 crfd
= cwfd
= comm_open(SOCK_STREAM
,
120 prfd
= pwfd
= comm_open(SOCK_STREAM
,
125 IPC_CHECK_FAIL(crfd
, "child read", "TCP " << local_addr
);
126 IPC_CHECK_FAIL(prfd
, "parent read", "TCP " << local_addr
);
127 } else if (type
== IPC_UDP_SOCKET
) {
128 crfd
= cwfd
= comm_open(SOCK_DGRAM
,
133 prfd
= pwfd
= comm_open(SOCK_DGRAM
,
138 IPC_CHECK_FAIL(crfd
, "child read", "UDP" << local_addr
);
139 IPC_CHECK_FAIL(prfd
, "parent read", "UDP" << local_addr
);
140 } else if (type
== IPC_FIFO
) {
145 debugs(54, DBG_CRITICAL
, "ipcCreate: pipe: " << xstrerror());
146 return -1; // maybe ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
148 fd_open(prfd
= p2c
[0], FD_PIPE
, "IPC FIFO Parent Read");
149 fd_open(cwfd
= p2c
[1], FD_PIPE
, "IPC FIFO Child Write");
152 debugs(54, DBG_CRITICAL
, "ipcCreate: pipe: " << xstrerror());
153 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
155 fd_open(crfd
= c2p
[0], FD_PIPE
, "IPC FIFO Child Read");
156 fd_open(pwfd
= c2p
[1], FD_PIPE
, "IPC FIFO Parent Write");
158 IPC_CHECK_FAIL(crfd
, "child read", "FIFO pipe");
159 IPC_CHECK_FAIL(prfd
, "parent read", "FIFO pipe");
161 #if HAVE_SOCKETPAIR && defined(AF_UNIX)
163 } else if (type
== IPC_UNIX_STREAM
) {
167 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
) < 0) {
168 debugs(54, DBG_CRITICAL
, "ipcCreate: socketpair: " << xstrerror());
172 setsockopt(fds
[0], SOL_SOCKET
, SO_SNDBUF
, (void *) &buflen
, sizeof(buflen
));
173 setsockopt(fds
[0], SOL_SOCKET
, SO_RCVBUF
, (void *) &buflen
, sizeof(buflen
));
174 setsockopt(fds
[1], SOL_SOCKET
, SO_SNDBUF
, (void *) &buflen
, sizeof(buflen
));
175 setsockopt(fds
[1], SOL_SOCKET
, SO_RCVBUF
, (void *) &buflen
, sizeof(buflen
));
176 fd_open(prfd
= pwfd
= fds
[0], FD_PIPE
, "IPC UNIX STREAM Parent");
177 fd_open(crfd
= cwfd
= fds
[1], FD_PIPE
, "IPC UNIX STREAM Parent");
178 IPC_CHECK_FAIL(crfd
, "child read", "UDS socket");
179 IPC_CHECK_FAIL(prfd
, "parent read", "UDS socket");
181 } else if (type
== IPC_UNIX_DGRAM
) {
184 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, fds
) < 0) {
185 debugs(54, DBG_CRITICAL
, "ipcCreate: socketpair: " << xstrerror());
189 fd_open(prfd
= pwfd
= fds
[0], FD_PIPE
, "IPC UNIX DGRAM Parent");
190 fd_open(crfd
= cwfd
= fds
[1], FD_PIPE
, "IPC UNIX DGRAM Parent");
192 IPC_CHECK_FAIL(crfd
, "child read", "UDS datagram");
193 IPC_CHECK_FAIL(prfd
, "parent read", "UDS datagram");
200 debugs(54, 3, "ipcCreate: prfd FD " << prfd
);
201 debugs(54, 3, "ipcCreate: pwfd FD " << pwfd
);
202 debugs(54, 3, "ipcCreate: crfd FD " << crfd
);
203 debugs(54, 3, "ipcCreate: cwfd FD " << cwfd
);
205 if (type
== IPC_TCP_SOCKET
|| type
== IPC_UDP_SOCKET
) {
206 PaS
.InitAddrInfo(AI
);
208 if (getsockname(pwfd
, AI
->ai_addr
, &AI
->ai_addrlen
) < 0) {
209 PaS
.FreeAddrInfo(AI
);
210 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
211 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
216 debugs(54, 3, "ipcCreate: FD " << pwfd
<< " sockaddr " << PaS
);
218 PaS
.FreeAddrInfo(AI
);
220 ChS
.InitAddrInfo(AI
);
222 if (getsockname(crfd
, AI
->ai_addr
, &AI
->ai_addrlen
) < 0) {
223 ChS
.FreeAddrInfo(AI
);
224 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerror());
225 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
230 ChS
.FreeAddrInfo(AI
);
232 debugs(54, 3, "ipcCreate: FD " << crfd
<< " sockaddr " << ChS
);
236 if (type
== IPC_TCP_SOCKET
) {
237 if (listen(crfd
, 1) < 0) {
238 debugs(54, DBG_IMPORTANT
, "ipcCreate: listen FD " << crfd
<< ": " << xstrerror());
239 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
242 debugs(54, 3, "ipcCreate: FD " << crfd
<< " listening...");
245 /* flush or else we get dup data if unbuffered_logs is set */
248 if ((pid
= fork()) < 0) {
249 debugs(54, DBG_IMPORTANT
, "ipcCreate: fork: " << xstrerror());
250 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
253 if (pid
> 0) { /* parent */
254 /* close shared socket with child */
262 if (type
== IPC_TCP_SOCKET
|| type
== IPC_UDP_SOCKET
) {
263 if (comm_connect_addr(pwfd
, ChS
) == COMM_ERROR
)
264 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
267 memset(hello_buf
, '\0', HELLO_BUF_SZ
);
269 if (type
== IPC_UDP_SOCKET
)
270 x
= comm_udp_recv(prfd
, hello_buf
, HELLO_BUF_SZ
- 1, 0);
272 x
= read(prfd
, hello_buf
, HELLO_BUF_SZ
- 1);
275 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: hello read test failed");
276 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerror());
277 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
278 } else if (strcmp(hello_buf
, hello_string
)) {
279 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: hello read test failed");
280 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
281 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
282 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
285 commUnsetFdTimeout(prfd
);
286 commSetNonBlocking(prfd
);
287 commSetNonBlocking(pwfd
);
295 fd_table
[prfd
].flags
.ipc
= 1;
297 fd_table
[pwfd
].flags
.ipc
= 1;
299 if (Config
.sleep_after_fork
) {
300 /* XXX emulation of usleep() */
303 sl
.tv_sec
= Config
.sleep_after_fork
/ 1000000;
304 sl
.tv_usec
= Config
.sleep_after_fork
% 1000000;
305 select(0, NULL
, NULL
, NULL
, &sl
);
312 no_suid(); /* give up extra priviliges */
314 /* close shared socket with parent */
322 if (type
== IPC_TCP_SOCKET
) {
323 debugs(54, 3, "ipcCreate: calling accept on FD " << crfd
);
325 if ((fd
= accept(crfd
, NULL
, NULL
)) < 0) {
326 debugs(54, DBG_CRITICAL
, "ipcCreate: FD " << crfd
<< " accept: " << xstrerror());
330 debugs(54, 3, "ipcCreate: CHILD accepted new FD " << fd
);
333 } else if (type
== IPC_UDP_SOCKET
) {
334 if (comm_connect_addr(crfd
, PaS
) == COMM_ERROR
)
335 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
338 if (type
== IPC_UDP_SOCKET
) {
339 x
= comm_udp_send(cwfd
, hello_string
, strlen(hello_string
) + 1, 0);
342 debugs(54, DBG_CRITICAL
, "sendto FD " << cwfd
<< ": " << xstrerror());
343 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: hello write test failed");
347 if (write(cwfd
, hello_string
, strlen(hello_string
) + 1) < 0) {
348 debugs(54, DBG_CRITICAL
, "write FD " << cwfd
<< ": " << xstrerror());
349 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: hello write test failed");
356 * This double-dup stuff avoids problems when one of
357 * crfd, cwfd, or debug_log are in the rage 0-2.
361 /* First make sure 0-2 is occupied by something. Gets cleaned up later */
364 } while (x
< 3 && x
> -1);
372 t3
= dup(fileno(debug_log
));
374 assert(t1
> 2 && t2
> 2 && t3
> 2);
380 close(fileno(debug_log
));
394 /* Make sure all other filedescriptors are closed */
395 for (x
= 3; x
< SQUID_MAXFD
; ++x
)
403 execvp(prog
, (char *const *) args
);
405 debug_log
= fdopen(2, "a+");
407 debugs(54, DBG_CRITICAL
, "ipcCreate: " << prog
<< ": " << xstrerror());