]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/log/file/log_file_daemon.cc
2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
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.
22 #include <sys/param.h>
31 #include "helper/protocol_defines.h"
33 /* parse buffer - ie, length of longest expected line */
34 #define LOGFILE_BUF_LEN 65536
37 rotate(const char *path
, int rotate_count
)
43 char from
[MAXPATHLEN
];
47 if (stat(path
, &sb
) == 0)
48 if (S_ISREG(sb
.st_mode
) == 0)
51 /* Rotate numbers 0 through N up one */
52 for (i
= rotate_count
; i
> 1;) {
54 snprintf(from
, MAXPATHLEN
, "%s.%d", path
, i
- 1);
55 snprintf(to
, MAXPATHLEN
, "%s.%d", path
, i
);
56 #if _SQUID_OS2_ || _SQUID_WINDOWS_
59 fprintf(stderr
, "WARNING: remove '%s' failure: %s\n", to
, xstrerr(xerrno
));
62 if (rename(from
, to
) < 0 && errno
!= ENOENT
) {
64 fprintf(stderr
, "WARNING: rename '%s' to '%s' failure: %s\n", from
, to
, xstrerr(xerrno
));
67 if (rotate_count
> 0) {
68 snprintf(to
, MAXPATHLEN
, "%s.%d", path
, 0);
69 #if _SQUID_OS2_ || _SQUID_WINDOWS_
72 fprintf(stderr
, "WARNING: remove '%s' failure: %s\n", to
, xstrerr(xerrno
));
75 if (rename(path
, to
) < 0 && errno
!= ENOENT
) {
77 fprintf(stderr
, "WARNING: rename %s to %s failure: %s\n", path
, to
, xstrerr(xerrno
));
85 * L<data>\n - logfile data
90 * r<n>\n - set rotate count to <n>
91 * b<n>\n - 1 = buffer output, 0 = don't buffer output
94 main(int argc
, char *argv
[])
98 char buf
[LOGFILE_BUF_LEN
];
99 int rotate_count
= 10;
103 printf("Error: usage: %s <logfile>\n", argv
[0]);
106 fp
= fopen(argv
[1], "a");
111 setbuf(stdout
, NULL
);
112 /* XXX stderr should not be closed, but in order to support squid must be
113 * able to collect and manage modules's stderr first.
116 t
= open(_PATH_DEVNULL
, O_RDWR
);
120 while (fgets(buf
, LOGFILE_BUF_LEN
, stdin
)) {
121 /* First byte indicates what we're logging! */
124 if (buf
[1] != '\0') {
125 fprintf(fp
, "%s", buf
+ 1);
126 /* try to detect the 32-bit file too big write error and rotate */
127 int err
= ferror(fp
);
130 /* file too big - recover by rotating the logs and starting a new one.
131 * out of device space - recover by rotating and hoping that rotation count drops a big one.
133 if (err
== EFBIG
|| err
== ENOSPC
) {
134 fprintf(stderr
, "WARNING: %s writing %s. Attempting to recover via a log rotation.\n",xstrerr(err
),argv
[1]);
136 rotate(argv
[1], rotate_count
);
137 fp
= fopen(argv
[1], "a");
142 fprintf(fp
, "%s", buf
+ 1);
154 rotate(argv
[1], rotate_count
);
155 fp
= fopen(argv
[1], "a");
166 //fprintf(fp, "SET ROTATE: %s\n", buf + 1);
167 rotate_count
= atoi(buf
+ 1);
170 //fprintf(fp, "SET BUFFERED: %s\n", buf + 1);
171 do_buffer
= (buf
[1] == '1');
177 /* Just in case .. */
178 fprintf(fp
, "%s", buf
);