Fix #246: conditions for running/halted services

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>
This commit is contained in:
Joachim Wiberg
2022-04-26 00:07:44 +02:00
parent 30fb9780d7
commit 82cc10be88
2 changed files with 44 additions and 2 deletions
+26 -2
View File
@@ -21,6 +21,7 @@
* THE SOFTWARE.
*/
#include <dirent.h>
#include <ftw.h>
#include <libgen.h>
#include <stdio.h>
@@ -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;
@@ -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:
+18
View File
@@ -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;
}
}
}
/*