Force kernel to unload process when connecting SIGSEGV

I am writing an application for a system with a very limited RAM value. Since the application crash is always possible, and it uses dynamic memory allocation, I created callbacks for all possible ways the application can stop and clear all buffers there, for example:

sigaddset ( &sigact.sa_mask, SIGSEGV );
sigaction ( SIGSEGV, &sigact, ( struct sigaction* ) NULL );

It triggers some notifications and tries to restart itself several times. But still I want to know what caused the crash, so I need crashdump. GDB is not suitable for such a small system, and only a core core dump is possible. But since the application intercepted such a signal and after all exits by itself, the kernel does not receive a signal - the kernel has not been created.
I can not give out

 kill(getpid(), kernel_signal);

How this leads to recursion - this signal is also intercepted. Is it even possible in such a situation?

+4
source share
1 answer

Perhaps, instead of the main dump, you can print the return line. This should use less memory and is an exciting signal. See This Signal Handler.

#include <execinfo.h>

/* This signal handler ensures clean exits while not running in daemon mode */
void signal_handler(int type)
{
    fprintf(stderr, "\nSIGNAL CAUGHT: %d: ", type);

    switch (type)
    {
    case SIGSEGV:
        {
#ifdef GLIBC
            {
                void *array[10];
                size_t size;

                // get void* for all entries on the stack
                size = backtrace(array, 10);

                // print out all the frames to stderr
                fprintf(stderr, "Error: signal %d:\n", type);
                backtrace_symbols_fd(array, size, STDERR_FILENO);

                // DO CLEANUP HERE
            }
#else // Cause ulibc is terrible
            fprintf(stderr, "SEGMENTATION FAULT");

            // DO CLEANUP HERE

            break;
        }
#endif
    case SIGINT:
    case SIGTERM:
    case SIGHUP:
    default:
        {
          // DO CLEANUP HERE
        }

    }
}
+2
source

All Articles