0

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

ronihm
  • 65
  • 4
  • 1
    Exec will reset all handlers of the child to default anyway [see also](https://stackoverflow.com/questions/2333637/is-it-possible-to-signal-handler-to-survive-after-exec). It's more likely that SIGINT is not being delivered to the child in the first place. Try adding a handler to the shell and the executed program that prints which PID got the signal and see for yourself. –  Nov 11 '21 at 09:51
  • Thank you you are right, please see my edit – ronihm Nov 11 '21 at 12:25

0 Answers0