ubus.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_hotplug(ctx);
  61. ubus_init_service(ctx);
  62. ubus_init_system(ctx);
  63. watch_ubus(ctx);
  64. DEBUG(2, "Connected to ubus, id=%08x\n", ctx->local_id);
  65. reset_timeout();
  66. ubus_add_uloop(ctx);
  67. procd_state_ubus_connect();
  68. }
  69. void
  70. procd_connect_ubus(void)
  71. {
  72. ubus_timer.cb = ubus_connect_cb;
  73. reset_timeout();
  74. timeout_retry();
  75. }