Nilorea Library
C utilities for networking, threading, graphics
ex_signals.c

Nilorea Library signals api test.

Nilorea Library signals api test

Author
Castagnier Mickael
Version
1.0
Date
04/12/2019
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <signal.h>
#include <assert.h>
#include "nilorea/n_log.h"
#include <libgen.h>
int divide_by_zero(void);
void cause_segfault(void);
int stack_overflow(void);
void infinite_loop(void);
void illegal_instruction(void);
void cause_calamity(void);
char *progname = NULL ;
int main( int argc, char * argv[])
{
(void)argc;
progname = argv[ 0 ];
//set_log_file( "ex_signals.log" );
//set_log_level( LOG_FILE );
n_log( LOG_DEBUG, "set_signal_handler" );
set_signal_handler( basename( progname ) );
n_log( LOG_DEBUG, "cause_calamity" );
cause_calamity();
return 0;
}
void cause_calamity(void)
{
/* uncomment one of the following error conditions to cause a calamity of
your choosing! */
stack_overflow();
(void)divide_by_zero();
cause_segfault();
assert(false);
infinite_loop();
illegal_instruction();
}
int divide_by_zero(void)
{
int a = 1;
int b = 0;
return a / b;
}
void cause_segfault(void)
{
int * p = (int*)0x12345678;
*p = 0;
}
int stack_overflow(void)
{
int foo = 0 ;
(void)foo;
foo = stack_overflow();
return rand()%1000 ;
}
/* break out with ctrl+c to test SIGINT handling */
void infinite_loop(void)
{
int a = 1 ;
int b = 1 ;
int c = 1 ;
while( a == 1 )
{
b=c;
c=a;
a=b;
}
}
void illegal_instruction(void)
{
/* I couldn't find an easy way to cause this one, so I'm cheating */
raise(SIGILL);
}
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition: n_log.h:74
#define LOG_DEBUG
debug-level messages
Definition: n_log.h:66
#define LOG_STDERR
internal, default LOG_TYPE
Definition: n_log.h:31
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition: n_log.c:97
void set_signal_handler(const char *progname)
Install a signal handler for progname.
Definition: n_signals.c:393
Generic log system.
Signals general handling with stack printing, from https://gist.github.com/jvranish/4441299.