Home > Archive > Unix Programming > August 2006 > I can't trap SIGHUP.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
I can't trap SIGHUP.
|
|
| K-mart Cashier 2006-08-13, 7:01 pm |
| I'm using Suse Linux 9 point something for little adventure. What I'm
trying to do is have some stuff echo to the standard output when my
modem hangs up. However, I can't seem to automatically trap SIGHUP.
However, I can manually enter SIGHUP (ie kill -SIGHUP <process id> and
the program will work correctly. Here is what I have so far.
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
/*#define LOG "/home/cdalten/telfile"*/
static void log_tel(int signo) {
printf("la\n");
}
int main(void) {
printf("%d\n",getpid());
if(signal(SIGHUP, log_tel) == SIG_ERR) {
printf("nada \n");
}
for(;;)
pause();
return 0;
}
What I'm I missing?
| |
| Barry Margolin 2006-08-14, 4:01 am |
| In article <1155510257.958975.186710@m79g2000cwm.googlegroups.com>,
"K-mart Cashier" <cdalten@gmail.com> wrote:
> I'm using Suse Linux 9 point something for little adventure. What I'm
> trying to do is have some stuff echo to the standard output when my
> modem hangs up. However, I can't seem to automatically trap SIGHUP.
> However, I can manually enter SIGHUP (ie kill -SIGHUP <process id> and
> the program will work correctly. Here is what I have so far.
Do you have your output redirected when you try to test this? If stdout
is connected to the modem, there's no place for it to write when the
modem hangs up.
>
> #include <signal.h>
> #include <stdlib.h>
> #include <stdio.h>
> #include <unistd.h>
>
> /*#define LOG "/home/cdalten/telfile"*/
>
>
> static void log_tel(int signo) {
> printf("la\n");
> }
>
> int main(void) {
>
> printf("%d\n",getpid());
>
> if(signal(SIGHUP, log_tel) == SIG_ERR) {
> printf("nada \n");
> }
>
> for(;;)
> pause();
>
> return 0;
> }
>
> What I'm I missing?
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| K-mart Cashier 2006-08-14, 8:00 am |
|
Barry Margolin wrote:
> In article <1155510257.958975.186710@m79g2000cwm.googlegroups.com>,
> "K-mart Cashier" <cdalten@gmail.com> wrote:
>
>
> Do you have your output redirected when you try to test this? If stdout
> is connected to the modem, there's no place for it to write when the
> modem hangs up.
>
I never occurred to me to redirect the output. Call this inexperience
with the Linux Operating system or having a brain lapse. So would
something like freopen() work?
| |
| Lew Pitcher 2006-08-14, 8:00 am |
|
K-mart Cashier wrote:
> I'm using Suse Linux 9 point something for little adventure. What I'm
> trying to do is have some stuff echo to the standard output when my
> modem hangs up. However, I can't seem to automatically trap SIGHUP.
> However, I can manually enter SIGHUP (ie kill -SIGHUP <process id> and
> the program will work correctly. Here is what I have so far.
>
> #include <signal.h>
> #include <stdlib.h>
> #include <stdio.h>
> #include <unistd.h>
>
> /*#define LOG "/home/cdalten/telfile"*/
>
>
> static void log_tel(int signo) {
> printf("la\n");
> }
>
> int main(void) {
>
> printf("%d\n",getpid());
>
> if(signal(SIGHUP, log_tel) == SIG_ERR) {
> printf("nada \n");
> }
>
> for(;;)
> pause();
>
> return 0;
> }
>
> What I'm I missing?
One caution: most library functions are not safe to use within a signal
handler. IIRC, printf() is one of those unsafe functions.
Better to have your log_tel() function record data in a global variable
than to have it attempt to write data directly.
Something like
int got_signal = 0;
char signal_msg[64];
static void log_tel(int signo)
{
++got_signal;
sprintf(signal_msg,"got signal");
}
int main(void) {
printf("%d\n",getpid());
if(signal(SIGHUP, log_tel) == SIG_ERR) {
printf("nada \n");
}
for(;;)
{
if (got_signal) printf("Got signal - msg is %s\n",signal_msg);
pause();
}
return 0;
}
--
Lew Pitcher
| |
| Nils O. Selåsdal 2006-08-14, 8:00 am |
| K-mart Cashier wrote:
> Barry Margolin wrote:
>
> I never occurred to me to redirect the output. Call this inexperience
> with the Linux Operating system or having a brain lapse. So would
> something like freopen() work?
Your program should have the modem device as it's controlling tty if
you want to get a SIGHUP from it when it hangs up.
How is your program related to your modem ?
| |
| K-mart Cashier 2006-08-14, 10:00 pm |
|
Nils O. Sel=E5sdal wrote:
> K-mart Cashier wrote:
ut[color=darkred]
> Your program should have the modem device as it's controlling tty if
> you want to get a SIGHUP from it when it hangs up.
> How is your program related to your modem ?
Okay, here comes the "I work as a minimum wage monkey and couldn't get
into UC-Berkeley if my life depended on it" moment. How the heck do I
get my modem device to be the controlling terminal for this program? I
was thinking maybe two tin cans and a rope, but I don't think that
would work. Oh god, my head is starting to hurt.
| |
| Barry Margolin 2006-08-14, 10:00 pm |
| In article <1155604227.054972.212730@i3g2000cwc.googlegroups.com>,
"K-mart Cashier" <cdalten@gmail.com> wrote:
> Nils O. Selåsdal wrote:
>
> Okay, here comes the "I work as a minimum wage monkey and couldn't get
> into UC-Berkeley if my life depended on it" moment. How the heck do I
> get my modem device to be the controlling terminal for this program? I
> was thinking maybe two tin cans and a rope, but I don't think that
> would work. Oh god, my head is starting to hurt.
The usual way is to login to the system from a terminal dialed into the
modem. By default the login terminal will be the controlling terminal
for any processes created in that login session.
If this isn't how the modem got connected to your program, you can use
the TIOCSCTTY ioctl to make a particular open terminal the process's
controlling terminal.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| K-mart Cashier 2006-08-14, 10:00 pm |
|
Barry Margolin wrote:
> In article <1155604227.054972.212730@i3g2000cwc.googlegroups.com>,
> "K-mart Cashier" <cdalten@gmail.com> wrote:
>
>
> The usual way is to login to the system from a terminal dialed into the
> modem. By default the login terminal will be the controlling terminal
> for any processes created in that login session.
>
> If this isn't how the modem got connected to your program, you can use
> the TIOCSCTTY ioctl to make a particular open terminal the process's
> controlling terminal.
>
I might have to investigate this more....
I connect to a bbs in Michigan. The bbs has two dial-in numbers. That
is I can dial in and I get the shell prompt via xterm. However, I'm in
California. I use my local ISP to establish a ppp connection. I then
fire up xterm (through KDE) and at the $ sign, I type:
telnet> open <bbs name>
Would connecting this way possibly be the reason why the program
refuses to write anything when I hang-up my modem?
| |
| K-mart Cashier 2006-08-14, 10:00 pm |
|
>
> I might have to investigate this more....
>
> I connect to a bbs in Michigan. The bbs has two dial-in numbers. That
> is I can dial in and I get the shell prompt via xterm. However, I'm in
> California. I use my local ISP to establish a ppp connection. I then
> fire up xterm (through KDE) and at the $ sign, I type:
>
> telnet> open <bbs name>
>
> Would connecting this way possibly be the reason why the program
> refuses to write anything when I hang-up my modem?
Okay, I just tested if I had a controlling terminal on the bbs system
by using the ps command. The computer showed that my login session did
not have a controlling terminal. BTW, here is the "borrowed code" I
used. In other words, it's not my code.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
int s ;
pid_t f ;
if ((f = fork()) == 0)
{
FILE* fp ;
if (setsid() == (pid_t)-1) {
perror("setsid") ;
return EXIT_FAILURE ;
}
system("ps") ;
if ((fp = fopen("/dev/tty", "r")) != (FILE*)0) {
puts("Opened /dev/tty") ;
fclose(fp) ;
}
else
puts("Could not open /dev/tty") ;
return EXIT_SUCCESS ;
}
if (f == (pid_t)-1)
{
perror("fork") ;
return EXIT_FAILURE ;
}
waitpid(f, &s, 0) ;
return EXIT_SUCCESS ;
}
and the result on the bbs system
% ./control
PID TT STAT TIME COMMAND
10126 pB Is 0:00.01 -csh (csh)
24253 pB I+ 0:00.00 ./control
Could not open /dev/tty
|
|
|
|
|