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

Control/main.c
Mokou/service.c

index 6acc5fe11f7e4d1b67ee6c1f34b536f195a96621..14eb32a13bb43ef47feb9d311894e80c659201e2 100644 (file)
@@ -62,6 +62,7 @@ int main(int argc, char** argv){
                if(connect(sock, (struct sockaddr*)&sun, sizeof(sun)) == -1){
                        fprintf(stderr, "Connection failure: %s\n", strerror(errno));
                        close(sock);
+                       return 1;
                }
                send(sock, msg, strlen(msg), 0);
                
index 4fe7857cc6295c04744b26f956b1856aa05801aa..fa0ac2457d79c87178b4ab13fdefea42575243c2 100644 (file)
@@ -208,7 +208,8 @@ const char* mk_errors[] = {
        "Failed to start",
        "Service is dead",
        "Bad signal",
-       "Could not stop the service"
+       "Could not stop the service",
+       "Could not run the stop command"
 };
 
 int mk_stop_service(const char* name){
@@ -256,22 +257,7 @@ int mk_stop_service(const char* name){
                                        log = mk_strcat("Sending SIG", sys_signame[sig]);
                                        mk_log(log);
                                        free(log);
-                                       bool dead = false;
                                        kill(pid, sig);
-                                       for(i = 0; i < 3; i++){
-                                               if(kill(pid, 0) == -1){
-                                                       mk_log("Process died");
-                                                       dead = true;
-                                                       break;
-                                               }else{
-                                                       mk_log("Process is still alive");
-                                               }
-                                               if(i != 2) sleep(1);
-                                       }
-                                       if(!dead){
-                                               mk_log("Could not kill the process");
-                                               return 6;
-                                       }
                                }
                        }else{
                                char** pargv = malloc(sizeof(*pargv));
@@ -298,6 +284,46 @@ int mk_stop_service(const char* name){
                                                if(srv->exec[i] == 0) break;
                                        }
                                }
+
+                               bool fail = false;
+                               pid_t pid = fork();
+                               if(pid == 0){
+                                       int n = open("/dev/null", O_RDWR);
+                                       dup2(n, 1);
+                                       dup2(n, 2);
+                                       execvp(pargv[0], pargv);
+                                       _exit(-1);
+                               }else{
+                                       int status;
+                                       waitpid(pid, &status, 0);
+                                       if(WEXITSTATUS(status) != 0) fail = true;
+                               }
+
+                               for(i = 0; pargv[i] != NULL; i++) free(pargv[i]);
+                               free(pargv);
+
+                               if(fail){
+                                       mk_log("Failed to run stop command");
+                                       return 7;
+                               }
+                       }
+                       
+                       usleep(100);
+
+                       bool dead = false;
+                       for(i = 0; i < 3; i++){
+                               if(kill(pid, 0) == -1){
+                                       mk_log("Process died");
+                                       dead = true;
+                                       break;
+                               }else{
+                                       mk_log("Process is still alive");
+                               }
+                               if(i != 2) sleep(1);
+                       }
+                       if(!dead){
+                               mk_log("Could not kill the process");
+                               return 6;
                        }
 
                        srv->stopped = true;