]> Nishi Git Mirror - mokou.git/commitdiff
it works
authorNishi <nishi@nishi.boats>
Sat, 7 Sep 2024 09:55:12 +0000 (09:55 +0000)
committerNishi <nishi@nishi.boats>
Sat, 7 Sep 2024 09:55:12 +0000 (09:55 +0000)
git-svn-id: file:///raid/svn-personal/mokou/trunk@10 35d6bad2-6c5c-c749-ada2-a2c82cb3bd79

Mokou/mk_service.h
Mokou/server.c
Mokou/service.c

index 189663dcb42f2ab52d2282940e631430b380ac4c..23132b9c857f330a255f03c3f9eaace8fa5dd928 100644 (file)
@@ -18,5 +18,6 @@ void mk_service_scan(void);
 int mk_start_service(const char* name);
 int mk_stop_service(const char* name);
 void mk_start_services(void);
+void mk_resurrect_services(void);
 
 #endif
index c0c0b97385f83dfa8151deb2e227971a4b493e88..0e3e51039ed2069a77eabb1c022ac355d85d3665 100644 (file)
@@ -7,10 +7,12 @@
 #include "mk_util.h"
 #include "mk_log.h"
 
+#include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <poll.h>
+#include <time.h>
 
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -48,7 +50,6 @@ int mk_server_init(void){
 void mk_server_loop(void){
        struct sockaddr_un cun;
        socklen_t socklen = sizeof(cun);
-       char* ver = mk_strcat3("R", mk_get_version(), "\n");
        char cbuf[2];
        cbuf[1] = 0;
        char* str = malloc(1);
@@ -56,11 +57,19 @@ void mk_server_loop(void){
        struct pollfd pollfds[16 + 1];
        pollfds[0].fd = server;
        pollfds[0].events = POLLIN | POLLPRI;
+       time_t trigger = time(NULL) + 5;
        while(1){
-               mk_log("Waiting for the connection");
                int r = poll(pollfds, 16 + 1, 5000);
+               if(!(r > 0 && pollfds[0].revents & POLLIN)){
+                       time_t t = time(NULL);
+                       if(t >= trigger){
+                               mk_resurrect_services();
+                               trigger = time(NULL) + 5;
+                       }
+                       usleep(1000);
+                       continue;
+               }
                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'){
@@ -71,7 +80,7 @@ void mk_server_loop(void){
                                                send(cli, mk_errors[err], strlen(mk_errors[err]), 0);
                                                send(cli, "\n", 1, 0);
                                        }else{
-                                               send(cli, "Mok\n", 4, 0);
+                                               send(cli, "MOk\n", 4, 0);
                                        }
                                }else if(str[0] == 'D'){
                                        int err = mk_stop_service(str + 1);
@@ -80,8 +89,11 @@ void mk_server_loop(void){
                                                send(cli, mk_errors[err], strlen(mk_errors[err]), 0);
                                                send(cli, "\n", 1, 0);
                                        }else{
-                                               send(cli, "Mok\n", 4, 0);
+                                               send(cli, "MOk\n", 4, 0);
                                        }
+                               }else if(str[0] == 'R'){
+                                       mk_service_scan();
+                                       send(cli, "MOk\n", 4, 0);
                                }else{
                                        send(cli, PROTOCOL_ERROR, strlen(PROTOCOL_ERROR), 0);
                                }
@@ -97,6 +109,5 @@ void mk_server_loop(void){
                }
                close(cli);
        }
-       free(ver);
-       free(ver);
+       free(str);
 }
index 958c0ecac9e420630855c853d8e5d18bcebf315f..4fe7857cc6295c04744b26f956b1856aa05801aa 100644 (file)
@@ -396,3 +396,28 @@ void mk_start_services(void){
                mk_start_service(services[i]->name);
        }
 }
+
+void mk_resurrect_services(void){
+       int i;
+       bool re = false;
+       for(i = 0; services[i] != NULL; i++){
+               if(!services[i]->stopped){
+                       bool alive = false;
+
+                       FILE* f = fopen(services[i]->pidfile, "r");
+                       if(f != NULL){
+                               unsigned long long pid;
+                               fscanf(f, "%llu", &pid);
+                               fclose(f);
+                               alive = kill(pid, 0) == 0;
+                       }
+                       if(!alive){
+                               if(!re){
+                                       mk_log("Resurrection");
+                                       re = true;
+                               }
+                               mk_start_service(services[i]->name);
+                       }
+               }
+       }
+}