Click to See Complete Forum and Search --> : Can't trap any signals from shutdown. It looks like none are sent.


bakerb
10-19-2001, 02:30 PM
Here comes a long one . . .

I am running Red Hat 7.1 with a 2.4.2 kernel on an x86 platform

I am trying to write a program that traps a signal from shutdown. The problem is, I never see a SIGTERM (the signal the man page says I should see) when I run shutdown (shutdown -t 2 -r +1 "Going Down Now"). I can use kill to send my program the iterm signal (kill -s SIGTERM <pid> ) and my program gets it. The man page says that shutdown sends SIGTERM to every proces with 1 <= pid <= MAX_INT. I just don't understand what is going on. I even ran strace to watch the signals comming in to my program. Nothing pops up during shutdown but when I send it by hand, it does. I have attached my code below. If anyone has any idea what I am doing wrong or if anyone knows where the source to shutdown is, I would really appreciate it.


// This program prints out every interupt
// (signal) thrown at it except SIGKILL
// and SIGSTOP
//
// Author: Brian Baker
// Email: brian_j_baker@yahoo.com

#include <stdio.h>
#include <unistd.h>
#include <signal.h>

// Called on any signal execpt SIGKILL and SIGSTOP
void sig_handler(int signal)
{
printf("Signal %d sent.\n", signal);
if (signal == SIGINT) exit(0); // for ctrl-c for easy kill
}

int main ()
{
int i;
struct sigaction sig_struct; // holds info about signal handler for kernel

sig_struct.sa_handler = &sig_handler;
sigemptyset(&sig_struct.sa_mask);
sig_struct.sa_flags = 0;


for (i = 1; i < 32; i++) // For each type of signal . . . (see /usr/include/bits/signum.h)
{
if (i == SIGKILL || i == SIGSTOP) continue; // Except for SIGKILL and SIGSTOP
if ( sigaction(i, &sig_struct, NULL ) == -1 ) // Tell the kernel i can trap this signal
{
perror("Error informing kernel of what signals we want to trap");
exit(1);
}
}

printf("Running\n");
while ( 1 ); // keep the program alive so we can get interupts
return 0;
}




Thank you so much for your time.

Keep on rockin' in the free world,
- bakerb

[ 19 October 2001: Message edited by: bakerb ]

bwkaz
10-19-2001, 04:59 PM
Maybe the ouput buffer isn't getting flushed? I would think so, but maybe add an fflush(stdout) after the printf() in the signal handler.

Maybe.....

bakerb
10-19-2001, 05:27 PM
Thanks bwkaz.

It turns out that is not the problem. I was under the impression that shutdown sent sigterm the moment it was called. That is not the case. The next time you shut down your computer watch the shutdown messages. The first one says "Sending all processes the TERM signal." I've seen this tons of times and it didn't click that this is actually wjen shutdown sends the TERM signal. (Smacks own head)

I would have hoped that shutdown would send some kind of signal the instant it was called. This, unfortunately, is not the case.

Thanks for taking the time to read my post.

Keep on rocking in the free world.
bakerb

bwkaz
10-19-2001, 10:31 PM
Aaah, I see.

Yeah, I had noticed it saying that, I thought that that was when you were looking for the output and not seeing it.