add blobmsg_list_move
[project/procd.git] / instance.c
1 #include "procd.h"
2 #include "service.h"
3 #include "instance.h"
4
5 void
6 instance_start(struct service_instance *in)
7 {
8 in->restart = false;
9 }
10
11 static void
12 instance_timeout(struct uloop_timeout *t)
13 {
14 struct service_instance *in;
15
16 in = container_of(t, struct service_instance, timeout);
17 kill(in->proc.pid, SIGKILL);
18 uloop_process_delete(&in->proc);
19 in->proc.cb(&in->proc, -1);
20 }
21
22 static void
23 instance_exit(struct uloop_process *p, int ret)
24 {
25 struct service_instance *in;
26
27 in = container_of(p, struct service_instance, proc);
28 uloop_timeout_cancel(&in->timeout);
29 if (in->restart)
30 instance_start(in);
31 }
32
33 void
34 instance_stop(struct service_instance *in, bool restart)
35 {
36 if (!in->proc.pending)
37 return;
38
39 kill(in->proc.pid, SIGTERM);
40 }
41
42 static bool
43 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
44 {
45 int len = blob_pad_len(in->config);
46
47 if (len != blob_pad_len(in_new->config))
48 return true;
49
50 if (memcmp(in->config, in_new->config, blob_pad_len(in->config)) != 0)
51 return true;
52
53 return false;
54 }
55
56 bool
57 instance_update(struct service_instance *in, struct service_instance *in_new)
58 {
59 bool changed = instance_config_changed(in, in_new);
60
61 in->config = in_new->config;
62 if (!changed)
63 return false;
64
65 instance_stop(in, true);
66 return true;
67 }
68
69 void
70 instance_free(struct service_instance *in)
71 {
72 uloop_process_delete(&in->proc);
73 uloop_timeout_cancel(&in->timeout);
74 free(in);
75 }
76
77 void
78 instance_init(struct service_instance *in, struct blob_attr *config)
79 {
80 in->config = config;
81 in->timeout.cb = instance_timeout;
82 in->proc.cb = instance_exit;
83 }
84
85