Signal handlers not working across different process groups - c

I am using execp under a child created using fork for which I have set new process group and have set it has foreground process group using tcsetpgrp
Here is my code for main
int main()
{
signal(SIGINT,SIG_IGN);
signal(SIGTSTP,SIG_IGN);
pid_t pid=fork();
if(pid==-1)
{
perror("Error in forking");
}
else if(pid == 0)
{
signal(SIGTTOU, SIG_IGN);
setpgid(getpid(),getpid());
//check denotes if process needs to be run in background or not
if(!check)
{
tcsetpgrp(STDOUT_FILENO,getpgid(getpid()));
tcsetpgrp(STDIN_FILENO,getpgid(getpid()));
}
signal(SIGTSTP, tstphandler);
signal(SIGINT,inthandler);
if(strlen(token)>0)
{
execer=execlp(token,token,args,(char*)NULL);
if (execer==-1)
{
printf("Error in execution);
}
exit(0);
}
}
else
{
//processes is a data structure which contains pid of
//processes and current status :- 0 = running in background
//, 1= dead , 2 = stopped in background , -1= in foreground.
if(check)
{
processes[pid_counter].pid=pid;
processes[pid_counter].dead=0;
}
if(!check && strlen(token)>0)
{
processes[pid_counter].pid=pid;
processes[pid_counter].dead=-1;
waitpid(pid,&status,0);
tcsetpgrp(STDOUT_FILENO,globalpgid);
tcsetpgrp(STDIN_FILENO,globalpgid);
processes[pid_counter].dead=1;
}
}
}
and these are my handlers
//SIGINT handler
void inthandler(int sig)
{
printf("int handler\n");
int i;
for(i=0;i<=pid_counter;i++)
{
if(processes[i].dead==-1)
{
kill(processes[i].pid,SIGKILL);
break;
}
}
}
//SIGTSTP handler
void tstphandler(int sig)
{
int i;
for(i=0;i<=pid_counter;i++)
{
if(processes[i].dead==-1)
{
processes[i].dead=2;
kill(processes[i].pid, SIGSTOP);
break;
}
}
tcsetpgrp(STDOUT_FILENO,globalpgid);
tcsetpgrp(STDIN_FILENO,globalpgid);
}
My problem is that these signal handlers are not being called from child processes. They are being called with their default action but not with my declared handler.

Related

why child can't send signal to parent

parent:
volatile int signalval = 0;
void signal_handle_c(int sig_num)
{
printf("child SIGUSR1 ok\n");
signalval = 1;
};
int main(int argc,char**argv){
struct sigaction act, oldact;
act.sa_handler = signal_handle_c;
act.sa_flags = SA_NOMASK;//SA_ONESHOT |
pid_t pid=fork();
if (pid < 0)
printf("error in fork!");
else if (pid == 0) {
printf("exec child!%s\n",argv[1]);
int res = execl(argv[1], "testc", NULL);
printf("res=%d", res);
}
else {
sleep(3);
sigaction(SIGUSR1, &act, &oldact);
printf("now parent\n");
}
waitpid(pid,NULL,0);
return 0;
}
child:
volatile int signal_pipe_end = 0;
void signal_handle_c(int sig_num)
{
printf("child IGUSR1 ok\n");
signal_pipe_end = 1;
};
int main(int argc, char *argv[])
{
printf("now child run!");
while (1)
{
printf("now child send siganl\n");
kill(getpid(), SIGUSR1);
sleep(1);
}
}
parent try to receive the SIGUSR1 signal from child.but none!
run result:
exec child!./testc
now child run!now child
now parent
i hope can printf "child SIGUSR1 ok".
why child "kill" send signal fail.

Why can't I catch SIGCHLD every time after fork?

I am trying to create 4 child processes and until the children die, parent should wait. I wrote a program but when I run this code, 1 out of 10, it can't catch the SIGCHLD from every child and my program goes to infinite loop after. It happens really rare but still..
Could you tell me why and how can I fix it?
Here is my code.
sig_atomic_t child_exit_status;
sig_atomic_t child_numbers = 0;
void clean_up(int signal_number, siginfo_t * info, void* context)
{
//printf("SIGCHILD from %d calling\n", info->si_pid);
waitpid(info->si_pid, &child_exit_status, 0);
child_numbers++;
}
int main(int argc, char **argv)
{
// SIGCHLD catcher
struct sigaction sigchld_action;
memset(&sigchld_action, 0, sizeof(sigchld_action));
sigchld_action.sa_sigaction = &clean_up;
sigchld_action.sa_flags = SA_SIGINFO;
sigaction(SIGCHLD, &sigchld_action, NULL);
int pid1, pid2, pid3, pid4;
printf("pid : %d\n", getpid());
pid1 = fork();
//child1
if(pid1 == 0)
{
printf("child1 %d, parent %d\n", getpid(), getppid());
}
else
{
pid2 = fork();
//child2
if(pid2 == 0)
{
printf("child2 %d, parent %d\n", getpid(), getppid());
}
else
{
pid3 = fork();
//child3
if(pid3 == 0)
{
printf("child3 %d, parent %d\n", getpid(), getppid());
}
else
{
pid4 = fork();
//child4
if(pid4 == 0)
{
printf("child4 %d, parent %d\n", getpid(), getppid());
}
else
{
while(child_numbers < 4)
{
}
printf("i got the signals.");
}
}
}
}
return 0;
}
I tried something new but it also doesn't work..
void clean_up(int signal_number, siginfo_t * info, void* context)
{
printf("SIGCHILD from %d calling\n", info->si_pid);
while (1)
{
int status;
pid_t pid = waitpid(-1, &status, WNOHANG);
if (pid <= 0)
{
break;
}
else
{
waitpid(pid, &status, 0);
break;
}
}
child_numbers++;
}

How to make multiple child proccess pause until a signal is sent

I have a program with three child processes all doing the exact same thing with a fourth child process told send signals while the main function waits until the three child processes are complete. I am using an alarm signal as well as two user signals to make the child process send output to a different terminal. I'm using the pause function to wait for a signal but the second I start the program it completes, not waiting at all. Am I using pause correctly?
int alarmR =0;
int SNMPR =0;
int reconR=0;
int child_pid[4];
char strD[100];
FILE *fpt[4];
void Alarmhandler(int sig);
void Reconfigure(int sig);
void SNMPhandler(int sig);
int which(int wait_ret, int child_proc[], int p);
void IT1(void);
void IT2(void);
void IT3(void);
void command(void);
void command(void)
{
signal(SIGALRM,Alarmhandler);
signal(SIGUSR1,Reconfigure );
signal(SIGUSR2,SNMPhandler );
char text;
int n;
printf("please enter a command from the following list\n");
printf("\tsn:Send a SNMP request\n\trn:Send a reconfiguration");
printf("script\n\tkn: shutdown process\n");
scanf("%c%d",&text,&n);
if(text == 'k')
{
printf("Terminated IT service %d\n",n);
n = n-1;
kill(child_pid[n],1);
}
else if(text=='s')
{
n = n-1;
kill(child_pid[n],SIGUSR2);
}
else if(text=='r')
{
n = n-1;
kill(child_pid[n],SIGUSR1);
}
}
void Alarmhandler(int sig)
{
alarmR =1;
}
void Reconfigure(int sig)
{
reconR =1;
}
void SNMPhandler(int sig)
{
SNMPR =1;
}
int which(int wait_ret, int child_proc[], int p)
{
int i;
for (i = 0; i < p; i++)
if (child_proc[i] == wait_ret)
return i;
return -1;
}
int main(int argc, char *argv[])
{
int ttyindex;
// int Terminal[4];
int term_cnt = 0;
int wait_r,x;
int process = 0;
int child_proc[4];
int es[3];
if(argc != 5) {
printf("Usage: ./lab7 1 2 3 4\n");
exit(1);
}
for(term_cnt =0; term_cnt <4;term_cnt ++)
{
ttyindex = -1;
ttyindex = atoi(argv[term_cnt+1]);
if (ttyindex < 1) {
printf("invalid terminal number %s\n", argv[term_cnt+1]);
exit(1);
}
sprintf(strD, "/dev/pts/%d", ttyindex);
child_proc[process] = fork();
if (child_proc[process] != 0)
{
// parent process
process++;
child_proc[process] = fork();
if (child_proc[process] != 0)
{
// parent process
process++;
child_proc[process] = fork();
if (child_proc[process] != 0)
{
// parent process
process++;
child_proc[process] = fork();
if(child_proc[process] !=0)
{
wait_r = wait(NULL);
x =which(wait_r, child_proc, process);
printf("Waited for %d (child %d) to finish.\n", wait_r,x);
if(WIFEXITED(wait_r))
{
es[x] = WEXITSTATUS(wait_r);
}
wait_r = wait(NULL);
x =which(wait_r, child_proc, process);
printf("Waited for %d (child %d) to finish.\n", wait_r,x);
if(WIFEXITED(wait_r))
{
es[x] = WEXITSTATUS(wait_r);
}
wait_r = wait(NULL);
x =which(wait_r, child_proc, process);
printf("Waited for %d (child %d) to finish.\n", wait_r,x);
if(WIFEXITED(wait_r))
{
es[x] = WEXITSTATUS(wait_r);
}
for(x = 0; x <3;x++)
{
if(es[x] == 0)
{
printf("\nJob well done IT specialist %d",(x+1));
printf(" Prepare for new attacks!\n");
}
else if(es[x] == 1)
{
printf("\nIT service %d compromised",(x+1));
printf(" , we are going out of business!\n");
}
else
{
printf("\nCall HR, we need a new");
printf(" cybersecurity expert for service");
printf(" %d\n",(x+1));
}
}
}
else
{
child_pid[3] = getpid();
command();
}
}
else
{
child_pid[2]=getpid();
IT3();
}
}
else
{
child_pid[1] = getpid();
IT2();
}
}
else
{
child_pid[0] = getpid();
IT1();
}
}
void IT1(void)
{
printf("i live");
signal(SIGALRM,Alarmhandler);
signal(SIGUSR1,Reconfigure );
signal(SIGUSR2,SNMPhandler );
clock_t start=0, end=0;
static int check = 0;
static int recona =0;
double t= 0;
int threat= 1;
srand48(time(NULL));
fprintf(fpt[1],"This is IT service 1\n");
while(1)
{
printf("%d %d %d",reconR,alarmR,SNMPR);
pause();
// alarm(1);
if(reconR == 1)
{
reconR =0;
if(recona >0)
{
fprintf(fpt[1],"Cannot reconfigure more than once.you are fired!");
exit(1);
}
if(threat < 16)
{
fprintf(fpt[1],"Threat level is not critical.you are fired");
exit(1);
}
fprintf(fpt[1],"Reconfiguring system to thwart attack-this may take a few seconds\n");
recona++;
}
if(alarmR == 1 )
{
if(check >0)
{
t =((double)(end - start))/CLOCKS_PER_SEC;
}
alarmR = 0;
if(recona >0)
{
threat --;
}
else
{
if (drand48() < 0.5)
{
threat++;
}
else if (threat > 1 && drand48() < 0.6)
{
threat--;
}
}
if(t <5)
{
fprintf(fpt[1],"Next report available in %f seconds\n",(5-t));
}
if(threat >15)
{
fprintf(fpt[1],"Intruder! Data stolen...");
exit(1);
}
else if(threat < 10 && recona >0)
{
fprintf(fpt[1],"Attack averted. Mission Complete");
exit(0);
}
}
if(SNMPR == 1)
{
if(check == 2)
{
end = clock();
t = ((double)(end - start))/CLOCKS_PER_SEC;
}
SNMPR = 0;
check = 1;
if(t <5)
{
fprintf(fpt[1],"Load to high. Threat is increased");
threat++;
t = 0;
}
else{
if(threat >=10)
fprintf(fpt[1],"Threat level is red\n");
else if(threat <10 && threat >=5)
fprintf(fpt[1],"Threat level is orange\n");
else
fprintf(fpt[1],"Threat level is green\n");
}
if(check ==1)
{
start = clock();
check = 2;
}
}
}
}
you can use the sigsuspend() system call to make a process wait for a signal, and kill() for sending signal to a process or process group.I hope by using both of these system calls, you can code your desired task.
you are putting a wait() on a process which has no child here
wait_r = wait(NULL);
This causes that wait() return immediately with error ECHILD and terminates the fourth child process. It's termination raises SIGCHLD causes pause() to return.

Why the child process will not exit normally?

Why is the following program will not work properly?
int cnt = 0;
void deal(int sig) {
++cnt;
}
int main() {
signal(SIGUSR1, deal);
pid_t child = fork();
if (child == 0) {
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
raise(SIGSTOP);
freopen("ab.out", "w", stdout);
printf("child : %d\n", cnt);
} else {
int app_status, app_sig, fd;
struct user_regs_struct app_reg;
struct rusage app_ruse;
waitpid(child, &app_status, 0);
printf("father: waitpid: %d\n", app_status);
while (1) {
ptrace(PTRACE_SYSCALL, child, NULL, NULL);
wait4(child, &app_status, 0, &app_ruse);
if (WIFEXITED(app_status)) {
printf("father: child exit normally, pid - %d\n", child);
break;
}
if (WIFSIGNALED(app_status) || (WIFSTOPPED(app_status)
&& WSTOPSIG(app_status) != SIGTRAP)) {
....
break;
}
ptrace(PTRACE_GETREGS, child, NULL, &app_reg);
if (app_reg.orig_rax == SYS_open && (fd = app_reg.rax) >= 0) {
kill(child, SIGUSR1);
}
}
}
return 0;
}
When run the program, got follow result
Why did the child process exit directly after receiving the signal?
The full code can got here.
https://pastebin.ubuntu.com/p/FddVZpfGH7/

Use loop to fork varying number of children

Most of the examples I come across show how to fork, like this one:
main()
{
int pid;
pid = fork();
// child
if (pid == 0)
{
...
}
// Parent
else if (pID > 0)
{
...
}
}
Some show how to fork 2 children, like so
pid = fork();
// Child
if ( pid==0 )
{
...
}
// Parent
else if ( pid>0 )
{
pid=fork();
// Second child
if ( pid==0 ){
...
}
}
And here is my attempt to fork 5 children...
pid = fork();
// Child
if ( pid==0 )
{
...
}
// Parent
else if ( pid>0 )
{
pid=fork();
// Second child
if ( pid==0 ){
...
}
// Parent
else if ( pid>0 )
{
pid=fork();
// Third child
if ( pid==0 ){
...
}
// Parent
else if ( pid>0 )
{
pid=fork();
// Fourth child
if ( pid==0 ){
...
}
// Parent
else if ( pid>0 )
{
pid=fork();
// Fifth child
if ( pid==0 ){
...
}
}
}
}
}
Question:
My point is that this is insanity. Is there a cleaner way to loop through and create a varying number of children (ie. specified via a command line argument)?
NOTE:
It is vitally important that the children distinguish themselves apart (ie. write "hi, I am child number 6"...) as my job involves each process signalling a different computer. Currently I am doing so using semaphores and an array of target computers. What I would rather want is increasing indeces 1..N for the processes.
Use a for loop. Each process will get it's own copy of the loop variable.
int pid;
int numKids = 5;
int procNum;
for(procNum = 0; procNum < numKids; procNum++) {
// Note: haven't included error handling...
pid = fork();
if (pid == 0) {
break;
}
}
if (pid == 0) {
// child specific stuff. procNum is set to the "child number"
}
else {
// parent stuff
}

Resources