]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc.cc
Merged from trunk
[thirdparty/squid.git] / src / ipc.cc
1 /*
2 * DEBUG: section 54 Interprocess Communication
3 * AUTHOR: Duane Wessels
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
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.
16 *
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.
21 *
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.
26 *
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.
30 *
31 */
32
33 #include "squid.h"
34 #include "comm/Connection.h"
35 #include "fd.h"
36 #include "fde.h"
37 #include "globals.h"
38 #include "ip/Address.h"
39 #include "SquidConfig.h"
40 #include "SquidIpc.h"
41 #include "tools.h"
42 #include "rfc1738.h"
43
44 static const char *hello_string = "hi there\n";
45 #define HELLO_BUF_SZ 32
46 static char hello_buf[HELLO_BUF_SZ];
47
48 static int
49 ipcCloseAllFD(int prfd, int pwfd, int crfd, int cwfd)
50 {
51 if (prfd >= 0)
52 comm_close(prfd);
53
54 if (prfd != pwfd)
55 if (pwfd >= 0)
56 comm_close(pwfd);
57
58 if (crfd >= 0)
59 comm_close(crfd);
60
61 if (crfd != cwfd)
62 if (cwfd >= 0)
63 comm_close(cwfd);
64
65 return -1;
66 }
67
68 static void
69 PutEnvironment()
70 {
71 #if HAVE_PUTENV
72 char *env_str;
73 int tmp_s;
74 env_str = (char *)xcalloc((tmp_s = strlen(Debug::debugOptions) + 32), 1);
75 snprintf(env_str, tmp_s, "SQUID_DEBUG=%s", Debug::debugOptions);
76 putenv(env_str);
77 #endif
78 }
79
80 pid_t
81 ipcCreate(int type, const char *prog, const char *const args[], const char *name, Ip::Address &local_addr, int *rfd, int *wfd, void **hIpc)
82 {
83 pid_t pid;
84 Ip::Address ChS;
85 Ip::Address PaS;
86 struct addrinfo *AI = NULL;
87 int crfd = -1;
88 int prfd = -1;
89 int cwfd = -1;
90 int pwfd = -1;
91 int fd;
92 int t1, t2, t3;
93 int x;
94
95 #if USE_POLL && _SQUID_OSF_
96 assert(type != IPC_FIFO);
97 #endif
98
99 if (rfd)
100 *rfd = -1;
101
102 if (wfd)
103 *wfd = -1;
104
105 if (hIpc)
106 *hIpc = NULL;
107
108 // NP: no wrapping around d and c usage since we *want* code expansion
109 #define IPC_CHECK_FAIL(f,d,c) \
110 if ((f) < 0) { \
111 debugs(54, DBG_CRITICAL, "ERROR: Failed to create helper " d " FD: " << c); \
112 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd); \
113 } else void(0)
114
115 if (type == IPC_TCP_SOCKET) {
116 crfd = cwfd = comm_open(SOCK_STREAM,
117 0,
118 local_addr,
119 COMM_NOCLOEXEC,
120 name);
121 prfd = pwfd = comm_open(SOCK_STREAM,
122 0, /* protocol */
123 local_addr,
124 0, /* blocking */
125 name);
126 IPC_CHECK_FAIL(crfd, "child read", "TCP " << local_addr);
127 IPC_CHECK_FAIL(prfd, "parent read", "TCP " << local_addr);
128 } else if (type == IPC_UDP_SOCKET) {
129 crfd = cwfd = comm_open(SOCK_DGRAM,
130 0,
131 local_addr,
132 COMM_NOCLOEXEC,
133 name);
134 prfd = pwfd = comm_open(SOCK_DGRAM,
135 0,
136 local_addr,
137 0,
138 name);
139 IPC_CHECK_FAIL(crfd, "child read", "UDP" << local_addr);
140 IPC_CHECK_FAIL(prfd, "parent read", "UDP" << local_addr);
141 } else if (type == IPC_FIFO) {
142 int p2c[2];
143 int c2p[2];
144
145 if (pipe(p2c) < 0) {
146 debugs(54, DBG_CRITICAL, "ipcCreate: pipe: " << xstrerror());
147 return -1; // maybe ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
148 }
149 fd_open(prfd = p2c[0], FD_PIPE, "IPC FIFO Parent Read");
150 fd_open(cwfd = p2c[1], FD_PIPE, "IPC FIFO Child Write");
151
152 if (pipe(c2p) < 0) {
153 debugs(54, DBG_CRITICAL, "ipcCreate: pipe: " << xstrerror());
154 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
155 }
156 fd_open(crfd = c2p[0], FD_PIPE, "IPC FIFO Child Read");
157 fd_open(pwfd = c2p[1], FD_PIPE, "IPC FIFO Parent Write");
158
159 IPC_CHECK_FAIL(crfd, "child read", "FIFO pipe");
160 IPC_CHECK_FAIL(prfd, "parent read", "FIFO pipe");
161
162 #if HAVE_SOCKETPAIR && defined(AF_UNIX)
163
164 } else if (type == IPC_UNIX_STREAM) {
165 int fds[2];
166 int buflen = 32768;
167
168 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
169 debugs(54, DBG_CRITICAL, "ipcCreate: socketpair: " << xstrerror());
170 return -1;
171 }
172
173 setsockopt(fds[0], SOL_SOCKET, SO_SNDBUF, (void *) &buflen, sizeof(buflen));
174 setsockopt(fds[0], SOL_SOCKET, SO_RCVBUF, (void *) &buflen, sizeof(buflen));
175 setsockopt(fds[1], SOL_SOCKET, SO_SNDBUF, (void *) &buflen, sizeof(buflen));
176 setsockopt(fds[1], SOL_SOCKET, SO_RCVBUF, (void *) &buflen, sizeof(buflen));
177 fd_open(prfd = pwfd = fds[0], FD_PIPE, "IPC UNIX STREAM Parent");
178 fd_open(crfd = cwfd = fds[1], FD_PIPE, "IPC UNIX STREAM Parent");
179 IPC_CHECK_FAIL(crfd, "child read", "UDS socket");
180 IPC_CHECK_FAIL(prfd, "parent read", "UDS socket");
181
182 } else if (type == IPC_UNIX_DGRAM) {
183 int fds[2];
184
185 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds) < 0) {
186 debugs(54, DBG_CRITICAL, "ipcCreate: socketpair: " << xstrerror());
187 return -1;
188 }
189
190 fd_open(prfd = pwfd = fds[0], FD_PIPE, "IPC UNIX DGRAM Parent");
191 fd_open(crfd = cwfd = fds[1], FD_PIPE, "IPC UNIX DGRAM Parent");
192
193 IPC_CHECK_FAIL(crfd, "child read", "UDS datagram");
194 IPC_CHECK_FAIL(prfd, "parent read", "UDS datagram");
195 #endif
196
197 } else {
198 assert(IPC_NONE);
199 }
200
201 debugs(54, 3, "ipcCreate: prfd FD " << prfd);
202 debugs(54, 3, "ipcCreate: pwfd FD " << pwfd);
203 debugs(54, 3, "ipcCreate: crfd FD " << crfd);
204 debugs(54, 3, "ipcCreate: cwfd FD " << cwfd);
205
206 if (type == IPC_TCP_SOCKET || type == IPC_UDP_SOCKET) {
207 PaS.InitAddrInfo(AI);
208
209 if (getsockname(pwfd, AI->ai_addr, &AI->ai_addrlen) < 0) {
210 PaS.FreeAddrInfo(AI);
211 debugs(54, DBG_CRITICAL, "ipcCreate: getsockname: " << xstrerror());
212 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
213 }
214
215 PaS = *AI;
216
217 debugs(54, 3, "ipcCreate: FD " << pwfd << " sockaddr " << PaS);
218
219 PaS.FreeAddrInfo(AI);
220
221 ChS.InitAddrInfo(AI);
222
223 if (getsockname(crfd, AI->ai_addr, &AI->ai_addrlen) < 0) {
224 ChS.FreeAddrInfo(AI);
225 debugs(54, DBG_CRITICAL, "ipcCreate: getsockname: " << xstrerror());
226 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
227 }
228
229 ChS = *AI;
230
231 ChS.FreeAddrInfo(AI);
232
233 debugs(54, 3, "ipcCreate: FD " << crfd << " sockaddr " << ChS );
234
235 }
236
237 if (type == IPC_TCP_SOCKET) {
238 if (listen(crfd, 1) < 0) {
239 debugs(54, DBG_IMPORTANT, "ipcCreate: listen FD " << crfd << ": " << xstrerror());
240 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
241 }
242
243 debugs(54, 3, "ipcCreate: FD " << crfd << " listening...");
244 }
245
246 /* flush or else we get dup data if unbuffered_logs is set */
247 logsFlush();
248
249 if ((pid = fork()) < 0) {
250 debugs(54, DBG_IMPORTANT, "ipcCreate: fork: " << xstrerror());
251 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
252 }
253
254 if (pid > 0) { /* parent */
255 /* close shared socket with child */
256 comm_close(crfd);
257
258 if (cwfd != crfd)
259 comm_close(cwfd);
260
261 cwfd = crfd = -1;
262
263 if (type == IPC_TCP_SOCKET || type == IPC_UDP_SOCKET) {
264 if (comm_connect_addr(pwfd, ChS) == COMM_ERROR)
265 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
266 }
267
268 memset(hello_buf, '\0', HELLO_BUF_SZ);
269
270 if (type == IPC_UDP_SOCKET)
271 x = comm_udp_recv(prfd, hello_buf, HELLO_BUF_SZ - 1, 0);
272 else
273 x = read(prfd, hello_buf, HELLO_BUF_SZ - 1);
274
275 if (x < 0) {
276 debugs(54, DBG_CRITICAL, "ipcCreate: PARENT: hello read test failed");
277 debugs(54, DBG_CRITICAL, "--> read: " << xstrerror());
278 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
279 } else if (strcmp(hello_buf, hello_string)) {
280 debugs(54, DBG_CRITICAL, "ipcCreate: PARENT: hello read test failed");
281 debugs(54, DBG_CRITICAL, "--> read returned " << x);
282 debugs(54, DBG_CRITICAL, "--> got '" << rfc1738_escape(hello_buf) << "'");
283 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
284 }
285
286 commUnsetFdTimeout(prfd);
287 commSetNonBlocking(prfd);
288 commSetNonBlocking(pwfd);
289
290 if (rfd)
291 *rfd = prfd;
292
293 if (wfd)
294 *wfd = pwfd;
295
296 fd_table[prfd].flags.ipc = 1;
297
298 fd_table[pwfd].flags.ipc = 1;
299
300 if (Config.sleep_after_fork) {
301 /* XXX emulation of usleep() */
302
303 struct timeval sl;
304 sl.tv_sec = Config.sleep_after_fork / 1000000;
305 sl.tv_usec = Config.sleep_after_fork % 1000000;
306 select(0, NULL, NULL, NULL, &sl);
307 }
308
309 return pid;
310 }
311
312 /* child */
313 no_suid(); /* give up extra priviliges */
314
315 /* close shared socket with parent */
316 close(prfd);
317
318 if (pwfd != prfd)
319 close(pwfd);
320
321 pwfd = prfd = -1;
322
323 if (type == IPC_TCP_SOCKET) {
324 debugs(54, 3, "ipcCreate: calling accept on FD " << crfd);
325
326 if ((fd = accept(crfd, NULL, NULL)) < 0) {
327 debugs(54, DBG_CRITICAL, "ipcCreate: FD " << crfd << " accept: " << xstrerror());
328 _exit(1);
329 }
330
331 debugs(54, 3, "ipcCreate: CHILD accepted new FD " << fd);
332 close(crfd);
333 cwfd = crfd = fd;
334 } else if (type == IPC_UDP_SOCKET) {
335 if (comm_connect_addr(crfd, PaS) == COMM_ERROR)
336 return ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
337 }
338
339 if (type == IPC_UDP_SOCKET) {
340 x = comm_udp_send(cwfd, hello_string, strlen(hello_string) + 1, 0);
341
342 if (x < 0) {
343 debugs(54, DBG_CRITICAL, "sendto FD " << cwfd << ": " << xstrerror());
344 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: hello write test failed");
345 _exit(1);
346 }
347 } else {
348 if (write(cwfd, hello_string, strlen(hello_string) + 1) < 0) {
349 debugs(54, DBG_CRITICAL, "write FD " << cwfd << ": " << xstrerror());
350 debugs(54, DBG_CRITICAL, "ipcCreate: CHILD: hello write test failed");
351 _exit(1);
352 }
353 }
354
355 PutEnvironment();
356 /*
357 * This double-dup stuff avoids problems when one of
358 * crfd, cwfd, or debug_log are in the rage 0-2.
359 */
360
361 do {
362 /* First make sure 0-2 is occupied by something. Gets cleaned up later */
363 x = dup(crfd);
364 assert(x > -1);
365 } while (x < 3 && x > -1);
366
367 close(x);
368
369 t1 = dup(crfd);
370
371 t2 = dup(cwfd);
372
373 t3 = dup(fileno(debug_log));
374
375 assert(t1 > 2 && t2 > 2 && t3 > 2);
376
377 close(crfd);
378
379 close(cwfd);
380
381 close(fileno(debug_log));
382
383 dup2(t1, 0);
384
385 dup2(t2, 1);
386
387 dup2(t3, 2);
388
389 close(t1);
390
391 close(t2);
392
393 close(t3);
394
395 /* Make sure all other filedescriptors are closed */
396 for (x = 3; x < SQUID_MAXFD; ++x)
397 close(x);
398
399 #if HAVE_SETSID
400 if (opt_no_daemon)
401 setsid();
402 #endif
403
404 execvp(prog, (char *const *) args);
405
406 debug_log = fdopen(2, "a+");
407
408 debugs(54, DBG_CRITICAL, "ipcCreate: " << prog << ": " << xstrerror());
409
410 _exit(1);
411
412 return 0;
413 }