Subversion Repositories Mokou

Rev

Rev 4 | Rev 6 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4 Rev 5
Line 1... Line 1...
1
/* $Id: service.c 4 2024-09-06 09:55:53Z nishi $ */
1
/* $Id: service.c 5 2024-09-07 08:42:09Z nishi $ */
2
 
2
 
3
#include "mk_service.h"
3
#include "mk_service.h"
4
 
4
 
-
 
5
#include <fcntl.h>
5
#include <stdio.h>
6
#include <stdio.h>
6
#include <dirent.h>
7
#include <dirent.h>
7
#include <stdlib.h>
8
#include <stdlib.h>
8
#include <string.h>
9
#include <string.h>
9
#include <sys/stat.h>
10
#include <sys/stat.h>
-
 
11
#include <signal.h>
-
 
12
#include <unistd.h>
-
 
13
#include <sys/wait.h>
10
 
14
 
11
#include "mk_log.h"
15
#include "mk_log.h"
12
#include "mk_util.h"
16
#include "mk_util.h"
13
 
17
 
14
struct mk_service** services = NULL;
18
struct mk_service** services = NULL;
15
 
19
 
16
void mk_service_scan(void){
20
void mk_service_scan(void){
17
	if(services != NULL){
21
	if(services != NULL){
18
		int i;
22
		int i;
19
		for(i = 0; services[i] != NULL; i++){
23
		for(i = 0; services[i] != NULL; i++){
-
 
24
			if(services[i]->name != NULL) free(services[i]->name);
-
 
25
			if(services[i]->stop != NULL) free(services[i]->stop);
20
			if(services[i]->description != NULL) free(services[i]->description);
26
			if(services[i]->description != NULL) free(services[i]->description);
21
			if(services[i]->exec != NULL) free(services[i]->exec);
27
			if(services[i]->exec != NULL) free(services[i]->exec);
22
			if(services[i]->pidfile != NULL) free(services[i]->pidfile);
28
			if(services[i]->pidfile != NULL) free(services[i]->pidfile);
23
			free(services[i]);
29
			free(services[i]);
24
		}
30
		}
Line 50... Line 56...
50
					int i;
56
					int i;
51
					int incr = 0;
57
					int incr = 0;
52
 
58
 
53
					char* desc = NULL;
59
					char* desc = NULL;
54
					char* exec = NULL;
60
					char* exec = NULL;
-
 
61
					char* stop = NULL;
55
					char* pidfile = NULL;
62
					char* pidfile = NULL;
56
					
63
					
57
					for(i = 0;; i++){
64
					for(i = 0;; i++){
58
						if(buffer[i] == '\n' || buffer[i] == 0){
65
						if(buffer[i] == '\n' || buffer[i] == 0){
59
							char oldc = buffer[i];
66
							char oldc = buffer[i];
Line 77... Line 84...
77
											if(exec != NULL) free(exec);
84
											if(exec != NULL) free(exec);
78
											exec = mk_strdup(value);
85
											exec = mk_strdup(value);
79
										}else if(strcmp(key, "pidfile") == 0){
86
										}else if(strcmp(key, "pidfile") == 0){
80
											if(pidfile != NULL) free(pidfile);
87
											if(pidfile != NULL) free(pidfile);
81
											pidfile = mk_strdup(value);
88
											pidfile = mk_strdup(value);
-
 
89
										}else if(strcmp(key, "stop") == 0){
-
 
90
											if(stop != NULL) free(stop);
-
 
91
											stop = mk_strdup(value);
82
										}
92
										}
83
	
93
	
84
										break;
94
										break;
85
									}
95
									}
86
								}
96
								}
Line 109... Line 119...
109
					if(!bad){
119
					if(!bad){
110
						char* log = mk_strcat3("Adding ", desc == NULL ? path : desc, " to the list");
120
						char* log = mk_strcat3("Adding ", desc == NULL ? path : desc, " to the list");
111
						mk_log(log);
121
						mk_log(log);
112
						free(log);
122
						free(log);
113
 
123
 
-
 
124
						int i;
114
						struct mk_service* serv = malloc(sizeof(*serv));
125
						struct mk_service* serv = malloc(sizeof(*serv));
-
 
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
 
115
						serv->description = desc != NULL ? mk_strdup(desc) : NULL;
135
						serv->description = desc != NULL ? mk_strdup(desc) : NULL;
-
 
136
						serv->stop = stop != NULL ? mk_strdup(stop) : NULL;
116
						serv->exec = mk_strdup(exec);
137
						serv->exec = mk_strdup(exec);
117
						serv->pidfile = mk_strdup(pidfile);
138
						serv->pidfile = mk_strdup(pidfile);
-
 
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);
118
					}
150
					}
119
 
151
 
120
					if(desc != NULL) free(desc);
152
					if(desc != NULL) free(desc);
121
					if(exec != NULL) free(exec);
153
					if(exec != NULL) free(exec);
122
					if(pidfile != NULL) free(pidfile);
154
					if(pidfile != NULL) free(pidfile);
Line 128... Line 160...
128
		closedir(dir);
160
		closedir(dir);
129
	}else{
161
	}else{
130
		mk_log("Cannot open the directory.");
162
		mk_log("Cannot open the directory.");
131
	}
163
	}
132
}
164
}
-
 
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
}