watch.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <libubox/blobmsg_json.h>
  17. #include "../procd.h"
  18. struct watch_object {
  19. struct list_head list;
  20. void *id;
  21. char *name;
  22. };
  23. static struct ubus_event_handler watch_event;
  24. static struct ubus_subscriber watch_subscribe;
  25. static LIST_HEAD(watch_objects);
  26. static void watch_subscribe_cb(struct ubus_context *ctx, struct ubus_event_handler *ev,
  27. const char *type, struct blob_attr *msg)
  28. {
  29. static const struct blobmsg_policy policy = {
  30. "path", BLOBMSG_TYPE_STRING
  31. };
  32. struct watch_object *o;
  33. struct blob_attr *attr;
  34. const char *path;
  35. DEBUG(3, "ubus event %s\n", type);
  36. if (strcmp(type, "ubus.object.add") != 0)
  37. return;
  38. blobmsg_parse(&policy, 1, &attr, blob_data(msg), blob_len(msg));
  39. if (!attr)
  40. return;
  41. path = blobmsg_data(attr);
  42. DEBUG(3, "ubus path %s\n", path);
  43. list_for_each_entry(o, &watch_objects, list) {
  44. unsigned int id;
  45. if (strcmp(o->name, path))
  46. continue;
  47. if (ubus_lookup_id(ctx, path, &id))
  48. continue;
  49. if (!ubus_subscribe(ctx, &watch_subscribe, id))
  50. return;
  51. ERROR("failed to subscribe %d\n", id);
  52. }
  53. }
  54. void
  55. watch_add(const char *_name, void *id)
  56. {
  57. int len = strlen(_name);
  58. char *name;
  59. struct watch_object *o = calloc_a(sizeof(*o), &name, len + 1);
  60. o->name = name;
  61. strcpy(name, _name);
  62. o->id = id;
  63. list_add(&o->list, &watch_objects);
  64. }
  65. void
  66. watch_del(void *id)
  67. {
  68. struct watch_object *t, *n;
  69. list_for_each_entry_safe(t, n, &watch_objects, list) {
  70. if (t->id != id)
  71. continue;
  72. list_del(&t->list);
  73. free(t);
  74. }
  75. }
  76. static int
  77. watch_notify_cb(struct ubus_context *ctx, struct ubus_object *obj,
  78. struct ubus_request_data *req, const char *method,
  79. struct blob_attr *msg)
  80. {
  81. if (debug >= 3) {
  82. char *str;
  83. str = blobmsg_format_json(msg, true);
  84. DEBUG(3, "Received ubus notify '%s': %s\n", method, str);
  85. free(str);
  86. }
  87. trigger_event(method, msg);
  88. return 0;
  89. }
  90. void
  91. watch_ubus(struct ubus_context *ctx)
  92. {
  93. watch_event.cb = watch_subscribe_cb;
  94. watch_subscribe.cb = watch_notify_cb;
  95. if (ubus_register_subscriber(ctx, &watch_subscribe))
  96. ERROR("failed to register ubus subscriber\n");
  97. if (ubus_register_event_handler(ctx, &watch_event, "ubus.object.add"))
  98. ERROR("failed to add ubus event handler\n");
  99. }