diff options
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | NEWS | 28 | ||||
-rw-r--r-- | README | 14 | ||||
-rw-r--r-- | am/proctitle.m4 | 79 | ||||
-rw-r--r-- | configure.ac | 5 | ||||
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/com_stop.c | 65 | ||||
-rw-r--r-- | src/genrc.8 | 45 | ||||
-rw-r--r-- | src/genrc.c | 46 | ||||
-rw-r--r-- | src/genrc.h | 12 | ||||
-rw-r--r-- | src/pid_ps.c | 4 | ||||
-rw-r--r-- | src/pidlist.c | 2 | ||||
-rw-r--r-- | src/proctitle.c | 160 | ||||
-rw-r--r-- | src/sentinel.c | 170 | ||||
-rw-r--r-- | src/transform.c | 10 |
15 files changed, 540 insertions, 103 deletions
diff --git a/Makefile.am b/Makefile.am index bb011c5..bf3a9d1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,13 +1,13 @@ -ACLOCAL_AMFLAGS = -I runcap +ACLOCAL_AMFLAGS = -I am -I runcap SUBDIRS = grecs runcap src dist: ChangeLog .PHONY: ChangeLog ChangeLog: $(AM_V_GEN)if test -d .git; then \ git log --pretty='format:%ct %an <%ae>%n%n%s%n%n%b%n' | \ awk -f $(top_srcdir)/@GRECS_SUBDIR@/build-aux/git2chg.awk \ > ChangeLog.tmp; \ cmp ChangeLog ChangeLog.tmp > /dev/null 2>&1 || \ mv ChangeLog.tmp ChangeLog; \ rm -f ChangeLog.tmp; \ fi @@ -1,17 +1,41 @@ -genrc NEWS -- history of user-visible changes. 2020-09-09 +genrc NEWS -- history of user-visible changes. 2022-05-13 See the end of file for copying conditions. Please send genrc bug reports to <gray@gnu.org> or <gray@gnu.org.ua> +Version 1.3.91 (git) + +* New option -k (--kill-mode) + +The option determines how the termination sequence for the program. +Its argument can be: + + group + Send both the SIGTERM and subsequent SIGKILL (if necessary) to the + process control group of the command. + process + Send both the SIGTERM and subsequent SIGKILL (if necessary) to the + main process of the command. + mixed + Send SIGTERM to the process and subsequent SIGKILL to the process + control group. + + +Version 1.3, 2021-07-08 + +* Fix the --timeout option + + +Version 1.2, 2020-09-09 * Verbose diagnostics Verbose diagnostics is enabled by the -v,--verbose option. * Shell selection By default, the program is started using "/bin/sh -c". Two options are provided to alter this behavior: -s, --shell=SHELL Use SHELL to start the program. Default is /bin/sh. @@ -71,25 +95,25 @@ For example: This will ensure that the COMMAND will be restarted immediately after it terminates, unless it exits with the status 0 or terminates on SIGTERM or SIGQUIT. Version 1.0, 2018-05-15 Initial release ========================================================================= Copyright information: -Copyright (C) 2018-2020 Sergey Poznyakoff +Copyright (C) 2018-2022 Sergey Poznyakoff Permission is granted to anyone to make or distribute verbatim copies of this document as received, in any medium, provided that the copyright notice and this permission notice are preserved, thus giving the recipient permission to redistribute in turn. Permission is granted to distribute modified versions of this document, or of portions of it, under the above conditions, provided also that they carry prominent notices stating who last changed them. Local variables: @@ -1,78 +1,78 @@ * Overview -This is a generic helper program for writing system initialization +genrc is a generic helper program for writing system initialization scripts. Depending on the operation mode, it starts, stops, reconfigures or displays the status of a specific program. Primary audience is Slackware system administrators. However, the tool is generic enough to be used on any other Linux (and not only) distribution. In the contrast to another similar programs like start-stop-daemon, it is designed so that the entire rc file can consist of only one -line, invoking (or exec'ing) genrc with the right set of arguments. +line, invoking (or exec'ing) genrc with a proper set of arguments. * Example The following is the example /etc/rc.d/rc.ntpd script: #! /bin/sh PIDFILE=/var/run/ntpd.pid exec /sbin/genrc \ --command="/usr/sbin/ntpd -g -p $PIDFILE" \ --no-reload \ --signal-stop=SIGHUP \ --pid-from="FILE:$PIDFILE" "$@" * Downloads This and newer versions of genrc can be downloaded from - http://download.gnu.org.ua/release/genrc + https://download.gnu.org.ua/release/genrc For the recent development sources, see - http://git.gnu.org.ua/cgit/genrc.git + https://git.gnu.org.ua/genrc.git * Building When building from source package, usual incantations apply: ./configure make make install If you are building from a clone of the Git repository, you will need GNU autotools to bootstrap the package first. Run - autoreconf -f -i -s + ./bootstrap in the top level source directory. This will create the configure script and populate the directory with the missing files. Then proceed as described above. * Documentation -Manpage genrc.8 included. After installing the package, run +A manpage (genrc.8) is included. After installing the package, run man genrc. * Bug reporting. Send bug reports to <gray@gnu.org>. * Copyright information: -Copyright (C) 2018 Sergey Poznyakoff +Copyright (C) 2018-2022 Sergey Poznyakoff Permission is granted to anyone to make or distribute verbatim copies of this document as received, in any medium, provided that the copyright notice and this permission notice are preserved, thus giving the recipient permission to redistribute in turn. Permission is granted to distribute modified versions of this document, or of portions of it, under the above conditions, provided also that they carry prominent notices stating who last changed them. diff --git a/am/proctitle.m4 b/am/proctitle.m4 new file mode 100644 index 0000000..d01cebe --- /dev/null +++ b/am/proctitle.m4 @@ -0,0 +1,79 @@ +dnl This file is part of Mailfromd. +dnl Copyright (C) 2008-2022 Sergey Poznyakoff +dnl +dnl This program is free software; you can redistribute it and/or modify +dnl it under the terms of the GNU General Public License as published by +dnl the Free Software Foundation; either version 3, or (at your option) +dnl any later version. +dnl +dnl This program is distributed in the hope that it will be useful, +dnl but WITHOUT ANY WARRANTY; without even the implied warranty of +dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +dnl GNU General Public License for more details. +dnl +dnl You should have received a copy of the GNU General Public License +dnl along with this program. If not, see <http://www.gnu.org/licenses/>. + +AC_DEFUN([MF_PROCTITLE],[ + AH_TEMPLATE([MF_PROCTITLE_TYPE], [Type of setproctitle implementation]) + AH_BOTTOM([ +#define MF_PROCTITLE_SETPROCTITLE 0 +#define MF_PROCTITLE_REPLACE_ARGV 1 +#define MF_PROCTITLE_REWRITE_ARGV 2 +#define MF_PROCTITLE_PSTAT 3 +#define MF_PROCTITLE_PSSTRINGS 4 +]) + AC_SUBST(MF_PROCTITLE_LIBS) + + AC_TRY_LINK([extern char *__progname, *__progname_full;], + [__progname = "foo"; __progname_full = "foo bar";], + [AC_DEFINE(HAVE___PROGNAME, 1, + [Define to 1 if libc has __progname])]) + + mf_proctitle_type=no + AC_CHECK_FUNC(setproctitle, + [mf_proctitle_type=MF_PROCTITLE_SETPROCTITLE], + [AC_CHECK_HEADERS(libutil.h) + AC_CHECK_LIB(util, setproctitle, + [mf_proctitle_type=MF_PROCTITLE_SETPROCTITLE + MF_PROCTITLE_LIBS="-lutil"])]) + + if test "$mf_proctitle_type" = no; then + AC_CHECK_HEADER([sys/pstat.h], + [AC_CHECK_FUNC([pstat], + [mf_proctitle_type=MF_PROCTITLE_PSTAT], + [mf_proctitle_type=MF_PROCTITLE_REWRITE_ARGV])]) + if test "$mf_proctitle_type" = no; then + AC_TRY_COMPILE([#include <machine/vmparam.h> + #include <sys/exec.h>], + [ +#include <sys/types.h> +#include <sys/proc.h> +#include <vm/pmap.h> +#include <machine/pmap.h> +#include <machine/vmparam.h> +#include <sys/exec.h> + +main() +{ + int i = PS_STRINGS; +} +], + [mf_proctitle_type=MF_PROCTITLE_PSSTRINGS]) + + if test "$mf_proctitle_type" = no; then + AC_EGREP_CPP(yes,[ +#if defined(__GNU_HURD__) + yes +#endif +], + [mf_proctitle_type=MF_PROCTITLE_REPLACE_ARGV], + [mf_proctitle_type=MF_PROCTITLE_REWRITE_ARGV]) + fi + fi + fi + + AC_DEFINE_UNQUOTED([MF_PROCTITLE_TYPE],$mf_proctitle_type) + +]) +
\ No newline at end of file diff --git a/configure.ac b/configure.ac index 2610762..41ff5ef 100644 --- a/configure.ac +++ b/configure.ac @@ -1,51 +1,52 @@ # This file is part of genrc. -*- Autoconf -*- -# Copyright (C) 2018 Sergey Poznyakoff. +# Copyright (C) 2018-2022 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 # the Free Software Foundation; either version 3, or (at your option) # any later version. # # Genrc is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with genrc. If not, see <http://www.gnu.org/licenses/>. AC_PREREQ([2.69]) -AC_INIT([genrc], [1.1], [gray@gnu.org]) +AC_INIT([genrc], [1.3.91], [gray@gnu.org]) AC_CONFIG_SRCDIR([src/genrc.c]) AC_CONFIG_HEADERS([config.h]) AM_INIT_AUTOMAKE([1.11 foreign silent-rules]) AC_CONFIG_MACRO_DIR(grecs/am) # Enable silent rules by default AM_SILENT_RULES([yes]) # Checks for programs. AC_PROG_CC AC_PROG_RANLIB # Checks for libraries. AC_CHECK_LIB(pcre, main) # Checks for header files. AC_CHECK_HEADERS([getopt.h pcre.h]) # Checks for library functions. AC_CHECK_FUNCS([getdtablesize]) +MF_PROCTITLE GRECS_SETUP(grecs, [all-parsers git2chg]) RUNCAP_SETUP AM_CONDITIONAL([COND_PCRE], [test "$ac_cv_header_pcre_h" = yes && test "$ac_cv_lib_pcre_main" = yes]) if test -f /proc/$$/cmdline; then DEFAULT_PID_SOURCE='"PROC"' else DEFAULT_PID_SOURCE='"PS"' fi AC_DEFINE_UNQUOTED(DEFAULT_PID_SOURCE,$DEFAULT_PID_SOURCE, diff --git a/src/Makefile.am b/src/Makefile.am index 168ab11..d0cf883 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -18,24 +18,25 @@ sbin_PROGRAMS = genrc genrc_SOURCES = \ genrc.c\ genrc.h\ err.c\ pidfrom.c\ pid_file.c\ pid_config.c\ pid_grep.c\ pid_proc.c\ pid_ps.c\ pidlist.c\ procscan.c\ + proctitle.c\ com_status.c\ com_start.c\ com_stop.c\ com_restart.c\ com_reload.c\ transform.c\ match_exact.c\ match_glob.c\ match_regex.c\ sentinel.c\ runas.c diff --git a/src/com_stop.c b/src/com_stop.c index d57fa5e..ffa2ff9 100644 --- a/src/com_stop.c +++ b/src/com_stop.c @@ -1,75 +1,96 @@ /* This file is part of genrc -Copyryght (C) 2018 Sergey Poznyakoff +Copyryght (C) 2018-2022 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" static void timermul(struct timeval *a, int b) { a->tv_sec *= b; a->tv_usec *= b; a->tv_sec = a->tv_sec + a->tv_usec / 1000000; a->tv_usec %= 1000000; } #define MIN_POLL_TTW 20000 -int -com_stop(void) +static int +pidlist_wait(PIDLIST *pids) { - PIDLIST pids; struct timeval stoptime, before, after, ttw, maxttw; int ratio; - pidlist_init(&pids); - if (get_pid_list(genrc_pid_closure, &pids)) { - genrc_error("program status unknown"); - return 1; - } - - if (pids.pidc == 0) { - genrc_error("%s not running", genrc_program); - return 1; - } - - if (genrc_verbose) - printf("Stopping %s\n", genrc_program); - gettimeofday(&stoptime, NULL); stoptime.tv_sec += genrc_timeout; ratio = 1; - while (pids.pidc) { + + /* Wait for the process to terminate */ + while (pids->pidc) { gettimeofday(&before, NULL); if (timercmp(&before, &stoptime, >)) break; - pidlist_kill(&pids, genrc_signal_stop); - if (get_pid_list(genrc_pid_closure, &pids)) + if (get_pid_list(genrc_pid_closure, pids)) break; - if (pids.pidc == 0) + if (pids->pidc == 0) return 0; gettimeofday(&after, NULL); if (timercmp(&after, &stoptime, >=)) break; if (ratio < 10) ++ratio; timersub(&stoptime, &after, &maxttw); timersub(&after, &before, &ttw); timermul(&ttw, ratio); if (ttw.tv_sec < 0 || ttw.tv_usec < 0) ttw.tv_sec = ttw.tv_usec = 0; if (timercmp(&ttw, &maxttw, >)) ttw = maxttw; if (ttw.tv_sec == 0 && ttw.tv_usec < MIN_POLL_TTW) ttw.tv_usec = MIN_POLL_TTW; if (select(0, NULL, NULL, NULL, &ttw) < 0 && errno != EINTR) { system_error(errno, "select"); break; } } + return pids->pidc > 0; +} + +int +com_stop(void) +{ + PIDLIST pids; + + pidlist_init(&pids); + if (get_pid_list(genrc_pid_closure, &pids)) { + genrc_error("program status unknown"); return 1; } + if (pids.pidc == 0) { + genrc_error("%s not running", genrc_program); + return 1; + } + + if (genrc_verbose) + printf("Stopping %s\n", genrc_program); + + /* Send the stop signal */ + if (genrc_kill_mode == genrc_kill_group && pids.pidc == 1) + pids.pidv[0] = - pids.pidv[0]; + pidlist_kill(&pids, genrc_signal_stop); + + /* Wait for the process to terminate */ + if (pidlist_wait(&pids)) { + /* Forcefully terminate the remaining processes */ + if (genrc_kill_mode == genrc_kill_mixed && pids.pidc == 1) + pids.pidv[0] = - pids.pidv[0]; + pidlist_kill(&pids, SIGKILL); + return pidlist_wait(&pids); + } + return 0; +} + diff --git a/src/genrc.8 b/src/genrc.8 index c4715da..beb599c 100644 --- a/src/genrc.8 +++ b/src/genrc.8 @@ -1,49 +1,51 @@ .\" This file is part of genrc. -.\" Copyright (C) 2018-2020 Sergey Poznyakoff. +.\" Copyright (C) 2018-2022 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 .\" the Free Software Foundation; either version 3, or (at your option) .\" any later version. .\" .\" Genrc is distributed in the hope that it will be useful, .\" but WITHOUT ANY WARRANTY; without even the implied warranty of .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the .\" GNU General Public License for more details. .\" .\" 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 9, 2020" "GENRC" "Genrc User Manual" +.TH GENRC 8 "March 26, 2022" "GENRC" "Genrc User Manual" .SH NAME genrc \- generic system initialization script helper .SH SYNOPSIS .nh .na \fBgenrc\fR\ [\fB\-hev\fR]\ [\fB\-F\fR \fIPIDFILE\fR]\ [\fB\-P\fR \fISOURCE\fR]\ [\fB\-c\fR \fICOMMAND\fR]\ [\fB\-g\fR \fIGROUP\fR[,\fIGROUP\fR...]]\ [\fB\-l\fR \fILIMIT\fR]\ + [\fB\-k\fR \fIMODE\fR]\ [\fB\-p\fR \fIPROGRAM\fR]\ [\fB\-s\fR \fISHELL\fR]\ [\fB\-t\fR \fISECONDS\fR]\ [\fB\-u\fR \fIUSER\fR]\ [\fB\-\-command=\fICOMMAND\fR]\ [\fB\-\-create\-pidfile=\fIPIDFILE\fR]\ [\fB\-\-exec\fR]\ [\fB\-\-group=\fIGROUP\fR[,\fIGROUP\fR...]]\ [\fB\-\-help\fR]\ + [\fB\-\-kill\-mode=\fIMODE\fR]\ [\fB\-\-log\-facility=\fIFACILITY\fR]\ [\fB\-\-log\-tag=\fITAG\fR]\ [\fB\-\-limit=\fILIMIT\fR]\ [\fB\-\-no\-reload\fR]\ [\fB\-\-pid\-from=\fISOURCE\fR]\ [\fB\-\-pidfile=\fIPIDFILE\fR]\ [\fB\-\-program=\fIPROGRAM\fR]\ [\fB\-\-restart\-on\-exit=\fR[\fB!\fR]\fISTATUS\fR[\fB,\fISTATUS\fR...]]\ [\fB\-\-restart\-on\-signal=\fR[\fB!\fR]\fISIG\fR[\fB,\fISIG\fR...]]\ [\fB\-\-sentinel\fR]\ [\fB\-\-shell=\fISHELL\fR]\ [\fB\-\-signal\-reload=\fISIG\fR]\ @@ -60,25 +62,25 @@ genrc \- generic system initialization script helper \fBrestart\fR\ |\ \fBreload\fR\ |\ \fBstatus\fR\ } .ad .hy .SH DESCRIPTION .B genrc is a generic helper program for writing system initialization scripts. Depending on the operation mode, it starts, stops, -reconfigures or displays the status of a specific program. +reconfigures or displays current status of a specific program. .PP The operation mode of the program is set by its only mandatory argument. Other program settings are specified via the command line options or environment variables. Most options have a corresponding environment variable. For example, the command line of the program to be run is given by the \fB\-\-command\fR option, or by the \fBGENRC_COMMAND\fR environment variable. If both the variable is set and the option is supplied, the later takes precedence over the former. .PP The \fB\-\-program\fR option or the \fBGENRC_PROGRAM\fR environment variable supplies the name of the program, which is used to determine @@ -86,38 +88,38 @@ whether the program is already running. If not supplied, the first word (in the shell sense) from \fICOMMAND\fR is used. .PP If \fB\-\-program\fR is given, but \fB\-\-command\fR is not, its value will be used as the command to run. .PP The program operation modes are: .SS start If given this argument, \fBgenrc\fR runs the supplied command. Before, it checks if the program is not already running and refuses to start its second copy if so. .PP It is supposed that the program to be run will detach from the -controlling terminal and will continue running in the background (i.e. it +controlling terminal and continue running in the background (i.e. it is a \fIdaemon\fR, in UNIX sense). If it is not the case, use the \fB\-\-sentinel\fR option. With this option, \fBgenrc\fR will start -the command and become a daemon itself, controlling the execution +the command and become a daemon, controlling the execution of the program. It will exit when the command terminates. So long as the command runs, \fBgenrc\fR will pipe its standard output and error 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\-\-create\-pidfile=\fIFILENAME\fR option is given together with -\fB\-\-sentinel\fR, the PID of the subsidiary command will be stored +\fB\-\-sentinel\fR, the PID of the started command will be stored in \fIFILE\fR. The file will be unlinked after the subsidiary command -terminates. Unless the \fB\-\-pid\-from\fR option is given, +terminates. Unless the \fB\-\-pid\-from\fR option is also given, \fB\-\-pid\-from=FILE:\fIFILENAME\fR will be assumed. .PP In sentinel mode, it is possible to restart the program if it terminates with a specific exit code or on a specific signal. This is controlled by the \fB\-\-restart\-on\-exit\fR and \fB\-\-restart\-on\-signal\fR options. Use this feature to ensure the service provided by the program won't get terminated because of hitting a bug or encountering an unforeseen external condition. For example, the following two options make sure that the program will be terminated only if it exits with status 0 or is delivered the SIGTERM or SIGQUIT signal: .EX @@ -132,44 +134,55 @@ minutes, \fBgenrc\fR will disable subsequent restarts for the next 5 minutes. If the \fB\-\-create\-pidfile\fR option was used, the PID of the controlling \fBgenrc\fR process will be stored in the file during that interval. If the \fBSIGHUP\fR signal is delivered during the sleep interval, the sleep will be broken prematurely and the program restarted again. .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 effect, it uses an abstraction called \fIPID source\fR, which allows it to determine the PID of the program. .PP The default PID source is the Linux \fB/proc\fR filesystem (or, if it -is not available, the output of \fBps -ef\fR), which is scanned for +is not available, the output of \fBps -efw\fR), which is scanned for the name of the program (as given by \fB\-\-program\fR or \fB\-\-command\fR options). .PP The source to use can be supplied with the \fB\-\-pid\-from\fR option (or the \fB\-\-pidfile\R option, which is equivalent to \fB\-\-pid\-from=FILE:\fR). See the section \fBPID SOURCES\fR for a detailed discussion of available sources. .SS stop In the \fBstop\fR mode \fBgenrc\fR stops the command by sending it \fBSIGTERM\fR (or another signal, as supplied with the \fB\-\-signal\-stop\fR option). If the PID source returns multiple PIDs, by default only parent PID is selected. However, \fBgenrc\fR can be instructed to signal all PIDs instead (see the \fBa\fR flag in the description of \fBPROC\fR or \fBPS\fR PID source). .PP After sending the signal, the program will wait for all processes to -terminate. It will report an error if they don't terminate within 5 -seconds. This timeout can be changed using the \fB\-\-timeout\fR +terminate. If they don't terminate within 5 seconds, \fBgenrc\fR will +send the \fBSIGKILL\fR signal and wait another 5 seconds for them to +terminate. The timeout can be changed using the \fB\-\-timeout\fR option. +.PP +The way to send the signals to the program is determined by the +\fB\-k\fR (\fB\-\-kill\-mode\fR) option. If its value is \fBgroup\fR, +both \fBSIGTERM\fR and, if necessary subsequent \fBSIGKILL\fR will be +sent to all processes in the process control group. This is the +default mode if the command is a daemon (i.e., if \fB\-\-sentinel\fR) +is not given). If the value is \fBprocess\fR, the signal will be sent +to the process individually. Finally, if the value is \fBmixed\fR, the +\fBSIGTERM\fR is sent to the main process, while the subsequent +\fBSIGKILL\fR is sent to the control group of the process. .SS restart Restarts the program. It is equivalent to running .B genrc stop immediately followed by .BR "genrc start" . .SS reload Attempts to reload (or reconfigure) the program by sending it the \fBSIGHUP\fR signal (or another signal, as given with the \fB\-\-signal\-reload\fR option). The \fB\-\-no\-reload\fR or \fB\-\-signal\-reload=0\fR option disables this behavior, making this mode equivalent to .BR "genrc restart" . @@ -207,24 +220,33 @@ Name of the PID file (same as \fB\-\-pid\-from=FILE:\fINAME\fR) \fB\-h\fR, \fB\-\-help\fR Display a short help list. .TP \fB\-g\fR, \fB\-\-group=\fIGROUP\fR[,\fIGROUP\fR...] Run program with this \fIGROUP\fR privileges. If the argument is a list of groups, the first group becomes the principal, and the rest of them supplementary groups. Each \fIGROUP\fR is either a group name or a numeric group number prefixed with a plus sign. Whatever notation is used, the groups must exist in the system group database. See also the \fB\-\-user\fR option. .TP +\fB\-k\fR, \fB\-\-kill\-mode=\fIMODE\fR +Specifies how to send termination signal to the main +process. \fIMODE\fR is one of \fBgroup\fR (send termination signal to +the process control group), \fBprocess\fR (send it to the process +itself), or \fBmixed\fR (send termination signal to the process, and +send subsequent \fBSIGKILL\fR to the program control group). Refer to +the description of the \fBstop\fR command above for a detailed +discussion of these three modes. +.TP \fB\-l\fR, \fB\-\-limit=\fILIM\fR Set resource limit. \fILIM\fR is a resource letter followed by a value. Resource letters are: .RS .TP .B c Core file size (KB). .TP .B d Data size (KB). .TP .B f @@ -434,33 +456,34 @@ Influential environment variables and corresponding options: .nf .ta 5n 35n .ul Variable Option \fBGENRC_COMMAND=\fICOMMAND\fR \fB\-\-command=\fICOMMAND\fR \fBGENRC_PROGRAM=\fINAME\fR \fB\-\-program=\fINAME\fR \fBGENRC_PID_FROM=\fISOURCE\fR \fB\-\-pid\-from=\fISOURCE\fR \fBGENRC_TIMEOUT=\fISECONDS\fR \fB\-\-timeout=\fISECONDS\fR \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_KILL_MODE=\fIMODE\fR \fB\-\-kill\-mode=\fIMODE\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 -- 2020 Sergey Poznyakoff +Copyright \(co 2018 -- 2022 Sergey Poznyakoff .br .na License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> .br .ad This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. .\" Local variables: .\" eval: (add-hook 'write-file-hooks 'time-stamp) .\" time-stamp-start: ".TH [A-Z_][A-Z0-9_.\\-]* [0-9] \"" .\" time-stamp-format: "%:B %:d, %:y" .\" time-stamp-end: "\"" diff --git a/src/genrc.c b/src/genrc.c index 04845d0..c6ebcaf 100644 --- a/src/genrc.c +++ b/src/genrc.c @@ -1,30 +1,31 @@ /* This file is part of genrc -Copyryght (C) 2018-2020 Sergey Poznyakoff +Copyryght (C) 2018-2022 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> #include <sys/resource.h> char *genrc_command; char *genrc_program; char *genrc_pid_from; unsigned genrc_timeout = 5; int genrc_no_reload; int genrc_signal_stop = SIGTERM; int genrc_signal_reload = SIGHUP; +enum genrc_kill_mode genrc_kill_mode; 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, OPT_VERSION, OPT_SIGNAL_RELOAD, OPT_NO_RELOAD, @@ -51,24 +52,25 @@ struct option longopts[] = { { "shell", no_argument, 0, 's' }, { "exec", no_argument, 0, 'e' }, { "create-pidfile", required_argument, 0, OPT_CREATE_PIDFILE }, { "version", no_argument, 0, OPT_VERSION }, { "verbose", no_argument, 0, 'v' }, { "user", required_argument, 0, 'u' }, { "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 }, { "limit", required_argument, 0, 'l' }, + { "kill-mode", required_argument, 0, 'k' }, { NULL } }; char shortopts[2*sizeof(longopts)/sizeof(longopts[0])]; static void shortopts_setup(void) { int i; char *p; for (i = 0, p = shortopts; longopts[i].name; i++) { if (longopts[i].val < 128) { @@ -226,39 +228,46 @@ char const *help_msg[] = { " f file size (KB)", " l locked-in-memory address space (KB)", " m resident set size (KB)", " n number of open files", " p process priority -20..20", " s stack size (KB)", " t CPU time (seconds)", " u number of subprocesses", " v virtual memory (KB)", "", "Additional configuration:", "", + " -k, --kill-mode=MODE set program termination mode: group, program,", + " or mixed", " -t, --timeout=SECONDS time to wait for the program to start up or", " terminate", " -P, --pid-from=SOURCE where to look for PIDs of the running programs", " -F, --pidfile=NAME name of the PID file", " (same as --pid-from=FILE:NAME)", " --signal-reload=SIG signal to send on reload (default: SIGHUP)", " setting to 0 is equivalent to --no-reload", " --no-reload makes reload equivalent to restart", " --signal-stop=SIG signal to send in order to terminate the program", " (default: SIGTERM)", + " -v. --verbose enable verbose diagnostics", "", "Sentinel mode:", "", - " --sentinel PROGRAM runs in foreground; disconnect from the", + " -S, --sentinel PROGRAM runs in foreground; disconnect from the", " controlling terminal, run it and act as a sentinel", + " --create-pidfile=FILE", + " store PID of the PROGRAM in FILE; implies", + " --pid-from=FILE:FILENAME, unless --pid-from is also", + " given", " -s, --shell=SHELL use SHELL instead of /bin/sh;", " --shell=none to exec PROGRAM directly", " -e, --exec same as --shell=none", " --restart-on-exit=[!]CODE[,...]", " restart the program if it exits with one of the", " listed status codes", " --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", @@ -271,24 +280,25 @@ char const *help_msg[] = { "", "Influential environment variables and corresponding options:", "", " GENRC_COMMAND=COMMAND --command=COMMAND", " GENRC_PROGRAM=NAME --program=NAME", " GENRC_PID_FROM=SOURCE --pid-from=SOURCE", " GENRC_TIMEOUT=SECONDS --timeout=SECONDS", " 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", + " GENRC_KILL_MODE=MODE --kill-mode=MODE", "", "", "PID sources:", "", " FILE:<NAME>", " Read PID from the file <NAME>", "", " CONFIG:<LANG>:<FILENAME>:<FQRN>", " Name of the PID file is stored in relation <FQRN> of the configuration", " file <FILENAME>, written in language <LANG>", "", " GREP:<FILE>:s/<RX>/<REPL>/[<FLAGS>][;...]", @@ -301,55 +311,57 @@ char const *help_msg[] = { " Look for matching program in /proc/<PID>/*. If <EXE> is not supplied", " or empty, program name from --program will be used. <FLAGS> are:", " e exact match", " g glob pattern match", " x extended POSIX regexp match (default)", " p PCRE match", " i case-insensitive match", "", " c match entire command line", " r match real executable name (instead of argv0)", " a signal all matching PIDs", "", - " PS:[:[<EXE>][:<FLAGS>]]", - " Look for matching program in the output of 'ps -ef'. <EXE> and <FLAGS>", + " PS[:[<EXE>][:<FLAGS>]]", + " Look for matching program in the output of 'ps -efw'. <EXE> and <FLAGS>", " as described above", "", NULL }; void help(void) { int i; for (i = 0; help_msg[i]; i++) puts(help_msg[i]); } char const *usage_msg[] = { "genrc", "[-hev]", "[-F PIDFILE]", "[-P SOURCE]", "[-c COMMAND]", "[-g GROUP[,GROUP...]]", + "[-k MODE]", "[-l LIM]", "[-p PROGRAM]", "[-s SHELL]", "[-t SECONDS]", "[-u USER]", "[--command=COMMAND]", "[--group GROUP[,GROUP...]]", "[--help]", + "[--kill-mode=MODE]", "[--limit=LIM]", "[--log-facility=FACILITY]", "[--log-tag=TAG]", "[--no-reload]", "[--pid-from=SOURCE]", "[--pidfile=PIDFILE]", "[--program=PROGRAM]", "[--restart-on-exit=[!]CODE[,...]]", "[--restart-on-signal=[!]SIG[,...]]", "[--sentinel]", "[--shell=SHELL]", "[--signal-reload=SIG]", @@ -491,25 +503,25 @@ usage(void) } static const char gplv3[] = "License GPLv3+: GNU GPL version 3 or later " "<http://gnu.org/licenses/gpl.html>\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\n"; void version(void) { printf("%s\n", PACKAGE_STRING); - printf("Copyryght (C) 2018--2020 Sergey Poznyakoff\n"); + printf("Copyryght (C) 2018-2021 Sergey Poznyakoff\n"); printf("%s", gplv3); } static struct facility_def { char const *name; int facility; } facility_tab[] = { { "auth", LOG_AUT |