I'm trying to implement my own shell that exits on ctrl+d (and not ctrl+C) To do that, I want my program to ignore SIGINT so I used
signal(SIGINT, SIG_IGN);
but I do want the programs I'm running in the shell to exit on ctrl+c, so after each fork(), in the child process, I'm trying to restore the default behavior using
signal(SIGINT, SIG_DFL);
but unfortunately SIGING is still ignored. What am I missing? thank you!
EDIT: As mentioned in the comments problem was that SIGINT did not reach the child process. If I understand correctly this is because exec will reset only the handlers for caught signals, and in my case I ignored sigint. to fix this I replaced signal(SIGINT, SIG_IGN) with my own handler that does nothing:
void sigint_signal_handler(int signum) {
printf("testttttt");
}
int register_signal_handling() {
struct sigaction sigint_action;
memset(&sigint_action, 0, sizeof(sigint_action));
sigint_action.sa_handler = sigint_signal_handler;
return sigaction(SIGINT, &sigint_action, NULL);
}
int main(void) {
if (register_signal_handling() == -1) {
perror("signal handler registration error");
exit(1);
}
return 0;
}
What happens now is weird - when I do ctrl+c I see "testttttt" printed, but it still exits the program. how is this possible if the handler is replaced? Thank you