]> git.ipfire.org Git - thirdparty/squid.git/blob - src/unlinkd.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / unlinkd.cc
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9 /* DEBUG: section 02 Unlink Daemon */
10
11 #include "squid.h"
12
13 #if USE_UNLINKD
14 #include "disk.h"
15 #include "fd.h"
16 #include "fde.h"
17 #include "globals.h"
18 #include "SquidIpc.h"
19 #include "SquidTime.h"
20 #include "StatCounters.h"
21 #include "SwapDir.h"
22 #include "tools.h"
23 #include "xusleep.h"
24
25 /* This code gets linked to Squid */
26
27 static int unlinkd_wfd = -1;
28 static int unlinkd_rfd = -1;
29
30 static void * hIpc;
31 static pid_t pid;
32
33 #define UNLINKD_QUEUE_LIMIT 20
34
35 void
36 unlinkdUnlink(const char *path)
37 {
38 char buf[MAXPATHLEN];
39 int l;
40 int bytes_written;
41 static int queuelen = 0;
42
43 if (unlinkd_wfd < 0) {
44 debug_trap("unlinkdUnlink: unlinkd_wfd < 0");
45 safeunlink(path, 0);
46 return;
47 }
48
49 /*
50 * If the queue length is greater than our limit, then we pause
51 * for a small amount of time, hoping that unlinkd has some
52 * feedback for us. Maybe it just needs a slice of the CPU's
53 * time.
54 */
55 if (queuelen >= UNLINKD_QUEUE_LIMIT) {
56 #if defined(USE_EPOLL) || defined(USE_KQUEUE) || defined(USE_DEVPOLL)
57 /*
58 * DPW 2007-04-23
59 * We can't use fd_set when using epoll() or kqueue(). In
60 * these cases we block for 10 ms.
61 */
62 xusleep(10000);
63 #else
64 /*
65 * DPW 2007-04-23
66 * When we can use select, block for up to 100 ms.
67 */
68 struct timeval to;
69 fd_set R;
70 FD_ZERO(&R);
71 FD_SET(unlinkd_rfd, &R);
72 to.tv_sec = 0;
73 to.tv_usec = 100000;
74 select(unlinkd_rfd + 1, &R, NULL, NULL, &to);
75 #endif
76 }
77
78 /*
79 * If there is at least one outstanding unlink request, then
80 * try to read a response. If there's nothing to read we'll
81 * get an EWOULDBLOCK or whatever. If we get a response, then
82 * decrement the queue size by the number of newlines read.
83 */
84 if (queuelen > 0) {
85 int bytes_read;
86 int i;
87 char rbuf[512];
88 bytes_read = read(unlinkd_rfd, rbuf, 511);
89
90 if (bytes_read > 0) {
91 rbuf[bytes_read] = '\0';
92
93 for (i = 0; i < bytes_read; ++i)
94 if ('\n' == rbuf[i])
95 --queuelen;
96
97 assert(queuelen >= 0);
98 }
99 }
100
101 l = strlen(path);
102 assert(l < MAXPATHLEN);
103 xstrncpy(buf, path, MAXPATHLEN);
104 buf[l] = '\n';
105 ++l;
106 bytes_written = write(unlinkd_wfd, buf, l);
107
108 if (bytes_written < 0) {
109 debugs(2, DBG_IMPORTANT, "unlinkdUnlink: write FD " << unlinkd_wfd << " failed: " << xstrerror());
110 safeunlink(path, 0);
111 return;
112 } else if (bytes_written != l) {
113 debugs(2, DBG_IMPORTANT, "unlinkdUnlink: FD " << unlinkd_wfd << " only wrote " << bytes_written << " of " << l << " bytes");
114 safeunlink(path, 0);
115 return;
116 }
117
118 ++statCounter.unlink.requests;
119 /*
120 * Increment this syscalls counter here, even though the syscall
121 * is executed by the helper process. We try to be consistent
122 * in counting unlink operations.
123 */
124 ++statCounter.syscalls.disk.unlinks;
125 ++queuelen;
126 }
127
128 void
129 unlinkdClose(void)
130 #if _SQUID_WINDOWS_
131 {
132
133 if (unlinkd_wfd > -1) {
134 debugs(2, DBG_IMPORTANT, "Closing unlinkd pipe on FD " << unlinkd_wfd);
135 shutdown(unlinkd_wfd, SD_BOTH);
136 comm_close(unlinkd_wfd);
137
138 if (unlinkd_wfd != unlinkd_rfd)
139 comm_close(unlinkd_rfd);
140
141 unlinkd_wfd = -1;
142
143 unlinkd_rfd = -1;
144 }
145
146 if (hIpc) {
147 if (WaitForSingleObject(hIpc, 5000) != WAIT_OBJECT_0) {
148 getCurrentTime();
149 debugs(2, DBG_IMPORTANT, "unlinkdClose: WARNING: (unlinkd," << pid << "d) didn't exit in 5 seconds");
150 }
151
152 CloseHandle(hIpc);
153 }
154 }
155 #else
156 {
157
158 if (unlinkd_wfd < 0)
159 return;
160
161 debugs(2, DBG_IMPORTANT, "Closing unlinkd pipe on FD " << unlinkd_wfd);
162
163 file_close(unlinkd_wfd);
164
165 if (unlinkd_wfd != unlinkd_rfd)
166 file_close(unlinkd_rfd);
167
168 unlinkd_wfd = -1;
169
170 unlinkd_rfd = -1;
171 }
172
173 #endif
174
175 bool
176 unlinkdNeeded(void)
177 {
178 // we should start unlinkd if there are any cache_dirs using it
179 for (int i = 0; i < Config.cacheSwap.n_configured; ++i) {
180 const RefCount<SwapDir> sd = Config.cacheSwap.swapDirs[i];
181 if (sd->unlinkdUseful())
182 return true;
183 }
184
185 return false;
186 }
187
188 void
189 unlinkdInit(void)
190 {
191 if (unlinkd_wfd >= 0)
192 return; // unlinkd already started
193
194 const char *args[2];
195 Ip::Address localhost;
196
197 args[0] = "(unlinkd)";
198 args[1] = NULL;
199 localhost.setLocalhost();
200
201 pid = ipcCreate(
202 #if USE_POLL && _SQUID_OSF_
203 /* pipes and poll() don't get along on DUNIX -DW */
204 IPC_STREAM,
205 #elif _SQUID_WINDOWS_
206 /* select() will fail on a pipe */
207 IPC_TCP_SOCKET,
208 #else
209 /* We currently need to use FIFO.. see below */
210 IPC_FIFO,
211 #endif
212 Config.Program.unlinkd,
213 args,
214 "unlinkd",
215 localhost,
216 &unlinkd_rfd,
217 &unlinkd_wfd,
218 &hIpc);
219
220 if (pid < 0)
221 fatal("Failed to create unlinkd subprocess");
222
223 xusleep(250000);
224
225 fd_note(unlinkd_wfd, "squid -> unlinkd");
226
227 fd_note(unlinkd_rfd, "unlinkd -> squid");
228
229 commUnsetFdTimeout(unlinkd_rfd);
230 commUnsetFdTimeout(unlinkd_wfd);
231
232 /*
233 * unlinkd_rfd should already be non-blocking because of
234 * ipcCreate. We change unlinkd_wfd to blocking mode because
235 * we never want to lose an unlink request, and we don't have
236 * code to retry if we get EWOULDBLOCK. Unfortunately, we can
237 * do this only for the IPC_FIFO case.
238 */
239 assert(fd_table[unlinkd_rfd].flags.nonblocking);
240
241 if (FD_PIPE == fd_table[unlinkd_wfd].type)
242 commUnsetNonBlocking(unlinkd_wfd);
243
244 debugs(2, DBG_IMPORTANT, "Unlinkd pipe opened on FD " << unlinkd_wfd);
245
246 #if _SQUID_WINDOWS_
247
248 debugs(2, 4, "Unlinkd handle: 0x" << std::hex << hIpc << std::dec << ", PID: " << pid);
249
250 #endif
251
252 }
253 #endif /* USE_UNLINKD */
254