X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fprocd.git;a=blobdiff_plain;f=ubus.c;h=54ead3363c124c270553fbc1cd66ba8763877e27;hp=7b96fd666d7f7a29ef1d71eaeec296ac3ba8e029;hb=64f8c21e5a670053ab9e4719cedbb2d963634c0c;hpb=0f479be2068d7fe3bd5953e7454aaabb2be38d93 diff --git a/ubus.c b/ubus.c index 7b96fd6..54ead33 100644 --- a/ubus.c +++ b/ubus.c @@ -1,3 +1,17 @@ +/* + * Copyright (C) 2013 Felix Fietkau + * Copyright (C) 2013 John Crispin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + #include #include #include @@ -9,6 +23,8 @@ 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 void procd_ubus_connection_lost(struct ubus_context *old_ctx); @@ -42,7 +58,7 @@ static void procd_restart_ubus(void) return; } - LOG("Launched new ubus instance, pid=%d\n", (int) ubus_proc.pid); + DEBUG(1, "Launched new ubus instance, pid=%d\n", (int) ubus_proc.pid); uloop_process_add(&ubus_proc); } @@ -52,29 +68,40 @@ static void procd_ubus_try_connect(void) 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"); return; } ctx->connection_lost = procd_ubus_connection_lost; ubus_connected = true; - procd_init_service(ctx); + ubus_init_service(ctx); + ubus_init_system(ctx); + if (getpid() == 1) + ubus_init_log(ctx); } -static void procd_ubus_connection_lost(struct ubus_context *old_ctx) +static void +procd_ubus_reconnect_timer(struct uloop_timeout *timeout) { procd_ubus_try_connect(); - while (!ubus_connected) { - procd_restart_ubus(); - sleep(1); - procd_ubus_try_connect(); + if (ubus_connected) { + DEBUG(1, "Connected to ubus, id=%08x\n", ctx->local_id); + ubus_add_uloop(ctx); + return; } - LOG("Connected to ubus, id=%08x\n", ctx->local_id); - ubus_add_uloop(ctx); + uloop_timeout_set(&retry, 1000); + procd_restart_ubus(); +} + +static void procd_ubus_connection_lost(struct ubus_context *old_ctx) +{ + retry.cb = procd_ubus_reconnect_timer; + procd_restart_ubus(); + uloop_timeout_set(&retry, 1000); } void procd_connect_ubus(void) @@ -83,3 +110,8 @@ void procd_connect_ubus(void) procd_ubus_connection_lost(NULL); } +void procd_reconnect_ubus(int _reconnect) +{ + reconnect = _reconnect; +} +