From fadd30a380a0ed11ceec1f5a9844ba5f8d72ef74 Mon Sep 17 00:00:00 2001 From: Nishi Date: Sat, 7 Sep 2024 10:18:01 +0000 Subject: [PATCH] functional git-svn-id: file:///raid/svn-personal/mokou/trunk@12 35d6bad2-6c5c-c749-ada2-a2c82cb3bd79 --- Control/main.c | 1 + Mokou/service.c | 58 +++++++++++++++++++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/Control/main.c b/Control/main.c index 6acc5fe..14eb32a 100644 --- a/Control/main.c +++ b/Control/main.c @@ -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); diff --git a/Mokou/service.c b/Mokou/service.c index 4fe7857..fa0ac24 100644 --- a/Mokou/service.c +++ b/Mokou/service.c @@ -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;