service/service_stopped(): fix a use-after-free
[project/procd.git] / service / service.c
index 9675ba2..72e269d 100644 (file)
@@ -60,8 +60,8 @@ service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
                instance_free(in_n);
        } else if (in_o) {
                DEBUG(2, "Stop instance %s::%s\n", in_o->srv->name, in_o->name);
                instance_free(in_n);
        } else if (in_o) {
                DEBUG(2, "Stop instance %s::%s\n", in_o->srv->name, in_o->name);
-               instance_stop(in_o);
-       } else if (in_n) {
+               instance_stop(in_o, true);
+       } else if (in_n && in_n->srv->autostart) {
                DEBUG(2, "Start instance %s::%s\n", in_n->srv->name, in_n->name);
                instance_start(in_n);
        }
                DEBUG(2, "Start instance %s::%s\n", in_n->srv->name, in_n->name);
                instance_start(in_n);
        }
@@ -93,6 +93,7 @@ enum {
        SERVICE_SET_INSTANCES,
        SERVICE_SET_TRIGGER,
        SERVICE_SET_VALIDATE,
        SERVICE_SET_INSTANCES,
        SERVICE_SET_TRIGGER,
        SERVICE_SET_VALIDATE,
+       SERVICE_SET_AUTOSTART,
        __SERVICE_SET_MAX
 };
 
        __SERVICE_SET_MAX
 };
 
@@ -102,6 +103,7 @@ static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
        [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
        [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
        [SERVICE_SET_VALIDATE] = { "validate", BLOBMSG_TYPE_ARRAY },
        [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
        [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
        [SERVICE_SET_VALIDATE] = { "validate", BLOBMSG_TYPE_ARRAY },
+       [SERVICE_SET_AUTOSTART] = { "autostart", BLOBMSG_TYPE_BOOL },
 };
 
 static int
 };
 
 static int
@@ -118,6 +120,11 @@ service_update(struct service *s, struct blob_attr **tb, bool add)
 
        service_validate_del(s);
 
 
        service_validate_del(s);
 
+       if (tb[SERVICE_SET_AUTOSTART] && !blobmsg_get_bool(tb[SERVICE_SET_AUTOSTART]))
+               s->autostart = false;
+       else
+               s->autostart = true;
+
        if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
                s->trigger = blob_memdup(tb[SERVICE_SET_TRIGGER]);
                if (!s->trigger)
        if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
                s->trigger = blob_memdup(tb[SERVICE_SET_TRIGGER]);
                if (!s->trigger)
@@ -200,6 +207,17 @@ static const struct blobmsg_policy service_signal_attrs[__SERVICE_SIGNAL_ATTR_MA
 };
 
 enum {
 };
 
 enum {
+       SERVICE_STATE_ATTR_SPAWN,
+       SERVICE_STATE_ATTR_NAME,
+       __SERVICE_STATE_ATTR_MAX,
+};
+
+static const struct blobmsg_policy service_state_attrs[__SERVICE_STATE_ATTR_MAX] = {
+       [SERVICE_STATE_ATTR_SPAWN] = { "spawn", BLOBMSG_TYPE_BOOL },
+       [SERVICE_STATE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
+};
+
+enum {
        EVENT_TYPE,
        EVENT_DATA,
        __EVENT_MAX
        EVENT_TYPE,
        EVENT_DATA,
        __EVENT_MAX
@@ -284,6 +302,9 @@ service_dump(struct service *s, bool verbose)
 
        c = blobmsg_open_table(&b, s->name);
 
 
        c = blobmsg_open_table(&b, s->name);
 
+       if (!s->autostart)
+               blobmsg_add_u8(&b, "autostart", false);
+
        if (!avl_is_empty(&s->instances.avl)) {
                i = blobmsg_open_table(&b, "instances");
                vlist_for_each_element(&s->instances, in, node)
        if (!avl_is_empty(&s->instances.avl)) {
                i = blobmsg_open_table(&b, "instances");
                vlist_for_each_element(&s->instances, in, node)
@@ -421,6 +442,41 @@ service_handle_signal(struct ubus_context *ctx, struct ubus_object *obj,
 }
 
 static int
 }
 
 static int
+service_handle_state(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_STATE_ATTR_MAX];
+       struct service *s;
+       struct service_instance *in;
+       int spawn;
+
+       blobmsg_parse(service_state_attrs, __SERVICE_STATE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
+
+       if (!tb[SERVICE_STATE_ATTR_SPAWN])
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       if (!tb[SERVICE_STATE_ATTR_NAME])
+               return UBUS_STATUS_NOT_FOUND;
+
+       s = avl_find_element(&services, blobmsg_data(tb[SERVICE_STATE_ATTR_NAME]), s, avl);
+       if (!s)
+               return UBUS_STATUS_NOT_FOUND;
+
+       spawn = !!blobmsg_get_u8(tb[SERVICE_STATE_ATTR_SPAWN]);
+       vlist_for_each_element(&s->instances, in, node) {
+               if (!!in->proc.pending == !!spawn)
+                       continue;
+               else if (!in->proc.pending)
+                       instance_start(in);
+               else
+                       instance_stop(in, false);
+       }
+
+       return UBUS_STATUS_OK;
+}
+
+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)
 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
                      struct ubus_request_data *req, const char *method,
                      struct blob_attr *msg)
@@ -563,6 +619,7 @@ static struct ubus_method main_object_methods[] = {
        UBUS_METHOD("event", service_handle_event, event_policy),
        UBUS_METHOD("validate", service_handle_validate, validate_policy),
        UBUS_METHOD("get_data", service_get_data, get_data_policy),
        UBUS_METHOD("event", service_handle_event, event_policy),
        UBUS_METHOD("validate", service_handle_validate, validate_policy),
        UBUS_METHOD("get_data", service_get_data, get_data_policy),
+       UBUS_METHOD("state", service_handle_state, service_state_attrs),
 };
 
 static struct ubus_object_type main_object_type =
 };
 
 static struct ubus_object_type main_object_type =
@@ -609,9 +666,9 @@ void service_stopped(struct service *s)
                service_event("service.stop", s->name, NULL);
                avl_delete(&services, &s->avl);
                trigger_del(s);
                service_event("service.stop", s->name, NULL);
                avl_delete(&services, &s->avl);
                trigger_del(s);
+               service_validate_del(s);
                free(s->trigger);
                free(s);
                free(s->trigger);
                free(s);
-               service_validate_del(s);
        }
 }
 
        }
 }