From d905c9da4d8984f19cd74a17cebd2743d399b035 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 30 Nov 2015 14:49:56 +0100 Subject: Reorganize source directory --- src/unitd/ubus.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/unitd/ubus.c (limited to 'src/unitd/ubus.c') diff --git a/src/unitd/ubus.c b/src/unitd/ubus.c new file mode 100644 index 0000000..370d4ba --- /dev/null +++ b/src/unitd/ubus.c @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2015 Matthias Schiffer + * + * Based on "procd" by: + * 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 "unitd.h" + +#include +#include +#include +#include + + +char *ubus_socket = NULL; +static struct ubus_context *ctx; +static struct uloop_timeout ubus_timer; + +static void +ubus_reconnect_cb(struct uloop_timeout *timeout) +{ + if (!ubus_reconnect(ctx, ubus_socket)) + ubus_add_uloop(ctx); + else + uloop_timeout_set(timeout, 2000); +} + +static void +ubus_disconnect_cb(struct ubus_context *ctx) +{ + ubus_timer.cb = ubus_reconnect_cb; + uloop_timeout_set(&ubus_timer, 2000); +} + +static void +ubus_connect_cb(struct uloop_timeout *timeout) +{ + ctx = ubus_connect(ubus_socket); + + if (!ctx) { + DEBUG(4, "Connection to ubus failed\n"); + uloop_timeout_set(&ubus_timer, 1000); + return; + } + + ctx->connection_lost = ubus_disconnect_cb; + ubus_init_service(ctx); + ubus_init_system(ctx); + + DEBUG(2, "Connected to ubus, id=%08x\n", ctx->local_id); + ubus_add_uloop(ctx); + unitd_state_ubus_connect(); +} + +void +unitd_connect_ubus(void) +{ + ubus_timer.cb = ubus_connect_cb; + uloop_timeout_set(&ubus_timer, 1000); +} -- cgit v1.2.3