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 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
28#include <stdio.h>
29#include <stdlib.h>
30#include <stdbool.h>
31#include <stdint.h>
32#include <string.h>
33#include <stdbool.h>
34#include <errno.h>
35#include <signal.h>
36#include <assert.h>
37
38#include "nilorea/n_signals.h"
39#include "nilorea/n_log.h"
40
41#include <libgen.h>
42
43int divide_by_zero(void);
44void cause_segfault(void);
45int stack_overflow(void);
46void infinite_loop(void);
47void illegal_instruction(void);
48void cause_calamity(void);
49
50char* progname = NULL;
51
52int main(int argc, char* argv[]) {
53 (void)argc;
54 progname = argv[0];
55
56 // set_log_file( "ex_signals.log" );
59 // set_log_level( LOG_FILE );
60
61 n_log(LOG_DEBUG, "set_signal_handler");
63
64 n_log(LOG_DEBUG, "cause_calamity");
66
67 return 0;
68}
69
70void cause_calamity(void) {
71 /* uncomment one of the following error conditions to cause a calamity of
72 your choosing! */
74 (void)divide_by_zero();
76 assert(false);
79}
80
81int divide_by_zero(void) {
82 int a = 1;
83 int b = 0;
84 return a / b;
85}
86
87void cause_segfault(void) {
88 int* p = (int*)0x12345678;
89 *p = 0;
90}
91
92// that function was made to generate an error. Temporary disabling infinite recursion detection
93#pragma GCC diagnostic push
94#if defined(__GNUC__) && (__GNUC__ >= 12)
95#pragma GCC diagnostic ignored "-Winfinite-recursion"
96#endif
97int stack_overflow(void) {
98 int foo = 0;
99 (void)foo;
100 foo = stack_overflow();
101 return rand() % 1000;
102}
103#pragma GCC diagnostic pop
104
105/* break out with ctrl+c to test SIGINT handling */
106void infinite_loop(void) {
107 int a = 1;
108 int b = 1;
109 int c = 1;
110 while (a == 1) {
111 b = c;
112 c = a;
113 a = b;
114 }
115}
116
118 /* I couldn't find an easy way to cause this one, so I'm cheating */
119 raise(SIGILL);
120}
int main(void)
void infinite_loop(void)
Definition ex_signals.c:106
void cause_calamity(void)
Definition ex_signals.c:70
void illegal_instruction(void)
Definition ex_signals.c:117
int divide_by_zero(void)
Definition ex_signals.c:81
char * progname
Definition ex_signals.c:50
void cause_segfault(void)
Definition ex_signals.c:87
int stack_overflow(void)
Definition ex_signals.c:97
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
#define LOG_STDERR
internal, default LOG_TYPE
Definition n_log.h:50
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
void set_signal_handler(const char *progname)
Install a signal handler for progname.
Definition n_signals.c:392
Generic log system.
Signals general handling with stack printing, from https://gist.github.com/jvranish/4441299.