trace: use standard POSIX header for basename()
[project/procd.git] / ubus.c
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
15 #include <sys/resource.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <signal.h>
19
20 #include "procd.h"
21
22 char *ubus_socket = NULL;
23 static struct ubus_context *ctx;
24 static struct uloop_timeout ubus_timer;
25 static int timeout;
26 static struct udebug_ubus udebug;
27
28 static void
29 procd_udebug_cb(struct udebug_ubus *ctx, struct blob_attr *data, bool enabled)
30 {
31 procd_udebug_set_enabled(enabled);
32 }
33
34 static void reset_timeout(void)
35 {
36 timeout = 50;
37 }
38
39 static void timeout_retry(void)
40 {
41 uloop_timeout_set(&ubus_timer, timeout);
42 timeout *= 2;
43 if (timeout > 1000)
44 timeout = 1000;
45 }
46
47 static void
48 ubus_reconnect_cb(struct uloop_timeout *timeout)
49 {
50 if (!ubus_reconnect(ctx, ubus_socket)) {
51 ubus_add_uloop(ctx);
52 return;
53 }
54
55 timeout_retry();
56 }
57
58 static void
59 ubus_disconnect_cb(struct ubus_context *ctx)
60 {
61 ubus_timer.cb = ubus_reconnect_cb;
62 reset_timeout();
63 timeout_retry();
64 }
65
66 static void
67 ubus_connect_cb(struct uloop_timeout *timeout)
68 {
69 ctx = ubus_connect(ubus_socket);
70
71 if (!ctx) {
72 DEBUG(4, "Connection to ubus failed\n");
73 timeout_retry();
74 return;
75 }
76
77 udebug_ubus_init(&udebug, ctx, "procd", procd_udebug_cb);
78 ctx->connection_lost = ubus_disconnect_cb;
79 ubus_init_hotplug(ctx);
80 ubus_init_service(ctx);
81 ubus_init_system(ctx);
82 watch_ubus(ctx);
83
84 DEBUG(2, "Connected to ubus, id=%08x\n", ctx->local_id);
85 reset_timeout();
86 ubus_add_uloop(ctx);
87 procd_state_ubus_connect();
88 }
89
90 void
91 procd_connect_ubus(void)
92 {
93 ubus_timer.cb = ubus_connect_cb;
94 reset_timeout();
95 timeout_retry();
96 }