From 76db5066c2d10293db9143f2c5290401b39a2fd0 Mon Sep 17 00:00:00 2001 From: Nishi Date: Fri, 6 Sep 2024 09:21:55 +0000 Subject: [PATCH] adding some stuff git-svn-id: file:///raid/svn-personal/mokou/trunk@3 35d6bad2-6c5c-c749-ada2-a2c82cb3bd79 --- Mokou/Makefile | 2 +- Mokou/main.c | 7 ++++ Mokou/mk_version.h | 8 +++++ Mokou/service.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++ Mokou/version.c | 9 +++++ 5 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 Mokou/mk_version.h create mode 100644 Mokou/version.c diff --git a/Mokou/Makefile b/Mokou/Makefile index 57ff221..656b1fb 100644 --- a/Mokou/Makefile +++ b/Mokou/Makefile @@ -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) diff --git a/Mokou/main.c b/Mokou/main.c index b7d4fe0..6a6c600 100644 --- a/Mokou/main.c +++ b/Mokou/main.c @@ -2,13 +2,20 @@ #include #include +#include #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(); } diff --git a/Mokou/mk_version.h b/Mokou/mk_version.h new file mode 100644 index 0000000..73862a3 --- /dev/null +++ b/Mokou/mk_version.h @@ -0,0 +1,8 @@ +/* $Id$ */ + +#ifndef __MK_VERSION_H__ +#define __MK_VERSION_H__ + +const char* mk_get_version(void); + +#endif diff --git a/Mokou/service.c b/Mokou/service.c index 58fa194..0439a83 100644 --- a/Mokou/service.c +++ b/Mokou/service.c @@ -2,8 +2,11 @@ #include "mk_service.h" +#include #include #include +#include +#include #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); } } diff --git a/Mokou/version.c b/Mokou/version.c new file mode 100644 index 0000000..b2fa85f --- /dev/null +++ b/Mokou/version.c @@ -0,0 +1,9 @@ +/* $Id$ */ + +#include "mk_version.h" + +const char* MOKOU_VERSION = "0.0"; + +const char* mk_get_version(void){ + return MOKOU_VERSION; +}