]>
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"
36 #include "ip/Address.h"
39 static const char *hello_string
= "hi there\n";
40 #define HELLO_BUF_SZ 32
41 static char hello_buf
[HELLO_BUF_SZ
];
44 ipcCloseAllFD(int prfd
, int pwfd
, int crfd
, int cwfd
)
69 env_str
= (char *)xcalloc((tmp_s
= strlen(Debug::debugOptions
) + 32), 1);
70 snprintf(env_str
, tmp_s
, "SQUID_DEBUG=%s", Debug::debugOptions
);
76 ipcCreate(int type
, const char *prog
, const char *const args
[], const char *name
, Ip::Address
&local_addr
, int *rfd
, int *wfd
, void **hIpc
)
81 struct addrinfo
*AI
= NULL
;
90 #if USE_POLL && _SQUID_OSF_
91 assert(type
!= IPC_FIFO
);
103 if (type
== IPC_TCP_SOCKET
) {
104 crfd
= cwfd
= comm_open(SOCK_STREAM
,
109 prfd
= pwfd
= comm_open(SOCK_STREAM
,
114 } else if (type
== IPC_UDP_SOCKET
) {
115 crfd
= cwfd
= comm_open(SOCK_DGRAM
,
120 prfd
= pwfd
= comm_open(SOCK_DGRAM
,
125 } else if (type
== IPC_FIFO
) {
130 debugs(54, 0, "ipcCreate: pipe: " << xstrerror());
131 return -1; // maybe ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
133 fd_open(prfd
= p2c
[0], FD_PIPE
, "IPC FIFO Parent Read");
134 fd_open(cwfd
= p2c
[1], FD_PIPE
, "IPC FIFO Child Write");
137 debugs(54, 0, "ipcCreate: pipe: " << xstrerror());
138 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
140 fd_open(crfd
= c2p
[0], FD_PIPE
, "IPC FIFO Child Read");
141 fd_open(pwfd
= c2p
[1], FD_PIPE
, "IPC FIFO Parent Write");
143 #if HAVE_SOCKETPAIR && defined(AF_UNIX)
145 } else if (type
== IPC_UNIX_STREAM
) {
149 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
) < 0) {
150 debugs(54, 0, "ipcCreate: socketpair: " << xstrerror());
154 setsockopt(fds
[0], SOL_SOCKET
, SO_SNDBUF
, (void *) &buflen
, sizeof(buflen
));
155 setsockopt(fds
[0], SOL_SOCKET
, SO_RCVBUF
, (void *) &buflen
, sizeof(buflen
));
156 setsockopt(fds
[1], SOL_SOCKET
, SO_SNDBUF
, (void *) &buflen
, sizeof(buflen
));
157 setsockopt(fds
[1], SOL_SOCKET
, SO_RCVBUF
, (void *) &buflen
, sizeof(buflen
));
158 fd_open(prfd
= pwfd
= fds
[0], FD_PIPE
, "IPC UNIX STREAM Parent");
159 fd_open(crfd
= cwfd
= fds
[1], FD_PIPE
, "IPC UNIX STREAM Parent");
160 } else if (type
== IPC_UNIX_DGRAM
) {
163 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, fds
) < 0) {
164 debugs(54, 0, "ipcCreate: socketpair: " << xstrerror());
168 fd_open(prfd
= pwfd
= fds
[0], FD_PIPE
, "IPC UNIX DGRAM Parent");
169 fd_open(crfd
= cwfd
= fds
[1], FD_PIPE
, "IPC UNIX DGRAM Parent");
176 debugs(54, 3, "ipcCreate: prfd FD " << prfd
);
177 debugs(54, 3, "ipcCreate: pwfd FD " << pwfd
);
178 debugs(54, 3, "ipcCreate: crfd FD " << crfd
);
179 debugs(54, 3, "ipcCreate: cwfd FD " << cwfd
);
182 debugs(54, 0, "ipcCreate: Failed to create child FD.");
183 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
187 debugs(54, 0, "ipcCreate: Failed to create server FD.");
188 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
191 if (type
== IPC_TCP_SOCKET
|| type
== IPC_UDP_SOCKET
) {
192 PaS
.InitAddrInfo(AI
);
194 if (getsockname(pwfd
, AI
->ai_addr
, &AI
->ai_addrlen
) < 0) {
195 PaS
.FreeAddrInfo(AI
);
196 debugs(54, 0, "ipcCreate: getsockname: " << xstrerror());
197 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
202 debugs(54, 3, "ipcCreate: FD " << pwfd
<< " sockaddr " << PaS
);
204 PaS
.FreeAddrInfo(AI
);
206 ChS
.InitAddrInfo(AI
);
208 if (getsockname(crfd
, AI
->ai_addr
, &AI
->ai_addrlen
) < 0) {
209 ChS
.FreeAddrInfo(AI
);
210 debugs(54, 0, "ipcCreate: getsockname: " << xstrerror());
211 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
216 ChS
.FreeAddrInfo(AI
);
218 debugs(54, 3, "ipcCreate: FD " << crfd
<< " sockaddr " << ChS
);
222 if (type
== IPC_TCP_SOCKET
) {
223 if (listen(crfd
, 1) < 0) {
224 debugs(54, 1, "ipcCreate: listen FD " << crfd
<< ": " << xstrerror());
225 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
228 debugs(54, 3, "ipcCreate: FD " << crfd
<< " listening...");
231 /* flush or else we get dup data if unbuffered_logs is set */
234 if ((pid
= fork()) < 0) {
235 debugs(54, 1, "ipcCreate: fork: " << xstrerror());
236 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
239 if (pid
> 0) { /* parent */
240 /* close shared socket with child */
248 if (type
== IPC_TCP_SOCKET
|| type
== IPC_UDP_SOCKET
) {
249 if (comm_connect_addr(pwfd
, ChS
) == COMM_ERROR
)
250 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
253 memset(hello_buf
, '\0', HELLO_BUF_SZ
);
255 if (type
== IPC_UDP_SOCKET
)
256 x
= comm_udp_recv(prfd
, hello_buf
, HELLO_BUF_SZ
- 1, 0);
258 x
= read(prfd
, hello_buf
, HELLO_BUF_SZ
- 1);
261 debugs(54, 0, "ipcCreate: PARENT: hello read test failed");
262 debugs(54, 0, "--> read: " << xstrerror());
263 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
264 } else if (strcmp(hello_buf
, hello_string
)) {
265 debugs(54, 0, "ipcCreate: PARENT: hello read test failed");
266 debugs(54, 0, "--> read returned " << x
);
267 debugs(54, 0, "--> got '" << rfc1738_escape(hello_buf
) << "'");
268 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
271 commSetTimeout(prfd
, -1, NULL
, NULL
);
272 commSetNonBlocking(prfd
);
273 commSetNonBlocking(pwfd
);
281 fd_table
[prfd
].flags
.ipc
= 1;
283 fd_table
[pwfd
].flags
.ipc
= 1;
285 if (Config
.sleep_after_fork
) {
286 /* XXX emulation of usleep() */
289 sl
.tv_sec
= Config
.sleep_after_fork
/ 1000000;
290 sl
.tv_usec
= Config
.sleep_after_fork
% 1000000;
291 select(0, NULL
, NULL
, NULL
, &sl
);
298 no_suid(); /* give up extra priviliges */
300 /* close shared socket with parent */
308 if (type
== IPC_TCP_SOCKET
) {
309 debugs(54, 3, "ipcCreate: calling accept on FD " << crfd
);
311 if ((fd
= accept(crfd
, NULL
, NULL
)) < 0) {
312 debugs(54, 0, "ipcCreate: FD " << crfd
<< " accept: " << xstrerror());
316 debugs(54, 3, "ipcCreate: CHILD accepted new FD " << fd
);
319 } else if (type
== IPC_UDP_SOCKET
) {
320 if (comm_connect_addr(crfd
, PaS
) == COMM_ERROR
)
321 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
324 if (type
== IPC_UDP_SOCKET
) {
325 x
= comm_udp_send(cwfd
, hello_string
, strlen(hello_string
) + 1, 0);
328 debugs(54, 0, "sendto FD " << cwfd
<< ": " << xstrerror());
329 debugs(54, 0, "ipcCreate: CHILD: hello write test failed");
333 if (write(cwfd
, hello_string
, strlen(hello_string
) + 1) < 0) {
334 debugs(54, 0, "write FD " << cwfd
<< ": " << xstrerror());
335 debugs(54, 0, "ipcCreate: CHILD: hello write test failed");
342 * This double-dup stuff avoids problems when one of
343 * crfd, cwfd, or debug_log are in the rage 0-2.
347 /* First make sure 0-2 is occupied by something. Gets cleaned up later */
350 } while (x
< 3 && x
> -1);
358 t3
= dup(fileno(debug_log
));
360 assert(t1
> 2 && t2
> 2 && t3
> 2);
366 close(fileno(debug_log
));
380 /* Make sure all other filedescriptors are closed */
381 for (x
= 3; x
< SQUID_MAXFD
; x
++)
389 execvp(prog
, (char *const *) args
);
391 debug_log
= fdopen(2, "a+");
393 debugs(54, 0, "ipcCreate: " << prog
<< ": " << xstrerror());