From 6f6211b261abdd0c342a108f8766ddc613da034a Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Tue, 15 May 2018 14:01:22 +0300 Subject: Version 1.0 --- configure.ac | 4 ++-- src/genrc.8 | 13 +++++++++++-- src/genrc.c | 19 +++++++++++++++---- src/genrc.h | 2 +- src/sentinel.c | 8 ++++---- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/configure.ac b/configure.ac index 5b4774a..8cf75a7 100644 --- a/configure.ac +++ b/configure.ac @@ -15,7 +15,7 @@ # along with genrc. If not, see . AC_PREREQ([2.69]) -AC_INIT([genrc], [0.1], [gray@gnu.org]) +AC_INIT([genrc], [1.0], [gray@gnu.org]) AC_CONFIG_SRCDIR([src/genrc.c]) AC_CONFIG_HEADERS([config.h]) AM_INIT_AUTOMAKE([1.11 foreign silent-rules]) @@ -37,7 +37,7 @@ AC_CHECK_HEADERS([getopt.h pcre.h]) # Checks for library functions. AC_CHECK_FUNCS([getdtablesize]) -GRECS_SETUP(grecs, [all-parsers git2chg]) +GRECS_SETUP(grecs, [all-parsers]) AM_CONDITIONAL([COND_PCRE], [test "$ac_cv_header_pcre_h" = yes && test "$ac_cv_lib_pcre_main" = yes]) diff --git a/src/genrc.8 b/src/genrc.8 index 1255b45..920252e 100644 --- a/src/genrc.8 +++ b/src/genrc.8 @@ -26,6 +26,7 @@ genrc \- generic system initialization script helper [\fB\-p\fR \fIPROGRAM\fR]\ [\fB\-t\fR \fISECONDS\fR]\ [\fB\-\-command=\fICOMMAND\fR]\ + [\fB\-\-create\-pidfile=\fIPIDFILE\fR]\ [\fB\-\-help\fR]\ [\fB\-\-no\-reload\fR]\ [\fB\-\-pid\-from=\fISOURCE\fR]\ @@ -89,10 +90,11 @@ to syslog facility \fBdaemon\fR. The standard output will be logged with the priority \fBinfo\fR and the error with the priority \fBerr\fR. .PP -If the \fB\-\-pidfile=\fIFILE\fR option is given together with +If the \fB\-\-create\-pidfile=\fIFILENAME\fR option is given together with \fB\-\-sentinel\fR, the PID of the subsidiary command will be stored in \fIFILE\fR. The file will be unlinked after the subsidiary command -terminates. +terminates. Unless the \fB\-\-pid\-from\fR option is given, +\fB\-\-pid\-from=FILE:\fIFILENAME\fR will be assumed. .SS status In \fBstatus\fR mode \fBgenrc\fR verifies if the \fICOMMAND\fR is already running and outputs its status on the standard output. To this @@ -149,6 +151,13 @@ exec /sbin/genrc \\ \fB\-c\fR, \fB\-\-command=\fICOMMAND\fR Command line to run. .TP +\fB\-\-create\-pidfile=\fINAME\fR +When used together with \fB\-\-sentinel\fR, the PID of the command +being run will be stored in file \fINAME\fR. The +\fB\-\-pid\-from=FILE:\fINAME\fR will be assumed, unless the +\fB\-\-pid\-from\fR is given explicitly (or the \fBGENRC_PID_FROM\fR +variable is set). +.TP \fB\-F\fR, \fB\-\-pidfile=\fINAME\fR Name of the PID file (same as \fB\-\-pid\-from=FILE:\fINAME\fR) .TP diff --git a/src/genrc.c b/src/genrc.c index ecc26fe..3f89d03 100644 --- a/src/genrc.c +++ b/src/genrc.c @@ -15,7 +15,7 @@ int genrc_no_reload; int genrc_signal_stop = SIGTERM; int genrc_signal_reload = SIGHUP; GENRC_PID_CLOSURE *genrc_pid_closure; -char *genrc_pidfile; +char *genrc_create_pidfile; enum { @@ -23,7 +23,8 @@ enum { OPT_VERSION, OPT_SIGNAL_RELOAD, OPT_NO_RELOAD, - OPT_SIGNAL_STOP + OPT_SIGNAL_STOP, + OPT_CREATE_PIDFILE }; struct option longopts[] = { @@ -38,6 +39,7 @@ struct option longopts[] = { { "no-reload", no_argument, 0, OPT_NO_RELOAD }, { "signal-stop", required_argument, 0, OPT_SIGNAL_STOP }, { "sentinel", no_argument, 0, 'S' }, + { "create-pidfile", required_argument, 0, OPT_CREATE_PIDFILE }, { "version", no_argument, 0, OPT_VERSION }, { NULL } }; @@ -147,7 +149,7 @@ sig_name_to_str(char const *s) char const *help_msg[] = { "Usage: genrc [OPTIONS] COMMAND", - "generic rc program\n", + "generic system initialization script helper\n", "COMMANDs are:\n", " start start the program", " stop stop the program", @@ -377,12 +379,14 @@ main(int argc, char **argv) setenv("GENRC_PID_FROM", optarg, 1); break; case 'F': - genrc_pidfile = optarg; p = xmalloc(6 + strlen(optarg)); strcat(strcpy(p, "FILE:"), optarg); setenv("GENRC_PID_FROM", p, 1); free(p); break; + case OPT_CREATE_PIDFILE: + setenv("GENRC_CREATE_PIDFILE", optarg, 1); + break; case 't': setenv("GENRC_TIMEOUT", optarg, 1); break; @@ -454,8 +458,15 @@ main(int argc, char **argv) wordsplit_free(&ws); } + genrc_create_pidfile = getenv("GENRC_CREATE_PIDFILE"); + if ((p = getenv("GENRC_PID_FROM")) != NULL) { genrc_pid_closure = get_pid_closure(p); + } else if (genrc_create_pidfile) { + p = xmalloc(6 + strlen(genrc_create_pidfile)); + strcat(strcpy(p, "FILE:"), genrc_create_pidfile); + genrc_pid_closure = get_pid_closure(p); + free(p); } else { genrc_pid_closure = get_pid_closure("PROC"); } diff --git a/src/genrc.h b/src/genrc.h index 82e29d6..13b40d7 100644 --- a/src/genrc.h +++ b/src/genrc.h @@ -131,7 +131,7 @@ extern int genrc_no_reload; extern int genrc_signal_reload; extern int genrc_signal_stop; extern GENRC_PID_CLOSURE *genrc_pid_closure; -extern char *genrc_pidfile; +extern char *genrc_create_pidfile; int sentinel(void); diff --git a/src/sentinel.c b/src/sentinel.c index 55395b3..1d44763 100644 --- a/src/sentinel.c +++ b/src/sentinel.c @@ -23,9 +23,9 @@ xpipe(int p[2]) static void write_pid_file(pid_t pid) { - if (genrc_pidfile) { + if (genrc_create_pidfile) { FILE *fp; - fp = fopen(genrc_pidfile, "w"); + fp = fopen(genrc_create_pidfile, "w"); fprintf(fp, "%lu\n", (unsigned long)pid); fclose(fp); } @@ -83,8 +83,8 @@ wait_loop(pid_t child, int out, int err) int rc, status; if (waitpid(child, &status, WNOHANG) == child) { - if (genrc_pidfile) - unlink(genrc_pidfile); + if (genrc_create_pidfile) + unlink(genrc_create_pidfile); if (WIFEXITED(status)) { syslog(LOG_INFO, "%s exited with status %d", genrc_program, WEXITSTATUS(status)); -- cgit v1.2.1