aboutsummaryrefslogtreecommitdiff
path: root/lib/src
diff options
context:
space:
mode:
Diffstat (limited to 'lib/src')
-rw-r--r--lib/src/delsvc.c17
-rw-r--r--lib/src/rdsvc.c121
-rw-r--r--lib/src/splitkv.c73
-rw-r--r--lib/src/svc_tsort.c16
4 files changed, 165 insertions, 62 deletions
diff --git a/lib/src/delsvc.c b/lib/src/delsvc.c
index 3e077fb..7370653 100644
--- a/lib/src/delsvc.c
+++ b/lib/src/delsvc.c
@@ -21,16 +21,19 @@
void delsvc(service_t *svc)
{
- size_t i;
+ exec_t *e;
- for (i = 0; i < svc->num_exec; ++i)
- free(svc->exec[i]);
+ while (svc->exec != NULL) {
+ e = svc->exec;
+ svc->exec = e->next;
- for (i = 0; i < svc->num_before; ++i)
- free(svc->before[i]);
+ free(e->argv);
+ free(e->raw_argv);
+ free(e);
+ }
- for (i = 0; i < svc->num_after; ++i)
- free(svc->after[i]);
+ free(svc->raw_before);
+ free(svc->raw_after);
free(svc->before);
free(svc->after);
diff --git a/lib/src/rdsvc.c b/lib/src/rdsvc.c
index 04cf30f..5058c76 100644
--- a/lib/src/rdsvc.c
+++ b/lib/src/rdsvc.c
@@ -38,6 +38,26 @@ static int try_unescape(char *arg, const char *filename, size_t lineno)
return 0;
}
+static char **try_split_argv(char *str, const char *filename, size_t lineno)
+{
+ char **argv = split_argv(str);
+
+ if (argv == NULL) {
+ switch (errno) {
+ case EINVAL:
+ fprintf(stderr, "%s: %zu: malformed string constant\n",
+ filename, lineno);
+ break;
+ default:
+ fprintf(stderr, "%s: %zu: %s\n", filename, lineno,
+ strerror(errno));
+ break;
+ }
+ }
+
+ return argv;
+}
+
static int svc_desc(service_t *svc, char *arg,
const char *filename, size_t lineno)
{
@@ -59,107 +79,108 @@ static int svc_tty(service_t *svc, char *arg,
static int svc_exec(service_t *svc, char *arg,
const char *filename, size_t lineno)
{
- char **new = realloc(svc->exec, sizeof(char*) * (svc->num_exec + 1));
+ exec_t *e, *end;
+ char **argv;
- if (new == NULL) {
+ argv = try_split_argv(arg, filename, lineno);
+ if (argv == NULL)
+ return -1;
+
+ e = calloc(1, sizeof(*e));
+ if (e == NULL) {
fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno);
- free(arg);
+ free(argv);
return -1;
}
- svc->exec = new;
- svc->exec[svc->num_exec++] = arg;
+ e->argv = argv;
+ e->raw_argv = arg;
+
+ if (svc->exec == NULL) {
+ svc->exec = e;
+ } else {
+ for (end = svc->exec; end->next != NULL; end = end->next)
+ ;
+ end->next = e;
+ }
return 0;
}
static int svc_before(service_t *svc, char *arg,
const char *filename, size_t lineno)
{
- char **new;
-
- if (try_unescape(arg, filename, lineno))
+ if (svc->before != NULL) {
+ fprintf(stderr, "%s: %zu: 'before' dependencies respecified\n",
+ filename, lineno);
return -1;
+ }
- new = realloc(svc->before, sizeof(char*) * (svc->num_before + 1));
-
- if (new == NULL) {
- fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno);
- free(arg);
+ svc->before = try_split_argv(arg, filename, lineno);
+ if (svc->before == NULL)
return -1;
- }
- svc->before = new;
- svc->before[svc->num_before++] = arg;
+ svc->raw_before = arg;
return 0;
}
static int svc_after(service_t *svc, char *arg,
const char *filename, size_t lineno)
{
- char **new;
-
- if (try_unescape(arg, filename, lineno))
+ if (svc->after != NULL) {
+ fprintf(stderr, "%s: %zu: 'after' dependencies respecified\n",
+ filename, lineno);
return -1;
+ }
- new = realloc(svc->after, sizeof(char*) * (svc->num_after + 1));
-
- if (new == NULL) {
- fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno);
- free(arg);
+ svc->after = try_split_argv(arg, filename, lineno);
+ if (svc->after == NULL)
return -1;
- }
- svc->after = new;
- svc->after[svc->num_after++] = arg;
+ svc->raw_after = arg;
return 0;
}
static int svc_type(service_t *svc, char *arg,
const char *filename, size_t lineno)
{
- char *extra;
- int type;
+ char **args;
+ int i, type;
- if (try_unescape(arg, filename, lineno))
- return -1;
-
- for (extra = arg; *extra != ' ' && *extra != '\0'; ++extra)
- ;
+ args = try_split_argv(arg, filename, lineno);
- if (*extra == ' ') {
- *(extra++) = '\0';
- } else {
- extra = NULL;
- }
+ if (args == NULL)
+ return -1;
- type = svc_type_from_string(arg);
+ type = svc_type_from_string(args[0]);
if (type == -1) {
fprintf(stderr, "%s: %zu: unknown service type '%s'\n",
- filename, lineno, arg);
+ filename, lineno, args[0]);
+ free(args);
return -1;
}
- if (extra != NULL) {
+ if (args[1] != NULL) {
switch (type) {
case SVC_RESPAWN:
- if (strncmp(extra, "limit ", 6) != 0)
+ if (strcmp(args[1], "limit") != 0)
goto fail_limit;
- extra += 6;
svc->rspwn_limit = 0;
- if (!isdigit(*extra))
+ if (!isdigit(args[2][0]))
goto fail_limit;
- while (isdigit(*extra)) {
+ for (i = 0; isdigit(args[2][i]); ++i) {
svc->rspwn_limit *= 10;
- svc->rspwn_limit += *(extra++) - '0';
+ svc->rspwn_limit += args[2][i] - '0';
}
- if (*extra != '\0')
+ if (args[2][i] != '\0')
goto fail_limit;
- break;
+ if (args[3] == NULL)
+ break;
+ /* fall-through */
default:
fprintf(stderr, "%s: %zu: unexpected extra arguments "
"for type '%s'\n", filename, lineno, arg);
@@ -168,11 +189,13 @@ static int svc_type(service_t *svc, char *arg,
}
svc->type = type;
+ free(args);
free(arg);
return 0;
fail_limit:
fprintf(stderr, "%s: %zu: expected 'limit <value>' after 'respawn'\n",
filename, lineno);
+ free(args);
return -1;
}
diff --git a/lib/src/splitkv.c b/lib/src/splitkv.c
index 3631ea8..7d652d3 100644
--- a/lib/src/splitkv.c
+++ b/lib/src/splitkv.c
@@ -15,7 +15,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
+#include <stdlib.h>
#include <ctype.h>
+#include <errno.h>
#include "util.h"
@@ -86,3 +88,74 @@ int unescape(char *src)
*(dst++) = '\0';
return 0;
}
+
+char **split_argv(char *str)
+{
+ size_t i = 0, cap = 0, new_cap;
+ char **argv = NULL, **new;
+ char *ptr;
+
+ ptr = str;
+
+ for (;;) {
+ if (*ptr == ' ') {
+ ++ptr;
+ continue;
+ }
+
+ if (i == cap) {
+ new_cap = cap ? cap * 2 : 16;
+ new = realloc(argv, sizeof(argv[0]) * new_cap);
+
+ if (new == NULL) {
+ free(argv);
+ errno = ENOMEM;
+ return NULL;
+ }
+
+ cap = new_cap;
+ argv = new;
+ }
+
+ if (*ptr == '\0') {
+ argv[i++] = NULL;
+ break;
+ }
+
+ argv[i++] = ptr;
+
+ if (*ptr == '"') {
+ ++ptr;
+ while (*ptr != '\0' && *ptr != '"') {
+ if (ptr[0] == '\\' && ptr[1] != '\0')
+ ++ptr;
+
+ ++ptr;
+ }
+
+ if (*ptr == '"')
+ ++ptr;
+
+ if (*ptr == ' ') {
+ *(ptr++) = '\0';
+ } else if (*ptr != '\0') {
+ goto fail_str;
+ }
+
+ if (unescape(argv[i - 1]))
+ goto fail_str;
+ } else {
+ while (*ptr != '\0' && *ptr != ' ')
+ ++ptr;
+
+ if (*ptr == ' ')
+ *(ptr++) = '\0';
+ }
+ }
+
+ return argv;
+fail_str:
+ free(argv);
+ errno = EINVAL;
+ return NULL;
+}
diff --git a/lib/src/svc_tsort.c b/lib/src/svc_tsort.c
index 50517f2..dcd1b4c 100644
--- a/lib/src/svc_tsort.c
+++ b/lib/src/svc_tsort.c
@@ -27,14 +27,18 @@ static bool has_dependencies(service_t *list, service_t *svc)
size_t i;
while (list != NULL) {
- for (i = 0; i < svc->num_after; ++i) {
- if (!strcmp(svc->after[i], list->name))
- return true;
+ if (svc->after != NULL) {
+ for (i = 0; svc->after[i] != NULL; ++i) {
+ if (!strcmp(svc->after[i], list->name))
+ return true;
+ }
}
- for (i = 0; i < list->num_before; ++i) {
- if (!strcmp(list->before[i], svc->name))
- return true;
+ if (list->before != NULL) {
+ for (i = 0; list->before[i] != NULL; ++i) {
+ if (!strcmp(list->before[i], svc->name))
+ return true;
+ }
}
list = list->next;