Re: unsafe functions from signal handler
- From: "bill pursell" <bill.pursell@xxxxxxxxx>
- Date: 5 Feb 2006 00:25:12 -0800
Kasper Dupont wrote:
bill pursell wrote:
Is the deadlock condition still possible if the sigaction resets
the signal dispostion to abort? (so the first signal enters the
handler which calls printf, but the second signal causes an abort)
The first handler may interrupt a printf call in the program.
Right, thanks. I am clearly somewhat confused still, and I'm hoping
you can clarify for me. I wrote the following code in an attempt
to understand better what is happening with signal handling, and
as always more questions were raised than answered.
I'm trying to determine if a pointer is writable. My function sets the
signal disposition for SIGSEGV, attempts to write to the address,
resets the signal disposition, and returns a value based on whether
or not a SIGSEGV was generated when writing to the address. However,
it doesn't work. When you write to an unwritable adress the attempt
to write
is made again after the handler returns, generating a segfault and
looping in
and out of the handler. The obvious solution was to modify the address
being written to in the signal handler so that the second attempt
will not generate a seg fault. However, subsequent writes do generate
the segfault, and I am baffled as to why. I suspect it is because
the address being written to is no longer being read from the variable,
but is already in a register. How do I achieve the desired result?
Also, on a totally unrelated note, when I run this with gdb -tui, it
bombs
out. The debugger goes to sleep. Any thoughts on why?
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
volatile char *test_ptr;
volatile bool can_write;
char good_buf;
static void
handle(int signum, siginfo_t *info, void *context)
{ /* Handler for SIGSEGV. */
test_ptr = &good_buf;
can_write = false;
}
bool
is_writable(void *ptr)
{ /** Return true if the ptr is writable */
struct sigaction oldact, act;
char value;
/* set signal disposition.*/
sigemptyset(&act.sa_mask);
act.sa_sigaction = handle;
sigaction(SIGSEGV, &act, &oldact);
/* Test the pointer. */
test_ptr = (char *)ptr;
can_write = true;
value = *test_ptr;
*test_ptr = value;
/* Restore the signal disposition. */
sigaction(SIGSEGV, &oldact, NULL);
return can_write;
}
int
main()
{
char *a = "test";
char b[] = "test";
printf(" can%s write to b\n", (is_writable(b)) ? "":"'t");
printf(" can%s write to a\n", (is_writable(a)) ? "":"'t");
}
.
- Follow-Ups:
- Re: unsafe functions from signal handler
- From: John Reiser
- Re: unsafe functions from signal handler
- References:
- Re: unsafe functions from signal handler
- From: bill pursell
- Re: unsafe functions from signal handler
- Prev by Date: NCurses-based Text Editor (Menus/GPM)
- Next by Date: Re: unsafe functions from signal handler
- Previous by thread: Re: unsafe functions from signal handler
- Next by thread: Re: unsafe functions from signal handler
- Index(es):
Relevant Pages
|