adding some stuff

git-svn-id: file:///raid/svn-personal/mokou/trunk@3 35d6bad2-6c5c-c749-ada2-a2c82cb3bd79
This commit is contained in:
Nishi 2024-09-06 09:21:55 +00:00
parent da66e84ceb
commit 76db5066c2
5 changed files with 109 additions and 1 deletions

View File

@ -5,7 +5,7 @@ include $(PWD)/Platform/$(PLATFORM).mk
.PHONY: all clean
.SUFFIXES: .c .o
OBJS = main.o log.o service.o util.o
OBJS = main.o log.o service.o util.o version.o
all: mokou$(EXEC)

View File

@ -2,13 +2,20 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "mk_service.h"
#include "mk_log.h"
#include "mk_util.h"
#include "mk_version.h"
int main(int argc, char** argv){
if(getuid() != 0){
fprintf(stderr, "Run me as root.\n");
return 1;
}
char* log = mk_strcat3("Mokou version ", mk_get_version(), " starting up");
mk_log(log);
free(log);
mk_service_scan();
}

8
Mokou/mk_version.h Normal file
View File

@ -0,0 +1,8 @@
/* $Id$ */
#ifndef __MK_VERSION_H__
#define __MK_VERSION_H__
const char* mk_get_version(void);
#endif

View File

@ -2,8 +2,11 @@
#include "mk_service.h"
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "mk_log.h"
#include "mk_util.h"
@ -20,7 +23,10 @@ void mk_service_scan(void){
free(services[i]);
}
free(services);
mk_log("Cleaning up the list");
}
services = malloc(sizeof(*services));
services[0] = NULL;
mk_log("Scanning the service directory.");
@ -33,6 +39,84 @@ void mk_service_scan(void){
char* str = mk_strcat("Reading ", path);
mk_log(str);
free(str);
FILE* f = fopen(path, "r");
if(f != NULL){
struct stat s;
stat(path, &s);
char* buffer = malloc(s.st_size + 1);
buffer[s.st_size] = 0;
fread(buffer, s.st_size, 1, f);
int i;
int incr = 0;
char* desc = NULL;
char* exec = NULL;
char* pidfile = NULL;
for(i = 0;; i++){
if(buffer[i] == '\n' || buffer[i] == 0){
char oldc = buffer[i];
buffer[i] = 0;
char* line = buffer + incr;
if(strlen(line) > 0 && line[0] != '#'){
int j;
for(j = 0; line[j] != 0; j++){
if(line[j] == '='){
line[j] = 0;
char* key = line;
char* value = line + j + 1;
if(strcmp(key, "description") == 0){
if(desc != NULL) free(desc);
desc = mk_strdup(value);
}else if(strcmp(key, "exec") == 0){
if(exec != NULL) free(exec);
exec = mk_strdup(value);
}else if(strcmp(key, "pidfile") == 0){
if(pidfile != NULL) free(pidfile);
pidfile = mk_strdup(value);
}
break;
}
}
}
incr = i + 1;
if(oldc == 0) break;
}
}
fclose(f);
bool bad = false;
if(exec == NULL){
char* log = mk_strcat(desc == NULL ? path : desc, ": Missing exec");
mk_log(log);
free(log);
bad = true;
}
if(pidfile == NULL){
char* log = mk_strcat(desc == NULL ? path : desc, ": Missing pidfile");
mk_log(log);
free(log);
bad = true;
}
if(!bad){
char* log = mk_strcat3("Adding ", desc == NULL ? path : desc, " to the list");
mk_log(log);
free(log);
}
if(desc != NULL) free(desc);
if(exec != NULL) free(exec);
if(pidfile != NULL) free(pidfile);
}
free(path);
}
}

9
Mokou/version.c Normal file
View File

@ -0,0 +1,9 @@
/* $Id$ */
#include "mk_version.h"
const char* MOKOU_VERSION = "0.0";
const char* mk_get_version(void){
return MOKOU_VERSION;
}