From e0098d49a95d6f33252c9eb2f98e963c3fffe50b Mon Sep 17 00:00:00 2001 From: John Crispin Date: Wed, 5 Apr 2017 08:58:22 +0200 Subject: [PATCH] service/instance: add an auto start option this allows us to register services with procd but not auto start them. An additional ubus call is required to start the service. Signed-off-by: John Crispin --- service/instance.c | 4 +-- service/instance.h | 2 +- service/service.c | 61 ++++++++++++++++++++++++++++++++++++++++++++-- service/service.h | 1 + 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/service/instance.c b/service/instance.c index 4d340fd..677f8eb 100644 --- a/service/instance.c +++ b/service/instance.c @@ -563,11 +563,11 @@ instance_exit(struct uloop_process *p, int ret) } void -instance_stop(struct service_instance *in) +instance_stop(struct service_instance *in, bool halt) { if (!in->proc.pending) return; - in->halt = true; + in->halt = halt; in->restart = in->respawn = false; kill(in->proc.pid, SIGTERM); uloop_timeout_set(&in->timeout, in->term_timeout * 1000); diff --git a/service/instance.h b/service/instance.h index 78999c8..bdd14de 100644 --- a/service/instance.h +++ b/service/instance.h @@ -81,7 +81,7 @@ struct service_instance { }; void instance_start(struct service_instance *in); -void instance_stop(struct service_instance *in); +void instance_stop(struct service_instance *in, bool halt); void instance_update(struct service_instance *in, struct service_instance *in_new); void instance_init(struct service_instance *in, struct service *s, struct blob_attr *config); void instance_free(struct service_instance *in); diff --git a/service/service.c b/service/service.c index 9675ba2..6ba0c17 100644 --- a/service/service.c +++ b/service/service.c @@ -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_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); } @@ -93,6 +93,7 @@ enum { SERVICE_SET_INSTANCES, SERVICE_SET_TRIGGER, SERVICE_SET_VALIDATE, + SERVICE_SET_AUTOSTART, __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_AUTOSTART] = { "autostart", BLOBMSG_TYPE_BOOL }, }; static int @@ -118,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) @@ -199,6 +206,17 @@ static const struct blobmsg_policy service_signal_attrs[__SERVICE_SIGNAL_ATTR_MA [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, @@ -284,6 +302,9 @@ service_dump(struct service *s, bool verbose) 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) @@ -420,6 +441,41 @@ service_handle_signal(struct ubus_context *ctx, struct ubus_object *obj, 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, @@ -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("state", service_handle_state, service_state_attrs), }; static struct ubus_object_type main_object_type = diff --git a/service/service.h b/service/service.h index cc629b1..a433c9f 100644 --- a/service/service.h +++ b/service/service.h @@ -41,6 +41,7 @@ struct service { struct avl_node avl; const char *name; bool deleted; + bool autostart; struct blob_attr *trigger; struct vlist_tree instances; -- 2.25.1