]> git.ipfire.org Git - thirdparty/squid.git/blob - src/log/ModStdio.cc
Moved SquidConfig definition from structs.h to own header file.
[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 #if HAVE_ERRNO_H
43 #include <errno.h>
44 #endif
45
46 typedef struct {
47 int fd;
48 char *buf;
49 size_t bufsz;
50 int offset;
51 } l_stdio_t;
52
53 /*
54 * Aborts with fatal message if write() returns something other
55 * than its length argument.
56 */
57 static void
58 logfileWriteWrapper(Logfile * lf, const void *buf, size_t len)
59 {
60 l_stdio_t *ll = (l_stdio_t *) lf->data;
61 size_t s;
62 s = FD_WRITE_METHOD(ll->fd, (char const *) buf, len);
63 fd_bytes(ll->fd, s, FD_WRITE);
64
65 if (s == len)
66 return;
67
68 if (!lf->flags.fatal)
69 return;
70
71 fatalf("logfileWrite: %s: %s\n", lf->path, xstrerror());
72 }
73
74 static void
75 logfile_mod_stdio_writeline(Logfile * lf, const char *buf, size_t len)
76 {
77 l_stdio_t *ll = (l_stdio_t *) lf->data;
78
79 if (0 == ll->bufsz) {
80 /* buffering disabled */
81 logfileWriteWrapper(lf, buf, len);
82 return;
83 }
84 if (ll->offset > 0 && (ll->offset + len) > ll->bufsz)
85 logfileFlush(lf);
86
87 if (len > ll->bufsz) {
88 /* too big to fit in buffer */
89 logfileWriteWrapper(lf, buf, len);
90 return;
91 }
92 /* buffer it */
93 memcpy(ll->buf + ll->offset, buf, len);
94
95 ll->offset += len;
96
97 assert(ll->offset >= 0);
98
99 assert((size_t) ll->offset <= ll->bufsz);
100 }
101
102 static void
103 logfile_mod_stdio_linestart(Logfile * lf)
104 {
105 }
106
107 static void
108 logfile_mod_stdio_lineend(Logfile * lf)
109 {
110 lf->f_flush(lf);
111 }
112
113 static void
114 logfile_mod_stdio_flush(Logfile * lf)
115 {
116 l_stdio_t *ll = (l_stdio_t *) lf->data;
117 if (0 == ll->offset)
118 return;
119 logfileWriteWrapper(lf, ll->buf, (size_t) ll->offset);
120 ll->offset = 0;
121 }
122
123 static void
124 logfile_mod_stdio_rotate(Logfile * lf)
125 {
126 #ifdef S_ISREG
127
128 struct stat sb;
129 #endif
130
131 int i;
132 char from[MAXPATHLEN];
133 char to[MAXPATHLEN];
134 l_stdio_t *ll = (l_stdio_t *) lf->data;
135 assert(lf->path);
136 const char *realpath = lf->path+6; // skip 'stdio:' prefix.
137 assert(realpath);
138
139 #ifdef S_ISREG
140
141 if (stat(realpath, &sb) == 0)
142 if (S_ISREG(sb.st_mode) == 0)
143 return;
144
145 #endif
146
147 debugs(0, DBG_IMPORTANT, "Rotate log file " << lf->path);
148
149 /* Rotate numbers 0 through N up one */
150 for (i = Config.Log.rotateNumber; i > 1;) {
151 --i;
152 snprintf(from, MAXPATHLEN, "%s.%d", realpath, i - 1);
153 snprintf(to, MAXPATHLEN, "%s.%d", realpath, i);
154 xrename(from, to);
155 }
156
157 /* Rotate the current log to .0 */
158 logfileFlush(lf);
159
160 file_close(ll->fd); /* always close */
161
162 if (Config.Log.rotateNumber > 0) {
163 snprintf(to, MAXPATHLEN, "%s.%d", realpath, 0);
164 xrename(realpath, to);
165 }
166 /* Reopen the log. It may have been renamed "manually" */
167 ll->fd = file_open(realpath, O_WRONLY | O_CREAT | O_TEXT);
168
169 if (DISK_ERROR == ll->fd && lf->flags.fatal) {
170 debugs(50, DBG_CRITICAL, "ERROR: logfileRotate: " << lf->path << ": " << xstrerror());
171 fatalf("Cannot open %s: %s", lf->path, xstrerror());
172 }
173 }
174
175 static void
176 logfile_mod_stdio_close(Logfile * lf)
177 {
178 l_stdio_t *ll = (l_stdio_t *) lf->data;
179 lf->f_flush(lf);
180
181 if (ll->fd >= 0)
182 file_close(ll->fd);
183
184 if (ll->buf)
185 xfree(ll->buf);
186
187 xfree(lf->data);
188 lf->data = NULL;
189 }
190
191 /*
192 * This code expects the path to be a writable filename
193 */
194 int
195 logfile_mod_stdio_open(Logfile * lf, const char *path, size_t bufsz, int fatal_flag)
196 {
197 lf->f_close = logfile_mod_stdio_close;
198 lf->f_linewrite = logfile_mod_stdio_writeline;
199 lf->f_linestart = logfile_mod_stdio_linestart;
200 lf->f_lineend = logfile_mod_stdio_lineend;
201 lf->f_flush = logfile_mod_stdio_flush;
202 lf->f_rotate = logfile_mod_stdio_rotate;
203
204 l_stdio_t *ll = static_cast<l_stdio_t*>(xcalloc(1, sizeof(*ll)));
205 lf->data = ll;
206
207 ll->fd = file_open(path, O_WRONLY | O_CREAT | O_TEXT);
208
209 if (DISK_ERROR == ll->fd) {
210 if (ENOENT == errno && fatal_flag) {
211 fatalf("Cannot open '%s' because\n"
212 "\tthe parent directory does not exist.\n"
213 "\tPlease create the directory.\n", path);
214 } else if (EACCES == errno && fatal_flag) {
215 fatalf("Cannot open '%s' for writing.\n"
216 "\tThe parent directory must be writeable by the\n"
217 "\tuser '%s', which is the cache_effective_user\n"
218 "\tset in squid.conf.", path, Config.effectiveUser);
219 } else if (EISDIR == errno && fatal_flag) {
220 fatalf("Cannot open '%s' because it is a directory, not a file.\n", path);
221 } else {
222 debugs(50, DBG_IMPORTANT, "ERROR: logfileOpen " << lf->path << ": " << xstrerror());
223 return 0;
224 }
225 }
226 if (bufsz > 0) {
227 ll->buf = static_cast<char*>(xmalloc(bufsz));
228 ll->bufsz = bufsz;
229 }
230 return 1;
231 }