Signal Handler Added. - redy to deploy

This commit is contained in:
Sagi Dayan 2014-04-02 14:37:41 +03:00
parent 91af9dcc91
commit bc1f7e0712

20
main.c
View file

@ -21,8 +21,10 @@ void FreeTokens(char** tokens) ;
void checkIfQuit(char* word); void checkIfQuit(char* word);
int getFileNameIndex(char** args); int getFileNameIndex(char** args);
void freeRemainderInArr(char** args, int beforLast); void freeRemainderInArr(char** args, int beforLast);
void sigHandler(int sig);
//GLOBAL INT - THIS WILL HOLD CHILD PID OR -1 IF THERE IS NO CHILD
int pidOfChild = -1;
int main() int main()
{ {
@ -37,7 +39,7 @@ int main()
if(getUserName(user_n) == ERROR) if(getUserName(user_n) == ERROR)
goto quit; goto quit;
signal(SIGINT, SIG_IGN); // ignore SIGINT signal(SIGINT, sigHandler); // Change the default SIGINT handler
while(1) while(1)
{ {
@ -114,8 +116,6 @@ int main()
} }
else if(pid == 0) // child else if(pid == 0) // child
{ {
signal(SIGINT, SIG_DFL); // set the child SIGINT handler to default! -> ^C will kill child but not father.
if(writeToFile == true) if(writeToFile == true)
{ {
int i_lastArg = getFileNameIndex(args); int i_lastArg = getFileNameIndex(args);
@ -286,3 +286,15 @@ void freeRemainderInArr(char** args, int beforLast)
free(args[beforLast]); free(args[beforLast]);
args[beforLast - 1] = NULL; args[beforLast - 1] = NULL;
} }
/**
* This function will be calld when the SIGINT is invoked.
* only if there is a child, the "father" will kill it.
* @param sig -
*/
void sigHandler(int sig)
{
if(pidOfChild >= 0)
kill(pidOfChild, SIGINT);
pidOfChild = -1;
}