ubus.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 <sys/resource.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <signal.h>
  18. #include "procd.h"
  19. char *ubus_socket = NULL;
  20. static struct ubus_context *ctx;
  21. static struct uloop_timeout ubus_timer;
  22. static int timeout;
  23. static struct udebug_ubus udebug;
  24. static void
  25. procd_udebug_cb(struct udebug_ubus *ctx, struct blob_attr *data, bool enabled)
  26. {
  27. procd_udebug_set_enabled(enabled);
  28. }
  29. static void reset_timeout(void)
  30. {
  31. timeout = 50;
  32. }
  33. static void timeout_retry(void)
  34. {
  35. uloop_timeout_set(&ubus_timer, timeout);
  36. timeout *= 2;
  37. if (timeout > 1000)
  38. timeout = 1000;
  39. }
  40. static void
  41. ubus_reconnect_cb(struct uloop_timeout *timeout)
  42. {
  43. if (!ubus_reconnect(ctx, ubus_socket)) {
  44. ubus_add_uloop(ctx);
  45. return;
  46. }
  47. timeout_retry();
  48. }
  49. static void
  50. ubus_disconnect_cb(struct ubus_context *ctx)
  51. {
  52. ubus_timer.cb = ubus_reconnect_cb;
  53. reset_timeout();
  54. timeout_retry();
  55. }
  56. static void
  57. ubus_connect_cb(struct uloop_timeout *timeout)
  58. {
  59. ctx = ubus_connect(ubus_socket);
  60. if (!ctx) {
  61. DEBUG(4, "Connection to ubus failed\n");
  62. timeout_retry();
  63. return;
  64. }
  65. udebug_ubus_init(&udebug, ctx, "procd", procd_udebug_cb);
  66. ctx->connection_lost = ubus_disconnect_cb;
  67. ubus_init_hotplug(ctx);
  68. ubus_init_service(ctx);
  69. ubus_init_system(ctx);
  70. watch_ubus(ctx);
  71. DEBUG(2, "Connected to ubus, id=%08x\n", ctx->local_id);
  72. reset_timeout();
  73. ubus_add_uloop(ctx);
  74. procd_state_ubus_connect();
  75. }
  76. void
  77. procd_connect_ubus(void)
  78. {
  79. ubus_timer.cb = ubus_connect_cb;
  80. reset_timeout();
  81. timeout_retry();
  82. }