Subversion Repositories Mokou

Rev

Rev 4 | Rev 6 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 nishi 1
/* $Id: service.c 5 2024-09-07 08:42:09Z nishi $ */
2
 
3
#include "mk_service.h"
4
 
5 nishi 5
#include <fcntl.h>
3 nishi 6
#include <stdio.h>
2 nishi 7
#include <dirent.h>
8
#include <stdlib.h>
3 nishi 9
#include <string.h>
10
#include <sys/stat.h>
5 nishi 11
#include <signal.h>
12
#include <unistd.h>
13
#include <sys/wait.h>
2 nishi 14
 
15
#include "mk_log.h"
16
#include "mk_util.h"
17
 
18
struct mk_service** services = NULL;
19
 
20
void mk_service_scan(void){
21
	if(services != NULL){
22
		int i;
23
		for(i = 0; services[i] != NULL; i++){
5 nishi 24
			if(services[i]->name != NULL) free(services[i]->name);
25
			if(services[i]->stop != NULL) free(services[i]->stop);
4 nishi 26
			if(services[i]->description != NULL) free(services[i]->description);
27
			if(services[i]->exec != NULL) free(services[i]->exec);
28
			if(services[i]->pidfile != NULL) free(services[i]->pidfile);
2 nishi 29
			free(services[i]);
30
		}
31
		free(services);
3 nishi 32
		mk_log("Cleaning up the list");
2 nishi 33
	}
3 nishi 34
	services = malloc(sizeof(*services));
35
	services[0] = NULL;
2 nishi 36
 
37
	mk_log("Scanning the service directory.");
38
 
39
	DIR* dir = opendir(PREFIX "/etc/mokou");
40
	if(dir != NULL){
41
		struct dirent* d;
42
		while((d = readdir(dir)) != NULL){
43
			if(mk_endswith(d->d_name, ".conf")){
44
				char* path = mk_strcat(PREFIX "/etc/mokou/", d->d_name);
45
				char* str = mk_strcat("Reading ", path);
46
				mk_log(str);
47
				free(str);
3 nishi 48
 
49
				FILE* f = fopen(path, "r");
50
				if(f != NULL){
51
					struct stat s;
52
					stat(path, &s);
53
					char* buffer = malloc(s.st_size + 1);
54
					buffer[s.st_size] = 0;
55
					fread(buffer, s.st_size, 1, f);
56
					int i;
57
					int incr = 0;
58
 
59
					char* desc = NULL;
60
					char* exec = NULL;
5 nishi 61
					char* stop = NULL;
3 nishi 62
					char* pidfile = NULL;
63
 
64
					for(i = 0;; i++){
65
						if(buffer[i] == '\n' || buffer[i] == 0){
66
							char oldc = buffer[i];
67
							buffer[i] = 0;
68
 
69
							char* line = buffer + incr;
70
 
71
							if(strlen(line) > 0 && line[0] != '#'){
72
								int j;
73
 
74
								for(j = 0; line[j] != 0; j++){
75
									if(line[j] == '='){
76
										line[j] = 0;
77
 
78
										char* key = line;
79
										char* value = line + j + 1;
80
										if(strcmp(key, "description") == 0){
81
											if(desc != NULL) free(desc);
82
											desc = mk_strdup(value);
83
										}else if(strcmp(key, "exec") == 0){
84
											if(exec != NULL) free(exec);
85
											exec = mk_strdup(value);
86
										}else if(strcmp(key, "pidfile") == 0){
87
											if(pidfile != NULL) free(pidfile);
88
											pidfile = mk_strdup(value);
5 nishi 89
										}else if(strcmp(key, "stop") == 0){
90
											if(stop != NULL) free(stop);
91
											stop = mk_strdup(value);
3 nishi 92
										}
93
 
94
										break;
95
									}
96
								}
97
							}
98
 
99
							incr = i + 1;
100
							if(oldc == 0) break;
101
						}
102
					}
103
					fclose(f);
104
 
105
					bool bad = false;
106
					if(exec == NULL){
107
						char* log = mk_strcat(desc == NULL ? path : desc, ": Missing exec");
108
						mk_log(log);
109
						free(log);
110
						bad = true;
111
					}
112
					if(pidfile == NULL){
113
						char* log = mk_strcat(desc == NULL ? path : desc, ": Missing pidfile");
114
						mk_log(log);
115
						free(log);
116
						bad = true;
117
					}
118
 
119
					if(!bad){
120
						char* log = mk_strcat3("Adding ", desc == NULL ? path : desc, " to the list");
121
						mk_log(log);
122
						free(log);
4 nishi 123
 
5 nishi 124
						int i;
4 nishi 125
						struct mk_service* serv = malloc(sizeof(*serv));
5 nishi 126
						serv->name = mk_strdup(d->d_name);
127
 
128
						for(i = strlen(d->d_name) - 1; i >= 0; i--){
129
							if(serv->name[i] == '.'){
130
								serv->name[i] = 0;
131
								break;
132
							}
133
						}
134
 
4 nishi 135
						serv->description = desc != NULL ? mk_strdup(desc) : NULL;
5 nishi 136
						serv->stop = stop != NULL ? mk_strdup(stop) : NULL;
4 nishi 137
						serv->exec = mk_strdup(exec);
138
						serv->pidfile = mk_strdup(pidfile);
5 nishi 139
						serv->stopped = false;
140
 
141
						struct mk_service** oldsrvs = services;
142
						for(i = 0; oldsrvs[i] != NULL; i++);
143
						services = malloc(sizeof(*services) * (i + 2));
144
						for(i = 0; oldsrvs[i] != NULL; i++){
145
							services[i] = oldsrvs[i];
146
						}
147
						services[i] = serv;
148
						services[i + 1] = NULL;
149
						free(oldsrvs);
3 nishi 150
					}
151
 
152
					if(desc != NULL) free(desc);
153
					if(exec != NULL) free(exec);
154
					if(pidfile != NULL) free(pidfile);
155
				}
156
 
2 nishi 157
				free(path);
158
			}
159
		}
160
		closedir(dir);
161
	}else{
162
		mk_log("Cannot open the directory.");
163
	}
164
}
5 nishi 165
 
166
const char* errors[] = {
167
	"Success",
168
	"No such service",
169
	"Service is alive",
170
	"Failed to start",
171
	"Service is dead",
172
	"Bad signal",
173
	"Could not stop the service"
174
};
175
 
176
int mk_stop_service(const char* name){
177
	int i;
178
	for(i = 0; services[i] != NULL; i++){
179
		if(strcmp(services[i]->name, name) == 0){
180
			struct mk_service* srv = services[i];
181
			char* log = mk_strcat("Stopping ", name);
182
			mk_log(log);
183
			free(log);
184
 
185
			bool alive = false;
186
 
187
			FILE* f = fopen(srv->pidfile, "r");
188
			unsigned long long pid;
189
			if(f != NULL){
190
				fscanf(f, "%llu", &pid);
191
				fclose(f);
192
				alive = kill(pid, 0) == 0;
193
			}
194
 
195
			if(!alive){
196
				mk_log("Process seems to be dead, not stopping");
197
				return 4;
198
			}
199
 
200
			if(srv->stop == NULL || srv->stop[0] == '#'){
201
				int sig = -1;
202
				if(srv->stop == NULL){
203
					sig = SIGINT;
204
				}
205
				if(sig == -1){
206
					int i;
207
					for(i = 1; i < NSIG; i++){
208
						if(strcmp(sys_signame[i], srv->stop + 1) == 0){
209
							sig = i;
210
							break;
211
						}
212
					}
213
				}
214
				if(sig == -1){
215
					mk_log("Bad signal");
216
					return 5;
217
				}else{
218
					log = mk_strcat("Sending SIG", sys_signame[sig]);
219
					mk_log(log);
220
					free(log);
221
					bool dead = false;
222
					kill(pid, sig);
223
					for(i = 0; i < 3; i++){
224
						if(kill(pid, 0) == -1){
225
							mk_log("Process died");
226
							dead = true;
227
							break;
228
						}else{
229
							mk_log("Process is still alive");
230
						}
231
						if(i != 2) sleep(1);
232
					}
233
					if(!dead){
234
						mk_log("Could not kill the process");
235
						return 6;
236
					}
237
				}
238
			}else{
239
				char** pargv = malloc(sizeof(*pargv));
240
				pargv[0] = NULL;
241
 
242
				int i;
243
				int incr = 0;
244
				for(i = 0;; i++){
245
					if(srv->stop[i] == 0 || srv->stop[i] == ' '){
246
						char* str = malloc(i - incr + 1);
247
						memcpy(str, srv->stop + incr, i - incr);
248
						str[i - incr] = 0;
249
 
250
						char** oldargv = pargv;
251
						int j;
252
						for(j = 0; oldargv[j] != NULL; j++);
253
						pargv = malloc(sizeof(*pargv) * (j + 2));
254
						for(j = 0; oldargv[j] != NULL; j++) pargv[j] = oldargv[j];
255
						pargv[j] = str;
256
						pargv[j + 1]  = NULL;
257
						free(oldargv);
258
 
259
						incr = i + 1;
260
						if(srv->exec[i] == 0) break;
261
					}
262
				}
263
			}
264
 
265
			srv->stopped = true;
266
			return 0;
267
		}
268
	}
269
	return 1;
270
}
271
 
272
int mk_start_service(const char* name){
273
	int i;
274
	for(i = 0; services[i] != NULL; i++){
275
		if(strcmp(services[i]->name, name) == 0){
276
			struct mk_service* srv = services[i];
277
			char* log = mk_strcat("Starting ", name);
278
			mk_log(log);
279
			free(log);
280
 
281
			bool alive = false;
282
 
283
			FILE* f = fopen(srv->pidfile, "r");
284
			if(f != NULL){
285
				unsigned long long pid;
286
				fscanf(f, "%llu", &pid);
287
				fclose(f);
288
				alive = kill(pid, 0) == 0;
289
			}
290
			if(alive){
291
				mk_log("Process seems to be alive, not starting");
292
				return 2;
293
			}
294
 
295
			char** pargv = malloc(sizeof(*pargv));
296
			pargv[0] = NULL;
297
 
298
			int i;
299
			int incr = 0;
300
			for(i = 0;; i++){
301
				if(srv->exec[i] == 0 || srv->exec[i] == ' '){
302
					char* str = malloc(i - incr + 1);
303
					memcpy(str, srv->exec + incr, i - incr);
304
					str[i - incr] = 0;
305
 
306
					char** oldargv = pargv;
307
					int j;
308
					for(j = 0; oldargv[j] != NULL; j++);
309
					pargv = malloc(sizeof(*pargv) * (j + 2));
310
					for(j = 0; oldargv[j] != NULL; j++) pargv[j] = oldargv[j];
311
					pargv[j] = str;
312
					pargv[j + 1]  = NULL;
313
					free(oldargv);
314
 
315
					incr = i + 1;
316
					if(srv->exec[i] == 0) break;
317
				}
318
			}
319
 
320
			bool fail = false;
321
 
322
			pid_t pid = fork();
323
			if(pid == 0){
324
				int n = open("/dev/null", O_RDWR);
325
				dup2(n, 1);
326
				dup2(n, 2);
327
				execvp(pargv[0], pargv);
328
				_exit(-1);
329
			}else{
330
				int status;
331
				waitpid(pid, &status, 0);
332
				if(WEXITSTATUS(status) != 0) fail = true;
333
			}
334
			for(i = 0; pargv[i] != NULL; i++) free(pargv[i]);
335
			free(pargv);
336
			if(fail){
337
				log = mk_strcat("Failed to start ", name);
338
				mk_log(log);
339
				free(log);
340
				srv->stopped = false;
341
				return 3;
342
			}else{
343
				log = mk_strcat("Started ", name);
344
				mk_log(log);
345
				free(log);
346
				srv->stopped = false;
347
			}
348
 
349
			return 0;
350
		}
351
	}
352
	return 1;
353
}
354
 
355
void mk_start_services(void){
356
	int i;
357
	for(i = 0; services[i] != NULL; i++){
358
		mk_start_service(services[i]->name);
359
	}
360
}