2 * Copyright (C) 1996-2014 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 */
17 #include "log/ModStdio.h"
18 #include "SquidConfig.h"
30 * Aborts with fatal message if write() returns something other
31 * than its length argument.
34 logfileWriteWrapper(Logfile
* lf
, const void *buf
, size_t len
)
36 l_stdio_t
*ll
= (l_stdio_t
*) lf
->data
;
38 s
= FD_WRITE_METHOD(ll
->fd
, (char const *) buf
, len
);
39 fd_bytes(ll
->fd
, s
, FD_WRITE
);
47 fatalf("logfileWrite: %s: %s\n", lf
->path
, xstrerror());
51 logfile_mod_stdio_writeline(Logfile
* lf
, const char *buf
, size_t len
)
53 l_stdio_t
*ll
= (l_stdio_t
*) lf
->data
;
56 /* buffering disabled */
57 logfileWriteWrapper(lf
, buf
, len
);
60 if (ll
->offset
> 0 && (ll
->offset
+ len
) > ll
->bufsz
)
63 if (len
> ll
->bufsz
) {
64 /* too big to fit in buffer */
65 logfileWriteWrapper(lf
, buf
, len
);
69 memcpy(ll
->buf
+ ll
->offset
, buf
, len
);
73 assert(ll
->offset
>= 0);
75 assert((size_t) ll
->offset
<= ll
->bufsz
);
79 logfile_mod_stdio_linestart(Logfile
* lf
)
84 logfile_mod_stdio_lineend(Logfile
* lf
)
90 logfile_mod_stdio_flush(Logfile
* lf
)
92 l_stdio_t
*ll
= (l_stdio_t
*) lf
->data
;
95 logfileWriteWrapper(lf
, ll
->buf
, (size_t) ll
->offset
);
100 logfile_mod_stdio_rotate(Logfile
* lf
)
108 char from
[MAXPATHLEN
];
110 l_stdio_t
*ll
= (l_stdio_t
*) lf
->data
;
112 const char *realpath
= lf
->path
+6; // skip 'stdio:' prefix.
117 if (stat(realpath
, &sb
) == 0)
118 if (S_ISREG(sb
.st_mode
) == 0)
123 debugs(0, DBG_IMPORTANT
, "Rotate log file " << lf
->path
);
125 /* Rotate numbers 0 through N up one */
126 for (i
= Config
.Log
.rotateNumber
; i
> 1;) {
128 snprintf(from
, MAXPATHLEN
, "%s.%d", realpath
, i
- 1);
129 snprintf(to
, MAXPATHLEN
, "%s.%d", realpath
, i
);
133 /* Rotate the current log to .0 */
136 file_close(ll
->fd
); /* always close */
138 if (Config
.Log
.rotateNumber
> 0) {
139 snprintf(to
, MAXPATHLEN
, "%s.%d", realpath
, 0);
140 xrename(realpath
, to
);
142 /* Reopen the log. It may have been renamed "manually" */
143 ll
->fd
= file_open(realpath
, O_WRONLY
| O_CREAT
| O_TEXT
);
145 if (DISK_ERROR
== ll
->fd
&& lf
->flags
.fatal
) {
146 debugs(50, DBG_CRITICAL
, "ERROR: logfileRotate: " << lf
->path
<< ": " << xstrerror());
147 fatalf("Cannot open %s: %s", lf
->path
, xstrerror());
152 logfile_mod_stdio_close(Logfile
* lf
)
154 l_stdio_t
*ll
= (l_stdio_t
*) lf
->data
;
168 * This code expects the path to be a writable filename
171 logfile_mod_stdio_open(Logfile
* lf
, const char *path
, size_t bufsz
, int fatal_flag
)
173 lf
->f_close
= logfile_mod_stdio_close
;
174 lf
->f_linewrite
= logfile_mod_stdio_writeline
;
175 lf
->f_linestart
= logfile_mod_stdio_linestart
;
176 lf
->f_lineend
= logfile_mod_stdio_lineend
;
177 lf
->f_flush
= logfile_mod_stdio_flush
;
178 lf
->f_rotate
= logfile_mod_stdio_rotate
;
180 l_stdio_t
*ll
= static_cast<l_stdio_t
*>(xcalloc(1, sizeof(*ll
)));
183 ll
->fd
= file_open(path
, O_WRONLY
| O_CREAT
| O_TEXT
);
185 if (DISK_ERROR
== ll
->fd
) {
186 if (ENOENT
== errno
&& fatal_flag
) {
187 fatalf("Cannot open '%s' because\n"
188 "\tthe parent directory does not exist.\n"
189 "\tPlease create the directory.\n", path
);
190 } else if (EACCES
== errno
&& fatal_flag
) {
191 fatalf("Cannot open '%s' for writing.\n"
192 "\tThe parent directory must be writeable by the\n"
193 "\tuser '%s', which is the cache_effective_user\n"
194 "\tset in squid.conf.", path
, Config
.effectiveUser
);
195 } else if (EISDIR
== errno
&& fatal_flag
) {
196 fatalf("Cannot open '%s' because it is a directory, not a file.\n", path
);
198 debugs(50, DBG_IMPORTANT
, "ERROR: logfileOpen " << lf
->path
<< ": " << xstrerror());
203 ll
->buf
= static_cast<char*>(xmalloc(bufsz
));