]>
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.
43 #include "SquidTime.h"
44 #include "StatCounters.h"
47 /* This code gets linked to Squid */
49 static int unlinkd_wfd
= -1;
50 static int unlinkd_rfd
= -1;
55 #define UNLINKD_QUEUE_LIMIT 20
58 unlinkdUnlink(const char *path
)
63 static int queuelen
= 0;
65 if (unlinkd_wfd
< 0) {
66 debug_trap("unlinkdUnlink: unlinkd_wfd < 0");
72 * If the queue length is greater than our limit, then we pause
73 * for a small amount of time, hoping that unlinkd has some
74 * feedback for us. Maybe it just needs a slice of the CPU's
77 if (queuelen
>= UNLINKD_QUEUE_LIMIT
) {
78 #if defined(USE_EPOLL) || defined(USE_KQUEUE) || defined(USE_DEVPOLL)
81 * We can't use fd_set when using epoll() or kqueue(). In
82 * these cases we block for 10 ms.
88 * When we can use select, block for up to 100 ms.
93 FD_SET(unlinkd_rfd
, &R
);
96 select(unlinkd_rfd
+ 1, &R
, NULL
, NULL
, &to
);
101 * If there is at least one outstanding unlink request, then
102 * try to read a response. If there's nothing to read we'll
103 * get an EWOULDBLOCK or whatever. If we get a response, then
104 * decrement the queue size by the number of newlines read.
110 bytes_read
= read(unlinkd_rfd
, rbuf
, 511);
112 if (bytes_read
> 0) {
113 rbuf
[bytes_read
] = '\0';
115 for (i
= 0; i
< bytes_read
; ++i
)
119 assert(queuelen
>= 0);
124 assert(l
< MAXPATHLEN
);
125 xstrncpy(buf
, path
, MAXPATHLEN
);
128 bytes_written
= write(unlinkd_wfd
, buf
, l
);
130 if (bytes_written
< 0) {
131 debugs(2, DBG_IMPORTANT
, "unlinkdUnlink: write FD " << unlinkd_wfd
<< " failed: " << xstrerror());
134 } else if (bytes_written
!= l
) {
135 debugs(2, DBG_IMPORTANT
, "unlinkdUnlink: FD " << unlinkd_wfd
<< " only wrote " << bytes_written
<< " of " << l
<< " bytes");
140 ++statCounter
.unlink
.requests
;
142 * Increment this syscalls counter here, even though the syscall
143 * is executed by the helper process. We try to be consistent
144 * in counting unlink operations.
146 ++statCounter
.syscalls
.disk
.unlinks
;
155 if (unlinkd_wfd
> -1) {
156 debugs(2, DBG_IMPORTANT
, "Closing unlinkd pipe on FD " << unlinkd_wfd
);
157 shutdown(unlinkd_wfd
, SD_BOTH
);
158 comm_close(unlinkd_wfd
);
160 if (unlinkd_wfd
!= unlinkd_rfd
)
161 comm_close(unlinkd_rfd
);
169 if (WaitForSingleObject(hIpc
, 5000) != WAIT_OBJECT_0
) {
171 debugs(2, DBG_IMPORTANT
, "unlinkdClose: WARNING: (unlinkd," << pid
<< "d) didn't exit in 5 seconds");
183 debugs(2, DBG_IMPORTANT
, "Closing unlinkd pipe on FD " << unlinkd_wfd
);
185 file_close(unlinkd_wfd
);
187 if (unlinkd_wfd
!= unlinkd_rfd
)
188 file_close(unlinkd_rfd
);
200 // we should start unlinkd if there are any cache_dirs using it
201 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
202 const RefCount
<SwapDir
> sd
= Config
.cacheSwap
.swapDirs
[i
];
203 if (sd
->unlinkdUseful())
213 if (unlinkd_wfd
>= 0)
214 return; // unlinkd already started
217 Ip::Address localhost
;
219 args
[0] = "(unlinkd)";
221 localhost
.SetLocalhost();
224 #if USE_POLL && _SQUID_OSF_
225 /* pipes and poll() don't get along on DUNIX -DW */
228 /* select() will fail on a pipe */
231 /* We currently need to use FIFO.. see below */
234 Config
.Program
.unlinkd
,
243 fatal("Failed to create unlinkd subprocess");
247 fd_note(unlinkd_wfd
, "squid -> unlinkd");
249 fd_note(unlinkd_rfd
, "unlinkd -> squid");
251 commUnsetFdTimeout(unlinkd_rfd
);
252 commUnsetFdTimeout(unlinkd_wfd
);
255 * unlinkd_rfd should already be non-blocking because of
256 * ipcCreate. We change unlinkd_wfd to blocking mode because
257 * we never want to lose an unlink request, and we don't have
258 * code to retry if we get EWOULDBLOCK. Unfortunately, we can
259 * do this only for the IPC_FIFO case.
261 assert(fd_table
[unlinkd_rfd
].flags
.nonblocking
);
263 if (FD_PIPE
== fd_table
[unlinkd_wfd
].type
)
264 commUnsetNonBlocking(unlinkd_wfd
);
266 debugs(2, DBG_IMPORTANT
, "Unlinkd pipe opened on FD " << unlinkd_wfd
);
270 debugs(2, 4, "Unlinkd handle: 0x" << std::hex
<< hIpc
<< std::dec
<< ", PID: " << pid
);