OpenVAS Scanner 23.40.3
processes.c File Reference

Creates new threads. More...

#include "processes.h"
#include "../misc/kb_cache.h"
#include "debug_utils.h"
#include "sighand.h"
#include <errno.h>
#include <glib.h>
#include <gvm/base/logging.h>
#include <gvm/util/mqtt.h>
#include <setjmp.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
Include dependency graph for processes.c:

Go to the source code of this file.

Macros

#define G_LOG_DOMAIN   "sd main"
 GLib log domain.

Functions

int procs_cleanup_children (void)
 iterates through ipcc and verify if a child is stopped or killed to free the file handler.
static void clean_procs (void)
 Cleans the process list and frees memory. This will not terminate child processes. Is primarily used after fork.
int terminate_process (pid_t pid)
 Terminates a given process. If termination does not work, the process will get killed. Terminate process can be called with the (-1 * pid) to send the signal to the process group.
void procs_terminate_childs (void)
 This function terminates all processes spawned with create_process. Calls terminate_child for each process active. In case init_procs was not called this function does nothing.
static void init_child_signal_handlers (void)
static void pre_fn_call (struct ipc_context *ctx, void *args)
static void post_fn_call (struct ipc_context *ctx, void *args)
static void reuse_or_add_context (struct ipc_context *ctx)
pid_t create_ipc_process (ipc_process_func func, void *args)
 initializes a communication channels and calls a function with a new process
const struct ipc_contextsprocs_get_ipc_contexts (void)
 returns ipc_contexts.

Variables

static struct ipc_contextsipcc = NULL

Detailed Description

Creates new threads.

Definition in file processes.c.

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "sd main"

GLib log domain.

Definition at line 35 of file processes.c.

Function Documentation

◆ clean_procs()

void clean_procs ( void )
static

Cleans the process list and frees memory. This will not terminate child processes. Is primarily used after fork.

Definition at line 79 of file processes.c.

80{
82 ipcc = NULL;
83}
int ipc_destroy_contexts(struct ipc_contexts *ctxs)
destroys given contexts
Definition ipc.c:305
static struct ipc_contexts * ipcc
Definition processes.c:39

References ipc_destroy_contexts(), and ipcc.

Referenced by pre_fn_call().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ create_ipc_process()

pid_t create_ipc_process ( ipc_process_func func,
void * args )

initializes a communication channels and calls a function with a new process

Parameters
funcFunction to call
argsarguments
Returns
pid of spawned process on success or one of the following errors: FORKFAILED

Definition at line 195 of file processes.c.

196{
197 struct ipc_context *pctx = NULL;
198 struct ipc_exec_context ec = {0};
199 pid_t child_pid;
200 // previously init call, we want to store the contexts without making
201 // assumptions about signal handlung
202 if (ipcc == NULL)
203 ipcc = ipc_contexts_init (10);
204
208 ec.func_arg = args;
209 // check for exited processes and clean file descriptor
210 // we do it twice, before forking and when forking fails with EMFILE or EAGAIN
211retry:
212 g_debug ("%s: closed %d fd.", __func__, procs_cleanup_children ());
213 if ((pctx = ipc_exec_as_process (IPC_PIPE, ec)) == NULL)
214 {
215 if (errno == EMFILE || errno == EAGAIN)
216 {
217 g_debug (
218 "%s: could not fork: %s (%d) retrying after trying to close fd.",
219 __func__, strerror (errno), errno);
220 goto retry;
221 }
222 g_warning ("%s: could not fork: %s (%d)", __func__, strerror (errno),
223 errno);
224 return FORKFAILED;
225 }
227 child_pid = pctx->pid;
228 // ipcc works uses copies of pctx therefore we free it
229 free (pctx);
230 return child_pid;
231}
struct ipc_context * ipc_exec_as_process(enum ipc_protocol type, struct ipc_exec_context exec_ctx)
runs given functions with the given protocol type.
Definition ipc.c:175
struct ipc_contexts * ipc_contexts_init(int cap)
initializes ipc_contexts with a given preallocated capacity.
Definition ipc.c:248
void(* ipc_process_func)(struct ipc_context *, void *)
Definition ipc.h:47
@ IPC_PIPE
Definition ipc.h:13
void free(void *)
static void post_fn_call(struct ipc_context *ctx, void *args)
Definition processes.c:157
int procs_cleanup_children(void)
iterates through ipcc and verify if a child is stopped or killed to free the file handler.
Definition processes.c:47
static void reuse_or_add_context(struct ipc_context *ctx)
Definition processes.c:166
static void pre_fn_call(struct ipc_context *ctx, void *args)
Definition processes.c:138
#define FORKFAILED
Definition processes.h:20
pid_t pid
Definition ipc.h:36
ipc_process_func pre_func
Definition ipc.h:52
void * func_arg
Definition ipc.h:58
ipc_process_func post_func
Definition ipc.h:56
ipc_process_func func
Definition ipc.h:54

References FORKFAILED, free(), ipc_exec_context::func, ipc_exec_context::func_arg, ipc_contexts_init(), ipc_exec_as_process(), IPC_PIPE, ipcc, ipc_context::pid, post_fn_call(), ipc_exec_context::post_func, pre_fn_call(), ipc_exec_context::pre_func, procs_cleanup_children(), and reuse_or_add_context().

Referenced by attack_network().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_child_signal_handlers()

void init_child_signal_handlers ( void )
static

Definition at line 126 of file processes.c.

127{
128 /* SIGHUP is only for reloading main scanner process. */
129 openvas_signal (SIGHUP, SIG_IGN);
130 openvas_signal (SIGTERM, make_em_die);
131 openvas_signal (SIGINT, make_em_die);
132 openvas_signal (SIGQUIT, make_em_die);
133 openvas_signal (SIGSEGV, sighand_segv);
134 openvas_signal (SIGPIPE, SIG_IGN);
135}
void(*)(int) openvas_signal(int signum, void(*handler)(int))
Definition sighand.c:79
void sighand_segv(int given_signal)
Definition sighand.c:123
void make_em_die(int sig)
Definition sighand.c:40

References make_em_die(), openvas_signal, and sighand_segv().

Referenced by pre_fn_call().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ post_fn_call()

void post_fn_call ( struct ipc_context * ctx,
void * args )
static

Definition at line 157 of file processes.c.

158{
159 (void) ctx;
160 (void) args;
161 g_debug ("%s: called", __func__);
162 gvm_close_sentry ();
163}

Referenced by create_ipc_process().

Here is the caller graph for this function:

◆ pre_fn_call()

void pre_fn_call ( struct ipc_context * ctx,
void * args )
static

Definition at line 138 of file processes.c.

139{
140 (void) ctx;
141 (void) args;
142 // in a chuld we clean up every preexisting context
145 g_debug ("%s: called", __func__);
146 usleep (1000);
148 clean_procs ();
149 mqtt_reset ();
150 init_sentry ();
151 srand48 (getpid () + getppid () + (long) time (NULL));
152
153 g_debug ("%s: exit", __func__);
154}
int init_sentry(void)
Init sentry.
Definition debug_utils.c:23
static void init_child_signal_handlers(void)
Definition processes.c:126
static void clean_procs(void)
Cleans the process list and frees memory. This will not terminate child processes....
Definition processes.c:79

References clean_procs(), init_child_signal_handlers(), init_sentry(), ipc_contexts_init(), ipc_destroy_contexts(), and ipcc.

Referenced by create_ipc_process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ procs_cleanup_children()

int procs_cleanup_children ( void )

iterates through ipcc and verify if a child is stopped or killed to free the file handler.

Returns
the amount of freed file handler or -1 on ipcc not initialized

Definition at line 47 of file processes.c.

48{
49 int freed = 0, i, status;
50 pid_t pid;
51 if (ipcc == NULL)
52 return -1;
53 g_debug ("%s: checking %d ipc.", __func__, ipcc->len);
54 for (i = 0; i < ipcc->len; i++)
55 {
56 if (ipcc->ctxs[i].closed)
57 {
58 continue;
59 }
60 pid = waitpid (ipcc->ctxs[i].pid, &status, WNOHANG);
61 if ((pid < 0)
62 || ((pid == ipcc->ctxs[i].pid)
63 && (WIFEXITED (status) || WIFSTOPPED (status)
64 || WIFSIGNALED (status))))
65 {
66 freed++;
67 ipc_close (&ipcc->ctxs[i]);
68 }
69 }
70 return freed;
71}
int ipc_close(struct ipc_context *context)
closes given context
Definition ipc.c:116
static pid_t pid

References ipc_close(), ipcc, and pid.

Referenced by create_ipc_process(), and pluginlaunch_wait_for_free_process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ procs_get_ipc_contexts()

const struct ipc_contexts * procs_get_ipc_contexts ( void )

returns ipc_contexts.

Returns
the globally hold array of all ipc_context; do not manipulate them.

Definition at line 239 of file processes.c.

240{
241 return ipcc;
242}

References ipcc.

Referenced by launch_plugin().

Here is the caller graph for this function:

◆ procs_terminate_childs()

void procs_terminate_childs ( void )

This function terminates all processes spawned with create_process. Calls terminate_child for each process active. In case init_procs was not called this function does nothing.

Definition at line 113 of file processes.c.

114{
115 if (ipcc == NULL)
116 return;
117
118 for (int i = 0; i < ipcc->len; i++)
119 {
120 if (!ipcc->ctxs[i].closed)
121 terminate_process (ipcc->ctxs[i].pid);
122 }
123}
int terminate_process(pid_t pid)
Terminates a given process. If termination does not work, the process will get killed....
Definition processes.c:96

References ipcc, and terminate_process().

Referenced by handle_termination_signal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reuse_or_add_context()

void reuse_or_add_context ( struct ipc_context * ctx)
static

Definition at line 166 of file processes.c.

167{
168 if (ipcc == NULL)
169 return;
170 for (int i = 0; i < ipcc->len; i++)
171 {
172 if (ipcc->ctxs[i].closed == 1)
173 {
174 ipcc->ctxs[i].context = ctx->context;
175 ipcc->ctxs[i].pid = ctx->pid;
176 ipcc->ctxs[i].relation = ctx->relation;
177 ipcc->ctxs[i].type = ctx->type;
178 ipcc->ctxs[i].closed = 0;
179 return;
180 }
181 }
182 ipc_add_context (ipcc, ctx);
183}
struct ipc_contexts * ipc_add_context(struct ipc_contexts *ctxs, struct ipc_context *ctx)
adds a given context to contexts
Definition ipc.c:274
void * context
Definition ipc.h:37
enum ipc_protocol type
Definition ipc.h:33
enum ipc_relation relation
Definition ipc.h:34

References ipc_context::context, ipc_add_context(), ipcc, ipc_context::pid, ipc_context::relation, and ipc_context::type.

Referenced by create_ipc_process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ terminate_process()

int terminate_process ( pid_t pid)

Terminates a given process. If termination does not work, the process will get killed. Terminate process can be called with the (-1 * pid) to send the signal to the process group.

Parameters
pidid of the child process
Returns
int 0 on success, NOCHILD if child does not exist, NOINIT if not initialized

Definition at line 96 of file processes.c.

97{
98 kill (pid, SIGTERM);
99 usleep (10000);
100 if (waitpid (pid, NULL, WNOHANG))
101 kill (pid, SIGKILL);
102
103 return 0;
104}

References pid.

Referenced by pluginlaunch_stop(), procs_terminate_childs(), and update_running_processes().

Here is the caller graph for this function:

Variable Documentation

◆ ipcc