2 * Copyright (C) 1996-2016 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 50 Log file handling */
13 #include "comm/Loops.h"
17 #include "log/Config.h"
19 #include "log/ModDaemon.h"
20 #include "SquidConfig.h"
22 #include "SquidTime.h"
26 /* How many buffers to keep before we say we've buffered too much */
27 #define LOGFILE_MAXBUFS 128
29 /* Size of the logfile buffer */
31 * For optimal performance this should match LOGFILE_BUFSIZ in logfile-daemon.c
33 #define LOGFILE_BUFSZ 32768
35 /* How many seconds between warnings */
36 #define LOGFILE_WARN_TIME 30
38 static LOGWRITE logfile_mod_daemon_writeline
;
39 static LOGLINESTART logfile_mod_daemon_linestart
;
40 static LOGLINEEND logfile_mod_daemon_lineend
;
41 static LOGROTATE logfile_mod_daemon_rotate
;
42 static LOGFLUSH logfile_mod_daemon_flush
;
43 static LOGCLOSE logfile_mod_daemon_close
;
45 static void logfile_mod_daemon_append(Logfile
* lf
, const char *buf
, int len
);
57 typedef struct _l_daemon l_daemon_t
;
61 logfileNewBuffer(Logfile
* lf
)
63 l_daemon_t
*ll
= (l_daemon_t
*) lf
->data
;
66 debugs(50, 5, "logfileNewBuffer: " << lf
->path
<< ": new buffer");
68 b
= static_cast<logfile_buffer_t
*>(xcalloc(1, sizeof(logfile_buffer_t
)));
70 b
->buf
= static_cast<char*>(xcalloc(1, LOGFILE_BUFSZ
));
71 assert(b
->buf
!= NULL
);
72 b
->size
= LOGFILE_BUFSZ
;
75 dlinkAddTail(b
, &b
->node
, &ll
->bufs
);
80 logfileFreeBuffer(Logfile
* lf
, logfile_buffer_t
* b
)
82 l_daemon_t
*ll
= (l_daemon_t
*) lf
->data
;
84 dlinkDelete(&b
->node
, &ll
->bufs
);
91 logfileHandleWrite(int, void *data
)
93 Logfile
*lf
= static_cast<Logfile
*>(data
);
94 l_daemon_t
*ll
= static_cast<l_daemon_t
*>(lf
->data
);
97 * We'll try writing the first entry until its done - if we
98 * get a partial write then we'll re-schedule until its completed.
99 * Its naive but it'll do for now.
101 if (!ll
->bufs
.head
) // abort if there is nothing pending right now.
104 logfile_buffer_t
*b
= static_cast<logfile_buffer_t
*>(ll
->bufs
.head
->data
);
106 ll
->flush_pending
= 0;
108 int ret
= FD_WRITE_METHOD(ll
->wfd
, b
->buf
+ b
->written_len
, b
->len
- b
->written_len
);
110 debugs(50, 3, lf
->path
<< ": write returned " << ret
);
112 if (ignoreErrno(xerrno
)) {
113 /* something temporary */
114 Comm::SetSelect(ll
->wfd
, COMM_SELECT_WRITE
, logfileHandleWrite
, lf
, 0);
115 ll
->flush_pending
= 1;
118 debugs(50, DBG_IMPORTANT
,"logfileHandleWrite: " << lf
->path
<< ": error writing (" << xstrerr(xerrno
) << ")");
119 /* XXX should handle this better */
120 fatal("I don't handle this error well!");
124 debugs(50, DBG_IMPORTANT
, "logfileHandleWrite: " << lf
->path
<< ": wrote 0 bytes?");
125 /* XXX should handle this better */
126 fatal("I don't handle this error well!");
128 /* ret > 0, so something was written */
129 b
->written_len
+= ret
;
130 assert(b
->written_len
<= b
->len
);
131 if (b
->written_len
== b
->len
) {
132 /* written the whole buffer! */
133 logfileFreeBuffer(lf
, b
);
136 /* Is there more to write? */
139 /* there is, so schedule more */
141 Comm::SetSelect(ll
->wfd
, COMM_SELECT_WRITE
, logfileHandleWrite
, lf
, 0);
142 ll
->flush_pending
= 1;
147 logfileQueueWrite(Logfile
* lf
)
149 l_daemon_t
*ll
= (l_daemon_t
*) lf
->data
;
150 if (ll
->flush_pending
|| ll
->bufs
.head
== NULL
) {
153 ll
->flush_pending
= 1;
155 logfile_buffer_t
*b
= static_cast<logfile_buffer_t
*>(ll
->bufs
.head
->data
);
156 if (b
->len
+ 2 <= b
->size
)
157 logfile_mod_daemon_append(lf
, "F\n", 2);
159 /* Ok, schedule a write-event */
160 Comm::SetSelect(ll
->wfd
, COMM_SELECT_WRITE
, logfileHandleWrite
, lf
, 0);
164 logfile_mod_daemon_append(Logfile
* lf
, const char *buf
, int len
)
166 l_daemon_t
*ll
= (l_daemon_t
*) lf
->data
;
170 /* Is there a buffer? If not, create one */
171 if (ll
->bufs
.head
== NULL
) {
172 logfileNewBuffer(lf
);
174 debugs(50, 3, "logfile_mod_daemon_append: " << lf
->path
<< ": appending " << len
<< " bytes");
175 /* Copy what can be copied */
177 b
= static_cast<logfile_buffer_t
*>(ll
->bufs
.tail
->data
);
178 debugs(50, 3, "logfile_mod_daemon_append: current buffer has " << b
->len
<< " of " << b
->size
<< " bytes before append");
179 s
= min(len
, (b
->size
- b
->len
));
180 memcpy(b
->buf
+ b
->len
, buf
, s
);
184 assert(b
->len
<= LOGFILE_BUFSZ
);
187 logfileNewBuffer(lf
);
193 * only schedule a flush (write) if one isn't scheduled.
196 logfileFlushEvent(void *data
)
198 Logfile
*lf
= static_cast<Logfile
*>(data
);
201 * This might work better if we keep track of when we wrote last and only
202 * schedule a write if we haven't done so in the last second or two.
204 logfileQueueWrite(lf
);
205 eventAdd("logfileFlush", logfileFlushEvent
, lf
, 1.0, 1);
211 logfile_mod_daemon_open(Logfile
* lf
, const char *path
, size_t, int)
217 lf
->f_close
= logfile_mod_daemon_close
;
218 lf
->f_linewrite
= logfile_mod_daemon_writeline
;
219 lf
->f_linestart
= logfile_mod_daemon_linestart
;
220 lf
->f_lineend
= logfile_mod_daemon_lineend
;
221 lf
->f_flush
= logfile_mod_daemon_flush
;
222 lf
->f_rotate
= logfile_mod_daemon_rotate
;
224 cbdataInternalLock(lf
); // WTF?
225 debugs(50, DBG_IMPORTANT
, "Logfile Daemon: opening log " << path
);
226 ll
= static_cast<l_daemon_t
*>(xcalloc(1, sizeof(*ll
)));
230 Ip::Address localhost
;
231 args
[0] = "(logfile-daemon)";
234 localhost
.setLocalhost();
235 ll
->pid
= ipcCreate(IPC_STREAM
, Log::TheConfig
.logfile_daemon
, args
, "logfile-daemon", localhost
, &ll
->rfd
, &ll
->wfd
, NULL
);
237 fatal("Couldn't start logfile helper");
241 /* Queue the initial control data */
242 tmpbuf
= static_cast<char*>(xmalloc(BUFSIZ
));
243 snprintf(tmpbuf
, BUFSIZ
, "r%d\nb%d\n", Config
.Log
.rotateNumber
, Config
.onoff
.buffered_logs
);
244 logfile_mod_daemon_append(lf
, tmpbuf
, strlen(tmpbuf
));
247 /* Start the flush event */
248 eventAdd("logfileFlush", logfileFlushEvent
, lf
, 1.0, 1);
254 logfile_mod_daemon_close(Logfile
* lf
)
256 l_daemon_t
*ll
= static_cast<l_daemon_t
*>(lf
->data
);
257 debugs(50, DBG_IMPORTANT
, "Logfile Daemon: closing log " << lf
->path
);
259 if (ll
->rfd
== ll
->wfd
)
265 kill(ll
->pid
, SIGTERM
);
266 eventDelete(logfileFlushEvent
, lf
);
269 cbdataInternalUnlock(lf
); // WTF??
273 logfile_mod_daemon_rotate(Logfile
* lf
, const int16_t)
276 debugs(50, DBG_IMPORTANT
, "logfileRotate: " << lf
->path
);
280 logfile_mod_daemon_append(lf
, tb
, 2);
284 * This routine assumes that up to one line is written. Don't try to
285 * call this routine with more than one line or subsequent lines
286 * won't be prefixed with the command type and confuse the logging
290 logfile_mod_daemon_writeline(Logfile
* lf
, const char *buf
, size_t len
)
292 l_daemon_t
*ll
= static_cast<l_daemon_t
*>(lf
->data
);
293 /* Make sure the logfile buffer isn't too large */
294 if (ll
->nbufs
> LOGFILE_MAXBUFS
) {
295 if (ll
->last_warned
< squid_curtime
- LOGFILE_WARN_TIME
) {
296 ll
->last_warned
= squid_curtime
;
297 debugs(50, DBG_IMPORTANT
, "Logfile: " << lf
->path
<< ": queue is too large; some log messages have been lost.");
301 /* Append this data to the end buffer; create a new one if needed */
302 /* Are we eol? If so, prefix with our logfile command byte */
303 logfile_mod_daemon_append(lf
, buf
, len
);
307 logfile_mod_daemon_linestart(Logfile
* lf
)
309 l_daemon_t
*ll
= static_cast<l_daemon_t
*>(lf
->data
);
311 assert(ll
->eol
== 1);
315 logfile_mod_daemon_append(lf
, tb
, 1);
319 logfile_mod_daemon_lineend(Logfile
* lf
)
321 l_daemon_t
*ll
= static_cast<l_daemon_t
*>(lf
->data
);
323 assert(ll
->eol
== 0);
325 /* Kick a write off if the head buffer is -full- */
326 if (ll
->bufs
.head
!= NULL
) {
327 b
= static_cast<logfile_buffer_t
*>(ll
->bufs
.head
->data
);
328 if (b
->node
.next
!= NULL
|| !Config
.onoff
.buffered_logs
)
329 logfileQueueWrite(lf
);
334 logfile_mod_daemon_flush(Logfile
* lf
)
336 l_daemon_t
*ll
= static_cast<l_daemon_t
*>(lf
->data
);
337 if (commUnsetNonBlocking(ll
->wfd
)) {
338 debugs(50, DBG_IMPORTANT
, "Logfile Daemon: Couldn't set the pipe blocking for flush! You're now missing some log entries.");
341 while (ll
->bufs
.head
!= NULL
) {
342 logfileHandleWrite(ll
->wfd
, lf
);
344 if (commSetNonBlocking(ll
->wfd
)) {
345 fatalf("Logfile Daemon: %s: Couldn't set the pipe non-blocking for flush!\n", lf
->path
);