OpenVAS Scanner  5.1.3
pluginload.c File Reference
#include <stdio.h>
#include <openvas/nasl/nasl.h>
#include <openvas/base/nvticache.h>
#include <openvas/misc/openvas_proctitle.h>
#include <openvas/misc/prefs.h>
#include <glib.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/shm.h>
#include <string.h>
#include <errno.h>
#include "utils.h"
#include "pluginload.h"
#include "log.h"
#include "processes.h"
#include "sighand.h"
Include dependency graph for pluginload.c:

Go to the source code of this file.

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)
 
int plugins_init (void)
 

Function Documentation

◆ calculate_eta()

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

Definition at line 110 of file pluginload.c.

111 {
112  struct timeval current_time;
113  int elapsed, remaining;
114 
115  if (start_time.tv_sec == 0)
116  return 0;
117 
118  gettimeofday (&current_time, NULL);
119  elapsed = current_time.tv_sec - start_time.tv_sec;
120  remaining = total - loaded;
121  return (remaining * elapsed) / loaded;
122 }

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

67 {
68  GDir *dir;
69  const gchar *fname;
70 
71  if (folder == NULL)
72  return files;
73 
74  dir = g_dir_open (folder, 0, NULL);
75  if (dir == NULL)
76  return files;
77 
78  fname = g_dir_read_name (dir);
79  while (fname)
80  {
81  char *path;
82 
83  path = g_build_filename (folder, fname, NULL);
84  if (g_file_test (path, G_FILE_TEST_IS_DIR))
85  {
86  char *new_folder, *new_subdir;
87 
88  new_folder = g_build_filename (folder, fname, NULL);
89  new_subdir = g_build_filename (subdir, fname, NULL);
90 
91  files = collect_nvts (new_folder, new_subdir, files);
92 
93  if (new_folder)
94  g_free (new_folder);
95  if (new_subdir)
96  g_free (new_subdir);
97  }
98  else if (g_str_has_suffix (fname, ".nasl"))
99  files = g_slist_prepend (files,
100  g_build_filename (subdir, fname, NULL));
101  g_free (path);
102  fname = g_dir_read_name (dir);
103  }
104 
105  g_dir_close (dir);
106  return files;
107 }
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:66

◆ current_loading_plugins()

int current_loading_plugins ( void  )

Definition at line 183 of file pluginload.c.

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

Referenced by comm_loading().

Here is the caller graph for this function:

◆ destroy_loading_shm()

void destroy_loading_shm ( void  )

Definition at line 165 of file pluginload.c.

166 {
167  if (loading_shm)
168  {
169  shmdt (loading_shm);
170  if (shmctl (loading_shmid, IPC_RMID, NULL))
171  perror ("shmctl");
172  loading_shm = NULL;
173  loading_shmid = 0;
174  }
175 }

Referenced by loading_handler_stop().

Here is the caller graph for this function:

◆ init_loading_shm()

void init_loading_shm ( void  )

Definition at line 132 of file pluginload.c.

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

◆ plugins_init()

int plugins_init ( void  )

Definition at line 358 of file pluginload.c.

359 {
360  int ret = 0;
361  pid_t child_pid;
362  const char *plugins_folder = prefs_get ("plugins_folder");
363 
364  if (nvticache_init (plugins_folder, prefs_get ("kb_location")))
365  {
366  log_write ("Failed to initialize nvti cache.");
367  return -1;
368  }
369  include_dirs ();
370 
371  child_pid = create_process (plugins_reload_from_dir, (void *) plugins_folder);
372  waitpid (child_pid, &ret, 0);
373  return ret;
374 }
void log_write(const char *str,...)
Write into the logfile / syslog.
Definition: log.c:140
pid_t create_process(process_func_t function, void *argument)
Create a new process (fork).
Definition: processes.c:77

References log_write().

Here is the call graph for this function:

◆ set_current_loading_plugins()

void set_current_loading_plugins ( int  current)

Definition at line 205 of file pluginload.c.

206 {
207  if (loading_shm)
208  loading_shm[0] = current;
209 }

◆ set_total_loading_plugins()

void set_total_loading_plugins ( int  total)

Definition at line 217 of file pluginload.c.

218 {
219  if (loading_shm)
220  loading_shm[1] = total;
221 }

◆ total_loading_plugins()

int total_loading_plugins ( void  )

Definition at line 194 of file pluginload.c.

195 {
196  return loading_shm ? loading_shm[1] : 0;
197 }

Referenced by comm_loading().

Here is the caller graph for this function: