Skip to content

Commit

Permalink
updated svc, svscan, supervise
Browse files Browse the repository at this point in the history
01. svc.c: added -W option to wait for service to be down
02. svc.c: added -T timeout option for -w, -W options
03. supervise.c: open supervise/dn fifo in read mode after service goes
    down
04. svscan.c, supervise.c: renamed SERVICEDIR to SCANDIR
  • Loading branch information
mbhangui committed Oct 22, 2024
1 parent f5cc7ae commit e96909c
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 226 deletions.
5 changes: 5 additions & 0 deletions daemontools-x/doc/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
06. svc.c: added -w option to wait for service to be up
- 21/10/2024
07. supervise.c: BUG: status file truncated before announce
- 22/10/2024
08. svc.c: added -W option to wait for service to be down
09. svc.c: added -T timeout option for -w, -W options
10. supervise.c: open supervise/dn fifo in read mode after service goes down
11. svscan.c, supervise.c: renamed SERVICEDIR to SCANDIR

* Tue Aug 06 2024 12:48:42 +0000 Manvendra Bhangui <daemontools@indimail.org> 1.1.5-1.1%{?dist}
Release 1.1.5-1.1 Start 05/02/2024 End 06/08/2024
Expand Down
22 changes: 17 additions & 5 deletions daemontools-x/supervise.8
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,23 @@ secs. Any value above this value will be limited to 60 secs. The wait for
another service is implemented by opening \fIs\fR/\fIsupervise\fR/\fIup\fR
in write mode.

\fIsupervise\fR(8) opens \fIs\fR/\fIsupervise\fR/\fIup\fR in write mode
just after it executes ./run. Hence, if service \fIw\fR is up, write on
\fIw\fR/\fIsupervise\fR/\fIup\fR, returns immediately. If service \fIw\fR
is down, the write will block untill \fIw\fR is up and running. If service
\fIsupervise\fR(8) opens \fIs\fR/\fIsupervise\fR/\fIup\fR in read mode just
after it executes ./run. Hence, if service \fIw\fR is up, write on
\fIw\fR/\fIsupervise\fR/\fIup\fR returns immediately. If service \fIw\fR is
down, the write will block until \fIw\fR is up and running. If service
\fIw\fR doesn't have \fBsupervise\fR running, \fPsupervise\fR will wait for
60 seconds before opening the file \fIw\fR/\fIsupervise\fR/\fIup\fR again
in write mode. The default value of 60 seconds gets overriden by the
in read mode. The default value of 60 seconds gets overriden by the
\fBSCANINTERVAL\fR environment variable used by \fBsvscan\fR(8). If service
\fIw\fR doesn't exist, \fIs\fR/\fIwait\fR will be ignored.

\fIsupervise\fR(8) opens \fIs\fR/\fIsupervise\fR/\fIdn\fR in read mode when
asked to bring down a service using \fBsvc\fR (-d or -r option). It
opens this named pipe after issuing the TERM, CONT signal to the service.
Hence, if service\fIw\fR is down, write on \fIw\fR/\fIsupervise\fR/\fIdn\fR
returns immediately. if service \fIw\fR is up, the write will block until
\fIw\fR is down.

\fBsupervise\fR(8) creates the following FIFOs for with O_RDONLY|O_NDELAY
mode. See open(2) for description of O_RDONLY, O_NDELAY

Expand All @@ -132,6 +139,11 @@ fifo in O_WRONLY mode, will block until the service \fIs\fR is up. If write
returns, it means service \fIs\fR has executed \fIs\fR/\fIrun\fR.
\fBsvc\fR(8) is one such client (-w option) that can be used to check if a
service is up.
.IP \[bu] 2
\fIs\fR/\fIsupervise\fR/\fIdn\fR - clients can open this in write mode to
test if service \fIs\fR is down. This works like exactly like
\fIs\fR/\fIsupervise\fR/\fIup\fR. \fBsvc\fR(8) (-W option) can be used to
check if a service is down.

.PP
\fBsupervise\fR logs informational, warning and error messages to
Expand Down
54 changes: 39 additions & 15 deletions daemontools-x/supervise.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*- $Id: supervise.c,v 1.45 2024-10-21 17:47:39+05:30 Cprogrammer Exp mbhangui $ */
/*- $Id: supervise.c,v 1.46 2024-10-22 23:02:31+05:30 Cprogrammer Exp mbhangui $ */
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
Expand Down Expand Up @@ -31,17 +31,18 @@

static char *dir;
static int selfpipe[2];
static int fdlock;
static int fdcontrolwrite;
static int fdcontrol;
static int fdstatus;
static int fdok;
static int fdup;
static int fdlock = -1;
static int fdcontrolwrite = -1;
static int fdcontrol = -1;
static int fdstatus = -1;
static int fdok = -1;
static int fdup = -1;
static int fddn = -1;
static int flagexit = 0;
static int flagwantxx = 1;
static int flagwantup = 1;
static int flagpaused; /*- defined if (pid) */
static int fddir;
static int fddir = -1;
static int logger = 0;
static int verbose = 0, silent = 0;
static char flagfailed;
Expand Down Expand Up @@ -293,6 +294,12 @@ do_wait()
* opening supervise/up with O_WRONLY should return
* if the service service_name is down
* opening supervise/up with O_WRONLY should block
*
* supervise/dn is a FIFO
* if the service service_name is running
* opening supervise/dn with O_WRONLY should block
* if the service service_name is down
* opening supervise/dn with O_WRONLY should return
*/
for (i = 0;;) {
if ((fd_depend = open(wait_sv_status.s, O_WRONLY)) == -1) {
Expand Down Expand Up @@ -422,10 +429,13 @@ trystart(const char *how)
}
pidchange((childpid = f), 1);
announce(0);
fifo_make("supervise/up", 0600);
if ((fdup = open_read("supervise/up")) == -1) /*- open O_RDONLY|O_NDELAY */
strerr_die2sys(111, fatal.s, "read: supervise/up: ");
coe(fdup);
if (fddn != -1) {
close(fddn);
fddn = -1;
}
deepsleep(1);
}

Expand Down Expand Up @@ -703,16 +713,19 @@ doit()
#endif
if (!access(*shutdown, F_OK))
tryaction(shutdown, childpid, 0, 0); /*- run the shutdown command */
#ifdef USE_RUNFS
if (use_runfs && chdir(dir) == -1) /*- switch back to /run/svscan */
strerr_die4sys(111, fatal.s, "unable to switch back to ", dir, ": ");
#endif
siglist[0] = SIGTERM;
siglist[1] = SIGCONT;
signame[0] = "SIGTERM";
signame[1] = "SIGCONT";
do_kill(g, siglist, signame);
g = flagpaused = 0;
#ifdef USE_RUNFS
if (use_runfs && chdir(dir) == -1) /*- switch back to /run/svscan */
strerr_die4sys(111, fatal.s, "unable to switch back to ", dir, ": ");
#endif
if ((fddn = open_read("supervise/dn")) == -1)
strerr_die4sys(111, fatal.s, "unable to read ", dir, "/supervise/dn: ");
coe(fddn);
}
announce(0);
break;
Expand Down Expand Up @@ -1048,7 +1061,7 @@ main(int argc, char **argv)
sig_block(sig_child);
sig_catch(sig_child, trigger);

if (!(ptr = env_get("SERVICEDIR"))) {
if (!(ptr = env_get("SCANDIR"))) {
if (!getcwd(pwdbuf, 255))
strerr_die2sys(111, fatal.s, "unable to get current working directory: ");
ptr = pwdbuf;
Expand Down Expand Up @@ -1110,6 +1123,13 @@ main(int argc, char **argv)
if ((fdok = open_read("supervise/ok")) == -1)
strerr_die4sys(111, fatal.s, "unable to read ", dir, "/supervise/ok: ");
coe(fdok);
fifo_make("supervise/up", 0600);
fifo_make("supervise/dn", 0600);
if (!flagwantup) {
if ((fddn = open_read("supervise/dn")) == -1)
strerr_die4sys(111, fatal.s, "unable to read ", dir, "/supervise/dn: ");
coe(fddn);
}

/*-
* By now we have finished initialization of /run. We
Expand Down Expand Up @@ -1138,13 +1158,17 @@ main(int argc, char **argv)
void
getversion_supervise_c()
{
const char *x = "$Id: supervise.c,v 1.45 2024-10-21 17:47:39+05:30 Cprogrammer Exp mbhangui $";
const char *x = "$Id: supervise.c,v 1.46 2024-10-22 23:02:31+05:30 Cprogrammer Exp mbhangui $";

x++;
}

/*
* $Log: supervise.c,v $
* Revision 1.46 2024-10-22 23:02:31+05:30 Cprogrammer
* renamed SERVICEDIR to SCANDIR for svscan
* open supervise/dn after service is brought down
*
* Revision 1.45 2024-10-21 17:47:39+05:30 Cprogrammer
* BUG: status file truncated before announce
*
Expand Down
109 changes: 49 additions & 60 deletions daemontools-x/svc.8
Original file line number Diff line number Diff line change
@@ -1,110 +1,83 @@
.\" vim: tw=75
.TH svc 8
.SH NAME
svc \- controls services monitored by
.BR supervise(8).
svc \- controls services monitored by \fBsupervise\fR(8).

.SH SYNOPSIS
\fBsvc\fR [ \fB\-udopchaitkx\fR ] \fIservices\fR

.SH DESCRIPTION
\fIservices\fR consists of any number of arguments, each argument naming a
directory used by \fBsupervise\fR(8).
directory used by \fBsupervise\fR.

\fBsvc\fR applies all the options to each \fIservice\fR in turn.

.SH OPTIONS
.TP 4
.TP 3
.B \-u
Up. If the
.I service
is not running, start it. If the
.I service
stops, restart it.
Up. If the \fIservice\fR is not running, start it. If the \fIservice\fR
stops, restart it.

.TP
.B \-d
Down. If the
.I service
is running, send it a TERM signal and then a CONT signal. After it stops, do
not restart it. If the script shutdown exists, run shutdown to terminate the service
Down. If the \fIservice\fR is running, send it a TERM signal and then a
CONT signal. After it stops, do not restart it. If the script shutdown
exists, run shutdown to terminate the service

.TP
.B \-r
Restart. If the
.I service
is running, send it a term signal, then a CONT signal. After it stops,
start it.
Restart. If the \fIservice\fR is running, send it a term signal, then a
CONT signal. After it stops, start it.

.TP
.B \-o
Once. If the
.I service
is not running, start it. Do not restart it if it stops.
Once. If the \fIservice\fR is not running, start it. Do not restart it if
it stops.

.TP
.B \-p
Pause. Send the
.I service
a STOP signal.
Pause. Send the \fIservice\fR a STOP signal.

.TP
.B \-c
Continue. Send the
.I service
a CONT signal.
Continue. Send the \fIservice\fR a CONT signal.

.TP
.B \-h
Hangup. Send the
.I service
a HUP signal.
Hangup. Send the \fIservice\fR a HUP signal.

.TP
.B \-a
Alarm. Send the
.I service
an ALRM signal.
Alarm. Send the \fIservice\fR an ALRM signal.

.TP
.B \-i
Interrupt. Send the
.I service
an INT signal.
Interrupt. Send the \fIservice\fR an INT signal.

.TP
.B \-t
Terminate. Send the
.I service
a TERM signal.
Terminate. Send the \fIservice\fR a TERM signal.

.TP
.B \-q
Quit. Send the
.I service
a QUIT signal.
Quit. Send the \fIservice\fR a QUIT signal.

.TP
.B \-1 | \-U
SIGUSR1. Send the
.I service
a SIGUSR1 signal.
SIGUSR1. Send the \fIservice\fR a SIGUSR1 signal.

.TP
.B \-2
SIGUSR2. Send the
.I service
a SIGUSR2 signal.
SIGUSR2. Send the \fIservice\fR a SIGUSR2 signal.

.TP
.B \-k
Kill. Send the
.I service
a KILL signal.
Kill. Send the \fIservice\fR a KILL signal.

.TP
.B \-G
For all options above, the signal will be sent to the entire process group instead of just the pid of
the \fIservice\fR.
For all options above, the signal will be sent to the entire process group
instead of just the pid of the \fIservice\fR.

.TP
.B \-w
Expand All @@ -116,16 +89,32 @@ pipe is opened by \fBsupervise\fR. This option can be used by any program
to wait for a supervised service. This can also be used by \fBsupervise\fR
to setup inter-dependencies between services.

.TP
.B \-W
Wait. \fBsvc\fR(8) will wait for \fIservice\fR to be down.
\fBsupervise\fR(8) opens the named pipe \fIservice\fR/\fIdn\fR after
sending TERM folowed by CONT signal to \fIservice\fR, when svc -d option is
used. \fBsvc\fR opens this named pipe in O_WRONLY mode in blocking mode,
thus making \fBsvc\fR wait till the named pipe is opened by
\fBsupervise\fR. This option can be used by any program to wait for a
supervised service.

.TP
.B \-T \fItimeout
Use \fItimeout\fR as timeout when waiting for service to be up or down (-w
or -W options)

.TP
.B \-x
Exit.
.BR supervise (8)
will exit as soon as the
.I service
is down. If you use this option on a stable system, you're doing something
wrong;
.BR supervise (8)
is designed to run forever.
Exit. \fBsupervise\fR(8) will exit as soon as the \fIservice\fR is down. If
you use this option on a stable system, you're doing something wrong;
\fBsupervise\fR is designed to run forever.

.SH RETURN VALUE
\fBsvc\fR returns 0 if all operations succeeded, 1 if it was not able to
open \fIservice\fR/\fIsupervise\fR/\fIcontrol\fR, 2 if \fBsupervise\fR for
service \fIservice\fR is not running and 3 for timeout. The return value
cannot be used reliably when \fBsvc\fR is passed multiple arguments.

.SH SEE ALSO
supervise(8),
Expand Down
Loading

0 comments on commit e96909c

Please sign in to comment.