~clint-fewbar/ubuntu/precise/squid3/ignore-sighup-early

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * $Id$
 *
 * DEBUG: section 50    Log file handling
 * AUTHOR: Duane Wessels
 *
 * SQUID Web Proxy Cache          http://www.squid-cache.org/
 * ----------------------------------------------------------
 *
 *  Squid is the result of efforts by numerous individuals from
 *  the Internet community; see the CONTRIBUTORS file for full
 *  details.   Many organizations have provided support for Squid's
 *  development; see the SPONSORS file for full details.  Squid is
 *  Copyrighted (C) 2001 by the Regents of the University of
 *  California; see the COPYRIGHT file for full details.  Squid
 *  incorporates software developed and/or copyrighted by other
 *  sources; see the CREDITS file for full details.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 */

#include "squid.h"
#include "fde.h"

static void logfileWriteWrapper(Logfile * lf, const void *buf, size_t len);

#if HAVE_SYSLOG

/* Define LOG_AUTHPRIV as LOG_AUTH on systems still using the old deprecated LOG_AUTH */
#if !defined(LOG_AUTHPRIV) && defined(LOG_AUTH)
#define LOG_AUTHPRIV LOG_AUTH
#endif

struct syslog_symbol_t {
    const char* name;
    int value;
};

static int syslog_ntoa (const char* s)
{
#define syslog_symbol(a) #a, a
    static syslog_symbol_t _symbols[] = {
        { syslog_symbol(LOG_AUTHPRIV) },
        { syslog_symbol(LOG_DAEMON) },
        { syslog_symbol(LOG_LOCAL0) },
        { syslog_symbol(LOG_LOCAL1) },
        { syslog_symbol(LOG_LOCAL2) },
        { syslog_symbol(LOG_LOCAL3) },
        { syslog_symbol(LOG_LOCAL4) },
        { syslog_symbol(LOG_LOCAL5) },
        { syslog_symbol(LOG_LOCAL6) },
        { syslog_symbol(LOG_LOCAL7) },
        { syslog_symbol(LOG_USER) },
        { syslog_symbol(LOG_ERR) },
        { syslog_symbol(LOG_WARNING) },
        { syslog_symbol(LOG_NOTICE) },
        { syslog_symbol(LOG_INFO) },
        { syslog_symbol(LOG_DEBUG) },
        { NULL, 0 }
    };

    for (syslog_symbol_t* p = _symbols; p->name != NULL; ++p)
        if (!strcmp(s, p->name) || !strcasecmp(s, p->name+4))
            return p->value;

    debugs(1, 1, "Unknown syslog facility/priority '" << s << "'");

    return 0;
}

#define PRIORITY_MASK (LOG_ERR | LOG_WARNING | LOG_NOTICE | LOG_INFO | LOG_DEBUG)
#endif
Logfile *
logfileOpen(const char *path, size_t bufsz, int fatal_flag)
{
    int fd;
    Logfile *lf = static_cast<Logfile *>(xcalloc(1, sizeof(*lf)));

    xstrncpy(lf->path, path, MAXPATHLEN);

#if HAVE_SYSLOG

    if (strncmp(path, "syslog", 6) == 0) {
        lf->flags.syslog = 1;
        lf->syslog_priority = LOG_INFO;
        lf->fd = -1;

        if (path[6] != '\0') {
            path += 7;
            char *priority = xstrdup(path);
            char *facility = (char *) strchr(priority, '.');
            if (!facility)
                facility = (char *) strchr(priority, '|');
            if (facility) {
                *facility++ = '\0';
                lf->syslog_priority |= syslog_ntoa(facility);
            }
            lf->syslog_priority |= syslog_ntoa(priority);
            xfree(priority);
            if (0 == (lf->syslog_priority & PRIORITY_MASK))
                lf->syslog_priority |= LOG_INFO;
        }
    } else
#endif
    {
        fd = file_open(path, O_WRONLY | O_CREAT | O_TEXT);

        if (DISK_ERROR == fd) {
            if (ENOENT == errno && fatal_flag) {
                fatalf("Cannot open '%s' because\n"
                       "\tthe parent directory does not exist.\n"
                       "\tPlease create the directory.\n", path);
            } else if (EACCES == errno && fatal_flag) {
                fatalf("Cannot open '%s' for writing.\n"
                       "\tThe parent directory must be writeable by the\n"
                       "\tuser '%s', which is the cache_effective_user\n"
                       "\tset in squid.conf.", path, Config.effectiveUser);
            } else if (EISDIR == errno && fatal_flag) {
                fatalf("Cannot open '%s' because it is a directory, not a file.\n", path);
            } else {
                debugs(50, 1, "logfileOpen: " << path << ": " << xstrerror());
                return NULL;
            }
        }

        lf->fd = fd;

        if (bufsz > 0) {
            lf->buf = (char *) xmalloc(bufsz);
            lf->bufsz = bufsz;
        }
    }

    if (fatal_flag)
        lf->flags.fatal = 1;

    return lf;
}

void
logfileClose(Logfile * lf)
{
    logfileFlush(lf);

    if (lf->fd >= 0)
        file_close(lf->fd);

    if (lf->buf)
        xfree(lf->buf);

    xfree(lf);
}

void
logfileRotate(Logfile * lf)
{
#ifdef S_ISREG

    struct stat sb;
#endif

    int i;
    char from[MAXPATHLEN];
    char to[MAXPATHLEN];
    assert(lf->path);

    if (lf->flags.syslog)
        return;

#ifdef S_ISREG

    if (stat(lf->path, &sb) == 0)
        if (S_ISREG(sb.st_mode) == 0)
            return;

#endif

    debugs(0, 1, "logfileRotate: " << lf->path);

    /* Rotate numbers 0 through N up one */
    for (i = Config.Log.rotateNumber; i > 1;) {
        i--;
        snprintf(from, MAXPATHLEN, "%s.%d", lf->path, i - 1);
        snprintf(to, MAXPATHLEN, "%s.%d", lf->path, i);
        xrename(from, to);
    }

    /* Rotate the current log to .0 */
    logfileFlush(lf);

    file_close(lf->fd);		/* always close */

    if (Config.Log.rotateNumber > 0) {
        snprintf(to, MAXPATHLEN, "%s.%d", lf->path, 0);
        xrename(lf->path, to);
    }

    /* Reopen the log.  It may have been renamed "manually" */
    lf->fd = file_open(lf->path, O_WRONLY | O_CREAT | O_TEXT);

    if (DISK_ERROR == lf->fd && lf->flags.fatal) {
        debugs(50, 1, "logfileRotate: " << lf->path << ": " << xstrerror());
        fatalf("Cannot open %s: %s", lf->path, xstrerror());
    }
}

void
logfileWrite(Logfile * lf, void *buf, size_t len)
{
#if HAVE_SYSLOG

    if (lf->flags.syslog) {
        syslog(lf->syslog_priority, "%s", (char *)buf);
        return;
    }

#endif

    if (0 == lf->bufsz) {
        /* buffering disabled */
        logfileWriteWrapper(lf, buf, len);
        return;
    }

    if (lf->offset > 0 && lf->offset + len > lf->bufsz)
        logfileFlush(lf);

    if (len > lf->bufsz) {
        /* too big to fit in buffer */
        logfileWriteWrapper(lf, buf, len);
        return;
    }

    /* buffer it */
    xmemcpy(lf->buf + lf->offset, buf, len);

    lf->offset += len;

    assert (lf->offset >= 0);

    assert((size_t)lf->offset <= lf->bufsz);
}

void
logfilePrintf(Logfile * lf, const char *fmt,...)
{
    va_list args;
    char buf[8192];
    int s;

    va_start(args, fmt);

    s = vsnprintf(buf, 8192, fmt, args);

    if (s > 8192) {
        s = 8192;

        if (fmt[strlen(fmt) - 1] == '\n')
            buf[8191] = '\n';
    }

    logfileWrite(lf, buf, (size_t) s);
    va_end(args);
}

void
logfileFlush(Logfile * lf)
{
    if (0 == lf->offset)
        return;

    logfileWriteWrapper(lf, lf->buf, (size_t) lf->offset);

    lf->offset = 0;
}

/*
 * Aborts with fatal message if write() returns something other
 * than its length argument.
 */
static void
logfileWriteWrapper(Logfile * lf, const void *buf, size_t len)
{
    size_t s;
    s = FD_WRITE_METHOD(lf->fd, (char const *)buf, len);
    fd_bytes(lf->fd, s, FD_WRITE);

    if (s == len)
        return;

    if (!lf->flags.fatal)
        return;

    fatalf("logfileWrite: %s: %s\n", lf->path, xstrerror());
}