Re: Signal Handling

Jens.Toerring_at_physik.fu-berlin.de
Date: 01/31/05


Date: 31 Jan 2005 17:12:28 GMT

Sontu <abhaywit@gmail.com> wrote:
> Consider the following code:

> #include<signal.h>
> #include<stdio.h>
> #include<sys/mman.h>

Non-standard header.

> void handler(int sig)
> {
> printf("abhay: caught SIGSEGV\n\n");
> }

You better don't use non-reentrant functions in signal handlers.
In principle not much more than setting a variable of type
sig_atomic_t is guaranteed to work in a signal handler.

> void func(char *buffer)
> {

> unsigned int start=0;
> //to make the starting address in mprotect as page aligned
> asm("andl $-4096, %esp");
> asm("movl %%esp, %0":"=r"(start));

No we get into completely platform dependent stuff. Don't expect
comments here in clc. Take that to a group that deals with the
platform you're using.

> //raise(SIGSEGV);

> printf("Mprotect worked:
> %d\n\n",mprotect((void*)start,4096,PROT_READ));

Non-standard function.

> buffer[3]='c';

> printf("Mprotect worked:
> %d\n\n",mprotect((void*)start,4096,PROT_WRITE|PROT_READ|PROT_EXEC));
> }

> int main(void)
> {
> char buffer[10];

> if( signal(SIGSEGV, handler)== SIG_ERR )
> printf("problem installing new signal handler\n\n");

> func(buffer);

> printf("into main\n\n");

> return 0;
> }

> My program makes the previous frame as write protected, thus when i am

It may on the platform you are using, but that's nothing related to
the C language, which hasn't frames nor functions or to make them
write protected. Since you seem to be using Linux better take that
question to comp.os.linux.development.apps.

<OT>
Actually, when you get a real SIGSEGV signal, i.e. not one that you
faked using raise(), and don't exit() from the signal handler, flow
of control is passed back to instruction that led to the signal
getting raised. Since nothing has changed to remove the reason for
the signal it gets raised again immediately and you end up in an
infinite loop.
</OT>
                                  Regards, Jens

-- 
  \   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
   \__________________________  http://www.toerring.de


Relevant Pages

  • Re: signal() behaviour
    ... > other apps not. ... > of signalcan differ from system to system and from platform to ... but I expected it to behave consistently on the same machine ... signal handler with code that resets the handler when it's called). ...
    (comp.unix.programmer)
  • Re: How can I "peek" at the keyboard without suspending?
    ... >On the topic of the original post, I am hesitant to write platform ... >specific code to avoid suspending. ... If this is anything but setting up a signal handler, ... >the sub process ...
    (comp.lang.c)