Unix Programming - signal handler scope?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2004 > signal handler scope?





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 signal handler scope?
jim green

2004-05-17, 9:52 am

Hi Unixers

I have some signal handlers to install:

static void reread_handler(int);
static void halt_handler(int);
static void ignore_handler(int);
static int install_action(int,const struct sigaction*);

static int install_actions(void)
{
static struct sigaction sar,sai,sah;
int errs = 0;

sai.sa_handler = ignore_handler;
sar.sa_handler = reread_handler;
sah.sa_handler = halt_handler;

/* reread */

errs += install_action(SIGPIPE,&sai);
:
:
}

static int install_action(int sig,const struct sigaction *act)
{
if (sigaction(sig,act,NULL) == -1) return 1;

return 0;
}

static void reread_handler(int sig)
{
reread = 1;
}

static void halt_handler(int sig)
{
halt = 1;
}

static void ignore_handler(int sig)
{
}

.... and this all works fine. However, if the struct sigactions
are not declared as static in the function install_actions()
then the second SIGPIPE I send to the program results in an
uncaught SIGSEGV & program termination.

Is this the expected behaviour (I could kind of see why --
when the handler is reinstalled it is out of scope & so an
invalid memory address ) or do I need to track down
a bug somewhere?

Thanks in advance

-j
--
J. J. Green, Department of Applied Mathematics, Hicks Bd.,
Hounsfield Rd., university of Sheffield, Sheffield, UK.
+44 (0114) 222 3742, http://www.vindaloo.uklinux.net/jjg


Barry Margolin

2004-05-17, 9:52 am

In article <m27jvbs1es.fsf@pc004166.shef.ac.uk>,
jim green <jjg@withheld.ac.uk> wrote:

> ... and this all works fine. However, if the struct sigactions
> are not declared as static in the function install_actions()
> then the second SIGPIPE I send to the program results in an
> uncaught SIGSEGV & program termination.
>
> Is this the expected behaviour (I could kind of see why --
> when the handler is reinstalled it is out of scope & so an
> invalid memory address ) or do I need to track down
> a bug somewhere?


When install_actions() returns, its automatic variables no longer exist,
and any pointers to them are invalid. So when the signal handler is
invoked, it receives one of these invalid pointers.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
jim green

2004-05-17, 11:35 am

Hi Barry

> When install_actions() returns, its automatic variables no longer exist,
> and any pointers to them are invalid. So when the signal handler is
> invoked, it receives one of these invalid pointers.


Thanks for confirming my suspicions, no bugs for me to fix!

Cheers

Jim
--
J. J. Green, Department of Applied Mathematics, Hicks Bd.,
Hounsfield Rd., university of Sheffield, Sheffield, UK.
+44 (0114) 222 3742, http://www.vindaloo.uklinux.net/jjg
Villy Kruse

2004-05-17, 11:35 am

On 17 May 2004 13:06:51 +0100,
jim green <jjg@withheld.ac.uk> wrote:


>
> ... and this all works fine. However, if the struct sigactions
> are not declared as static in the function install_actions()
> then the second SIGPIPE I send to the program results in an
> uncaught SIGSEGV & program termination.
>


A non-static structure have randome contents until you assign something
to it. So do initialize all fields in the sigaction structure.

Villy
Villy Kruse

2004-05-17, 11:35 am

On Mon, 17 May 2004 09:50:27 -0400,
Barry Margolin <barmar@alum.mit.edu> wrote:


>
> When install_actions() returns, its automatic variables no longer exist,
> and any pointers to them are invalid. So when the signal handler is
> invoked, it receives one of these invalid pointers.
>


The sigaction structure isn't needed after you've called sigaction().


Villy
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com