From: Felix Fietkau Date: Sun, 24 Jun 2012 16:43:46 +0000 (+0200) Subject: add more parsing code X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=14447cd46a3c6ad3f5b08e921b39a694b4ad14b0;p=oweals%2Fprocd.git add more parsing code --- diff --git a/instance.c b/instance.c index e2c8b28..8c3af4b 100644 --- a/instance.c +++ b/instance.c @@ -2,6 +2,19 @@ #include "service.h" #include "instance.h" +enum { + INSTANCE_ATTR_COMMAND, + INSTANCE_ATTR_ENV, + INSTANCE_ATTR_DATA, + __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 }, +}; + void instance_start(struct service_instance *in) { @@ -42,17 +55,57 @@ instance_stop(struct service_instance *in, bool restart) static bool instance_config_changed(struct service_instance *in, struct service_instance *in_new) { - int len = blob_pad_len(in->config); + if (!in->valid) + return true; + + if (!blob_attr_equal(in->command, in_new->command)) + return true; - if (len != blob_pad_len(in_new->config)) + if (!blobmsg_list_equal(&in->env, &in_new->env)) return true; - if (memcmp(in->config, in_new->config, blob_pad_len(in->config)) != 0) + if (!blobmsg_list_equal(&in->data, &in_new->data)) return true; return false; } +static bool +instance_config_parse(struct service_instance *in) +{ + struct blob_attr *tb[__INSTANCE_ATTR_MAX]; + struct blob_attr *cur; + + blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb, + blobmsg_data(in->config), blobmsg_data_len(in->config)); + if (!tb[INSTANCE_ATTR_COMMAND]) + return false; + + if ((cur = tb[INSTANCE_ATTR_ENV])) + blobmsg_list_fill(&in->env, blobmsg_data(cur), blobmsg_data_len(cur)); + + if ((cur = tb[INSTANCE_ATTR_DATA])) + blobmsg_list_fill(&in->data, blobmsg_data(cur), blobmsg_data_len(cur)); + + return true; +} + +static void +instance_config_cleanup(struct service_instance *in) +{ + blobmsg_list_free(&in->env); + blobmsg_list_free(&in->data); +} + +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); + in->command = in_src->command; +} + bool instance_update(struct service_instance *in, struct service_instance *in_new) { @@ -63,6 +116,7 @@ instance_update(struct service_instance *in, struct service_instance *in_new) return false; instance_stop(in, true); + instance_config_move(in, in_new); return true; } @@ -71,6 +125,7 @@ instance_free(struct service_instance *in) { uloop_process_delete(&in->proc); uloop_timeout_cancel(&in->timeout); + instance_config_cleanup(in); free(in); } @@ -80,6 +135,10 @@ instance_init(struct service_instance *in, struct blob_attr *config) in->config = config; in->timeout.cb = instance_timeout; in->proc.cb = instance_exit; + + blobmsg_list_simple_init(&in->env); + blobmsg_list_simple_init(&in->data); + in->valid = instance_config_parse(in); } diff --git a/instance.h b/instance.h index 668dde0..ebe4d20 100644 --- a/instance.h +++ b/instance.h @@ -3,15 +3,21 @@ #include #include +#include "utils.h" struct service_instance { struct vlist_node node; const char *name; + bool valid; bool restart; struct blob_attr *config; struct uloop_process proc; struct uloop_timeout timeout; + + struct blob_attr *command; + struct blobmsg_list env; + struct blobmsg_list data; }; void instance_start(struct service_instance *in);