Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_signals.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#include <stdio.h>
28#include <stdlib.h>
29#include <stdbool.h>
30#include <stdint.h>
31#include <string.h>
32#include <stdbool.h>
33#include <errno.h>
34#include <signal.h>
35#include <assert.h>
36
37#include "nilorea/n_signals.h"
38#include "nilorea/n_log.h"
39
40#include <libgen.h>
41
42int divide_by_zero(void);
43void cause_segfault(void);
44int stack_overflow(void);
45void infinite_loop(void);
46void illegal_instruction(void);
47void cause_calamity(void);
48
49char* progname = NULL;
50
51int main(int argc, char* argv[]) {
52 (void)argc;
53 progname = argv[0];
54
55 // set_log_file( "ex_signals.log" );
58 // set_log_level( LOG_FILE );
59
60 n_log(LOG_DEBUG, "set_signal_handler");
62
63 n_log(LOG_DEBUG, "cause_calamity");
65
66 return 0;
67}
68
69void cause_calamity(void) {
70 /* uncomment one of the following error conditions to cause a calamity of
71 your choosing! */
73 (void)divide_by_zero();
75 assert(false);
78}
79
80int divide_by_zero(void) {
81 int a = 1;
82 int b = 0;
83 return a / b;
84}
85
86void cause_segfault(void) {
87 int* p = (int*)0x12345678;
88 *p = 0;
89}
90
91// that function was made to generate an error. Temporary disabling infinite recursion detection
92#pragma GCC diagnostic push
93#if defined(__GNUC__) && (__GNUC__ >= 12)
94#pragma GCC diagnostic ignored "-Winfinite-recursion"
95#endif
96int stack_overflow(void) {
97 int foo = 0;
98 (void)foo;
99 foo = stack_overflow();
100 return rand() % 1000;
101}
102#pragma GCC diagnostic pop
103
104/* break out with ctrl+c to test SIGINT handling */
105void infinite_loop(void) {
106 int a = 1;
107 int b = 1;
108 int c = 1;
109 while (a == 1) {
110 b = c;
111 c = a;
112 a = b;
113 }
114}
115
117 /* I couldn't find an easy way to cause this one, so I'm cheating */
118 raise(SIGILL);
119}
int main(void)
void infinite_loop(void)
Definition ex_signals.c:105
void cause_calamity(void)
Definition ex_signals.c:69
void illegal_instruction(void)
Definition ex_signals.c:116
int divide_by_zero(void)
Definition ex_signals.c:80
char * progname
Definition ex_signals.c:49
void cause_segfault(void)
Definition ex_signals.c:86
int stack_overflow(void)
Definition ex_signals.c:96
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_STDERR
internal, default LOG_TYPE
Definition n_log.h:49
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
void set_signal_handler(const char *progname)
Install a signal handler for progname.
Definition n_signals.c:391
Generic log system.
Signals general handling with stack printing, from https://gist.github.com/jvranish/4441299.