gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r9851 - Extractor/src/main


From: gnunet
Subject: [GNUnet-SVN] r9851 - Extractor/src/main
Date: Mon, 21 Dec 2009 16:58:21 +0100

Author: durner
Date: 2009-12-21 16:58:21 +0100 (Mon, 21 Dec 2009)
New Revision: 9851

Modified:
   Extractor/src/main/Makefile.am
   Extractor/src/main/extract.c
   Extractor/src/main/extractor.c
Log:
Windows

Modified: Extractor/src/main/Makefile.am
===================================================================
--- Extractor/src/main/Makefile.am      2009-12-21 14:26:31 UTC (rev 9850)
+++ Extractor/src/main/Makefile.am      2009-12-21 15:58:21 UTC (rev 9851)
@@ -17,6 +17,7 @@
   $(top_builddir)/src/main/libextractor.la 
 
 if !MINGW
+    rtflag=-lrt
 if !SOMEBSD
     dlflag=-ldl
 endif
@@ -32,7 +33,7 @@
 libextractor_la_LDFLAGS = \
   $(LE_LIB_LDFLAGS) -version-info 
@LIB_VERSION_CURRENT@:@LIB_VERSION_REVISION@:@LIB_VERSION_AGE@
 libextractor_la_LIBADD = \
-  $(LIBLTDL) $(dlflag) $(zlib) $(bz2lib) $(LIBICONV) -lrt
+  $(LIBLTDL) $(dlflag) $(zlib) $(bz2lib) $(LIBICONV) $(rtflag)
 
 
 EXTRA_DIST = \

Modified: Extractor/src/main/extract.c
===================================================================
--- Extractor/src/main/extract.c        2009-12-21 14:26:31 UTC (rev 9850)
+++ Extractor/src/main/extract.c        2009-12-21 15:58:21 UTC (rev 9851)
@@ -49,6 +49,7 @@
 {
 }
 
+#ifndef WINDOWS
 /**
  * Install a signal handler to ignore SIGPIPE.
  */
@@ -69,6 +70,7 @@
     fprintf (stderr,
              "Failed to install SIGPIPE handler: %s\n", strerror (errno));
 }
+#endif
 
 
 
@@ -563,7 +565,9 @@
   setlocale(LC_ALL, "");
   textdomain(PACKAGE);
 #endif
+#ifndef WINDOWS
   ignore_sigpipe ();
+#endif
   print = malloc (sizeof (int) * EXTRACTOR_metatype_get_max ());
   for (i = 0; i < EXTRACTOR_metatype_get_max (); i++)
     print[i] = YES;            /* default: print everything */

Modified: Extractor/src/main/extractor.c
===================================================================
--- Extractor/src/main/extractor.c      2009-12-21 14:26:31 UTC (rev 9850)
+++ Extractor/src/main/extractor.c      2009-12-21 15:58:21 UTC (rev 9851)
@@ -19,11 +19,14 @@
  */
 
 #include "platform.h"
+#include "plibc.h"
 #include "extractor.h"
 #include <dirent.h>
 #include <sys/types.h>
+#ifndef WINDOWS
 #include <sys/wait.h>
 #include <sys/shm.h>
+#endif
 #include <signal.h>
 
 
@@ -120,20 +123,31 @@
    * Process ID of the child process for this plugin. 0 for 
    * none.
    */
+#ifndef WINDOWS
   pid_t cpid;
+#else
+  DWORD cpid;
+#endif
 
   /**
    * Pipe used to send information about shared memory segments to
    * the child process.  NULL if not initialized.
    */
+#ifndef WINDOWS
   FILE *cpipe_in;
+#else
+  HANDLE cpipe_in;
+#endif
 
   /**
    * Pipe used to read information about extracted meta data from
    * the child process.  -1 if not initialized.
    */
+#ifndef WINDOWS
   int cpipe_out;
-
+#else
+  HANDLE cpipe_out;
+#endif
 };
 
 
@@ -874,6 +888,9 @@
 stop_process (struct EXTRACTOR_PluginList *plugin)
 {
   int status;
+#ifdef WINDOWS
+  HANDLE process;
+#endif
 
 #if DEBUG
   if (plugin->cpid == -1)
@@ -884,12 +901,23 @@
   if ( (plugin->cpid == -1) ||
        (plugin->cpid == 0) )
     return;
+#ifndef WINDOWS
   kill (plugin->cpid, SIGKILL);
   waitpid (plugin->cpid, &status, 0);
+#else
+  process = OpenProcess (PROCESS_TERMINATE, FALSE, plugin->cpid);
+  TerminateProcess (process, 0);
+  CloseHandle (process);
+#endif
   plugin->cpid = -1;
+#ifndef WINDOWS
   close (plugin->cpipe_out);
+  fclose (plugin->cpipe_in);
+#else
+  CloseHandle (plugin->cpipe_out);
+  CloseHandle (plugin->cpipe_in);
+#endif
   plugin->cpipe_out = -1;
-  fclose (plugin->cpipe_in);
   plugin->cpipe_in = NULL;
 }
 
@@ -975,7 +1003,12 @@
 
 
 static int
-read_all (int fd,
+read_all (
+#ifndef WINDOWS
+    int fd,
+#else
+    HANDLE fd,
+#endif
          void *buf,
          size_t size)
 {
@@ -985,7 +1018,11 @@
   
   while (off < size)
     {
+#ifndef WINDOWS
       ret = read (fd, &data[off], size - off);
+#else
+      ReadFile (fd, &data[off], size - off, &ret, NULL);
+#endif
       if (ret <= 0)
        return -1;
       off += ret;
@@ -1082,6 +1119,9 @@
   int shmid;
   struct IpcHeader hdr;
   size_t size;
+#ifdef WINDOWS
+  HANDLE map;
+#endif
 
   if (0 != plugin_load (plugin))
     {
@@ -1115,10 +1155,16 @@
        break;
       ptr = NULL;
       fn[strlen(fn)-1] = '\0'; /* kill newline */
+#ifndef WINDOWS
       if ( (-1 != (shmid = shm_open (fn, O_RDONLY, 0))) &&
           (((off_t)-1) != (size = lseek (shmid, 0, SEEK_END))) &&
           (NULL != (ptr = mmap (NULL, size, PROT_READ, MAP_SHARED, shmid, 0))) 
&&
           (ptr != (void*) -1) )
+#else
+      map = OpenFileMapping (PAGE_READONLY, FALSE, fn);
+      ptr = MapViewOfFile (map, FILE_MAP_READ, 0, 0, 0);
+      if (ptr != NULL)
+#endif
        {
          if (0 != plugin->extractMethod (ptr,
                                          size,
@@ -1129,11 +1175,18 @@
          if (0 != write_all (out, &hdr, sizeof(hdr)))
            break;
        }
+#ifndef WINDOWS
       if ( (ptr != NULL) &&
           (ptr != (void*) -1) )
        munmap (ptr, size);
       if (-1 != shmid)
        close (shmid);
+#else
+      if (ptr != NULL && ptr != (void*) -1)
+        UnmapViewOfFile (ptr);
+      if (map != NULL)
+        CloseHandle (map);
+#endif
       if ( (plugin->specials != NULL) &&
           (NULL != strstr (plugin->specials,
                            "force-kill")) )
@@ -1147,13 +1200,67 @@
   close (out);
 }
 
+#ifdef WINDOWS
+void write_plugin_data (HANDLE h, const struct EXTRACTOR_PluginList *plugin)
+{
+  size_t i;
 
+  i = strlen (plugin->libname);
+  WriteFile (h, &i, sizeof (size_t), NULL, NULL);
+  WriteFile (h, plugin->libname, i, NULL, NULL);
+
+  i = strlen (plugin->short_libname);
+  WriteFile (h, &i, sizeof (size_t), NULL, NULL);
+  WriteFile (h, plugin->short_libname,i, NULL, NULL);
+
+  i = strlen (plugin->plugin_options);
+  WriteFile (h, &i, sizeof (size_t), NULL, NULL);
+  WriteFile (h, plugin->plugin_options, i, NULL, NULL);
+}
+
+struct EXTRACTOR_PluginList *read_plugin_data (FILE *f)
+{
+  struct EXTRACTOR_PluginList *ret;
+  size_t i;
+
+  ret = malloc (sizeof (struct EXTRACTOR_PluginList));
+
+  fread (&i, sizeof (size_t), 1, f);
+  ret->libname = malloc (i);
+  fread (ret->libname, i, 1, f);
+
+  fread (&i, sizeof (size_t), 1, f);
+  ret->short_libname = malloc (i);
+  fread (ret->short_libname, i, 1, f);
+
+  fread (&i, sizeof (size_t), 1, f);
+  ret->plugin_options = malloc (i);
+  fread (ret->plugin_options, i, 1, f);
+
+  return ret;
+}
+
+void CALLBACK RundllEntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, 
int nCmdShow)
+{
+  int in, out;
+
+  in = fileno (stdin);
+  out = fileno (stdout);
+
+  setmode (in, _O_BINARY);
+  setmode (out, _O_BINARY);
+
+  process_requests (read_plugin_data (stdin), in, out);
+}
+#endif
+
 /**
  * Start the process for the given plugin.
  */ 
 static void
 start_process (struct EXTRACTOR_PluginList *plugin)
 {
+#ifndef WINDOWS
   int p1[2];
   int p2[2];
   pid_t pid;
@@ -1199,6 +1306,22 @@
   if (plugin->cpipe_in == NULL)
     perror ("fdopen");
   plugin->cpipe_out = p2[0];
+#else
+  STARTUPINFO startup;
+  PROCESS_INFORMATION proc;
+
+  memset (&startup, 0, sizeof (STARTUPINFO));
+  CreatePipe (&startup.hStdInput, &plugin->cpipe_in, NULL, 0);
+  CreatePipe (&plugin->cpipe_out, &startup.hStdOutput, NULL, 0);
+
+  write_plugin_data (plugin->cpipe_in, plugin);
+
+  // FIXME library name
+  CreateProcess (NULL, "rundll32 libextractor-1.dll,RundllEntryPoint", NULL, 
NULL, FALSE, 0, NULL, NULL, &startup, &proc);
+  CloseHandle (proc.hProcess);
+  CloseHandle (proc.hThread);
+  plugin->cpid = proc.dwProcessId;
+#endif
 }
 
 
@@ -1315,7 +1438,11 @@
         void *proc_cls) 
 {
   struct EXTRACTOR_PluginList *ppos;
+#ifndef WINDOWS
   int shmid;
+#else
+  HANDLE map, mappedFile;
+#endif
   enum EXTRACTOR_Options flags;
   void *ptr;
   char fn[255];
@@ -1349,9 +1476,15 @@
     {
       snprintf (fn,
                sizeof(fn),
-               "/libextractor-shm-%u-%u",
+#ifdef WINDOWS
+               "%TEMP%\\"
+#else
+               "/"
+#endif
+               "libextractor-shm-%u-%u",
                getpid(),
-               (unsigned int) random());
+               (unsigned int) RANDOM());
+#ifndef WINDOWS
       shmid = shm_open (fn, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
       ptr = NULL;
       if (shmid != -1)
@@ -1368,16 +1501,41 @@
              memcpy (ptr, data, size);
            }
        }
+#else
+      mappedFile = CreateFile (fn, GENERIC_READ | GENERIC_WRITE,
+          FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
+          FILE_FLAG_DELETE_ON_CLOSE, NULL);
+      map = CreateFileMapping (mappedFile, NULL, PAGE_READWRITE, 1, 0, NULL);
+      ptr = MapViewOfFile (map, FILE_MAP_READ, 0, 0, 0);
+      if (ptr == NULL)
+        {
+          CloseHandle (map);
+          CloseHandle (mappedFile);
+          map = NULL;
+        }
+      else
+        memcpy (ptr, data, size);
+#endif
     }
   else
+#ifndef WINDOWS
     shmid = -1;
   if (want_shm && (shmid == -1))
     _exit(1);
+#else
+    map = NULL;
+  if (want_shm && map == NULL)
+    _exit(1);
+#endif
   ppos = plugins;
   while (NULL != ppos)
     {
       flags = ppos->flags;
+#ifndef WINDOWS
       if (shmid == -1)
+#else
+      if (map == NULL)
+#endif
        flags = EXTRACTOR_OPTION_IN_PROCESS;
       switch (flags)
        {
@@ -1416,11 +1574,17 @@
     }
   if (want_shm)
     {
+#ifndef WINDOWS
       if (NULL != ptr)
        munmap (ptr, size);
       if (shmid != -1)
        close (shmid);
       shm_unlink (fn);
+#else
+      UnmapViewOfFile (ptr);
+      CloseHandle (map);
+      CloseHandle (mappedFile);
+#endif
     }
 }
 
@@ -1789,7 +1953,6 @@
     close(fd);  
 }
 
-
 /**
  * Initialize gettext and libltdl (and W32 if needed).
  */





reply via email to

[Prev in Thread] Current Thread [Next in Thread]