dztall / ccr_resources

Mobile C Resources
10 stars 8 forks source link

Signal handlers #28

Open mgood7123 opened 6 years ago

mgood7123 commented 6 years ago

What signal handlers are currently implimented/catchable, as exit(), abort() and others do not appear to generate a signal when called, as i can catch sig abort by freeing a invalid pointer, and segv by causing my program to intentionally seg fault, but i seem to be unable to catch any other signals, or at least dont know how to generate them

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

void sig_handler(int sig_num) {
    printf("Signal recieved: %d\n", sig_num);
    exit(sig_num);
}

signal(SIGABRT, sig_handler);
signal(SIGALRM, sig_handler);
signal(SIGBUS, sig_handler);
signal(SIGCHLD, sig_handler);
signal(SIGCONT, sig_handler);
signal(SIGEMT, sig_handler);
signal(SIGEV_NONE, sig_handler);
signal(SIGEV_SIGNAL, sig_handler);
signal(SIGEV_THREAD, sig_handler);
signal(SIGFPE, sig_handler);
signal(SIGHUP, sig_handler);
signal(SIGILL, sig_handler);
signal(SIGINFO, sig_handler);
signal(SIGINT, sig_handler);
signal(SIGIO, sig_handler);
signal(SIGIOT, sig_handler);
signal(SIGKILL, sig_handler);
signal(SIGPIPE, sig_handler);
signal(SIGPROF, sig_handler);
signal(SIGQUIT, sig_handler);
signal(SIGSEGV, sig_handler);
signal(SIGSTKSZ, sig_handler);
signal(SIGSTOP, sig_handler);
signal(SIGSYS, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGTRAP, sig_handler);
signal(SIGTSTP, sig_handler);
signal(SIGTTIN, sig_handler);
signal(SIGTTOU, sig_handler);
signal(SIGURG, sig_handler);
signal(SIGUSR1, sig_handler);
signal(SIGUSR2, sig_handler);
signal(SIGVTALRM, sig_handler);
signal(SIGWINCH, sig_handler);
signal(SIGXCPU, sig_handler);
signal(SIGXFSZ, sig_handler);
dztall commented 6 years ago

Signals are consumed internally not to crash the app. Maybe I need to redirect it to user code?

mgood7123 commented 6 years ago

what do you mean by user code?, and is there a way to set a function to happen at exit() without it interfering with any other already existing atexit handlers that may already be set up, and if possible make sure it is always called either first, or last, depending on what the atexit handlers do if they exist, preferably id want it to be called after all handlers unless one happens to call exit or equivilant cpp/crr exit function

mgood7123 commented 6 years ago

nvm i thought u meant user code as in like u have some kind of user code for all the signals lol

mgood7123 commented 6 years ago

would i be correct at assuming by usercode you mean, for example, when exit() or abort() is called, or any other error such as manually stopping the program via stop or back button, or anything that would normally generate a signal of some kind, it would check for a signal hander for the signal that is being consumed internally and if one exists redirect it to that function for the apropriate signal?

dztall commented 6 years ago

User is YOU here!

dztall commented 6 years ago

If you give me signal handling example code, I can test and fix for it to work correctly.

mgood7123 commented 6 years ago

Look at the one in first post lol

mgood7123 commented 6 years ago

it should be able to catch every signal, if the signals are sent correctly, assuming all signals can be catched/intercepted/produced

mgood7123 commented 6 years ago

i will attempt to try to reproduce some of the signals if possible

dztall commented 6 years ago

But it's not valid c code!! lol

mgood7123 commented 6 years ago

is there any way to get longjmp to work

mgood7123 commented 6 years ago

@dztall A rough file with all signals, not complete

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define ifs(x) if(sig_num == x) printf("Signal recieved: %s\n", #x)
int sig_handler(int sig_num) {
    ifs(SIGABRT);
    else ifs(SIGALRM);
    else ifs(SIGBUS);
    else ifs(SIGCHLD);
    else ifs(SIGCONT);
    else ifs(SIGEMT);
    else ifs(SIGEV_NONE);
    else ifs(SIGEV_SIGNAL);
    else ifs(SIGEV_THREAD);
    else ifs(SIGFPE);
    else ifs(SIGHUP);
    else ifs(SIGILL);
    else ifs(SIGINFO);
    else ifs(SIGINT);
    else ifs(SIGIO);
    else ifs(SIGIOT);
    else ifs(SIGKILL);
    else ifs(SIGPIPE);
    else ifs(SIGPROF);
    else ifs(SIGQUIT);
    else ifs(SIGSEGV);
    else ifs(SIGSTKSZ);
    else ifs(SIGSTOP);
    else ifs(SIGSYS);
    else ifs(SIGTERM);
    else ifs(SIGTRAP);
    else ifs(SIGTSTP);
    else ifs(SIGTTIN);
    else ifs(SIGTTOU);
    else ifs(SIGURG);
    else ifs(SIGUSR1);
    else ifs(SIGUSR2);
    else ifs(SIGVTALRM);
    else ifs(SIGWINCH);
    else ifs(SIGXCPU);
    else ifs(SIGXFSZ);
    else printf("Unknown Signal recieved: %d\n", sig_num);
    abort();
}

signal(SIGABRT, sig_handler);
signal(SIGALRM, sig_handler);
signal(SIGBUS, sig_handler);
signal(SIGCHLD, sig_handler);
signal(SIGCONT, sig_handler);
signal(SIGEMT, sig_handler);
signal(SIGEV_NONE, sig_handler);
signal(SIGEV_SIGNAL, sig_handler);
signal(SIGEV_THREAD, sig_handler);
signal(SIGFPE, sig_handler);
signal(SIGHUP, sig_handler);
signal(SIGILL, sig_handler);
signal(SIGINFO, sig_handler);
signal(SIGINT, sig_handler);
signal(SIGIO, sig_handler);
signal(SIGIOT, sig_handler);
signal(SIGKILL, sig_handler);
signal(SIGPIPE, sig_handler);
signal(SIGPROF, sig_handler);
signal(SIGQUIT, sig_handler);
signal(SIGSEGV, sig_handler);
signal(SIGSTKSZ, sig_handler);
signal(SIGSTOP, sig_handler);
signal(SIGSYS, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGTRAP, sig_handler);
signal(SIGTSTP, sig_handler);
signal(SIGTTIN, sig_handler);
signal(SIGTTOU, sig_handler);
signal(SIGURG, sig_handler);
signal(SIGUSR1, sig_handler);
signal(SIGUSR2, sig_handler);
signal(SIGVTALRM, sig_handler);
signal(SIGVTALRM, sig_handler);
signal(SIGWINCH, sig_handler);
signal(SIGXCPU, sig_handler);
signal(SIGXFSZ, sig_handler);

void signal_SIGABRT(void) {
    // should generate a SIGABRT signal and be handled by the signal handler
}

void signal_SIGALRM(void) {
    // should generate a SIGALRM signal and be handled by the signal handler
}

void signal_SIGBUS(void) {
    // should generate a SIGBUS signal and be handled by the signal handler
    int *a = 0x1;
    int b = *a;
}

void signal_SIGCHLD(void) {
    // should generate a SIGCHLD signal and be handled by the signal handler
}

void signal_SIGCONT(void) {
    // should generate a SIGCONT signal and be handled by the signal handler
}

void signal_SIGEMT(void) {
    // should generate a SIGEMT signal and be handled by the signal handler
}

void signal_SIGEV_NONE(void) {
    // should generate a SIGEV_NONE signal and be handled by the signal handler
}

void signal_SIGEV_SIGNAL(void) {
    // should generate a SIGEV_SIGNAL signal and be handled by the signal handler
}

void signal_SIGEV_THREAD(void) {
    // should generate a SIGEV_THREAD signal and be handled by the signal handler
}

void signal_SIGFPE(void) {
    // should generate a SIGFPE signal and be handled by the signal handler
}

void signal_SIGHUP(void) {
    // should generate a SIGHUP signal and be handled by the signal handler
}

void signal_SIGILL(void) {
    // should generate a SIGILL signal and be handled by the signal handler
}

void signal_SIGINFO(void) {
    // should generate a SIGINFO signal and be handled by the signal handler
}

void signal_SIGINT(void) {
    // should generate a SIGINT signal and be handled by the signal handler
}

void signal_SIGIO(void) {
    // should generate a SIGIO signal and be handled by the signal handler
}

void signal_SIGIOT(void) {
    // should generate a SIGIOT signal and be handled by the signal handler
}

void signal_SIGKILL(void) {
    // should generate a SIGKILL signal and be handled by the signal handler
}

void signal_SIGPIPE(void) {
    // should generate a SIGPIPE signal and be handled by the signal handler
}

void signal_SIGPROF(void) {
    // should generate a SIGPROF signal and be handled by the signal handler
}

void signal_SIGQUIT(void) {
    // should generate a SIGQUIT signal and be handled by the signal handler
}

void signal_SIGSEGV(void) {
    // should generate a SIGSEGV signal and be handled by the signal handler, cannot use *NULL due to the app complaining about null pointer dereferencing
    char *SC =  "\x01\x60\x8f\xe2"; (*(void(*)()) SC)();
}

void signal_SIGSTKSZ(void) {
    // should generate a SIGSTKSZ signal and be handled by the signal handler
}

void signal_SIGSTOP(void) {
    // should generate a SIGSTOP signal and be handled by the signal handler
}

void signal_SIGSYS(void) {
    // should generate a SIGSYS signal and be handled by the signal handler
}

void signal_SIGTERM(void) {
    // should generate a SIGTERM signal and be handled by the signal handler
}

void signal_SIGTRAP(void) {
    // should generate a SIGTRAP signal and be handled by the signal handler
}

void signal_SIGTSTP(void) {
    // should generate a SIGTSTP signal and be handled by the signal handler
}

void signal_SIGTTIN(void) {
    // should generate a SIGTTIN signal and be handled by the signal handler
}

void signal_SIGTTOU(void) {
    // should generate a SIGTTOU signal and be handled by the signal handler
}

void signal_SIGURG(void) {
    // should generate a SIGURG signal and be handled by the signal handler
}

void signal_SIGUSR1(void) {
    // should generate a SIGUSR1 signal and be handled by the signal handler
}

void signal_SIGUSR2(void) {
    // should generate a SIGUSR2 signal and be handled by the signal handler
}

void signal_SIGVTALRM(void) {
    // should generate a SIGVTALRM signal and be handled by the signal handler
}

void signal_SIGWINCH(void) {
    // should generate a SIGWINCH signal and be handled by the signal handler
}

void signal_SIGXCPU(void) {
    // should generate a SIGXCPU signal and be handled by the signal handler
}

void signal_SIGXFSZ(void) {
    // should generate a SIGXFSZ signal and be handled by the signal handler
}

#include <setjmp.h>

void longjump_test(void) {
    jmp_buf env;
    memset(&env, 0, sizeof(jmp_buf));
    int ret = setjmp(env);
    if (ret == 0) {
        puts("attempting to long jump");
        longjmp(env, -1);
        puts("long jump failed");
    }
    else return;
}

#define sig_do(x) if(sig_num == x) signal_##x()
#define sig_list(x) printf("%s = %d\n", #x, x)

int main(int argc, char ** argv[]) {
    int sig_num = 0;
    if(argc < 2)
    {
        printf("Useage: %s <signal number>\n",argv[0]);
        sig_list(SIGABRT);
        sig_list(SIGALRM);
        sig_list(SIGBUS);
        sig_list(SIGCHLD);
        sig_list(SIGCONT);
        sig_list(SIGEMT);
        sig_list(SIGEV_NONE);
        sig_list(SIGEV_SIGNAL);
        sig_list(SIGEV_THREAD);
        sig_list(SIGFPE);
        sig_list(SIGHUP);
        sig_list(SIGILL);
        sig_list(SIGINFO);
        sig_list(SIGINT);
        sig_list(SIGIO);
        sig_list(SIGIOT);
        sig_list(SIGKILL);
        sig_list(SIGPIPE);
        sig_list(SIGPROF);
        sig_list(SIGQUIT);
        sig_list(SIGSEGV);
        sig_list(SIGSTKSZ);
        sig_list(SIGSTOP);
        sig_list(SIGSYS);
        sig_list(SIGTERM);
        sig_list(SIGTRAP);
        sig_list(SIGTSTP);
        sig_list(SIGTTIN);
        sig_list(SIGTTOU);
        sig_list(SIGURG);
        sig_list(SIGUSR1);
        sig_list(SIGUSR2);
        sig_list(SIGVTALRM);
        sig_list(SIGWINCH);
        sig_list(SIGXCPU);
        sig_list(SIGXFSZ);
        return 0;
    }
    else sig_num = atoi(argv[1]);

    sig_do(SIGABRT);
    else sig_do(SIGALRM);
    else sig_do(SIGBUS);
    else sig_do(SIGCHLD);
    else sig_do(SIGCONT);
    else sig_do(SIGEMT);
    else sig_do(SIGEV_NONE);
    else sig_do(SIGEV_SIGNAL);
    else sig_do(SIGEV_THREAD);
    else sig_do(SIGFPE);
    else sig_do(SIGHUP);
    else sig_do(SIGILL);
    else sig_do(SIGINFO);
    else sig_do(SIGINT);
    else sig_do(SIGIO);
    else sig_do(SIGIOT);
    else sig_do(SIGKILL);
    else sig_do(SIGPIPE);
    else sig_do(SIGPROF);
    else sig_do(SIGQUIT);
    else sig_do(SIGSEGV);
    else sig_do(SIGSTKSZ);
    else sig_do(SIGSTOP);
    else sig_do(SIGSYS);
    else sig_do(SIGTERM);
    else sig_do(SIGTRAP);
    else sig_do(SIGTSTP);
    else sig_do(SIGTTIN);
    else sig_do(SIGTTOU);
    else sig_do(SIGURG);
    else sig_do(SIGUSR1);
    else sig_do(SIGUSR2);
    else sig_do(SIGVTALRM);
    else sig_do(SIGWINCH);
    else sig_do(SIGXCPU);
    else sig_do(SIGXFSZ);
    else printf("Unknown Signal recieved: %d\n", sig_num);
    return 0;
}