service/service_stopped(): fix a use-after-free
[project/procd.git] / service / service.c
index 8d1217f..72e269d 100644 (file)
@@ -22,8 +22,9 @@
 
 #include "../rcS.h"
 
-struct avl_tree services;
+AVL_TREE(services, avl_strcmp, false, NULL);
 static struct blob_buf b;
+static struct ubus_context *ctx;
 
 static void
 service_instance_add(struct service *s, struct blob_attr *attr)
@@ -58,13 +59,14 @@ service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
                instance_update(in_o, in_n);
                instance_free(in_n);
        } else if (in_o) {
-               DEBUG(2, "Free instance %s::%s\n", in_o->srv->name, in_o->name);
-               instance_stop(in_o);
-               instance_free(in_o);
-       } else if (in_n) {
-               DEBUG(2, "Create instance %s::%s\n", in_n->srv->name, in_n->name);
+               DEBUG(2, "Stop instance %s::%s\n", in_o->srv->name, in_o->name);
+               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);
        }
+       blob_buf_init(&b, 0);
+       trigger_event("instance.update", b.head);
 }
 
 static struct service *
@@ -77,7 +79,7 @@ service_alloc(const char *name)
        strcpy(new_name, name);
 
        vlist_init(&s->instances, avl_strcmp, service_instance_update);
-       s->instances.keep_old = true;
+       s->instances.no_delete = true;
        s->name = new_name;
        s->avl.key = s->name;
        INIT_LIST_HEAD(&s->validators);
@@ -91,6 +93,7 @@ enum {
        SERVICE_SET_INSTANCES,
        SERVICE_SET_TRIGGER,
        SERVICE_SET_VALIDATE,
+       SERVICE_SET_AUTOSTART,
        __SERVICE_SET_MAX
 };
 
@@ -100,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_AUTOSTART] = { "autostart", BLOBMSG_TYPE_BOOL },
 };
 
 static int
@@ -116,6 +120,11 @@ service_update(struct service *s, struct blob_attr **tb, bool add)
 
        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)
@@ -138,6 +147,8 @@ service_update(struct service *s, struct blob_attr **tb, bool add)
                        vlist_flush(&s->instances);
        }
 
+       s->deleted = false;
+
        rc(s->name, "running");
 
        return 0;
@@ -147,11 +158,8 @@ static void
 service_delete(struct service *s)
 {
        vlist_flush_all(&s->instances);
-       avl_delete(&services, &s->avl);
-       trigger_del(s);
-       free(s->trigger);
-       free(s);
-       service_validate_del(s);
+       s->deleted = true;
+       service_stopped(s);
 }
 
 enum {
@@ -175,15 +183,41 @@ static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
 };
 
 enum {
+       SERVICE_LIST_ATTR_NAME,
        SERVICE_LIST_ATTR_VERBOSE,
        __SERVICE_LIST_ATTR_MAX,
 };
 
 static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
+       [SERVICE_LIST_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
        [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
 };
 
 enum {
+       SERVICE_SIGNAL_ATTR_NAME,
+       SERVICE_SIGNAL_ATTR_INSTANCE,
+       SERVICE_SIGNAL_ATTR_SIGNAL,
+       __SERVICE_SIGNAL_ATTR_MAX,
+};
+
+static const struct blobmsg_policy service_signal_attrs[__SERVICE_SIGNAL_ATTR_MAX] = {
+       [SERVICE_SIGNAL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
+       [SERVICE_SIGNAL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
+       [SERVICE_SIGNAL_ATTR_SIGNAL] = { "signal", BLOBMSG_TYPE_INT32 },
+};
+
+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
@@ -207,6 +241,19 @@ static const struct blobmsg_policy validate_policy[__VALIDATE_MAX] = {
        [VALIDATE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
 };
 
+enum {
+       DATA_NAME,
+       DATA_INSTANCE,
+       DATA_TYPE,
+       __DATA_MAX
+};
+
+static const struct blobmsg_policy get_data_policy[] = {
+       [DATA_NAME] = { "name", BLOBMSG_TYPE_STRING },
+       [DATA_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
+       [DATA_TYPE] = { "type", BLOBMSG_TYPE_STRING },
+};
+
 static int
 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
                   struct ubus_request_data *req, const char *method,
@@ -242,17 +289,22 @@ service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
 
        avl_insert(&services, &s->avl);
 
+       service_event("service.start", s->name, NULL);
+
        return 0;
 }
 
 static void
-service_dump(struct service *s, int verbose)
+service_dump(struct service *s, bool verbose)
 {
        struct service_instance *in;
        void *c, *i;
 
        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)
@@ -273,16 +325,23 @@ service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
 {
        struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
        struct service *s;
-       int verbose = 0;
+       const char *name = NULL;
+       bool verbose = false;
 
        blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
 
-       if (tb[SERVICE_LIST_ATTR_VERBOSE] && blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]))
-               verbose = 1;
+       if (tb[SERVICE_LIST_ATTR_VERBOSE])
+               verbose = blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]);
+       if (tb[SERVICE_LIST_ATTR_NAME])
+               name = blobmsg_get_string(tb[SERVICE_LIST_ATTR_NAME]);
 
        blob_buf_init(&b, 0);
-       avl_for_each_element(&services, s, avl)
+       avl_for_each_element(&services, s, avl) {
+               if (name && strcmp(s->name, name) != 0)
+                       continue;
+
                service_dump(s, verbose);
+       }
 
        ubus_send_reply(ctx, req, b.head);
 
@@ -326,6 +385,98 @@ service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
 }
 
 static int
+service_handle_kill(struct service_instance *in, int sig)
+{
+       if (kill(in->proc.pid, sig) == 0)
+               return 0;
+
+       switch (errno) {
+       case EINVAL: return UBUS_STATUS_INVALID_ARGUMENT;
+       case EPERM:  return UBUS_STATUS_PERMISSION_DENIED;
+       case ESRCH:  return UBUS_STATUS_NOT_FOUND;
+       }
+
+       return UBUS_STATUS_UNKNOWN_ERROR;
+}
+
+static int
+service_handle_signal(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_SIGNAL_ATTR_MAX], *cur;
+       struct service *s;
+       struct service_instance *in;
+       int sig = SIGHUP;
+       int rv = 0;
+
+       blobmsg_parse(service_signal_attrs, __SERVICE_SIGNAL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
+
+       cur = tb[SERVICE_SIGNAL_ATTR_SIGNAL];
+       if (cur)
+               sig = blobmsg_get_u32(cur);
+
+       cur = tb[SERVICE_SIGNAL_ATTR_NAME];
+       if (!cur)
+               return UBUS_STATUS_NOT_FOUND;
+
+       s = avl_find_element(&services, blobmsg_data(cur), s, avl);
+       if (!s)
+               return UBUS_STATUS_NOT_FOUND;
+
+       cur = tb[SERVICE_SIGNAL_ATTR_INSTANCE];
+       if (!cur) {
+               vlist_for_each_element(&s->instances, in, node)
+                       rv = service_handle_kill(in, sig);
+
+               return rv;
+       }
+
+       in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
+       if (!in) {
+               ERROR("instance %s not found\n", blobmsg_get_string(cur));
+               return UBUS_STATUS_NOT_FOUND;
+       }
+
+       return service_handle_kill(in, sig);
+}
+
+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)
@@ -335,7 +486,7 @@ service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
 
        blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
 
-       cur = tb[SERVICE_ATTR_NAME];
+       cur = tb[SERVICE_SET_NAME];
        if (!cur)
                return UBUS_STATUS_INVALID_ARGUMENT;
 
@@ -398,15 +549,77 @@ service_handle_validate(struct ubus_context *ctx, struct ubus_object *obj,
        return 0;
 }
 
+static int
+service_get_data(struct ubus_context *ctx, struct ubus_object *obj,
+                struct ubus_request_data *req, const char *method,
+                struct blob_attr *msg)
+{
+       struct service_instance *in;
+       struct service *s;
+       struct blob_attr *tb[__DATA_MAX];
+       const char *name = NULL;
+       const char *instance = NULL;
+       const char *type = NULL;
+
+       blobmsg_parse(get_data_policy, __DATA_MAX, tb, blob_data(msg), blob_len(msg));
+       if (tb[DATA_NAME])
+               name = blobmsg_data(tb[DATA_NAME]);
+       if (tb[DATA_INSTANCE])
+               instance = blobmsg_data(tb[DATA_INSTANCE]);
+       if (tb[DATA_TYPE])
+               type = blobmsg_data(tb[DATA_TYPE]);
+
+       blob_buf_init(&b, 0);
+       avl_for_each_element(&services, s, avl) {
+               void *cs = NULL;
+
+               if (name && strcmp(name, s->name))
+                       continue;
+
+               vlist_for_each_element(&s->instances, in, node) {
+                       struct blobmsg_list_node *var;
+                       void *ci = NULL;
+
+                       if (instance && strcmp(instance, in->name))
+                               continue;
+
+                       blobmsg_list_for_each(&in->data, var) {
+                               if (type &&
+                                   strcmp(blobmsg_name(var->data), type))
+                                       continue;
+
+                               if (!cs)
+                                       cs = blobmsg_open_table(&b, s->name);
+                               if (!ci)
+                                       ci = blobmsg_open_table(&b, in->name);
+
+                               blobmsg_add_blob(&b, var->data);
+                       }
+
+                       if (ci)
+                               blobmsg_close_table(&b, ci);
+               }
+
+               if (cs)
+                       blobmsg_close_table(&b, cs);
+       }
+
+       ubus_send_reply(ctx, req, b.head);
+       return 0;
+}
+
 static struct ubus_method main_object_methods[] = {
        UBUS_METHOD("set", service_handle_set, service_set_attrs),
        UBUS_METHOD("add", service_handle_set, service_set_attrs),
-       UBUS_METHOD("list", service_handle_list, service_attrs),
+       UBUS_METHOD("list", service_handle_list, service_list_attrs),
        UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
+       UBUS_METHOD("signal", service_handle_signal, service_signal_attrs),
        UBUS_METHOD("update_start", service_handle_update, service_attrs),
        UBUS_METHOD("update_complete", service_handle_update, service_attrs),
        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 =
@@ -447,15 +660,32 @@ 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_stopped(struct service *s)
 {
-       ubus_add_object(ctx, &main_object);
+       if (s->deleted && avl_is_empty(&s->instances.avl)) {
+               service_event("service.stop", s->name, NULL);
+               avl_delete(&services, &s->avl);
+               trigger_del(s);
+               service_validate_del(s);
+               free(s->trigger);
+               free(s);
+       }
 }
 
-void
-service_init(void)
+void service_event(const char *type, const char *service, const char *instance)
 {
-       avl_init(&services, avl_strcmp, false, NULL);
-       service_validate_init();
+       if (!ctx)
+               return;
+
+       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);
+}