OpenVAS Scanner 23.40.3
sighand.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Greenbone AG
2 * SPDX-FileCopyrightText: 2006 Software in the Public Interest, Inc.
3 * SPDX-FileCopyrightText: 1998-2006 Tenable Network Security, Inc.
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 */
7
12
13#include "sighand.h"
14
15#include "debug_utils.h"
16#include "processes.h"
17
18#include <execinfo.h> /* for backtrace() */
19#include <glib.h> /* for G_LOG_DOMAIN, for g_critical() */
20#include <signal.h> /* for kill() */
21#include <sys/wait.h> /* for waitpid() */
22#include <unistd.h> /* for getpid() */
23
24#undef G_LOG_DOMAIN
28#define G_LOG_DOMAIN "sd main"
29
30/* do not leave a zombie, hanging around if possible */
31void
33{
34 int status;
35
36 waitpid (pid, &status, WNOHANG);
37}
38
39void
40make_em_die (int sig)
41{
42 /* number of times, the sig is sent at most */
43 int n = 3;
44
45 /* leave if we are session leader */
46 if (getpgrp () != getpid ())
47 return;
48
49 /* quickly send signals and check the result */
50 if (kill (0, sig) < 0)
51 return;
52 let_em_die (0);
53 if (kill (0, 0) < 0)
54 return;
55
56 do
57 {
58 /* send the signal to everybody in the group */
59 if (kill (0, sig) < 0)
60 return;
61 sleep (1);
62 /* do not leave a zombie, hanging around if possible */
63 let_em_die (0);
64 }
65 while (--n > 0);
66
67 if (kill (0, 0) < 0)
68 return;
69
70 kill (0, SIGKILL);
71 sleep (1);
72 let_em_die (0);
73}
74
75/*
76 * Replacement for the signal() function, written
77 * by Sagi Zeevi <sagiz@yahoo.com>
78 */
79void (*openvas_signal (int signum, void (*handler) (int))) (int)
80{
81 struct sigaction saNew, saOld;
82
83 /* Init new handler */
84 sigfillset (&saNew.sa_mask);
85 sigdelset (&saNew.sa_mask, SIGALRM); /* make sleep() work */
86
87 saNew.sa_flags = 0;
88 saNew.sa_handler = handler;
89
90 sigaction (signum, &saNew, &saOld);
91 return saOld.sa_handler;
92}
93
94void
95sighand_chld (int sig)
96{
97 (void) sig;
98 // if we call multiple times waitpid it will disturb the attack loop.
99 // therefore we cannot cleanup multiple ipc here
100 waitpid (-1, NULL, WNOHANG);
101}
102
103static void
105{
106 void *array[10];
107 int ret = 0, left;
108 char *message = "SIGSEGV occurred!\n";
109 char **strings;
110
111 /*It used log_get_fd() in log.h to know where to log the backtrace.*/
112 ret = backtrace (array, 10);
113 strings = backtrace_symbols (array, ret);
114 g_warning ("%s", message);
115
116 for (left = 0; left < ret; left++)
117 g_warning ("%s\n", strings[left]);
118
119 g_free (strings);
120}
121
122void
123sighand_segv (int given_signal)
124{
125 print_trace ();
126 make_em_die (SIGTERM);
127 gvm_close_sentry ();
128 /* Raise signal again, to exit with the correct return value,
129 * and to enable core dumping. */
130 openvas_signal (given_signal, SIG_DFL);
131 raise (given_signal);
132}
debug_utils.c headerfile.
static pid_t pid
processes.c header.
void(*)(int) openvas_signal(int signum, void(*handler)(int))
Definition sighand.c:79
static void print_trace(void)
Definition sighand.c:104
void let_em_die(int pid)
Definition sighand.c:32
void sighand_segv(int given_signal)
Definition sighand.c:123
void sighand_chld(int sig)
Definition sighand.c:95
void make_em_die(int sig)
Definition sighand.c:40
headerfile for sighand.c.