trafgen: fix NULL pointer dereference in -i option parsing
[netsniff-ng.git] / die.c
blob4e525f9e15539434be76341c237ef06d7f0fe8f3
1 /*
2 * Subject to the GPL, version 2.
3 */
5 #include "xmalloc.h"
7 struct panic_handler {
8 void *arg;
9 pid_t pid;
10 bool is_enabled;
11 void (*on_panic)(void *arg);
12 struct panic_handler *next;
15 static struct panic_handler *panic_handlers;
17 void panic_handler_add(void (*on_panic)(void *arg), void *arg)
19 struct panic_handler *handler = xmallocz(sizeof(*handler));
21 handler->arg = arg;
22 handler->pid = getpid();
23 handler->is_enabled = true;
24 handler->on_panic = on_panic;
25 handler->next = panic_handlers;
26 panic_handlers = handler;
29 void call_panic_handlers(void)
31 struct panic_handler *it;
32 pid_t pid = getpid();
34 for (it = panic_handlers; it; it = it->next) {
35 if (it->pid == pid && it->is_enabled) {
36 it->is_enabled = false;
37 it->on_panic(it->arg);