Home > Software engineering >  Linux signal: process exit if there's no handler
Linux signal: process exit if there's no handler

Time:03-23

I'm using signal to transmit messages within my program, I write code like this:

union sigval sv;
sv.sival_ptr = (void*) data;
pid_t pid = getpid();
if (sigqueue(pid, SIGUSR2, sv) != 0) {
    Log("failed: %d", errno);
    return 0;
} else {
    // do something
}

And I found that if I don't register a handler, my program will exit when calling sigqueue, as if I send a signal with SIGTERM, but im my code I send SIGUSR2. I checked the doc and nothing talks about the exit, so what did I do wrong?

P.S. The program might not register the handler by design.

CodePudding user response:

I checked the doc and nothing talks about the exit, so what did I do wrong?

You simply did not find the right documentation :-)

From man 7 signal you will get a list of signals and the default behavior, if no handler was set by user.

Stolen form the man page:

DESCRIPTION
       Linux  supports both POSIX reliable signals (hereinafter "standard sig-
       nals") and POSIX real-time signals.

   Signal dispositions
       Each signal has a current disposition, which determines how the process
       behaves when it is delivered the signal.

       The  entries  in the "Action" column of the table below specify the de-
       fault disposition for each signal, as follows:

       Term   Default action is to terminate the process.

       Ign    Default action is to ignore the signal.

       Core   Default action is to terminate the process and  dump  core  (see
              core(5)).

       Stop   Default action is to stop the process.

       Cont   Default  action  is  to  continue the process if it is currently
              stopped.

And from the table of default actions:

      Signal      Standard   Action   Comment
      ------------------------------------------------------------------------
 
       SIGABRT      P1990      Core    Abort signal from abort(3)
...
       SIGUSR1      P1990      Term    User-defined signal 1
       SIGUSR2      P1990      Term    User-defined signal 2

As you can see, if no other action was specified by user, the program terminates if it receives a SIGUSR1 or SIGUSR2.

CodePudding user response:

"I checked the doc and nothing talks about the exit".

The doc does talk about exit/termination. From the signal manual:

The entries in the "Action" column of the table below specify the default disposition for each signal, as follows:

Term Default action is to terminate the process.
 
...
 
Signal      Standard    Action   Comment
-----------------------------------------
...

SIGUSR2      P1990      Term     User-defined signal 2

What that tells us is that the default action for SIGUSR2 is to terminate the process. Which is exactly what you have observed.

  • Related