OpenVAS Scanner  7.0.1~git
pluginload.c File Reference

Loads plugins from disk into memory. More...

#include "pluginload.h"
#include "../nasl/nasl.h"
#include "processes.h"
#include "sighand.h"
#include "utils.h"
#include <errno.h>
#include <glib.h>
#include <gvm/base/prefs.h>
#include <gvm/base/proctitle.h>
#include <gvm/util/nvticache.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/time.h>
#include <sys/wait.h>
Include dependency graph for pluginload.c:

Go to the source code of this file.

Macros

#define G_LOG_DOMAIN   "sd main"
 GLib log domain. More...
 

Functions

GSList * collect_nvts (const char *folder, const char *subdir, GSList *files)
 Collects all NVT files in a directory and recurses into subdirs. More...
 
int calculate_eta (struct timeval start_time, int loaded, int total)
 
void init_loading_shm (void)
 
void destroy_loading_shm (void)
 
int current_loading_plugins (void)
 
int total_loading_plugins (void)
 
void set_current_loading_plugins (int current)
 
void set_total_loading_plugins (int total)
 
static void cleanup_leftovers (int num_files)
 
static void plugins_reload_from_dir (void *folder)
 
static void include_dirs (void)
 
int plugins_cache_init (void)
 Main function for nvticache initialization without loading the plugins. More...
 
int plugins_init (void)
 

Variables

static int * loading_shm = NULL
 
static int loading_shmid = 0
 

Detailed Description

Loads plugins from disk into memory.

Definition in file pluginload.c.

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "sd main"

GLib log domain.

Definition at line 49 of file pluginload.c.

Function Documentation

◆ calculate_eta()

int calculate_eta ( struct timeval  start_time,
int  loaded,
int  total 
)

Definition at line 111 of file pluginload.c.

References timeval().

Referenced by plugins_reload_from_dir().

112 {
113  struct timeval current_time;
114  int elapsed, remaining;
115 
116  if (start_time.tv_sec == 0)
117  return 0;
118 
119  gettimeofday (&current_time, NULL);
120  elapsed = current_time.tv_sec - start_time.tv_sec;
121  remaining = total - loaded;
122  return (remaining * elapsed) / loaded;
123 }
struct timeval timeval(unsigned long val)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cleanup_leftovers()

static void cleanup_leftovers ( int  num_files)
static

Definition at line 230 of file pluginload.c.

Referenced by plugins_reload_from_dir().

231 {
232  size_t count;
233  GSList *oids, *element;
234 
235  proctitle_set ("openvas: Cleaning leftover NVTs.");
236 
237  count = nvticache_count ();
238  if ((int) count <= num_files)
239  return;
240 
241  oids = element = nvticache_get_oids ();
242  while (element)
243  {
244  char *path = nvticache_get_src (element->data);
245 
246  if (!g_file_test (path, G_FILE_TEST_EXISTS))
247  nvticache_delete (element->data);
248  g_free (path);
249  element = element->next;
250  }
251  g_slist_free_full (oids, g_free);
252 }
Here is the caller graph for this function:

◆ collect_nvts()

GSList* collect_nvts ( const char *  folder,
const char *  subdir,
GSList *  files 
)

Collects all NVT files in a directory and recurses into subdirs.

Parameters
folderThe main directory from where to descend and collect.
subdirA subdirectory to consider for the collection: "folder/subdir" is thus the effective directory to descend from. "subdir" can be "" to make "folder" the effective start.
filesA list that is extended with all found files. If it is NULL, a new list is created automatically.
Returns
Parameter "files", extended with all the NVT files found in "folder" and its subdirectories. Not added are directory names. NVT files are identified by the defined filename suffixes.

Definition at line 68 of file pluginload.c.

Referenced by plugins_reload_from_dir().

69 {
70  GDir *dir;
71  const gchar *fname;
72 
73  if (folder == NULL)
74  return files;
75 
76  dir = g_dir_open (folder, 0, NULL);
77  if (dir == NULL)
78  return files;
79 
80  fname = g_dir_read_name (dir);
81  while (fname)
82  {
83  char *path;
84 
85  path = g_build_filename (folder, fname, NULL);
86  if (g_file_test (path, G_FILE_TEST_IS_DIR))
87  {
88  char *new_folder, *new_subdir;
89 
90  new_folder = g_build_filename (folder, fname, NULL);
91  new_subdir = g_build_filename (subdir, fname, NULL);
92 
93  files = collect_nvts (new_folder, new_subdir, files);
94 
95  if (new_folder)
96  g_free (new_folder);
97  if (new_subdir)
98  g_free (new_subdir);
99  }
100  else if (g_str_has_suffix (fname, ".nasl"))
101  files = g_slist_prepend (files, g_build_filename (subdir, fname, NULL));
102  g_free (path);
103  fname = g_dir_read_name (dir);
104  }
105 
106  g_dir_close (dir);
107  return files;
108 }
GSList * collect_nvts(const char *folder, const char *subdir, GSList *files)
Collects all NVT files in a directory and recurses into subdirs.
Definition: pluginload.c:68
Here is the caller graph for this function:

◆ current_loading_plugins()

int current_loading_plugins ( void  )

Definition at line 184 of file pluginload.c.

References loading_shm.

185 {
186  return loading_shm ? loading_shm[0] : 0;
187 }
static int * loading_shm
Definition: pluginload.c:125

◆ destroy_loading_shm()

void destroy_loading_shm ( void  )

Definition at line 166 of file pluginload.c.

References loading_shm, and loading_shmid.

167 {
168  if (loading_shm)
169  {
170  shmdt (loading_shm);
171  if (shmctl (loading_shmid, IPC_RMID, NULL))
172  perror ("shmctl");
173  loading_shm = NULL;
174  loading_shmid = 0;
175  }
176 }
static int loading_shmid
Definition: pluginload.c:126
static int * loading_shm
Definition: pluginload.c:125

◆ include_dirs()

static void include_dirs ( void  )
static

Definition at line 331 of file pluginload.c.

References add_nasl_inc_dir().

Referenced by plugins_cache_init().

332 {
333  const gchar *pref_include_folders;
334 
335  add_nasl_inc_dir (""); // for absolute and relative paths
336  pref_include_folders = prefs_get ("include_folders");
337  if (pref_include_folders != NULL)
338  {
339  gchar **include_folders = g_strsplit (pref_include_folders, ":", 0);
340  unsigned int i = 0;
341 
342  for (i = 0; i < g_strv_length (include_folders); i++)
343  {
344  int result = add_nasl_inc_dir (include_folders[i]);
345  if (result < 0)
346  g_debug ("Could not add %s to the list of include folders.\n"
347  "Make sure %s exists and is a directory.\n",
348  include_folders[i], include_folders[i]);
349  }
350 
351  g_strfreev (include_folders);
352  }
353 }
int add_nasl_inc_dir(const char *)
Adds the given string as directory for searching for includes.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_loading_shm()

void init_loading_shm ( void  )

Definition at line 133 of file pluginload.c.

References loading_shm, and loading_shmid.

134 {
135  int shm_key;
136 
137  if (loading_shm)
138  return;
139 
140  shm_key = rand () + 1;
141  /*
142  * Create shared memory segment if it doesn't exist.
143  * This will be used to communicate current plugins loading progress to other
144  * processes.
145  * loading_shm[0]: Number of loaded plugins.
146  * loading_shm[1]: Total number of plugins.
147  */
148  loading_shmid = shmget (shm_key, sizeof (int) * 2, IPC_CREAT | 0600);
149  if (loading_shmid < 0)
150  perror ("shmget");
151  loading_shm = shmat (loading_shmid, NULL, 0);
152  if (loading_shm == (void *) -1)
153  {
154  perror ("shmat");
155  loading_shm = NULL;
156  }
157  else
158  bzero (loading_shm, sizeof (int) * 2);
159 }
static int loading_shmid
Definition: pluginload.c:126
static int * loading_shm
Definition: pluginload.c:125

◆ plugins_cache_init()

int plugins_cache_init ( void  )

Main function for nvticache initialization without loading the plugins.

Definition at line 359 of file pluginload.c.

References include_dirs().

Referenced by plugins_init(), and start_single_task_scan().

360 {
361  const char *plugins_folder = prefs_get ("plugins_folder");
362 
363  if (nvticache_init (plugins_folder, prefs_get ("db_address")))
364  {
365  g_debug ("Failed to initialize nvti cache.");
366  return -1;
367  }
368  include_dirs ();
369  return 0;
370 }
static void include_dirs(void)
Definition: pluginload.c:331
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plugins_init()

int plugins_init ( void  )

Definition at line 376 of file pluginload.c.

References create_process(), plugins_cache_init(), and plugins_reload_from_dir().

Referenced by openvas(), and reload_openvas().

377 {
378  int ret = 0;
379  pid_t child_pid;
380  const char *plugins_folder = prefs_get ("plugins_folder");
381 
382  ret = plugins_cache_init ();
383  if (ret)
384  return ret;
385 
386  child_pid = create_process (plugins_reload_from_dir, (void *) plugins_folder);
387  waitpid (child_pid, &ret, 0);
388  nvticache_save ();
389  return ret;
390 }
static void plugins_reload_from_dir(void *folder)
Definition: pluginload.c:255
pid_t create_process(process_func_t function, void *argument)
Create a new process (fork).
Definition: processes.c:97
int plugins_cache_init(void)
Main function for nvticache initialization without loading the plugins.
Definition: pluginload.c:359
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plugins_reload_from_dir()

static void plugins_reload_from_dir ( void *  folder)
static

Definition at line 255 of file pluginload.c.

References calculate_eta(), cleanup_leftovers(), collect_nvts(), name, nasl_clean_inc(), nasl_plugin_add(), openvas_signal, set_current_loading_plugins(), set_total_loading_plugins(), and timeval().

Referenced by plugins_init().

256 {
257  GSList *files = NULL, *f;
258  int loaded_files = 0, num_files = 0;
259  struct timeval start_time;
260 
261  openvas_signal (SIGTERM, SIG_DFL);
262  if (folder == NULL)
263  {
264  g_debug ("%s:%d : folder == NULL", __FILE__, __LINE__);
265  g_debug ("Could not determine the value of <plugins_folder>. "
266  " Check %s\n",
267  (char *) prefs_get ("config_file"));
268  exit (1);
269  }
270 
271  files = collect_nvts (folder, "", files);
272  num_files = g_slist_length (files);
273 
274  /*
275  * Add the plugins
276  */
277 
278  if (gettimeofday (&start_time, NULL))
279  {
280  bzero (&start_time, sizeof (start_time));
281  g_debug ("gettimeofday: %s", strerror (errno));
282  }
283  f = files;
284  set_total_loading_plugins (num_files);
285  while (f != NULL)
286  {
287  static int err_count = 0;
288  char *name = f->data;
289 
290  loaded_files++;
291  if (loaded_files % 50 == 0)
292  {
293  int percentile, eta;
294 
295  set_current_loading_plugins (loaded_files);
296  percentile = (loaded_files * 100) / num_files;
297  eta = calculate_eta (start_time, loaded_files, num_files);
298  proctitle_set ("openvas: Reloaded %d of %d NVTs"
299  " (%d%% / ETA: %02d:%02d)",
300  loaded_files, num_files, percentile, eta / 60,
301  eta % 60);
302  }
303  if (prefs_get_bool ("log_plugins_name_at_load"))
304  g_message ("Loading %s", name);
305  if (g_str_has_suffix (name, ".nasl"))
306  {
307  if (nasl_plugin_add (folder, name))
308  err_count++;
309  }
310 
311  if (err_count == 20)
312  {
313  g_debug ("Stopped loading plugins: High number of errors.");
314  proctitle_set ("openvas: Error loading NVTs.");
315  g_slist_free_full (files, g_free);
316  exit (1);
317  }
318  f = g_slist_next (f);
319  }
320 
321  cleanup_leftovers (num_files);
322  g_slist_free_full (files, g_free);
323  nasl_clean_inc ();
324 
325  proctitle_set ("openvas: Reloaded all the NVTs.");
326 
327  exit (0);
328 }
void(*)(int) openvas_signal(int signum, void(*handler)(int))
Definition: sighand.c:87
void nasl_clean_inc(void)
void set_current_loading_plugins(int current)
Definition: pluginload.c:206
int nasl_plugin_add(char *folder, char *filename)
Add one .nasl plugin to the plugin list.
Definition: nasl_plugins.c:99
int calculate_eta(struct timeval start_time, int loaded, int total)
Definition: pluginload.c:111
const char * name
Definition: nasl_init.c:377
struct timeval timeval(unsigned long val)
void set_total_loading_plugins(int total)
Definition: pluginload.c:218
GSList * collect_nvts(const char *folder, const char *subdir, GSList *files)
Collects all NVT files in a directory and recurses into subdirs.
Definition: pluginload.c:68
static void cleanup_leftovers(int num_files)
Definition: pluginload.c:230
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_current_loading_plugins()

void set_current_loading_plugins ( int  current)

Definition at line 206 of file pluginload.c.

References loading_shm.

Referenced by plugins_reload_from_dir().

207 {
208  if (loading_shm)
209  loading_shm[0] = current;
210 }
static int * loading_shm
Definition: pluginload.c:125
Here is the caller graph for this function:

◆ set_total_loading_plugins()

void set_total_loading_plugins ( int  total)

Definition at line 218 of file pluginload.c.

References loading_shm.

Referenced by plugins_reload_from_dir().

219 {
220  if (loading_shm)
221  loading_shm[1] = total;
222 }
static int * loading_shm
Definition: pluginload.c:125
Here is the caller graph for this function:

◆ total_loading_plugins()

int total_loading_plugins ( void  )

Definition at line 195 of file pluginload.c.

References loading_shm.

196 {
197  return loading_shm ? loading_shm[1] : 0;
198 }
static int * loading_shm
Definition: pluginload.c:125

Variable Documentation

◆ loading_shm

◆ loading_shmid

int loading_shmid = 0
static

Definition at line 126 of file pluginload.c.

Referenced by destroy_loading_shm(), and init_loading_shm().