From 29e4fc5607ace15c0be03fab6930acfadda2610a Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 16 Aug 2018 22:32:34 +0200 Subject: usyslogd: more control over log rotate behaviour, command line processing - Add more fine grained control over how log rotation is supposed to behave - Add command line option processing to usyslogd - Expose log rotation control via command line switches - Add default values to usyslogd service for pygos use case Signed-off-by: David Oberhollenzer --- syslogd/main.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 2 deletions(-) (limited to 'syslogd/main.c') diff --git a/syslogd/main.c b/syslogd/main.c index 6b4937e..7a466d6 100644 --- a/syslogd/main.c +++ b/syslogd/main.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -32,9 +33,41 @@ #define SYSLOG_SOCKET "/dev/log" +#define GPL_URL "https://gnu.org/licenses/gpl.html" + + +static const struct option long_opts[] = { + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { "rotate-replace", no_argument, NULL, 'r' }, + { "max-size", required_argument, NULL, 'm' }, + { NULL, 0, NULL, 0 }, +}; + +const char *short_opts = "hVrm:"; + +const char *version_string = +"usyslogd (pygos init) " PACKAGE_VERSION "\n" +"Copyright (C) 2018 David Oberhollenzer\n\n" +"License GPLv3+: GNU GPL version 3 or later <" GPL_URL ">.\n" +"This is free software: you are free to change and redistribute it.\n" +"There is NO WARRANTY, to the extent permitted by law.\n"; + +const char *usage_string = +"Usage: usyslogd [OPTIONS..]\n\n" +"The following options are supported:\n" +" -h, --help Print this help text and exit\n" +" -V, --version Print version information and exit\n" +" -r, --rotate-replace Replace old log files when doing log rotation.\n" +" -m, --max-size Automatically rotate log files bigger than this.\n"; + + static volatile sig_atomic_t syslog_run = 1; static volatile sig_atomic_t syslog_rotate = 0; +static int log_flags = 0; +static size_t max_size = 0; + static void sighandler(int signo) @@ -82,17 +115,58 @@ static int handle_data(int fd) return logmgr->write(logmgr, &msg); } -int main(void) +static void process_options(int argc, char **argv) +{ + char *end; + int i; + + for (;;) { + i = getopt_long(argc, argv, short_opts, long_opts, NULL); + if (i == -1) + break; + + switch (i) { + case 'r': + log_flags |= LOG_ROTATE_OVERWRITE; + break; + case 'm': + log_flags |= LOG_ROTATE_SIZE_LIMIT; + max_size = strtol(optarg, &end, 10); + if (max_size == 0 || *end != '\0') { + fputs("Numeric argument > 0 expected for -m\n", + stderr); + goto fail; + } + break; + case 'h': + fputs(usage_string, stdout); + exit(EXIT_SUCCESS); + case 'V': + fputs(version_string, stdout); + exit(EXIT_SUCCESS); + default: + goto fail; + } + } + return; +fail: + fputs("Try `usyslogd --help' for more information\n", stderr); + exit(EXIT_FAILURE); +} + +int main(int argc, char **argv) { int sfd, status = EXIT_FAILURE; + process_options(argc, argv); + signal_setup(); sfd = mksock(SYSLOG_SOCKET, SOCK_FLAG_EVERYONE | SOCK_FLAG_DGRAM); if (sfd < 0) return EXIT_FAILURE; - if (logmgr->init(logmgr)) + if (logmgr->init(logmgr, log_flags, max_size)) goto out; while (syslog_run) { -- cgit v1.2.3