summaryrefslogtreecommitdiffstats
path: root/service
diff options
context:
space:
mode:
Diffstat (limited to 'service')
-rw-r--r--service/instance.c480
-rw-r--r--service/instance.h61
-rw-r--r--service/service.c470
-rw-r--r--service/service.h57
-rw-r--r--service/trigger.c334
-rw-r--r--service/validate.c162
6 files changed, 1564 insertions, 0 deletions
diff --git a/service/instance.c b/service/instance.c
new file mode 100644
index 0000000..5ac7d57
--- /dev/null
+++ b/service/instance.c
@@ -0,0 +1,480 @@
+/*
+ * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
+ * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <sys/resource.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <fcntl.h>
+
+#include "../procd.h"
+
+#include "service.h"
+#include "instance.h"
+
+#include "../utils/md5.h"
+
+enum {
+ INSTANCE_ATTR_COMMAND,
+ INSTANCE_ATTR_ENV,
+ INSTANCE_ATTR_DATA,
+ INSTANCE_ATTR_NETDEV,
+ INSTANCE_ATTR_FILE,
+ INSTANCE_ATTR_TRIGGER,
+ INSTANCE_ATTR_RESPAWN,
+ INSTANCE_ATTR_NICE,
+ __INSTANCE_ATTR_MAX
+};
+
+static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
+ [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
+ [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
+ [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
+ [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
+ [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
+ [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
+ [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
+ [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
+};
+
+struct instance_netdev {
+ struct blobmsg_list_node node;
+ int ifindex;
+};
+
+struct instance_file {
+ struct blobmsg_list_node node;
+ uint32_t md5[4];
+};
+
+static void
+instance_run(struct service_instance *in)
+{
+ struct blobmsg_list_node *var;
+ struct blob_attr *cur;
+ char **argv;
+ int argc = 1; /* NULL terminated */
+ int rem, fd;
+
+ if (in->nice)
+ setpriority(PRIO_PROCESS, 0, in->nice);
+
+ blobmsg_for_each_attr(cur, in->command, rem)
+ argc++;
+
+ blobmsg_list_for_each(&in->env, var)
+ setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
+
+ argv = alloca(sizeof(char *) * argc);
+ argc = 0;
+
+ blobmsg_for_each_attr(cur, in->command, rem)
+ argv[argc++] = blobmsg_data(cur);
+
+ argv[argc] = NULL;
+ fd = open("/dev/null", O_RDWR);
+ if (fd > -1) {
+ dup2(fd, STDIN_FILENO);
+ dup2(fd, STDOUT_FILENO);
+ dup2(fd, STDERR_FILENO);
+ if (fd > STDERR_FILENO)
+ close(fd);
+ }
+ execvp(argv[0], argv);
+ exit(127);
+}
+
+void
+instance_start(struct service_instance *in)
+{
+ int pid;
+
+ if (in->proc.pending)
+ return;
+
+ in->restart = false;
+ in->halt = !in->respawn;
+
+ if (!in->valid)
+ return;
+
+ pid = fork();
+ if (pid < 0)
+ return;
+
+ if (!pid) {
+ uloop_done();
+ instance_run(in);
+ return;
+ }
+
+ DEBUG(2, "Started instance %s::%s\n", in->srv->name, in->name);
+ in->proc.pid = pid;
+ clock_gettime(CLOCK_MONOTONIC, &in->start);
+ uloop_process_add(&in->proc);
+}
+
+static void
+instance_timeout(struct uloop_timeout *t)
+{
+ struct service_instance *in;
+
+ in = container_of(t, struct service_instance, timeout);
+
+ if (!in->halt && (in->restart || in->respawn))
+ instance_start(in);
+}
+
+static void
+instance_exit(struct uloop_process *p, int ret)
+{
+ struct service_instance *in;
+ struct timespec tp;
+ long runtime;
+
+ in = container_of(p, struct service_instance, proc);
+
+ clock_gettime(CLOCK_MONOTONIC, &tp);
+ runtime = tp.tv_sec - in->start.tv_sec;
+
+ DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
+ if (upgrade_running)
+ return;
+
+ uloop_timeout_cancel(&in->timeout);
+ if (in->halt) {
+ /* no action */
+ } else if (in->restart) {
+ instance_start(in);
+ } else if (in->respawn) {
+ if (runtime < in->respawn_threshold)
+ in->respawn_count++;
+ else
+ in->respawn_count = 0;
+ if (in->respawn_count > in->respawn_retry) {
+ LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
+ in->srv->name, in->name, in->respawn_count, runtime);
+ in->restart = in->respawn = 0;
+ in->halt = 1;
+ } else {
+ uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
+ }
+ }
+}
+
+void
+instance_stop(struct service_instance *in)
+{
+ if (!in->proc.pending)
+ return;
+ in->halt = true;
+ in->restart = in->respawn = false;
+ kill(in->proc.pid, SIGTERM);
+}
+
+static void
+instance_restart(struct service_instance *in)
+{
+ if (!in->proc.pending)
+ return;
+ in->halt = false;
+ in->restart = true;
+ kill(in->proc.pid, SIGTERM);
+}
+
+static bool
+instance_config_changed(struct service_instance *in, struct service_instance *in_new)
+{
+ if (!in->valid)
+ return true;
+
+ if (!blob_attr_equal(in->command, in_new->command))
+ return true;
+
+ if (!blobmsg_list_equal(&in->env, &in_new->env))
+ return true;
+
+ if (!blobmsg_list_equal(&in->data, &in_new->data))
+ return true;
+
+ if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
+ return true;
+
+ if (!blobmsg_list_equal(&in->file, &in_new->file))
+ return true;
+
+ if (in->nice != in_new->nice)
+ return true;
+
+ return false;
+}
+
+static bool
+instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
+{
+ struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
+ struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
+
+ return n1->ifindex == n2->ifindex;
+}
+
+static void
+instance_netdev_update(struct blobmsg_list_node *l)
+{
+ struct instance_netdev *n = container_of(l, struct instance_netdev, node);
+
+ n->ifindex = if_nametoindex(n->node.avl.key);
+}
+
+static bool
+instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
+{
+ struct instance_file *f1 = container_of(l1, struct instance_file, node);
+ struct instance_file *f2 = container_of(l2, struct instance_file, node);
+
+ return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
+}
+
+static void
+instance_file_update(struct blobmsg_list_node *l)
+{
+ struct instance_file *f = container_of(l, struct instance_file, node);
+ md5_ctx_t md5;
+ char buf[256];
+ int len, fd;
+
+ memset(f->md5, 0, sizeof(f->md5));
+
+ fd = open(l->avl.key, O_RDONLY);
+ if (fd < 0)
+ return;
+
+ md5_begin(&md5);
+ do {
+ len = read(fd, buf, sizeof(buf));
+ if (len < 0) {
+ if (errno == EINTR)
+ continue;
+
+ break;
+ }
+ if (!len)
+ break;
+
+ md5_hash(buf, len, &md5);
+ } while(1);
+
+ md5_end(f->md5, &md5);
+ close(fd);
+}
+
+static bool
+instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
+{
+ struct blobmsg_list_node *node;
+
+ if (!cur)
+ return true;
+
+ if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
+ return false;
+
+ blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
+ if (cb) {
+ blobmsg_list_for_each(l, node)
+ cb(node);
+ }
+ return true;
+}
+
+static bool
+instance_config_parse(struct service_instance *in)
+{
+ struct blob_attr *tb[__INSTANCE_ATTR_MAX];
+ struct blob_attr *cur, *cur2;
+ int argc = 0;
+ int rem;
+
+ blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
+ blobmsg_data(in->config), blobmsg_data_len(in->config));
+
+ cur = tb[INSTANCE_ATTR_COMMAND];
+ if (!cur)
+ return false;
+
+ if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
+ return false;
+
+ blobmsg_for_each_attr(cur2, cur, rem) {
+ argc++;
+ break;
+ }
+ if (!argc)
+ return false;
+
+ in->command = cur;
+
+ if (tb[INSTANCE_ATTR_RESPAWN]) {
+ int i = 0;
+ uint32_t vals[3] = { 3600, 5, 5};
+
+ blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
+ if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
+ continue;
+ vals[i] = atoi(blobmsg_get_string(cur2));
+ i++;
+ }
+ in->respawn = true;
+ in->respawn_count = 0;
+ in->respawn_threshold = vals[0];
+ in->respawn_timeout = vals[1];
+ in->respawn_retry = vals[2];
+ }
+ if (tb[INSTANCE_ATTR_TRIGGER]) {
+ in->trigger = malloc(blob_pad_len(tb[INSTANCE_ATTR_TRIGGER]));
+ if (!in->trigger)
+ return -1;
+ memcpy(in->trigger, tb[INSTANCE_ATTR_TRIGGER], blob_pad_len(tb[INSTANCE_ATTR_TRIGGER]));
+ trigger_add(in->trigger, in);
+ }
+
+ if ((cur = tb[INSTANCE_ATTR_NICE])) {
+ in->nice = (int8_t) blobmsg_get_u32(cur);
+ if (in->nice < -20 || in->nice > 20)
+ return false;
+ }
+
+ if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
+ return false;
+
+ if (!instance_fill_array(&in->data, tb[INSTANCE_ATTR_DATA], NULL, false))
+ return false;
+
+ if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
+ return false;
+
+ if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
+ return false;
+
+ return true;
+}
+
+static void
+instance_config_cleanup(struct service_instance *in)
+{
+ blobmsg_list_free(&in->env);
+ blobmsg_list_free(&in->data);
+ blobmsg_list_free(&in->netdev);
+}
+
+static void
+instance_config_move(struct service_instance *in, struct service_instance *in_src)
+{
+ instance_config_cleanup(in);
+ blobmsg_list_move(&in->env, &in_src->env);
+ blobmsg_list_move(&in->data, &in_src->data);
+ blobmsg_list_move(&in->netdev, &in_src->netdev);
+ in->trigger = in_src->trigger;
+ in->command = in_src->command;
+ in->name = in_src->name;
+ in->node.avl.key = in_src->node.avl.key;
+
+ free(in->config);
+ in->config = in_src->config;
+ in_src->config = NULL;
+}
+
+bool
+instance_update(struct service_instance *in, struct service_instance *in_new)
+{
+ bool changed = instance_config_changed(in, in_new);
+ bool running = in->proc.pending;
+
+ if (!changed && running)
+ return false;
+
+ if (!running) {
+ if (changed)
+ instance_config_move(in, in_new);
+ instance_start(in);
+ } else {
+ instance_restart(in);
+ instance_config_move(in, in_new);
+ /* restart happens in the child callback handler */
+ }
+ return true;
+}
+
+void
+instance_free(struct service_instance *in)
+{
+ uloop_process_delete(&in->proc);
+ uloop_timeout_cancel(&in->timeout);
+ trigger_del(in);
+ free(in->trigger);
+ instance_config_cleanup(in);
+ free(in->config);
+ free(in);
+}
+
+void
+instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
+{
+ config = blob_memdup(config);
+ in->srv = s;
+ in->name = blobmsg_name(config);
+ in->config = config;
+ in->timeout.cb = instance_timeout;
+ in->proc.cb = instance_exit;
+
+ blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
+ blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
+ blobmsg_list_simple_init(&in->env);
+ blobmsg_list_simple_init(&in->data);
+ in->valid = instance_config_parse(in);
+}
+
+void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
+{
+ void *i;
+
+ i = blobmsg_open_table(b, in->name);
+ blobmsg_add_u8(b, "running", in->proc.pending);
+ if (in->proc.pending)
+ blobmsg_add_u32(b, "pid", in->proc.pid);
+ blobmsg_add_blob(b, in->command);
+
+ if (!avl_is_empty(&in->env.avl)) {
+ struct blobmsg_list_node *var;
+ void *e = blobmsg_open_table(b, "env");
+ blobmsg_list_for_each(&in->env, var)
+ blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
+ blobmsg_close_table(b, e);
+ }
+
+ if (in->respawn) {
+ void *r = blobmsg_open_table(b, "respawn");
+ blobmsg_add_u32(b, "timeout", in->respawn_timeout);
+ blobmsg_add_u32(b, "threshold", in->respawn_threshold);
+ blobmsg_add_u32(b, "retry", in->respawn_retry);
+ blobmsg_close_table(b, r);
+ }
+
+ if (verbose && in->trigger)
+ blobmsg_add_blob(b, in->trigger);
+
+ blobmsg_close_table(b, i);
+}
diff --git a/service/instance.h b/service/instance.h
new file mode 100644
index 0000000..65b670e
--- /dev/null
+++ b/service/instance.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
+ * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __PROCD_INSTANCE_H
+#define __PROCD_INSTANCE_H
+
+#include <libubox/vlist.h>
+#include <libubox/uloop.h>
+#include "../utils/utils.h"
+
+#define RESPAWN_ERROR (5 * 60)
+
+struct service_instance {
+ struct vlist_node node;
+ struct service *srv;
+ const char *name;
+
+ int8_t nice;
+ bool valid;
+
+ bool halt;
+ bool restart;
+ bool respawn;
+ int respawn_count;
+ struct timespec start;
+
+ uint32_t respawn_timeout;
+ uint32_t respawn_threshold;
+ uint32_t respawn_retry;
+
+ struct blob_attr *config;
+ struct uloop_process proc;
+ struct uloop_timeout timeout;
+
+ struct blob_attr *command;
+ struct blob_attr *trigger;
+ struct blobmsg_list env;
+ struct blobmsg_list data;
+ struct blobmsg_list netdev;
+ struct blobmsg_list file;
+};
+
+void instance_start(struct service_instance *in);
+void instance_stop(struct service_instance *in);
+bool instance_update(struct service_instance *in, struct service_instance *in_new);
+void instance_init(struct service_instance *in, struct service *s, struct blob_attr *config);
+void instance_free(struct service_instance *in);
+void instance_dump(struct blob_buf *b, struct service_instance *in, int debug);
+
+#endif
diff --git a/service/service.c b/service/service.c
new file mode 100644
index 0000000..aa393b9
--- /dev/null
+++ b/service/service.c
@@ -0,0 +1,470 @@
+/*
+ * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
+ * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <libubox/blobmsg_json.h>
+#include <libubox/avl-cmp.h>
+
+#include "../procd.h"
+
+#include "service.h"
+#include "instance.h"
+
+#include "../rcS.h"
+
+struct avl_tree services;
+static struct blob_buf b;
+
+static void
+service_instance_add(struct service *s, struct blob_attr *attr)
+{
+ struct service_instance *in;
+
+ if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
+ return;
+
+ in = calloc(1, sizeof(*in));
+ if (!in)
+ return;
+
+ instance_init(in, s, attr);
+ vlist_add(&s->instances, &in->node, (void *) in->name);
+}
+
+static void
+service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
+ struct vlist_node *node_old)
+{
+ struct service_instance *in_o = NULL, *in_n = NULL;
+
+ if (node_old)
+ in_o = container_of(node_old, struct service_instance, node);
+
+ if (node_new)
+ in_n = container_of(node_new, struct service_instance, node);
+
+ if (in_o && in_n) {
+ DEBUG(2, "Update instance %s::%s\n", in_o->srv->name, in_o->name);
+ instance_update(in_o, in_n);
+ instance_free(in_n);
+ } else if (in_o) {
+ DEBUG(2, "Free instance %s::%s\n", in_o->srv->name, in_o->name);
+ instance_stop(in_o);
+ instance_free(in_o);
+ } else if (in_n) {
+ DEBUG(2, "Create instance %s::%s\n", in_n->srv->name, in_n->name);
+ instance_start(in_n);
+ }
+}
+
+static struct service *
+service_alloc(const char *name)
+{
+ struct service *s;
+ char *new_name;
+
+ s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1);
+ strcpy(new_name, name);
+
+ vlist_init(&s->instances, avl_strcmp, service_instance_update);
+ s->instances.keep_old = true;
+ s->name = new_name;
+ s->avl.key = s->name;
+ INIT_LIST_HEAD(&s->validators);
+
+ return s;
+}
+
+enum {
+ SERVICE_SET_NAME,
+ SERVICE_SET_SCRIPT,
+ SERVICE_SET_INSTANCES,
+ SERVICE_SET_TRIGGER,
+ SERVICE_SET_VALIDATE,
+ __SERVICE_SET_MAX
+};
+
+static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
+ [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
+ [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
+ [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
+ [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
+ [SERVICE_SET_VALIDATE] = { "validate", BLOBMSG_TYPE_ARRAY },
+};
+
+static int
+service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb, bool add)
+{
+ struct blob_attr *cur;
+ int rem;
+
+ if (s->trigger) {
+ trigger_del(s);
+ free(s->trigger);
+ s->trigger = NULL;
+ }
+
+ service_validate_del(s);
+
+ if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
+ s->trigger = malloc(blob_pad_len(tb[SERVICE_SET_TRIGGER]));
+ if (!s->trigger)
+ return -1;
+ memcpy(s->trigger, tb[SERVICE_SET_TRIGGER], blob_pad_len(tb[SERVICE_SET_TRIGGER]));
+ trigger_add(s->trigger, s);
+ }
+
+ if (tb[SERVICE_SET_VALIDATE] && blobmsg_data_len(tb[SERVICE_SET_VALIDATE])) {
+ blobmsg_for_each_attr(cur, tb[SERVICE_SET_VALIDATE], rem)
+ service_validate_add(s, cur);
+ }
+
+ if (tb[SERVICE_SET_INSTANCES]) {
+ if (!add)
+ vlist_update(&s->instances);
+ blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
+ service_instance_add(s, cur);
+ }
+ if (!add)
+ vlist_flush(&s->instances);
+ }
+
+ rc(s->name, "running");
+
+ return 0;
+}
+
+static void
+service_delete(struct service *s)
+{
+ vlist_flush_all(&s->instances);
+ avl_delete(&services, &s->avl);
+ trigger_del(s);
+ s->trigger = NULL;
+ free(s->trigger);
+ free(s);
+ service_validate_del(s);
+}
+
+enum {
+ SERVICE_ATTR_NAME,
+ __SERVICE_ATTR_MAX,
+};
+
+static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
+ [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
+};
+
+enum {
+ SERVICE_DEL_ATTR_NAME,
+ SERVICE_DEL_ATTR_INSTANCE,
+ __SERVICE_DEL_ATTR_MAX,
+};
+
+static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
+ [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
+ [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
+};
+
+enum {
+ SERVICE_LIST_ATTR_VERBOSE,
+ __SERVICE_LIST_ATTR_MAX,
+};
+
+static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
+ [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
+};
+
+enum {
+ EVENT_TYPE,
+ EVENT_DATA,
+ __EVENT_MAX
+};
+
+static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
+ [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
+ [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
+};
+
+enum {
+ VALIDATE_PACKAGE,
+ VALIDATE_TYPE,
+ VALIDATE_SERVICE,
+ __VALIDATE_MAX
+};
+
+static const struct blobmsg_policy validate_policy[__VALIDATE_MAX] = {
+ [VALIDATE_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
+ [VALIDATE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
+ [VALIDATE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
+};
+
+static int
+service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
+ struct service *s = NULL;
+ const char *name;
+ int ret = UBUS_STATUS_INVALID_ARGUMENT;
+ bool add = !strcmp(method, "add");
+
+ blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
+ cur = tb[SERVICE_ATTR_NAME];
+ if (!cur)
+ goto free;
+
+ name = blobmsg_data(cur);
+
+ s = avl_find_element(&services, name, s, avl);
+ if (s) {
+ DEBUG(2, "Update service %s\n", name);
+ return service_update(s, msg, tb, add);
+ }
+
+ DEBUG(2, "Create service %s\n", name);
+ s = service_alloc(name);
+ if (!s)
+ return UBUS_STATUS_UNKNOWN_ERROR;
+
+ ret = service_update(s, msg, tb, add);
+ if (ret)
+ goto free;
+
+ avl_insert(&services, &s->avl);
+
+ return 0;
+
+free:
+ free(msg);
+ return ret;
+}
+
+static void
+service_dump(struct service *s, int verbose)
+{
+ struct service_instance *in;
+ void *c, *i;
+
+ c = blobmsg_open_table(&b, s->name);
+
+ if (avl_is_empty(&s->instances.avl)) {
+ blobmsg_close_table(&b, c);
+ return;
+ }
+
+ i = blobmsg_open_table(&b, "instances");
+ vlist_for_each_element(&s->instances, in, node)
+ instance_dump(&b, in, verbose);
+ blobmsg_close_table(&b, i);
+ if (verbose && s->trigger)
+ blobmsg_add_blob(&b, s->trigger);
+ if (verbose && !list_empty(&s->validators))
+ service_validate_dump(&b, s);
+ blobmsg_close_table(&b, c);
+}
+
+static int
+service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
+ struct service *s;
+ int verbose = 0;
+
+ blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
+
+ if (tb[SERVICE_LIST_ATTR_VERBOSE] && blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]))
+ verbose = 1;
+
+ blob_buf_init(&b, 0);
+ avl_for_each_element(&services, s, avl)
+ service_dump(s, verbose);
+
+ ubus_send_reply(ctx, req, b.head);
+
+ return 0;
+}
+
+static int
+service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
+ struct service *s;
+ struct service_instance *in;
+
+ blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
+
+ cur = tb[SERVICE_DEL_ATTR_NAME];
+ if (!cur)
+ return UBUS_STATUS_NOT_FOUND;
+
+ s = avl_find_element(&services, blobmsg_data(cur), s, avl);
+ if (!s)
+ return UBUS_STATUS_NOT_FOUND;
+
+ cur = tb[SERVICE_DEL_ATTR_INSTANCE];
+ if (!cur) {
+ service_delete(s);
+ return 0;
+ }
+
+ in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
+ if (!in) {
+ ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
+ return UBUS_STATUS_NOT_FOUND;
+ }
+
+ vlist_delete(&s->instances, &in->node);
+
+ return 0;
+}
+
+static int
+service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
+ struct service *s;
+
+ blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
+
+ cur = tb[SERVICE_ATTR_NAME];
+ if (!cur)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ s = avl_find_element(&services, blobmsg_data(cur), s, avl);
+ if (!s)
+ return UBUS_STATUS_NOT_FOUND;
+
+ if (!strcmp(method, "update_start"))
+ vlist_update(&s->instances);
+ else
+ vlist_flush(&s->instances);
+
+ return 0;
+}
+
+static int
+service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct blob_attr *tb[__EVENT_MAX];
+
+ if (!msg)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
+ if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
+
+ return 0;
+}
+
+static int
+service_handle_validate(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct blob_attr *tb[__VALIDATE_MAX];
+ char *p = NULL, *t = NULL;
+
+ if (!msg)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ blobmsg_parse(validate_policy, __VALIDATE_MAX, tb, blob_data(msg), blob_len(msg));
+ if (tb[VALIDATE_SERVICE]) {
+ return 0;
+ }
+ if (tb[VALIDATE_PACKAGE])
+ p = blobmsg_get_string(tb[VALIDATE_PACKAGE]);
+
+ if (tb[VALIDATE_TYPE])
+ t = blobmsg_get_string(tb[VALIDATE_TYPE]);
+
+ blob_buf_init(&b, 0);
+ service_validate_dump_all(&b, p, t);
+ ubus_send_reply(ctx, req, b.head);
+
+ return 0;
+}
+
+static struct ubus_method main_object_methods[] = {
+ UBUS_METHOD("set", service_handle_set, service_set_attrs),
+ UBUS_METHOD("add", service_handle_set, service_set_attrs),
+ UBUS_METHOD("list", service_handle_list, service_attrs),
+ UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
+ UBUS_METHOD("update_start", service_handle_update, service_attrs),
+ UBUS_METHOD("update_complete", service_handle_update, service_attrs),
+ UBUS_METHOD("event", service_handle_event, event_policy),
+ UBUS_METHOD("validate", service_handle_validate, validate_policy),
+};
+
+static struct ubus_object_type main_object_type =
+ UBUS_OBJECT_TYPE("service", main_object_methods);
+
+static struct ubus_object main_object = {
+ .name = "service",
+ .type = &main_object_type,
+ .methods = main_object_methods,
+ .n_methods = ARRAY_SIZE(main_object_methods),
+};
+
+int
+service_start_early(char *name, char *cmdline)
+{
+ void *instances, *instance, *command, *respawn;
+ char *t;
+
+ blob_buf_init(&b, 0);
+ blobmsg_add_string(&b, "name", name);
+ instances = blobmsg_open_table(&b, "instances");
+ instance = blobmsg_open_table(&b, "instance1");
+ command = blobmsg_open_array(&b, "command");
+ t = strtok(cmdline, " ");
+ while (t) {
+ blobmsg_add_string(&b, NULL, t);
+ t = strtok(NULL, " ");
+ }
+ blobmsg_close_array(&b, command);
+ respawn = blobmsg_open_array(&b, "respawn");
+ blobmsg_add_string(&b, NULL, "1");
+ blobmsg_add_string(&b, NULL, "3600");
+ blobmsg_add_string(&b, NULL, "10");
+ blobmsg_close_array(&b, respawn);
+ blobmsg_close_table(&b, instance);
+ blobmsg_close_table(&b, instances);
+
+ return service_handle_set(NULL, NULL, NULL, "add", b.head);
+}
+
+void ubus_init_service(struct ubus_context *ctx)
+{
+ ubus_add_object(ctx, &main_object);
+}
+
+void
+service_init(void)
+{
+ avl_init(&services, avl_strcmp, false, NULL);
+ service_validate_init();
+}
+
diff --git a/service/service.h b/service/service.h
new file mode 100644
index 0000000..46ba746
--- /dev/null
+++ b/service/service.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
+ * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __PROCD_SERVICE_H
+#define __PROCD_SERVICE_H
+
+#include <libubox/avl.h>
+#include <libubox/vlist.h>
+#include <libubox/list.h>
+
+extern struct avl_tree services;
+
+struct vrule {
+ struct avl_node avl;
+ char *option;
+ char *rule;
+};
+
+struct validate {
+ struct avl_node avl;
+ struct list_head list;
+
+ char *package;
+ char *type;
+
+ struct avl_tree rules;
+};
+
+struct service {
+ struct avl_node avl;
+ const char *name;
+
+ struct blob_attr *trigger;
+ struct vlist_tree instances;
+ struct list_head validators;
+};
+
+void service_validate_add(struct service *s, struct blob_attr *attr);
+void service_validate_dump(struct blob_buf *b, struct service *s);
+void service_validate_dump_all(struct blob_buf *b, char *p, char *s);
+int service_start_early(char *name, char *cmdline);
+void service_validate_del(struct service *s);
+void service_validate_init(void);
+void service_init(void);
+
+#endif
diff --git a/service/trigger.c b/service/trigger.c
new file mode 100644
index 0000000..41fb55d
--- /dev/null
+++ b/service/trigger.c
@@ -0,0 +1,334 @@
+/*
+ * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
+ * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+
+#include <linux/types.h>
+#include <linux/netlink.h>
+
+#include <libubox/blobmsg_json.h>
+#include <libubox/json_script.h>
+#include <libubox/runqueue.h>
+#include <libubox/ustream.h>
+#include <libubox/uloop.h>
+
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <libgen.h>
+
+#include "../procd.h"
+
+struct trigger {
+ struct list_head list;
+
+ char *type;
+
+ int pending;
+ int remove;
+ int timeout;
+
+ void *id;
+
+ struct blob_attr *rule;
+ struct blob_attr *data;
+ struct uloop_timeout delay;
+
+ struct json_script_ctx jctx;
+};
+
+struct job;
+struct cmd {
+ char *name;
+ void (*handler)(struct job *job, struct blob_attr *exec, struct blob_attr *env);
+};
+
+struct job {
+ struct runqueue_process proc;
+ struct cmd *cmd;
+ struct trigger *trigger;
+ struct blob_attr *exec;
+ struct blob_attr *env;
+};
+
+static LIST_HEAD(triggers);
+static struct runqueue q;
+
+static const char* rule_handle_var(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars)
+{
+ return NULL;
+}
+
+static struct json_script_file *
+rule_load_script(struct json_script_ctx *ctx, const char *name)
+{
+ struct trigger *t = container_of(ctx, struct trigger, jctx);
+
+ return json_script_file_from_blobmsg(t->type, t->rule, blob_pad_len(t->rule));
+}
+
+static void q_job_run(struct runqueue *q, struct runqueue_task *t)
+{
+ struct job *j = container_of(t, struct job, proc.task);
+
+ DEBUG(4, "handle event %s\n", j->cmd->name);
+ j->cmd->handler(j, j->exec, j->env);
+}
+
+static void q_job_complete(struct runqueue *q, struct runqueue_task *p)
+{
+ struct job *j = container_of(p, struct job, proc.task);
+
+ if (j->trigger->remove) {
+ list_del(&j->trigger->list);
+ free(j->trigger);
+ } else {
+ j->trigger->pending = 0;
+ }
+ free(j);
+}
+
+static void add_job(struct trigger *t, struct cmd *cmd, struct blob_attr *exec, struct blob_attr *data)
+{
+ static const struct runqueue_task_type job_type = {
+ .run = q_job_run,
+ .cancel = runqueue_process_cancel_cb,
+ .kill = runqueue_process_kill_cb,
+ };
+ struct blob_attr *d, *e;
+ struct job *j = calloc_a(sizeof(*j), &e, blob_pad_len(exec), &d, blob_pad_len(data));
+
+ j->env = d;
+ j->exec = e;
+ j->cmd = cmd;
+ j->trigger = t;
+ j->proc.task.type = &job_type;
+ j->proc.task.complete = q_job_complete;
+ t->pending = 1;
+
+ memcpy(j->exec, exec, blob_pad_len(exec));
+ memcpy(j->env, data, blob_pad_len(data));
+
+ runqueue_task_add(&q, &j->proc.task, false);
+}
+
+static void _setenv(const char *key, const char *val)
+{
+ char _key[32];
+
+ snprintf(_key, sizeof(_key), "PARAM_%s", key);
+ setenv(_key, val, 1);
+}
+
+static void handle_run_script(struct job *j, struct blob_attr *exec, struct blob_attr *env)
+{
+ char *argv[8];
+ struct blob_attr *cur;
+ int rem;
+ int i = 0;
+ pid_t pid;
+
+ pid = fork();
+ if (pid < 0)
+ return;
+
+ if (pid) {
+ runqueue_process_add(&q, &j->proc, pid);
+ return;
+ }
+
+ if (debug < 3) {
+ close(STDIN_FILENO);
+ close(STDOUT_FILENO);
+ close(STDERR_FILENO);
+ }
+
+ _setenv("type", j->trigger->type);
+ blobmsg_for_each_attr(cur, j->env, rem)
+ _setenv(blobmsg_name(cur), blobmsg_data(cur));
+
+ blobmsg_for_each_attr(cur, j->exec, rem) {
+ argv[i] = blobmsg_data(cur);
+ i++;
+ if (i == 7)
+ break;
+ }
+
+ if (i > 0) {
+ argv[i] = NULL;
+ execvp(argv[0], &argv[0]);
+ }
+
+ exit(1);
+}
+
+static struct cmd handlers[] = {
+ {
+ .name = "run_script",
+ .handler = handle_run_script,
+ },
+};
+
+static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
+ struct blob_attr *exec, struct blob_attr *vars)
+{
+ struct trigger *t = container_of(ctx, struct trigger, jctx);
+ int i;
+
+ if (t->pending)
+ return;
+
+ for (i = 0; i < ARRAY_SIZE(handlers); i++) {
+ if (!strcmp(handlers[i].name, name)) {
+ add_job(t, &handlers[i], exec, vars);
+ break;
+ }
+ }
+}
+
+static void rule_handle_error(struct json_script_ctx *ctx, const char *msg,
+ struct blob_attr *context)
+{
+ char *s;
+
+ s = blobmsg_format_json(context, false);
+ ERROR("ERROR: %s in block: %s\n", msg, s);
+ free(s);
+}
+
+static void q_empty(struct runqueue *q)
+{
+}
+
+static void trigger_delay_cb(struct uloop_timeout *tout)
+{
+ struct trigger *t = container_of(tout, struct trigger, delay);
+
+ json_script_run(&t->jctx, "foo", t->data);
+ free(t->data);
+}
+
+static struct trigger* _trigger_add(char *type, struct blob_attr *rule, int timeout, void *id)
+{
+ char *_t;
+ struct blob_attr *_r;
+ struct trigger *t = calloc_a(sizeof(*t), &_t, strlen(type) + 1, &_r, blob_pad_len(rule));
+
+ t->type = _t;
+ t->rule = _r;
+ t->delay.cb = trigger_delay_cb;
+ t->timeout = timeout;
+ t->pending = 0;
+ t->remove = 0;
+ t->id = id;
+ t->jctx.handle_var = rule_handle_var,
+ t->jctx.handle_error = rule_handle_error,
+ t->jctx.handle_command = rule_handle_command,
+ t->jctx.handle_file = rule_load_script,
+
+ strcpy(t->type, type);
+ memcpy(t->rule, rule, blob_pad_len(rule));
+
+ list_add(&t->list, &triggers);
+ json_script_init(&t->jctx);
+
+ return t;
+}
+
+void trigger_add(struct blob_attr *rule, void *id)
+{
+ struct blob_attr *cur;
+ int rem;
+
+ blobmsg_for_each_attr(cur, rule, rem) {
+ struct blob_attr *_cur, *type = NULL, *script = NULL, *timeout = NULL;
+ int _rem;
+ int i = 0;
+
+ if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY)
+ continue;
+
+ blobmsg_for_each_attr(_cur, cur, _rem) {
+ switch (i++) {
+ case 0:
+ if (blobmsg_type(_cur) == BLOBMSG_TYPE_STRING)
+ type = _cur;
+ break;
+
+ case 1:
+ if (blobmsg_type(_cur) == BLOBMSG_TYPE_ARRAY)
+ script = _cur;
+ break;
+
+ case 2:
+ if (blobmsg_type(_cur) == BLOBMSG_TYPE_INT32)
+ timeout = _cur;
+ break;
+ }
+ }
+
+ if (type && script) {
+ int t = 0;
+
+ if (timeout)
+ t = blobmsg_get_u32(timeout);
+ _trigger_add(blobmsg_get_string(type), script, t, id);
+ }
+ }
+}
+
+void trigger_del(void *id)
+{
+ struct trigger *t, *n;
+
+ list_for_each_entry_safe(t, n, &triggers, list) {
+ if (t->id != id)
+ continue;
+
+ if (t->pending) {
+ t->remove = 1;
+ continue;
+ }
+ list_del(&t->list);
+ free(t);
+ }
+}
+
+void trigger_init(void)
+{
+ runqueue_init(&q);
+ q.empty_cb = q_empty;
+ q.max_running_tasks = 1;
+}
+
+void trigger_event(char *type, struct blob_attr *data)
+{
+ struct trigger *t;
+
+ list_for_each_entry(t, &triggers, list) {
+ if (t->pending || t->remove)
+ continue;
+ if (!strcmp(t->type, type)) {
+ if (t->timeout) {
+ t->data = malloc(blob_pad_len(data));
+ memcpy(t->data, data, blob_pad_len(data));
+ uloop_timeout_set(&t->delay, t->timeout);
+ } else {
+ json_script_run(&t->jctx, "foo", data);
+ }
+ }
+ }
+}
diff --git a/service/validate.c b/service/validate.c
new file mode 100644
index 0000000..ca9bb39
--- /dev/null
+++ b/service/validate.c
@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <libubox/blobmsg_json.h>
+#include <libubox/avl-cmp.h>
+
+#include "../procd.h"
+
+#include "service.h"
+
+enum {
+ SERVICE_VAL_PACKAGE,
+ SERVICE_VAL_TYPE,
+ SERVICE_VAL_DATA,
+ __SERVICE_VAL_MAX
+};
+
+static const struct blobmsg_policy service_validate_attrs[__SERVICE_VAL_MAX] = {
+ [SERVICE_VAL_PACKAGE] = { "package", BLOBMSG_TYPE_STRING },
+ [SERVICE_VAL_TYPE] = { "type", BLOBMSG_TYPE_STRING },
+ [SERVICE_VAL_DATA] = { "data", BLOBMSG_TYPE_TABLE },
+};
+
+static struct avl_tree validators;
+
+void
+service_validate_dump_all(struct blob_buf *b, char *p, char *s)
+{
+ struct json_object *r = json_object_new_object();
+ struct validate *v;
+
+ if (!r)
+ return;
+
+ avl_for_each_element(&validators, v, avl) {
+ struct json_object *o, *t;
+ struct vrule *vr;
+
+ if (p && strcmp(p, v->package))
+ continue;
+
+ if (s && strcmp(s, v->type))
+ continue;
+
+ o = json_object_object_get(r, v->package);
+ if (!o) {
+ o = json_object_new_object();
+ json_object_object_add(r, v->package, o);
+ }
+ t = json_object_object_get(o, v->type);
+ if (!t) {
+ t = json_object_new_object();
+ json_object_object_add(o, v->type, t);
+ }
+ avl_for_each_element(&v->rules, vr, avl)
+ json_object_object_add(t, vr->option, json_object_new_string(vr->rule));
+ }
+ blobmsg_add_object(b, r);
+}
+
+void
+service_validate_dump(struct blob_buf *b, struct service *s)
+{
+ struct validate *v;
+ void *i = blobmsg_open_array(b, "validate");
+
+ list_for_each_entry(v, &s->validators, list) {
+ struct vrule *vr;
+ void *k, *j = blobmsg_open_table(b, "validate");
+
+ blobmsg_add_string(b, "package", v->package);
+ blobmsg_add_string(b, "type", v->type);
+ k = blobmsg_open_table(b, "rules");
+ avl_for_each_element(&v->rules, vr, avl)
+ blobmsg_add_string(b, vr->option, vr->rule);
+ blobmsg_close_table(b, k);
+ blobmsg_close_table(b, j);
+ }
+ blobmsg_close_array(b, i);
+}
+
+void
+service_validate_del(struct service *s)
+{
+ struct validate *v, *n;
+
+ if (list_empty(&s->validators))
+ return;
+
+ list_for_each_entry_safe(v, n, &s->validators, list) {
+ struct vrule *vr, *a;
+
+ avl_for_each_element_safe(&v->rules, vr, avl, a) {
+ avl_delete(&v->rules, &vr->avl);
+ free(vr);
+ }
+ avl_delete(&validators, &v->avl);
+ list_del(&v->list);
+ free(v);
+ }
+}
+
+void
+service_validate_add(struct service *s, struct blob_attr *msg)
+{
+ struct blob_attr *tb[__SERVICE_VAL_MAX];
+ struct validate *v;
+ char *type, *package;
+ struct blob_attr *cur;
+ int rem;
+
+ blobmsg_parse(service_validate_attrs, __SERVICE_VAL_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
+ if (!tb[SERVICE_VAL_PACKAGE] || !tb[SERVICE_VAL_TYPE] || !tb[SERVICE_VAL_DATA])
+ return;
+
+ v = calloc_a(sizeof(*v), &package, blobmsg_data_len(tb[SERVICE_VAL_PACKAGE]) + 1,
+ &type, blobmsg_data_len(tb[SERVICE_VAL_TYPE]) + 1);
+ if (!v)
+ return;
+
+ v->type = type;
+ v->avl.key = v->package = package;
+ strcpy(v->package, blobmsg_get_string(tb[SERVICE_VAL_PACKAGE]));
+ strcpy(v->type, blobmsg_get_string(tb[SERVICE_VAL_TYPE]));
+
+ list_add(&v->list, &s->validators);
+ if (avl_insert(&validators, &v->avl)) {
+ free(v);
+ return;
+ }
+ avl_init(&v->rules, avl_strcmp, false, NULL);
+
+ blobmsg_for_each_attr(cur, tb[SERVICE_VAL_DATA], rem) {
+ char *option;
+ char *rule;
+ struct vrule *vr = calloc_a(sizeof(*vr), &option, strlen(blobmsg_name(cur)) + 1,
+ &rule, strlen(blobmsg_get_string(cur)) + 1);
+
+ vr->avl.key = vr->option = option;
+ vr->rule = rule;
+ strcpy(vr->option, blobmsg_name(cur));
+ strcpy(vr->rule, blobmsg_get_string(cur));
+ if (avl_insert(&v->rules, &vr->avl))
+ free(vr);
+ }
+}
+
+void
+service_validate_init(void)
+{
+ avl_init(&validators, avl_strcmp, true, NULL);
+}