X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fprocd.git;a=blobdiff_plain;f=service.c;h=8083e93d203ccfed1f58fe004ca6ac39ab65c3a9;hp=64ef3e8ab871b1836ab0c66632fb15177883b9df;hb=4fad44987a3869accf103783b4b8768c282eb86a;hpb=ac94141916ea0731f507055dcf294c8cdcb37a4d diff --git a/service.c b/service.c index 64ef3e8..8083e93 100644 --- a/service.c +++ b/service.c @@ -1,3 +1,17 @@ +/* + * Copyright (C) 2013 Felix Fietkau + * Copyright (C) 2013 John Crispin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + #include #include "procd.h" #include "service.h" @@ -35,15 +49,15 @@ service_instance_update(struct vlist_tree *tree, struct vlist_node *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); + DEBUG(1, "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); + DEBUG(1, "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); + DEBUG(1, "Create instance %s::%s\n", in_n->srv->name, in_n->name); instance_start(in_n); } } @@ -54,9 +68,7 @@ service_alloc(const char *name) struct service *s; char *new_name; - s = calloc(1, sizeof(*s) + strlen(name) + 1); - - new_name = (char *) (s + 1); + s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1); strcpy(new_name, name); vlist_init(&s->instances, avl_strcmp, service_instance_update); @@ -118,6 +130,17 @@ static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = { [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING }, }; +enum { + SERVICE_DEL_ATTR_NAME, + SERVICE_DEL_ATTR_INSTANCE, + __SERVICE_DEL_ATTR_MAX, +}; + +static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = { + [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING }, + [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING }, +}; + static int service_handle_set(struct ubus_context *ctx, struct ubus_object *obj, @@ -139,11 +162,11 @@ service_handle_set(struct ubus_context *ctx, struct ubus_object *obj, s = avl_find_element(&services, name, s, avl); if (s) { - DPRINTF("Update service %s\n", name); + DEBUG(1, "Update service %s\n", name); return service_update(s, msg, tb, add); } - DPRINTF("Create service %s\n", name); + DEBUG(1, "Create service %s\n", name); s = service_alloc(name); if (!s) return UBUS_STATUS_UNKNOWN_ERROR; @@ -196,12 +219,13 @@ service_handle_delete(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_ATTR_MAX], *cur; + struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur; struct service *s, *tmp; + struct service_instance *in; - blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg)); + blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg)); - cur = tb[SERVICE_ATTR_NAME]; + cur = tb[SERVICE_DEL_ATTR_NAME]; if (!cur) { avl_for_each_element_safe(&services, s, avl, tmp) service_delete(s); @@ -212,7 +236,20 @@ service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj, if (!s) return UBUS_STATUS_NOT_FOUND; - service_delete(s); + cur = tb[SERVICE_DEL_ATTR_INSTANCE]; + if (!cur) { + service_delete(s); + return 0; + } + + in = vlist_find(&s->instances, blobmsg_data(cur), in, node); + if (!in) { + ERROR("instance %s not found\n", (char *) blobmsg_data(cur)); + return UBUS_STATUS_NOT_FOUND; + } + + vlist_delete(&s->instances, &in->node); + return 0; } @@ -246,7 +283,7 @@ 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("delete", service_handle_delete, service_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), }; @@ -261,7 +298,7 @@ static struct ubus_object main_object = { .n_methods = ARRAY_SIZE(main_object_methods), }; -void procd_init_service(struct ubus_context *ctx) +void ubus_init_service(struct ubus_context *ctx) { avl_init(&services, avl_strcmp, false, NULL); ubus_add_object(ctx, &main_object);