ubus.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 void reset_timeout(void)
  24. {
  25. timeout = 50;
  26. }
  27. static void timeout_retry(void)
  28. {
  29. uloop_timeout_set(&ubus_timer, timeout);
  30. timeout *= 2;
  31. if (timeout > 1000)
  32. timeout = 1000;
  33. }
  34. static void
  35. ubus_reconnect_cb(struct uloop_timeout *timeout)
  36. {
  37. if (!ubus_reconnect(ctx, ubus_socket)) {
  38. ubus_add_uloop(ctx);
  39. return;
  40. }
  41. timeout_retry();
  42. }
  43. static void
  44. ubus_disconnect_cb(struct ubus_context *ctx)
  45. {
  46. ubus_timer.cb = ubus_reconnect_cb;
  47. reset_timeout();
  48. timeout_retry();
  49. }
  50. static void
  51. ubus_connect_cb(struct uloop_timeout *timeout)
  52. {
  53. ctx = ubus_connect(ubus_socket);
  54. if (!ctx) {
  55. DEBUG(4, "Connection to ubus failed\n");
  56. timeout_retry();
  57. return;
  58. }
  59. ctx->connection_lost = ubus_disconnect_cb;
  60. ubus_init_service(ctx);
  61. ubus_init_system(ctx);
  62. watch_ubus(ctx);
  63. DEBUG(2, "Connected to ubus, id=%08x\n", ctx->local_id);
  64. reset_timeout();
  65. ubus_add_uloop(ctx);
  66. procd_state_ubus_connect();
  67. }
  68. void
  69. procd_connect_ubus(void)
  70. {
  71. ubus_timer.cb = ubus_connect_cb;
  72. reset_timeout();
  73. timeout_retry();
  74. }