aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDavid Oberhollenzer <goliath@infraroot.at>2020-05-14 01:41:30 +0200
committerDavid Oberhollenzer <goliath@infraroot.at>2020-05-14 01:41:30 +0200
commit8ce7d986283f5ef63377a0a447850c970c5cc493 (patch)
tree2a8bcf66f3d42104dfdaf1e71e9a11710245a936 /lib
parent32c3ad35b4985718e2b92c979292f6b0f6816587 (diff)
Move service rt data to libinit, try to improve memory packing
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
Diffstat (limited to 'lib')
-rw-r--r--lib/include/service.h33
-rw-r--r--lib/init/rdsvc.c28
-rw-r--r--lib/init/svc_tsort.c2
3 files changed, 47 insertions, 16 deletions
diff --git a/lib/include/service.h b/lib/include/service.h
index 05ab8de..f388f87 100644
--- a/lib/include/service.h
+++ b/lib/include/service.h
@@ -3,6 +3,7 @@
#define SERVICE_H
#include <sys/types.h>
+#include <stdint.h>
typedef struct exec_t {
struct exec_t *next;
@@ -11,6 +12,14 @@ typedef struct exec_t {
} exec_t;
enum {
+ STATE_OFF,
+ STATE_RUNNING,
+ STATE_QUEUED,
+ STATE_COMPLETED,
+ STATE_FAILED,
+};
+
+enum {
/*
Start the service in the background and continue with
other services. The service will eventually terminate.
@@ -44,17 +53,13 @@ enum {
SVC_FLAG_HAS_EXEC = 0x10,
};
+/* persistent service configuration read from disk */
typedef struct service_t {
struct service_t *next;
char *fname; /* source file name */
-
- int type; /* SVC_* service type */
- int target; /* TGT_* service target */
char *desc; /* description string */
char *ctty; /* controlling tty or log file */
- int rspwn_limit; /* maximum respawn count */
- unsigned int flags; /* SVC_FLAG_* bit field */
/* linked list of command lines to execute */
exec_t *exec;
@@ -62,8 +67,13 @@ typedef struct service_t {
char *before; /* services that must be executed later */
char *after; /* services that must be executed first */
- int num_before;
- int num_after;
+ uint32_t num_before;
+ uint32_t num_after;
+
+ uint16_t rspwn_limit; /* maximum respawn count */
+ uint16_t flags; /* SVC_FLAG_* bit field */
+ uint8_t type; /* SVC_* service type */
+ uint8_t target; /* TGT_* service target */
char name[]; /* canonical service name */
} service_t;
@@ -72,6 +82,15 @@ typedef struct {
service_t *targets[TGT_MAX];
} service_list_t;
+/* run time configuration in-memory for managing services */
+typedef struct {
+ service_t *svc; /* the underlying service description */
+ pid_t pid; /* if still running, the pid */
+ uint16_t rspwn_count; /* services respawn counter */
+ uint8_t state; /* what STATE_* the service is currently in */
+ uint8_t status; /* if exited, process exit status */
+} svc_run_data_t;
+
/*
Read a service from a file.
*/
diff --git a/lib/init/rdsvc.c b/lib/init/rdsvc.c
index c461d98..9992668 100644
--- a/lib/init/rdsvc.c
+++ b/lib/init/rdsvc.c
@@ -104,6 +104,7 @@ static int svc_exec(void *user, char *arg, rdline_t *rd)
static int svc_before(void *user, char *arg, rdline_t *rd)
{
service_t *svc = user;
+ int ret;
if (svc->before != NULL) {
fprintf(stderr, "%s: %zu: 'before' dependencies respecified\n",
@@ -115,13 +116,18 @@ static int svc_before(void *user, char *arg, rdline_t *rd)
if (svc->before == NULL)
return -1;
- svc->num_before = try_pack_argv(svc->before, rd);
- return (svc->num_before < 0) ? -1 : 0;
+ ret = try_pack_argv(svc->before, rd);
+ if (ret < 0)
+ return -1;
+
+ svc->num_before = ret;
+ return 0;
}
static int svc_after(void *user, char *arg, rdline_t *rd)
{
service_t *svc = user;
+ int ret;
if (svc->after != NULL) {
fprintf(stderr, "%s: %zu: 'after' dependencies respecified\n",
@@ -133,26 +139,32 @@ static int svc_after(void *user, char *arg, rdline_t *rd)
if (svc->after == NULL)
return -1;
- svc->num_after = try_pack_argv(svc->after, rd);
- return (svc->num_after < 0) ? -1 : 0;
+ ret = try_pack_argv(svc->after, rd);
+ if (ret < 0)
+ return -1;
+
+ svc->num_after = ret;
+ return 0;
}
static int svc_type(void *user, char *arg, rdline_t *rd)
{
service_t *svc = user;
- int count = try_pack_argv(arg, rd);
+ int ret, count;
+ count = try_pack_argv(arg, rd);
if (count < 1)
return -1;
- svc->type = svc_type_from_string(arg);
-
- if (svc->type == -1) {
+ ret = svc_type_from_string(arg);
+ if (ret < 0) {
fprintf(stderr, "%s: %zu: unknown service type '%s'\n",
rd->filename, rd->lineno, arg);
return -1;
}
+ svc->type = ret;
+
if (count > 1) {
arg += strlen(arg) + 1;
diff --git a/lib/init/svc_tsort.c b/lib/init/svc_tsort.c
index 37f3eb8..99feac8 100644
--- a/lib/init/svc_tsort.c
+++ b/lib/init/svc_tsort.c
@@ -9,7 +9,7 @@
static bool has_dependencies(service_t *list, service_t *svc)
{
const char *ptr;
- int i;
+ uint32_t i;
while (list != NULL) {
for (ptr = svc->after, i = 0; i < svc->num_after; ++i) {