]> git.ipfire.org Git - thirdparty/squid.git/blob - src/log/ModStdio.cc
Sync with trunk rev.13542
[thirdparty/squid.git] / src / log / ModStdio.cc
1 /*
2 * DEBUG: section 50 Log file handling
3 * AUTHOR: Duane Wessels
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 */
32
33 #include "squid.h"
34 #include "disk.h"
35 #include "fd.h"
36 #include "fde.h"
37 #include "globals.h"
38 #include "log/File.h"
39 #include "log/ModStdio.h"
40 #include "SquidConfig.h"
41
42 #include <cerrno>
43
44 typedef struct {
45 int fd;
46 char *buf;
47 size_t bufsz;
48 int offset;
49 } l_stdio_t;
50
51 /*
52 * Aborts with fatal message if write() returns something other
53 * than its length argument.
54 */
55 static void
56 logfileWriteWrapper(Logfile * lf, const void *buf, size_t len)
57 {
58 l_stdio_t *ll = (l_stdio_t *) lf->data;
59 size_t s;
60 s = FD_WRITE_METHOD(ll->fd, (char const *) buf, len);
61 fd_bytes(ll->fd, s, FD_WRITE);
62
63 if (s == len)
64 return;
65
66 if (!lf->flags.fatal)
67 return;
68
69 fatalf("logfileWrite: %s: %s\n", lf->path, xstrerror());
70 }
71
72 static void
73 logfile_mod_stdio_writeline(Logfile * lf, const char *buf, size_t len)
74 {
75 l_stdio_t *ll = (l_stdio_t *) lf->data;
76
77 if (0 == ll->bufsz) {
78 /* buffering disabled */
79 logfileWriteWrapper(lf, buf, len);
80 return;
81 }
82 if (ll->offset > 0 && (ll->offset + len) > ll->bufsz)
83 logfileFlush(lf);
84
85 if (len > ll->bufsz) {
86 /* too big to fit in buffer */
87 logfileWriteWrapper(lf, buf, len);
88 return;
89 }
90 /* buffer it */
91 memcpy(ll->buf + ll->offset, buf, len);
92
93 ll->offset += len;
94
95 assert(ll->offset >= 0);
96
97 assert((size_t) ll->offset <= ll->bufsz);
98 }
99
100 static void
101 logfile_mod_stdio_linestart(Logfile * lf)
102 {
103 }
104
105 static void
106 logfile_mod_stdio_lineend(Logfile * lf)
107 {
108 lf->f_flush(lf);
109 }
110
111 static void
112 logfile_mod_stdio_flush(Logfile * lf)
113 {
114 l_stdio_t *ll = (l_stdio_t *) lf->data;
115 if (0 == ll->offset)
116 return;
117 logfileWriteWrapper(lf, ll->buf, (size_t) ll->offset);
118 ll->offset = 0;
119 }
120
121 static void
122 logfile_mod_stdio_rotate(Logfile * lf)
123 {
124 #ifdef S_ISREG
125
126 struct stat sb;
127 #endif
128
129 int i;
130 char from[MAXPATHLEN];
131 char to[MAXPATHLEN];
132 l_stdio_t *ll = (l_stdio_t *) lf->data;
133 assert(lf->path);
134 const char *realpath = lf->path+6; // skip 'stdio:' prefix.
135 assert(realpath);
136
137 #ifdef S_ISREG
138
139 if (stat(realpath, &sb) == 0)
140 if (S_ISREG(sb.st_mode) == 0)
141 return;
142
143 #endif
144
145 debugs(0, DBG_IMPORTANT, "Rotate log file " << lf->path);
146
147 /* Rotate numbers 0 through N up one */
148 for (i = Config.Log.rotateNumber; i > 1;) {
149 --i;
150 snprintf(from, MAXPATHLEN, "%s.%d", realpath, i - 1);
151 snprintf(to, MAXPATHLEN, "%s.%d", realpath, i);
152 xrename(from, to);
153 }
154
155 /* Rotate the current log to .0 */
156 logfileFlush(lf);
157
158 file_close(ll->fd); /* always close */
159
160 if (Config.Log.rotateNumber > 0) {
161 snprintf(to, MAXPATHLEN, "%s.%d", realpath, 0);
162 xrename(realpath, to);
163 }
164 /* Reopen the log. It may have been renamed "manually" */
165 ll->fd = file_open(realpath, O_WRONLY | O_CREAT | O_TEXT);
166
167 if (DISK_ERROR == ll->fd && lf->flags.fatal) {
168 debugs(50, DBG_CRITICAL, "ERROR: logfileRotate: " << lf->path << ": " << xstrerror());
169 fatalf("Cannot open %s: %s", lf->path, xstrerror());
170 }
171 }
172
173 static void
174 logfile_mod_stdio_close(Logfile * lf)
175 {
176 l_stdio_t *ll = (l_stdio_t *) lf->data;
177 lf->f_flush(lf);
178
179 if (ll->fd >= 0)
180 file_close(ll->fd);
181
182 if (ll->buf)
183 xfree(ll->buf);
184
185 xfree(lf->data);
186 lf->data = NULL;
187 }
188
189 /*
190 * This code expects the path to be a writable filename
191 */
192 int
193 logfile_mod_stdio_open(Logfile * lf, const char *path, size_t bufsz, int fatal_flag)
194 {
195 lf->f_close = logfile_mod_stdio_close;
196 lf->f_linewrite = logfile_mod_stdio_writeline;
197 lf->f_linestart = logfile_mod_stdio_linestart;
198 lf->f_lineend = logfile_mod_stdio_lineend;
199 lf->f_flush = logfile_mod_stdio_flush;
200 lf->f_rotate = logfile_mod_stdio_rotate;
201
202 l_stdio_t *ll = static_cast<l_stdio_t*>(xcalloc(1, sizeof(*ll)));
203 lf->data = ll;
204
205 ll->fd = file_open(path, O_WRONLY | O_CREAT | O_TEXT);
206
207 if (DISK_ERROR == ll->fd) {
208 if (ENOENT == errno && fatal_flag) {
209 fatalf("Cannot open '%s' because\n"
210 "\tthe parent directory does not exist.\n"
211 "\tPlease create the directory.\n", path);
212 } else if (EACCES == errno && fatal_flag) {
213 fatalf("Cannot open '%s' for writing.\n"
214 "\tThe parent directory must be writeable by the\n"
215 "\tuser '%s', which is the cache_effective_user\n"
216 "\tset in squid.conf.", path, Config.effectiveUser);
217 } else if (EISDIR == errno && fatal_flag) {
218 fatalf("Cannot open '%s' because it is a directory, not a file.\n", path);
219 } else {
220 debugs(50, DBG_IMPORTANT, "ERROR: logfileOpen " << lf->path << ": " << xstrerror());
221 return 0;
222 }
223 }
224 if (bufsz > 0) {
225 ll->buf = static_cast<char*>(xmalloc(bufsz));
226 ll->bufsz = bufsz;
227 }
228 return 1;
229 }