OpenVAS Scanner  5.1.3
processes.c
Go to the documentation of this file.
1 /* OpenVAS
2 * $Id$
3 * Description: Creates new threads.
4 *
5 * Authors: - Renaud Deraison <deraison@nessus.org> (Original pre-fork develoment)
6 * - Tim Brown <mailto:timb@openvas.org> (Initial fork)
7 * - Laban Mwangi <mailto:labanm@openvas.org> (Renaming work)
8 * - Tarik El-Yassem <mailto:tarik@openvas.org> (Headers section)
9 *
10 * Copyright:
11 * Portions Copyright (C) 2006 Software in the Public Interest, Inc.
12 * Based on work Copyright (C) 1998 - 2006 Tenable Network Security, Inc.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2,
16 * as published by the Free Software Foundation
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27 
28 #include <signal.h> /* for kill() */
29 #include <sys/wait.h> /* for waitpid() */
30 #include <unistd.h> /* for fork() */
31 #include <errno.h> /* for errno() */
32 #include <time.h> /* for time() */
33 #include <string.h> /* for strerror() */
34 #include <stdlib.h> /* for exit() */
35 
36 #include <setjmp.h>
37 #include "processes.h"
38 #include "sighand.h"
39 #include "log.h"
40 
41 
42 int
43 terminate_process (pid_t pid)
44 {
45  int ret;
46 
47  if (pid <= 0)
48  return 0;
49 
50  ret = kill (pid, SIGTERM);
51 
52  if (ret == 0)
53  {
54  usleep (1000);
55  if (waitpid (pid, NULL, WNOHANG) >= 0)
56  kill (pid, SIGKILL);
57  }
58  return -1;
59 }
60 
61 static void
62 init_child_signal_handlers ()
63 {
64  /* SIGHUP is only for reloading main scanner process. */
65  openvas_signal (SIGHUP, SIG_IGN);
66  openvas_signal (SIGTERM, make_em_die);
67  openvas_signal (SIGINT, make_em_die);
68  openvas_signal (SIGQUIT, make_em_die);
69  openvas_signal (SIGSEGV, sighand_segv);
70  openvas_signal (SIGPIPE, SIG_IGN);
71 }
72 
76 pid_t
77 create_process (process_func_t function, void *argument)
78 {
79  int pid;
80 
81  pid = fork ();
82 
83  if (pid == 0)
84  {
85  init_child_signal_handlers ();
86  srand48 (getpid () + getppid () + (long) time (NULL));
87  (*function) (argument);
88  exit (0);
89  }
90  if (pid < 0)
91  log_write ("Error : could not fork ! Error : %s", strerror (errno));
92  return pid;
93 }
void(*)(int) openvas_signal(int signum, void(*handler)(int))
Definition: sighand.c:92
void log_write(const char *str,...)
Write into the logfile / syslog.
Definition: log.c:140
void sighand_segv(int given_signal)
Definition: sighand.c:145
void make_em_die(int sig)
Definition: sighand.c:53
void(* process_func_t)(void *)
Definition: processes.h:31
int terminate_process(pid_t pid)
Definition: processes.c:43
pid_t create_process(process_func_t function, void *argument)
Create a new process (fork).
Definition: processes.c:77