OpenVAS Scanner  22.7.9
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 <bsd/unistd.h>
#include <errno.h>
#include <glib.h>
#include <gvm/base/prefs.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

static GSList * collect_nvts (const char *folder, const char *subdir, GSList *files)
 Collects all NVT files in a directory and recurses into subdirs. More...
 
static 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)
 
static void set_current_loading_plugins (int current)
 
static void set_total_loading_plugins (int total)
 
static void cleanup_leftovers (int num_files)
 
static int plugins_reload_from_dir (const char *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)
 main function for loading all the plugins More...
 

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 36 of file pluginload.c.

Function Documentation

◆ calculate_eta()

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

Definition at line 98 of file pluginload.c.

99 {
100  struct timeval current_time;
101  int elapsed, remaining;
102 
103  if (start_time.tv_sec == 0)
104  return 0;
105 
106  gettimeofday (&current_time, NULL);
107  elapsed = current_time.tv_sec - start_time.tv_sec;
108  remaining = total - loaded;
109  return (remaining * elapsed) / loaded;
110 }

References timeval().

Referenced by plugins_reload_from_dir().

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 217 of file pluginload.c.

218 {
219  size_t count;
220  GSList *oids, *element;
221 
222  setproctitle ("openvas: Cleaning leftover NVTs.");
223 
224  count = nvticache_count ();
225  if ((int) count <= num_files)
226  return;
227 
228  oids = element = nvticache_get_oids ();
229  while (element)
230  {
231  char *path = nvticache_get_src (element->data);
232 
233  if (!g_file_test (path, G_FILE_TEST_EXISTS))
234  nvticache_delete (element->data);
235  g_free (path);
236  element = element->next;
237  }
238  g_slist_free_full (oids, g_free);
239 }

Referenced by plugins_reload_from_dir().

Here is the caller graph for this function:

◆ collect_nvts()

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

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 55 of file pluginload.c.

56 {
57  GDir *dir;
58  const gchar *fname;
59 
60  if (folder == NULL)
61  return files;
62 
63  dir = g_dir_open (folder, 0, NULL);
64  if (dir == NULL)
65  return files;
66 
67  fname = g_dir_read_name (dir);
68  while (fname)
69  {
70  char *path;
71 
72  path = g_build_filename (folder, fname, NULL);
73  if (g_file_test (path, G_FILE_TEST_IS_DIR))
74  {
75  char *new_folder, *new_subdir;
76 
77  new_folder = g_build_filename (folder, fname, NULL);
78  new_subdir = g_build_filename (subdir, fname, NULL);
79 
80  files = collect_nvts (new_folder, new_subdir, files);
81 
82  if (new_folder)
83  g_free (new_folder);
84  if (new_subdir)
85  g_free (new_subdir);
86  }
87  else if (g_str_has_suffix (fname, ".nasl"))
88  files = g_slist_prepend (files, g_build_filename (subdir, fname, NULL));
89  g_free (path);
90  fname = g_dir_read_name (dir);
91  }
92 
93  g_dir_close (dir);
94  return files;
95 }

Referenced by plugins_reload_from_dir().

Here is the caller graph for this function:

◆ current_loading_plugins()

int current_loading_plugins ( void  )

Definition at line 171 of file pluginload.c.

172 {
173  return loading_shm ? loading_shm[0] : 0;
174 }

References loading_shm.

◆ destroy_loading_shm()

void destroy_loading_shm ( void  )

Definition at line 153 of file pluginload.c.

154 {
155  if (loading_shm)
156  {
157  shmdt (loading_shm);
158  if (shmctl (loading_shmid, IPC_RMID, NULL))
159  perror ("shmctl");
160  loading_shm = NULL;
161  loading_shmid = 0;
162  }
163 }

References loading_shm, and loading_shmid.

◆ include_dirs()

static void include_dirs ( void  )
static

Definition at line 318 of file pluginload.c.

319 {
320  const gchar *pref_include_folders;
321 
322  add_nasl_inc_dir (""); // for absolute and relative paths
323  pref_include_folders = prefs_get ("include_folders");
324  if (pref_include_folders != NULL)
325  {
326  gchar **include_folders = g_strsplit (pref_include_folders, ":", 0);
327  unsigned int i = 0;
328 
329  for (i = 0; i < g_strv_length (include_folders); i++)
330  {
331  int result = add_nasl_inc_dir (include_folders[i]);
332  if (result < 0)
333  g_debug ("Could not add %s to the list of include folders.\n"
334  "Make sure %s exists and is a directory.\n",
335  include_folders[i], include_folders[i]);
336  }
337 
338  g_strfreev (include_folders);
339  }
340 }

References add_nasl_inc_dir().

Referenced by plugins_cache_init().

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 120 of file pluginload.c.

121 {
122  int shm_key;
123 
124  if (loading_shm)
125  return;
126 
127  shm_key = rand () + 1;
128  /*
129  * Create shared memory segment if it doesn't exist.
130  * This will be used to communicate current plugins loading progress to other
131  * processes.
132  * loading_shm[0]: Number of loaded plugins.
133  * loading_shm[1]: Total number of plugins.
134  */
135  loading_shmid = shmget (shm_key, sizeof (int) * 2, IPC_CREAT | 0600);
136  if (loading_shmid < 0)
137  perror ("shmget");
138  loading_shm = shmat (loading_shmid, NULL, 0);
139  if (loading_shm == (void *) -1)
140  {
141  perror ("shmat");
142  loading_shm = NULL;
143  }
144  else
145  bzero (loading_shm, sizeof (int) * 2);
146 }

References loading_shm, and loading_shmid.

◆ plugins_cache_init()

int plugins_cache_init ( void  )

Main function for nvticache initialization without loading the plugins.

Returns
0 on success, -1 on failure.

Definition at line 348 of file pluginload.c.

349 {
350  int ret;
351  const char *plugins_folder = prefs_get ("plugins_folder");
352 
353  if (nvticache_init (plugins_folder, prefs_get ("db_address")))
354  {
355  g_debug ("Failed to initialize nvti cache.");
356  return -1;
357  }
358  include_dirs ();
359  ret = nasl_file_check (plugins_folder, "plugin_feed_info.inc");
360  if (ret)
361  return -1;
362 
363  return 0;
364 }

References include_dirs(), and nasl_file_check().

Referenced by attack_network_init(), openvas(), and plugins_init().

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

◆ plugins_init()

int plugins_init ( void  )

main function for loading all the plugins

Returns
0 on success, !=0 on failure.

Definition at line 372 of file pluginload.c.

373 {
374  int ret = 0;
375  const char *plugins_folder = prefs_get ("plugins_folder");
376 
377  ret = plugins_cache_init ();
378  if (ret)
379  return ret;
380 
381  ret = plugins_reload_from_dir (plugins_folder);
382  nvticache_save ();
383  return ret;
384 }

References plugins_cache_init(), and plugins_reload_from_dir().

Referenced by openvas().

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

◆ plugins_reload_from_dir()

static int plugins_reload_from_dir ( const char *  folder)
static

Definition at line 242 of file pluginload.c.

243 {
244  GSList *files = NULL, *f;
245  int loaded_files = 0, num_files = 0;
246  struct timeval start_time;
247 
248  openvas_signal (SIGTERM, SIG_DFL);
249  if (folder == NULL)
250  {
251  g_debug ("%s:%d : folder == NULL", __FILE__, __LINE__);
252  g_debug ("Could not determine the value of <plugins_folder>. "
253  " Check %s\n",
254  (char *) prefs_get ("config_file"));
255  return 1;
256  }
257 
258  files = collect_nvts (folder, "", files);
259  num_files = g_slist_length (files);
260 
261  /*
262  * Add the plugins
263  */
264 
265  if (gettimeofday (&start_time, NULL))
266  {
267  bzero (&start_time, sizeof (start_time));
268  g_debug ("gettimeofday: %s", strerror (errno));
269  }
270  f = files;
271  set_total_loading_plugins (num_files);
272  while (f != NULL)
273  {
274  static int err_count = 0;
275  char *name = f->data;
276 
277  loaded_files++;
278  if (loaded_files % 50 == 0)
279  {
280  int percentile, eta;
281 
282  set_current_loading_plugins (loaded_files);
283  percentile = (loaded_files * 100) / num_files;
284  eta = calculate_eta (start_time, loaded_files, num_files);
285  setproctitle ("openvas: Reloaded %d of %d NVTs"
286  " (%d%% / ETA: %02d:%02d)",
287  loaded_files, num_files, percentile, eta / 60,
288  eta % 60);
289  }
290  if (prefs_get_bool ("log_plugins_name_at_load"))
291  g_message ("Loading %s", name);
292  if (g_str_has_suffix (name, ".nasl"))
293  {
294  if (nasl_plugin_add (folder, name))
295  err_count++;
296  }
297 
298  if (err_count == 20)
299  {
300  g_debug ("Stopped loading plugins: High number of errors.");
301  setproctitle ("openvas: Error loading NVTs.");
302  g_slist_free_full (files, g_free);
303  return 1;
304  }
305  f = g_slist_next (f);
306  }
307 
308  cleanup_leftovers (num_files);
309  g_slist_free_full (files, g_free);
310  nasl_clean_inc ();
311 
312  setproctitle ("openvas: Reloaded all the NVTs.");
313 
314  return 0;
315 }

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().

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

◆ set_current_loading_plugins()

static void set_current_loading_plugins ( int  current)
static

Definition at line 193 of file pluginload.c.

194 {
195  if (loading_shm)
196  loading_shm[0] = current;
197 }

References loading_shm.

Referenced by plugins_reload_from_dir().

Here is the caller graph for this function:

◆ set_total_loading_plugins()

static void set_total_loading_plugins ( int  total)
static

Definition at line 205 of file pluginload.c.

206 {
207  if (loading_shm)
208  loading_shm[1] = total;
209 }

References loading_shm.

Referenced by plugins_reload_from_dir().

Here is the caller graph for this function:

◆ total_loading_plugins()

int total_loading_plugins ( void  )

Definition at line 182 of file pluginload.c.

183 {
184  return loading_shm ? loading_shm[1] : 0;
185 }

References loading_shm.

Variable Documentation

◆ loading_shm

◆ loading_shmid

int loading_shmid = 0
static

Definition at line 113 of file pluginload.c.

Referenced by destroy_loading_shm(), and init_loading_shm().

add_nasl_inc_dir
int add_nasl_inc_dir(const char *)
Adds the given string as directory for searching for includes.
Definition: nasl_grammar.tab.c:2539
plugins_reload_from_dir
static int plugins_reload_from_dir(const char *folder)
Definition: pluginload.c:242
nasl_plugin_add
int nasl_plugin_add(const char *folder, char *filename)
Add one .nasl plugin to the plugin list.
Definition: nasl_plugins.c:120
include_dirs
static void include_dirs(void)
Definition: pluginload.c:318
loading_shmid
static int loading_shmid
Definition: pluginload.c:113
collect_nvts
static 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:55
name
const char * name
Definition: nasl_init.c:411
openvas_signal
void(*)(int) openvas_signal(int signum, void(*handler)(int))
Definition: sighand.c:79
nasl_file_check
int nasl_file_check(const char *folder, const char *filename)
Check a single .nasl/.inc file.
Definition: nasl_plugins.c:83
loading_shm
static int * loading_shm
Definition: pluginload.c:112
timeval
static struct timeval timeval(unsigned long val)
Definition: nasl_builtin_synscan.c:94
set_total_loading_plugins
static void set_total_loading_plugins(int total)
Definition: pluginload.c:205
cleanup_leftovers
static void cleanup_leftovers(int num_files)
Definition: pluginload.c:217
calculate_eta
static int calculate_eta(struct timeval start_time, int loaded, int total)
Definition: pluginload.c:98
set_current_loading_plugins
static void set_current_loading_plugins(int current)
Definition: pluginload.c:193
plugins_cache_init
int plugins_cache_init(void)
Main function for nvticache initialization without loading the plugins.
Definition: pluginload.c:348
nasl_clean_inc
void nasl_clean_inc(void)
Definition: nasl_grammar.tab.c:2806