send ubus_notify events when servers and instances change state
[project/procd.git] / service / service.c
index bfc8d492edb9a539b6b918510e15fe11e489603d..642692963a2dfd3e949740f89ebdfbfedbc8b8ac 100644 (file)
@@ -24,6 +24,7 @@
 
 struct avl_tree services;
 static struct blob_buf b;
+static struct ubus_context *ctx;
 
 static void
 service_instance_add(struct service *s, struct blob_attr *attr)
@@ -103,7 +104,7 @@ static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
 };
 
 static int
-service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb, bool add)
+service_update(struct service *s, struct blob_attr **tb, bool add)
 {
        struct blob_attr *cur;
        int rem;
@@ -146,10 +147,10 @@ service_update(struct service *s, struct blob_attr *config, struct blob_attr **t
 static void
 service_delete(struct service *s)
 {
+       service_event("service.stop", s->name, NULL);
        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);
@@ -229,7 +230,7 @@ service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
        s = avl_find_element(&services, name, s, avl);
        if (s) {
                DEBUG(2, "Update service %s\n", name);
-               return service_update(s, msg, tb, add);
+               return service_update(s, tb, add);
        }
 
        DEBUG(2, "Create service %s\n", name);
@@ -237,12 +238,14 @@ service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
        if (!s)
                return UBUS_STATUS_UNKNOWN_ERROR;
 
-       ret = service_update(s, msg, tb, add);
+       ret = service_update(s, tb, add);
        if (ret)
                return ret;
 
        avl_insert(&services, &s->avl);
 
+       service_event("service.start", s->name, NULL);
+
        return 0;
 }
 
@@ -448,8 +451,18 @@ service_start_early(char *name, char *cmdline)
        return service_handle_set(NULL, NULL, NULL, "add", b.head);
 }
 
-void ubus_init_service(struct ubus_context *ctx)
+void service_event(const char *type, const char *service, const char *instance)
+{
+       blob_buf_init(&b, 0);
+       blobmsg_add_string(&b, "service", service);
+       if (instance)
+               blobmsg_add_string(&b, "instance", instance);
+       ubus_notify(ctx, &main_object, type, b.head, -1);
+}
+
+void ubus_init_service(struct ubus_context *_ctx)
 {
+       ctx = _ctx;
        ubus_add_object(ctx, &main_object);
 }