]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/unlinkd.cc
2 * Copyright (C) 1996-2023 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 "StatCounters.h"
21 #include "store/Disk.h"
28 /* This code gets linked to Squid */
30 static int unlinkd_wfd
= -1;
31 static int unlinkd_rfd
= -1;
36 #define UNLINKD_QUEUE_LIMIT 20
39 unlinkdUnlink(const char *path
)
44 static int queuelen
= 0;
46 if (unlinkd_wfd
< 0) {
47 debug_trap("unlinkdUnlink: unlinkd_wfd < 0");
53 * If the queue length is greater than our limit, then we pause
54 * for a small amount of time, hoping that unlinkd has some
55 * feedback for us. Maybe it just needs a slice of the CPU's
58 if (queuelen
>= UNLINKD_QUEUE_LIMIT
) {
59 #if defined(USE_EPOLL) || defined(USE_KQUEUE) || defined(USE_DEVPOLL)
62 * We can't use fd_set when using epoll() or kqueue(). In
63 * these cases we block for 10 ms.
65 std::this_thread::sleep_for(std::chrono::milliseconds(10));
69 * When we can use select, block for up to 100 ms.
74 FD_SET(unlinkd_rfd
, &R
);
77 select(unlinkd_rfd
+ 1, &R
, nullptr, nullptr, &to
);
82 * If there is at least one outstanding unlink request, then
83 * try to read a response. If there's nothing to read we'll
84 * get an EWOULDBLOCK or whatever. If we get a response, then
85 * decrement the queue size by the number of newlines read.
91 bytes_read
= read(unlinkd_rfd
, rbuf
, 511);
94 rbuf
[bytes_read
] = '\0';
96 for (i
= 0; i
< bytes_read
; ++i
)
100 assert(queuelen
>= 0);
105 assert(l
< MAXPATHLEN
);
106 xstrncpy(buf
, path
, MAXPATHLEN
);
109 bytes_written
= write(unlinkd_wfd
, buf
, l
);
111 if (bytes_written
< 0) {
113 debugs(2, DBG_IMPORTANT
, "ERROR: unlinkdUnlink: write FD " << unlinkd_wfd
<< " failed: " << xstrerr(xerrno
));
116 } else if (bytes_written
!= l
) {
117 debugs(2, DBG_IMPORTANT
, "unlinkdUnlink: FD " << unlinkd_wfd
<< " only wrote " << bytes_written
<< " of " << l
<< " bytes");
122 ++statCounter
.unlink
.requests
;
124 * Increment this syscalls counter here, even though the syscall
125 * is executed by the helper process. We try to be consistent
126 * in counting unlink operations.
128 ++statCounter
.syscalls
.disk
.unlinks
;
137 if (unlinkd_wfd
> -1) {
138 debugs(2, DBG_IMPORTANT
, "Closing unlinkd pipe on FD " << unlinkd_wfd
);
139 shutdown(unlinkd_wfd
, SD_BOTH
);
140 comm_close(unlinkd_wfd
);
142 if (unlinkd_wfd
!= unlinkd_rfd
)
143 comm_close(unlinkd_rfd
);
151 if (WaitForSingleObject(hIpc
, 5000) != WAIT_OBJECT_0
) {
153 debugs(2, DBG_IMPORTANT
, "WARNING: unlinkdClose: (unlinkd," << pid
<< "d) didn't exit in 5 seconds");
165 debugs(2, DBG_IMPORTANT
, "Closing unlinkd pipe on FD " << unlinkd_wfd
);
167 file_close(unlinkd_wfd
);
169 if (unlinkd_wfd
!= unlinkd_rfd
)
170 file_close(unlinkd_rfd
);
182 // we should start unlinkd if there are any cache_dirs using it
183 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
184 const RefCount
<SwapDir
> sd
= Config
.cacheSwap
.swapDirs
[i
];
185 if (sd
->unlinkdUseful())
195 if (unlinkd_wfd
>= 0)
196 return; // unlinkd already started
199 Ip::Address localhost
;
201 args
[0] = "(unlinkd)";
203 localhost
.setLocalhost();
206 #if USE_POLL && _SQUID_OSF_
207 /* pipes and poll() don't get along on DUNIX -DW */
209 #elif _SQUID_WINDOWS_
210 /* select() will fail on a pipe */
213 /* We currently need to use FIFO.. see below */
216 Config
.Program
.unlinkd
,
225 fatal("Failed to create unlinkd subprocess");
227 std::this_thread::sleep_for(std::chrono::milliseconds(250));
229 fd_note(unlinkd_wfd
, "squid -> unlinkd");
231 fd_note(unlinkd_rfd
, "unlinkd -> squid");
233 commUnsetFdTimeout(unlinkd_rfd
);
234 commUnsetFdTimeout(unlinkd_wfd
);
237 * unlinkd_rfd should already be non-blocking because of
238 * ipcCreate. We change unlinkd_wfd to blocking mode because
239 * we never want to lose an unlink request, and we don't have
240 * code to retry if we get EWOULDBLOCK. Unfortunately, we can
241 * do this only for the IPC_FIFO case.
243 assert(fd_table
[unlinkd_rfd
].flags
.nonblocking
);
245 if (FD_PIPE
== fd_table
[unlinkd_wfd
].type
)
246 commUnsetNonBlocking(unlinkd_wfd
);
248 debugs(2, DBG_IMPORTANT
, "Unlinkd pipe opened on FD " << unlinkd_wfd
);
252 debugs(2, 4, "Unlinkd handle: 0x" << std::hex
<< hIpc
<< std::dec
<< ", PID: " << pid
);
257 #endif /* USE_UNLINKD */