Skip to contentSkip to conversation

signal handling issue.

DISCUSSION Alex Davis 3 participants 3 messages May 19, 2004

lore
Share & tools
lore

Alex Davis

author
#0 lore raw

There appears to be a change between linux 2.4 and 2.6 in how signals are handled. As a test, I wrote the program below:

#include <stdio.h>
#include <signal.h>
#include <setjmp.h>

static jmp_buf env;

static void handler(int s) {
        printf("caught signal %d\n", s);
        longjmp(env, 1);
}
int main() {
        int * p = 0;

        printf("write\n");
        signal(SIGSEGV, handler);
        if ( ! setjmp(env) )
        {
                *p = 0;
        }

        printf("read\n");
        signal(SIGSEGV, handler);
        if ( ! setjmp(env) )
        {
                int a = *p;
        }
        return 0;
}

When run on 2.4.26, the program prints:

write
caught signal 11
read
caught signal 11

Which (I think) is expected, but when run on 2.6.5, the program prints:

write
caught signal 11
read
Segmentation fault

It's as if the second call to signal is being ignored. Is this a bug or a feature?

-Alex

=====
I code, therefore I am

__________________________________
Do you Yahoo!?
SBC Yahoo! - Internet access at a great low price.
http://promo.yahoo.com/sbc/

#1 lore raw

Fix your code:

Quoted 4 lines
static void handler(int s) {
        printf("caught signal %d\n", s);
        longjmp(env, 1);
}

Use siglonjmp() and sigsetjmp(). You are not allowed to use longjmp() to jump from a signal handler.

[How many times have I explained this here now?]

Signature · 1 line
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖
#2 lore raw
In reply to Alex Davis
Quoted 8 lines · Alex Davis

There appears to be a change between linux 2.4 and 2.6 in how signals are handled. As a test, I wrote the program below:

#include <stdio.h>
#include <signal.h>
#include <setjmp.h>

Quoted 6 lines

static jmp_buf env;

static void handler(int s) {
        printf("caught signal %d\n", s);
        longjmp(env, 1);
}

[SNIPPED...]

A a couple years ago they changed the rules. You can't longjmp from a signal handler anymore. There's some other function call with 'sig' in it, like siglongjmp or something like that. Ahh... here it is:

/usr/include/setjmp.h:extern void siglongjmp __P ((sigjmp_buf __env, int __val))

Cheers,
Dick Johnson
Penguin : Linux version 2.4.26 on an i686 machine (5557.45 BogoMips).
Note 96.31% of all statistics are fiction.