load service info from procd
authorJohn Crispin <blogic@openwrt.org>
Fri, 29 Aug 2014 12:02:31 +0000 (14:02 +0200)
committerJohn Crispin <blogic@openwrt.org>
Fri, 29 Aug 2014 13:20:53 +0000 (15:20 +0200)
Signed-off-by: John Crispin <blogic@openwrt.org>
json/http.json
json/ssh.json
main.c
service.c
ubus.c
ubus.h

index f7e607c674936138ce5ecc6f182ccd6054a2deaa..cf60532e2de4f9f3635ea7e0e3d58c7c0ef18235 100644 (file)
@@ -1,3 +1,3 @@
 {
-       "_http._tcp.local": { "port": 80, "txt": [ "foo", "bar"] },
+       "http_80": { "service": "_http._tcp.local", "port": 80, "txt": [ "foo=bar"] },
 }
index c61423fc725d5bbac2ddd65c26598548f4a4c97f..3af62c34a0cf85d87300a94a7b0080421258a252 100644 (file)
@@ -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 93db2e5c0398dbcb5a5b270fa0377c7eb2d6f54f..4747eef5e60eda57126d8ffd3528e5bb84d1c562 100644 (file)
--- 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();
 
index ab6227acbb594665613fa34acbb2d63848441d08..71d987b2c5e67c104d41cf1dae584305a77a64a3 100644 (file)
--- a/service.c
+++ b/service.c
 #include <uci.h>
 #include <uci_blob.h>
 
+#include <libubus.h>
 #include <libubox/vlist.h>
 #include <libubox/uloop.h>
 #include <libubox/avl-cmp.h>
 #include <libubox/blobmsg_json.h>
 
+#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,23 +312,74 @@ 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)
 {
diff --git a/ubus.c b/ubus.c
index b9bb9ee229bca52745fbc4029bbfd136471e10dc..32e30876ed5c276ee4962e68679ef277b39316bf 100644 (file)
--- 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 428d16640848ae5a6b41a1d97a8e74a740ec95f7..e880b7365f6aeb90d2fa11d5c7daec3a076b4884 100644 (file)
--- 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