From aee2d5582e56582ed0dc3096b364630b2ecfd56a Mon Sep 17 00:00:00 2001 From: John Crispin Date: Fri, 29 Aug 2014 14:02:31 +0200 Subject: [PATCH] load service info from procd Signed-off-by: John Crispin --- json/http.json | 2 +- json/ssh.json | 2 +- main.c | 3 +- service.c | 152 ++++++++++++++++++++++++++++++++++++++------------------- ubus.c | 13 +++++ ubus.h | 1 + 6 files changed, 119 insertions(+), 54 deletions(-) diff --git a/json/http.json b/json/http.json index f7e607c..cf60532 100644 --- a/json/http.json +++ b/json/http.json @@ -1,3 +1,3 @@ { - "_http._tcp.local": { "port": 80, "txt": [ "foo", "bar"] }, + "http_80": { "service": "_http._tcp.local", "port": 80, "txt": [ "foo=bar"] }, } diff --git a/json/ssh.json b/json/ssh.json index c61423f..3af62c3 100644 --- a/json/ssh.json +++ b/json/ssh.json @@ -1,3 +1,3 @@ { - "_ssh._tcp.local": { "port": 22, "txt": [ "a=foo", "d=bar"] }, + "ssh_22": { "service": "_ssh._tcp.local", "port": 22, "txt": [ "daemon=dropbear" ] } } diff --git a/main.c b/main.c index 93db2e5..4747eef 100644 --- a/main.c +++ b/main.c @@ -77,9 +77,10 @@ main(int argc, char **argv) if (cache_init()) return -1; + ubus_startup(); + service_init(0); - ubus_startup(); uloop_run(); uloop_done(); diff --git a/service.c b/service.c index ab6227a..71d987b 100644 --- a/service.c +++ b/service.c @@ -24,11 +24,13 @@ #include #include +#include #include #include #include #include +#include "ubus.h" #include "dns.h" #include "service.h" #include "util.h" @@ -250,62 +252,59 @@ service_update(struct vlist_tree *tree, struct vlist_node *node_new, static void service_load_blob(struct blob_attr *b) { - struct blob_attr *txt, *cur, *_tb[__SERVICE_MAX]; - int rem; - - blob_for_each_attr(cur, b, rem) { - struct service *s; - char *d_service, *d_id; - uint8_t *d_txt; - int rem2; - int txt_len = 0; - - blobmsg_parse(service_policy, ARRAY_SIZE(service_policy), - _tb, blobmsg_data(cur), blobmsg_data_len(cur)); - if (!_tb[SERVICE_PORT] || !_tb[SERVICE_SERVICE]) - continue; + struct blob_attr *txt, *_tb[__SERVICE_MAX]; + struct service *s; + char *d_service, *d_id; + uint8_t *d_txt; + int rem2; + int txt_len = 0; + + blobmsg_parse(service_policy, ARRAY_SIZE(service_policy), + _tb, blobmsg_data(b), blobmsg_data_len(b)); + if (!_tb[SERVICE_PORT] || !_tb[SERVICE_SERVICE]) + return; - if (_tb[SERVICE_SERVICE]) - blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) - txt_len += 1 + strlen(blobmsg_get_string(txt)); + if (_tb[SERVICE_SERVICE]) + blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) + txt_len += 1 + strlen(blobmsg_get_string(txt)); - s = calloc_a(sizeof(*s), - &d_id, strlen(blobmsg_name(cur)) + 1, - &d_service, strlen(blobmsg_get_string(_tb[SERVICE_SERVICE])) + 1, - &d_txt, txt_len); - if (!s) - continue; + s = calloc_a(sizeof(*s), + &d_id, strlen(blobmsg_name(b)) + 1, + &d_service, strlen(blobmsg_get_string(_tb[SERVICE_SERVICE])) + 1, + &d_txt, txt_len); + if (!s) + return; - s->port = blobmsg_get_u32(_tb[SERVICE_PORT]); - s->id = strcpy(d_id, blobmsg_name(cur)); - s->service = strcpy(d_service, blobmsg_get_string(_tb[SERVICE_SERVICE])); - s->active = 1; - s->t = 0; - s->txt_len = txt_len; - s->txt = d_txt; - - if (_tb[SERVICE_SERVICE]) - blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) { - int len = strlen(blobmsg_get_string(txt)); - if (!len) - continue; - if (len > 0xff) - len = 0xff; - *d_txt = len; - d_txt++; - memcpy(d_txt, blobmsg_get_string(txt), len); - d_txt += len; - } + s->port = blobmsg_get_u32(_tb[SERVICE_PORT]); + s->id = strcpy(d_id, blobmsg_name(b)); + s->service = strcpy(d_service, blobmsg_get_string(_tb[SERVICE_SERVICE])); + s->active = 1; + s->t = 0; + s->txt_len = txt_len; + s->txt = d_txt; + + if (_tb[SERVICE_SERVICE]) + blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) { + int len = strlen(blobmsg_get_string(txt)); + if (!len) + return; + if (len > 0xff) + len = 0xff; + *d_txt = len; + d_txt++; + memcpy(d_txt, blobmsg_get_string(txt), len); + d_txt += len; + } - vlist_add(&services, &s->node, s->id); - } + vlist_add(&services, &s->node, s->id); } static void service_load(char *path) { + struct blob_attr *cur; glob_t gl; - int i; + int i, rem; if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl)) return; @@ -313,24 +312,75 @@ service_load(char *path) for (i = 0; i < gl.gl_pathc; i++) { blob_buf_init(&b, 0); if (blobmsg_add_json_from_file(&b, gl.gl_pathv[i])) - service_load_blob(b.head); + blob_for_each_attr(cur, b.head, rem) + service_load_blob(cur); } globfree(&gl); } -void -service_init(int announce) +static void +service_init_cb(struct ubus_request *req, int type, struct blob_attr *msg) { - service_init_announce = announce; + struct blob_attr *cur; + int rem; get_hostname(); vlist_update(&services); - service_load("/tmp/run/mdnsd/*"); + service_load("/tmp/run/mdns/*"); + + blob_for_each_attr(cur, msg, rem) { + struct blob_attr *cur2; + int rem2; + + blobmsg_for_each_attr(cur2, cur, rem2) { + struct blob_attr *cur3; + int rem3; + + if (strcmp(blobmsg_name(cur2), "instances")) + continue; + + blobmsg_for_each_attr(cur3, cur2, rem3) { + struct blob_attr *cur4; + int rem4; + int running = 0; + + blobmsg_for_each_attr(cur4, cur3, rem4) { + const char *name = blobmsg_name(cur4); + + if (!strcmp(name, "running")) { + running = blobmsg_get_bool(cur4); + } else if (running && !strcmp(name, "data")) { + struct blob_attr *cur5; + int rem5; + + blobmsg_for_each_attr(cur5, cur4, rem5) { + struct blob_attr *cur6; + int rem6; + + if (strcmp(blobmsg_name(cur5), "mdns")) + continue; + + blobmsg_for_each_attr(cur6, cur5, rem6) + service_load_blob(cur6); + } + break; + } + } + } + } + } vlist_flush(&services); } void +service_init(int announce) +{ + service_init_announce = announce; + ubus_service_list(service_init_cb); +} + +void service_cleanup(void) { vlist_flush(&services); diff --git a/ubus.c b/ubus.c index b9bb9ee..32e3087 100644 --- a/ubus.c +++ b/ubus.c @@ -182,3 +182,16 @@ ubus_startup(void) conn.cb = ubus_connect_handler; ubus_auto_connect(&conn); } + +int ubus_service_list(ubus_data_handler_t cb) +{ + uint32_t id; + int ret; + + blob_buf_init(&b, 0); + ret = ubus_lookup_id(&conn.ctx, "service", &id); + if (ret) + return ret; + + return ubus_invoke(&conn.ctx, id, "list", b.head, cb, NULL, 5 * 1000); +} diff --git a/ubus.h b/ubus.h index 428d166..e880b73 100644 --- a/ubus.h +++ b/ubus.h @@ -15,5 +15,6 @@ #define _UBUS_H__ extern void ubus_startup(void); +extern int ubus_service_list(ubus_data_handler_t cb); #endif -- 2.11.0