Subversion Repositories Mokou

Rev

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

Rev 2 Rev 3
Line 1... Line 1...
1
/* $Id: service.c 2 2024-09-05 18:31:51Z nishi $ */
1
/* $Id: service.c 3 2024-09-06 09:21:55Z nishi $ */
2
 
2
 
3
#include "mk_service.h"
3
#include "mk_service.h"
4
 
4
 
-
 
5
#include <stdio.h>
5
#include <dirent.h>
6
#include <dirent.h>
6
#include <stdlib.h>
7
#include <stdlib.h>
-
 
8
#include <string.h>
-
 
9
#include <sys/stat.h>
7
 
10
 
8
#include "mk_log.h"
11
#include "mk_log.h"
9
#include "mk_util.h"
12
#include "mk_util.h"
10
 
13
 
11
struct mk_service** services = NULL;
14
struct mk_service** services = NULL;
Line 18... Line 21...
18
			free(services[i]->exec);
21
			free(services[i]->exec);
19
			free(services[i]->pidfile);
22
			free(services[i]->pidfile);
20
			free(services[i]);
23
			free(services[i]);
21
		}
24
		}
22
		free(services);
25
		free(services);
-
 
26
		mk_log("Cleaning up the list");
23
	}
27
	}
-
 
28
	services = malloc(sizeof(*services));
-
 
29
	services[0] = NULL;
24
 
30
 
25
	mk_log("Scanning the service directory.");
31
	mk_log("Scanning the service directory.");
26
 
32
 
27
	DIR* dir = opendir(PREFIX "/etc/mokou");
33
	DIR* dir = opendir(PREFIX "/etc/mokou");
28
	if(dir != NULL){
34
	if(dir != NULL){
Line 31... Line 37...
31
			if(mk_endswith(d->d_name, ".conf")){
37
			if(mk_endswith(d->d_name, ".conf")){
32
				char* path = mk_strcat(PREFIX "/etc/mokou/", d->d_name);
38
				char* path = mk_strcat(PREFIX "/etc/mokou/", d->d_name);
33
				char* str = mk_strcat("Reading ", path);
39
				char* str = mk_strcat("Reading ", path);
34
				mk_log(str);
40
				mk_log(str);
35
				free(str);
41
				free(str);
-
 
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
 
36
				free(path);
120
				free(path);
37
			}
121
			}
38
		}
122
		}
39
		closedir(dir);
123
		closedir(dir);
40
	}else{
124
	}else{