summaryrefslogtreecommitdiffstats
path: root/instance.c
blob: e2c8b28839f449dd7facfab5af132f1144b3d786 (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
#include "procd.h"
#include "service.h"
#include "instance.h"

void
instance_start(struct service_instance *in)
{
	in->restart = false;
}

static void
instance_timeout(struct uloop_timeout *t)
{
	struct service_instance *in;

	in = container_of(t, struct service_instance, timeout);
	kill(in->proc.pid, SIGKILL);
	uloop_process_delete(&in->proc);
	in->proc.cb(&in->proc, -1);
}

static void
instance_exit(struct uloop_process *p, int ret)
{
	struct service_instance *in;

	in = container_of(p, struct service_instance, proc);
	uloop_timeout_cancel(&in->timeout);
	if (in->restart)
		instance_start(in);
}

void
instance_stop(struct service_instance *in, bool restart)
{
	if (!in->proc.pending)
		return;

	kill(in->proc.pid, SIGTERM);
}

static bool
instance_config_changed(struct service_instance *in, struct service_instance *in_new)
{
	int len = blob_pad_len(in->config);

	if (len != blob_pad_len(in_new->config))
		return true;

	if (memcmp(in->config, in_new->config, blob_pad_len(in->config)) != 0)
		return true;

	return false;
}

bool
instance_update(struct service_instance *in, struct service_instance *in_new)
{
	bool changed = instance_config_changed(in, in_new);

	in->config = in_new->config;
	if (!changed)
		return false;

	instance_stop(in, true);
	return true;
}

void
instance_free(struct service_instance *in)
{
	uloop_process_delete(&in->proc);
	uloop_timeout_cancel(&in->timeout);
	free(in);
}

void
instance_init(struct service_instance *in, struct blob_attr *config)
{
	in->config = config;
	in->timeout.cb = instance_timeout;
	in->proc.cb = instance_exit;
}