Skip to content

Commit

Permalink
add trigger support
Browse files Browse the repository at this point in the history
Signed-off-by: John Crispin <blogic@openwrt.org>
  • Loading branch information
John Crispin committed Jul 4, 2013
1 parent 4ec2545 commit 3283d68
Show file tree
Hide file tree
Showing 9 changed files with 439 additions and 37 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion instance.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ enum {
INSTANCE_ATTR_DATA,
INSTANCE_ATTR_NETDEV,
INSTANCE_ATTR_FILE,
INSTANCE_ATTR_TRIGGER,
INSTANCE_ATTR_NICE,
__INSTANCE_ATTR_MAX
};
Expand All @@ -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 },
};

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;

Expand All @@ -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);
}
3 changes: 2 additions & 1 deletion instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
1 change: 1 addition & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ int main(int argc, char **argv)
}
uloop_init();
procd_signal();
trigger_init();
if (getpid() != 1)
procd_connect_ubus();
else
Expand Down
6 changes: 6 additions & 0 deletions procd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
87 changes: 83 additions & 4 deletions service.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,25 @@ enum {
SERVICE_SET_NAME,
SERVICE_SET_SCRIPT,
SERVICE_SET_INSTANCES,
SERVICE_SET_TRIGGER,
__SERVICE_SET_MAX
};

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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -185,15 +207,17 @@ service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
}

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);
}
Expand All @@ -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);

Expand Down Expand Up @@ -279,13 +310,61 @@ 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),
UBUS_METHOD("list", service_handle_list, 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),
UBUS_METHOD("event", service_handle_event, event_policy),
UBUS_METHOD("trigger", service_handle_trigger, trigger_policy),
};

static struct ubus_object_type main_object_type =
Expand Down
1 change: 1 addition & 0 deletions service.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct service {
const char *name;

struct blob_attr *config;
struct blob_attr *trigger;
struct vlist_tree instances;
};

Expand Down
30 changes: 0 additions & 30 deletions system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Loading

0 comments on commit 3283d68

Please sign in to comment.