From: John Crispin Date: Mon, 1 Jul 2013 16:34:13 +0000 (+0200) Subject: add trigger support X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=3283d681f9da65b500ad0cb3d4550cb840ed481a;p=oweals%2Fprocd.git add trigger support Signed-off-by: John Crispin --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d3a989..b174a15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ IF(APPLE) LINK_DIRECTORIES(/opt/local/lib) ENDIF() -SET(SOURCES main.c ubus.c service.c instance.c utils.c md5.c hotplug.c state.c mkdev.c early.c inittab.c preinit.c coldplug.c syslog.c log.c watchdog.c signal.c system.c debug.c rcS.c) +SET(SOURCES main.c ubus.c service.c instance.c utils.c md5.c hotplug.c state.c mkdev.c early.c inittab.c preinit.c coldplug.c syslog.c log.c watchdog.c signal.c system.c debug.c rcS.c trigger.c) find_library(json NAMES json-c json) SET(LIBS ubox ubus ${json} blobmsg_json json_script) diff --git a/instance.c b/instance.c index a1459b7..b5ecad0 100644 --- a/instance.c +++ b/instance.c @@ -31,6 +31,7 @@ enum { INSTANCE_ATTR_DATA, INSTANCE_ATTR_NETDEV, INSTANCE_ATTR_FILE, + INSTANCE_ATTR_TRIGGER, INSTANCE_ATTR_NICE, __INSTANCE_ATTR_MAX }; @@ -41,6 +42,7 @@ static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = { [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE }, [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY }, [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY }, + [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY }, [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 }, }; @@ -272,7 +274,11 @@ instance_config_parse(struct service_instance *in) return false; in->command = cur; + in->trigger = tb[INSTANCE_ATTR_TRIGGER]; + if (in->trigger) { + trigger_add(in->trigger, in); + } if ((cur = tb[INSTANCE_ATTR_NICE])) { in->nice = (int8_t) blobmsg_get_u32(cur); if (in->nice < -20 || in->nice > 20) @@ -309,6 +315,7 @@ instance_config_move(struct service_instance *in, struct service_instance *in_sr blobmsg_list_move(&in->env, &in_src->env); blobmsg_list_move(&in->data, &in_src->data); blobmsg_list_move(&in->netdev, &in_src->netdev); + in->trigger = in_src->trigger; in->command = in_src->command; in->name = in_src->name; in->node.avl.key = in_src->node.avl.key; @@ -344,6 +351,7 @@ instance_free(struct service_instance *in) { uloop_process_delete(&in->proc); uloop_timeout_cancel(&in->timeout); + trigger_del(in); instance_config_cleanup(in); free(in->config); free(in); @@ -366,7 +374,7 @@ instance_init(struct service_instance *in, struct service *s, struct blob_attr * in->valid = instance_config_parse(in); } -void instance_dump(struct blob_buf *b, struct service_instance *in) +void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose) { void *i; @@ -375,5 +383,7 @@ void instance_dump(struct blob_buf *b, struct service_instance *in) if (in->proc.pending) blobmsg_add_u32(b, "pid", in->proc.pid); blobmsg_add_blob(b, in->command); + if (verbose && in->trigger) + blobmsg_add_blob(b, in->trigger); blobmsg_close_table(b, i); } diff --git a/instance.h b/instance.h index 530041c..ceae834 100644 --- a/instance.h +++ b/instance.h @@ -33,6 +33,7 @@ struct service_instance { struct uloop_timeout timeout; struct blob_attr *command; + struct blob_attr *trigger; struct blobmsg_list env; struct blobmsg_list data; struct blobmsg_list netdev; @@ -44,6 +45,6 @@ void instance_stop(struct service_instance *in, bool restart); bool 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); -void instance_dump(struct blob_buf *b, struct service_instance *in); +void instance_dump(struct blob_buf *b, struct service_instance *in, int debug); #endif diff --git a/main.c b/main.c index 6610c37..a1b4fdb 100644 --- a/main.c +++ b/main.c @@ -70,6 +70,7 @@ int main(int argc, char **argv) } uloop_init(); procd_signal(); + trigger_init(); if (getpid() != 1) procd_connect_ubus(); else diff --git a/procd.h b/procd.h index 7f6c7cb..4fd45f2 100644 --- a/procd.h +++ b/procd.h @@ -69,4 +69,10 @@ void procd_inittab_run(const char *action); int mkdev(const char *progname, int progmode); +struct trigger; +void trigger_init(void); +void trigger_event(char *type, struct blob_attr *data); +void trigger_add(struct blob_attr *rule, void *id); +void trigger_del(void *id); + #endif diff --git a/service.c b/service.c index 8083e93..7db1c4a 100644 --- a/service.c +++ b/service.c @@ -83,6 +83,7 @@ enum { SERVICE_SET_NAME, SERVICE_SET_SCRIPT, SERVICE_SET_INSTANCES, + SERVICE_SET_TRIGGER, __SERVICE_SET_MAX }; @@ -90,15 +91,17 @@ static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = { [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING }, [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING }, [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE }, + [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY }, }; - static int service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb, bool add) { struct blob_attr *cur; int rem; + s->trigger = tb[SERVICE_SET_TRIGGER]; + if (tb[SERVICE_SET_INSTANCES]) { if (!add) vlist_update(&s->instances); @@ -141,6 +144,25 @@ static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = { [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING }, }; +enum { + SERVICE_LIST_ATTR_VERBOSE, + __SERVICE_LIST_ATTR_MAX, +}; + +static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = { + [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_INT32 }, +}; + +enum { + EVENT_TYPE, + EVENT_DATA, + __EVENT_MAX +}; + +static const struct blobmsg_policy event_policy[__EVENT_MAX] = { + [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING }, + [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE }, +}; static int service_handle_set(struct ubus_context *ctx, struct ubus_object *obj, @@ -185,15 +207,17 @@ free: } static void -service_dump(struct service *s) +service_dump(struct service *s, int verbose) { struct service_instance *in; void *c, *i; c = blobmsg_open_table(&b, s->name); i = blobmsg_open_table(&b, "instances"); + if (verbose && s->trigger) + blobmsg_add_blob(&b, s->trigger); vlist_for_each_element(&s->instances, in, node) - instance_dump(&b, in); + instance_dump(&b, in, verbose); blobmsg_close_table(&b, i); blobmsg_close_table(&b, c); } @@ -203,11 +227,18 @@ service_handle_list(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_LIST_ATTR_MAX]; struct service *s; + int verbose = 0; + + blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg)); + + if (tb[SERVICE_LIST_ATTR_VERBOSE] && blobmsg_get_u32(tb[SERVICE_LIST_ATTR_VERBOSE])) + verbose = 1; blob_buf_init(&b, 0); avl_for_each_element(&services, s, avl) - service_dump(s); + service_dump(s, verbose); ubus_send_reply(ctx, req, b.head); @@ -279,6 +310,52 @@ service_handle_update(struct ubus_context *ctx, struct ubus_object *obj, return 0; } +static int +service_handle_event(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct blob_attr *tb[__EVENT_MAX]; + + if (!msg) + return UBUS_STATUS_INVALID_ARGUMENT; + + blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg)); + if (!tb[EVENT_TYPE] || !tb[EVENT_DATA]) + return UBUS_STATUS_INVALID_ARGUMENT; + + trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]); + + return 0; +} + +enum { + TRIGGER_ATTR, + __TRIGGER_MAX +}; + +static const struct blobmsg_policy trigger_policy[__TRIGGER_MAX] = { + [TRIGGER_ATTR] = { .name = "triggers", .type = BLOBMSG_TYPE_ARRAY }, +}; + +static int service_handle_trigger(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct blob_attr *tb[__TRIGGER_MAX]; + + if (!msg) + return UBUS_STATUS_INVALID_ARGUMENT; + + blobmsg_parse(trigger_policy, __TRIGGER_MAX, tb, blob_data(msg), blob_len(msg)); + if (!tb[TRIGGER_ATTR]) + return UBUS_STATUS_INVALID_ARGUMENT; + + trigger_add(tb[TRIGGER_ATTR], NULL); + + 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), @@ -286,6 +363,8 @@ static struct ubus_method main_object_methods[] = { 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("trigger", service_handle_trigger, trigger_policy), }; static struct ubus_object_type main_object_type = diff --git a/service.h b/service.h index 4e68eed..64c8a2f 100644 --- a/service.h +++ b/service.h @@ -25,6 +25,7 @@ struct service { const char *name; struct blob_attr *config; + struct blob_attr *trigger; struct vlist_tree instances; }; diff --git a/system.c b/system.c index e1f4a64..c96cb63 100644 --- a/system.c +++ b/system.c @@ -259,41 +259,11 @@ static int watchdog_set(struct ubus_context *ctx, struct ubus_object *obj, return 0; } -enum { - EVENT_TYPE, - EVENT_DATA, - __EVENT_MAX -}; - -static const struct blobmsg_policy event_policy[__WDT_MAX] = { - [EVENT_TYPE] = { .name = "frequency", .type = BLOBMSG_TYPE_INT32 }, - [EVENT_DATA] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 }, -}; - -static int system_event(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) -{ - struct blob_attr *tb[__EVENT_MAX]; - - if (!msg) - return UBUS_STATUS_INVALID_ARGUMENT; - - blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg)); - if (!tb[EVENT_TYPE]) - return UBUS_STATUS_INVALID_ARGUMENT; - - fprintf(stderr, "%s\n", blobmsg_get_string(tb[EVENT_TYPE])); - - return 0; -} - static const struct ubus_method system_methods[] = { UBUS_METHOD_NOARG("board", system_board), UBUS_METHOD_NOARG("info", system_info), UBUS_METHOD_NOARG("upgrade", system_upgrade), UBUS_METHOD("watchdog", watchdog_set, watchdog_policy), - UBUS_METHOD("event", system_event, event_policy), }; static struct ubus_object_type system_object_type = diff --git a/trigger.c b/trigger.c new file mode 100644 index 0000000..fff1855 --- /dev/null +++ b/trigger.c @@ -0,0 +1,334 @@ +/* + * 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 +#include + +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "procd.h" + +struct trigger { + struct list_head list; + + char *type; + + int pending; + int remove; + int timeout; + + void *id; + + struct blob_attr *rule; + struct blob_attr *data; + struct uloop_timeout delay; + + struct json_script_ctx jctx; +}; + +struct job; +struct cmd { + char *name; + void (*handler)(struct job *job, struct blob_attr *exec, struct blob_attr *env); +}; + +struct job { + struct runqueue_process proc; + struct cmd *cmd; + struct trigger *trigger; + struct blob_attr *exec; + struct blob_attr *env; +}; + +static LIST_HEAD(triggers); +static struct runqueue q; + +static const char* rule_handle_var(struct json_script_ctx *ctx, const char *name, struct blob_attr *vars) +{ + return NULL; +} + +static struct json_script_file * +rule_load_script(struct json_script_ctx *ctx, const char *name) +{ + struct trigger *t = container_of(ctx, struct trigger, jctx); + + return json_script_file_from_blobmsg(t->type, t->rule, blob_pad_len(t->rule)); +} + +static void q_job_run(struct runqueue *q, struct runqueue_task *t) +{ + struct job *j = container_of(t, struct job, proc.task); + + LOG("handle event %s\n", j->cmd->name); + j->cmd->handler(j, j->exec, j->env); +} + +static void q_job_complete(struct runqueue *q, struct runqueue_task *p) +{ + struct job *j = container_of(p, struct job, proc.task); + + if (j->trigger->remove) { + list_del(&j->trigger->list); + free(&j->trigger); + } else { + j->trigger->pending = 0; + } + free(j); +} + +static void add_job(struct trigger *t, struct cmd *cmd, struct blob_attr *exec, struct blob_attr *data) +{ + static const struct runqueue_task_type job_type = { + .run = q_job_run, + .cancel = runqueue_process_cancel_cb, + .kill = runqueue_process_kill_cb, + }; + struct blob_attr *d, *e; + struct job *j = calloc_a(sizeof(*j), &e, blob_pad_len(exec), &d, blob_pad_len(data)); + + j->env = d; + j->exec = e; + j->cmd = cmd; + j->trigger = t; + j->proc.task.type = &job_type; + j->proc.task.complete = q_job_complete; + t->pending = 1; + + memcpy(j->exec, exec, blob_pad_len(exec)); + memcpy(j->env, data, blob_pad_len(data)); + + runqueue_task_add(&q, &j->proc.task, false); +} + +static void _setenv(const char *key, const char *val) +{ + char _key[32]; + + snprintf(_key, sizeof(_key), "PARAM_%s", key); + setenv(_key, val, 1); +} + +static void handle_run_script(struct job *j, struct blob_attr *exec, struct blob_attr *env) +{ + char *argv[8]; + struct blob_attr *cur; + int rem; + int i = 0; + pid_t pid; + + pid = fork(); + if (pid < 0) + return; + + if (pid) { + runqueue_process_add(&q, &j->proc, pid); + return; + } + + if (debug < 2) { + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + } + + _setenv("type", j->trigger->type); + blobmsg_for_each_attr(cur, j->env, rem) + _setenv(blobmsg_name(cur), blobmsg_data(cur)); + + blobmsg_for_each_attr(cur, j->exec, rem) { + argv[i] = blobmsg_data(cur); + i++; + if (i == 7) + break; + } + + if (i > 0) { + argv[i] = NULL; + execvp(argv[0], &argv[0]); + } + + exit(1); +} + +static struct cmd handlers[] = { + { + .name = "run_script", + .handler = handle_run_script, + }, +}; + +static void rule_handle_command(struct json_script_ctx *ctx, const char *name, + struct blob_attr *exec, struct blob_attr *vars) +{ + struct trigger *t = container_of(ctx, struct trigger, jctx); + int i; + + if (t->pending) + return; + + for (i = 0; i < ARRAY_SIZE(handlers); i++) { + if (!strcmp(handlers[i].name, name)) { + add_job(t, &handlers[i], exec, vars); + break; + } + } +} + +static void rule_handle_error(struct json_script_ctx *ctx, const char *msg, + struct blob_attr *context) +{ + char *s; + + s = blobmsg_format_json(context, false); + ERROR("ERROR: %s in block: %s\n", msg, s); + free(s); +} + +static void q_empty(struct runqueue *q) +{ +} + +static void trigger_delay_cb(struct uloop_timeout *tout) +{ + struct trigger *t = container_of(tout, struct trigger, delay); + + json_script_run(&t->jctx, "foo", t->data); + free(t->data); +} + +static struct trigger* _trigger_add(char *type, struct blob_attr *rule, int timeout, void *id) +{ + char *_t; + struct blob_attr *_r; + struct trigger *t = calloc_a(sizeof(*t), &_t, strlen(type) + 1, &_r, blob_pad_len(rule)); + + t->type = _t; + t->rule = _r; + t->delay.cb = trigger_delay_cb; + t->timeout = timeout; + t->pending = 0; + t->remove = 0; + t->id = id; + t->jctx.handle_var = rule_handle_var, + t->jctx.handle_error = rule_handle_error, + t->jctx.handle_command = rule_handle_command, + t->jctx.handle_file = rule_load_script, + + strcpy(t->type, type); + memcpy(t->rule, rule, blob_pad_len(rule)); + + list_add(&t->list, &triggers); + json_script_init(&t->jctx); + + return t; +} + +void trigger_add(struct blob_attr *rule, void *id) +{ + struct blob_attr *cur; + int rem; + + blobmsg_for_each_attr(cur, rule, rem) { + struct blob_attr *_cur, *type = NULL, *script = NULL, *timeout = NULL; + int _rem; + int i = 0; + + if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY) + continue; + + blobmsg_for_each_attr(_cur, cur, _rem) { + switch (i++) { + case 0: + if (blobmsg_type(_cur) == BLOBMSG_TYPE_STRING) + type = _cur; + break; + + case 1: + if (blobmsg_type(_cur) == BLOBMSG_TYPE_ARRAY) + script = _cur; + break; + + case 2: + if (blobmsg_type(_cur) == BLOBMSG_TYPE_INT32) + timeout = _cur; + break; + } + } + + if (type && script) { + int t = 0; + + if (timeout) + t = blobmsg_get_u32(timeout); + _trigger_add(blobmsg_get_string(type), script, t, id); + } + } +} + +void trigger_del(void *id) +{ + struct trigger *t, *n; + + list_for_each_entry_safe(t, n, &triggers, list) { + if (t->id != id) + continue; + + if (t->pending) { + t->remove = 1; + continue; + } + list_del(&t->list); + free(t); + } +} + +void trigger_init(void) +{ + runqueue_init(&q); + q.empty_cb = q_empty; + q.max_running_tasks = 1; +} + +void trigger_event(char *type, struct blob_attr *data) +{ + struct trigger *t; + + list_for_each_entry(t, &triggers, list) { + if (t->pending || t->remove) + continue; + if (!strcmp(t->type, type)) { + if (t->timeout) { + t->data = malloc(blob_pad_len(data)); + memcpy(t->data, data, blob_pad_len(data)); + uloop_timeout_set(&t->delay, t->timeout); + } else { + json_script_run(&t->jctx, "foo", data); + } + } + } +}