]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc.cc
2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 54 Interprocess Communication */
12 #include "comm/Connection.h"
16 #include "ip/Address.h"
19 #include "SquidConfig.h"
23 static const char *hello_string
= "hi there\n";
25 #define HELLO_BUF_SZ 32
27 static char hello_buf
[HELLO_BUF_SZ
];
30 ipcCloseAllFD(int prfd
, int pwfd
, int crfd
, int cwfd
)
55 env_str
= (char *)xcalloc((tmp_s
= strlen(Debug::debugOptions
) + 32), 1);
56 snprintf(env_str
, tmp_s
, "SQUID_DEBUG=%s", Debug::debugOptions
);
62 ipcCreate(int type
, const char *prog
, const char *const args
[], const char *name
, Ip::Address
&local_addr
, int *rfd
, int *wfd
, void **hIpc
)
67 struct addrinfo
*AI
= NULL
;
77 #if USE_POLL && _SQUID_OSF_
78 assert(type
!= IPC_FIFO
);
90 // NP: no wrapping around d and c usage since we *want* code expansion
91 #define IPC_CHECK_FAIL(f,d,c) \
93 debugs(54, DBG_CRITICAL, "ERROR: Failed to create helper " d " FD: " << c); \
94 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd); \
97 if (type
== IPC_TCP_SOCKET
) {
98 crfd
= cwfd
= comm_open(SOCK_STREAM
,
103 prfd
= pwfd
= comm_open(SOCK_STREAM
,
108 IPC_CHECK_FAIL(crfd
, "child read", "TCP " << local_addr
);
109 IPC_CHECK_FAIL(prfd
, "parent read", "TCP " << local_addr
);
110 } else if (type
== IPC_UDP_SOCKET
) {
111 crfd
= cwfd
= comm_open(SOCK_DGRAM
,
116 prfd
= pwfd
= comm_open(SOCK_DGRAM
,
121 IPC_CHECK_FAIL(crfd
, "child read", "UDP" << local_addr
);
122 IPC_CHECK_FAIL(prfd
, "parent read", "UDP" << local_addr
);
123 } else if (type
== IPC_FIFO
) {
129 debugs(54, DBG_CRITICAL
, "ipcCreate: pipe: " << xstrerr(xerrno
));
130 return -1; // maybe ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
132 fd_open(prfd
= p2c
[0], FD_PIPE
, "IPC FIFO Parent Read");
133 fd_open(cwfd
= p2c
[1], FD_PIPE
, "IPC FIFO Child Write");
137 debugs(54, DBG_CRITICAL
, "ipcCreate: pipe: " << xstrerr(xerrno
));
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 IPC_CHECK_FAIL(crfd
, "child read", "FIFO pipe");
144 IPC_CHECK_FAIL(prfd
, "parent read", "FIFO pipe");
146 #if HAVE_SOCKETPAIR && defined(AF_UNIX)
148 } else if (type
== IPC_UNIX_STREAM
) {
152 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
) < 0) {
154 debugs(54, DBG_CRITICAL
, "ipcCreate: socketpair: " << xstrerr(xerrno
));
159 if (setsockopt(fds
[0], SOL_SOCKET
, SO_SNDBUF
, (void *) &buflen
, sizeof(buflen
)) == -1) {
161 debugs(54, DBG_IMPORTANT
, "setsockopt failed: " << xstrerr(xerrno
));
164 if (setsockopt(fds
[0], SOL_SOCKET
, SO_RCVBUF
, (void *) &buflen
, sizeof(buflen
)) == -1) {
166 debugs(54, DBG_IMPORTANT
, "setsockopt failed: " << xstrerr(xerrno
));
169 if (setsockopt(fds
[1], SOL_SOCKET
, SO_SNDBUF
, (void *) &buflen
, sizeof(buflen
)) == -1) {
171 debugs(54, DBG_IMPORTANT
, "setsockopt failed: " << xstrerr(xerrno
));
174 if (setsockopt(fds
[1], SOL_SOCKET
, SO_RCVBUF
, (void *) &buflen
, sizeof(buflen
)) == -1) {
176 debugs(54, DBG_IMPORTANT
, "setsockopt failed: " << xstrerr(xerrno
));
179 fd_open(prfd
= pwfd
= fds
[0], FD_PIPE
, "IPC UNIX STREAM Parent");
180 fd_open(crfd
= cwfd
= fds
[1], FD_PIPE
, "IPC UNIX STREAM Parent");
181 IPC_CHECK_FAIL(crfd
, "child read", "UDS socket");
182 IPC_CHECK_FAIL(prfd
, "parent read", "UDS socket");
184 } else if (type
== IPC_UNIX_DGRAM
) {
187 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, fds
) < 0) {
189 debugs(54, DBG_CRITICAL
, "ipcCreate: socketpair: " << xstrerr(xerrno
));
193 fd_open(prfd
= pwfd
= fds
[0], FD_PIPE
, "IPC UNIX DGRAM Parent");
194 fd_open(crfd
= cwfd
= fds
[1], FD_PIPE
, "IPC UNIX DGRAM Parent");
196 IPC_CHECK_FAIL(crfd
, "child read", "UDS datagram");
197 IPC_CHECK_FAIL(prfd
, "parent read", "UDS datagram");
204 debugs(54, 3, "ipcCreate: prfd FD " << prfd
);
205 debugs(54, 3, "ipcCreate: pwfd FD " << pwfd
);
206 debugs(54, 3, "ipcCreate: crfd FD " << crfd
);
207 debugs(54, 3, "ipcCreate: cwfd FD " << cwfd
);
209 if (type
== IPC_TCP_SOCKET
|| type
== IPC_UDP_SOCKET
) {
210 Ip::Address::InitAddr(AI
);
212 if (getsockname(pwfd
, AI
->ai_addr
, &AI
->ai_addrlen
) < 0) {
214 Ip::Address::FreeAddr(AI
);
215 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerr(xerrno
));
216 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
221 debugs(54, 3, "ipcCreate: FD " << pwfd
<< " sockaddr " << PaS
);
223 Ip::Address::FreeAddr(AI
);
225 Ip::Address::InitAddr(AI
);
227 if (getsockname(crfd
, AI
->ai_addr
, &AI
->ai_addrlen
) < 0) {
229 Ip::Address::FreeAddr(AI
);
230 debugs(54, DBG_CRITICAL
, "ipcCreate: getsockname: " << xstrerr(xerrno
));
231 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
236 Ip::Address::FreeAddr(AI
);
238 debugs(54, 3, "ipcCreate: FD " << crfd
<< " sockaddr " << ChS
);
242 if (type
== IPC_TCP_SOCKET
) {
243 if (listen(crfd
, 1) < 0) {
245 debugs(54, DBG_IMPORTANT
, "ipcCreate: listen FD " << crfd
<< ": " << xstrerr(xerrno
));
246 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
249 debugs(54, 3, "ipcCreate: FD " << crfd
<< " listening...");
252 /* flush or else we get dup data if unbuffered_logs is set */
255 if ((pid
= fork()) < 0) {
257 debugs(54, DBG_IMPORTANT
, "ipcCreate: fork: " << xstrerr(xerrno
));
258 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
261 if (pid
> 0) { /* parent */
262 /* close shared socket with child */
270 if (type
== IPC_TCP_SOCKET
|| type
== IPC_UDP_SOCKET
) {
271 if (comm_connect_addr(pwfd
, ChS
) == Comm::COMM_ERROR
)
272 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
275 if (type
== IPC_UDP_SOCKET
)
276 x
= comm_udp_recv(prfd
, hello_buf
, sizeof(hello_buf
)-1, 0);
278 x
= read(prfd
, hello_buf
, sizeof(hello_buf
)-1);
284 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: hello read test failed");
285 debugs(54, DBG_CRITICAL
, "--> read: " << xstrerr(xerrno
));
286 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
287 } else if (strcmp(hello_buf
, hello_string
)) {
288 debugs(54, DBG_CRITICAL
, "ipcCreate: PARENT: hello read test failed");
289 debugs(54, DBG_CRITICAL
, "--> read returned " << x
);
290 debugs(54, DBG_CRITICAL
, "--> got '" << rfc1738_escape(hello_buf
) << "'");
291 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
294 commUnsetFdTimeout(prfd
);
295 commSetNonBlocking(prfd
);
296 commSetNonBlocking(pwfd
);
304 fd_table
[prfd
].flags
.ipc
= 1;
306 fd_table
[pwfd
].flags
.ipc
= 1;
308 if (Config
.sleep_after_fork
) {
309 /* XXX emulation of usleep() */
312 sl
.tv_sec
= Config
.sleep_after_fork
/ 1000000;
313 sl
.tv_usec
= Config
.sleep_after_fork
% 1000000;
314 select(0, NULL
, NULL
, NULL
, &sl
);
321 TheProcessKind
= pkHelper
;
322 no_suid(); /* give up extra priviliges */
324 /* close shared socket with parent */
332 if (type
== IPC_TCP_SOCKET
) {
333 debugs(54, 3, "ipcCreate: calling accept on FD " << crfd
);
335 if ((fd
= accept(crfd
, NULL
, NULL
)) < 0) {
337 debugs(54, DBG_CRITICAL
, "ipcCreate: FD " << crfd
<< " accept: " << xstrerr(xerrno
));
341 debugs(54, 3, "ipcCreate: CHILD accepted new FD " << fd
);
344 } else if (type
== IPC_UDP_SOCKET
) {
345 if (comm_connect_addr(crfd
, PaS
) == Comm::COMM_ERROR
)
346 return ipcCloseAllFD(prfd
, pwfd
, crfd
, cwfd
);
349 if (type
== IPC_UDP_SOCKET
) {
350 x
= comm_udp_send(cwfd
, hello_string
, strlen(hello_string
) + 1, 0);
354 debugs(54, DBG_CRITICAL
, "sendto FD " << cwfd
<< ": " << xstrerr(xerrno
));
355 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: hello write test failed");
359 if (write(cwfd
, hello_string
, strlen(hello_string
) + 1) < 0) {
361 debugs(54, DBG_CRITICAL
, "write FD " << cwfd
<< ": " << xstrerr(xerrno
));
362 debugs(54, DBG_CRITICAL
, "ipcCreate: CHILD: hello write test failed");
369 * This double-dup stuff avoids problems when one of
370 * crfd, cwfd, or debug_log are in the rage 0-2.
374 /* First make sure 0-2 is occupied by something. Gets cleaned up later */
377 } while (x
< 3 && x
> -1);
385 t3
= dup(fileno(debug_log
));
387 assert(t1
> 2 && t2
> 2 && t3
> 2);
393 close(fileno(debug_log
));
407 /* Make sure all other filedescriptors are closed */
408 for (x
= 3; x
< SQUID_MAXFD
; ++x
)
416 execvp(prog
, (char *const *) args
);
419 debug_log
= fdopen(2, "a+");
421 debugs(54, DBG_CRITICAL
, "ipcCreate: " << prog
<< ": " << xstrerr(xerrno
));