ubus.c 1.6 KB

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