Subversion Repositories Mokou

Rev

Rev 2 | Rev 4 | 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 3 2024-09-06 09:21:55Z nishi $ */
2
 
3
#include "mk_service.h"
4
 
3 nishi 5
#include <stdio.h>
2 nishi 6
#include <dirent.h>
7
#include <stdlib.h>
3 nishi 8
#include <string.h>
9
#include <sys/stat.h>
2 nishi 10
 
11
#include "mk_log.h"
12
#include "mk_util.h"
13
 
14
struct mk_service** services = NULL;
15
 
16
void mk_service_scan(void){
17
	if(services != NULL){
18
		int i;
19
		for(i = 0; services[i] != NULL; i++){
20
			free(services[i]->name);
21
			free(services[i]->exec);
22
			free(services[i]->pidfile);
23
			free(services[i]);
24
		}
25
		free(services);
3 nishi 26
		mk_log("Cleaning up the list");
2 nishi 27
	}
3 nishi 28
	services = malloc(sizeof(*services));
29
	services[0] = NULL;
2 nishi 30
 
31
	mk_log("Scanning the service directory.");
32
 
33
	DIR* dir = opendir(PREFIX "/etc/mokou");
34
	if(dir != NULL){
35
		struct dirent* d;
36
		while((d = readdir(dir)) != NULL){
37
			if(mk_endswith(d->d_name, ".conf")){
38
				char* path = mk_strcat(PREFIX "/etc/mokou/", d->d_name);
39
				char* str = mk_strcat("Reading ", path);
40
				mk_log(str);
41
				free(str);
3 nishi 42
 
43
				FILE* f = fopen(path, "r");
44
				if(f != NULL){
45
					struct stat s;
46
					stat(path, &s);
47
					char* buffer = malloc(s.st_size + 1);
48
					buffer[s.st_size] = 0;
49
					fread(buffer, s.st_size, 1, f);
50
					int i;
51
					int incr = 0;
52
 
53
					char* desc = NULL;
54
					char* exec = NULL;
55
					char* pidfile = NULL;
56
 
57
					for(i = 0;; i++){
58
						if(buffer[i] == '\n' || buffer[i] == 0){
59
							char oldc = buffer[i];
60
							buffer[i] = 0;
61
 
62
							char* line = buffer + incr;
63
 
64
							if(strlen(line) > 0 && line[0] != '#'){
65
								int j;
66
 
67
								for(j = 0; line[j] != 0; j++){
68
									if(line[j] == '='){
69
										line[j] = 0;
70
 
71
										char* key = line;
72
										char* value = line + j + 1;
73
										if(strcmp(key, "description") == 0){
74
											if(desc != NULL) free(desc);
75
											desc = mk_strdup(value);
76
										}else if(strcmp(key, "exec") == 0){
77
											if(exec != NULL) free(exec);
78
											exec = mk_strdup(value);
79
										}else if(strcmp(key, "pidfile") == 0){
80
											if(pidfile != NULL) free(pidfile);
81
											pidfile = mk_strdup(value);
82
										}
83
 
84
										break;
85
									}
86
								}
87
							}
88
 
89
							incr = i + 1;
90
							if(oldc == 0) break;
91
						}
92
					}
93
					fclose(f);
94
 
95
					bool bad = false;
96
					if(exec == NULL){
97
						char* log = mk_strcat(desc == NULL ? path : desc, ": Missing exec");
98
						mk_log(log);
99
						free(log);
100
						bad = true;
101
					}
102
					if(pidfile == NULL){
103
						char* log = mk_strcat(desc == NULL ? path : desc, ": Missing pidfile");
104
						mk_log(log);
105
						free(log);
106
						bad = true;
107
					}
108
 
109
					if(!bad){
110
						char* log = mk_strcat3("Adding ", desc == NULL ? path : desc, " to the list");
111
						mk_log(log);
112
						free(log);
113
					}
114
 
115
					if(desc != NULL) free(desc);
116
					if(exec != NULL) free(exec);
117
					if(pidfile != NULL) free(pidfile);
118
				}
119
 
2 nishi 120
				free(path);
121
			}
122
		}
123
		closedir(dir);
124
	}else{
125
		mk_log("Cannot open the directory.");
126
	}
127
}