Subject says it all.
I want to be able to defer to the default OS signal handler for certain signals after I am done with my cleanups.
E.g. if SIGINT is received I want to terminate the program after my custom cleanup routine and want to make use of the standard OS routine for that instead of callind _exit() or whatever.
I figure I can get a reference to the original signal handler somehow before overwriting it with sigaction(), but i don't know how to do that.
This is the code I have at the moment:
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
volatile sig_atomic_t sigterm_flag = 0;
volatile sig_atomic_t sighup_flag = 0;
volatile sig_atomic_t sig_default_flag = 0;
void sigact(int signum, siginfo_t *siginfo, void *ucontext)
{
switch(signum)
{
case SIGTERM:
sigterm_flag = 1;
break;
case SIGHUP:
sighup_flag = 1;
break;
default:
sig_default_flag = 1;
break;
}
}
int main(void)
{
struct sigaction sa;
sa.sa_sigaction = sigact;
sa.sa_flags = SA_SIGINFO;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
while (1) {
if (sigterm_flag == 1) {
sigterm_flag = 0;
printf("SIGTERM caught.\n");
}
if (sighup_flag == 1) {
sighup_flag = 0;
printf("SIGHUP caught.\n");
}
if (sig_default_flag == 1) {
sig_default_flag = 0;
printf("Other signal caught.\n");
}
sleep(1);
}
return 0;
}
EDIT:
Challenge solved based on suggestion from tuxlike in answer https://stackoverflow.com/a/64595945/8359654
Here is the final code:
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
volatile sig_atomic_t sigterm_flag = 0;
volatile sig_atomic_t sighup_flag = 0;
volatile sig_atomic_t sigint_flag = 0;
volatile sig_atomic_t sig_default_flag = 0;
void sigact(int signum, siginfo_t *siginfo, void *ucontext)
{
switch(signum)
{
case SIGTERM:
sigterm_flag = 1;
break;
case SIGHUP:
sighup_flag = 1;
break;
case SIGINT:
sigint_flag = 1;
break;
default:
sig_default_flag = 1;
break;
}
}
int main(int argc, char *argv[])
{
struct sigaction sa;
struct sigaction sa_default;
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = sigact;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa_default.sa_mask);
sa_default.sa_handler = SIG_DFL;
sa_default.sa_flags = 0;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
printf("Started %s as PID %ld\n", argv[0], (long int)getpid());
while (1) {
if (sigterm_flag == 1) {
sigterm_flag = 0;
printf("SIGTERM caught.\n");
}
if (sighup_flag == 1) {
sighup_flag = 0;
printf("SIGHUP caught.\n");
}
if (sigint_flag == 1) {
sigint_flag = 0;
printf("\nSIGINT caught.\n");
sigaction(SIGINT, &sa_default, NULL);
raise(SIGINT);
}
if (sig_default_flag == 1) {
sig_default_flag = 0;
printf("Other signal caught.\n");
}
sleep(1);
}
return 0;
}
(Note that you are missing sigemptyset(&sa.sa_mask); from your signal action structure initialization. It is a common, recommended practice to initially do a memset(&sa, 0, sizeof sa); to initialize all, even padding, to all-zeros, first.)
There is no userspace function for the default signal handler actions at all, the default handling happens in the kernel.
What you can do, is use sigaction() to reset the default action (sa.sa_handler = SIG_DFL; sa.sa_flags = 0;), then re-raise the signal using raise(signum), and return from the handler.
Both sigaction() and raise() are async-signal safe, and therefore perfectly acceptable to use in a signal handler.
Related
server
typedef struct s_server
{
unsigned char c;
int counter;
} t_server;
t_server server;
void ft_one(int sig, siginfo_t *info, void *context)
{
(void)sig;
(void)context;
server.c += server.counter;
server.counter /= 2;
if (server.counter == 0)
{
write(1, &server.c, 1);
server.c = 0;
server.counter = 128;
}
kill(info->si_pid, SIGUSR1);
}
void ft_zero(int sig, siginfo_t *info, void *context)
{
(void)sig;
(void)context;
server.counter /= 2;
if (server.counter == 0)
{
write(1, &server.c, 1);
server.c = 0;
server.counter = 128;
}
kill(info->si_pid, SIGUSR1);
}
int main(void)
{
struct sigaction act_one;
struct sigaction act_zero;
memset(&act_one, '\0', sizeof(act_one));
memset(&act_zero, '\0', sizeof(act_zero));
act_one.__sigaction_u.__sa_sigaction = ft_one;
act_zero.__sigaction_u.__sa_sigaction = ft_zero;
act_one.sa_flags = SA_SIGINFO;
act_zero.sa_flags = SA_SIGINFO;
if (sigaction(SIGUSR1, &act_one, NULL) < 0)
return (0);
if (sigaction(SIGUSR2, &act_zero, NULL) < 0)
return (0);
printf("server pid: %d\n", getpid());
server.c = 0;
server.counter = 128;
while (1)
pause();
return (0);
}
client
void empty(int sig, siginfo_t *info, void *context)
{
(void)sig;
(void)context;
(void)info;
}
int main(int argc, char **argv)
{
int i;
struct sigaction act;
char *str;
int serv_pid;
memset(&act, '\0', sizeof(act));
act.__sigaction_u.__sa_sigaction = empty;
act.sa_flags = SA_SIGINFO;
serv_pid = atoi(argv[1]);
str = argv[2];
if (sigaction(SIGUSR1, &act, NULL) < 0)
return (0);
while (*str)
{
i = 128;
while (i > 0)
{
if (i & (unsigned char)*str)
{
if (kill(serv_pid, SIGUSR1) == -1)
return (0);
}
else
{
if (kill(serv_pid, SIGUSR2) == -1)
return (0);
}
i /= 2;
pause();
}
str++;
}
return (0);
}
The screenshots show the result of work, programs. In the first case, I call the client several times. In the second with a lot of text. In both cases, apparently, the response signal from the server does not go away. Why? I can t understand
enter image description here.
enter image description here
You have a race condition in the client program. There is no guarantee that the signal will be delivered after the client calls pause.
The correct way is to use sigprocmask and sigsuspend. Block incoming SIGUSR1 with sigprocmask. After sending the bit, instead of calling pause, call sigsuspend with a mask that unblocks SIGUSR1. sigsuspend will return when the signal is caught, and block again.
sigset_t myset, oldset;
sigemptyset(&myset);
sigaddset (&myset, SIGUSR1);
sigprocmask(SIG_BLOCK, &myset, &oldset);
while (*str)
{
...
// pause() -- wrong! race condition!
sigsuspend(&oldset);
...
}
If I use the pthread_create() call in an infinite while loop in main, does it create multiple threads each time or will it only create the 2 threads that I need?
while(1){
pthread_create(&thread_1...);
pthread_create(&thread_2...);
}
it creates multiple threads each time.
you can use pthread_cancel to cancel any thread from the process.
Below is one of many ways to handle the termination of thread upon some event/timeout etc..
#include <stdio.h>
#include <signal.h> // for sigaction
#include <stdlib.h> // for exit and
#include <pthread.h> // for pthread
#include <string.h> // for memset
#define TIMEOUT 10
pthread_t tid1, tid2;
void* thread_function1(void *arg)
{
while(1)
{
printf(" thread_function1 invoked\n");
sleep(1); //avoid heavy prints
}
}
void* thread_function2(void *arg)
{
while(1)
{
printf(" thread_function2 invoked\n");
sleep(1); //avoid heavy prints
}
}
static void timer_handler(int sig, siginfo_t *siginfo, void *context)
{
printf("Inside handler function timeout happened \n");
if( sig == SIGALRM)
{
pthread_cancel(tid1);
pthread_cancel(tid2);
//exit(0); to exit from main
}
}
int main()
{
int count = 0;
void *status;
struct sigaction act;
memset (&act, '\0', sizeof(act));
sigemptyset(&act.sa_mask);
/* Use the sa_sigaction field because the handles has two additional parameters */
act.sa_sigaction = timer_handler;
/* The SA_SIGINFO flag tells sigaction() to use the sa_sigaction field, not sa_handler. */
act.sa_flags = SA_SIGINFO;
if (sigaction(SIGALRM, &act, NULL) < 0)
{
perror ("sigaction SIGALRM");
return 1;
}
alarm (TIMEOUT);
pthread_create(&tid1,NULL,thread_function1,NULL);
pthread_create(&tid2,NULL,thread_function2,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
printf(" MIAN ENDS ...BYE BYE \n");
return 0;
}
I'm trying to write a process in C/linux that ignores the SIGINT and SIGQUIT signals and exits for the SIGTERM. For the other signals it should write out the signal and the time. I'm having trouble cathing all the signals because i'm familiar only with catching 1 signal. If anyone could help me with this I'd appreciate it very much. Here is my code:
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
int done = 0;
void term(int signum)
{
if (signum == 15)
{
//printf("%d\n",signum);
printf("Received SIGTERM, exiting ... \n");
done = 1;
}
else
{
time_t mytime = time(0);
printf("%d: %s\n", signum, asctime(localtime(&mytime)));
printf("%d\n",signum);
}
}
int main(int argc, char *argv[])
{
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = term;
sigaction(SIGTERM, &action, NULL);
struct sigaction act;
memset(&act, 0, sizeof(struct sigaction));
act.sa_handler = SIG_IGN;
sigaction(SIGQUIT, &act, NULL);
sigaction(SIGINT, &act, NULL);
int loop = 0;
while(!done)
{
sleep(1);
}
printf("done.\n");
return 0;
}
Here is the easy way
void sig_handler(int signo)
{
if (signo == SIGINT)
printf("received SIGINT\n");
}
int main(void)
{
if (signal(SIGINT, sig_handler) == SIG_ERR)
and so on.
signal() and sighandler() is the least complicated way to do this.
Call signal for each signal that you want to catch. But as some have said earlier you can only catch certain signals. Best to have a way to gracefully shut the program down.
I have a C program that operates by responding to signals. Some signals cause the parent to fork. This allows other processing while the parent continues to respond to signals.
When the parent is sent a SIGTERM, I want the forked children to receive a SIGTERM as well. It isn't critical that the children finish handling the SIGTERM before the parent exits.
However, with the below code, the children do not receive a SIGTERM when I call kill(0, SIGTERM) from the parent. From the kill manpage, it looks like all of the children should get this SIGTERM.
I have a signal handler setup for the parent.
static volatile sig_atomic_t done = 0;
const int handled_signals[] = {SIGINT, SIGTERM, 0};
static void set_flag(int signum) {
switch (signum) {
/* Intentionally exclude SIGQUIT/SIGABRT/etc. as we want to exit
* without cleaning up to help with debugging */
case SIGTERM:
case SIGINT:
done = 1;
break;
default:
/* Should be unreachable, but just in case */
if (signal(signum, SIG_DFL) != SIG_ERR) {
raise(signum);
}
}
}
static int setup_handlers() {
struct sigaction sa;
sigset_t block_all;
int i;
/* Block all other signals while handling a signal. This is okay as
* our handler is very brief */
sigfillset(&block_all);
sa.sa_mask = block_all;
sa.sa_handler = set_flag;
for (i = 0; handled_signals[i] != 0; i++) {
if (sigaction(handled_signals[i], &sa, NULL)) {
err_log("Unable to set sigaction");
return 1;
}
}
/* Ignore SIGCHLD as we don't keep track of child success */
sa.sa_handler = SIG_IGN;
if (sigaction(SIGCHLD, &sa, NULL)) {
err_log("Unable to ignore SIGCHLD");
return 1;
}
return 0;
}
int main() {
int i;
sigset_t block_mask, orig_mask;
setup_handlers();
/* Block all of our handled signals as we will be using
* sigsuspend in the loop below */
sigemptyset(&block_mask);
for (i = 0; handled_signals[i] != 0; i++) {
sigaddset(&block_mask, handled_signals[i]);
}
if (sigprocmask(SIG_BLOCK, &block_mask, &orig_mask)) {
err_log("Error blocking signals");
}
while (!done) {
if (sigsuspend(&orig_mask) && errno != EINTR) {
err_log("sigsuspend");
}
}
/* Kill all children */
if (kill(0, SIGTERM)) {
err_log("kill(0, SIGTERM))");
}
}
After getting a signal that requires a fork, I do the following
static int unregister_handlers() {
struct sigaction sa;
int i;
sa.sa_handler = SIG_DFL;
for (i = 0; handled_signals[i] != 0; i++) {
if (sigaction(handled_signals[i], &sa, NULL)) {
err_log("sigaction unregister");
return 1;
}
}
if (sigaction(SIGCHLD, &sa, NULL)) {
err_log("sigaction SIGCHLD unregister");
return 1;
}
return 0;
}
void do_fork() {
switch(fork()) {
/* Error */
case -1:
err_log("fork");
break;
/* Child */
case 0:
if (unregister_handlers()) {
_exit(1);
}
do_fork_stuff();
_exit(0);
break;
/* Parent */
default:
break;
}
}
In do_fork_stuff, the child sleeps for 30 seconds. I then call kill(0, SIGTERM) from the parent. The children do not terminate.
What's the reason the children aren't getting the SIGTERM?
Ah, a little help from /proc/[PID]/status solved this.
$ cat /proc/31171/status
Name: myprog
SigQ: 2/16382
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000004203
SigIgn: 0000000000000000
SigCgt: 0000000180000000
The blocked signals (SigBlk) were the issue here. While the handlers were unregistered, the children were blocking SIGTERM. Removing the blocked signals resolved the issue.
I am trying to have two timers in my system for two different purpose but I dont understand why it doesnt work. Can somebody help me?Also, Should the handler code be a bare minumum so the tasks themselves dont interfere with the tick? Also can I define separate handler?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <netinet/in.h>
#include <linux/socket.h>
#include <time.h>
#define SIGTIMER (SIGRTMAX)
#define SIG SIGUSR1
static timer_t tid;
static timer_t tid2;
void SignalHandler(int, siginfo_t*, void* );
timer_t SetTimer(int, int, int);
int main(int argc, char *argv[]) {
struct sigaction sigact;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = SA_SIGINFO;
sigact.sa_sigaction = SignalHandler;
// set up sigaction to catch signal
if (sigaction(SIGTIMER, &sigact, NULL) == -1) {
perror("sigaction failed");
exit( EXIT_FAILURE );
}
// Establish a handler to catch CTRL+c and use it for exiting.
sigaction(SIGINT, &sigact, NULL);
tid=SetTimer(SIGTIMER, 1000, 1);
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = SignalHandler;
// set up sigaction to catch signal
if (sigaction(SIG, &sa, NULL) == -1) {
perror("sa failed");
exit( EXIT_FAILURE );
}
// Establish a handler to catch CTRL+c and use it for exiting.
sigaction(SIGINT, &sa, NULL);
tid2=SetTimer(SIG, 1000, 3);
for(;;);
return 0;
}
void SignalHandler(int signo, siginfo_t* info, void* context)
{
if (signo == SIGTIMER) {
printf("Command Caller has ticked\n");
}else if (signo == SIG) {
printf("Data Caller has ticked\n");
} else if (signo == SIGINT) {
timer_delete(tid);
perror("Crtl+c cached!");
exit(1); // exit if CRTL/C is issued
}
}
timer_t SetTimer(int signo, int sec, int mode)
{
static struct sigevent sigev;
static timer_t tid;
static struct itimerspec itval;
static struct itimerspec oitval;
// Create the POSIX timer to generate signo
sigev.sigev_notify = SIGEV_SIGNAL;
sigev.sigev_signo = signo;
sigev.sigev_value.sival_ptr = &tid;
if (timer_create(CLOCK_REALTIME, &sigev, &tid) == 0) {
itval.it_value.tv_sec = sec / 1000;
itval.it_value.tv_nsec = (long)(sec % 1000) * (1000000L);
if (mode == 1) {
itval.it_interval.tv_sec = itval.it_value.tv_sec;
itval.it_interval.tv_nsec = itval.it_value.tv_nsec;
}
else {
itval.it_interval.tv_sec = 0;
itval.it_interval.tv_nsec = 0;
}
if (timer_settime(tid, 0, &itval, &oitval) != 0) {
perror("time_settime error!");
}
}
else {
perror("timer_create error!");
return NULL;
}
return tid;
}
When you define your second timer with this tid2=SetTimer(SIG, 1000, 3);, your code configure this timer to be a one-shot-timer
if (mode == 1) {
itval.it_interval.tv_sec = itval.it_value.tv_sec; // here you arm the timer periodically (that's the meaning of it_interval
itval.it_interval.tv_nsec = itval.it_value.tv_nsec;
}
else {
itval.it_interval.tv_sec = 0; // here you arm the timer once
itval.it_interval.tv_nsec = 0;
}
If you configure the second timer with a mode=1, like this tid2=SetTimer(SIG, 1000, 4);, you'll obtain this on your console :
Command Caller has ticked
Data Caller has ticked
Command Caller has ticked
Data Caller has ticked
Command Caller has ticked
Data Caller has ticked
^CCrtl+c cached!: Success
And you can use different handlers for your timers because you're using different signals to catch their expiration.