Reputation: 125
I've been working on a shell pretty extensively, and now I'm trying to upgrade this code to report when a child has been terminated by a signal (other than SIGINT). I'm also trying to report a "core dump" if one happened in parenthesis (similar to bash).
...
if(test == -1){
cpid = fork();
if(cpid < 0){
//Fork wasn't successful
perror("fork");
free(argList);
return -1;
}
if(cpid == 0){
//We are the child!
close(pipefd[0]);
dup2(pipefd[1], 1);
execvp(args[0], args);
//execvp returned, wasn't successful
perror("exec");
fclose(stdin);
exit(127);
}
close(pipefd[1]);
//Have the parent wait for child to complete, if flags allow
if(strcmp(flags, "NOWAIT") != 0){
if(wait (&status) < 0){
//Wait wasn't successful
perror("wait");
}
else{
////////////////////////////////////////////////////////
//report if a child has been terminated by a signal other than SIGINT
if((WIFSTOPPED(status) && (WSTOPSIG(status) != SIGINT))){
printf("child terminated by signal [%d]\n", WSTOPSIG(status));
if(WCOREDUMP(status)){
printf("core dumped\n");
}
}
//////////////////////////////////////////////////////////
free(argList);
//since no child is running, return 0
return 0;
}
}
else{
//if a child is running, return the child's pid
return cpid;
}
}
...
I'm not really sure how to proceed with this. This is my first time working this extensively with the fork() command and my knowledge of the parent-child relationship is pretty shoddy, to tell the truth. I've searched for answers and the closest I've gotten is that SIGCHLD handles this sort of thing, but I need to be able to print out a specific number. something like:
printf("child terminated (%d)\n", 15 + signal);
edit*
I put what I think to be the correct implementation of what I want into the code, surrounded by ////////
Upvotes: 2
Views: 2841
Reputation: 754080
You collect the status of the child with your chosen variant of wait()
or waitpid()
— or on BSD wait3()
or wait4()
if you like — and then analyze the status.
POSIX provides:
WIFEXITED(status)
— returns true if the program exited under control.WEXITSTATUS(status)
— returns the exit status (0..255).WIFSIGNALED(status)
— returns true if the program exited because of a signal.WTERMSIG(status)
— returns the terminating signal.
WIFSTOPPED(status)
— if the child was stopped by a signal.
WSTOPSIG(status)
— the signal that stopped the child.WIFCONTINUED(status)
— if the child has continued since a stop signal.POSIX does not define, but many Unix-based implementations provide:
WCOREDUMP(status)
— returns true if a core was dumped.Upvotes: 5