aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile.am2
-rw-r--r--NEWS76
-rw-r--r--README14
-rw-r--r--am/proctitle.m479
-rw-r--r--configure.ac5
m---------grecs0
-rw-r--r--src/Makefile.am1
-rw-r--r--src/com_start.c4
-rw-r--r--src/com_stop.c65
-rw-r--r--src/genrc.8114
-rw-r--r--src/genrc.c218
-rw-r--r--src/genrc.h14
-rw-r--r--src/pid_ps.c4
-rw-r--r--src/pidlist.c2
-rw-r--r--src/proctitle.c160
-rw-r--r--src/sentinel.c174
-rw-r--r--src/transform.c10
17 files changed, 832 insertions, 110 deletions
diff --git a/Makefile.am b/Makefile.am
index bb011c5..bf3a9d1 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,7 +1,7 @@
-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' | \
diff --git a/NEWS b/NEWS
index 57d7295..70bacd4 100644
--- a/NEWS
+++ b/NEWS
@@ -1,12 +1,82 @@
-genrc NEWS -- history of user-visible changes. 2018-05-20
-Copyright (C) 2018 Sergey Poznyakoff
+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.
+
+ -e, --exec
+ In sentinel mode, run the command directly via exec(3), instead
+ of via the shell.
+
+* Syslog
+
+In sentinel mode, the syslog facility and tag can be changed:
+
+ --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)
+
+* Runtime resource limits
+
+The --limit (-l) option is provided to control the resource usage.
+Its argument is a resource designator followed by the limit value
+for that resource. Resource designators are:
+
+ c Core file size (KB).
+ d Data size (KB).
+ f File size (KB).
+ l Locked-in-memory address space (KB).
+ m Resident set size (KB).
+ n Number of open files.
+ p Process priority (nice value), -20..20.
+ s Stack size (KB).
+ t CPU time (seconds).
+ u Number of subprocesses.
+ v Virtual memory size (KB).
+
Version 1.1, 2018-05-20
Implements restart mode.
The restart mode is a special feature of sentinel mode, controlled by
the new options --restart-on-exit and --restart-on-signal. In this
@@ -31,13 +101,13 @@ Version 1.0, 2018-05-15
Initial release
=========================================================================
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.
diff --git a/README b/README
index e9a0c5b..e882a91 100644
--- a/README
+++ b/README
@@ -1,19 +1,19 @@
* 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
@@ -25,48 +25,48 @@ exec /sbin/genrc \
--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.
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,8 +1,8 @@
# 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.
#
@@ -12,13 +12,13 @@
# 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
@@ -34,12 +34,13 @@ 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])
diff --git a/grecs b/grecs
-Subproject b06fb7d30415eec1d2efb39286ab96070c724fd
+Subproject 893d875a4065acb757fef55876c391b1dd07004
diff --git a/src/Makefile.am b/src/Makefile.am
index 168ab11..d0cf883 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -24,12 +24,13 @@ genrc_SOURCES = \
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\
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,8 +1,8 @@
/* 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>
@@ -124,13 +124,13 @@ spawn(int *p)
close(i);
}
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);
exit(127);
}
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,8 +1,8 @@
/* 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"
@@ -14,44 +14,30 @@ timermul(struct timeval *a, int 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;
@@ -66,10 +52,45 @@ com_stop(void)
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 0fdfa54..beb599c 100644
--- a/src/genrc.8
+++ b/src/genrc.8
@@ -1,8 +1,8 @@
.\" This file is part of genrc.
-.\" Copyright (C) 2018, 2019 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.
.\"
@@ -10,33 +10,39 @@
.\" 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 13, 2019" "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...]]\
@@ -62,13 +68,13 @@ genrc \- generic system initialization script helper
.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,
@@ -88,26 +94,26 @@ 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
@@ -134,13 +140,13 @@ the program restarted again.
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
@@ -151,15 +157,26 @@ In the \fBstop\fR mode \fBgenrc\fR stops the command by sending it
\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
@@ -209,12 +226,81 @@ 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
+File size (KB).
+.TP
+.B l
+Locked-in-memory address space (KB).
+.TP
+.B m
+Resident set size (KB).
+.TP
+.B n
+Number of open files.
+.TP
+.B p
+Process priority (nice value), -20..20.
+.TP
+.B s
+Stack size (KB).
+.TP
+.B t
+CPU time (seconds).
+.TP
+.B u
+Number of subprocesses.
+.TP
+.B v
+Virtual memory size (KB).
+.RE
+.TP
+.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
\fB\-p\fR, \fB\-\-program=\fIPROGRAM\fR
Name of the program to run.
.TP
@@ -247,12 +333,15 @@ restart the program unless it terminates on one of the listed signals.
\fB\-\-sentinel\fR
\fIPROGRAM\fR runs in foreground; disconnect from the controlling
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.
.TP
\fB\-s\fR, \fB\-\-shell=\fISHELL\fR
In sentinel mode, use \fISHELL\fR to run the command, instead of the
@@ -373,19 +462,22 @@ Influential environment variables and corresponding options:
\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, 2019 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.
diff --git a/src/genrc.c b/src/genrc.c
index 3dccbda..c6ebcaf 100644
--- a/src/genrc.c
+++ b/src/genrc.c
@@ -1,37 +1,43 @@
/* This file is part of genrc
-Copyryght (C) 2018, 2019 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,
OPT_SIGNAL_STOP,
OPT_CREATE_PIDFILE,
OPT_RESTART_ON_EXIT,
OPT_RESTART_ON_SIGNAL,
+ OPT_LOG_FACILITY,
+ OPT_LOG_TAG
};
struct option longopts[] = {
{ "help", no_argument, 0, 'h' },
{ "usage", no_argument, 0, OPT_USAGE },
{ "command", required_argument, 0, 'c' },
@@ -49,12 +55,16 @@ struct option longopts[] = {
{ "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)
@@ -203,44 +213,67 @@ char const *help_msg[] = {
"",
" At least one of COMMAND or PROGRAM must be given.",
" If PROGRAM is supplied, but COMMAND is not, COMMAND is set to PROGRAM",
" Otherwise, if COMMAND is set, but PROGRAM is not, PROGRAM is set to the",
" first word (in the shell sense) in COMMAND.",
"",
- "Runtime privileges:",
+ "Runtime privileges and limits:",
"",
" -u, --user=NAME run with this user privileges",
" -g, --group=GROUP[,GROUP...]]",
" run with this group(s) privileges",
+ " -l, --limit=LIM set resource limit; LIM is a resource letter",
+ " followed by the resource value; resource letters are:",
+ " c core file size (KB)",
+ " d data size (KB)",
+ " 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",
"",
"Informational options:",
"",
" -h, --help display this help list",
" --usage display short usage information",
" --version display program version and exist",
@@ -251,12 +284,15 @@ char const *help_msg[] = {
" 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>",
@@ -281,14 +317,14 @@ char const *help_msg[] = {
" 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
@@ -304,19 +340,25 @@ 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[,...]]",
@@ -354,12 +396,90 @@ screen_width(void)
if (ws.ws_col == 0)
ws.ws_col = 80;
}
return ws.ws_col;
}
+static struct {
+ int letter;
+ int resource;