ubusd_monitor.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 bool
  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. if (!m)
  34. return false;
  35. m->cl = cl;
  36. list_add_tail(&m->list, &monitors);
  37. return true;
  38. }
  39. static struct ubus_monitor*
  40. ubusd_monitor_find(struct ubus_client *cl)
  41. {
  42. struct ubus_monitor *m, *tmp;
  43. list_for_each_entry_safe(m, tmp, &monitors, list) {
  44. if (m->cl != cl)
  45. continue;
  46. return m;
  47. }
  48. return NULL;
  49. }
  50. void
  51. ubusd_monitor_disconnect(struct ubus_client *cl)
  52. {
  53. struct ubus_monitor *m;
  54. m = ubusd_monitor_find(cl);
  55. if (!m)
  56. return;
  57. ubusd_monitor_free(m);
  58. }
  59. void
  60. ubusd_monitor_message(struct ubus_client *cl, struct ubus_msg_buf *ub, bool send)
  61. {
  62. static struct blob_buf mb;
  63. struct ubus_monitor *m;
  64. if (list_empty(&monitors))
  65. return;
  66. blob_buf_init(&mb, 0);
  67. blob_put_int32(&mb, UBUS_MONITOR_CLIENT, cl->id.id);
  68. blob_put_int32(&mb, UBUS_MONITOR_PEER, ub->hdr.peer);
  69. blob_put_int32(&mb, UBUS_MONITOR_SEQ, ub->hdr.seq);
  70. blob_put_int32(&mb, UBUS_MONITOR_TYPE, ub->hdr.type);
  71. blob_put_int8(&mb, UBUS_MONITOR_SEND, send);
  72. blob_put(&mb, UBUS_MONITOR_DATA, blob_data(ub->data), blob_len(ub->data));
  73. ub = ubus_msg_new(mb.head, blob_raw_len(mb.head), true);
  74. ub->hdr.type = UBUS_MSG_MONITOR;
  75. list_for_each_entry(m, &monitors, list) {
  76. ub->hdr.seq = ++m->seq;
  77. ubus_msg_send(m->cl, ub);
  78. }
  79. ubus_msg_free(ub);
  80. }
  81. static int
  82. ubusd_monitor_recv(struct ubus_client *cl, struct ubus_msg_buf *ub,
  83. const char *method, struct blob_attr *msg)
  84. {
  85. /* Only root is allowed for now */
  86. if (cl->uid != 0 || cl->gid != 0)
  87. return UBUS_STATUS_PERMISSION_DENIED;
  88. if (!strcmp(method, "add")) {
  89. if (!ubusd_monitor_connect(cl, ub))
  90. return UBUS_STATUS_UNKNOWN_ERROR;
  91. return UBUS_STATUS_OK;
  92. }
  93. if (!strcmp(method, "remove")) {
  94. ubusd_monitor_disconnect(cl);
  95. return UBUS_STATUS_OK;
  96. }
  97. return UBUS_STATUS_METHOD_NOT_FOUND;
  98. }
  99. void
  100. ubusd_monitor_init(void)
  101. {
  102. monitor_obj = ubusd_create_object_internal(NULL, UBUS_SYSTEM_OBJECT_MONITOR);
  103. if (monitor_obj != NULL)
  104. monitor_obj->recv_msg = ubusd_monitor_recv;
  105. }