]> Nishi Git Mirror - mokou.git/commitdiff
can start and stop service now
authorNishi <nishi@nishi.boats>
Sat, 7 Sep 2024 09:22:48 +0000 (09:22 +0000)
committerNishi <nishi@nishi.boats>
Sat, 7 Sep 2024 09:22:48 +0000 (09:22 +0000)
git-svn-id: file:///raid/svn-personal/mokou/trunk@8 35d6bad2-6c5c-c749-ada2-a2c82cb3bd79

Mokou/server.c
Mokou/service.c

index eac544dd6fdccf261cccc037342fda63855911c2..5569660200fe0e58068d1872e075adf904719f1c 100644 (file)
@@ -2,6 +2,7 @@
 
 #include "mk_server.h"
 
+#include "mk_service.h"
 #include "mk_version.h"
 #include "mk_util.h"
 #include "mk_log.h"
@@ -16,6 +17,7 @@
 
 struct sockaddr_un sun;
 int server;
+extern const char* mk_errors[];
 
 int mk_server_init(void){
        remove("/tmp/mokou.sock");
@@ -40,15 +42,56 @@ int mk_server_init(void){
        return 0;
 }
 
+#define PROTOCOL_ERROR "EProtocol Error\n"
+
 void mk_server_loop(void){
        struct sockaddr_un cun;
        socklen_t socklen = sizeof(cun);
-       char* ver = mk_strcat3("V", mk_get_version(), "\n");
+       char* ver = mk_strcat3("R", mk_get_version(), "\n");
+       char cbuf[2];
+       cbuf[1] = 0;
+       char* str = malloc(1);
+       str[0] = 0;
        while(1){
                mk_log("Waiting for the connection");
                int cli = accept(server, (struct sockaddr*)&cun, &socklen);
                send(cli, ver, strlen(ver), 0);
+               while(1){
+                       if(recv(cli, cbuf, 1, 0) <= 0) break;
+                       if(cbuf[0] == '\n'){
+                               if(str[0] == 'U'){
+                                       int err = mk_start_service(str + 1);
+                                       if(err != 0){
+                                               send(cli, "E", 1, 0);
+                                               send(cli, mk_errors[err], strlen(mk_errors[err]), 0);
+                                               send(cli, "\n", 1, 0);
+                                       }else{
+                                               send(cli, "Mok\n", 4, 0);
+                                       }
+                               }else if(str[0] == 'D'){
+                                       int err = mk_stop_service(str + 1);
+                                       if(err != 0){
+                                               send(cli, "E", 1, 0);
+                                               send(cli, mk_errors[err], strlen(mk_errors[err]), 0);
+                                               send(cli, "\n", 1, 0);
+                                       }else{
+                                               send(cli, "Mok\n", 4, 0);
+                                       }
+                               }else{
+                                       send(cli, PROTOCOL_ERROR, strlen(PROTOCOL_ERROR), 0);
+                               }
+                               free(str);
+                               str = malloc(1);
+                               str[0] = 0;
+                               break;
+                       }else if(cbuf[0] != '\r'){
+                               char* tmp = str;
+                               str = mk_strcat(tmp, cbuf);
+                               free(tmp);
+                       }
+               }
                close(cli);
        }
        free(ver);
+       free(ver);
 }
index 1080f4b7c1fedaa632d43c296bb2d25c0370a203..958c0ecac9e420630855c853d8e5d18bcebf315f 100644 (file)
@@ -19,6 +19,7 @@ struct mk_service** services = NULL;
 
 #ifdef __linux__
 const char* sys_signame[] = {
+       "",
        "HUP",
        "INT",
        "QUIT",
@@ -200,7 +201,7 @@ void mk_service_scan(void){
        }
 }
 
-const char* errors[] = {
+const char* mk_errors[] = {
        "Success",
        "No such service",
        "Service is alive",