Skip to content

Commit

Permalink
Fix #246: conditions for running/halted services
Browse files Browse the repository at this point in the history
This change adds the following service conditions, which can be used to
synchronize other stanzas:

  - service/foo/running
  - service/foo/halted
  - service/foo/missing
  - service/foo/crashed
  - service/foo/stopped
  - service/foo/busy
  - service/foo/restart

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
  • Loading branch information
troglobit committed Apr 25, 2022
1 parent 30fb978 commit 82cc10b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/cond-w.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* THE SOFTWARE.
*/

#include <dirent.h>
#include <ftw.h>
#include <libgen.h>
#include <stdio.h>
Expand Down Expand Up @@ -132,6 +133,25 @@ static int cond_checkpath(const char *path)
return 0;
}

/* Intended for 'service/foo/`, nothing else */
static void cond_delpath(const char *path)
{
char fn[PATH_MAX];
struct dirent *d;
DIR *dir;

dir = opendir(path);
if (!dir)
return;

while ((d = readdir(dir))) {
paste(fn, sizeof(fn), path, d->d_name);
remove(fn);
}
closedir(dir);
unlink(path);
}

int cond_set_path(const char *path, enum cond_state next)
{
enum cond_state prev;
Expand All @@ -155,8 +175,12 @@ int cond_set_path(const char *path, enum cond_state next)
break;

case COND_OFF:
if (unlink(path) && errno != ENOENT)
_pe("Failed removing condition '%s'", path);
if (unlink(path)) {
if (errno == ENOENT)
_pe("Failed removing condition '%s'", path);
if (errno == EISDIR)
cond_delpath(path);
}
break;

default:
Expand Down
18 changes: 18 additions & 0 deletions src/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,24 @@ static void svc_set_state(svc_t *svc, svc_state_t new)
cond_clear(failure);
}
}

if (svc_is_daemon(svc)) {
char cond[MAX_COND_LEN];

snprintf(cond, sizeof(cond), "service/%s/", svc_ident(svc, NULL, 0));
cond_clear(cond);

switch (svc->state) {
case SVC_HALTED_STATE:
case SVC_RUNNING_STATE:
strlcat(cond, svc_status(svc), sizeof(cond));
cond_set_oneshot(cond);
break;

default:
break;
}
}
}

/*
Expand Down

0 comments on commit 82cc10b

Please sign in to comment.