add simple blobmsg list wrapper
[project/procd.git] / service.c
index f50daa7..46f846d 100644 (file)
--- a/service.c
+++ b/service.c
@@ -1,41 +1,12 @@
 #include <libubox/avl-cmp.h>
 #include "procd.h"
 #include "service.h"
+#include "instance.h"
 
 struct avl_tree services;
 static struct blob_buf b;
 
 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);
-
-       do {
-               if (!in_o || !in_n)
-                       break;
-
-               /* full match, nothing to do */
-               return;
-       } while (0);
-
-       if (in_o) {
-               /* kill old process */
-               free(in_o);
-       }
-
-       if (in_n) {
-               /* start new process */
-       }
-}
-
-static void
 service_instance_add(struct service *s, struct blob_attr *attr)
 {
        struct service_instance *in;
@@ -48,10 +19,33 @@ service_instance_add(struct service *s, struct blob_attr *attr)
        if (!in)
                return;
 
-       in->config = attr;
+       instance_init(in, attr);
        vlist_add(&s->instances, &in->node, (void *) 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) {
+               instance_update(in_o, in_n);
+               instance_free(in_n);
+       } else if (in_o) {
+               instance_stop(in_o, false);
+               instance_free(in_o);
+       } else if (in_n) {
+               instance_start(in_n);
+       }
+}
+
 static struct service *
 service_alloc(const char *name)
 {
@@ -59,6 +53,7 @@ service_alloc(const char *name)
 
        s = calloc(1, sizeof(*s));
        vlist_init(&s->instances, avl_strcmp, service_instance_update);
+       s->instances.keep_old = true;
 
        return s;
 }