diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2020-09-09 16:58:55 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2020-09-09 17:03:22 +0300 |
commit | aeef348db666ab537054a6ab48a75586fc7c6733 (patch) | |
tree | d3c0ef042714e825a4974c7e74e2b9df66fa5db8 | |
parent | 04f9616cbcbdb486d0832f4e8c35e74a08e266c5 (diff) | |
download | genrc-aeef348db666ab537054a6ab48a75586fc7c6733.tar.gz genrc-aeef348db666ab537054a6ab48a75586fc7c6733.tar.bz2 |
Configurable syslog facility and tag.
* src/genrc.c: New options --log-facility and --log-tag.
(genrc_openlog): New function.
* src/genrc.h (genrc_openlog): New proto.
* src/com_start.c (spawn): Use genrc_openlog.
* src/sentinel.c (wait_loop): Use genrc_openlog.
* NEWS: Update.
* src/genrc.8: Update.
-rw-r--r-- | NEWS | 27 | ||||
-rw-r--r-- | src/com_start.c | 4 | ||||
-rw-r--r-- | src/genrc.8 | 35 | ||||
-rw-r--r-- | src/genrc.c | 73 | ||||
-rw-r--r-- | src/genrc.h | 2 | ||||
-rw-r--r-- | src/sentinel.c | 4 |
6 files changed, 131 insertions, 14 deletions
@@ -1,9 +1,30 @@ -genrc NEWS -- history of user-visible changes. 2018-05-20 -Copyright (C) 2018 Sergey Poznyakoff +genrc NEWS -- history of user-visible changes. 2020-09-09 See the end of file for copying conditions. Please send genrc bug reports to <gray@gnu.org> or <gray@gnu.org.ua> + +* New options + + -s, --shell=SHELL + Use SHELL to start the program. Default is /bin/sh. + + -e, --exec + In sentinel mode, run the command directly via exec(3), instead + of via the shell. + + -v,--verbose + Print verbose diagnostics. + + --log-facility=F + Use syslog facility F instead of the default "daemon". + (equivalent environment variable - GENRC_LOG_FACILITY) + + --log-tag=S + Use syslog tag S instead of the program name. + (equivalent environment variable - GENRC_LOG_TAG) + + Version 1.1, 2018-05-20 Implements restart mode. @@ -34,7 +55,7 @@ Initial release ========================================================================= Copyright information: -Copyright (C) 2018 Sergey Poznyakoff +Copyright (C) 2018-2020 Sergey Poznyakoff Permission is granted to anyone to make or distribute verbatim copies of this document as received, in any medium, provided that the diff --git a/src/com_start.c b/src/com_start.c index 17581cb..5ad1786 100644 --- a/src/com_start.c +++ b/src/com_start.c @@ -1,5 +1,5 @@ /* This file is part of genrc -Copyryght (C) 2018, 2019 Sergey Poznyakoff +Copyryght (C) 2018-2020 Sergey Poznyakoff License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. @@ -127,7 +127,7 @@ spawn(int *p) execvp(argv[0], argv); if (p) { - openlog(genrc_program, LOG_PID, LOG_DAEMON); + genrc_openlog(); syslog(LOG_CRIT, "failed to exec: %m"); } else system_error(errno, "failed to exec %s", genrc_program); diff --git a/src/genrc.8 b/src/genrc.8 index 0fdfa54..49d8127 100644 --- a/src/genrc.8 +++ b/src/genrc.8 @@ -1,5 +1,5 @@ .\" This file is part of genrc. -.\" Copyright (C) 2018, 2019 Sergey Poznyakoff. +.\" Copyright (C) 2018-2020 Sergey Poznyakoff. .\" .\" Genrc is free software; you can redistribute it and/or modify .\" it under the terms of the GNU General Public License as published by @@ -13,7 +13,7 @@ .\" .\" You should have received a copy of the GNU General Public License .\" along with genrc. If not, see <http://www.gnu.org/licenses/>. -.TH GENRC 8 "September 13, 2019" "GENRC" "Genrc User Manual" +.TH GENRC 8 "September 9, 2020" "GENRC" "Genrc User Manual" .SH NAME genrc \- generic system initialization script helper .SH SYNOPSIS @@ -34,6 +34,8 @@ genrc \- generic system initialization script helper [\fB\-\-exec\fR]\ [\fB\-\-group=\fIGROUP\fR[,\fIGROUP\fR...]]\ [\fB\-\-help\fR]\ + [\fB\-\-log\-facility=\fIFACILITY\fR]\ + [\fB\-\-log\-tag=\fITAG\fR]\ [\fB\-\-no\-reload\fR]\ [\fB\-\-pid\-from=\fISOURCE\fR]\ [\fB\-\-pidfile=\fIPIDFILE\fR]\ @@ -212,6 +214,26 @@ notation is used, the groups must exist in the system group database. See also the \fB\-\-user\fR option. .TP +\fB\-\-log\-facility=\fIFACILITY\fR +Selects \fBsyslog\fR facility for use in sentinel mode. Valid arguments are +.BR auth , +.BR authpriv , +.BR cron , +.BR daemon , +.BR ftp , +.BR lpr , +.BR mail , +.BR user , +and +.BR local0 +through +.BR local7 . +The default is +.BR daemon . +.TP +\fB\-\-log\-tag=\fITAG\fR +Sets \fBsyslog\fR tag for use in sentinel mode. +.TP \fB\-\-no\-reload\fR Makes \fBreload\fR equivalent to \fBrestart\fR. .TP @@ -250,6 +272,9 @@ terminal, start it and run in background until it terminates. The program's stdout and stderr are sent to the syslog facility \fBdaemon\fR, priorities \fBinfo\fR and \fBerr\fR, correspondingly. +The default facility and syslog tag can be changed using the +\fB\-\-log\-facility\fR and \fB\-\-log-tag\fR options. + See the options \fB\-\-restart\-on\-exit\fR and \fB\-\-restart\-on\-signal\fR for details on how to restart the program. @@ -375,14 +400,16 @@ Influential environment variables and corresponding options: \fBGENRC_SENTINEL=1\fR \fB\-\-sentinel\fR \fBGENRC_CREATE_PIDFILE=\fINAME\fR \fB\-\-create\-pidfile=\fINAME\fR \fBGENRC_USER=\fINAME\fR \fB\-\-user=\fINAME\fR - \fBGENRC_GROUP=\fIGROUPS\fR \fB\-\-group=\fIGROUPS\fR + \fBGENRC_GROUP=\fIGROUPS\fR \fB\-\-group=\fIGROUPS\fR + \fBGENRC_LOG_FACILITY=\fIF\fR \fB\-\-log\-facility=\fIF\fR + \fBGENRC_LOG_TAG=\fISTR\fR \fB\-\-log\-tag=\fISTR\fR .fi .SH AUTHORS Sergey Poznyakoff .SH "BUG REPORTS" Report bugs to <gray@gnu.org>. .SH COPYRIGHT -Copyright \(co 2018, 2019 Sergey Poznyakoff +Copyright \(co 2018 -- 2020 Sergey Poznyakoff .br .na License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> diff --git a/src/genrc.c b/src/genrc.c index 3dccbda..5f66552 100644 --- a/src/genrc.c +++ b/src/genrc.c @@ -1,10 +1,11 @@ /* This file is part of genrc -Copyryght (C) 2018, 2019 Sergey Poznyakoff +Copyryght (C) 2018-2020 Sergey Poznyakoff License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. */ #include "genrc.h" +#include <syslog.h> #include <sys/ioctl.h> char *genrc_command; @@ -18,7 +19,8 @@ GENRC_PID_CLOSURE *genrc_pid_closure; char *genrc_create_pidfile; int genrc_verbose; char *genrc_shell = SHELL; - +int genrc_log_facility = LOG_DAEMON; +char *genrc_log_tag = NULL; enum { OPT_USAGE = 256, @@ -29,6 +31,8 @@ enum { OPT_CREATE_PIDFILE, OPT_RESTART_ON_EXIT, OPT_RESTART_ON_SIGNAL, + OPT_LOG_FACILITY, + OPT_LOG_TAG }; struct option longopts[] = { @@ -52,6 +56,8 @@ struct option longopts[] = { { "group", required_argument, 0, 'g' }, { "restart-on-exit", required_argument, 0, OPT_RESTART_ON_EXIT }, { "restart-on-signal", required_argument, 0, OPT_RESTART_ON_SIGNAL }, + { "log-facility", required_argument, 0, OPT_LOG_FACILITY }, + { "log-tag", required_argument, 0, OPT_LOG_TAG }, { NULL } }; char shortopts[2*sizeof(longopts)/sizeof(longopts[0])]; @@ -238,6 +244,9 @@ char const *help_msg[] = { " --restart-on-signal=[!]SIG[,...]", " restart the program if it terminates on one of the", " listed signals", + " --log-facility=FACILITY", + " log diagnostic messages to this syslog facility", + " --log-tag=TAG use this syslog tag instead of the program name", "", "Informational options:", "", @@ -254,6 +263,8 @@ char const *help_msg[] = { " GENRC_SENTINEL=1 --sentinel", " GENRC_USER=NAME --user=NAME", " GENRC_GROUP=GROUPS --group=GROUPS", + " GENRC_LOG_FACILITY=F --log-facility=F", + " GENRC_LOG_TAG=STR --log-tag=STR", "", "", "PID sources:", @@ -314,6 +325,8 @@ char const *usage_msg[] = { "[--command=COMMAND]", "[--group GROUP[,GROUP...]]", "[--help]", + "[--log-facility=FACILITY]", + "[--log-tag=TAG]", "[--no-reload]", "[--pid-from=SOURCE]", "[--pidfile=PIDFILE]", @@ -392,10 +405,48 @@ void version(void) { printf("%s\n", PACKAGE_STRING); - printf("Copyryght (C) 2018 Sergey Poznyakoff\n"); + printf("Copyryght (C) 2018--2020 Sergey Poznyakoff\n"); printf("%s", gplv3); } + +static struct facility_def { + char const *name; + int facility; +} facility_tab[] = { + { "auth", LOG_AUTH }, + { "authpriv",LOG_AUTHPRIV }, + { "cron", LOG_CRON }, + { "daemon", LOG_DAEMON }, + { "ftp", LOG_FTP }, + { "lpr", LOG_LPR }, + { "mail", LOG_MAIL }, + { "user", LOG_USER }, + { "local0", LOG_LOCAL0 }, + { "local1", LOG_LOCAL1 }, + { "local2", LOG_LOCAL2 }, + { "local3", LOG_LOCAL3 }, + { "local4", LOG_LOCAL4 }, + { "local5", LOG_LOCAL5 }, + { "local6", LOG_LOCAL6 }, + { "local7", LOG_LOCAL7 }, + { NULL } +}; +static int +str_to_facility(char const *str) +{ + int i; + for (i = 0; facility_tab[i].name; i++) + if (strcasecmp(str, facility_tab[i].name) == 0) + return facility_tab[i].facility; + return -1; +} + +void +genrc_openlog(void) +{ + openlog(genrc_log_tag, LOG_PID, genrc_log_facility); +} typedef int (*GENRC_COMMAND)(void); @@ -477,6 +528,12 @@ main(int argc, char **argv) case OPT_CREATE_PIDFILE: setenv("GENRC_CREATE_PIDFILE", optarg, 1); break; + case OPT_LOG_FACILITY: + setenv("GENRC_LOG_FACILITY", optarg, 1); + break; + case OPT_LOG_TAG: + setenv("GENRC_LOG_TAG", optarg, 1); + break; case 't': setenv("GENRC_TIMEOUT", optarg, 1); break; @@ -573,6 +630,16 @@ main(int argc, char **argv) genrc_pid_closure = get_pid_closure(DEFAULT_PID_SOURCE); } + if ((p = getenv("GENRC_LOG_FACILITY")) != NULL) { + if ((c = str_to_facility(p)) == -1) + usage_error("%s: unknown facility", p); + genrc_log_facility = c; + } + if ((p = getenv("GENRC_LOG_TAG")) != NULL) + genrc_log_tag = p; + else + genrc_log_tag = genrc_program; + argc -= optind; if (argc == 0) usage_error("missing command verb"); diff --git a/src/genrc.h b/src/genrc.h index e77c840..9326edd 100644 --- a/src/genrc.h +++ b/src/genrc.h @@ -147,6 +147,8 @@ extern char *genrc_create_pidfile; extern int genrc_verbose; extern char *genrc_shell; +void genrc_openlog(void); + void spawn(int *p); int sentinel(void); diff --git a/src/sentinel.c b/src/sentinel.c index 989acf1..c2c2d08 100644 --- a/src/sentinel.c +++ b/src/sentinel.c @@ -1,5 +1,5 @@ /* This file is part of genrc -Copyryght (C) 2018, 2019 Sergey Poznyakoff +Copyryght (C) 2018-2020 Sergey Poznyakoff License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. @@ -171,7 +171,7 @@ wait_loop(pid_t child, int out, int err) int nfd = (out > err ? out : err) + 1; struct log_buffer obuf, ebuf; - openlog(genrc_program, LOG_PID, LOG_DAEMON); + genrc_openlog(); log_buffer_init(&obuf, LOG_INFO); log_buffer_init(&ebuf, LOG_ERR); |