]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/unlinkd.cc
5 * DEBUG: section 02 Unlink Daemon
6 * AUTHOR: Duane Wessels
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
36 #include "squid-old.h"
37 #include "SquidTime.h"
38 #include "StatCounters.h"
43 /* This code gets linked to Squid */
45 static int unlinkd_wfd
= -1;
46 static int unlinkd_rfd
= -1;
51 #define UNLINKD_QUEUE_LIMIT 20
54 unlinkdUnlink(const char *path
)
59 static int queuelen
= 0;
61 if (unlinkd_wfd
< 0) {
62 debug_trap("unlinkdUnlink: unlinkd_wfd < 0");
68 * If the queue length is greater than our limit, then we pause
69 * for a small amount of time, hoping that unlinkd has some
70 * feedback for us. Maybe it just needs a slice of the CPU's
73 if (queuelen
>= UNLINKD_QUEUE_LIMIT
) {
74 #if defined(USE_EPOLL) || defined(USE_KQUEUE) || defined(USE_DEVPOLL)
77 * We can't use fd_set when using epoll() or kqueue(). In
78 * these cases we block for 10 ms.
84 * When we can use select, block for up to 100 ms.
89 FD_SET(unlinkd_rfd
, &R
);
92 select(unlinkd_rfd
+ 1, &R
, NULL
, NULL
, &to
);
97 * If there is at least one outstanding unlink request, then
98 * try to read a response. If there's nothing to read we'll
99 * get an EWOULDBLOCK or whatever. If we get a response, then
100 * decrement the queue size by the number of newlines read.
106 bytes_read
= read(unlinkd_rfd
, rbuf
, 511);
108 if (bytes_read
> 0) {
109 rbuf
[bytes_read
] = '\0';
111 for (i
= 0; i
< bytes_read
; ++i
)
115 assert(queuelen
>= 0);
120 assert(l
< MAXPATHLEN
);
121 xstrncpy(buf
, path
, MAXPATHLEN
);
124 bytes_written
= write(unlinkd_wfd
, buf
, l
);
126 if (bytes_written
< 0) {
127 debugs(2, 1, "unlinkdUnlink: write FD " << unlinkd_wfd
<< " failed: " << xstrerror());
130 } else if (bytes_written
!= l
) {
131 debugs(2, 1, "unlinkdUnlink: FD " << unlinkd_wfd
<< " only wrote " << bytes_written
<< " of " << l
<< " bytes");
136 ++statCounter
.unlink
.requests
;
138 * Increment this syscalls counter here, even though the syscall
139 * is executed by the helper process. We try to be consistent
140 * in counting unlink operations.
142 ++statCounter
.syscalls
.disk
.unlinks
;
151 if (unlinkd_wfd
> -1) {
152 debugs(2, 1, "Closing unlinkd pipe on FD " << unlinkd_wfd
);
153 shutdown(unlinkd_wfd
, SD_BOTH
);
154 comm_close(unlinkd_wfd
);
156 if (unlinkd_wfd
!= unlinkd_rfd
)
157 comm_close(unlinkd_rfd
);
165 if (WaitForSingleObject(hIpc
, 5000) != WAIT_OBJECT_0
) {
167 debugs(2, 1, "unlinkdClose: WARNING: (unlinkd," << pid
<< "d) didn't exit in 5 seconds");
179 debugs(2, 1, "Closing unlinkd pipe on FD " << unlinkd_wfd
);
181 file_close(unlinkd_wfd
);
183 if (unlinkd_wfd
!= unlinkd_rfd
)
184 file_close(unlinkd_rfd
);
196 // we should start unlinkd if there are any cache_dirs using it
197 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
198 const RefCount
<SwapDir
> sd
= Config
.cacheSwap
.swapDirs
[i
];
199 if (sd
->unlinkdUseful())
209 if (unlinkd_wfd
>= 0)
210 return; // unlinkd already started
213 Ip::Address localhost
;
215 args
[0] = "(unlinkd)";
217 localhost
.SetLocalhost();
220 #if USE_POLL && _SQUID_OSF_
221 /* pipes and poll() don't get along on DUNIX -DW */
224 /* select() will fail on a pipe */
227 /* We currently need to use FIFO.. see below */
230 Config
.Program
.unlinkd
,
239 fatal("Failed to create unlinkd subprocess");
243 fd_note(unlinkd_wfd
, "squid -> unlinkd");
245 fd_note(unlinkd_rfd
, "unlinkd -> squid");
247 commUnsetFdTimeout(unlinkd_rfd
);
248 commUnsetFdTimeout(unlinkd_wfd
);
251 * unlinkd_rfd should already be non-blocking because of
252 * ipcCreate. We change unlinkd_wfd to blocking mode because
253 * we never want to lose an unlink request, and we don't have
254 * code to retry if we get EWOULDBLOCK. Unfortunately, we can
255 * do this only for the IPC_FIFO case.
257 assert(fd_table
[unlinkd_rfd
].flags
.nonblocking
);
259 if (FD_PIPE
== fd_table
[unlinkd_wfd
].type
)
260 commUnsetNonBlocking(unlinkd_wfd
);
262 debugs(2, 1, "Unlinkd pipe opened on FD " << unlinkd_wfd
);
266 debugs(2, 4, "Unlinkd handle: 0x" << std::hex
<< hIpc
<< std::dec
<< ", PID: " << pid
);