Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions rts.tests/nonexistent.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
test.sv: unable to open supervise/ok: file does not exist
nonexistent: unable to chdir: file does not exist
0
test.sv: unable to open supervise/ok: file does not exist
nonexistent: unable to chdir: file does not exist
0
--- svc handles new and nonexistent directories
svc: warning: unable to control test.sv: file does not exist
svc: warning: unable to chdir to nonexistent: file does not exist
Expand Down
1 change: 1 addition & 0 deletions rts.tests/nonexistent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ echo '--- svstat handles new and nonexistent directories'
chmod 755 test.sv/run
touch test.sv/down
svstat test.sv nonexistent; echo $?
svstat -c test.sv nonexistent; echo $?

echo '--- svc handles new and nonexistent directories'
svc test.sv nonexistent; echo $?
Expand Down
17 changes: 17 additions & 0 deletions rts.tests/supervise-base.exp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
100
test.sv: down x seconds, starting
0
test.sv: ^[[31;1mdown ^[[0mx seconds, starting
0
0
test.sv: supervise not running
0
Expand All @@ -16,6 +18,14 @@ hi
0
.: no log service
0
.: ^[[32;1mup^[[0m (pid x) x seconds, ^[[33;1mnormally down^[[0m, running
0
.: ^[[32;1mup^[[0m (pid x) x seconds, ^[[33;1mnormally down^[[0m, running
0
.: ^[[32;1mup^[[0m (pid x) x seconds, ^[[33;1mnormally down^[[0m, running
0
.: no log service
0
0
100
0
Expand All @@ -27,6 +37,13 @@ hi
0
. log: up (pid x) x seconds, normally down, running
0
.: ^[[32;1mup^[[0m (pid x) x seconds, ^[[33;1mnormally down^[[0m, running
. log: ^[[32;1mup^[[0m (pid x) x seconds, ^[[33;1mnormally down^[[0m, running
0
.: ^[[32;1mup^[[0m (pid x) x seconds, ^[[33;1mnormally down^[[0m, running
0
. log: ^[[32;1mup^[[0m (pid x) x seconds, ^[[33;1mnormally down^[[0m, running
0
0
0
0
Expand Down
15 changes: 15 additions & 0 deletions rts.tests/supervise-base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ svup test.sv; echo $?
svup -l test.sv; echo $?
svup -L test.sv; echo $?
( svstat test.sv; echo $?; ) | filter_svstat
( svstat -c test.sv; echo $?; ) | filter_svstat
svc -x test.sv; echo $?
wait
svstat test.sv; echo $?
Expand All @@ -37,6 +38,14 @@ svstat -l .
echo $?
svstat -L .
echo $?
svstat -c .
echo $?
SVCOLOR=1 svstat .
echo $?
svstat -l -c .
echo $?
svstat -L -c .
echo $?
svup .
echo \$?
svup -L .
Expand All @@ -62,6 +71,12 @@ svstat -l .
echo $?
svstat -L .
echo $?
svstat -c .
echo $?
svstat -l -c .
echo $?
svstat -L -c .
echo $?
svup .
echo \$?
svup -L .
Expand Down
10 changes: 10 additions & 0 deletions svcolor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef SVCOLOR_H
#define SVCOLOR_H

enum sv_color {
sv_color_green,
sv_color_red,
sv_color_yellow,
};

#endif
9 changes: 9 additions & 0 deletions svstat.8.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ svstat \- prints the status of services monitored by
[
.B \-l
]
[
.B \-c
]
.I services
.SH DESCRIPTION
.I services
Expand All @@ -32,6 +35,9 @@ Only report the status of the log process of all listed services.
.TP
.B \-l
Do not report the status of the log process of all listed services.
.TP
.B \-c
Enable colors in svstat output.
.SH ENVIRONMENT
.TP
.B SUPERVISEDIR
Expand All @@ -42,6 +48,9 @@ for the status information and control files. If
is an absolute path, it is suffixed with the path to
.I s
with all slashes ("/") replaced with colons (":").
.TP
.B SVCOLOR
If set to 1, the -c flag is enabled.
.SH SEE ALSO
supervise(8),
svc(8),
Expand Down
53 changes: 43 additions & 10 deletions svstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
#include <sys/types.h>
#include <sys/stat.h>
#include "strerr.h"
#include "env.h"
#include "error.h"
#include "open.h"
#include "fmt.h"
#include "tai.h"
#include "buffer.h"
#include "sgetopt.h"
#include "str.h"
#include "svcolor.h"
#include "svpath.h"
#include "svstatus.h"

Expand All @@ -20,6 +23,7 @@ buffer b = BUFFER_INIT(buffer_unixwrite,1,bspace,sizeof bspace);
char strnum[FMT_ULONG];

int check_log = -1;
int opt_color = 0;

static void die_nomem(void)
{
Expand All @@ -28,7 +32,22 @@ static void die_nomem(void)

static void die_usage(void)
{
strerr_die1x(100,"svstat: usage: svstat [-L] [-l] dir [dir ...]");
strerr_die1x(100,"svstat: usage: svstat [-L] [-l] [-c] dir [dir ...]");
}

void color_buffer_puts(buffer *s, const char *buf, enum sv_color color)
{
if (opt_color) {
switch (color) {
case sv_color_red: buffer_puts(s, "\e[31;1m"); break;
case sv_color_green: buffer_puts(s, "\e[32;1m"); break;
case sv_color_yellow: buffer_puts(s, "\e[33;1m"); break;
}
buffer_puts(s, buf);
buffer_puts(s, "\e[0m");
}
else
buffer_puts(s, buf);
}

static void showstatus(const char status[19], int r, int normallyup)
Expand Down Expand Up @@ -56,22 +75,29 @@ static void showstatus(const char status[19], int r, int normallyup)
tai_sub(&when,&now,&when);

if (pid) {
buffer_puts(&b,"up (pid ");
color_buffer_puts(&b, "up", sv_color_green);
buffer_puts(&b, " (pid ");
buffer_put(&b,strnum,fmt_ulong(strnum,pid));
buffer_puts(&b,") ");
}
else
buffer_puts(&b,"down ");
color_buffer_puts(&b,"down ", sv_color_red);

buffer_put(&b,strnum,fmt_ulong(strnum,tai_approx(&when)));
buffer_puts(&b," seconds");

if (pid && !normallyup)
buffer_puts(&b,", normally down");
if (!pid && normallyup)
buffer_puts(&b,", normally up");
if (pid && paused)
buffer_puts(&b,", paused");
if (pid && !normallyup) {
buffer_puts(&b,", ");
color_buffer_puts(&b,"normally down", sv_color_yellow);
}
if (!pid && normallyup) {
buffer_puts(&b,", ");
color_buffer_puts(&b,"normally up", sv_color_yellow);
}
if (pid && paused) {
buffer_puts(&b,", ");
color_buffer_puts(&b,"paused", sv_color_yellow);
}
if (!pid && (want == 'u'))
buffer_puts(&b,", want up");
if (pid && (want == 'd'))
Expand Down Expand Up @@ -188,14 +214,17 @@ int main(int argc,const char *const *argv)
const char *dir;
int opt;

while ((opt = getopt(argc,argv,"lL")) != opteof) {
while ((opt = getopt(argc,argv,"lLc")) != opteof) {
switch (opt) {
case 'L':
check_log = 1;
break;
case 'l':
check_log = 0;
break;
case 'c':
opt_color = 1;
break;
default:
die_usage();
}
Expand All @@ -209,6 +238,10 @@ int main(int argc,const char *const *argv)
if (fdorigdir == -1)
strerr_die2sys(111,FATAL,"unable to open current directory");

const char *env_svcolor_enable = env_get("SVCOLOR");
if (env_svcolor_enable && str_diff(env_svcolor_enable, "1") == 0)
opt_color = 1;

while ((dir = *argv++) != 0) {
doit(dir);
buffer_puts(&b,"\n");
Expand Down