Skip to content

Commit

Permalink
Fix: Stop scan (#1252)
Browse files Browse the repository at this point in the history
* Change: update code documentation. At this point there is no openvas grandparent process

* Fix: terminate process.
Always kill the process and spread the signal to child plugin processes.

* Remove: unused function
probably left after refactor.

* Change: only set the script name and not the complete path in the process title.

It makes the plugin/process name shorter and easier to see in the proc table (top, htop)
  • Loading branch information
jjnicola committed Dec 12, 2022
1 parent dbb492b commit e916780
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 43 deletions.
7 changes: 4 additions & 3 deletions src/attack.c
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,10 @@ attack_host (struct scan_globals *globals, struct in6_addr *ip,

if (check_kb_inconsistency (get_main_kb ()) != 0)
{
// As long as we don't have a proper communication channel
// to our ancestors we just kill our parent and ourselves
// (but let our grandparents live).
// We send the stop scan signal to the current parent process
// group, which is the main scan process and host processes.
// This avoid to attack new hosts and force the running host
// process to finish and spread the signal to the plugin processes
// To prevent duplicate results we don't let ACT_END run.
killpg (parent, SIGUSR1);
}
Expand Down
4 changes: 3 additions & 1 deletion src/nasl_plugins.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ nasl_thread (struct ipc_context *ipcc, struct script_infos *args)
kb_lnk_reset (get_main_kb ());
addr6_to_str (args->ip, ip_str);
// TODO extend sript_infos here
setproctitle ("openvas: testing %s (%s)", ip_str, args->name);

setproctitle ("openvas: testing %s (%s)", ip_str,
g_path_get_basename (args->name));

if (prefs_get_bool ("nasl_no_signature_check"))
nasl_mode |= NASL_ALWAYS_SIGNED;
Expand Down
45 changes: 9 additions & 36 deletions src/processes.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ clean_procs (void)

/**
* @brief Terminates a given process. If termination does not work, the process
* will get killed. In case init_procs was called, only direct child processes
* can be terminated
* will get killed.
* Terminate process can be called with the (-1 * pid) to send the signal to the
* process group.
*
* @param pid id of the child process
* @return int 0 on success, NOCHILD if child does not exist, NOINIT if not
Expand All @@ -107,29 +108,12 @@ clean_procs (void)
int
terminate_process (pid_t pid)
{
if (ipcc != NULL)
{
for (int i = 0; i < ipcc->len; i++)
{
if (ipcc->ctxs[i].pid == pid)
{
kill (pid, SIGTERM);
usleep (10000);
if (!ipcc->ctxs[i].closed)
kill (pid, SIGKILL);
return 0;
}
}
return NOCHILD;
}
else
{
kill (pid, SIGTERM);
usleep (10000);
if (waitpid (pid, NULL, WNOHANG))
kill (pid, SIGKILL);
return 0;
}
kill (pid, SIGTERM);
usleep (10000);
if (waitpid (pid, NULL, WNOHANG))
kill (pid, SIGKILL);

return 0;
}

/**
Expand All @@ -151,17 +135,6 @@ procs_terminate_childs (void)
}
}

/**
* @brief Init procs, must be called once per process
*
* @param max
*/
void
procs_init (int cap)
{
ipcc = ipc_contexts_init (cap);
}

static void
init_child_signal_handlers (void)
{
Expand Down
3 changes: 0 additions & 3 deletions src/processes.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@

typedef void (*process_func_t) (void *);

void
procs_init (int cap);

void
procs_terminate_childs (void);

Expand Down

0 comments on commit e916780

Please sign in to comment.