segfault
[project/procd.git] / service.c
index f50daa7..fe60503 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,36 @@ service_instance_add(struct service *s, struct blob_attr *attr)
        if (!in)
                return;
 
-       in->config = attr;
+       instance_init(in, s, 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) {
+               DPRINTF("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) {
+               DPRINTF("Free instance %s::%s\n", in_o->srv->name, in_o->name);
+               instance_stop(in_o, false);
+               instance_free(in_o);
+       } else if (in_n) {
+               DPRINTF("Create instance %s::%s\n", in_n->srv->name, in_n->name);
+               instance_start(in_n);
+       }
+}
+
 static struct service *
 service_alloc(const char *name)
 {
@@ -59,6 +56,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;
 }
@@ -133,9 +131,12 @@ service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
        name = blobmsg_data(cur);
 
        s = avl_find_element(&services, name, s, avl);
-       if (s)
+       if (s) {
+               DPRINTF("Update service %s\n", name);
                return service_update(s, msg, tb);
+       }
 
+       DPRINTF("Create service %s\n", name);
        s = service_alloc(name);
        if (!s)
                return UBUS_STATUS_UNKNOWN_ERROR;
@@ -153,6 +154,20 @@ free:
        return ret;
 }
 
+static void
+service_dump(struct service *s)
+{
+       struct service_instance *in;
+       void *c, *i;
+
+       c = blobmsg_open_table(&b, s->name);
+       i = blobmsg_open_table(&b, "instances");
+       vlist_for_each_element(&s->instances, in, node)
+               instance_dump(&b, in);
+       blobmsg_close_table(&b, i);
+       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,
@@ -161,12 +176,8 @@ service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
        struct service *s;
 
        blob_buf_init(&b, 0);
-       avl_for_each_element(&services, s, avl) {
-               void *c;
-
-               c = blobmsg_open_table(&b, s->name);
-               blobmsg_close_table(&b, c);
-       }
+       avl_for_each_element(&services, s, avl)
+               service_dump(s);
 
        ubus_send_reply(ctx, req, b.head);