30 #include <openvas/nasl/nasl.h> 31 #include <openvas/base/nvticache.h> 32 #include <openvas/misc/openvas_proctitle.h> 33 #include <openvas/misc/prefs.h> 66 collect_nvts (
const char *folder,
const char *subdir, GSList * files)
74 dir = g_dir_open (folder, 0, NULL);
78 fname = g_dir_read_name (dir);
83 path = g_build_filename (folder, fname, NULL);
84 if (g_file_test (path, G_FILE_TEST_IS_DIR))
86 char *new_folder, *new_subdir;
88 new_folder = g_build_filename (folder, fname, NULL);
89 new_subdir = g_build_filename (subdir, fname, NULL);
98 else if (g_str_has_suffix (fname,
".nasl"))
99 files = g_slist_prepend (files,
100 g_build_filename (subdir, fname, NULL));
102 fname = g_dir_read_name (dir);
112 struct timeval current_time;
113 int elapsed, remaining;
115 if (start_time.tv_sec == 0)
118 gettimeofday (¤t_time, NULL);
119 elapsed = current_time.tv_sec - start_time.tv_sec;
120 remaining = total - loaded;
121 return (remaining * elapsed) / loaded;
124 static int *loading_shm = NULL;
125 static int loading_shmid = 0;
139 shm_key = rand () + 1;
147 loading_shmid = shmget (shm_key,
sizeof (
int) * 2, IPC_CREAT | 0600);
148 if (loading_shmid < 0)
150 loading_shm = shmat (loading_shmid, NULL, 0);
151 if (loading_shm == (
void *) -1)
157 bzero (loading_shm,
sizeof (
int) * 2);
170 if (shmctl (loading_shmid, IPC_RMID, NULL))
185 return loading_shm ? loading_shm[0] : 0;
196 return loading_shm ? loading_shm[1] : 0;
208 loading_shm[0] = current;
220 loading_shm[1] = total;
229 cleanup_leftovers (
int num_files)
232 GSList *oids, *element;
234 proctitle_set (
"openvassd: Cleaning leftover NVTs.");
236 count = nvticache_count ();
237 if ((
int) count <= num_files)
240 oids = element = nvticache_get_oids ();
243 char *path = nvticache_get_src (element->data);
245 if (!g_file_test (path, G_FILE_TEST_EXISTS))
246 nvticache_delete (element->data);
248 element = element->next;
250 g_slist_free_full (oids, g_free);
254 plugins_reload_from_dir (
void *folder)
256 GSList *files = NULL, *f;
257 int loaded_files = 0, num_files = 0;
258 struct timeval start_time;
265 log_write (
"%s:%d : folder == NULL", __FILE__, __LINE__);
267 log_write (
"Could not determine the value of <plugins_folder>. " 268 " Check %s\n", (
char *) prefs_get (
"config_file"));
273 num_files = g_slist_length (files);
279 if (gettimeofday (&start_time, NULL))
281 bzero (&start_time,
sizeof (start_time));
282 log_write (
"gettimeofday: %s", strerror (errno));
288 static int err_count = 0;
289 char *name = f->data;
292 if (loaded_files % 50 == 0)
297 percentile = (loaded_files * 100) / num_files;
299 proctitle_set (
"openvassd: Reloaded %d of %d NVTs" 300 " (%d%% / ETA: %02d:%02d)", loaded_files, num_files,
301 percentile, eta / 60, eta % 60);
303 if (prefs_get_bool (
"log_plugins_name_at_load"))
305 if (g_str_has_suffix (name,
".nasl"))
313 log_write (
"Stopped loading plugins: High number of errors.");
314 proctitle_set (
"openvassd: Error loading NVTs.");
315 g_slist_free_full (files, g_free);
318 f = g_slist_next (f);
321 cleanup_leftovers (num_files);
322 g_slist_free_full (files, g_free);
324 proctitle_set (
"openvassd: Reloaded all the NVTs.");
332 const gchar *pref_include_folders;
334 add_nasl_inc_dir (
"");
335 pref_include_folders = prefs_get (
"include_folders");
336 if (pref_include_folders != NULL)
338 gchar **include_folders = g_strsplit (pref_include_folders,
":", 0);
341 for (i = 0; i < g_strv_length (include_folders); i++)
343 int result = add_nasl_inc_dir (include_folders[i]);
345 g_debug (
"Could not add %s to the list of include folders.\n" 346 "Make sure %s exists and is a directory.\n",
347 include_folders[i], include_folders[i]);
350 g_strfreev (include_folders);
362 const char *plugins_folder = prefs_get (
"plugins_folder");
364 if (nvticache_init (plugins_folder, prefs_get (
"kb_location")))
366 log_write (
"Failed to initialize nvti cache.");
371 child_pid =
create_process (plugins_reload_from_dir, (
void *) plugins_folder);
372 waitpid (child_pid, &ret, 0);
void(*)(int) openvas_signal(int signum, void(*handler)(int))
void log_write(const char *str,...)
Write into the logfile / syslog.
int total_loading_plugins(void)
void set_current_loading_plugins(int current)
int nasl_plugin_add(char *folder, char *filename)
The nasl - plugin class. Loads or launches nasl- plugins.
int current_loading_plugins(void)
void destroy_loading_shm(void)
int calculate_eta(struct timeval start_time, int loaded, int total)
void init_loading_shm(void)
void set_total_loading_plugins(int total)
GSList * collect_nvts(const char *folder, const char *subdir, GSList *files)
Collects all NVT files in a directory and recurses into subdirs.
pid_t create_process(process_func_t function, void *argument)
Create a new process (fork).