summaryrefslogtreecommitdiffstats
path: root/ubus.c
blob: e87543aeef73a09ff2403a4c8952ae40d9b06e83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
 *
 * 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 <sys/resource.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

#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 void ubus_proc_cb(struct uloop_process *proc, int ret)
{
	/* nothing to do here */
}

static void procd_restart_ubus(void)
{
	char *argv[] = { "ubusd", NULL, ubus_socket, NULL };

	if (ubus_proc.pending) {
		ERROR("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) {
		setpriority(PRIO_PROCESS, 0, -20);
		execvp(argv[0], argv);
		exit(-1);
	}

	if (ubus_proc.pid <= 0) {
		ERROR("Failed to start new ubus instance\n");
		return;
	}

	DEBUG(1, "Launched new ubus instance, pid=%d\n", (int) ubus_proc.pid);
	uloop_process_add(&ubus_proc);
}

static void procd_ubus_try_connect(void)
{
	if (ctx) {
		ubus_connected = !ubus_reconnect(ctx, ubus_socket);
		return;
	}

	ctx = ubus_connect(ubus_socket);
	if (!ctx) {
		DEBUG(2, "Connection to ubus failed\n");
		return;
	}

	ctx->connection_lost = procd_ubus_connection_lost;
	ubus_connected = true;
	ubus_init_service(ctx);
	if (getpid() == 1) {
		ubus_init_log(ctx);
		ubus_init_system(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();
	}

	DEBUG(1, "Connected to ubus, id=%08x\n", ctx->local_id);
	ubus_add_uloop(ctx);
}

void procd_connect_ubus(void)
{
	ubus_proc.cb = ubus_proc_cb;
	procd_ubus_connection_lost(NULL);
}