aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-04-11 13:29:13 +0200
committerDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-04-11 15:05:16 +0200
commit67d000cdc37009a975d0794f6fd347dfb17743eb (patch)
treedfc3f46ae2643ebaf6224295fe9df48deee470aa /lib
parent77725291efd609339cc560bec646782bc09ffb90 (diff)
Cleanup: redeuce number of allocations in rdsvc
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
Diffstat (limited to 'lib')
-rw-r--r--lib/include/service.h10
-rw-r--r--lib/src/delsvc.c2
-rw-r--r--lib/src/rdsvc.c76
3 files changed, 38 insertions, 50 deletions
diff --git a/lib/include/service.h b/lib/include/service.h
index eaa35bb..fee3ea3 100644
--- a/lib/include/service.h
+++ b/lib/include/service.h
@@ -45,16 +45,16 @@ enum {
};
typedef struct exec_t {
- char **argv; /* NULL terminated argument vector */
- char *raw_argv; /* backing store for argv contents */
-
struct exec_t *next;
+ char **argv; /* NULL terminated argument vector */
+ char buffer[]; /* backing store for argv */
} exec_t;
typedef struct service_t {
+ struct service_t *next;
+
int type; /* SVC_* service type */
int target; /* TGT_* service target */
- char *name; /* canonical service name */
char *desc; /* description string */
char *ctty; /* controlling tty or log file */
int rspwn_limit; /* maximum respawn count */
@@ -74,7 +74,7 @@ typedef struct service_t {
pid_t pid;
int status; /* process exit status */
- struct service_t *next;
+ char name[]; /* canonical service name */
} service_t;
typedef struct {
diff --git a/lib/src/delsvc.c b/lib/src/delsvc.c
index 968aa0d..d13c97f 100644
--- a/lib/src/delsvc.c
+++ b/lib/src/delsvc.c
@@ -31,7 +31,6 @@ void delsvc(service_t *svc)
svc->exec = e->next;
free(e->argv);
- free(e->raw_argv);
free(e);
}
@@ -40,7 +39,6 @@ void delsvc(service_t *svc)
free(svc->before);
free(svc->after);
- free(svc->name);
free(svc->desc);
free(svc->exec);
free(svc->ctty);
diff --git a/lib/src/rdsvc.c b/lib/src/rdsvc.c
index e911724..4b87312 100644
--- a/lib/src/rdsvc.c
+++ b/lib/src/rdsvc.c
@@ -89,22 +89,17 @@ static int svc_exec(service_t *svc, char *arg, rdline_t *rd)
{
exec_t *e, *end;
- e = calloc(1, sizeof(*e));
+ e = calloc(1, sizeof(*e) + strlen(arg) + 1);
if (e == NULL) {
fprintf(stderr, "%s: %zu: out of memory\n",
rd->filename, rd->lineno);
return -1;
}
- e->raw_argv = try_strdup(arg, rd);
- if (e->raw_argv == NULL) {
- free(e);
- return -1;
- }
+ strcpy(e->buffer, arg);
- e->argv = try_split_argv(e->raw_argv, rd);
+ e->argv = try_split_argv(e->buffer, rd);
if (e->argv == NULL) {
- free(e->raw_argv);
free(e);
return -1;
}
@@ -159,59 +154,59 @@ static int svc_after(service_t *svc, char *arg, rdline_t *rd)
static int svc_type(service_t *svc, char *arg, rdline_t *rd)
{
- char **args;
- int i, type;
+ char *ptr;
- args = try_split_argv(arg, rd);
-
- if (args == NULL)
- return -1;
+ for (ptr = arg; *ptr != ' ' && *ptr != '\0'; ++ptr)
+ ;
+ if (*ptr == ' ')
+ *(ptr++) = '\0';
- type = svc_type_from_string(args[0]);
+ svc->type = svc_type_from_string(arg);
- if (type == -1) {
+ if (svc->type == -1) {
fprintf(stderr, "%s: %zu: unknown service type '%s'\n",
- rd->filename, rd->lineno, args[0]);
- free(args);
+ rd->filename, rd->lineno, arg);
return -1;
}
- if (args[1] != NULL) {
- switch (type) {
+ if (*ptr != '\0') {
+ switch (svc->type) {
case SVC_RESPAWN:
- if (strcmp(args[1], "limit") != 0)
+ for (arg = ptr; *ptr != ' ' && *ptr != '\0'; ++ptr)
+ ;
+ if (*ptr == ' ')
+ *(ptr++) = '\0';
+
+ if (strcmp(arg, "limit") != 0)
goto fail_limit;
svc->rspwn_limit = 0;
- if (!isdigit(args[2][0]))
+ if (!isdigit(*ptr))
goto fail_limit;
- for (i = 0; isdigit(args[2][i]); ++i) {
+ while (isdigit(*ptr)) {
svc->rspwn_limit *= 10;
- svc->rspwn_limit += args[2][i] - '0';
+ svc->rspwn_limit += *(ptr++) - '0';
}
- if (args[2][i] != '\0')
- goto fail_limit;
- if (args[3] == NULL)
+ if (*ptr == '\0')
break;
+ if (*ptr != ' ')
+ goto fail_limit;
/* fall-through */
default:
- fprintf(stderr, "%s: %zu: unexpected extra arguments "
- "for type '%s'\n",
- rd->filename, rd->lineno, arg);
+ fprintf(stderr,
+ "%s: %zu: unexpected extra arguments\n",
+ rd->filename, rd->lineno);
return -1;
}
}
- svc->type = type;
- free(args);
return 0;
fail_limit:
fprintf(stderr, "%s: %zu: expected 'limit <value>' after 'respawn'\n",
rd->filename, rd->lineno);
- free(args);
return -1;
}
@@ -296,7 +291,7 @@ service_t *rdsvc(int dirfd, const char *filename)
const char *arg, *args[1];
service_t *svc = NULL;
char *key, *value;
- size_t argc;
+ size_t argc, nlen;
rdline_t rd;
int fd, ret;
@@ -316,18 +311,13 @@ service_t *rdsvc(int dirfd, const char *filename)
rdline_init(&rd, fd, filename, argc, args);
- svc = calloc(1, sizeof(*svc));
+ nlen = (arg != NULL) ? (size_t)(arg - filename) : strlen(filename);
+
+ svc = calloc(1, sizeof(*svc) + nlen + 1);
if (svc == NULL)
goto fail_oom;
- if (arg != NULL) {
- svc->name = strndup(filename, arg - filename);
- } else {
- svc->name = strdup(filename);
- }
-
- if (svc->name == NULL)
- goto fail_oom;
+ memcpy(svc->name, filename, nlen);
while ((ret = rdline(&rd)) == 0) {
if (splitkv(&rd, &key, &value))