OpenVAS Scanner  7.0.1~git
sighand.c
Go to the documentation of this file.
1 /* Portions Copyright (C) 2009-2019 Greenbone Networks GmbH
2  * Portions Copyright (C) 2006 Software in the Public Interest, Inc.
3  * Based on work Copyright (C) 1998 - 2006 Tenable Network Security, Inc.
4  *
5  * SPDX-License-Identifier: GPL-2.0-only
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
26 #include <execinfo.h> /* for backtrace() */
27 #include <glib.h> /* for G_LOG_DOMAIN, for g_critical() */
28 #include <signal.h> /* for kill() */
29 #include <sys/wait.h> /* for waitpid() */
30 #include <unistd.h> /* for getpid() */
31 
32 #undef G_LOG_DOMAIN
33 
36 #define G_LOG_DOMAIN "sd main"
37 
38 /* do not leave a zombie, hanging around if possible */
39 void
41 {
42  int status;
43 
44  waitpid (pid, &status, WNOHANG);
45 }
46 
47 void
48 make_em_die (int sig)
49 {
50  /* number of times, the sig is sent at most */
51  int n = 3;
52 
53  /* leave if we are session leader */
54  if (getpgrp () != getpid ())
55  return;
56 
57  /* quickly send signals and check the result */
58  if (kill (0, sig) < 0)
59  return;
60  let_em_die (0);
61  if (kill (0, 0) < 0)
62  return;
63 
64  do
65  {
66  /* send the signal to everybody in the group */
67  if (kill (0, sig) < 0)
68  return;
69  sleep (1);
70  /* do not leave a zombie, hanging around if possible */
71  let_em_die (0);
72  }
73  while (--n > 0);
74 
75  if (kill (0, 0) < 0)
76  return;
77 
78  kill (0, SIGKILL);
79  sleep (1);
80  let_em_die (0);
81 }
82 
83 /*
84  * Replacement for the signal() function, written
85  * by Sagi Zeevi <sagiz@yahoo.com>
86  */
87 void (*openvas_signal (int signum, void (*handler) (int))) (int)
88 {
89  struct sigaction saNew, saOld;
90 
91  /* Init new handler */
92  sigfillset (&saNew.sa_mask);
93  sigdelset (&saNew.sa_mask, SIGALRM); /* make sleep() work */
94 
95  saNew.sa_flags = 0;
96  saNew.sa_handler = handler;
97 
98  sigaction (signum, &saNew, &saOld);
99  return saOld.sa_handler;
100 }
101 
102 void
104 {
105  int status;
106 
107  waitpid (pid, &status, WNOHANG);
108 }
109 
110 static void
112 {
113  void *array[10];
114  int ret = 0, left;
115  char *message = "SIGSEGV occurred!\n";
116  char **strings;
117 
118  /*It used log_get_fd() in log.h to know where to log the backtrace.*/
119  ret = backtrace (array, 10);
120  strings = backtrace_symbols (array, ret);
121  g_warning ("%s", message);
122 
123  for (left = 0; left < 10; left++)
124  g_warning ("%s\n", strings[left]);
125 
126  g_free (strings);
127 }
128 
129 void
130 sighand_segv (int given_signal)
131 {
132  signal (SIGSEGV, _exit);
133  print_trace ();
134  make_em_die (SIGTERM);
135  /* Raise signal again, to exit with the correct return value,
136  * and to enable core dumping. */
137  openvas_signal (given_signal, SIG_DFL);
138  raise (given_signal);
139 }
void(*)(int) openvas_signal(int signum, void(*handler)(int))
Definition: sighand.c:87
static pid_t pid
static void print_trace()
Definition: sighand.c:111
void sighand_chld(pid_t pid)
Definition: sighand.c:103
void sighand_segv(int given_signal)
Definition: sighand.c:130
void make_em_die(int sig)
Definition: sighand.c:48
void let_em_die(int pid)
Definition: sighand.c:40