0

I want to send a copy of some data to a signal handler function, take the simplified example:

signal(SIGUSR1, handler);
void handler(int sig){
    
    //...
    _exit(some_variable);
}

The goal is to stop the process and make it return some_variable value to the parent without making it a global variable. Mind that some_variable is local to the project that catches the signal, not the process that raises the signal.

I woud like to be able to both raise the signal in another process (kill) (not the parent btw) and to be able to do it from a console (kill) or using a script, and that's the main reason for using signal(), as it's good for both.

I understand that there are other ways of doing this but would really like to know if this can be done using signal() or a related method.

Other threads on the site suggest that this can't be done like this but they are quite old, perhaps some newer solution exists.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 2
    You can't safely use `exit()` in a signal handler, btw, just `_exit()` or `_Exit()`. See https://man7.org/linux/man-pages/man7/signal-safety.7.html – Shawn Nov 06 '20 at 10:53
  • 1
    Who will raise the `SIGUSR1` signal? Because if you use [`sigaction`](https://man7.org/linux/man-pages/man2/sigaction.2.html) with the `SA_SIGINFO` flag set, then depending on how the signal was raised would allow user-specified data to be passed to the signal handler. – Some programmer dude Nov 06 '20 at 10:53
  • 1
    And you can pass information to a signal handler with `sigaction()` using `SA_SIGINFO` and `sigqueue()`. – Shawn Nov 06 '20 at 10:57
  • I found a nice duplicate https://stackoverflow.com/questions/6970224/providing-passing-argument-to-signal-handler voted to close my own question, if that makes sense. – anastaciu Nov 08 '20 at 11:09

1 Answers1

1

If the signal is sent from the same process, just use a global variable. There is no basis/justification for objecting to using a global variable here, because signal dispositions are already global variables. That is, by committing yourself to installing a handler for SIGUSR1 and choosing a purpose for it, you've already chosen to have a singleton here; adding a variable as part of that singleton is no further sin. However in this case (same process) there's probably no reason to be using a signal handler anyway. There are all sorts of far-less-dangerous ways to communicate between parts of your program.

If the signal is being sent from a different process, you can pass small amounts of data (one integer) via union sigval queued with sigqueue, but that only works for sigqueue; it doesn't work if you want to be able to just use the kill command. And once you choose to require a special tool to interact, it makes a lot more sense to drop signals and use some better IPC mechanism like unix sockets, named pipes, shared memory with some synchronization primitives (semaphore or mutex+condvar) living inside it, etc.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711