#include <sys/wait.h>
#include <sys/poll.h>
#include <string.h>
+#include <signal.h>
#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;
else {
if (pid == 0) {
/*child process*/
+ currentPID = getpid();
main_child(argc, argv);
}
}
struct protoent *protocol;
struct sockaddr_in addr_server;
struct sockaddr_in addr_client;
- int sockfd;
int sockclient;
pthread_t idThread;
int clilen;
}
close(sockclient);
+ /*FIXME: dunno why, but after finishing connection from client we have TIME_WAIT in this socket*/
pthread_exit(0);
}
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);
+}
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();
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;