Skip to content

Commit

Permalink
svc.c: added -w option to wait for service to be up
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhangui committed Oct 20, 2024
1 parent 23f7843 commit d92f08d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 57 deletions.
2 changes: 2 additions & 0 deletions daemontools-x/doc/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
04. svps: use .svscan.pid from service directory of each svscan instance
- 10/10/2024
05. runpod.sh: set systemd only if not set
- 20/10/2024
06. svc.c: added -w option to wait for service to be up

* Tue Aug 06 2024 12:48:42 +0000 Manvendra Bhangui <[email protected]> 1.1.5-1.1%{?dist}
Release 1.1.5-1.1 Start 05/02/2024 End 06/08/2024
Expand Down
8 changes: 4 additions & 4 deletions daemontools-x/supervise.8
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ 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 before it executes ./run. Hence, if service \fIw\fR is up, write on
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 till \fIw\fR is up and running. If servicee
is down, the write will block untill \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
Expand All @@ -128,8 +128,8 @@ returns, it means \fBsupervise\fR(8) is running.
.IP \[bu] 2
\fIs\fR/\fIsupervise\fR/\fIup\fR - clients can open this in write mode
(O_WRONLY) to test if service \fIs\fR is up. Any client that opens this
fifo will block until the service \fIs\fR is up. If write returns, it means
service \fIs\fR is running.
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.
.PP
\fBsupervise\fR logs informational, warning and error messages to
descriptor 2. Informational messages can be turned on by setting the
Expand Down
30 changes: 17 additions & 13 deletions daemontools-x/svc.8
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
.\" vim: tw=75
.TH svc 8
.SH NAME
svc \- controls services monitored by
.BR supervise(8).
.SH SYNOPSIS
.B svc
[
.B \-udopchaitkx
]
.I services
\fBsvc\fR [ \fB\-udopchaitkx\fR ] \fIservices\fR

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

.B svc
applies all the options to each
.I service
in turn.
\fBsvc\fR applies all the options to each \fIservice\fR in turn.

.SH OPTIONS
.TP
.TP 4
.B \-u
Up. If the
.I service
Expand Down Expand Up @@ -112,6 +106,16 @@ a KILL signal.
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
Wait. \fBsvc\fR(8) will wait for \fIservice\fR to be up.
\fBsupervise\fR(8) opens the named pipe \fIservice\fR/\fIup\fR after
executing \fIservice\fR/\fIrun\fR. \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. This can also be used by \fBsupervise\fR
to setup inter-dependencies between services.

.TP
.B \-x
Exit.
Expand Down
108 changes: 68 additions & 40 deletions daemontools-x/svc.c
Original file line number Diff line number Diff line change
@@ -1,40 +1,8 @@
/*
* $Log: svc.c,v $
* Revision 1.11 2024-05-09 22:39:36+05:30 mbhangui
* fix discarded-qualifier compiler warnings
*
* Revision 1.10 2023-03-04 14:42:52+05:30 Cprogrammer
* check for supervise/ok in original service dir before run filesystem
*
* Revision 1.9 2020-11-30 23:19:48+05:30 Cprogrammer
* change warning text message for missing directory in /run
*
* Revision 1.8 2020-11-30 22:53:40+05:30 Cprogrammer
* continue instead of exit on run_init() failure
*
* Revision 1.7 2020-10-08 18:34:02+05:30 Cprogrammer
* use /run, /var/run if system supports it
*
* Revision 1.6 2020-08-29 08:41:22+05:30 Cprogrammer
* new option 'G' to send signal to entire process group
*
* Revision 1.5 2020-06-10 17:37:42+05:30 Cprogrammer
* new option restart (stop and restart) a service
*
* Revision 1.4 2008-06-07 19:11:45+05:30 Cprogrammer
* added sigquit, sigusr1, sigusr2 signals
*
* Revision 1.3 2004-10-22 20:31:16+05:30 Cprogrammer
* added RCS id
*
* Revision 1.2 2004-10-12 00:09:01+05:30 Cprogrammer
* replaced buffer functions with substdio
*
* Revision 1.1 2003-12-31 18:37:19+05:30 Cprogrammer
* Initial revision
*
* $Id: $
*/
#include <unistd.h>
#include <fcntl.h>
#include "ndelay.h"
#include "strerr.h"
#include "error.h"
Expand All @@ -61,22 +29,24 @@ int fdorigdir;
int
main(int argc, char **argv)
{
int opt, i;
int opt, sopt = 0, i;
int fd, exit_stat;
char *dir;

sig_ignore(sig_pipe);
while ((opt = getopt(argc, argv, "udropchUaitkxq12G")) != opteof) {
while ((opt = getopt(argc, argv, "udropchUaitkxq12Gw")) != opteof) {
if (opt == '?')
strerr_die1x(100,
"svc options: u up, d down, r restart, o once, x exit, p pause, c continue h hup\n"
" a alarm, i interrupt, t term, k kill, q quit, U|1 SIGUSR1, 2 SIGUSR2\n"
" G process group");
else
if (datalen < sizeof data) {
if (byte_chr(data, datalen, opt) == datalen)
if (opt != 'w' && byte_chr(data, datalen, opt) == datalen)
data[datalen++] = opt;
}
if (opt == 'w')
sopt = opt;
}
argv += optind;
if ((fdorigdir = open_read(".")) == -1)
Expand Down Expand Up @@ -125,19 +95,40 @@ main(int argc, char **argv)
}
}
#endif
if (fd == -1 && (fd = open_write("supervise/control")) == -1) {
if (datalen && fd == -1 && (fd = open_write("supervise/control")) == -1) {
if (errno == error_nodevice || errno == error_noent) {
strerr_warn4(WARN, "unable to control ", dir, ": supervise not running: ", 0);
exit_stat = 2;
} else
} else {
exit_stat = 1;
strerr_warn4(WARN, "unable to control ", dir, ": ", &strerr_sys);
} else {
}
} else
if (datalen) {
ndelay_off(fd);
substdio_fdbuf(&b, write, fd, bspace, sizeof bspace);
if (substdio_putflush(&b, data, datalen) == -1)
strerr_warn4(WARN, "error writing commands to ", dir, ": ", &strerr_sys);
close(fd);
}
if (sopt == 'w') {
if (datalen)
sleep(1);
while (1) {
if ((fd = open("supervise/up", O_WRONLY, 0)) == -1) { /* this should block if service is down */
if (errno == error_nodevice || errno == error_noent) {
sleep(1);
continue;
} else {
strerr_warn4(WARN, "unable to wait for ", dir, ": ", &strerr_sys);
break;
}
} else { /*- service is up */
close(fd);
break;
}
}
}
if (fchdir(fdorigdir) == -1)
strerr_die2sys(111, FATAL, "unable to revert directory: ");
} /* while ((dir = *argv++)) */
Expand All @@ -151,3 +142,40 @@ getversion_svc_c()

x++;
}

/*
* $Log: svc.c,v $
* Revision 1.11 2024-05-09 22:39:36+05:30 mbhangui
* fix discarded-qualifier compiler warnings
*
* Revision 1.10 2023-03-04 14:42:52+05:30 Cprogrammer
* check for supervise/ok in original service dir before run filesystem
*
* Revision 1.9 2020-11-30 23:19:48+05:30 Cprogrammer
* change warning text message for missing directory in /run
*
* Revision 1.8 2020-11-30 22:53:40+05:30 Cprogrammer
* continue instead of exit on run_init() failure
*
* Revision 1.7 2020-10-08 18:34:02+05:30 Cprogrammer
* use /run, /var/run if system supports it
*
* Revision 1.6 2020-08-29 08:41:22+05:30 Cprogrammer
* new option 'G' to send signal to entire process group
*
* Revision 1.5 2020-06-10 17:37:42+05:30 Cprogrammer
* new option restart (stop and restart) a service
*
* Revision 1.4 2008-06-07 19:11:45+05:30 Cprogrammer
* added sigquit, sigusr1, sigusr2 signals
*
* Revision 1.3 2004-10-22 20:31:16+05:30 Cprogrammer
* added RCS id
*
* Revision 1.2 2004-10-12 00:09:01+05:30 Cprogrammer
* replaced buffer functions with substdio
*
* Revision 1.1 2003-12-31 18:37:19+05:30 Cprogrammer
* Initial revision
*
*/

0 comments on commit d92f08d

Please sign in to comment.