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