Do you know where I can see a list of signals and functions that cannot be used alongside sleep() command?
For example, you can see this code:
// this program presents how to block signal SIGINT
// while running in critical region
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
static void sig_int(int);
int main(void) //6
{
sigset_t newmask, oldmask, zeromask;
if (signal(SIGINT, sig_int) == SIG_ERR)
fprintf(stderr,"signal(SIGINT) error");
sigemptyset(&zeromask);
sigemptyset(&newmask);
sigaddset(&newmask, SIGINT);
/* block SIGINT and save current signal mask */
if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
fprintf(stderr,"SIG_BLOCK error");
/* critical region of code */
printf("In critical region: SIGINT will be blocked for 3 sec.\n");
printf("Type Ctrl-C in first 3 secs and see what happens.\n");
printf("Then run this program again and type Ctrl-C when 3 secs elapsed.\n");
fflush(stdout);
sleep(3);
printf("end sleep");
/* allow all signals and pause */
if (sigsuspend(&zeromask) != -1)
fprintf(stderr,"sigsuspend error");
printf("after return from sigsuspend: ");
/* reset signal mask which unblocks SIGINT */
if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
fprintf(stderr,"SIG_SETMASK error");
/* and continue processing ... */
exit(0);
}
static void sig_int(int signo)
{
printf("\nIn sig_int: SIGINT\n"); fflush(stdout);
return;
}
The program doesn't wake up after the sleep(). Do you know why?
If you use strace, you can see what your program actually does
strace ./my-sig-program
If sleep never returns, I guess that task has received a SIGSTOP (this one can not be intercepted) or SIGTSTP (this one you can intercept with a signal handler), causing the OS the halt the entire process, until a SIGCONT is received.
Related
I'm not new to programming, but pretty new to Linux. I'm trying to use signals to asynchronously catch a push on a button, like this:
Run a worker thread which raises SIGUSR1 when the button is pushed.
Run a loop (main thread) around sigtimedwait() that will rotate info every two seconds (as long as the button is not pushed) or break (when the button is pushed).
According to the notes on sigtimedwait(), one should block the signals you want to wait for, then call sigtimedwait(). But I never see sigtimedwait() catching the blocked signals. I have run the code below in a few ways to see what happens with different scenarios:
Call to pthread_sigmask() disabled, call to signal() disabled,
result: programs exits with message "User defined signal 1".
Call to pthread_sigmask() disabled, call to signal() enabled, result:
message "Button 1 pressed sync1 hit" appears, sigtimedwait() always
returns EAGAIN.
Call to pthread_sigmask() enabled, call to signal() disabled, result:
message "Button 1 pressed" appears, sigtimedwait() always returns
EAGAIN.
Call to pthread_sigmask() enabled, call to signal() enabled, result
of course same as previous because the handler will not be called.
All as expected, except for the fact that sigtimedwait() doesn't seem to catch the signal when it's pending.
I've looked into similar code, e.g. this. But I don't understand how that particular code could work: SIGUSR1 isn't blocked, so raising that should immediately terminate the program (the default action for SIGUSR1).
It looks like I'm missing something here. What am I doing wrong? Or is the whole idea of using raise() in a worker thread wrong? I'm running this on a Raspberry Pi 3 with Raspbian Stretch (Debian 9.1), could there be a problem in that?
[I know printf() shouldn't be used in a signal handler, but for this purpose it works]
Any help appreciated, thx!
#include <stdio.h>
#include <stdlib.h>
#include <bcm2835.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
#define PIN_BUTTON1 RPI_V2_GPIO_P1_22 // GPIO #24
// Thread function
void* check_button1(void* param)
{
while (true)
{
if (bcm2835_gpio_lev(PIN_BUTTON1) == HIGH)
{
printf("Button 1 pressed ");
raise(SIGUSR1);
}
delay(250);
}
}
// Signal handler, if applied
volatile sig_atomic_t usr_interrupt = 0;
void sync1(int sig)
{
printf("sync1 hit ... ");
usr_interrupt = 1;
}
int main(int argc, char** argv)
{
if (!bcm2835_init())
{
printf("Failed to initialize BCM2835 GPIO library.");
return 1;
}
bcm2835_gpio_fsel(PIN_BUTTON1, BCM2835_GPIO_FSEL_INPT);
sigset_t sigusr;
sigemptyset(&sigusr);
sigaddset(&sigusr, SIGUSR1);
pthread_sigmask(SIG_BLOCK, &sigusr, NULL);
signal(SIGUSR1, sync1);
// Start the threads to read the button pin state
pthread_t th1;
pthread_create(&th1, NULL, check_button1, NULL);
// Create a two second loop
struct timespec timeout = { 0 };
timeout.tv_sec = 2;
usr_interrupt = 0;
int nLoopCount = 0;
while (true)
{
printf("Loop %d, waiting %d seconds ... ", ++nLoopCount, timeout.tv_sec);
int nResult = sigtimedwait(&sigusr, NULL, &timeout);
if (nResult < 0)
{
switch (errno)
{
case EAGAIN: printf("EAGAIN "); break; // Time out, no signal raised, next loop
case EINTR: printf("EINTR "); break; // Interrupted by a signal other than SIGCHLD.
case EINVAL: printf("EINVAL "); exit(1); // Invalid timeout
default: printf("Result=%d Error=%d ", nResult, errno); break;
}
printf("\n");
continue;
}
printf("Signal %d caught\n", nResult);
}
return 0;
}
ADDENDUM: In the meantime, I got this working by replacing raise(SIGUSR1) by kill(getpid(), SIGUSR1). Strange, because according to the manual raise(x) is equivalent to kill(getpid, x) in single-threaded programs and to pthread_kill(pthread_self(), x) in multi-threaded ones. Replacing raise(SIGUSR1) by pthread_kill(pthread_self, SIGUSR1) has no effect. If anyone could explain this to me ...
I had been asked a question, how to allocated memory to a pointer which has just caused a SIGSEVG signal, can we do it by using a signal handler ?
SIGSEGV signal causes the program to terminate. However, what you are asking can be done but I don't see any reason to continue the program execution after a memory violation has occurred.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <malloc.h>
int *p;
void sighandler(int);
int main()
{
signal(SIGSEGV, sighandler);
printf("\nProcess %d waits for someone to send it SIGSEGV\n",
getpid());
scanf("%d", p);
printf("%d", *p);
return 0;
}
void sighandler(int signum)
{
p = malloc(sizeof(int));
printf("\n\nProcess %d got signal %d\n\n", getpid(), signum);
signal(signum, SIG_DFL);
main();
}
However, this still causes SIGSEGV, but prints the entered number from 2nd attempt onwards.
I have been working on signal handling on Linux lately, and have read all the concepts related to signal handling. One question that's tinkering my head is that why the signal in the set of sigtimedwait() doesn't get delivered while the process is unblocked. My code is as follows :-
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
void sighandler1(int sig)
{
printf("SIGINT caught\n");
}
void sighandler2(int sig)
{
printf("SIGTSTP caught\n");
}
void sighandler3(int sig)
{
printf("SIGALRM caught\n");
}
int main()
{
sigset_t s1,s2;
struct sigaction act1,act2,act3;
int ret;
sigemptyset(&s1);// The bit-mask s1 is cleared
sigaddset(&s1,SIGINT);//Add SIGINT to the bit-mask s1
sigemptyset(&s2);// The bit-mask s2 is cleared
sigaddset(&s2,SIGALRM);//Add SIGALRM to the bit-mask s2
sigprocmask(SIG_BLOCK,&s2,NULL);//Signal(s) in s2 blocked
act1.sa_handler = sighandler1; //function pointer pointing to the signal handler
act1.sa_flags = 0;
sigaction(SIGINT,&act1,NULL); // installing the action
// for SIGINT
act2.sa_handler = sighandler2; //function pointer pointing to another signal handler
act2.sa_flags = 0; // no flags
sigaction(SIGTSTP,&act2,NULL); // installing the action
// for SIGTSTP
act3.sa_handler = sighandler3; //function pointer pointing to another signal handler
act3.sa_flags = 0; // no flags
sigaction(SIGALRM,&act3,NULL); // installing the action for SIGALRM
sigprocmask(SIG_SETMASK,&s1,NULL); //Signals in s1 blocked and other signals unblocked
printf("sigprocmask() called with SIG_SETMASK on s1,which contains SIGINT\n");
printf("Blocked on sigtimedwait() with s1\n");
if(sigtimedwait(&s1,NULL,NULL) < 0)
{
if(errno == EINTR)
printf("Some other signal caught\n");
}
printf("This is a process. You can pass signal to it\n");
while(1);
}
To be more clear with the question, I have called sigtimedwait in the above code with "s1" as the "set "parameter . This set contains only the signal SIGINT. As per the man page, sigtimedwait() blocks the process , until one of the signals in its set is delivered. I am all okay with that statement. But Why is the SIGINT handler not called when I pass SIGINT to unblock the process? On the other hand, when I pass SIGALRM or SIGTSTP, which are not there in the set, EINTR is returned as expected, and also the signal handler gets called.
For anyone, who wants to observe the scenario, can execute the above code and then pass SIGINT to it. They will observe that the process is unblocked without the handler being called . Why is the handler not called? Am I misinterpreting any part of the man page of sigtimedwait()??
sigtimedwait seems to return the signal value, instead of the signal handler getting caught:
switch(sigtimedwait(&s1,NULL,NULL))
{
default:
printf ("Some other signal???");
break;
case SIGINT:
printf ("We got SIGINT\n");
break;
case -1:
perror ("sigtimedwait");
break;
}
My code is as follows,The first time ,alarm works very well and handler2() function can work. however, the alarm doesn't work after it implement "doMain()" in the "handler2()".
I mean after the second time print ""In main Pleasae input: \n"", handler2() doesn't wrok anymore.
I don't know why? My code as follows:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <setjmp.h>
#define MAX_LEN_COMM 64
jmp_buf jumper;
int stop =0; //o is not stop ,otherwise is stop;
void hanlder2();
void doMain();
void handler2()
{
int len_command = 0;
char character;
char commandStr[60];
printf("******************************\n");
printf("In Alarm Pleasae input: \n");
while((character=getchar())!='\n')
{
commandStr[len_command]=character;
len_command++;
}
commandStr[len_command]='\0';
printf("In Alarm input is %s\n",commandStr);
if (strcmp(commandStr,"N")==0||strcmp(commandStr,"n")==0){
printf("In Alarm You put no, we will stop alarm \n");
stop=1;
longjmp(jumper, 2);
}
else if(strcmp(commandStr,"Y")==0||strcmp(commandStr,"y")==0){
printf("In Alarm You put yes, we will continue alarm \n");
signal(SIGALRM, handler2);
alarm(5);
doMain();
}
}
void doMain(){
while(1){
setjmp(jumper);
if(stop==0){
signal(SIGALRM, handler2);
printf("return time %d\n",alarm(5));
}
int len_command = 0;
char character;
char commandStr[60];
printf("In main Pleasae input: \n");
while((character=getchar())!='\n')
{
commandStr[len_command]=character;
len_command++;
}
commandStr[len_command]='\0';
printf("In main input is %s\n",commandStr);
if (strcmp(commandStr,"N")==0||strcmp(commandStr,"n")==0){
printf("In main You put no\n");
}
else if(strcmp(commandStr,"Y")==0||strcmp(commandStr,"y")==0){
printf("In main You put yes\n");
}
}
}
void main()
{
doMain();
}
What you are doing is very wrong.
First, the signature of the handler should be void handler(int sig).
Second, there are very few functions that are safe to use within a handler so you should try to get out of a handler as quickly as possible and definitely not doing console i/o. You are using several unsafe library functions.
Lastly a signal handler is a function. It runs and returns to where your program was interrupted by the signal. During the time a handler runs signals of the same type are not deliverd. By calling doMain() from the handler - which is crazy - the handler never ends. Because it doesn't end you won't see any more alarm signals.
If I have two threads (Linux, NPTL), and I have one thread that is polling on one or more of file descriptors, and another is closing one of them, is that a reasonable action? Am I doing something that I shouldn't be doing in MT environment?
The main reason I consider doing that, is that I don't necessarily want to communicate with the polling thread, interrupt it, etc., I instead would like to just close the descriptor for whatever reasons, and when the polling thread wakes up, I expect the revents to contain POLLNVAL, which would be the indication that the file descriptor should just be thrown away by the thread before the next poll.
I've put together a simple test, which does show that the POLLNVAL is exactly what's going to happen. However, in that case, POLLNVAL is only set when the timeout expires, closing the socket doesn't seem to make the poll() return. If that's the case, I can kill the thread to make poll() restart to return.
#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <poll.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
static pthread_t main_thread;
void * close_some(void*a) {
printf("thread #2 (%d) is sleeping\n", getpid());
sleep(2);
close(0);
printf("socket closed\n");
// comment out the next line to not forcefully interrupt
pthread_kill(main_thread, SIGUSR1);
return 0;
}
void on_sig(int s) {
printf("signal recieved\n");
}
int main(int argc, char ** argv) {
pthread_t two;
struct pollfd pfd;
int rc;
struct sigaction act;
act.sa_handler = on_sig;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGUSR1, &act, 0);
main_thread = pthread_self();
pthread_create(&two, 0, close_some, 0);
pfd.fd = 0;
pfd.events = POLLIN | POLLRDHUP;
printf("thread 0 (%d) polling\n", getpid());
rc = poll(&pfd, 1, 7000);
if (rc < 0) {
printf("error : %s\n", strerror(errno));
} else if (!rc) {
printf("time out!\n");
} else {
printf("revents = %x\n", pfd.revents);
}
return 0;
}
For Linux at least, this seems risky. The manual page for close warns:
It is probably unwise to close file descriptors while they may be in
use by system calls in other threads in the same process. Since a
file descriptor may be reused, there are some obscure race conditions
that may cause unintended side effects.
Since you're on Linux, you could do the following:
Set up an eventfd and add it to the poll
Signal the eventfd (write to it) when you want to close a fd
In the poll, when you see activity on the eventfd you can immediately close a fd and remove it from poll
Alternatively you could simply establish a signal handler and check for errno == EINTR when poll returns. The signal handler would only need to set some global variable to the value of the fd you're closing.
Since you're on Linux you might want to consider epoll as a superior albeit non-standard alternative to poll.