From: gumartinm Date: Wed, 18 Jan 2012 06:58:29 +0000 (+0100) Subject: SIGTERM handler for Java fork process. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=8cc97cf664496924abd371a90341d1d1308d84b3;p=JavaForFun SIGTERM handler for Java fork process. --- diff --git a/JavaFork/Daemon/javafork b/JavaFork/Daemon/javafork index 6ad9212..7277198 100755 Binary files a/JavaFork/Daemon/javafork and b/JavaFork/Daemon/javafork differ diff --git a/JavaFork/Daemon/javafork.c b/JavaFork/Daemon/javafork.c index 6d78c0c..3ad2346 100644 --- a/JavaFork/Daemon/javafork.c +++ b/JavaFork/Daemon/javafork.c @@ -17,12 +17,22 @@ #include #include #include +#include #include "javafork.h" +pid_t currentPID; +int sockfd; + int main (int argc, char *argv[]) { pid_t pid; + if (signal(SIGTERM, sigterm_handler) == SIG_ERR) + { + perror ("\nerror signal handler"); + return -1; + } + if ((pid = fork()) == -1) { perror("fork: "); return -1; @@ -30,6 +40,7 @@ int main (int argc, char *argv[]) { else { if (pid == 0) { /*child process*/ + currentPID = getpid(); main_child(argc, argv); } } @@ -42,7 +53,6 @@ int main_child (int argc, char *argv[]) { struct protoent *protocol; struct sockaddr_in addr_server; struct sockaddr_in addr_client; - int sockfd; int sockclient; pthread_t idThread; int clilen; @@ -137,6 +147,7 @@ void *serverThread (void * arg) { } close(sockclient); + /*FIXME: dunno why, but after finishing connection from client we have TIME_WAIT in this socket*/ pthread_exit(0); } @@ -345,3 +356,20 @@ int fork_system(int socket, char *command) { return 0; } + + +void sigterm_handler(int sig) +{ + if (currentPID != getpid()) { + //Do nothing + return; + } + if (signal (SIGTERM, SIG_IGN) == SIG_ERR) + { + syslog (LOG_ERR, "signal desactivation failed"); + } + + close (sockfd); + /*TODO: kill child processes and release allocate memory*/ + exit (0); +} diff --git a/JavaFork/Daemon/javafork.h b/JavaFork/Daemon/javafork.h index d6321a0..7f03332 100644 --- a/JavaFork/Daemon/javafork.h +++ b/JavaFork/Daemon/javafork.h @@ -5,3 +5,4 @@ void *serverThread (void *arg); int daemonize(const char *pname, int facility, int option); int main_child (int argc, char *argv[]); int fork_system(int socket, char *command); +void sigterm_handler(); diff --git a/JavaFork/JavaExample/javafork-example/src/main/java/de/fork/java/MainJavaFork.java b/JavaFork/JavaExample/javafork-example/src/main/java/de/fork/java/MainJavaFork.java index bed6b6a..a422cdd 100644 --- a/JavaFork/JavaExample/javafork-example/src/main/java/de/fork/java/MainJavaFork.java +++ b/JavaFork/JavaExample/javafork-example/src/main/java/de/fork/java/MainJavaFork.java @@ -21,7 +21,7 @@ public class MainJavaFork { final ByteArrayOutputStream stdoutByteOut = new ByteArrayOutputStream(); final PrintStream stdout = new PrintStream(stdoutByteOut); - final String command = "ls -lah; ls -lah bbb aaa ccc; ls -lah"; + final String command = "/home/gustavo/github/JavaForFun/JavaFork/Daemon/script.sh"; final ByteArrayOutputStream stderrByteOut = new ByteArrayOutputStream(); final PrintStream stderr = new PrintStream(stderrByteOut); int result;