signal's "siginfo_t* info" causes segmentation fault - c

I am making a program containing a "Server.c" which waits a client to send it a SIGUSR1 msg 10 times, then dies, and a "client.c" which sends a SIGUSR1 msg to the server.
The problem is that if I try to access the siginfo_t* info, I get a segmentation fault.
Note that this is being tested on a Debian ssh server on which I do not have high permissions.
Node that this code works fine on Ubuntu.
Can siginfo_t *info fail due to permission issues? Or is there another issue causing this portability problem. As far as I know libc should be fairly standard throughout any linux distro, possibly unix.
Any Ideas?
Thanks
server.c
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
int counter = 0;
pid_t *clients = 0;
void on_signal(int signo, siginfo_t *info, void * context)
{
puts("SIGNAL RECEIVED");
assert(clients);
clients[counter] = info->si_pid;
++counter;
}
int main()
{
struct sigaction action;
sigset_t set;
int recieveflag = 0;
clients = (pid_t*)malloc(10 * sizeof(pid_t));
sigemptyset(&set);
sigaddset(&set, SA_SIGINFO);
memset(&action, 0, sizeof(struct sigaction));
action.sa_sigaction = on_signal;
sigaction(SIGUSR1, &action, 0);
while (counter < 10) {
//sigprocmask(SIG_BLOCK, &set, 0);
sigsuspend(&set);
}
puts("I'm done!");
return 0;
}
client.c:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
int main(int argc, const char** argv)
{
int server_id;
assert(argc == 2);
server_id = atoi(argv[1]);
assert(server_id > 0);
kill(server_id, SIGUSR1);
return 0;
}
I tried editing server.c to:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
int counter = 0;
pid_t *clients = 0;
void on_signal(int sig)
{
puts("SIGNAL RECEIVED");
}
int main()
{
struct sigaction action;
sigset_t set;
int recieveflag = 0;
clients = (pid_t*)malloc(10 * sizeof(pid_t));
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
memset(&action, 0, sizeof(struct sigaction));
action.sa_flags = SA_SIGINFO;
action.sa_handler = on_signal;
sigaction(SIGUSR1, &action, 0);
while (counter < 10) {
sigprocmask(SIG_BLOCK, &set, 0);
sigsuspend(&set);
++counter;
}
puts("I'm done!");
return 0;
}
now it no longer receives the SIGUSR1 event at all.

The basic behavior of sigaction is to call a simple callback like : void (*sa_handler)(int);. So if you want to use the sigaction handle with 3 parameters void (*sa_sigaction)(int, siginfo_t *, void *);, you must set the sa_flags field of your struct sigaction with the flag SA_SIGINFO. Take a look of the man page : http://www.kernel.org/doc/man-pages/online/pages/man2/sigaction.2.html who is clear.

Related

reading a line from stdin until a signal is sent

I want to write some code that takes input from stdin, and prints the input on the next line, until a signal is sent, the signal being SIGINT in this case. So far I have this code which just prints a message when SIGINT is sent:
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
bool signalSent = false;
void flag(int signal) {
signalSent = true;
}
int main(int argc, char** argv) {
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = flag;
sa.sa_flags = SA_RESTART;
sigaction(SIGINT, &sa, 0);
while (true) {
while (!signalSent) {
usleep(500000);
}
printf("signal sent\n");
signalSent = false;
}
return 0;
}
I tried using fgets() to get the input from stdin and print to stdout, but when I enter ^C (SIGINT) I have to press enter, but I want it to send the message ("signal sent\n"); as soon as it is pressed like the code I currently have. Code using fgets():
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
bool signalSent = false;
void flag(int signal) {
signalSent = true;
}
int main(int argc, char** argv) {
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = flag;
sa.sa_flags = SA_RESTART;
sigaction(SIGINT, &sa, 0);
while (true) {
while (!signalSent) {
char buffer[80];
char* t = fgets(buffer, sizeof(buffer), stdin);
printf("%s", t);
fflush(stdout);
}
printf("signal sent\n");
signalSent = false;
}
return 0;
}
Output from code:
For reference I want my output to look like the picture below. Im very new to using signals in C as well.
As detailed in the manual pages for sigaction(2) and signal(7), the SA_RESTART flag causes certain system calls, including read(2), to automatically restart after the signal handler has ended.
It seems that you do not want this behaviour.
fgets is implemented by way of read, and can fail for the same reasons it can. Without SA_RESTART, while waiting on input from a "slow" device (this includes terminals), these functions will set errno to EINTR if interrupted by a signal.
Note that what can and cannot be done in a signal handler is complicated.
Consider using sig_atomic_t objects, declared as volatile, for these types of flags.
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
static volatile sig_atomic_t signal_sent = 0;
void flag(int signal) {
(void) signal;
signal_sent = 1;
}
int main(void) {
struct sigaction sa = { .sa_handler = flag };
sigaction(SIGINT, &sa, 0);
while (!signal_sent) {
char buffer[80];
if (fgets(buffer, sizeof buffer, stdin))
printf("%s", buffer);
else if (errno == EINTR && signal_sent) {
puts("signal sent");
signal_sent = 0;
} else
return 1;
}
}
Example of running this program (^D to terminate):
$ ./a.out
foo
foo
^Csignal sent
^Csignal sent
bar
bar
hello wor^Csignal sent

Is there a way to execute code in the parent process after the child, which is created using fork and execl?

I am having a parent process play that creates a fork and runs foo using execl
Code for play.c
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
if (fork() == 0) {
execl("./foo", "", NULL);
} else {
wait(0);
write(STDOUT_FILENO, "in parent after waiting", 5);
}
printf("outside everything");
return 0;
}
Code for foo.c
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void signal_handler() {
write(STDOUT_FILENO, "\nBye!\n", 6);
exit(1);
}
int main() {
struct sigaction sa;
sa.sa_handler = signal_handler;
sigaction(SIGINT, &sa, NULL);
while (1) {
printf("Wasting time...%d \n", getpid());
sleep(1);
}
return 0;
}
My questions are,
Why aren't the print statements after the wait(0) statement executed?
Why isn't the signal handler in the child process triggered when Ctrl + C?
You should ensure that the sa_flags and sa_mask fields of struct sigaction are set. You can initialize them — struct sigaction sa = { 0 }; will probably do the job. Or you can use sigemptyset(&sa.sa_mask); and sa.sa_flags = 0; to assign values. Or you can set them to some non-zero value. Not setting sa_flags means you've no idea what operation you requested. You also need a signal handler in play.c. You need to ignore SIGINT before the fork(), then in the child re-enable the signal before executing foo. The write() in the parent does not print much; it may once have printed "\nBar!\n" or something.
Here's some working code.
play.c
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void)
{
struct sigaction sa = { 0 };
sa.sa_handler = SIG_IGN;
sigaction(SIGINT, &sa, NULL);
if (fork() == 0)
{
sa.sa_handler = SIG_DFL;
sigaction(SIGINT, &sa, NULL);
execl("./foo", "", NULL);
exit(1);
}
else
{
wait(0);
printf("in parent after waiting\n");
}
printf("outside everything\n");
return 0;
}
foo.c
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static void signal_handler(int signum)
{
char message[] = "\nBye (XX)\n";
message[6] = signum / 10 + '0';
message[7] = signum % 10 + '0';
write(STDOUT_FILENO, message, sizeof(message) - 1);
exit(1);
}
int main(void)
{
struct sigaction sa = { 0 };
sa.sa_handler = signal_handler;
sigaction(SIGINT, &sa, NULL);
while (1)
{
printf("Wasting time...%d \n", getpid());
sleep(1);
}
return 0;
}
Example output
$ play
Wasting time...11383
Wasting time...11383
Wasting time...11383
Wasting time...11383
^C
Bye (02)
in parent after waiting
outside everything
$

Unable to reassign signal handler second time

I have a simple code which uses two signal handlers for "Segmentation fault" signal. First one works when signal happens and after longjmp, I do reassigning of handler with second one for that signal. Unfortunately, the flow of code doesn't reach necessary handler and I still get "Segmentation fault".
#include <unistd.h>
#include <sys/syscall.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>
int i;
int ci;
jmp_buf m_env_buffer;
jmp_buf max_env_buffer;
void handler(int signum){
printf("sigsegv on i:[%d]", i);
ci = (++i);
longjmp(m_env_buffer,1);
}
void top_handler(int signum){
printf("sigsegv on max i:[%d]", i);
longjmp(max_env_buffer,10);
}
int main(void) {
signal(SIGSEGV, handler);
char * pstart = "loremipsum";
int m_cell = 0;
char m_cell_v;
int point;
point = setjmp(m_env_buffer);
if(point == 0){
for(i=0; ;i--){
m_cell_v = pstart[i];
}
}
//this wasn't invoked
signal(SIGSEGV, top_handler);
point = setjmp(max_env_buffer);
if(point == 0){
for(i=ci; ;i++){
char cur = pstart[i];
if(cur==10)
printf("\n");
printf("%c",cur);
}
}
puts("finish");
return 0;
}
signal() should not be used. It has unreliable semantics. On your system the default action (termination) is performed when the second SIGSEGV is received. The second call to signal() has effectively no effect.
You should use sigaction() instead. In your case you can use the following function instead of signal():
void set_signal (int signum, void (*handler)(int))
{
struct sigaction act;
act.sa_handler = handler;
sigemptyset (&act.sa_mask);
act.sa_flags = SA_NODEFER;
act.sa_restorer = NULL;
sigaction (signum, &act, NULL);
}
In the future, read the documentation at your disposal. A good ressource is the glibc manual. It has a good chapter on signal handling.

Child process in another group gets <defunct> after handling a signal

I have got a problem with dealing with some signals. I have got child processes with different group ID than a parent, and those kids are dealing with SIGALRM which parent is sending to them. Sometimes it works good, but sometimes it is called only once, and a child is becoming . Do you know where my mistake could be?
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
int workers=2;
void sigalrm_handler(int signum)
{
printf("caught\n");
}
int main(int argc, char** argv) {
//region variables
char message[255]="Hello world!";
struct timespec sendSleep = {1,0};
struct sigaction new_a;
int pid;
for(int i=0; i<workers;i++)
{
pid=fork();
if(pid==0)
{
break;
}
}
if(pid==0)
{
//region sigalrm_handler
new_a.sa_handler = sigalrm_handler;
sigaction(SIGALRM, &new_a, NULL);
//endregion
while(1)
{
}
}
else
{
//region sigalrm_handler
new_a.sa_handler = SIG_IGN;
sigaction(SIGALRM, &new_a, NULL);
//endregion
nanosleep(&sendSleep,NULL);
for(int i=0; i<strlen(message); i++)
{
killpg(getpgrp(),SIGALRM);
printf("signal sent\n");
nanosleep(&sendSleep,NULL);
}
}
}
It is necessary to run it few times to get:
signal sent
signal caught
signal sent
signal sent
signal sent

multiple timer to one process (without linking to rt)

is there any way to register multiple timer to a single process? I have tried following code, yet without success. (Use "gcc -lrt" to compile it...). Program output nothing, which should atleast print "test". Is it possibly due to the dependence to linking to rt?
#define TT_SIGUSR1 (SIGRTMAX)
#define TT_SIGUSR2 (SIGRTMAX - 1)
#define TIME_INTERVAL_1 1
#define TIME_INTERVAL_2 2
#include <signal.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <linux/unistd.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sched.h>
#include <signal.h>
#include <setjmp.h>
#include <errno.h>
#include <assert.h>
timer_t create_timer(int signo) {
timer_t timerid;
struct sigevent se;
se.sigev_signo = signo;
if (timer_create(CLOCK_REALTIME, &se, &timerid) == -1) {
fprintf(stderr, "Failed to create timer\n");
exit(-1);
}
return timerid;
}
void set_timer(timer_t timerid, int seconds) {
struct itimerspec timervals;
timervals.it_value.tv_sec = seconds;
timervals.it_value.tv_nsec = 0;
timervals.it_interval.tv_sec = seconds;
timervals.it_interval.tv_nsec = 0;
if (timer_settime(timerid, 0, &timervals, NULL) == -1) {
fprintf(stderr, "Failed to start timer\n");
exit(-1);
}
return;
}
void install_sighandler2(int signo, void(*handler)(int)) {
struct sigaction sigact;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = SA_SIGINFO;
//register the Signal Handler
sigact.sa_sigaction = handler;
// Set up sigaction to catch signal first timer
if (sigaction(signo, &sigact, NULL) == -1)
{
printf("sigaction failed");
return -1;
}
}
void install_sighandler(int signo, void(*handler)(int)) {
sigset_t set;
struct sigaction act;
/* Setup the handler */
act.sa_handler = handler;
act.sa_flags = SA_RESTART;
sigaction(signo, &act, 0);
/* Unblock the signal */
sigemptyset(&set);
sigaddset(&set, signo);
sigprocmask(SIG_UNBLOCK, &set, NULL);
return;
}
void signal_handler(int signo) {
printf("receiving sig %d", signo);
}
int main()
{
printf("test");
timer_t timer1 = create_timer(TT_SIGUSR1);
timer_t timer2 = create_timer(TT_SIGUSR2);
set_timer(timer1, TIME_INTERVAL_1);
set_timer(timer2, TIME_INTERVAL_2);
install_sighandler2(TT_SIGUSR1, signal_handler);
install_sighandler(TT_SIGUSR2, signal_handler);
while (1)
;
return 0;
}
missing in create_timer: se.sigev_notify=SIGEV_SIGNAL;
two install_sighandler methods. Install_sighandler2 has a compler warning about mismatching pointer types.
while (1) will hog your processor, sleep.
#define TT_SIGUSR1 (SIGUSR1)
#define TT_SIGUSR2 (SIGUSR2)
#define TIME_INTERVAL_1 1
#define TIME_INTERVAL_2 2
#include <signal.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <linux/unistd.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sched.h>
#include <signal.h>
#include <setjmp.h>
#include <errno.h>
#include <assert.h>
#include <stdlib.h>
timer_t create_timer(int signo) {
timer_t timerid;
struct sigevent se;
se.sigev_notify=SIGEV_SIGNAL;
se.sigev_signo = signo;
if (timer_create(CLOCK_REALTIME, &se, &timerid) == -1) {
perror("Failed to create timer");
exit(-1);
}
return timerid;
}
void set_timer(timer_t timerid, int seconds) {
struct itimerspec timervals;
timervals.it_value.tv_sec = seconds;
timervals.it_value.tv_nsec = 0;
timervals.it_interval.tv_sec = seconds;
timervals.it_interval.tv_nsec = 0;
if (timer_settime(timerid, 0, &timervals, NULL) == -1) {
perror("Failed to start timer");
exit(-1);
}
return;
}
void install_sighandler(int signo, void(*handler)(int)) {
sigset_t set;
struct sigaction act;
/* Setup the handler */
act.sa_handler = handler;
act.sa_flags = SA_RESTART;
sigaction(signo, &act, 0);
/* Unblock the signal */
sigemptyset(&set);
sigaddset(&set, signo);
sigprocmask(SIG_UNBLOCK, &set, NULL);
return;
}
void signal_handler(int signo) {
printf("receiving sig %d\n", signo);
}
int main()
{
printf("test\n");
timer_t timer1 = create_timer(TT_SIGUSR1);
timer_t timer2 = create_timer(TT_SIGUSR2);
install_sighandler(TT_SIGUSR1, signal_handler);
install_sighandler(TT_SIGUSR2, signal_handler);
set_timer(timer1, TIME_INTERVAL_1);
set_timer(timer2, TIME_INTERVAL_2);
while (1) sleep(1);
return 0;
}

Resources