X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fprocd.git;a=blobdiff_plain;f=ubus.c;h=8d521acf11e3ef17c36e9865c6b0cc1056f97c7f;hp=bba59b48a61a7c58a676a28a859006028889e026;hb=0cf8b0576a66ca28aeec98e19dc9c1cbf4324894;hpb=ca808f5c335a873c87359ce024e1a14ebae706b9 diff --git a/ubus.c b/ubus.c index bba59b4..8d521ac 100644 --- a/ubus.c +++ b/ubus.c @@ -1,84 +1,87 @@ +/* + * 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 #include -#include - #include "procd.h" char *ubus_socket = NULL; static struct ubus_context *ctx; -static struct uloop_process ubus_proc; -static bool ubus_connected = false; - -static void procd_ubus_connection_lost(struct ubus_context *old_ctx); +static struct uloop_timeout ubus_timer; +static int timeout; -static void ubus_proc_cb(struct uloop_process *proc, int ret) +static void reset_timeout(void) { - /* nothing to do here */ + timeout = 50; } -static void procd_restart_ubus(void) +static void timeout_retry(void) { - char *argv[] = { "ubusd", NULL, ubus_socket, NULL }; - - if (ubus_proc.pending) { - DPRINTF("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) { - execvp(argv[0], argv); - exit(-1); - } + uloop_timeout_set(&ubus_timer, timeout); + timeout *= 2; + if (timeout > 1000) + timeout = 1000; +} - if (ubus_proc.pid <= 0) { - DPRINTF("Failed to start new ubus instance\n"); +static void +ubus_reconnect_cb(struct uloop_timeout *timeout) +{ + if (!ubus_reconnect(ctx, ubus_socket)) { + ubus_add_uloop(ctx); return; } - DPRINTF("Launched new ubus instance, pid=%d\n", (int) ubus_proc.pid); - uloop_process_add(&ubus_proc); + timeout_retry(); } -static void procd_ubus_try_connect(void) +static void +ubus_disconnect_cb(struct ubus_context *ctx) { - if (ctx) { - ubus_connected = !ubus_reconnect(ctx, ubus_socket); - return; - } + ubus_timer.cb = ubus_reconnect_cb; + reset_timeout(); + timeout_retry(); +} +static void +ubus_connect_cb(struct uloop_timeout *timeout) +{ ctx = ubus_connect(ubus_socket); + if (!ctx) { - DPRINTF("Connection to ubus failed\n"); + DEBUG(4, "Connection to ubus failed\n"); + timeout_retry(); 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); + watch_ubus(ctx); -static void procd_ubus_connection_lost(struct ubus_context *old_ctx) -{ - procd_ubus_try_connect(); - while (!ubus_connected) { - procd_restart_ubus(); - sleep(1); - procd_ubus_try_connect(); - } - - DPRINTF("Connected to ubus, id=%08x\n", ctx->local_id); + DEBUG(2, "Connected to ubus, id=%08x\n", ctx->local_id); + reset_timeout(); ubus_add_uloop(ctx); + procd_state_ubus_connect(); } -void procd_connect_ubus(void) +void +procd_connect_ubus(void) { - ubus_proc.cb = ubus_proc_cb; - procd_ubus_connection_lost(NULL); + ubus_timer.cb = ubus_connect_cb; + reset_timeout(); + timeout_retry(); } -