ubusd_monitor.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2015 Felix Fietkau <nbd@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include "ubusd.h"
  14. static struct ubus_object *monitor_obj;
  15. static LIST_HEAD(monitors);
  16. struct ubus_monitor {
  17. struct list_head list;
  18. struct ubus_client *cl;
  19. uint32_t seq;
  20. };
  21. static void
  22. ubusd_monitor_free(struct ubus_monitor *m)
  23. {
  24. list_del(&m->list);
  25. free(m);
  26. }
  27. static void
  28. ubusd_monitor_connect(struct ubus_client *cl, struct ubus_msg_buf *ub)
  29. {
  30. struct ubus_monitor *m;
  31. ubusd_monitor_disconnect(cl);
  32. m = calloc(1, sizeof(*m));
  33. m->cl = cl;
  34. list_add(&m->list, &monitors);
  35. }
  36. void
  37. ubusd_monitor_disconnect(struct ubus_client *cl)
  38. {
  39. struct ubus_monitor *m;
  40. list_for_each_entry(m, &monitors, list) {
  41. if (m->cl != cl)
  42. continue;
  43. ubusd_monitor_free(m);
  44. return;
  45. }
  46. }
  47. void
  48. ubusd_monitor_message(struct ubus_client *cl, struct ubus_msg_buf *ub, bool send)
  49. {
  50. static struct blob_buf mb;
  51. struct ubus_monitor *m;
  52. if (list_empty(&monitors))
  53. return;
  54. blob_buf_init(&mb, 0);
  55. blob_put_int32(&mb, UBUS_MONITOR_CLIENT, cl->id.id);
  56. blob_put_int32(&mb, UBUS_MONITOR_PEER, ub->hdr.peer);
  57. blob_put_int32(&mb, UBUS_MONITOR_SEQ, ub->hdr.seq);
  58. blob_put_int32(&mb, UBUS_MONITOR_TYPE, ub->hdr.type);
  59. blob_put_int8(&mb, UBUS_MONITOR_SEND, send);
  60. blob_put(&mb, UBUS_MONITOR_DATA, blob_data(ub->data), blob_len(ub->data));
  61. ub = ubus_msg_new(mb.head, blob_raw_len(mb.head), true);
  62. ub->hdr.type = UBUS_MSG_MONITOR;
  63. list_for_each_entry(m, &monitors, list) {
  64. ub->hdr.seq = ++m->seq;
  65. ubus_msg_send(m->cl, ub);
  66. }
  67. ubus_msg_free(ub);
  68. }
  69. static int
  70. ubusd_monitor_recv(struct ubus_client *cl, struct ubus_msg_buf *ub,
  71. const char *method, struct blob_attr *msg)
  72. {
  73. /* Only root is allowed for now */
  74. if (cl->uid != 0 || cl->gid != 0)
  75. return UBUS_STATUS_PERMISSION_DENIED;
  76. if (!strcmp(method, "add")) {
  77. ubusd_monitor_connect(cl, ub);
  78. return 0;
  79. }
  80. if (!strcmp(method, "remove")) {
  81. ubusd_monitor_disconnect(cl);
  82. return 0;
  83. }
  84. return UBUS_STATUS_METHOD_NOT_FOUND;
  85. }
  86. void
  87. ubusd_monitor_init(void)
  88. {
  89. monitor_obj = ubusd_create_object_internal(NULL, UBUS_SYSTEM_OBJECT_MONITOR);
  90. if (monitor_obj != NULL)
  91. monitor_obj->recv_msg = ubusd_monitor_recv;
  92. }