[Openvas-commits] r2865 - in trunk/winslad: . testplugin

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sat Mar 21 01:53:14 CET 2009


Author: doj
Date: 2009-03-21 01:53:05 +0100 (Sat, 21 Mar 2009)
New Revision: 2865

Added:
   trunk/winslad/client.c
   trunk/winslad/daemon.c
   trunk/winslad/daemon.h
   trunk/winslad/show.c
   trunk/winslad/show.h
   trunk/winslad/testplugin/
   trunk/winslad/testplugin/plugin.xml
   trunk/winslad/writen.c
   trunk/winslad/writen.h
Modified:
   trunk/winslad/Makefile
   trunk/winslad/TODO.txt
   trunk/winslad/config.h
   trunk/winslad/init.c
   trunk/winslad/main.c
   trunk/winslad/struct.h
   trunk/winslad/xml.c
   trunk/winslad/xml.h
Log:
xml loading works

Modified: trunk/winslad/Makefile
===================================================================
--- trunk/winslad/Makefile	2009-03-20 18:41:26 UTC (rev 2864)
+++ trunk/winslad/Makefile	2009-03-21 00:53:05 UTC (rev 2865)
@@ -1,9 +1,9 @@
 EXE=sladd.exe
-SRC=main.c init.c xml.c util.c
+SRC=main.c init.c xml.c util.c client.c show.c daemon.c writen.c
 OBJ=$(SRC:%.c=%.o)
 CC=mingw32-gcc
 
-CFLAGS+=-Wall
+CFLAGS+=-Wall -DDOJDEBUG=1
 
 $(EXE):	$(OBJ) libexpat.a
 	$(CC) -o $@ $^

Modified: trunk/winslad/TODO.txt
===================================================================
--- trunk/winslad/TODO.txt	2009-03-20 18:41:26 UTC (rev 2864)
+++ trunk/winslad/TODO.txt	2009-03-21 00:53:05 UTC (rev 2865)
@@ -1,4 +1,5 @@
-- get XML code from unix slad to work
-- write IPv4 and IPv6 network code
+- write example plugin
+- Win32 interprocess communication
+- start background daemon
+- communicate with daemon from frontend
 - write process management
-- write example plugin

Added: trunk/winslad/client.c
===================================================================
--- trunk/winslad/client.c	2009-03-20 18:41:26 UTC (rev 2864)
+++ trunk/winslad/client.c	2009-03-21 00:53:05 UTC (rev 2865)
@@ -0,0 +1,195 @@
+#define WIN32_LEAN_AND_MEAN 1
+#include <windows.h>
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+
+#include "daemon.h"
+#include "debug.h"
+#include "struct.h"
+#include "util.h"
+#include "show.h"
+
+void sleep(int seconds)
+{
+  Sleep(seconds*1000);
+}
+
+int connect_socket (sladd_config_t * config)
+{
+#if 0
+  int sock;
+  struct sockaddr_in ipv4 = {
+  sin_family: AF_INET,
+  sin_port: htons (config->listenport),
+  sin_addr: { htonl (INADDR_LOOPBACK) }
+  };
+  struct sockaddr_in6 ipv6 = {
+  sin6_family: AF_INET6,
+  sin6_port: htons (config->listenport),
+  sin6_flowinfo: 0,
+  sin6_addr: IN6ADDR_LOOPBACK_INIT,
+  sin6_scope_id: 0
+  };
+
+  if ((sock = socket (PF_INET6, SOCK_STREAM, 0)) != -1) {
+    if (!connect (sock, (struct sockaddr *)&ipv6, sizeof (ipv6)))
+      return sock;
+    close (sock);
+  }
+  if (errno != EAFNOSUPPORT)
+    {
+      PRINTF("could not create IPv6 socket\n");
+      return -1;
+    }
+
+  if ((sock = socket (PF_INET, SOCK_STREAM, 0)) != -1) {
+    if (!connect (sock, (struct sockaddr *)&ipv4, sizeof (ipv4)))
+      return sock;
+    close (sock);
+  }
+
+  PRINTF("could not create IPv4 socket\n");
+#endif
+  return -1;
+}
+
+int send_server (const char * command, sladd_config_t * config)
+{
+#if 0
+  int socket, result, len;
+  fd_set set;
+  char buf[1024];
+
+  /* Get a connected socket */
+  socket = connect_socket (config);
+  if (socket == -1)
+    {
+      PRINTF("send_server(): could not connect_socket()\n");
+      return 1;
+    }
+
+  /* Send command */
+  write (socket, command, strlen (command));
+
+  /* Receive from server, print everything received to stdout*/
+  do {
+    FD_ZERO (&set);
+    FD_SET (socket, &set);
+    result = select (socket + 1, &set, NULL, NULL, NULL);
+    if (result >= 0)
+      if (FD_ISSET (socket, &set)) {
+	len = recv (socket, buf, 1023, 0);
+	if (len >= 0) {
+	  buf[len] = 0;
+	  printf ("%s", buf);
+	}
+      }
+  } while (result >= 0 && len > 0);
+  shutdown (socket, SHUT_RDWR);
+  close (socket);
+  return 0;
+#else
+  return -1;
+#endif
+}
+
+/* Queue a command on the server */
+int cl_runcmd (sladd_config_t * config)
+{
+  char command[256];
+
+  snprintf (command, sizeof(command), "run %s\n", config->arg);
+
+  if (send_server (command, config))
+    {
+      /* Send failed, maybe the daemon isn't running */
+      if (start_daemon (config))
+	{
+	  PRINTF("could not start the daemon process\n");
+	  return 1;
+	}
+      /* Daemon started, retry */
+      sleep(1); /* give server time to start up */
+      if (send_server (command, config))
+	{
+	  PRINTF(MSG_CANTQUEUE);
+	  return 1;
+	}
+    }
+
+  return 0;
+}
+
+/* Perform one of the show actions */
+int cl_show (sladd_config_t * config)
+{
+  char cmd[256];
+  int i, coloncount = 0;
+
+  if (!config->arg)
+    {
+      fprintf (stderr, "No selection to --show\n");
+      return 1;
+    }
+
+  /* List plugins */
+  if (!strcasecmp (config->arg, "plugins"))
+    {
+      if (send_server ("show plugins\n", config))
+	{
+	  /*
+	   * Couldn't list from server, print out own list
+	   * This is the same routine called from the daemon, using
+	   * the stdout == fd 1 assumption
+	   */
+	  show_plugins (1, config);
+	}
+    }
+  else if (!strcasecmp (config->arg, "alljobs"))
+    {
+      return send_server ("show alljobs\n", config);
+    }
+  else if (!strcasecmp (config->arg, "jobs"))
+    {
+      send_server ("show jobs\n", config);
+    }
+  else
+    {
+      /* Couldn't identify request directly, let's see if it's a plugin */
+      for (i=0; config->arg[i]; i++)
+	if (config->arg[i] == ':')
+	  coloncount++;
+      if (coloncount == 2)
+	{
+	  snprintf (cmd, sizeof(cmd), "show %s\n", config->arg);
+	  return send_server (cmd, config);
+	}
+      else
+	{
+	  printf (MSG_NOCOMMAND);
+	}
+    }
+  return 0;
+}
+
+/* Externally visible entry point from main () */
+int run_client (sladd_config_t * config)
+{
+  switch (config->command)
+    {
+    case 'q':
+      return send_server ("quit\n", config);
+    case 'r':
+      return cl_runcmd (config);
+    case 's':
+      return cl_show (config);
+    default:
+      fprintf (stderr, "Unknown command '%c'\n", config->command);
+      return 1;
+    }
+  return 0;
+}


Property changes on: trunk/winslad/client.c
___________________________________________________________________
Name: svn:executable
   + *

Modified: trunk/winslad/config.h
===================================================================
--- trunk/winslad/config.h	2009-03-20 18:41:26 UTC (rev 2864)
+++ trunk/winslad/config.h	2009-03-21 00:53:05 UTC (rev 2865)
@@ -6,7 +6,7 @@
 #endif
 
 #ifndef SLADD_PLUGIN_PATH
-#  define SLAD_PLUGIN_PATH   "/opt/slad/plugins"
+#  define SLAD_PLUGIN_PATH   "C:\\slad\\plugins"
 #endif
 
 #ifndef SLADD_SESSION_TIMEOUT

Added: trunk/winslad/daemon.c
===================================================================
--- trunk/winslad/daemon.c	2009-03-20 18:41:26 UTC (rev 2864)
+++ trunk/winslad/daemon.c	2009-03-21 00:53:05 UTC (rev 2865)
@@ -0,0 +1,6 @@
+#include "struct.h"
+
+int start_daemon (sladd_config_t * config)
+{
+  return -1;
+}


Property changes on: trunk/winslad/daemon.c
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/winslad/daemon.h
===================================================================
--- trunk/winslad/daemon.h	2009-03-20 18:41:26 UTC (rev 2864)
+++ trunk/winslad/daemon.h	2009-03-21 00:53:05 UTC (rev 2865)
@@ -0,0 +1,8 @@
+#ifndef __daemon_h_included__
+#define __daemon_h_included__
+
+#include "struct.h"
+
+int start_daemon (sladd_config_t * config);
+
+#endif


Property changes on: trunk/winslad/daemon.h
___________________________________________________________________
Name: svn:executable
   + *

Modified: trunk/winslad/init.c
===================================================================
--- trunk/winslad/init.c	2009-03-20 18:41:26 UTC (rev 2864)
+++ trunk/winslad/init.c	2009-03-21 00:53:05 UTC (rev 2865)
@@ -15,7 +15,8 @@
 #include "xml.h"
 #include "struct.h"
 
-sladd_config_t *  init_defaults () {
+sladd_config_t *  init_defaults ()
+{
   sladd_config_t * config;
 
   config = calloc (1, sizeof (sladd_config_t));
@@ -28,32 +29,37 @@
   config->listenport = SLADD_PORT;
   config->pluginpath = strdup (SLAD_PLUGIN_PATH);
 
-  if (!config->pluginpath) {
-    free (config);
-    config = NULL;
-  }
+  if (!config->pluginpath)
+    {
+      free (config);
+      config = NULL;
+    }
 
   return config;
 }
 
-void init_environment (sladd_config_t * config) {
+void init_environment (sladd_config_t * config)
+{
   char * envvar;
   int tmp;
 
   envvar = getenv ("SLADPORT");
-  if (envvar) {
-    tmp = atoi (envvar);
-    if (tmp > 0 && tmp < 65536)
-      config->listenport = tmp;
-  }
+  if (envvar)
+    {
+      tmp = atoi (envvar);
+      if (tmp > 0 && tmp < 65536)
+	config->listenport = tmp;
+    }
   envvar = getenv ("SLADPATH");
-  if (envvar) {
-    free (config->pluginpath);
-    config->pluginpath = strdup (envvar);
-  }
+  if (envvar)
+    {
+      free (config->pluginpath);
+      config->pluginpath = strdup (envvar);
+    }
 }
 
-void print_help (char * arg0) {
+void print_help (char * arg0)
+{
   printf ("Usage: %s [OPTION...]\n", basename (arg0));
   printf ("-p, --pluginpath=%-21s plug-in directory\n", SLAD_PLUGIN_PATH);
   printf ("-l, --listenport=%-21d the port the daemon will listen on\n", SLADD_PORT);
@@ -66,7 +72,8 @@
   _exit (0);
 }
 
-void init_cmdline (int argc, char * const argv[], sladd_config_t * config) {
+void init_cmdline (int argc, char * const argv[], sladd_config_t * config)
+{
   int daemonize = 0, listenport = 0;
   char * show = NULL, * pluginpath = NULL, * command = NULL;
   int i;
@@ -83,64 +90,70 @@
     {NULL, 0, NULL, 0}
   };
 
-  while (1) {
-    i = getopt_long (argc, argv, "dhl:p:qr:s:", long_options, &option_index);
-    if (i == -1)
-      break;
+  while (1)
+    {
+      i = getopt_long (argc, argv, "dhl:p:qr:s:", long_options, &option_index);
+      if (i == -1)
+	break;
 
-    switch (i) {
-    case 'd':
-      daemonize = 1;
-      break;
-    case 'h':
-      print_help (argv[0]); /* never returns */
-      break;
-    case 'l':
-      listenport = atoi (optarg);
-      break;
-    case 'p':
-      if (pluginpath)
-	free (config->pluginpath);
-      pluginpath = strdup (optarg);
-      break;
-    case 'q':
-      config->command = 'q';
-      break;
-    case 'r':
-      if (command)
-	free (command);
-      command = strdup (optarg);
-      break;
-    case 's':
-      if (show)
-	free (show);
-      show = strdup (optarg);
-      break;
+      switch (i) {
+      case 'd':
+	daemonize = 1;
+	break;
+      case 'h':
+	print_help (argv[0]); /* never returns */
+	break;
+      case 'l':
+	listenport = atoi (optarg);
+	break;
+      case 'p':
+	if (pluginpath)
+	  free (config->pluginpath);
+	pluginpath = strdup (optarg);
+	break;
+      case 'q':
+	config->command = 'q';
+	break;
+      case 'r':
+	if (command)
+	  free (command);
+	command = strdup (optarg);
+	break;
+      case 's':
+	if (show)
+	  free (show);
+	show = strdup (optarg);
+	break;
+      }
     }
-  }
 
-  if (pluginpath) {
-    free (config->pluginpath);
-    config->pluginpath = pluginpath;
-  }
+  if (pluginpath)
+    {
+      free (config->pluginpath);
+      config->pluginpath = pluginpath;
+    }
   if (listenport > 0 && listenport < 65536)
     config->listenport = listenport;
 
   if (daemonize)
     config->command = 'd';
-  else if (show) {
-    config->command = 's';
-    config->arg = show;
-  } else if (command) {
-    config->command = 'r';
-    config->arg = command;
-  }
+  else if (show)
+    {
+      config->command = 's';
+      config->arg = show;
+    }
+  else if (command)
+    {
+      config->command = 'r';
+      config->arg = command;
+    }
 
   if (!config->command)
     print_help (argv[0]);
 }
 
-void init_plugins (sladd_config_t * config) {
+int init_plugins (sladd_config_t * config)
+{
   char pluginpath[1024], tmppath[1024];
   struct stat statbuf;
   int result;
@@ -148,74 +161,91 @@
   struct dirent * entry;
 
   strcpy (pluginpath, config->pluginpath);
-  do {
-    if (stat (pluginpath, &statbuf) < 0)
-      {
-	PRINTF("init_plugins(): stat() error\n");
-	return;
-      }
+  do
+    {
+      if (stat (pluginpath, &statbuf) < 0)
+	{
+	  PRINTF("init_plugins(): stat(%s) error\n", pluginpath);
+	  return -1;
+	}
 #if 0
-    if (S_ISLNK (statbuf.st_mode)) {
-      result = readlink (pluginpath, tmppath, 1023);
-      if (result >= 0) {
-	tmppath[result] = 0;
-	if (tmppath[0] == '/')
-	  strcpy (pluginpath, tmppath);
-	else {
-	  strcpy (pluginpath, dirname (pluginpath));
-	  strcat (pluginpath, "/");
-	  strcat (pluginpath, tmppath);
+      if (S_ISLNK (statbuf.st_mode)) {
+	result = readlink (pluginpath, tmppath, 1023);
+	if (result >= 0) {
+	  tmppath[result] = 0;
+	  if (tmppath[0] == '/')
+	    strcpy (pluginpath, tmppath);
+	  else {
+	    strcpy (pluginpath, dirname (pluginpath));
+	    strcat (pluginpath, "/");
+	    strcat (pluginpath, tmppath);
+	  }
 	}
-      }
-      else
-	{
-	  PRINTF("init_plugins(): readlink() error\n");
-	  return;
-	}
-    } else
+	else
+	  {
+	    PRINTF("init_plugins(): readlink() error\n");
+	    return -1;
+	  }
+      } else
 #endif
-      if (!S_ISDIR (statbuf.st_mode))
-      {
-	PRINTF("init_plugins(): !S_ISDIR()\n");
-	return;
-      }
-  } while (/*S_ISLNK (statbuf.st_mode)*/0);
-
-  if (!(dir = opendir (pluginpath))) {
-    PRINTF("can't opendir() plugin directory %s: %s\n", pluginpath, strerror (errno));
-    return;
-  }
-
-  while ((entry = readdir (dir))) {
-    if (strcmp (entry->d_name, ".") && strcmp (entry->d_name, "..")) {
-      snprintf (tmppath, 1024, "%s/%s", pluginpath, entry->d_name);
-      if (!stat (tmppath, &statbuf) && S_ISDIR (statbuf.st_mode)) {
-	strcat (tmppath, "/plugin.xml");
-	if (!stat (tmppath, &statbuf) && S_ISREG (statbuf.st_mode))
+	if (!S_ISDIR (statbuf.st_mode))
 	  {
-	    load_plugin (tmppath, config);
+	    PRINTF("init_plugins(): !S_ISDIR()\n");
+	    return -1;
 	  }
-      }
+    } while (/*S_ISLNK (statbuf.st_mode)*/0);
+
+  if (!(dir = opendir (pluginpath)))
+    {
+      PRINTF("can't opendir() plugin directory %s: %s\n", pluginpath, strerror (errno));
+      return -1;
     }
-  }
 
-  if (strcmp (pluginpath, config->pluginpath)) {
-    free (config->pluginpath);
-    config->pluginpath = strdup (pluginpath);
-  }
+  while ((entry = readdir (dir)))
+    {
+      if(! strcmp(entry->d_name, ".")) continue;
+      if(! strcmp (entry->d_name, "..")) continue;
 
+      snprintf (tmppath, sizeof(tmppath), "%s/%s", pluginpath, entry->d_name);
+      if (!stat (tmppath, &statbuf) && S_ISDIR (statbuf.st_mode))
+	{
+	  strcat (tmppath, "/plugin.xml");
+	  if (!stat (tmppath, &statbuf) && S_ISREG (statbuf.st_mode))
+	    {
+	      if(load_plugin (tmppath, config) < 0)
+		{
+		  PRINTF("could not load %s\n", tmppath);
+		}
+	    }
+	}
+    }
+
+  if (strcmp (pluginpath, config->pluginpath))
+    {
+      free (config->pluginpath);
+      config->pluginpath = strdup (pluginpath);
+    }
+
   closedir (dir);
+
+  return 0;
 }
 
-sladd_config_t * init (int argc, char * const argv[]) {
+sladd_config_t * init (int argc, char * const argv[])
+{
   sladd_config_t * config;
 
   config = init_defaults ();
-  if (config) {
-    init_environment (config);
-    init_cmdline (argc, argv, config);
-    init_plugins (config);
-  }
+  if (config)
+    {
+      init_environment (config);
+      init_cmdline (argc, argv, config);
+      if(init_plugins (config) < 0)
+	{
+	  PRINTF("could not init plugins\n");
+	  return NULL;
+	}
+    }
 
   return config;
 }

Modified: trunk/winslad/main.c
===================================================================
--- trunk/winslad/main.c	2009-03-20 18:41:26 UTC (rev 2864)
+++ trunk/winslad/main.c	2009-03-21 00:53:05 UTC (rev 2865)
@@ -1,6 +1,8 @@
 #include "debug.h"
 #include "init.h"
 
+int run_client (sladd_config_t * config);
+
 int main (int argc, const char ** argv)
 {
   sladd_config_t * config;
@@ -20,6 +22,5 @@
     }
 
   PRINTF("I will run as client\n");
-  return 0;
-  //return run_client (config);
+  return run_client (config);
 }

Added: trunk/winslad/show.c
===================================================================
--- trunk/winslad/show.c	2009-03-20 18:41:26 UTC (rev 2864)
+++ trunk/winslad/show.c	2009-03-21 00:53:05 UTC (rev 2865)
@@ -0,0 +1,53 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "struct.h"
+#include "writen.h"
+
+int show_plugins (int socket, sladd_config_t * config)
+{
+  slad_plugin_t * plugin;
+  slad_plugin_set_t * set;
+  slad_plugin_entry_t * entry;
+  int p_num = 0, s_num = 0, e_num = 0;
+  char buffer [1024];
+
+  if(! config->plugins)
+    return -2;
+
+  for (p_num = 0; (plugin = config->plugins[p_num]); p_num++)
+    {
+      const int len=snprintf (buffer, sizeof(buffer), "p:%s:%s\n", plugin->name, plugin->desc);
+      if(writen(socket, buffer, len) != len)
+	return -1;
+
+      if (plugin->sets)
+	for (s_num = 0; (set = plugin->sets[s_num]); s_num++)
+	  {
+	    const int len=snprintf (buffer, sizeof(buffer), "s:%d:%s:%s:%s\n",
+				    set->is_default ? 1 : 0,
+				    plugin->name,
+				    set->name,
+				    set->desc ? set->desc : "(no desc)");
+	    if(writen(socket, buffer, len) != len)
+	      return -1;
+
+	    if (set->entries)
+	      for (e_num = 0; (entry = set->entries[e_num]); e_num++)
+		{
+		  const int len=snprintf (buffer, sizeof(buffer), "e:%d:%s:%s:%s:%s\n",
+					  entry->is_default ? 1 : 0,
+					  plugin->name,
+					  set->name,
+					  entry->name,
+					  entry->desc ? entry->desc : "(no desc)");
+		  if(writen(socket, buffer, len) != len)
+		    return -1;
+		}
+	  }
+    }
+
+  return 0;
+}

Added: trunk/winslad/show.h
===================================================================
--- trunk/winslad/show.h	2009-03-20 18:41:26 UTC (rev 2864)
+++ trunk/winslad/show.h	2009-03-21 00:53:05 UTC (rev 2865)
@@ -0,0 +1,8 @@
+#ifndef __show_h_included__
+#define __show_h_included__
+
+#include "struct.h"
+
+int show_plugins (int socket, sladd_config_t * config);
+
+#endif

Modified: trunk/winslad/struct.h
===================================================================
--- trunk/winslad/struct.h	2009-03-20 18:41:26 UTC (rev 2864)
+++ trunk/winslad/struct.h	2009-03-21 00:53:05 UTC (rev 2865)
@@ -26,7 +26,7 @@
   char * name;                          /* name of the entry */
   char * desc;                          /* description of the entry */
   int is_default;                       /* this entry is selected by default */
-  char * binary;                        /* location of the binay,
+  char * binary;                        /* location of the binary,
                                            relative to the plugin's path */
   char * cmdline;                       /* parameters to pass to the binary */
   int max_runtime;                      /* maximum time in seconds the process

Added: trunk/winslad/testplugin/plugin.xml
===================================================================
--- trunk/winslad/testplugin/plugin.xml	2009-03-20 18:41:26 UTC (rev 2864)
+++ trunk/winslad/testplugin/plugin.xml	2009-03-21 00:53:05 UTC (rev 2865)
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plugin SYSTEM "plugin.dtd">
+
+<slad:plugin xmlns:slad="http://slad.dnsystems.org/slad" id="winslad-test">
+	<slad:pluginDescription>Directory of C:</slad:pluginDescription>
+	<slad:pluginSet id="cdir">
+		<slad:pluginSetDescription>Show Directory of C:</slad:pluginSetDescription>
+		<slad:pluginSetEntry id="cdir">
+			<slad:pluginSetEntryBinary>dir.exe</slad:pluginSetEntryBinary>
+			<slad:pluginSetEntryCommandline>C:\</slad:pluginSetEntryCommandline>
+		</slad:pluginSetEntry>
+	</slad:pluginSet>
+</slad:plugin>


Property changes on: trunk/winslad/testplugin/plugin.xml
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/winslad/writen.c
===================================================================
--- trunk/winslad/writen.c	2009-03-20 18:41:26 UTC (rev 2864)
+++ trunk/winslad/writen.c	2009-03-21 00:53:05 UTC (rev 2865)
@@ -0,0 +1,105 @@
+/* Copyright (c) 2007..2009 Dirk Jagdmann <doj at cubic.org>
+
+This software is provided 'as-is', without any express or implied
+warranty. In no event will the authors be held liable for any damages
+arising from the use of this software.
+
+Permission is granted to anyone to use this software for any purpose,
+including commercial applications, and to alter it and redistribute it
+freely, subject to the following restrictions:
+
+    1. The origin of this software must not be misrepresented; you
+       must not claim that you wrote the original software. If you use
+       this software in a product, an acknowledgment in the product
+       documentation would be appreciated but is not required.
+
+    2. Altered source versions must be plainly marked as such, and
+       must not be misrepresented as being the original software.
+
+    3. This notice may not be removed or altered from any source
+       distribution. */
+
+/* $Header: /code/doj/writen.c,v 1.8 2008/12/17 20:39:04 doj Exp $ */
+
+#ifdef _MSC_VER
+#define WIN32_LEAN_AND_MEAN 1
+#include <windows.h>
+#include <io.h>
+typedef SSIZE_T ssize_t;
+typedef unsigned size_t;
+#pragma warning (disable: 4996)
+#else
+
+#include <assert.h>
+#include <signal.h>
+#include <unistd.h>
+
+#if defined(__FreeBSD__) || defined(__OpenBSD__)
+typedef sig_t sighandler_t;
+#endif
+#endif
+
+/*lint -save -e1924 -esym(534,alarm,signal) ignore return value*/
+/*lint -esym(952,buf) ignore non const buf*/
+
+ssize_t writen(int fd, const void* buf, const size_t count)
+{
+  const char *ptr=(const char*)buf;
+  size_t nleft=count;
+
+  if(fd<0 || !buf) return -1;
+
+  while(nleft > 0)
+    {
+      ssize_t nwritten;
+      if( (nwritten=write(fd, ptr, nleft)) < 0)
+	return nwritten;
+      /*lint -e{737} ignore int-unsigned mismatch*/
+      nleft -= nwritten;
+      ptr   += nwritten;
+    }
+  /*lint -e{713} ignore unsigned-int mismatch*/
+  return count;
+}
+
+#if defined(WIN32)
+#define writen_pipe_ign writen
+#define writen_pipe_ign_timeout(fd, buf, count, timeout) writen(fd, buf, count)
+#else
+ssize_t writen_pipe_ign(int fd, const void* buf, size_t count)
+{
+  if(fd<0 || !buf) return -1;
+  if(count==0) return 0;
+
+  /*lint --e{953} ignore non const variables*/
+  sighandler_t ph=signal(SIGPIPE, SIG_IGN);
+  const int w=writen(fd, buf, count);
+  signal(SIGPIPE, ph);
+  return w;
+}
+
+ssize_t writen_pipe_ign_timeout(int fd, const void* buf, size_t count, unsigned timeout)
+{
+  if(fd<0 || !buf) return -1;
+  if(count==0) return 0;
+
+  /* unix alarm() only has second resolution */
+  timeout/=1000000;
+  if(timeout==0) timeout=1;
+
+  /*lint --e{953} ignore non const variables*/
+  sighandler_t ph=signal(SIGPIPE, SIG_IGN);
+  sighandler_t ah=signal(SIGALRM, SIG_IGN);
+  const unsigned alarmRemaining=alarm(timeout);
+  const int w=writen(fd, buf, count);
+
+  alarm(0);
+  signal(SIGPIPE, ph);
+  signal(SIGALRM, ah);
+  alarm(alarmRemaining);
+
+  return w;
+}
+#endif
+
+/*lint -restore*/

Added: trunk/winslad/writen.h
===================================================================
--- trunk/winslad/writen.h	2009-03-20 18:41:26 UTC (rev 2864)
+++ trunk/winslad/writen.h	2009-03-21 00:53:05 UTC (rev 2865)
@@ -0,0 +1,52 @@
+/* $Header: /code/doj/writen.h,v 1.4 2008/12/17 20:39:04 doj Exp $ */
+
+#ifndef WRITEN__H
+#define WRITEN__H
+
+/**
+   writes a buffer to a file descriptor. Ensures that the complete
+   buffer is written.
+
+   ripped from Advanced Programming in the Unix Environment by Richard Stevens
+
+   @param fd file descriptor
+   @param buf pointer to a buffer
+   @param count size of buf in bytes
+
+   @return upon success the number of bytes written, upon error the negative return code from write() (see the errno variable for details)
+*/
+ssize_t writen(int fd, const void* buf, const size_t count);
+
+/**
+   writes a buffer to a file descriptor. Ensures that the complete
+   buffer is written. A SIGPIPE signal thrown during write is
+   ignored. If this signal was thrown errno will be EPIPE if this
+   function returns -1.
+
+   @param fd file descriptor
+   @param buf pointer to a buffer
+   @param count size of buf in bytes
+
+   @return upon success the number of bytes written, upon error the negative return code from write() (see the errno variable for details)
+*/
+ssize_t writen_pipe_ign(int fd, const void* buf, size_t count);
+
+/**
+   writes a buffer to a file descriptor. Ensures that the complete
+   buffer is written. A SIGPIPE signal thrown during write is
+   ignored. If this signal was thrown errno will be EPIPE if this
+   function returns -1. If count bytes could not be written without
+   timeout usec the function will return the number of bytes
+   written. However the actual timeout will be at least 1
+   second. timeout resolution may not be better than whole seconds.
+
+   @param fd file descriptor
+   @param buf pointer to a buffer
+   @param count size of buf in bytes
+   @param timeout timeout in usec
+
+   @return upon success the number of bytes written, upon error the negative return code from write() (see the errno variable for details)
+*/
+ssize_t writen_pipe_ign_timeout(int fd, const void* buf, size_t count, unsigned timeout);
+
+#endif


Property changes on: trunk/winslad/writen.h
___________________________________________________________________
Name: svn:executable
   + *

Modified: trunk/winslad/xml.c
===================================================================
--- trunk/winslad/xml.c	2009-03-20 18:41:26 UTC (rev 2864)
+++ trunk/winslad/xml.c	2009-03-21 00:53:05 UTC (rev 2865)
@@ -318,7 +318,7 @@
 
 #define BUFSIZE 256
 
-void load_plugin (const char * cfgname, sladd_config_t * config) {
+int load_plugin (const char * cfgname, sladd_config_t * config) {
   FILE * file;
   int len;
   int done = 0;
@@ -331,7 +331,7 @@
   if (!parser)
     {
       PRINTF("could not create XML parser\n");
-      return;
+      return -1;
     }
 
   XML_SetElementHandler (parser, start, end);
@@ -342,7 +342,7 @@
   if (!(file = fopen (cfgname, "r")))
     {
       PRINTF("could not open %s\n", cfgname);
-      return;
+      return -1;
     }
 
   tmp = config->pluginpath;
@@ -353,7 +353,7 @@
     if (!buffer) {
       PRINTF("Got no buffer for XML parsing: %s\n", XML_ErrorString(XML_GetErrorCode(parser)));
       config->pluginpath = tmp;
-      return;
+      return -1;
     }
 
     if (fgets (buffer, BUFSIZE-1, file))
@@ -373,4 +373,6 @@
   fclose (file);
   XML_ParserFree (parser);
   config->pluginpath = tmp;
+
+  return 0;
 }

Modified: trunk/winslad/xml.h
===================================================================
--- trunk/winslad/xml.h	2009-03-20 18:41:26 UTC (rev 2864)
+++ trunk/winslad/xml.h	2009-03-21 00:53:05 UTC (rev 2865)
@@ -3,6 +3,6 @@
 
 #include "struct.h"
 
-void load_plugin (const char * cfgname, sladd_config_t * config);
+int load_plugin (const char * cfgname, sladd_config_t * config);
 
 #endif



More information about the Openvas-commits mailing list