-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrespawn.c
256 lines (206 loc) · 4.44 KB
/
respawn.c
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
#include "tools.h"
#define RESPAWN_FILE "/etc/ueld/respawn.list"
#define HASHTABLE_FIRST_NODE_SIZE 32
#ifdef __GNUC__
#define likely(x) __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)
#else
#define likely(x) (x)
#define unlikely(x) (x)
#endif /* __GNUC__ */
struct respawn_node {
int nref;
int vt;
pid_t pid;
char *cmd;
struct respawn_node *prev;
struct respawn_node *next;
};
static struct respawn_node *first_nodes[HASHTABLE_FIRST_NODE_SIZE];
static unsigned int disable = 0;
static size_t length;
static char *respawn;
#define hashpid(pid) ((pid) % HASHTABLE_FIRST_NODE_SIZE)
#define nodefree(node) (free(node))
#define noderef(node) (((node)->nref)++)
static struct respawn_node *nodenew()
{
static struct respawn_node *node;
if ((node = malloc(sizeof(struct respawn_node))) == NULL) {
return NULL;
}
node->nref = 1;
return node;
}
static void nodeunref(struct respawn_node *node)
{
node->nref = node->nref - 1;
if (node->nref <= 0) {
nodefree(node);
}
}
static void insert(pid_t key, struct respawn_node *node)
{
int index = hashpid(key);
noderef(node);
node->prev = NULL;
node->next = NULL;
if (first_nodes[index] == NULL) {
first_nodes[index] = node;
} else {
struct respawn_node *prenode = first_nodes[index];
while (prenode->next != NULL) {
prenode = prenode->next;
}
prenode->next = node;
node->prev = prenode;
}
}
static struct respawn_node *get(pid_t key)
{
int index = hashpid(key);
struct respawn_node *node = first_nodes[index];
while (node != NULL) {
if (node->pid == key) {
return node;
}
node = node->next;
}
return NULL;
}
#define delete_node_without_index(node) delete_node((node), -1)
static void delete_node(struct respawn_node *node, int index)
{
struct respawn_node *prenode = node->prev;
if (prenode) {
prenode->next = node->next;
} else {
if (index == -1)
index = hashpid(node->pid);
first_nodes[index] = node->next;
}
if (node->next) {
node->next->prev = prenode;
}
nodeunref(node);
}
static void delete(pid_t key)
{
int index = hashpid(key);
struct respawn_node *node = first_nodes[index];
while (node != NULL) {
if (node->pid == key) {
delete_node(node, index);
}
node = node->next;
}
}
static pid_t runapp(struct respawn_node *app)
{
pid_t pid;
if (app->vt) {
pid = ueld_run(app->cmd, URF_SETVT|URF_CMDLINE, app->vt, NULL);
} else {
pid = ueld_run(app->cmd, URF_CMDLINE, 0, NULL);
}
if (pid < 0)
pid = 0;
return pid;
}
void respawn_init()
{
int fd;
int appssz = 0;
char *p, *vt, *cmd;
if ((fd = open(RESPAWN_FILE, O_RDONLY)) < 0)
return;
length = lseek(fd, 0, SEEK_END) + 1;
respawn = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
if (!respawn) {
close(fd);
return;
}
respawn[length - 1] = 0;
close(fd);
p = respawn;
while (1) {
cmd = strchr(p, ':');
if (!cmd || *cmd == 0) break;
*cmd = 0;
cmd++;
vt = p;
p = strchr(cmd, '\n');
if (!p || *p == 0) break;
*p = 0;
p++;
struct respawn_node *node;
if ((node = nodenew()) != NULL) {
node->vt = atoi(vt);
node->cmd = cmd;
pid_t pid = runapp(node);
node->pid = pid;
if (unlikely(pid == 0)) {
ueld_print("Run respwan process '%s' first time failed\n", cmd);
} else {
appssz++;
insert(pid, node);
}
nodeunref(node);
} else {
ueld_print("Create respwan node for '%s' failed\n", cmd);
}
}
if (!appssz)
munmap(respawn, length);
}
void respawnpid(pid_t pid)
{
if (disable) {
return;
}
struct respawn_node *node;
if ((node = get(pid)) != NULL) {
pid_t newpid = runapp(node);
if (unlikely(newpid == 0)) {
ueld_print("Respwan process '%s' failed, remove it from list...\n", node->cmd);
delete_node_without_index(node);
} else {
noderef(node);
delete_node_without_index(node);
node->pid = newpid;
insert(newpid, node);
nodeunref(node);
}
}
}
void clearpid(pid_t pid)
{
if (pid != 0) {
delete(pid);
} else {
disable = 1;
for (int i = 0; i < HASHTABLE_FIRST_NODE_SIZE; i++) {
if (first_nodes[i] != NULL) {
struct respawn_node *node = first_nodes[i];
while (node->next != NULL) {
struct respawn_node *tmp = node;
node = node->next;
nodeunref(tmp);
}
nodeunref(node);
first_nodes[i] = NULL;
}
}
munmap(respawn, length);
}
}