-fixes
[oweals/gnunet.git] / src / util / os_priority.c
index 15cbe3c982c764c5debd9f0b254a4a3429bf9872..0b56ce5c520e2929be58e0f014c7ff592a1f6add 100644 (file)
@@ -29,6 +29,7 @@
 #include "gnunet_os_lib.h"
 #include "gnunet_scheduler_lib.h"
 #include "gnunet_strings_lib.h"
+#include "gnunet_crypto_lib.h"
 #include "disk.h"
 
 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
@@ -298,7 +299,7 @@ npipe_open (const char *fn,
   } 
   if (-1 == fd)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                (flags == GNUNET_DISK_OPEN_READ) 
                ? _("Failed to open named pipe `%s' for reading: %s\n")
                : _("Failed to open named pipe `%s' for writing: %s\n"),
@@ -324,8 +325,9 @@ parent_control_handler (void *cls,
                         const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_DISK_FileHandle *control_pipe = cls;
-  int sig;
-
+  char sig;
+  ssize_t ret;
+  
   LOG (GNUNET_ERROR_TYPE_DEBUG, "`%s' invoked because of %d\n", __FUNCTION__,
        tc->reason);
   if (tc->reason &
@@ -335,10 +337,11 @@ parent_control_handler (void *cls,
     GNUNET_DISK_file_close (control_pipe);
     return;
   }
-  if (GNUNET_DISK_file_read (control_pipe, &sig, sizeof (sig)) !=
-      sizeof (sig))
+  ret = GNUNET_DISK_file_read (control_pipe, &sig, sizeof (sig));
+  if (sizeof (sig) != ret)
   {
-    LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read");
+    if (-1 == ret)
+      LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read");
     GNUNET_DISK_file_close (control_pipe);
     return;
   }
@@ -346,7 +349,7 @@ parent_control_handler (void *cls,
   GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
                                  control_pipe, &parent_control_handler,
                                  control_pipe);
-  raise (sig);
+  raise ((int) sig);
 }
 
 
@@ -424,30 +427,27 @@ int
 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig)
 {
   int ret;
+  char csig;
 
+  csig = (char) sig;
 #if !WINDOWS
   if ( (NULL == proc->control_pipe) &&
        (NULL != proc->childpipename) )
     proc->control_pipe = npipe_open (proc->childpipename,
                                     GNUNET_DISK_OPEN_WRITE);
 #endif
-  if (NULL == proc->control_pipe)
+  if (NULL != proc->control_pipe)
   {
-#if WINDOWS
-    /* no pipe and windows? can't do this */
-    errno = EINVAL;
-    return -1;
-#else
-    return kill (proc->pid, sig);
-#endif    
+    ret = GNUNET_DISK_file_write (proc->control_pipe, &csig, sizeof (csig));
+    if (ret == sizeof (csig))  
+      return 0;
   }
-  ret = GNUNET_DISK_file_write (proc->control_pipe, &sig, sizeof (sig));
-  if (ret == sizeof (sig))
-    return 0;
-  /* pipe failed, try other methods */
+  /* pipe failed or non-existent, try other methods */
   switch (sig)
   {
+#if !WINDOWS
   case SIGHUP:
+#endif
   case SIGINT:
   case SIGKILL:
   case SIGTERM:
@@ -466,7 +466,7 @@ GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig)
     errno = EINVAL;
     return -1;
 #else
-    return kill (proc->pid, sig);
+    return PLIBC_KILL (proc->pid, sig);
 #endif    
   }
 }
@@ -488,10 +488,8 @@ GNUNET_OS_process_get_pid (struct GNUNET_OS_Process * proc)
 void
 GNUNET_OS_process_close (struct GNUNET_OS_Process *proc)
 {
-#if ENABLE_WINDOWS_WORKAROUNDS
-  if (proc->control_pipe)
+  if (NULL != proc->control_pipe)
     GNUNET_DISK_file_close (proc->control_pipe);
-#endif
 // FIXME NILS
 #ifdef WINDOWS
   if (proc->handle != NULL)
@@ -499,7 +497,9 @@ GNUNET_OS_process_close (struct GNUNET_OS_Process *proc)
 #endif
   if (NULL != proc->childpipename)
   {
+#if !WINDOWS
     cleanup_npipe (proc->childpipename);
+#endif
     GNUNET_free (proc->childpipename);
   }
   GNUNET_free (proc);
@@ -845,6 +845,7 @@ GNUNET_OS_start_process_vap (int pipe_control,
   int argc, arg_count;
   HANDLE stdin_handle;
   HANDLE stdout_handle;
+  struct GNUNET_DISK_FileHandle *control_pipe;
 
   char path[MAX_PATH + 1];
 
@@ -966,6 +967,8 @@ GNUNET_OS_start_process_vap (int pipe_control,
       return NULL;
     }
   }
+  else
+    control_pipe = NULL;
   if (NULL != childpipename)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG, "Opened the parent end of the pipe `%s'\n",
@@ -979,8 +982,8 @@ GNUNET_OS_start_process_vap (int pipe_control,
     our_env[0] = NULL;
   }
   env_block = CreateCustomEnvTable (our_env);
-  GNUNET_free (our_env[0]);
-  GNUNET_free (our_env[1]);
+  GNUNET_free_non_null (our_env[0]);
+  GNUNET_free_non_null (our_env[1]);
 
   if (ERROR_SUCCESS != plibc_conv_to_win_pathwconv(path, wpath)
       || ERROR_SUCCESS != plibc_conv_to_win_pathwconv(cmd, wcmd)
@@ -1339,6 +1342,8 @@ GNUNET_OS_start_process_v (int pipe_control,
       return NULL;
     }
   }
+  else
+    control_pipe = NULL;
   if (lsocks != NULL && lsocks[0] != INVALID_SOCKET)
   {
     lsocks_pipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);