Add the ability to Bzip2 your logs.

PR:		27901
Submitted by:	Anders Nordby <anders@fix.no> (stylistic changes by me)
This commit is contained in:
David E. O'Brien 2001-07-30 15:17:17 +00:00
parent 4c05509cd0
commit e12f0d2fac
3 changed files with 63 additions and 7 deletions

View File

@ -271,6 +271,11 @@ flag will make the archive files compress to save space by
using using
.Xr gzip 1 . .Xr gzip 1 .
The The
.Ar J
flag will make the archive files compress to save space by
using
.Xr bzip2 1 .
The
.Ar B .Ar B
flag means that the file is a binary file, and so the flag means that the file is a binary file, and so the
.Tn ASCII .Tn ASCII

View File

@ -32,6 +32,9 @@ static const char rcsid[] =
#ifndef COMPRESS_POSTFIX #ifndef COMPRESS_POSTFIX
#define COMPRESS_POSTFIX ".gz" #define COMPRESS_POSTFIX ".gz"
#endif #endif
#ifndef BZCOMPRESS_POSTFIX
#define BZCOMPRESS_POSTFIX ".bz2"
#endif
#include <ctype.h> #include <ctype.h>
#include <err.h> #include <err.h>
@ -61,6 +64,7 @@ static const char rcsid[] =
#define CE_COMPACT 1 /* Compact the achived log files */ #define CE_COMPACT 1 /* Compact the achived log files */
#define CE_BINARY 2 /* Logfile is in binary, don't add */ #define CE_BINARY 2 /* Logfile is in binary, don't add */
#define CE_BZCOMPACT 8 /* Compact the achived log files with bzip2 */
/* status messages */ /* status messages */
#define CE_TRIMAT 4 /* trim at a specific time */ #define CE_TRIMAT 4 /* trim at a specific time */
@ -76,7 +80,7 @@ struct conf_entry {
int hours; /* Hours between log trimming */ int hours; /* Hours between log trimming */
time_t trim_at; /* Specific time to do trimming */ time_t trim_at; /* Specific time to do trimming */
int permissions; /* File permissions on the log */ int permissions; /* File permissions on the log */
int flags; /* Flags (CE_COMPACT & CE_BINARY) */ int flags; /* Flags (CE_COMPACT & CE_BZCOMPACT & CE_BINARY) */
int sig; /* Signal to send */ int sig; /* Signal to send */
struct conf_entry *next;/* Linked list pointer */ struct conf_entry *next;/* Linked list pointer */
}; };
@ -105,6 +109,7 @@ static void usage();
static void dotrim(char *log, char *pid_file, int numdays, int falgs, int perm, int owner_uid, int group_gid, int sig); static void dotrim(char *log, char *pid_file, int numdays, int falgs, int perm, int owner_uid, int group_gid, int sig);
static int log_trim(char *log); static int log_trim(char *log);
static void compress_log(char *log); static void compress_log(char *log);
static void bzcompress_log(char *log);
static int sizefile(char *file); static int sizefile(char *file);
static int age_old_log(char *file); static int age_old_log(char *file);
static pid_t get_pid(char *pid_file); static pid_t get_pid(char *pid_file);
@ -141,6 +146,8 @@ do_entry(struct conf_entry * ent)
if (verbose) { if (verbose) {
if (ent->flags & CE_COMPACT) if (ent->flags & CE_COMPACT)
printf("%s <%dZ>: ", ent->log, ent->numlogs); printf("%s <%dZ>: ", ent->log, ent->numlogs);
else if (ent->flags & CE_BZCOMPACT)
printf("%s <%dJ>: ", ent->log, ent->numlogs);
else else
printf("%s <%d>: ", ent->log, ent->numlogs); printf("%s <%d>: ", ent->log, ent->numlogs);
} }
@ -175,6 +182,9 @@ do_entry(struct conf_entry * ent)
if (ent->flags & CE_COMPACT) if (ent->flags & CE_COMPACT)
printf("%s <%dZ>: trimming\n", printf("%s <%dZ>: trimming\n",
ent->log, ent->numlogs); ent->log, ent->numlogs);
else if (ent->flags & CE_BZCOMPACT)
printf("%s <%dJ>: trimming\n",
ent->log, ent->numlogs);
else else
printf("%s <%d>: trimming\n", printf("%s <%d>: trimming\n",
ent->log, ent->numlogs); ent->log, ent->numlogs);
@ -412,6 +422,8 @@ parse_file(char **files)
while (q && *q && !isspace(*q)) { while (q && *q && !isspace(*q)) {
if ((*q == 'Z') || (*q == 'z')) if ((*q == 'Z') || (*q == 'z'))
working->flags |= CE_COMPACT; working->flags |= CE_COMPACT;
else if ((*q == 'J') || (*q == 'j'))
working->flags |= CE_BZCOMPACT;
else if ((*q == 'B') || (*q == 'b')) else if ((*q == 'B') || (*q == 'b'))
working->flags |= CE_BINARY; working->flags |= CE_BINARY;
else if (*q != '-') else if (*q != '-')
@ -480,6 +492,7 @@ dotrim(char *log, char *pid_file, int numdays, int flags, int perm,
char dirpart[MAXPATHLEN], namepart[MAXPATHLEN]; char dirpart[MAXPATHLEN], namepart[MAXPATHLEN];
char file1[MAXPATHLEN], file2[MAXPATHLEN]; char file1[MAXPATHLEN], file2[MAXPATHLEN];
char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN]; char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN];
char jfile1[MAXPATHLEN];
int notified, need_notification, fd, _numdays; int notified, need_notification, fd, _numdays;
struct stat st; struct stat st;
pid_t pid; pid_t pid;
@ -524,19 +537,25 @@ dotrim(char *log, char *pid_file, int numdays, int flags, int perm,
(void) snprintf(file1, sizeof(file1), "%s/%s.%d", dirpart, namepart, numdays); (void) snprintf(file1, sizeof(file1), "%s/%s.%d", dirpart, namepart, numdays);
(void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1, (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1,
COMPRESS_POSTFIX); COMPRESS_POSTFIX);
snprintf(jfile1, sizeof(jfile1), "%s%s", file1,
BZCOMPRESS_POSTFIX);
} else { } else {
/* name of oldest log */ /* name of oldest log */
(void) snprintf(file1, sizeof(file1), "%s.%d", log, numdays); (void) snprintf(file1, sizeof(file1), "%s.%d", log, numdays);
(void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1, (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1,
COMPRESS_POSTFIX); COMPRESS_POSTFIX);
snprintf(jfile1, sizeof(jfile1), "%s%s", file1,
BZCOMPRESS_POSTFIX);
} }
if (noaction) { if (noaction) {
printf("rm -f %s\n", file1); printf("rm -f %s\n", file1);
printf("rm -f %s\n", zfile1); printf("rm -f %s\n", zfile1);
printf("rm -f %s\n", jfile1);
} else { } else {
(void) unlink(file1); (void) unlink(file1);
(void) unlink(zfile1); (void) unlink(zfile1);
(void) unlink(jfile1);
} }
/* Move down log files */ /* Move down log files */
@ -555,8 +574,16 @@ dotrim(char *log, char *pid_file, int numdays, int flags, int perm,
if (lstat(file1, &st)) { if (lstat(file1, &st)) {
(void) strlcat(zfile1, COMPRESS_POSTFIX, sizeof(zfile1)); (void) strlcat(zfile1, COMPRESS_POSTFIX, sizeof(zfile1));
(void) strlcat(zfile2, COMPRESS_POSTFIX, sizeof(zfile2)); (void) strlcat(zfile2, COMPRESS_POSTFIX, sizeof(zfile2));
if (lstat(zfile1, &st)) if (lstat(zfile1, &st)) {
continue; strlcpy(zfile1, file1, sizeof(zfile1));
strlcpy(zfile2, file2, sizeof(zfile2));
strlcat(zfile1, BZCOMPRESS_POSTFIX,
sizeof(zfile1));
strlcat(zfile2, BZCOMPRESS_POSTFIX,
sizeof(zfile2));
if (lstat(zfile1, &st))
continue;
}
} }
if (noaction) { if (noaction) {
printf("mv %s %s\n", zfile1, zfile2); printf("mv %s %s\n", zfile1, zfile2);
@ -624,7 +651,7 @@ dotrim(char *log, char *pid_file, int numdays, int flags, int perm,
printf("daemon pid %d notified\n", (int) pid); printf("daemon pid %d notified\n", (int) pid);
} }
} }
if ((flags & CE_COMPACT)) { if ((flags & CE_COMPACT) || (flags & CE_BZCOMPACT)) {
if (need_notification && !notified) if (need_notification && !notified)
warnx("log %s not compressed because daemon not notified", log); warnx("log %s not compressed because daemon not notified", log);
else if (noaction) else if (noaction)
@ -637,9 +664,15 @@ dotrim(char *log, char *pid_file, int numdays, int flags, int perm,
} }
if (archtodir) { if (archtodir) {
(void) snprintf(file1, sizeof(file1), "%s/%s", dirpart, namepart); (void) snprintf(file1, sizeof(file1), "%s/%s", dirpart, namepart);
compress_log(file1); if (flags & CE_COMPACT)
compress_log(file1);
else if (flags & CE_BZCOMPACT)
bzcompress_log(file1);
} else { } else {
compress_log(log); if (flags & CE_COMPACT)
compress_log(log);
else if (flags & CE_BZCOMPACT)
bzcompress_log(log);
} }
} }
} }
@ -670,13 +703,30 @@ compress_log(char *log)
(void) snprintf(tmp, sizeof(tmp), "%s.0", log); (void) snprintf(tmp, sizeof(tmp), "%s.0", log);
pid = fork(); pid = fork();
if (pid < 0) if (pid < 0)
err(1, "fork"); err(1, "gzip fork");
else if (!pid) { else if (!pid) {
(void) execl(_PATH_GZIP, _PATH_GZIP, "-f", tmp, (char *)0); (void) execl(_PATH_GZIP, _PATH_GZIP, "-f", tmp, (char *)0);
err(1, _PATH_GZIP); err(1, _PATH_GZIP);
} }
} }
/* Fork of bzip2 to compress the old log file */
static void
bzcompress_log(char *log)
{
pid_t pid;
char tmp[MAXPATHLEN];
snprintf(tmp, sizeof(tmp), "%s.0", log);
pid = fork();
if (pid < 0)
err(1, "bzip2 fork");
else if (!pid) {
execl(_PATH_BZIP2, _PATH_BZIP2, "-f", tmp, 0);
err(1, _PATH_BZIP2);
}
}
/* Return size in kilobytes of a file */ /* Return size in kilobytes of a file */
static int static int
sizefile(char *file) sizefile(char *file)

View File

@ -24,4 +24,5 @@ provided "as is" without express or implied warranty.
#define _PATH_CONF "/etc/newsyslog.conf" #define _PATH_CONF "/etc/newsyslog.conf"
#define _PATH_SYSLOGPID _PATH_VARRUN "syslog.pid" #define _PATH_SYSLOGPID _PATH_VARRUN "syslog.pid"
#define _PATH_BZIP2 "/usr/bin/bzip2"
#define _PATH_GZIP "/usr/bin/gzip" #define _PATH_GZIP "/usr/bin/gzip"