]> git.ipfire.org Git - thirdparty/squid.git/blame - src/log/ModStdio.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / log / ModStdio.cc
CommitLineData
82b7abe3 1/*
5b74111a 2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
82b7abe3 3 *
bbc27441
AJ
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.
82b7abe3
AJ
7 */
8
bbc27441
AJ
9/* DEBUG: section 50 Log file handling */
10
582c2af2 11#include "squid.h"
ed6e9fb9 12#include "fatal.h"
c4ad1349 13#include "fd.h"
82b7abe3 14#include "fde.h"
b3f7fd88 15#include "fs_io.h"
582c2af2 16#include "globals.h"
82b7abe3
AJ
17#include "log/File.h"
18#include "log/ModStdio.h"
4d5904f7 19#include "SquidConfig.h"
82b7abe3 20
1a30fdf5 21#include <cerrno>
21d845b1 22
82b7abe3
AJ
23typedef struct {
24 int fd;
25 char *buf;
26 size_t bufsz;
27 int offset;
28} l_stdio_t;
29
30/*
31 * Aborts with fatal message if write() returns something other
32 * than its length argument.
33 */
34static void
35logfileWriteWrapper(Logfile * lf, const void *buf, size_t len)
36{
37 l_stdio_t *ll = (l_stdio_t *) lf->data;
38 size_t s;
39 s = FD_WRITE_METHOD(ll->fd, (char const *) buf, len);
b69e9ffa 40 int xerrno = errno;
82b7abe3
AJ
41 fd_bytes(ll->fd, s, FD_WRITE);
42
43 if (s == len)
9d65168e 44 return;
82b7abe3
AJ
45
46 if (!lf->flags.fatal)
9d65168e 47 return;
82b7abe3 48
b69e9ffa 49 fatalf("logfileWrite: %s: %s\n", lf->path, xstrerr(xerrno));
82b7abe3
AJ
50}
51
52static void
53logfile_mod_stdio_writeline(Logfile * lf, const char *buf, size_t len)
54{
55 l_stdio_t *ll = (l_stdio_t *) lf->data;
56
57 if (0 == ll->bufsz) {
9d65168e
A
58 /* buffering disabled */
59 logfileWriteWrapper(lf, buf, len);
60 return;
82b7abe3
AJ
61 }
62 if (ll->offset > 0 && (ll->offset + len) > ll->bufsz)
9d65168e 63 logfileFlush(lf);
82b7abe3
AJ
64
65 if (len > ll->bufsz) {
9d65168e
A
66 /* too big to fit in buffer */
67 logfileWriteWrapper(lf, buf, len);
68 return;
82b7abe3
AJ
69 }
70 /* buffer it */
41d00cd3 71 memcpy(ll->buf + ll->offset, buf, len);
82b7abe3
AJ
72
73 ll->offset += len;
74
75 assert(ll->offset >= 0);
76
77 assert((size_t) ll->offset <= ll->bufsz);
78}
79
80static void
ced8def3 81logfile_mod_stdio_linestart(Logfile *)
82b7abe3
AJ
82{
83}
84
85static void
86logfile_mod_stdio_lineend(Logfile * lf)
87{
88 lf->f_flush(lf);
89}
90
91static void
92logfile_mod_stdio_flush(Logfile * lf)
93{
94 l_stdio_t *ll = (l_stdio_t *) lf->data;
95 if (0 == ll->offset)
9d65168e 96 return;
82b7abe3
AJ
97 logfileWriteWrapper(lf, ll->buf, (size_t) ll->offset);
98 ll->offset = 0;
99}
100
101static void
efc23871 102logfile_mod_stdio_rotate(Logfile * lf, const int16_t nRotate)
82b7abe3
AJ
103{
104#ifdef S_ISREG
105
106 struct stat sb;
107#endif
108
82b7abe3
AJ
109 char from[MAXPATHLEN];
110 char to[MAXPATHLEN];
111 l_stdio_t *ll = (l_stdio_t *) lf->data;
8030a2a0
AJ
112 const char *realpath = lf->path+6; // skip 'stdio:' prefix.
113 assert(realpath);
82b7abe3
AJ
114
115#ifdef S_ISREG
116
8030a2a0 117 if (stat(realpath, &sb) == 0)
9d65168e
A
118 if (S_ISREG(sb.st_mode) == 0)
119 return;
82b7abe3
AJ
120
121#endif
122
8030a2a0 123 debugs(0, DBG_IMPORTANT, "Rotate log file " << lf->path);
82b7abe3
AJ
124
125 /* Rotate numbers 0 through N up one */
efc23871 126 for (int16_t i = nRotate; i > 1;) {
5e263176 127 --i;
8030a2a0
AJ
128 snprintf(from, MAXPATHLEN, "%s.%d", realpath, i - 1);
129 snprintf(to, MAXPATHLEN, "%s.%d", realpath, i);
9d65168e 130 xrename(from, to);
82b7abe3
AJ
131 }
132
133 /* Rotate the current log to .0 */
134 logfileFlush(lf);
135
f53969cc 136 file_close(ll->fd); /* always close */
82b7abe3 137
efc23871 138 if (nRotate > 0) {
8030a2a0
AJ
139 snprintf(to, MAXPATHLEN, "%s.%d", realpath, 0);
140 xrename(realpath, to);
82b7abe3
AJ
141 }
142 /* Reopen the log. It may have been renamed "manually" */
8030a2a0 143 ll->fd = file_open(realpath, O_WRONLY | O_CREAT | O_TEXT);
82b7abe3
AJ
144
145 if (DISK_ERROR == ll->fd && lf->flags.fatal) {
b69e9ffa
AJ
146 int xerrno = errno;
147 debugs(50, DBG_CRITICAL, MYNAME << "ERROR: " << lf->path << ": " << xstrerr(xerrno));
148 fatalf("Cannot open %s: %s", lf->path, xstrerr(xerrno));
82b7abe3
AJ
149 }
150}
151
152static void
153logfile_mod_stdio_close(Logfile * lf)
154{
155 l_stdio_t *ll = (l_stdio_t *) lf->data;
156 lf->f_flush(lf);
157
158 if (ll->fd >= 0)
9d65168e 159 file_close(ll->fd);
82b7abe3
AJ
160
161 if (ll->buf)
9d65168e 162 xfree(ll->buf);
82b7abe3
AJ
163
164 xfree(lf->data);
165 lf->data = NULL;
166}
167
168/*
169 * This code expects the path to be a writable filename
170 */
171int
172logfile_mod_stdio_open(Logfile * lf, const char *path, size_t bufsz, int fatal_flag)
173{
174 lf->f_close = logfile_mod_stdio_close;
175 lf->f_linewrite = logfile_mod_stdio_writeline;
176 lf->f_linestart = logfile_mod_stdio_linestart;
177 lf->f_lineend = logfile_mod_stdio_lineend;
178 lf->f_flush = logfile_mod_stdio_flush;
179 lf->f_rotate = logfile_mod_stdio_rotate;
180
181 l_stdio_t *ll = static_cast<l_stdio_t*>(xcalloc(1, sizeof(*ll)));
182 lf->data = ll;
183
184 ll->fd = file_open(path, O_WRONLY | O_CREAT | O_TEXT);
185
186 if (DISK_ERROR == ll->fd) {
b69e9ffa
AJ
187 int xerrno = errno;
188 if (ENOENT == xerrno && fatal_flag) {
9d65168e
A
189 fatalf("Cannot open '%s' because\n"
190 "\tthe parent directory does not exist.\n"
191 "\tPlease create the directory.\n", path);
b69e9ffa 192 } else if (EACCES == xerrno && fatal_flag) {
9d65168e
A
193 fatalf("Cannot open '%s' for writing.\n"
194 "\tThe parent directory must be writeable by the\n"
195 "\tuser '%s', which is the cache_effective_user\n"
196 "\tset in squid.conf.", path, Config.effectiveUser);
b69e9ffa 197 } else if (EISDIR == xerrno && fatal_flag) {
1d470d88 198 fatalf("Cannot open '%s' because it is a directory, not a file.\n", path);
9d65168e 199 } else {
b69e9ffa 200 debugs(50, DBG_IMPORTANT, MYNAME << "ERROR: " << lf->path << ": " << xstrerr(xerrno));
9d65168e
A
201 return 0;
202 }
82b7abe3
AJ
203 }
204 if (bufsz > 0) {
9d65168e
A
205 ll->buf = static_cast<char*>(xmalloc(bufsz));
206 ll->bufsz = bufsz;
82b7abe3
AJ
207 }
208 return 1;
209}
f53969cc 210