Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are SIGRTMIN and SIGRTMAX safe to use in signal handlers?

Tags:

c++

c

linux

signals

The code I'm working with has a shared signal handler that switches on the signal number to handle it appropriately.

I'm adding a custom signal. Something like this

static void signal_handler (int s)
{
    if ( s == SIGTERM ) clean_up () ;

    else if ( s == SIGRTMIN+1 ) ; // do nothing
}

SIGRTMIN and SIGRTMAX are #defines of function calls which initialize static data (in the implementations I've seen on google code search)

Signal handlers are supposed to be non-reentrant. Does the use of static data in the accessor to SIGRTMIN and SIGRTMAX make these macros unsafe to use in a signal handler?

like image 392
wreckgar23 Avatar asked Nov 24 '25 14:11

wreckgar23


1 Answers

I don't know what implementation you are smoking, but in libc those functions seem to simply return a constant static variable most of the time.

You are right, there is a possible race between the two calls to init(), but that simply just initializes a static int twice to the same constant, hardly a worry.

And, while the static variable is not really that constant, they tell you to only modify said variable at the start of your program(and I think only pthread really modifies it that much).

So no need to worry about these functions(from allocrtsig.c glibc 2.14).

And, if you are really worried, just call SIGRTMIN once before you bind the signal handler. That will get the init() function out of the way.

/* Return number of available real-time signal with highest priority.  */
int __libc_current_sigrtmin (void)
{
#ifdef __SIGRTMIN
  if (!initialized)
    init ();
#endif
  return current_rtmin;
}
libc_hidden_def (__libc_current_sigrtmin)

/* Return number of available real-time signal with lowest priority.  */
int __libc_current_sigrtmax (void)
{
#ifdef __SIGRTMIN
  if (!initialized)
    init ();
#endif
  return current_rtmax;
}
libc_hidden_def (__libc_current_sigrtmax)

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!