OpenVAS Scanner  22.7.9
processes.h File Reference

processes.c header. More...

#include "../misc/ipc.h"
#include <sys/types.h>
Include dependency graph for processes.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define FORKFAILED   -1
 
#define NOINIT   -2
 
#define PROCSFULL   -3
 
#define NOCHILD   -4
 

Typedefs

typedef void(* process_func_t) (void *)
 

Functions

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. More...
 
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. More...
 
pid_t create_ipc_process (ipc_process_func func, void *args)
 initializes a communication channels and calls a function with a new process More...
 
const struct ipc_contextsprocs_get_ipc_contexts (void)
 returns ipc_contexts. More...
 
int procs_cleanup_children (void)
 iterates through ipcc and verify if a child is stopped or killed to free the file handler. More...
 

Detailed Description

processes.c header.

Definition in file processes.h.

Macro Definition Documentation

◆ FORKFAILED

#define FORKFAILED   -1

Definition at line 20 of file processes.h.

◆ NOCHILD

#define NOCHILD   -4

Definition at line 23 of file processes.h.

◆ NOINIT

#define NOINIT   -2

Definition at line 21 of file processes.h.

◆ PROCSFULL

#define PROCSFULL   -3

Definition at line 22 of file processes.h.

Typedef Documentation

◆ process_func_t

typedef void(* process_func_t) (void *)

Definition at line 25 of file processes.h.

Function Documentation

◆ 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 
207  ec.func = (ipc_process_func) func;
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
211 retry:
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  }
226  reuse_or_add_context (pctx);
227  child_pid = pctx->pid;
228  // ipcc works uses copies of pctx therefore we free it
229  free (pctx);
230  return child_pid;
231 }

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(), and nasl_plugin_launch().

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 }

References ipc_context::closed, ipc_contexts::ctxs, ipc_close(), ipcc, ipc_contexts::len, ipc_context::pid, 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)
122  }
123 }

References ipc_context::closed, ipc_contexts::ctxs, ipcc, ipc_contexts::len, ipc_context::pid, and terminate_process().

Referenced by handle_termination_signal().

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:
terminate_process
int terminate_process(pid_t pid)
Terminates a given process. If termination does not work, the process will get killed....
Definition: processes.c:96
ipc_contexts::len
int len
Definition: ipc.h:42
FORKFAILED
#define FORKFAILED
Definition: processes.h:20
IPC_PIPE
@ IPC_PIPE
Definition: ipc.h:13
ipc_context::pid
pid_t pid
Definition: ipc.h:36
pre_fn_call
static void pre_fn_call(struct ipc_context *ctx, void *args)
Definition: processes.c:138
ipcc
static struct ipc_contexts * ipcc
Definition: processes.c:39
ipc_contexts_init
struct ipc_contexts * ipc_contexts_init(int cap)
initializes ipc_contexts with a given preallocated capacity.
Definition: ipc.c:248
ipc_context::closed
unsigned int closed
Definition: ipc.h:35
pid
static pid_t pid
Definition: nasl_cmd_exec.c:39
ipc_exec_context
Definition: ipc.h:50
ipc_exec_context::post_func
ipc_process_func post_func
Definition: ipc.h:56
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.
Definition: processes.c:47
ipc_process_func
void(* ipc_process_func)(struct ipc_context *, void *)
Definition: ipc.h:47
reuse_or_add_context
static void reuse_or_add_context(struct ipc_context *ctx)
Definition: processes.c:166
ipc_context
Definition: ipc.h:32
ipc_close
int ipc_close(struct ipc_context *context)
closes given context
Definition: ipc.c:116
ipc_exec_context::func
ipc_process_func func
Definition: ipc.h:54
ipc_exec_context::func_arg
void * func_arg
Definition: ipc.h:58
ipc_contexts::ctxs
struct ipc_context * ctxs
Definition: ipc.h:44
free
void free(void *)
ipc_exec_as_process
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
ipc_exec_context::pre_func
ipc_process_func pre_func
Definition: ipc.h:52
post_fn_call
static void post_fn_call(struct ipc_context *ctx, void *args)
Definition: processes.c:157