OpenVAS Scanner  7.0.1~git
processes.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 "processes.h"
27 
28 #include "sighand.h"
29 
30 #include <errno.h> /* for errno() */
31 #include <glib.h> /* for g_error */
32 #include <setjmp.h>
33 #include <signal.h> /* for kill() */
34 #include <stdlib.h> /* for exit() */
35 #include <string.h> /* for strerror() */
36 #include <sys/wait.h> /* for waitpid() */
37 #include <time.h> /* for time() */
38 #include <unistd.h> /* for fork() */
39 
40 #undef G_LOG_DOMAIN
41 
44 #define G_LOG_DOMAIN "sd main"
45 
57 int
59 {
60  int ret;
61 
62  if (pid == 0)
63  return 0;
64 
65  ret = kill (pid, SIGTERM);
66 
67  if (ret == 0)
68  {
69  usleep (1000);
70 
71  if (waitpid (pid, NULL, WNOHANG) >= 0)
72  {
73  kill (pid, SIGKILL);
74  return -1;
75  }
76  }
77 
78  return 0;
79 }
80 
81 static void
83 {
84  /* SIGHUP is only for reloading main scanner process. */
85  openvas_signal (SIGHUP, SIG_IGN);
86  openvas_signal (SIGTERM, make_em_die);
87  openvas_signal (SIGINT, make_em_die);
88  openvas_signal (SIGQUIT, make_em_die);
89  openvas_signal (SIGSEGV, sighand_segv);
90  openvas_signal (SIGPIPE, SIG_IGN);
91 }
92 
96 pid_t
97 create_process (process_func_t function, void *argument)
98 {
99  int pid;
100 
101  pid = fork ();
102 
103  if (pid == 0)
104  {
106  srand48 (getpid () + getppid () + (long) time (NULL));
107  (*function) (argument);
108  exit (0);
109  }
110  if (pid < 0)
111  g_error ("Error : could not fork ! Error : %s", strerror (errno));
112  return pid;
113 }
void(*)(int) openvas_signal(int signum, void(*handler)(int))
Definition: sighand.c:87
processes.c header.
headerfile for sighand.c.
static pid_t pid
void sighand_segv(int given_signal)
Definition: sighand.c:130
void make_em_die(int sig)
Definition: sighand.c:48
static void init_child_signal_handlers()
Definition: processes.c:82
void(* process_func_t)(void *)
Definition: processes.h:31
int terminate_process(pid_t pid)
Send SIGTERM to the pid process. Try to wait the the process. In case of the process has still not ch...
Definition: processes.c:58
pid_t create_process(process_func_t function, void *argument)
Create a new process (fork).
Definition: processes.c:97