Add drop command

This commit is contained in:
C. McEnroe 2020-08-16 23:01:25 -04:00
parent f9cfab1e0d
commit 6302579f22
5 changed files with 24 additions and 6 deletions

View File

@ -1,4 +1,4 @@
.Dd August 15, 2020
.Dd August 16, 2020
.Dt CATSIT 8
.Os
.
@ -9,7 +9,7 @@
.Sh SYNOPSIS
.Nm
.Op Fl c Ar control
.Cm start|stop|restart|status Ns | Ns Ar signal
.Cm start|stop|restart|status|drop Ns | Ns Ar signal
.Ar service ...
.
.Sh DESCRIPTION
@ -55,6 +55,10 @@ then stopped services will be started.
.It Cm status
Log the current status of any matching services.
.
.It Cm drop
Drop any matching stopped services
from the services list.
.
.It Ar signal
Send the named signal
to the processes of any matching started services.

View File

@ -27,7 +27,7 @@ fi
[ $# -lt 2 ] && die 'service name required'
action=$(echo "${1}" | tr 'A-Z' 'a-z')
for valid in start stop restart status $(kill -l | tr 'A-Z' 'a-z'); do
for valid in start stop restart status drop $(kill -l | tr 'A-Z' 'a-z'); do
[ "${action}" = "${valid}" ] && break
done
if [ "${action}" != "${valid}" ]; then

View File

@ -126,6 +126,7 @@ static void parseControl(char *command) {
return;
}
bool drop = false;
Action *fn = NULL;
int signal = 0;
if (!strcmp(action, "start")) {
@ -136,6 +137,8 @@ static void parseControl(char *command) {
fn = serviceRestart;
} else if (!strcmp(action, "status")) {
fn = serviceStatus;
} else if (!strcmp(action, "drop")) {
drop = true;
} else {
for (int i = 1; i < NSIG; ++i) {
if (strcasecmp(action, sys_signame[i])) continue;
@ -143,7 +146,7 @@ static void parseControl(char *command) {
break;
}
}
if (!fn && !signal) {
if (!drop && !fn && !signal) {
syslog(LOG_NOTICE, "unknown action or signal %s", action);
return;
}
@ -151,10 +154,12 @@ static void parseControl(char *command) {
while (command) {
bool found = false;
char *pattern = strsep(&command, WS);
for (size_t i = 0; i < services.len; ++i) {
for (size_t i = services.len - 1; i < services.len; --i) {
struct Service *service = &services.ptr[i];
if (fnmatch(pattern, service->name, 0)) continue;
if (signal) {
if (drop) {
serviceDrop(i);
} else if (signal) {
serviceSignal(service, signal);
} else {
fn(service);

View File

@ -130,6 +130,7 @@ extern struct Services {
} services;
int serviceAdd(const char *name, const char *command);
void serviceDrop(size_t index);
void serviceStatus(struct Service *service);
void serviceStart(struct Service *service);
void serviceStop(struct Service *service);

View File

@ -119,6 +119,14 @@ err:
return -1;
}
void serviceDrop(size_t index) {
struct Service *service = &services.ptr[index];
if (service->state != Stop) return;
syslog(LOG_NOTICE, "%s[] dropped", service->name);
serviceFree(service);
services.ptr[index] = services.ptr[--services.len];
}
void serviceStatus(struct Service *service) {
if (service->state == Stop && service->intent == Stop) {
syslog(LOG_NOTICE, "%s[] is stopped", service->name);