]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/unlinkd.cc
2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 02 Unlink Daemon */
18 #include "SquidConfig.h"
20 #include "SquidTime.h"
21 #include "StatCounters.h"
22 #include "store/Disk.h"
27 /* This code gets linked to Squid */
29 static int unlinkd_wfd
= -1;
30 static int unlinkd_rfd
= -1;
35 #define UNLINKD_QUEUE_LIMIT 20
38 unlinkdUnlink(const char *path
)
43 static int queuelen
= 0;
45 if (unlinkd_wfd
< 0) {
46 debug_trap("unlinkdUnlink: unlinkd_wfd < 0");
52 * If the queue length is greater than our limit, then we pause
53 * for a small amount of time, hoping that unlinkd has some
54 * feedback for us. Maybe it just needs a slice of the CPU's
57 if (queuelen
>= UNLINKD_QUEUE_LIMIT
) {
58 #if defined(USE_EPOLL) || defined(USE_KQUEUE) || defined(USE_DEVPOLL)
61 * We can't use fd_set when using epoll() or kqueue(). In
62 * these cases we block for 10 ms.
68 * When we can use select, block for up to 100 ms.
73 FD_SET(unlinkd_rfd
, &R
);
76 select(unlinkd_rfd
+ 1, &R
, NULL
, NULL
, &to
);
81 * If there is at least one outstanding unlink request, then
82 * try to read a response. If there's nothing to read we'll
83 * get an EWOULDBLOCK or whatever. If we get a response, then
84 * decrement the queue size by the number of newlines read.
90 bytes_read
= read(unlinkd_rfd
, rbuf
, 511);
93 rbuf
[bytes_read
] = '\0';
95 for (i
= 0; i
< bytes_read
; ++i
)
99 assert(queuelen
>= 0);
104 assert(l
< MAXPATHLEN
);
105 xstrncpy(buf
, path
, MAXPATHLEN
);
108 bytes_written
= write(unlinkd_wfd
, buf
, l
);
110 if (bytes_written
< 0) {
112 debugs(2, DBG_IMPORTANT
, "unlinkdUnlink: write FD " << unlinkd_wfd
<< " failed: " << xstrerr(xerrno
));
115 } else if (bytes_written
!= l
) {
116 debugs(2, DBG_IMPORTANT
, "unlinkdUnlink: FD " << unlinkd_wfd
<< " only wrote " << bytes_written
<< " of " << l
<< " bytes");
121 ++statCounter
.unlink
.requests
;
123 * Increment this syscalls counter here, even though the syscall
124 * is executed by the helper process. We try to be consistent
125 * in counting unlink operations.
127 ++statCounter
.syscalls
.disk
.unlinks
;
136 if (unlinkd_wfd
> -1) {
137 debugs(2, DBG_IMPORTANT
, "Closing unlinkd pipe on FD " << unlinkd_wfd
);
138 shutdown(unlinkd_wfd
, SD_BOTH
);
139 comm_close(unlinkd_wfd
);
141 if (unlinkd_wfd
!= unlinkd_rfd
)
142 comm_close(unlinkd_rfd
);
150 if (WaitForSingleObject(hIpc
, 5000) != WAIT_OBJECT_0
) {
152 debugs(2, DBG_IMPORTANT
, "unlinkdClose: WARNING: (unlinkd," << pid
<< "d) didn't exit in 5 seconds");
164 debugs(2, DBG_IMPORTANT
, "Closing unlinkd pipe on FD " << unlinkd_wfd
);
166 file_close(unlinkd_wfd
);
168 if (unlinkd_wfd
!= unlinkd_rfd
)
169 file_close(unlinkd_rfd
);
181 // we should start unlinkd if there are any cache_dirs using it
182 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
183 const RefCount
<SwapDir
> sd
= Config
.cacheSwap
.swapDirs
[i
];
184 if (sd
->unlinkdUseful())
194 if (unlinkd_wfd
>= 0)
195 return; // unlinkd already started
198 Ip::Address localhost
;
200 args
[0] = "(unlinkd)";
202 localhost
.setLocalhost();
205 #if USE_POLL && _SQUID_OSF_
206 /* pipes and poll() don't get along on DUNIX -DW */
208 #elif _SQUID_WINDOWS_
209 /* select() will fail on a pipe */
212 /* We currently need to use FIFO.. see below */
215 Config
.Program
.unlinkd
,
224 fatal("Failed to create unlinkd subprocess");
228 fd_note(unlinkd_wfd
, "squid -> unlinkd");
230 fd_note(unlinkd_rfd
, "unlinkd -> squid");
232 commUnsetFdTimeout(unlinkd_rfd
);
233 commUnsetFdTimeout(unlinkd_wfd
);
236 * unlinkd_rfd should already be non-blocking because of
237 * ipcCreate. We change unlinkd_wfd to blocking mode because
238 * we never want to lose an unlink request, and we don't have
239 * code to retry if we get EWOULDBLOCK. Unfortunately, we can
240 * do this only for the IPC_FIFO case.
242 assert(fd_table
[unlinkd_rfd
].flags
.nonblocking
);
244 if (FD_PIPE
== fd_table
[unlinkd_wfd
].type
)
245 commUnsetNonBlocking(unlinkd_wfd
);
247 debugs(2, DBG_IMPORTANT
, "Unlinkd pipe opened on FD " << unlinkd_wfd
);
251 debugs(2, 4, "Unlinkd handle: 0x" << std::hex
<< hIpc
<< std::dec
<< ", PID: " << pid
);
256 #endif /* USE_UNLINKD */