views:

289

answers:

2

Hi all,

i dont even know if this is even possible . but i m trying..

I have a Application A(by some company X). This application allows me to extend the functionality by allowing me to write my own functions(User written). I tell the Application A to call my user functions in the Applications A's configuration file(This is how it knows that Appl A must call user written Functions). The appl A uses Fucntion pointers which i must register with Application A,prior to calling my user written functions.

Now if there is a bug or fault in my user written functions in production, the Appl A will stop functioning, for e.g i have a segmentation fault in my User written fucntions.

So basically the Application A will load my user written function from a shared or DLL file. This means that my user written functions will be running in Application A' Process address space.

I wish to handle certain signals like Segmentaion fault, divide by zero and stack overflow.

But applications A has its own signal handlers written for this,

How can i write my own signal handlers to catch the exceptions in my user written functions, So that i can clean up grace fully w/o affecting much of Application A. Since my user functions will be called in Applications A's process , the OS will call signal handlers written in Application A and not my user functions.

How can i change this.I Want OS to call signal handlers written in my functions but only for signal raised by my functions, which is asynchronous in nature.

Note: i do not have source code of Application A and i cannot do any changes since its controlled by a different company.

I hope i m clear in writing my problem.

Sorry i didn't mention , I will be using C , and only C. platform : Linux, solaris and probably windows in future.

A: 

in C++, you can catch these by putting your code in a try-catch:

try
{
   // here goes your code
}
catch ( ... )
{
   // handle segfaults
}
Stefan
A: 

You do not specify which platform you're working with, so I'll answer for Linux, and it should be valid for Windows as well.

When you set your signal handlers, the system call that you use returns the previous handler. It does it so that you can return it once you are no longer interested in handling that signal.

Linux man page for signal
MSDN entry on signal

Since you are a shared library loaded into the application you should have no problems manipulating the signals handlers. Just make sure to override the minimum you need in order to reduce the chances of disrupting the application itself (some applications use signals for async notifications).

Leeor Aharon