Nilorea Library
C utilities for networking, threading, graphics
ex_signals.c
1
7#include <stdio.h>
8#include <stdlib.h>
9#include <stdbool.h>
10#include <stdint.h>
11#include <string.h>
12#include <stdbool.h>
13#include <errno.h>
14#include <signal.h>
15#include <assert.h>
16
17#include "nilorea/n_signals.h"
18#include "nilorea/n_log.h"
19
20#include <libgen.h>
21
22
23int divide_by_zero(void);
24void cause_segfault(void);
25int stack_overflow(void);
26void infinite_loop(void);
27void illegal_instruction(void);
28void cause_calamity(void);
29
30char *progname = NULL ;
31
32int main( int argc, char * argv[])
33{
34 (void)argc;
35 progname = argv[ 0 ];
36
37 //set_log_file( "ex_signals.log" );
40 //set_log_level( LOG_FILE );
41
42 n_log( LOG_DEBUG, "set_signal_handler" );
43 set_signal_handler( basename( progname ) );
44
45 n_log( LOG_DEBUG, "cause_calamity" );
46 cause_calamity();
47
48 return 0;
49}
50
51void cause_calamity(void)
52{
53 /* uncomment one of the following error conditions to cause a calamity of
54 your choosing! */
55 stack_overflow();
56 (void)divide_by_zero();
57 cause_segfault();
58 assert(false);
59 infinite_loop();
60 illegal_instruction();
61}
62
63
64
65int divide_by_zero(void)
66{
67 int a = 1;
68 int b = 0;
69 return a / b;
70}
71
72void cause_segfault(void)
73{
74 int * p = (int*)0x12345678;
75 *p = 0;
76}
77
78int stack_overflow(void)
79{
80 int foo = 0 ;
81 (void)foo;
82 foo = stack_overflow();
83 return rand()%1000 ;
84}
85
86/* break out with ctrl+c to test SIGINT handling */
87void infinite_loop(void)
88{
89 int a = 1 ;
90 int b = 1 ;
91 int c = 1 ;
92 while( a == 1 )
93 {
94 b=c;
95 c=a;
96 a=b;
97 }
98}
99
100void illegal_instruction(void)
101{
102 /* I couldn't find an easy way to cause this one, so I'm cheating */
103 raise(SIGILL);
104}
105
#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.