]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/unlinkd.cc
5 * DEBUG: section 2 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.
37 #include "SquidTime.h"
41 /* This code gets linked to Squid */
43 static int unlinkd_wfd
= -1;
44 static int unlinkd_rfd
= -1;
49 #define UNLINKD_QUEUE_LIMIT 20
52 unlinkdUnlink(const char *path
)
57 static int queuelen
= 0;
59 if (unlinkd_wfd
< 0) {
60 debug_trap("unlinkdUnlink: unlinkd_wfd < 0");
66 * If the queue length is greater than our limit, then we pause
67 * for a small amount of time, hoping that unlinkd has some
68 * feedback for us. Maybe it just needs a slice of the CPU's
71 if (queuelen
>= UNLINKD_QUEUE_LIMIT
) {
72 #if defined(USE_EPOLL) || defined(USE_KQUEUE)
75 * We can't use fd_set when using epoll() or kqueue(). In
76 * these cases we block for 10 ms.
82 * When we can use select, block for up to 100 ms.
87 FD_SET(unlinkd_rfd
, &R
);
90 select(unlinkd_rfd
+ 1, &R
, NULL
, NULL
, &to
);
95 * If there is at least one outstanding unlink request, then
96 * try to read a response. If there's nothing to read we'll
97 * get an EWOULDBLOCK or whatever. If we get a response, then
98 * decrement the queue size by the number of newlines read.
104 bytes_read
= read(unlinkd_rfd
, rbuf
, 511);
106 if (bytes_read
> 0) {
107 rbuf
[bytes_read
] = '\0';
109 for (i
= 0; i
< bytes_read
; i
++)
113 assert(queuelen
>= 0);
118 assert(l
< MAXPATHLEN
);
119 xstrncpy(buf
, path
, MAXPATHLEN
);
121 bytes_written
= write(unlinkd_wfd
, buf
, l
);
123 if (bytes_written
< 0) {
124 debugs(2, 1, "unlinkdUnlink: write FD " << unlinkd_wfd
<< " failed: " << xstrerror());
127 } else if (bytes_written
!= l
) {
128 debugs(2, 1, "unlinkdUnlink: FD " << unlinkd_wfd
<< " only wrote " << bytes_written
<< " of " << l
<< " bytes");
133 statCounter
.unlink
.requests
++;
135 * Increment this syscalls counter here, even though the syscall
136 * is executed by the helper process. We try to be consistent
137 * in counting unlink operations.
139 statCounter
.syscalls
.disk
.unlinks
++;
148 if (unlinkd_wfd
> -1) {
149 debugs(2, 1, "Closing unlinkd pipe on FD " << unlinkd_wfd
);
150 shutdown(unlinkd_wfd
, SD_BOTH
);
151 comm_close(unlinkd_wfd
);
153 if (unlinkd_wfd
!= unlinkd_rfd
)
154 comm_close(unlinkd_rfd
);
160 debugs(2, 0, "unlinkdClose: WARNING: unlinkd_wfd is " << unlinkd_wfd
);
163 if (WaitForSingleObject(hIpc
, 5000) != WAIT_OBJECT_0
) {
165 debugs(2, 1, "unlinkdClose: WARNING: (unlinkd," << pid
<< "d) didn't exit in 5 seconds");
177 debugs(2, 1, "Closing unlinkd pipe on FD " << unlinkd_wfd
);
179 file_close(unlinkd_wfd
);
181 if (unlinkd_wfd
!= unlinkd_rfd
)
182 file_close(unlinkd_rfd
);
195 Ip::Address localhost
;
197 args
[0] = "(unlinkd)";
199 localhost
.SetLocalhost();
202 #if USE_POLL && defined(_SQUID_OSF_)
203 /* pipes and poll() don't get along on DUNIX -DW */
205 #elif defined(_SQUID_MSWIN_)
206 /* select() will fail on a pipe */
209 /* We currently need to use FIFO.. see below */
212 Config
.Program
.unlinkd
,
221 fatal("Failed to create unlinkd subprocess");
225 fd_note(unlinkd_wfd
, "squid -> unlinkd");
227 fd_note(unlinkd_rfd
, "unlinkd -> squid");
229 commSetTimeout(unlinkd_rfd
, -1, NULL
, NULL
);
231 commSetTimeout(unlinkd_wfd
, -1, NULL
, NULL
);
234 * unlinkd_rfd should already be non-blocking because of
235 * ipcCreate. We change unlinkd_wfd to blocking mode because
236 * we never want to lose an unlink request, and we don't have
237 * code to retry if we get EWOULDBLOCK. Unfortunately, we can
238 * do this only for the IPC_FIFO case.
240 assert(fd_table
[unlinkd_rfd
].flags
.nonblocking
);
242 if (FD_PIPE
== fd_table
[unlinkd_wfd
].type
)
243 commUnsetNonBlocking(unlinkd_wfd
);
245 debugs(2, 1, "Unlinkd pipe opened on FD " << unlinkd_wfd
);
249 debugs(2, 4, "Unlinkd handle: 0x" << std::hex
<< hIpc
<< std::dec
<< ", PID: " << pid
);