• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/procd/service/watch.c

  1 /*
  2  * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  3  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
  4  *
  5  * This program is free software; you can redistribute it and/or modify
  6  * it under the terms of the GNU Lesser General Public License version 2.1
  7  * as published by the Free Software Foundation
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  * GNU General Public License for more details.
 13  */
 14 
 15 #include <stdlib.h>
 16 #include <unistd.h>
 17 
 18 #include <libubox/blobmsg_json.h>
 19 
 20 #include "../procd.h"
 21 
 22 struct watch_object {
 23         struct list_head list;
 24 
 25         void *id;
 26         char *name;
 27 };
 28 
 29 static struct ubus_event_handler watch_event;
 30 static struct ubus_subscriber watch_subscribe;
 31 static LIST_HEAD(watch_objects);
 32 
 33 static void watch_subscribe_cb(struct ubus_context *ctx, struct ubus_event_handler *ev,
 34                 const char *type, struct blob_attr *msg)
 35 {
 36         static const struct blobmsg_policy policy = {
 37                 "path", BLOBMSG_TYPE_STRING
 38         };
 39         struct watch_object *o;
 40         struct blob_attr *attr;
 41         const char *path;
 42 
 43         DEBUG(3, "ubus event %s\n", type);
 44         if (strcmp(type, "ubus.object.add") != 0)
 45                 return;
 46 
 47         blobmsg_parse(&policy, 1, &attr, blob_data(msg), blob_len(msg));
 48         if (!attr)
 49                 return;
 50 
 51         path = blobmsg_data(attr);
 52         DEBUG(3, "ubus path %s\n", path);
 53 
 54         list_for_each_entry(o, &watch_objects, list) {
 55                 unsigned int id;
 56 
 57                 if (strcmp(o->name, path))
 58                         continue;
 59                 if (ubus_lookup_id(ctx, path, &id))
 60                         continue;
 61                 if (!ubus_subscribe(ctx, &watch_subscribe, id))
 62                         return;
 63                 ERROR("failed to subscribe %d\n", id);
 64         }
 65 }
 66 
 67 void
 68 watch_add(const char *_name, void *id)
 69 {
 70         int len = strlen(_name);
 71         char *name;
 72         struct watch_object *o = calloc_a(sizeof(*o), &name, len + 1);
 73 
 74         o->name = name;
 75         strcpy(name, _name);
 76         o->id = id;
 77         list_add(&o->list, &watch_objects);
 78 }
 79 
 80 void
 81 watch_del(void *id)
 82 {
 83         struct watch_object *t, *n;
 84 
 85         list_for_each_entry_safe(t, n, &watch_objects, list) {
 86                 if (t->id != id)
 87                         continue;
 88                 list_del(&t->list);
 89                 free(t);
 90         }
 91 }
 92 
 93 static int
 94 watch_notify_cb(struct ubus_context *ctx, struct ubus_object *obj,
 95                 struct ubus_request_data *req, const char *method,
 96                 struct blob_attr *msg)
 97 {
 98         if (debug >= 3) {
 99                 char *str;
100 
101                 str = blobmsg_format_json(msg, true);
102                 DEBUG(3, "Received ubus notify '%s': %s\n", method, str);
103                 free(str);
104         }
105 
106         trigger_event(method, msg);
107         return 0;
108 }
109 
110 void
111 watch_ubus(struct ubus_context *ctx)
112 {
113         watch_event.cb = watch_subscribe_cb;
114         watch_subscribe.cb = watch_notify_cb;
115         if (ubus_register_subscriber(ctx, &watch_subscribe))
116                 ERROR("failed to register ubus subscriber\n");
117         if (ubus_register_event_handler(ctx, &watch_event, "ubus.object.add"))
118                 ERROR("failed to add ubus event handler\n");
119 }
120 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt