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