From 192d3aafe7d6ff143cfe58fe0509b4bc1481136c Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Mon, 15 Jan 2018 18:32:36 +0100 Subject: [PATCH] Add support for global logfile max size and max num. rotated # Max file size for each log file: 100 kiB, rotate max 4 copies: # log => log.1 => log.2.gz => log.3.gz => log.4.gz log size=100k count=4 Signed-off-by: Joachim Nilsson --- src/conf.c | 24 ++++++++++++++++++++++++ src/conf.h | 3 +++ src/service.c | 7 ++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/conf.c b/src/conf.c index db8d09cd..1f53a848 100644 --- a/src/conf.c +++ b/src/conf.c @@ -36,11 +36,15 @@ #include "service.h" #include "tty.h" #include "helpers.h" +#include "util.h" #define BOOTSTRAP (runlevel == 0) #define MATCH_CMD(l, c, x) \ (!strncasecmp(l, c, strlen(c)) && (x = (l) + strlen(c))) +int logfile_size_max = 200000; /* 200 kB */ +int logfile_count_max = 5; + struct rlimit global_rlimit[RLIMIT_NLIMITS]; struct conf_change { @@ -383,6 +387,26 @@ static void parse_static(char *line) return; } + if (MATCH_CMD(line, "log ", x)) { + char *tok; + static int size = 200000, count = 5; + + tok = strtok(x, ":= "); + while (tok) { + if (!strncmp(tok, "size", 4)) + size = strtobytes(strtok(NULL, ":= ")); + else if (!strncmp(tok, "count", 5)) + count = strtobytes(strtok(NULL, ":= ")); + + tok = strtok(NULL, ":= "); + } + + if (size > 0) + logfile_size_max = size; + if (count >= 0) + logfile_count_max = count; + } + if (MATCH_CMD(line, "shutdown ", x)) { if (sdown) free(sdown); sdown = strdup(strip_line(x)); diff --git a/src/conf.h b/src/conf.h index 64e98564..5db2a91b 100644 --- a/src/conf.h +++ b/src/conf.h @@ -26,6 +26,9 @@ #include "svc.h" +extern int logfile_size_max; +extern int logfile_count_max; + extern struct rlimit global_rlimit[]; int str2rlim(char *str); diff --git a/src/service.c b/src/service.c index e986b581..32f69456 100644 --- a/src/service.c +++ b/src/service.c @@ -266,7 +266,12 @@ static int service_start(svc_t *svc) sig_unblock(); if (svc->log.file[0] == '/') { - execlp("logit", "logit", "-f", svc->log.file, NULL); + char sz[20], num[3]; + + snprintf(sz, sizeof(sz), "%d", logfile_size_max); + snprintf(num, sizeof(num), "%d", logfile_count_max); + + execlp("logit", "logit", "-f", svc->log.file, "-n", sz, "-r", num, NULL); _exit(0); }