segfault handling

  • Thread starter Parahat Melayev
  • Start date
P

Parahat Melayev

I am programming simple TCP server.
Some packets causes server to get segmentation fault.
Is it possible to handle segmentation fault and log that packet and
make server to continue running?
If possible can you provide me some example or tutorials please.

thanks,
parahat melayev
 
P

Peteris Krumins

Parahat said:
I am programming simple TCP server.
Some packets causes server to get segmentation fault.

Nope, packets do not cause the server to segfault. Bad packet
handling code does.


P.Krumins
 
A

Alan Balmer

I am programming simple TCP server.
Some packets causes server to get segmentation fault.
Is it possible to handle segmentation fault and log that packet and
make server to continue running?

No, because your program is broken. Find out why some packets cause a
segfault.
 
G

Gordon Burditt

I am programming simple TCP server.
Some packets causes server to get segmentation fault.
Is it possible to handle segmentation fault and log that packet and
make server to continue running?

Try fixing the original problem. Whatever caused the segfault
(buffer overflow?) may have mortally wounded the server so badly
it can't function properly again. You are also not guaranteed to
be able to restart after a segfault (and if you don't fix the bad
pointer or array subscript, you're likely to get another one almost
immediately anyway).
If possible can you provide me some example or tutorials please.

Learn how to use a debugger and read a stack trace. Also, try to
write a client that can consistently crash the server whenever you
want, so you can quickly reproduce the problem.

Another possibility is to keep restarting the server whenever it
dies. There's lots of ways to do this, from a shell while loop
to the 'daemontools' software, which lets you have better controls
over logging, taking the server down intentionally, etc.

Gordon L. Burditt
 
C

Christian Bau

I am programming simple TCP server.
Some packets causes server to get segmentation fault.
Is it possible to handle segmentation fault and log that packet and
make server to continue running?
If possible can you provide me some example or tutorials please.

If some packets cause a segmentation fault, then I bet someone could
create malicious packets that will take over your computer.

Find the reason for the segmentation fault and fix it.
 
O

Old Wolf

Parahat said:
I am programming simple TCP server.
Some packets causes server to get segmentation fault.
Is it possible to handle segmentation fault and log that packet and
make server to continue running?
If possible can you provide me some example or tutorials please.

You could use setjmp() and longjmp() to detect SIGSEGV and
recover from it. See <setjmp.h> and <signal.h> .

Of course, it would be better to fix the code that causes
the segfault in the first place.
 
E

Eric Sosman

Old said:
You could use setjmp() and longjmp() to detect SIGSEGV and
recover from it. See <setjmp.h> and <signal.h> .

If you're suggesting calling longjmp() from a signal
handler, you're asking for undefined behavior.

7.14.1.1 The signal function
/5/ [...] the behavior is undefined if the signal
handler refers to any object with static storage
duration [with one exception] or if the signal
handler calls any function in the standard library
[with three exceptions].

Since the handler can't safely refer to static-duration
objects, the jmp_buf cannot be a global variable, nor can
a pointer to the jmp_buf be a global, nor a pointer to a
pointer ... Hence, there's no defined way for the handler
to find the jmp_buf and pass it to longjmp().

... and even if it could find the jmp_buf, the handler
still couldn't safely call the longjmp() library function.
(Note that while setjmp() is a macro, longjmp() is described
as an actual function.)
Of course, it would be better to fix the code that causes
the segfault in the first place.

Yes, that's the path to take. As someone has already
observed, there is no reason to believe that the SIGSEGV is
the first thing the program has done wrong, it's just the
first error that was detected. All you know is that the
program went off the rails some time ago, and in light of
the knowledge that the program has run amok there's little
reason to trust anything its memory might now contain.
 
P

Parahat Melayev

Old Wolf said:
You could use setjmp() and longjmp() to detect SIGSEGV and
recover from it. See <setjmp.h> and <signal.h> .

Of course, it would be better to fix the code that causes
the segfault in the first place.

Thanks everybody,

of course in the first plase I want to find reason of segfault.

yes i found & read about signals from Stevens' UNP & APUE book.
now looking into <setjmp.h> ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top