202646a7422cdd07ae04b95929999b8b1584f034
[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_process ubus_proc;
25 static bool ubus_connected = false;
26 static int reconnect = 1;
27
28 static void procd_ubus_connection_lost(struct ubus_context *old_ctx);
29
30 static void ubus_proc_cb(struct uloop_process *proc, int ret)
31 {
32         /* nothing to do here */
33 }
34
35 static void procd_restart_ubus(void)
36 {
37         char *argv[] = { "ubusd", NULL, ubus_socket, NULL };
38
39         if (ubus_proc.pending) {
40                 ERROR("Killing existing ubus instance, pid=%d\n", (int) ubus_proc.pid);
41                 kill(ubus_proc.pid, SIGKILL);
42                 uloop_process_delete(&ubus_proc);
43         }
44
45         if (ubus_socket)
46                 argv[1] = "-s";
47
48         ubus_proc.pid = fork();
49         if (!ubus_proc.pid) {
50                 setpriority(PRIO_PROCESS, 0, -20);
51                 execvp(argv[0], argv);
52                 exit(-1);
53         }
54
55         if (ubus_proc.pid <= 0) {
56                 ERROR("Failed to start new ubus instance\n");
57                 return;
58         }
59
60         DEBUG(1, "Launched new ubus instance, pid=%d\n", (int) ubus_proc.pid);
61         uloop_process_add(&ubus_proc);
62 }
63
64 static void procd_ubus_try_connect(void)
65 {
66         if (ctx) {
67                 ubus_connected = !ubus_reconnect(ctx, ubus_socket);
68                 return;
69         }
70
71         ctx = ubus_connect(ubus_socket);
72         if (!ctx) {
73                 DEBUG(2, "Connection to ubus failed\n");
74                 return;
75         }
76
77         ctx->connection_lost = procd_ubus_connection_lost;
78         ubus_connected = true;
79         ubus_init_service(ctx);
80         ubus_init_system(ctx);
81         if (getpid() == 1)
82                 ubus_init_log(ctx);
83 }
84
85 static void procd_ubus_connection_lost(struct ubus_context *old_ctx)
86 {
87         if (!reconnect)
88                 return;
89
90         procd_ubus_try_connect();
91         while (!ubus_connected) {
92                 procd_restart_ubus();
93                 sleep(1);
94                 procd_ubus_try_connect();
95         }
96
97         DEBUG(1, "Connected to ubus, id=%08x\n", ctx->local_id);
98         ubus_add_uloop(ctx);
99 }
100
101 void procd_connect_ubus(void)
102 {
103         ubus_proc.cb = ubus_proc_cb;
104         procd_ubus_connection_lost(NULL);
105 }
106
107 void procd_reconnect_ubus(int _reconnect)
108 {
109         reconnect = _reconnect;
110 }
111