X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fprocd.git;a=blobdiff_plain;f=service%2Fservice.c;h=95978b5e25728ddbe08844171133f513eb3e031f;hp=74feded39ed292e651f33941cdc6f553cba79fd0;hb=21ec2c7fb4ec4bae646456494090b8afa35ffbba;hpb=45ca87272954f46eb6d55365d5a2fbe3520d66ce diff --git a/service/service.c b/service/service.c index 74feded..95978b5 100644 --- a/service/service.c +++ b/service/service.c @@ -22,7 +22,7 @@ #include "../rcS.h" -struct avl_tree services; +AVL_TREE(services, avl_strcmp, false, NULL); static struct blob_buf b; static struct ubus_context *ctx; @@ -66,6 +66,8 @@ service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new, DEBUG(2, "Create 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 * @@ -177,11 +179,13 @@ 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 }, }; @@ -209,6 +213,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, @@ -250,7 +267,7 @@ service_handle_set(struct ubus_context *ctx, struct ubus_object *obj, } static void -service_dump(struct service *s, int verbose) +service_dump(struct service *s, bool verbose) { struct service_instance *in; void *c, *i; @@ -277,16 +294,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); @@ -339,7 +363,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; @@ -402,15 +426,75 @@ 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("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), }; static struct ubus_object_type main_object_type = @@ -468,11 +552,3 @@ void ubus_init_service(struct ubus_context *_ctx) ctx = _ctx; ubus_add_object(ctx, &main_object); } - -void -service_init(void) -{ - avl_init(&services, avl_strcmp, false, NULL); - service_validate_init(); -} -