Subversion Repositories Mokou

Rev

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

Rev 10 Rev 12
Line 1... Line 1...
1
/* $Id: service.c 10 2024-09-07 09:55:12Z nishi $ */
1
/* $Id: service.c 12 2024-09-07 10:18:01Z nishi $ */
2
 
2
 
3
#include "mk_service.h"
3
#include "mk_service.h"
4
 
4
 
5
#include <fcntl.h>
5
#include <fcntl.h>
6
#include <stdio.h>
6
#include <stdio.h>
Line 206... Line 206...
206
	"No such service",
206
	"No such service",
207
	"Service is alive",
207
	"Service is alive",
208
	"Failed to start",
208
	"Failed to start",
209
	"Service is dead",
209
	"Service is dead",
210
	"Bad signal",
210
	"Bad signal",
211
	"Could not stop the service"
211
	"Could not stop the service",
-
 
212
	"Could not run the stop command"
212
};
213
};
213
 
214
 
214
int mk_stop_service(const char* name){
215
int mk_stop_service(const char* name){
215
	int i;
216
	int i;
216
	for(i = 0; services[i] != NULL; i++){
217
	for(i = 0; services[i] != NULL; i++){
Line 254... Line 255...
254
					return 5;
255
					return 5;
255
				}else{
256
				}else{
256
					log = mk_strcat("Sending SIG", sys_signame[sig]);
257
					log = mk_strcat("Sending SIG", sys_signame[sig]);
257
					mk_log(log);
258
					mk_log(log);
258
					free(log);
259
					free(log);
259
					bool dead = false;
-
 
260
					kill(pid, sig);
260
					kill(pid, sig);
261
					for(i = 0; i < 3; i++){
-
 
262
						if(kill(pid, 0) == -1){
-
 
263
							mk_log("Process died");
-
 
264
							dead = true;
-
 
265
							break;
-
 
266
						}else{
-
 
267
							mk_log("Process is still alive");
-
 
268
						}
-
 
269
						if(i != 2) sleep(1);
-
 
270
					}
-
 
271
					if(!dead){
-
 
272
						mk_log("Could not kill the process");
-
 
273
						return 6;
-
 
274
					}
-
 
275
				}
261
				}
276
			}else{
262
			}else{
277
				char** pargv = malloc(sizeof(*pargv));
263
				char** pargv = malloc(sizeof(*pargv));
278
				pargv[0] = NULL;
264
				pargv[0] = NULL;
279
	
265
	
Line 296... Line 282...
296
	
282
	
297
						incr = i + 1;
283
						incr = i + 1;
298
						if(srv->exec[i] == 0) break;
284
						if(srv->exec[i] == 0) break;
299
					}
285
					}
300
				}
286
				}
-
 
287
 
-
 
288
				bool fail = false;
-
 
289
				pid_t pid = fork();
-
 
290
				if(pid == 0){
-
 
291
					int n = open("/dev/null", O_RDWR);
-
 
292
					dup2(n, 1);
-
 
293
					dup2(n, 2);
-
 
294
					execvp(pargv[0], pargv);
-
 
295
					_exit(-1);
-
 
296
				}else{
-
 
297
					int status;
-
 
298
					waitpid(pid, &status, 0);
-
 
299
					if(WEXITSTATUS(status) != 0) fail = true;
-
 
300
				}
-
 
301
 
-
 
302
				for(i = 0; pargv[i] != NULL; i++) free(pargv[i]);
-
 
303
				free(pargv);
-
 
304
 
-
 
305
				if(fail){
-
 
306
					mk_log("Failed to run stop command");
-
 
307
					return 7;
-
 
308
				}
-
 
309
			}
-
 
310
			
-
 
311
			usleep(100);
-
 
312
 
-
 
313
			bool dead = false;
-
 
314
			for(i = 0; i < 3; i++){
-
 
315
				if(kill(pid, 0) == -1){
-
 
316
					mk_log("Process died");
-
 
317
					dead = true;
-
 
318
					break;
-
 
319
				}else{
-
 
320
					mk_log("Process is still alive");
-
 
321
				}
-
 
322
				if(i != 2) sleep(1);
-
 
323
			}
-
 
324
			if(!dead){
-
 
325
				mk_log("Could not kill the process");
-
 
326
				return 6;
301
			}
327
			}
302
 
328
 
303
			srv->stopped = true;
329
			srv->stopped = true;
304
			return 0;
330
			return 0;
305
		}
331
		}