libnasl/nasl nasl_includes.c, NONE, 1.1 nasl_includes.h, NONE, 1.1 Makefile, 1.66, 1.67 nasl_grammar.y, 1.39, 1.40
Update of /usr/local/cvs/libnasl/nasl
In directory raccoon.nessus.org:/tmp/cvs-serv22549/libnasl/nasl
Modified Files:
Makefile nasl_grammar.y
Added Files:
nasl_includes.c nasl_includes.h
Log Message:
Experimental optimization: pre-parse and remember all "include" files.
This should save CPU but I don't have any figure yet.
--- NEW FILE: nasl_includes.c ---
/* Nessus Attack Scripting Language
*
* Copyright (C) 2002 - 2004 Michel Arboi and Renaud Deraison
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* In addition, as a special exception, Renaud Deraison and Michel Arboi
* give permission to link the code of this program with any
* version of the OpenSSL library which is distributed under a
* license identical to that listed in the included COPYING.OpenSSL
* file, and distribute linked combinations including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* this file, you may extend this exception to your version of the
* file, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*
*/
#include "includes.h"
#include "nasl_includes.h"
static nasl_include_t *inclist = NULL;
static int count = 0, hits = 0;
nasl_include_t*
nasl_read_include(const char * fname)
{
naslctxt subctx;
int x;
nasl_include_t *ni, *p;
char fullpath[MAXPATHLEN];
/* Kind of normalization */
{
char fp2[MAXPATHLEN], *p1, *p2;
if (fname[0] != '/')
{
char dname[MAXPATHLEN];
if (getcwd(dname, sizeof(dname)) == NULL)
{
perror("getcwd");
return NULL;
}
snprintf(fp2, sizeof(fp2), "%s/%s", dname, fname);
p1 = fp2;
}
else
p1 = fname;
p2 = fullpath;
/* fprintf(stderr, "> %s\n", p1); */
do
{
while (p1[0] == '/' && p1[1] == '/')
p1 ++;
*p2++ = *p1 ++;
}
while (p1[-1] != '\0');
/* fprintf(stderr, ">> %s\n", fullpath); */
}
count ++;
for (p = inclist; p != NULL; p = p->next)
if (strcmp(p->name, fullpath) == 0)
{
hits ++;
#if 0
if (count % 100 == 0)
fprintf(stderr, "nasl_read_include: HIT%: %.2f\n", 100.0 * hits / count);
#endif
ref_cell(p->tree);
return p;
}
#if 0
fprintf(stderr, "nasl_read_include: MISS: %s\n", fullpath);
#endif
p = NULL;
x = init_nasl_ctx(&subctx, fullpath);
if (x >= 0)
{
if (! naslparse(&subctx))
{
p = emalloc(sizeof(nasl_include_t));
p->next = inclist; inclist = p;
p->name = estrdup(fullpath);
p->tree = subctx.tree;
p->authenticated = subctx.authenticated;
ref_cell(p->tree);
}
else
nasl_perror(NULL, "%s: Parse error at or near line %d\n",
fullpath, subctx.line_nb);
efree(&subctx.buffer);
fclose(subctx.fp);
subctx.fp = NULL;
}
return p;
}
int
nasl_preload_include_files(const char* dname)
{
char old_dir[MAXPATHLEN];
int count = 0;
DIR *dir;
struct dirent *de;
#if 0
fprintf(stderr, "nasl_preload_include_files: %s\n", dname);
#endif
if (getcwd(old_dir, sizeof(old_dir)) == NULL)
{
perror("getcwd");
*old_dir = '\0';
}
if (chdir(dname) < 0)
perror(dname);
dir = opendir(dname);
if (dir == NULL)
{
perror(dname);
count = -1;
}
else
{
while ((de = readdir(dir)) != NULL)
{
char *suff = strrchr(de->d_name, '.');
if (suff != NULL && strcmp(suff, ".inc") == 0)
{
char fname[MAXPATHLEN];
snprintf(fname, sizeof(fname), "%s/%s", dname, de->d_name);
if (nasl_read_include(fname) != NULL)
count ++;
}
}
closedir(dir);
}
if (*old_dir != '\0')
if (chdir(old_dir))
perror(old_dir);
#if 0
fprintf(stderr, "nasl_preload_include_files: %d includes found in %s\n",
count, dname);
#endif
return count;
}
--- NEW FILE: nasl_includes.h ---
/* Nessus Attack Scripting Language
*
* Copyright (C) 2002 - 2004 Michel Arboi and Renaud Deraison
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* In addition, as a special exception, Renaud Deraison and Michel Arboi
* give permission to link the code of this program with any
* version of the OpenSSL library which is distributed under a
* license identical to that listed in the included COPYING.OpenSSL
* file, and distribute linked combinations including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* this file, you may extend this exception to your version of the
* file, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*
*/
#ifndef NASL_INCLUDES_H_INCLUDED
#define NASL_INCLUDES_H_INCLUDED
#include "nasl_tree.h"
#include "nasl_global_ctxt.h"
typedef struct nasl_include {
const char *name;
int authenticated;
tree_cell *tree;
struct nasl_include *next; /* list */
} nasl_include_t;
nasl_include_t* nasl_read_include(const char*);
int nasl_preload_include_files(const char*);
#endif
Index: Makefile
===================================================================
RCS file: /usr/local/cvs/libnasl/nasl/Makefile,v
retrieving revision 1.66
retrieving revision 1.67
diff -u -d -r1.66 -r1.67
--- Makefile 8 Sep 2004 14:48:18 -0000 1.66
+++ Makefile 7 Nov 2004 17:07:22 -0000 1.67
@@ -14,6 +14,7 @@
nasl_crypto2.o \
nasl_http.o \
nasl_host.o \
+ nasl_includes.o \
nasl_text_utils.o \
nasl_nessusd_glue.o \
nasl_misc_funcs.o \
@@ -45,6 +46,7 @@
nasl_crypto2.lo \
nasl_http.lo \
nasl_host.lo \
+ nasl_includes.lo \
nasl_text_utils.lo \
nasl_nessusd_glue.lo \
nasl_misc_funcs.lo \
@@ -76,6 +78,7 @@
nasl_crypto2.c \
nasl_http.c \
nasl_host.c \
+ nasl_includes.c \
nasl_text_utils.c \
nasl_nessusd_glue.c \
nasl_misc_funcs.c \
@@ -145,7 +148,7 @@
nasl_cmd_exec.o: nasl_cmd_exec.c
$(COMPILE) -c -DNESSUS_STATE_DIR=\"$(localstatedir)\" nasl_cmd_exec.c
-
+
nasl_func.o: nasl_func.c nasl_var.h
@@ -155,6 +158,7 @@
lint.o: lint.c exec.h nasl_tree.h nasl_global_ctxt.h nasl_func.h nasl_var.h nasl_lex_ctxt.h nasl_init.h strutils.h
+nasl_includes.o: nasl_includes.c nasl_includes.h nasl_tree.h nasl_global_ctxt.h
distclean : clean
@rm -f strutils.h
Index: nasl_grammar.y
===================================================================
RCS file: /usr/local/cvs/libnasl/nasl/nasl_grammar.y,v
retrieving revision 1.39
retrieving revision 1.40
diff -u -d -r1.39 -r1.40
--- nasl_grammar.y 15 Sep 2004 20:13:56 -0000 1.39
+++ nasl_grammar.y 7 Nov 2004 17:07:22 -0000 1.40
@@ -28,8 +28,10 @@
#include "nasl_var.h"
#include "nasl_lex_ctxt.h"
#include "nasl_debug.h"
+#include "nasl_includes.h"
static void naslerror(const char *);
+
#define YYERROR_VERBOSE
%}
@@ -276,34 +278,19 @@
/* include */
inc: INCLUDE '(' string ')'
{
- naslctxt subctx;
- int x;
+ nasl_include_t *inc;
- subctx.always_authenticated = ((naslctxt*)parm)->always_authenticated;
- x = init_nasl_ctx(&subctx, $3);
- $$ = NULL;
- if (x >= 0)
+ inc = nasl_read_include($3);
+ $$ = inc->tree;
+ /* If we are an authenticated script and the script we
+ * include is *NOT* authenticated,
+ * then we lose our authentication status */
+ if ( ((naslctxt*)parm)->always_authenticated == 0 &&
+ ((naslctxt*)parm)->authenticated != 0 && !inc->authenticated)
{
- if (! naslparse(&subctx))
- {
- $$ = subctx.tree;
- }
- else
- nasl_perror(NULL, "%s: Parse error at or near line %d\n",
- $3, subctx.line_nb);
- efree(&subctx.buffer);
- fclose(subctx.fp);
- subctx.fp = NULL;
- /* If we are an authenticated script and the script we include is *NOT* authenticated,
- then we lose our authentication status */
- if ( ((naslctxt*)parm)->always_authenticated == 0 &&
- ((naslctxt*)parm)->authenticated != 0 && subctx.authenticated == 0 )
- {
- ((naslctxt*)parm)->authenticated = 0;
- nasl_perror(NULL, "Including %s which is not authenticated - losing our authenticated status\n", $3);
- }
+ ((naslctxt*)parm)->authenticated = 0;
+ nasl_perror(NULL, "Including %s which is not authenticated - losing our authenticated status\n", $3);
}
- efree(& $3);
} ;
/* Function call */
This archive was generated by a fusion of
Pipermail 0.09 (Mailman edition) and
MHonArc 2.6.8.