summaryrefslogtreecommitdiffstats
path: root/ubus.c
diff options
context:
space:
mode:
authorJohn Crispin <blogic@openwrt.org>2013-11-14 13:41:13 +0100
committerJohn Crispin <blogic@openwrt.org>2013-11-15 17:05:20 +0100
commit916f95cb58604038695347ee41a430d8ca1f0556 (patch)
tree5dbb52a6adaf28c6c6989ea37e6975aa52075160 /ubus.c
parentf9d31edb8938341b9217ee4c14eb58111414eb97 (diff)
downloadunitd-916f95cb58604038695347ee41a430d8ca1f0556.tar
unitd-916f95cb58604038695347ee41a430d8ca1f0556.zip
debloat and reorganize code
split app into procd and init binaries remove log support, this is an external service now Signed-off-by: John Crispin <blogic@openwrt.org>
Diffstat (limited to 'ubus.c')
-rw-r--r--ubus.c97
1 files changed, 23 insertions, 74 deletions
diff --git a/ubus.c b/ubus.c
index 54ead33..6166254 100644
--- a/ubus.c
+++ b/ubus.c
@@ -21,97 +21,46 @@
char *ubus_socket = NULL;
static struct ubus_context *ctx;
-static struct uloop_process ubus_proc;
-static bool ubus_connected = false;
-static struct uloop_timeout retry;
-static int reconnect = 1;
+static struct uloop_timeout ubus_timer;
-static void procd_ubus_connection_lost(struct ubus_context *old_ctx);
-
-static void ubus_proc_cb(struct uloop_process *proc, int ret)
+static void
+ubus_reconnect_cb(struct uloop_timeout *timeout)
{
- /* nothing to do here */
+ if (!ubus_reconnect(ctx, ubus_socket))
+ ubus_add_uloop(ctx);
+ else
+ uloop_timeout_set(timeout, 2000);
}
-static void procd_restart_ubus(void)
+static void
+ubus_disconnect_cb(struct ubus_context *ctx)
{
- char *argv[] = { "ubusd", NULL, ubus_socket, NULL };
-
- if (ubus_proc.pending) {
- ERROR("Killing existing ubus instance, pid=%d\n", (int) ubus_proc.pid);
- kill(ubus_proc.pid, SIGKILL);
- uloop_process_delete(&ubus_proc);
- }
-
- if (ubus_socket)
- argv[1] = "-s";
-
- ubus_proc.pid = fork();
- if (!ubus_proc.pid) {
- setpriority(PRIO_PROCESS, 0, -20);
- execvp(argv[0], argv);
- exit(-1);
- }
-
- if (ubus_proc.pid <= 0) {
- ERROR("Failed to start new ubus instance\n");
- return;
- }
-
- DEBUG(1, "Launched new ubus instance, pid=%d\n", (int) ubus_proc.pid);
- uloop_process_add(&ubus_proc);
+ ubus_timer.cb = ubus_reconnect_cb;
+ uloop_timeout_set(&ubus_timer, 2000);
}
-static void procd_ubus_try_connect(void)
+static void
+ubus_connect_cb(struct uloop_timeout *timeout)
{
- if (ctx) {
- ubus_connected = !ubus_reconnect(ctx, ubus_socket);
- return;
- }
ctx = ubus_connect(ubus_socket);
+
if (!ctx) {
- ubus_connected = false;
- DEBUG(2, "Connection to ubus failed\n");
+ DEBUG(4, "Connection to ubus failed\n");
+ uloop_timeout_set(&ubus_timer, 1000);
return;
}
- ctx->connection_lost = procd_ubus_connection_lost;
- ubus_connected = true;
+ ctx->connection_lost = ubus_disconnect_cb;
ubus_init_service(ctx);
ubus_init_system(ctx);
- if (getpid() == 1)
- ubus_init_log(ctx);
-}
-
-static void
-procd_ubus_reconnect_timer(struct uloop_timeout *timeout)
-{
- procd_ubus_try_connect();
- if (ubus_connected) {
- DEBUG(1, "Connected to ubus, id=%08x\n", ctx->local_id);
- ubus_add_uloop(ctx);
- return;
- }
- uloop_timeout_set(&retry, 1000);
- procd_restart_ubus();
+ DEBUG(2, "Connected to ubus, id=%08x\n", ctx->local_id);
+ ubus_add_uloop(ctx);
}
-static void procd_ubus_connection_lost(struct ubus_context *old_ctx)
+void
+procd_connect_ubus(void)
{
- retry.cb = procd_ubus_reconnect_timer;
- procd_restart_ubus();
- uloop_timeout_set(&retry, 1000);
+ ubus_timer.cb = ubus_connect_cb;
+ uloop_timeout_set(&ubus_timer, 1000);
}
-
-void procd_connect_ubus(void)
-{
- ubus_proc.cb = ubus_proc_cb;
- procd_ubus_connection_lost(NULL);
-}
-
-void procd_reconnect_ubus(int _reconnect)
-{
- reconnect = _reconnect;
-}
-