I have a program that needs to use one custom signal handler for SIGINT and one for SIGCHILD.
I therefore added two struct sigaction called sigchildStruct and sigintStruct, and used sigaction to define two custo signal handling functions: handleSigInt() and handleSigChild()
First of all, is this the way you are supposed to do it; needing to register two separate sigaction structs?
Second of all, i need to block SIGCHILD during part of the code execution, i only want to receive the signal at one place in the code, so i used:
sigdelset(&sigchildStruct.sa_mask,SIGCHLD);
// Catch SIGCHILD signal here
sigaddset(&sigchildStruct.sa_mask,SIGCHLD);
Is that how you would do that? More importantly: there are two sigaction structs, but do i only need to change the sa_mask on one of them, or on both? Now i only changed the sa_mask on the struct called sigchildStruct, and not on the one called sigintStruct.
The rest of the code:
void handleSigchild(int sig) {
int childPID,childExitStatus;
printf("\nSIGCHILD received\n");
while ((childPID = waitpid(-1,&childExitStatus,WNOHANG)) >0) {
if (childExitStatus==2) {printf("Background process: %d%s",childPID," terminated by SIGINT\n");}
else if (childExitStatus!=0) {printf("Background process: %d%s",childPID," unknown command\n");}
printf("Background process: %d%s\n",childPID," has exited");
}
}
void handleSigInt(int sig) {
// SIGINT will be sent to all child processes so nothing needs to be done
printf("\nSIGINT received\n");
}
int main(int argc, char ** argv) {
// Sigaction for SIGCHILD
struct sigaction sigchildStruct;
sigchildStruct.sa_handler = &handleSigchild;
sigemptyset(&sigchildStruct.sa_mask);
sigaddset(&sigchildStruct.sa_mask,SIGCHLD);
sigchildStruct.sa_flags = SA_NOCLDSTOP;
if (sigaction(SIGCHLD, &sigchildStruct, 0) == -1) {
printf("Couldnt register signal handler: %s\n",strerror(errno));
exit(1);
}
// Sigaction for SIGINT
struct sigaction sigintStruct;
sigintStruct.sa_handler = &handleSigInt;
sigemptyset(&sigintStruct.sa_mask);
sigintStruct.sa_flags = SA_RESTART;
if (sigaction(SIGINT, &sigintStruct, 0) == -1) {
printf("Couldnt register signal handler: %s\n",strerror(errno));
exit(1);
}
sigdelset(&sigchildStruct.sa_mask,SIGCHLD);
// Catch SIGCHILD signal here
sigaddset(&sigchildStruct.sa_mask,SIGCHLD);
}
Because nobody answered, I'll try to do so. When I am dealing with signals I am using the function signal. This function takes two parameters: signal code and signal handler function. To start catching the signals I would put the code:
signal(SIGINT, handleSigInt);
signal(SIGCHILD, handleSigChild);
To stop catching the signal, I would execute:
signal(SIGINT, SIG_IGN);
signal(SIGCHILD, SIG_IGN);
Hope it helps.
There is several mistakes in your code.
1) It is not necessary to add SIGCHLD signal in the set of blocked signals during its handling. Every catched signal is blocked during its handling, so that there is no concurrent reentrance in the handling function :
struct sigaction sigchildStruct;
sigchildStruct.sa_handler = &handleSigchild;
sigemptyset(&sigchildStruct.sa_mask);
// sigaddset(&sigchildStruct.sa_mask,SIGCHLD); // unnecessary
sigchildStruct.sa_flags = SA_NOCLDSTOP;
2) You modified the sa_mask value of the struct after setting the action, which has no effect. If you want to block the delivery of signals for some time you need to modify the process signal mask. There is a function to do that : sigprocmask. So you can do something like :
sigset_t oldMask, newMask;
sigemptyset(&newMask);
sigaddset(&newMask,SIGCHLD);
sigprocmask(SIG_BLOCK,&newMask,&oldMask);
// now protected from SIGCHLD delivery, signal will be blocked...
sigprocmask(SIG_SETMASK,&oldMask,NULL);
// old protection reinstalled...
Related
I got this issue:
I made a program in c, where the main process creates some child process, and these, after a while, are able to send a signal to the main process:
the signal is sent with this code:
kill(getppid(), SIGUSR1);
and the main process, in the while loop is waiting the SIGUSR1 message...
everything is fine, but if I increase the child number and automatically the possibility to have more signals in the same time, the program crash printing the message:
User defined signal 1
the main code is like this:
void signalHandler(int sig, siginfo_t* info, void* vp) {
if (sig == SIGUSR1) {
printf("SIGUSR1 has arrived\n");
} else if (sig == SIGUSR2) {
printf("SIGUSR2 has arrived\n");
}
}
int main(int argc, char const *argv[]) {
struct sigaction action, old_action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_sigaction = signalHandler;
sigemptyset(&action.sa_mask);
action.sa_flags = SA_RESTART | SA_NODEFER;
while (1) {
sigaction(SIGUSR1, &action, &old_action);
sigaction(SIGUSR2, &action, &old_action);
}
}
I think the problem is that the signal is sent when the master is still working on the previous signal...but how can I do to fix this thing
thank you very much
It means that the child is sending the signal before the parent process was able to call sigaction() to configure the signal handler. When this happens, the default signal reaction to SIGUSR1 terminates the program:
SIGUSR1 P1990 Term User-defined signal 1
https://man7.org/linux/man-pages/man7/signal.7.html
However, there are many problems with your code. printf() is not safe to be called inside a signal handler (it's AS-Unsafe as defined by POSIX):
https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/functions/V2_chap02.html#tag_15_04_03
Also, using SA_NODEFER may create nested signals (another signal handler is called while some signal handler is running) but your program does not protect against a flood. Given enough children this will generate a stack overflow. Finally, the main program keeps running a non-stop infinite loop reconfiguring the signals, while it should have configured them only once outside the loop and blocked inside the loop (for example sigwait() or pselect()):
https://man7.org/linux/man-pages/man2/select.2.html
Finally, if you expect to run a large number of children that might flood the parent with signals, then it would be better to use the real time signal generation function (sigqueue()) rather than kill(). The difference is that with sigqueue(), all signals are queued and SA_NODEFER is not necessary to avoid discarding signals while some other signal handler is running:
https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/functions/V2_chap02.html#tag_15_04_02
Final conclusion: the code should be completely rewritten.
I'm trying to handle some of the C signals in the program I'm writing to add some safety nets in case of accidental closure etc. I've been trying to set up a signal handler for SIGINT using sigaction, and I can see the handler is being called, but it's not stopping the program from being killed anyway.
Trying to use signal with SIG_IGN works fine, however stops me being able to use custom handling. I've tried using signal(...) with my handler, and sigaction(...), and neither are actually working.
void handler(int SIG) {
//I know this is unsafe just for testing.
printf("Handled signal %d!\n", SIG);
//No actual logic for now.
}
void initializer() {
//...
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
//This doesn't work.
sigaction(SIGINT, &sa, NULL);
//This also didn't work.
// signal(SIGINT, handler);
//...
}
I believed that handling the signal would stop it by default, and everything I've seen online has indicated this should be enough, but I still receive:
^CHandled signal 2!
And then the process is killed...
I want use signal (SIGUSR1) to sync process in C. I want that parent process wainting for a signal, when receive this signal, send the same signal to child process. I wrote a short paragraph to stimulate reasoning, but it does not go away.
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void trataSIGUSR1(int sigNum) {
printf("SIGUSR1\n");
}
int main(void) {
pid_t pid;
struct sigaction sa;
pid = fork();
if (pid == 0) {
struct sigaction sa = {0};
sa.sa_handler = trataSIGUSR1;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGUSR1,&sa,NULL);
pause();
printf("This never execute");
} else {
printf("I'am father: %d!\n",getppid());
kill(0,SIGUSR1);
pause();
}
}
OUTPUT
I'am father: 12082!
User defined signal 1: 30
Simple hint is use pause() and kill() i.e pause() is for block the process execution until any signal received, once signal received then do_something() and kill() is for sending the SIGUSR1 signal.
Also when you use pause(), it will suspend the process until it received any signal and for that signal default action should be user defined ISR. from manual page of pause()
RETURN VALUE
pause() returns only when a signal was caught and the signal-catching function returned.
In this case pause() returns -1, and errno is set to EINTR.
Here is the sample required example code
//int nSIGINT = 0; /* declare variable of type volatile sigatomic_t */
volatile sigatomic_t nSIGINT;
void trataSIGINT(int sigNum) {
nSIGINT = 1;/* set the flag as needed */
}
int main(void ){
int pid;
pid=fork();/* create child process */
if(pid==0) {
//signal(SIGUSR1,trataSIGINT);/* instead of signal() use sigaction */
struct sigaction sa = {0}; /* initialize sa or fill all its members*/
sa.sa_handler = trataSIGINT;/* set the handler to trataSIGINT*/
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGUSR1,&sa,NULL); /* when child received SIGUSR1, trataSIGINT gets called */
pause(); /* wait until any signal received */
/* do_something_child() code, this you want to run only after receiving signal */
}
else {
/* do_something_parent() */
printf("parent about to send user signal to child\n");
kill(pid,SIGUSR1); /*send SIGUSR1 to child */
wait(0); /* wait till child completes */
}
return 0;
}
Side note, for setting a flag in trataSIGINT() when SIGUSR1 received, instead of declaring int nSIGINT = 0; declare flag variable as type of volatile sigatomic_t type.
From ISO/IEC 9899:2011 §7.14.1.1 The signal function
¶5 If the signal occurs other than as the result of calling the abort or
raise function, the behavior is undefined if the signal handler
refers to any object with static or thread storage duration that is
not a lock-free atomic object other than by assigning a value to an
object declared as volatile sig_atomic_t, or the signal handler
calls any function in the standard library other than the abort
function, the _Exit function, the quick_exit function, or the
signal function with the first argument equal to the signal number
corresponding to the signal that caused the invocation of the handler.
Furthermore, if such a call to the signal function results in a
SIG_ERR return, the value of errno is indeterminate.252)
252) If any signal is generated by an asynchronous signal handler, the
behavior is undefined.
I have the following program where I set the parent's process group and the child's process group, as well as giving the terminal control to the parent. Then, I run "cat" in the "background" child, which is supposed to generate SIGTTIN. However, the printf line in sighandler is not printed. Any ideas how to properly detect SIGTTIN in this case?
void sighandler(int signo){
printf("SIGTTIN detected\n");
}
int main() {
int status;
pid_t pid;
pid = fork ();
setpgid(0,0);
tcsetpgrp (STDIN_FILENO, 0);
signal(SIGTTIN, sighandler);
if (pid == 0)
{
setpgid(0,0);
execl ("cat", NULL);
_exit (EXIT_FAILURE);
}
else{
int status;
setpgid(pid,pid);
waitpid(-1, &status, 0);
}
return status;
}
Mariska,
For Parent Processes
As explained in the Stack Overflow post titled, "Catch Ctrl-C in C,":
The behavior of signal() varies across UNIX versions, and has also
varied historically across different versions of Linux. Avoid its use:
use sigaction(2) instead.
As described in the Linux Programmer's Manual, you should use sigaction():
The sigaction() system call is used to change the action taken by a
process on receipt of a specific signal.
Try this:
#include<stdio.h>
#include <signal.h>
static void handler(int signum)
{
/* Take appropriate actions for signal delivery */
printf("SIGTTIN detected\n");
}
int main()
{
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART; /* Restart functions if
interrupted by handler */
if (sigaction(SIGINT, &sa, NULL) == -1)
/* Handle error */;
/* Further code */
}
For Child Processes
There are a couple of points you should know when dealing with signal handlers for the child processes:
A forked child inherits the signal handlers from the parent
Because of the above, you need to implement some sort of signal handler for the parent and then change the signal handler before and after executing a child.
As explained in the Linux Programmer's Manual:
All process attributes are preserved during an execve(), except the following:
a. The set of pending signals is cleared (sigpending(2)).
b. The dispositions of any signals that are being caught are
reset to being ignored.
c. Any alternate signal stack is not preserved (sigaltstack(2)).
Thus, the exec() functions do not preserve signal handlers.
From the above, I am trying to show you that pressing Ctrl-C sends the signal to the parent process (unless you use exec()), and then the signals are automatically propagated to children. This is why we need to change the signal handler. Even when the child is currently "active", the parent will still receive signals before the child will.
Please let me know if you have any questions!
I have a thread in an application that has a loop like this:
...
while (1)
{
checkDatabase();
checkChildren();
sleep(3);
}
...
checkDatabase() is self-explanatory; checkChildren() simply calls waitpid(-1, &status, WNOHANG) to deal with child processes that have either exited or received a signal.
The application works fairly well, but it has default signal handling. The problem is that this parent process has a number of threads (don't worry about child processes for now) and I don't have any experience with synchronous signals, let alone in a POSIX threads application. I have used signal() before but apparently it's non-portable and it doesn't do what I need anyway. I have no experience at all with sigaction methods, and I can't find good documentation on how to fill in the structs and so on.
What I need to do is to synchronously catch terminating signals like SIGINT, SIGTERM and SIGQUIT in the above loop (and I need to ignore SIGPIPE altogether so that I can catch the EPIPE error from IO methods), so it would look like this:
...
while (1)
{
checkDatabase();
checkChildren();
checkForSignals();
sleep(3);
}
...
All other threads should not have anything to do with the signal; only the thread that executes this loop should be aware of it. And, obviously, it needs to be a non-blocking check so the loop doesn't block during its first iteration. The method called if a signal is found will sort out the other threads and destroy mutexes, and all that.
Could anyone please give me a heads-up? Many thanks.
(Following the question's comments, and for completeness, this solution tries to avoid signal handlers.)
It is possible to block signals from being raised through sigprocmask() (or, rather, pthread_sigmask() since you're using threads). From there on, the signals that were raised but blocked are available through sigpending().
Therefore, you could do something like (error checking omitted for brevity):
sigset_t blocked;
sigemptyset(&blocked);
sigaddset(&blocked, SIGINT);
sigaddset(&blocked, SIGTERM);
sigaddset(&blocked, SIGQUIT);
pthread_sigmask(SIG_BLOCK, &blocked, NULL); // Block SIGINT, SIGTERM and SIGQUIT.
signal(SIGPIPE, SIG_IGN); // Ignore SIGPIPE.
Then, later:
void checkForSignals(void)
{
sigset_t pending;
sigpending(&pending);
if (sigismember(&pending, SIGINT)) {
// Handle SIGINT...
}
if (sigismember(&pending, SIGTERM)) {
// Handle SIGTERM...
}
if (sigismember(&pending, SIGQUIT)) {
// Handle SIGQUIT...
}
}
Since sigpending() does not block, this seems to match your requirements.
Create a signal handler for SIGINT, SIGTERM and SIGQUIT, using the same function. In that signal function just set a flag that can be polled in your loop.
Something like this:
/* Global variable, will be set to non-zero if SIGINT, SIGTERM or SIGQUIT is caught */
int term_signal_set = 0;
void my_signal_handler(int)
{
term_signal_set = 1;
}
/* ... */
signal(SIGINT, my_signal_handler);
signal(SIGTERM, my_signal_handler);
signal(SIGQUIT, my_signal_handler);
signal(SIGPIPE, SIG_IGN); /* So functions return EPIPE */
while (1)
{
/* ... */
if (term_signal_set > 0)
break; /* Or do something else */
sleep(3);
}
In a multithreaded application receiving a signal, there is no predetermination, which thread receives the signal. Typical workaraounds include setting a global variable in the signal handler and checking it from a dedicated thread.
So in your case the signal handler (called from whatever thread) would just set something like a global variable for the signal received, and in CheckForSignals() you would test it.
sigaction is the way to go. man sigaction should help you. Here is an example from the web
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
struct sigaction act;
void sighandler(int signum, siginfo_t *info, void *ptr)
{
printf("Received signal %d\n", signum);
printf("Signal originates from process %lu\n",
(unsigned long)info->si_pid);
}
int main()
{
printf("I am %lu\n", (unsigned long)getpid());
memset(&act, 0, sizeof(act));
act.sa_sigaction = sighandler;
act.sa_flags = SA_SIGINFO;
sigaction(SIGTERM, &act, NULL);
// Waiting for CTRL+C...
sleep(100);
return 0;
}