2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
15 #include <sys/resource.h>
22 char *ubus_socket
= NULL
;
23 static struct ubus_context
*ctx
;
24 static struct uloop_process ubus_proc
;
25 static bool ubus_connected
= false;
26 static struct uloop_timeout retry
;
27 static int reconnect
= 1;
29 static void procd_ubus_connection_lost(struct ubus_context
*old_ctx
);
31 static void ubus_proc_cb(struct uloop_process
*proc
, int ret
)
33 /* nothing to do here */
36 static void procd_restart_ubus(void)
38 char *argv
[] = { "ubusd", NULL
, ubus_socket
, NULL
};
40 if (ubus_proc
.pending
) {
41 ERROR("Killing existing ubus instance, pid=%d\n", (int) ubus_proc
.pid
);
42 kill(ubus_proc
.pid
, SIGKILL
);
43 uloop_process_delete(&ubus_proc
);
49 ubus_proc
.pid
= fork();
51 setpriority(PRIO_PROCESS
, 0, -20);
52 execvp(argv
[0], argv
);
56 if (ubus_proc
.pid
<= 0) {
57 ERROR("Failed to start new ubus instance\n");
61 DEBUG(1, "Launched new ubus instance, pid=%d\n", (int) ubus_proc
.pid
);
62 uloop_process_add(&ubus_proc
);
65 static void procd_ubus_try_connect(void)
68 ubus_connected
= !ubus_reconnect(ctx
, ubus_socket
);
71 ctx
= ubus_connect(ubus_socket
);
73 ubus_connected
= false;
74 DEBUG(2, "Connection to ubus failed\n");
78 ctx
->connection_lost
= procd_ubus_connection_lost
;
79 ubus_connected
= true;
80 ubus_init_service(ctx
);
81 ubus_init_system(ctx
);
87 procd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
89 procd_ubus_try_connect();
91 DEBUG(1, "Connected to ubus, id=%08x\n", ctx
->local_id
);
96 uloop_timeout_set(&retry
, 1000);
100 static void procd_ubus_connection_lost(struct ubus_context
*old_ctx
)
102 retry
.cb
= procd_ubus_reconnect_timer
;
103 procd_restart_ubus();
104 uloop_timeout_set(&retry
, 1000);
107 void procd_connect_ubus(void)
109 ubus_proc
.cb
= ubus_proc_cb
;
110 procd_ubus_connection_lost(NULL
);
113 void procd_reconnect_ubus(int _reconnect
)
115 reconnect
= _reconnect
;