aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile.am2
-rw-r--r--NEWS28
-rw-r--r--README14
-rw-r--r--am/proctitle.m479
-rw-r--r--configure.ac5
-rw-r--r--src/Makefile.am1
-rw-r--r--src/com_stop.c67
-rw-r--r--src/genrc.845
-rw-r--r--src/genrc.c46
-rw-r--r--src/genrc.h12
-rw-r--r--src/pid_ps.c4
-rw-r--r--src/pidlist.c2
-rw-r--r--src/proctitle.c160
-rw-r--r--src/sentinel.c180
-rw-r--r--src/transform.c10
15 files changed, 546 insertions, 109 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 153fad6..70bacd4 100644
--- a/NEWS
+++ b/NEWS
@@ -1,11 +1,35 @@
-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
@@ -77,13 +101,13 @@ 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.
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/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_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 1;
+ 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,8 +1,8 @@
.\" 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.
.\"
@@ -10,34 +10,36 @@
.\" 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]\
@@ -66,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,
@@ -92,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
@@ -138,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
@@ -155,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
@@ -213,12 +226,21 @@ 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
@@ -440,21 +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 -- 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.
diff --git a/src/genrc.c b/src/genrc.c
index 04845d0..c6ebcaf 100644
--- a/src/genrc.c
+++ b/src/genrc.c
@@ -1,8 +1,8 @@
/* 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>
@@ -13,12 +13,13 @@ 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;
@@ -57,12 +58,13 @@ 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 },
{ "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)
@@ -232,27 +234,34 @@ char const *help_msg[] = {
" 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",
@@ -277,12 +286,13 @@ char const *help_msg[] = {
" 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>",
@@ -307,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
@@ -330,20 +340,22 @@ 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]",
@@ -497,13 +509,13 @@ static const char gplv3[] =
"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;
@@ -566,21 +578,24 @@ find_command(char const *s)
for (c = comtab; c->com_name; c++)
if (strcmp(c->com_name, s) == 0)
break;
return c->com_fun;
}
+extern char **environ;
+
int
main(int argc, char **argv)
{
int c;
char *p;
int no_reload = 0;
GENRC_COMMAND command;
setprogname(argv[0]);
+ mf_proctitle_init (argc, argv, environ);
shortopts_setup();
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL))
!= EOF) {
switch (c) {
case 'h':
@@ -595,12 +610,15 @@ main(int argc, char **argv)
case 'c':
setenv("GENRC_COMMAND", optarg, 1);
break;
case 'e':
genrc_shell = NULL;
break;
+ case 'k':
+ setenv("GENRC_KILL_MODE", optarg, 1);
+ break;
case 'l':
if (optarg[0] == 'p')
set_nice(optarg+1);
else
set_limit(optarg);
break;
@@ -685,18 +703,34 @@ main(int argc, char **argv)
if ((p = getenv("GENRC_SIGNAL_STOP")) != NULL) {
genrc_signal_stop = str_to_sig(p);
if (genrc_signal_stop <= 0)
usage_error("%s: invalid signal number", p);
}
+ if ((p = getenv("GENRC_KILL_MODE")) != NULL) {
+ if (strcmp(p, "group") == 0)
+ genrc_kill_mode = genrc_kill_group;
+ else if (strcmp(p, "process") == 0)
+ genrc_kill_mode = genrc_kill_process;
+ else if (strcmp(p, "mixed") == 0)
+ genrc_kill_mode = genrc_kill_mixed;
+ else
+ usage_error("%s: invalid kill mode", p);
+ } else {
+ if ((p = getenv("GENRC_SENTINEL")) && *p == '1')
+ genrc_kill_mode = genrc_kill_group;
+ else
+ genrc_kill_mode = genrc_kill_process;
+ }
+
if ((p = getenv("GENRC_TIMEOUT")) != NULL) {
char *end;
unsigned long n;
errno = 0;
n = strtoul(p, &end, 10);
- if (errno || *p || n > UINT_MAX)
+ if (errno || *end || n > UINT_MAX)
usage_error("%s: invalid timeout", p);
if (n == 0)
genrc_no_reload = 1;
else
genrc_timeout = n;
}
diff --git a/src/genrc.h b/src/genrc.h
index 9326edd..dc71aac 100644
--- a/src/genrc.h
+++ b/src/genrc.h
@@ -1,8 +1,8 @@
/* 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 <config.h>
#include <stdlib.h>
@@ -156,6 +156,16 @@ int sentinel(void);
int com_start(void);
int com_status(void);
int com_stop(void);
int com_restart(void);
int com_reload(void);
+enum genrc_kill_mode {
+ genrc_kill_group,
+ genrc_kill_process,
+ genrc_kill_mixed
+};
+
+extern enum genrc_kill_mode genrc_kill_mode;
+
+void mf_proctitle_init (int argc, char *argv[], char *env[]);
+void mf_proctitle_format (const char *fmt, ...);
diff --git a/src/pid_ps.c b/src/pid_ps.c
index ff224da..a6223d0 100644
--- a/src/pid_ps.c
+++ b/src/pid_ps.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"
@@ -66,13 +66,13 @@ pid_ps_get(GENRC_PID_CLOSURE *clos, PIDLIST *plist)
ssize_t n;
char *buf = NULL;
size_t size = 0;
size_t line = 0;
PIDLIST pplist;
- fp = popen("ps -ef", "r");
+ fp = popen("ps -efw", "r");
if (!fp) {
system_error(errno, "failed to run ps");
return -1;
}
ws.ws_delim = " ";
diff --git a/src/pidlist.c b/src/pidlist.c
index 1dc5893..60f2efc 100644
--- a/src/pidlist.c
+++ b/src/pidlist.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"
diff --git a/src/proctitle.c b/src/proctitle.c
new file mode 100644
index 0000000..891b588
--- /dev/null
+++ b/src/proctitle.c
@@ -0,0 +1,160 @@
+/* This file is part of Mailfromd.
+ Copyright (C) 2007-2022 Sergey Poznyakoff
+
+ This program 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.
+
+ This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <string.h>
+#if MF_PROCTITLE_TYPE == MF_PROCTITLE_PSTAT
+# include <sys/pstat.h>
+#elif MF_PROCTITLE_TYPE == MF_PROCTITLE_PSSTRINGS
+# include <sys/proc.h>
+# include <vm/pmap.h>
+# include <machine/pmap.h>
+# include <machine/vmparam.h>
+# include <sys/exec.h>
+#endif
+
+extern char **environ;
+
+/* Move the environment to prepare more space for argv */
+static int
+move_env(char *env[])
+{
+ size_t size;
+ int i;
+ char **p;
+
+ size = 0;
+ for (i = 0; env[i]; i++)
+ size += strlen(env[i]) + 1;
+
+ p = calloc(i + 1, sizeof(*p));
+ if (!p)
+ return 1;
+
+ for (i = 0; env[i]; i++)
+ if ((p[i] = strdup(env[i])) == NULL) {
+ int j;
+ /* Free allocated memory and return */
+ for (j = 0; j < i; j++)
+ free(p[i]);
+ free(p);
+ return 1;
+ }
+ p[i] = NULL;
+ environ = p;
+ return 0;
+}
+
+static int orig_argc;
+static char **orig_argv;
+static char *orig_argv_end;
+static char proctitle_buffer[1024];
+#ifdef HAVE___PROGNAME
+extern char *__progname;
+extern char *__progname_full;
+#else
+static char *__progname;
+#endif
+
+void
+mf_proctitle_init(int argc, char *argv[], char *env[])
+{
+ int i;
+
+ move_env(env);
+
+ orig_argc = argc;
+ orig_argv = argv;
+ orig_argv_end = argv[0] + strlen(argv[0]);
+ __progname = strrchr(argv[0], '/');
+ if (__progname)
+ __progname++;
+ else
+ __progname = argv[0];
+ __progname = strdup(__progname);