]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/unlinkd.cc
2 * DEBUG: section 02 Unlink Daemon
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.
43 #include "SquidTime.h"
44 #include "StatCounters.h"
48 /* This code gets linked to Squid */
50 static int unlinkd_wfd
= -1;
51 static int unlinkd_rfd
= -1;
56 #define UNLINKD_QUEUE_LIMIT 20
59 unlinkdUnlink(const char *path
)
64 static int queuelen
= 0;
66 if (unlinkd_wfd
< 0) {
67 debug_trap("unlinkdUnlink: unlinkd_wfd < 0");
73 * If the queue length is greater than our limit, then we pause
74 * for a small amount of time, hoping that unlinkd has some
75 * feedback for us. Maybe it just needs a slice of the CPU's
78 if (queuelen
>= UNLINKD_QUEUE_LIMIT
) {
79 #if defined(USE_EPOLL) || defined(USE_KQUEUE) || defined(USE_DEVPOLL)
82 * We can't use fd_set when using epoll() or kqueue(). In
83 * these cases we block for 10 ms.
89 * When we can use select, block for up to 100 ms.
94 FD_SET(unlinkd_rfd
, &R
);
97 select(unlinkd_rfd
+ 1, &R
, NULL
, NULL
, &to
);
102 * If there is at least one outstanding unlink request, then
103 * try to read a response. If there's nothing to read we'll
104 * get an EWOULDBLOCK or whatever. If we get a response, then
105 * decrement the queue size by the number of newlines read.
111 bytes_read
= read(unlinkd_rfd
, rbuf
, 511);
113 if (bytes_read
> 0) {
114 rbuf
[bytes_read
] = '\0';
116 for (i
= 0; i
< bytes_read
; ++i
)
120 assert(queuelen
>= 0);
125 assert(l
< MAXPATHLEN
);
126 xstrncpy(buf
, path
, MAXPATHLEN
);
129 bytes_written
= write(unlinkd_wfd
, buf
, l
);
131 if (bytes_written
< 0) {
132 debugs(2, DBG_IMPORTANT
, "unlinkdUnlink: write FD " << unlinkd_wfd
<< " failed: " << xstrerror());
135 } else if (bytes_written
!= l
) {
136 debugs(2, DBG_IMPORTANT
, "unlinkdUnlink: FD " << unlinkd_wfd
<< " only wrote " << bytes_written
<< " of " << l
<< " bytes");
141 ++statCounter
.unlink
.requests
;
143 * Increment this syscalls counter here, even though the syscall
144 * is executed by the helper process. We try to be consistent
145 * in counting unlink operations.
147 ++statCounter
.syscalls
.disk
.unlinks
;
156 if (unlinkd_wfd
> -1) {
157 debugs(2, DBG_IMPORTANT
, "Closing unlinkd pipe on FD " << unlinkd_wfd
);
158 shutdown(unlinkd_wfd
, SD_BOTH
);
159 comm_close(unlinkd_wfd
);
161 if (unlinkd_wfd
!= unlinkd_rfd
)
162 comm_close(unlinkd_rfd
);
170 if (WaitForSingleObject(hIpc
, 5000) != WAIT_OBJECT_0
) {
172 debugs(2, DBG_IMPORTANT
, "unlinkdClose: WARNING: (unlinkd," << pid
<< "d) didn't exit in 5 seconds");
184 debugs(2, DBG_IMPORTANT
, "Closing unlinkd pipe on FD " << unlinkd_wfd
);
186 file_close(unlinkd_wfd
);
188 if (unlinkd_wfd
!= unlinkd_rfd
)
189 file_close(unlinkd_rfd
);
201 // we should start unlinkd if there are any cache_dirs using it
202 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
203 const RefCount
<SwapDir
> sd
= Config
.cacheSwap
.swapDirs
[i
];
204 if (sd
->unlinkdUseful())
214 if (unlinkd_wfd
>= 0)
215 return; // unlinkd already started
218 Ip::Address localhost
;
220 args
[0] = "(unlinkd)";
222 localhost
.SetLocalhost();
225 #if USE_POLL && _SQUID_OSF_
226 /* pipes and poll() don't get along on DUNIX -DW */
229 /* select() will fail on a pipe */
232 /* We currently need to use FIFO.. see below */
235 Config
.Program
.unlinkd
,
244 fatal("Failed to create unlinkd subprocess");
248 fd_note(unlinkd_wfd
, "squid -> unlinkd");
250 fd_note(unlinkd_rfd
, "unlinkd -> squid");
252 commUnsetFdTimeout(unlinkd_rfd
);
253 commUnsetFdTimeout(unlinkd_wfd
);
256 * unlinkd_rfd should already be non-blocking because of
257 * ipcCreate. We change unlinkd_wfd to blocking mode because
258 * we never want to lose an unlink request, and we don't have
259 * code to retry if we get EWOULDBLOCK. Unfortunately, we can
260 * do this only for the IPC_FIFO case.
262 assert(fd_table
[unlinkd_rfd
].flags
.nonblocking
);
264 if (FD_PIPE
== fd_table
[unlinkd_wfd
].type
)
265 commUnsetNonBlocking(unlinkd_wfd
);
267 debugs(2, DBG_IMPORTANT
, "Unlinkd pipe opened on FD " << unlinkd_wfd
);
271 debugs(2, 4, "Unlinkd handle: 0x" << std::hex
<< hIpc
<< std::dec
<< ", PID: " << pid
);
276 #endif /* USE_UNLINKD */