X-Git-Url: http://git.openwrt.org/?a=blobdiff_plain;ds=sidebyside;f=service%2Fservice.c;h=642692963a2dfd3e949740f89ebdfbfedbc8b8ac;hb=1fd4e60e9d03c0e3aa79f5f0bac87f07c63081f5;hp=4153fec917053f27476c6618678c855268233e5a;hpb=eb7c76ede8d0d41c7139c704236c6111da56da46;p=project%2Fprocd.git diff --git a/service/service.c b/service/service.c index 4153fec..6426929 100644 --- a/service/service.c +++ b/service/service.c @@ -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; @@ -117,10 +118,9 @@ service_update(struct service *s, struct blob_attr *config, struct blob_attr **t service_validate_del(s); if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) { - s->trigger = malloc(blob_pad_len(tb[SERVICE_SET_TRIGGER])); + s->trigger = blob_memdup(tb[SERVICE_SET_TRIGGER]); if (!s->trigger) return -1; - memcpy(s->trigger, tb[SERVICE_SET_TRIGGER], blob_pad_len(tb[SERVICE_SET_TRIGGER])); trigger_add(s->trigger, s); } @@ -139,7 +139,7 @@ service_update(struct service *s, struct blob_attr *config, struct blob_attr **t vlist_flush(&s->instances); } - rcnow(s->name, "running"); + rc(s->name, "running"); return 0; } @@ -147,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); @@ -217,20 +217,20 @@ service_handle_set(struct ubus_context *ctx, struct ubus_object *obj, struct blob_attr *tb[__SERVICE_SET_MAX], *cur; struct service *s = NULL; const char *name; - int ret = UBUS_STATUS_INVALID_ARGUMENT; bool add = !strcmp(method, "add"); + int ret; blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg)); cur = tb[SERVICE_ATTR_NAME]; if (!cur) - goto free; + return UBUS_STATUS_INVALID_ARGUMENT; name = blobmsg_data(cur); 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); @@ -238,17 +238,15 @@ 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) - goto free; + return ret; avl_insert(&services, &s->avl); - return 0; + service_event("service.start", s->name, NULL); -free: - free(msg); - return ret; + return 0; } static void @@ -259,15 +257,12 @@ service_dump(struct service *s, int verbose) c = blobmsg_open_table(&b, s->name); - if (avl_is_empty(&s->instances.avl)) { - blobmsg_close_table(&b, c); - return; + if (!avl_is_empty(&s->instances.avl)) { + i = blobmsg_open_table(&b, "instances"); + vlist_for_each_element(&s->instances, in, node) + instance_dump(&b, in, verbose); + blobmsg_close_table(&b, i); } - - i = blobmsg_open_table(&b, "instances"); - vlist_for_each_element(&s->instances, in, node) - instance_dump(&b, in, verbose); - blobmsg_close_table(&b, i); if (verbose && s->trigger) blobmsg_add_blob(&b, s->trigger); if (verbose && !list_empty(&s->validators)) @@ -446,9 +441,9 @@ service_start_early(char *name, char *cmdline) } blobmsg_close_array(&b, command); respawn = blobmsg_open_array(&b, "respawn"); - blobmsg_add_string(&b, NULL, "1"); blobmsg_add_string(&b, NULL, "3600"); - blobmsg_add_string(&b, NULL, "10"); + blobmsg_add_string(&b, NULL, "1"); + blobmsg_add_string(&b, NULL, "0"); blobmsg_close_array(&b, respawn); blobmsg_close_table(&b, instance); blobmsg_close_table(&b, instances); @@ -456,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); }