473,395 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Signal Handling

Hi all,

Consider the following code:

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

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

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));
//raise(SIGSEGV);

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

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
in func() and i will try to write into the buffer that is allocated in
main(), it will generate SIGSEGV signal that is handled by my handler()

There is something wierd going on that i am not able to understand:

1. If both the mprotect functions are uncommented and i try to write
into the buffer[3]='c', SIGSEGV is generated
handler is called and it starts printing "abhay: caught SIGSEGV"
continuously on the screen until stack overflows. But it should have
printed it once and should have returned back into the func()

2. But if i comment both the mprotect and uncomment "raise(SIGSEGV)" to
generate the SIGSEGV signal explicitly, then this doesn't happen.

I am running the program on RedHat linux 8.0 and using GCC compiler.
Can anyone help me out?

Thanks.

Nov 14 '05 #1
5 2592
j

"Sontu" <ab******@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Hi all,

Consider the following code:

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

Inclusion of a non-standard header file.
void handler(int sig)
{
printf("abhay: caught SIGSEGV\n\n");
}


Since when can you call printf from a signal handler?
Undefined behaviour!

<snipped the rest of the garbage>

This is all really off-topic for this newsgroup.

You want comp.unix.programmer

--
j
Nov 14 '05 #2
"Sontu" <ab******@gmail.com> wrote:
Consider the following code:

#include<signal.h>
#include<stdio.h>
#include<sys/mman.h>
I'd rather not. This is quite off-topic.
//to make the starting address in mprotect as page aligned
asm("andl $-4096, %esp");
asm("movl %%esp, %0":"=r"(start));
And so is this (even worse, in fact)...
printf("Mprotect worked:
%d\n\n",mprotect((void*)start,4096,PROT_READ));


....and this.

Note that calling printf() inside a signal handler causes undefined
behaviour.

As for the rest, I cannot tell whether you have a POSIX problem, a Unix
problem, a Linux problem, or a $Distro problem, so I can't tell you
where it _is_ on-topic - but it isn't here.

Richard
Nov 14 '05 #3
Sontu <ab******@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 ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4
Sontu wrote:

Consider the following code:

#include<signal.h>
#include<stdio.h>
#include<sys/mman.h>
No such header in Standard C, so we can't consider the code here.

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


and these are syntax errors. 3rd strike. So you better look for a
newsgroup that deals with your particular system and compiler.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #5
I am sorry for troubling you people. I will try to do what some of you
have suggested and if the problem persists, i will post it to another
group comp.unix.programmers.

Thanks a lot for giving your time.

Abhay

Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Lionel van den Berg | last post by:
Hi all, I'm trying to do some signal handling using the csignal library but I can't find and specific examples for C++. The signal function as defined in C takes a parameter that is the signal...
2
by: lpw | last post by:
I have dilligently reviewed FAQ-lite Section 3.2, "How do I pass a pointer-to-member-function to a signal handler, X event callback, system call that starts a thread/task, etc." The only...
3
by: Martin McCormick | last post by:
A C program contains several signal statements to remove a lock file if the program gets killed: /*Set up interrupt handler to catch ctrl-C so that lock file can be removed.*/...
4
by: Eric Boutin | last post by:
Hi ! currently reading C docs, I think I'm reading docs about the stdc lib, but I'm not shure.. it talks about signals, does *all* OS implement the signal function, and use it well ? I mean.. I...
7
by: Stanley S | last post by:
Hi, Are Signal Handling part of ANSI C? I am not able to find any reference of Sig Handling in Stephen Prata's "C Primer Plus". The usage of signals is to trap errors I guess. (It looks...
10
by: subramanian | last post by:
Consider the following code: segment violation is deliberately generated to catch the SIGSEGV signal. The handler for this signal, namely SIGSEGV_handler, is called. However after the handler is...
2
by: hg | last post by:
Hi, I posted an equivalent question earlier ... but am still not sure: I currently (under Linux) have a program that uses Queue.put (raw_input('')) in a signal handler and Queue.get() in the...
3
by: vijay.db | last post by:
Hi Group, Running DB2 V8.2 Fxpack 9 in AIX 5.2, I get the following error frequently and my instance is stopped...collected some info like: the signal received is 11 which is SEGMENTATION...
1
by: 32Alpha | last post by:
Hi, first post here. First off, this IS a homework assignment for an operating systems class, but the question isn't "how do i do the assignment" but "why is my particular implementation not...
0
by: Marcin Krol | last post by:
Right, I didn't realize before that Python interpreter has its own signal handling routines. Now I am able to handle signals in Python code, but it still barfs on exit: import time import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.