1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-11-25 20:40:48 +01:00

Cleanup: redeuce number of allocations in rdsvc

Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
This commit is contained in:
David Oberhollenzer 2018-04-11 13:29:13 +02:00
parent 77725291ef
commit 67d000cdc3
3 changed files with 38 additions and 50 deletions

View file

@ -45,16 +45,16 @@ enum {
}; };
typedef struct exec_t { typedef struct exec_t {
char **argv; /* NULL terminated argument vector */
char *raw_argv; /* backing store for argv contents */
struct exec_t *next; struct exec_t *next;
char **argv; /* NULL terminated argument vector */
char buffer[]; /* backing store for argv */
} exec_t; } exec_t;
typedef struct service_t { typedef struct service_t {
struct service_t *next;
int type; /* SVC_* service type */ int type; /* SVC_* service type */
int target; /* TGT_* service target */ int target; /* TGT_* service target */
char *name; /* canonical service name */
char *desc; /* description string */ char *desc; /* description string */
char *ctty; /* controlling tty or log file */ char *ctty; /* controlling tty or log file */
int rspwn_limit; /* maximum respawn count */ int rspwn_limit; /* maximum respawn count */
@ -74,7 +74,7 @@ typedef struct service_t {
pid_t pid; pid_t pid;
int status; /* process exit status */ int status; /* process exit status */
struct service_t *next; char name[]; /* canonical service name */
} service_t; } service_t;
typedef struct { typedef struct {

View file

@ -31,7 +31,6 @@ void delsvc(service_t *svc)
svc->exec = e->next; svc->exec = e->next;
free(e->argv); free(e->argv);
free(e->raw_argv);
free(e); free(e);
} }
@ -40,7 +39,6 @@ void delsvc(service_t *svc)
free(svc->before); free(svc->before);
free(svc->after); free(svc->after);
free(svc->name);
free(svc->desc); free(svc->desc);
free(svc->exec); free(svc->exec);
free(svc->ctty); free(svc->ctty);

View file

@ -89,22 +89,17 @@ static int svc_exec(service_t *svc, char *arg, rdline_t *rd)
{ {
exec_t *e, *end; exec_t *e, *end;
e = calloc(1, sizeof(*e)); e = calloc(1, sizeof(*e) + strlen(arg) + 1);
if (e == NULL) { if (e == NULL) {
fprintf(stderr, "%s: %zu: out of memory\n", fprintf(stderr, "%s: %zu: out of memory\n",
rd->filename, rd->lineno); rd->filename, rd->lineno);
return -1; return -1;
} }
e->raw_argv = try_strdup(arg, rd); strcpy(e->buffer, arg);
if (e->raw_argv == NULL) {
free(e);
return -1;
}
e->argv = try_split_argv(e->raw_argv, rd); e->argv = try_split_argv(e->buffer, rd);
if (e->argv == NULL) { if (e->argv == NULL) {
free(e->raw_argv);
free(e); free(e);
return -1; 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) static int svc_type(service_t *svc, char *arg, rdline_t *rd)
{ {
char **args; char *ptr;
int i, type;
args = try_split_argv(arg, rd); for (ptr = arg; *ptr != ' ' && *ptr != '\0'; ++ptr)
;
if (*ptr == ' ')
*(ptr++) = '\0';
if (args == NULL) svc->type = svc_type_from_string(arg);
return -1;
type = svc_type_from_string(args[0]); if (svc->type == -1) {
if (type == -1) {
fprintf(stderr, "%s: %zu: unknown service type '%s'\n", fprintf(stderr, "%s: %zu: unknown service type '%s'\n",
rd->filename, rd->lineno, args[0]); rd->filename, rd->lineno, arg);
free(args);
return -1; return -1;
} }
if (args[1] != NULL) { if (*ptr != '\0') {
switch (type) { switch (svc->type) {
case SVC_RESPAWN: 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; goto fail_limit;
svc->rspwn_limit = 0; svc->rspwn_limit = 0;
if (!isdigit(args[2][0])) if (!isdigit(*ptr))
goto fail_limit; goto fail_limit;
for (i = 0; isdigit(args[2][i]); ++i) { while (isdigit(*ptr)) {
svc->rspwn_limit *= 10; svc->rspwn_limit *= 10;
svc->rspwn_limit += args[2][i] - '0'; svc->rspwn_limit += *(ptr++) - '0';
} }
if (args[2][i] != '\0') if (*ptr == '\0')
goto fail_limit;
if (args[3] == NULL)
break; break;
if (*ptr != ' ')
goto fail_limit;
/* fall-through */ /* fall-through */
default: default:
fprintf(stderr, "%s: %zu: unexpected extra arguments " fprintf(stderr,
"for type '%s'\n", "%s: %zu: unexpected extra arguments\n",
rd->filename, rd->lineno, arg); rd->filename, rd->lineno);
return -1; return -1;
} }
} }
svc->type = type;
free(args);
return 0; return 0;
fail_limit: fail_limit:
fprintf(stderr, "%s: %zu: expected 'limit <value>' after 'respawn'\n", fprintf(stderr, "%s: %zu: expected 'limit <value>' after 'respawn'\n",
rd->filename, rd->lineno); rd->filename, rd->lineno);
free(args);
return -1; return -1;
} }
@ -296,7 +291,7 @@ service_t *rdsvc(int dirfd, const char *filename)
const char *arg, *args[1]; const char *arg, *args[1];
service_t *svc = NULL; service_t *svc = NULL;
char *key, *value; char *key, *value;
size_t argc; size_t argc, nlen;
rdline_t rd; rdline_t rd;
int fd, ret; int fd, ret;
@ -316,18 +311,13 @@ service_t *rdsvc(int dirfd, const char *filename)
rdline_init(&rd, fd, filename, argc, args); 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) if (svc == NULL)
goto fail_oom; goto fail_oom;
if (arg != NULL) { memcpy(svc->name, filename, nlen);
svc->name = strndup(filename, arg - filename);
} else {
svc->name = strdup(filename);
}
if (svc->name == NULL)
goto fail_oom;
while ((ret = rdline(&rd)) == 0) { while ((ret = rdline(&rd)) == 0) {
if (splitkv(&rd, &key, &value)) if (splitkv(&rd, &key, &value))