LRN: Fix automake deps to allow -j* builds again
[oweals/gnunet.git] / src / util / os_priority.c
index 5e641949d263b2a50cf839a8a35e95eefdc11ad5..f2e3f3c382fa54a89c418bd1267a04f3b1856289 100644 (file)
@@ -47,68 +47,95 @@ static struct GNUNET_OS_Process current_process;
 
 /**
  * This handler is called when there are control data to be read on the pipe
+ *
+ * @param cls the 'struct GNUNET_DISK_FileHandle' of the control pipe
+ * @param tc scheduler context
  */
-void
-GNUNET_OS_parent_control_handler (void *cls,
-                                  const struct
-                                  GNUNET_SCHEDULER_TaskContext * tc)
+static void
+parent_control_handler (void *cls,
+                       const struct
+                       GNUNET_SCHEDULER_TaskContext * tc)
 {
   struct GNUNET_DISK_FileHandle *control_pipe = (struct GNUNET_DISK_FileHandle *) cls;
   int sig;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' invoked because of %d\n", __FUNCTION__, tc->reason);
-
+#if DEBUG_OS
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+             "`%s' invoked because of %d\n",
+             __FUNCTION__,
+             tc->reason);
+#endif
   if (tc->reason & (GNUNET_SCHEDULER_REASON_SHUTDOWN | GNUNET_SCHEDULER_REASON_TIMEOUT | GNUNET_SCHEDULER_REASON_PREREQ_DONE))
-  {
-    GNUNET_DISK_npipe_close (control_pipe);
-  }
-  else
-  {
-    if (GNUNET_DISK_file_read (control_pipe, &sig, sizeof (sig)) != sizeof (sig))
     {
-      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read");
       GNUNET_DISK_npipe_close (control_pipe);
     }
-    else
+  else
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got control code %d from parent\n", sig);
-      raise (sig);
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Re-scheduling the parent control handler pipe\n");
-      GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, control_pipe, GNUNET_OS_parent_control_handler, control_pipe);
+      if (GNUNET_DISK_file_read (control_pipe, 
+                                &sig, 
+                                sizeof (sig)) != sizeof (sig))
+       {
+         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
+                              "GNUNET_DISK_file_read");
+         GNUNET_DISK_npipe_close (control_pipe);
+       }
+      else
+       {
+#if DEBUG_OS
+         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+                     "Got control code %d from parent\n", sig);
+#endif
+         GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 
+                                         control_pipe, 
+                                         &parent_control_handler, control_pipe);
+         raise (sig);
+       }
     }
-  }
 }
 
+
 /**
- * Connects this process to its parent via pipe
+ * Task that connects this process to its parent via pipe
  */
 void
 GNUNET_OS_install_parent_control_handler (void *cls,
                                           const struct
                                           GNUNET_SCHEDULER_TaskContext * tc)
 {
-  char *env_buf;
-  struct GNUNET_DISK_FileHandle *control_pipe = NULL;
+  const char *env_buf;
+  struct GNUNET_DISK_FileHandle *control_pipe;
 
   env_buf = getenv (GNUNET_OS_CONTROL_PIPE);
-  if (env_buf == NULL || strlen (env_buf) <= 0)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not installing a handler because %s=%s\n", GNUNET_OS_CONTROL_PIPE, env_buf);
-    return;
-  }
-
-  control_pipe = GNUNET_DISK_npipe_open (env_buf, GNUNET_DISK_OPEN_READ,
-        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
+  if ( (env_buf == NULL) || (strlen (env_buf) <= 0) )
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
+                 _("Not installing a handler because $%s=%s\n"), 
+                 GNUNET_OS_CONTROL_PIPE, 
+                 env_buf);
+      return;
+    }
+  control_pipe = GNUNET_DISK_npipe_open (env_buf,
+                                        GNUNET_DISK_OPEN_READ,
+                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
   if (control_pipe == NULL)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to open the pipe `%s'\n", env_buf);
-    return;
-  }
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding parent control handler pipe `%s' to the scheduler\n", env_buf);
-  GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, control_pipe, GNUNET_OS_parent_control_handler, control_pipe);
+    {
+      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
+                               "open",
+                               env_buf);
+      return;
+    }
+#if DEBUG_OS
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Adding parent control handler pipe `%s' to the scheduler\n", 
+             env_buf);
+#endif
+  GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 
+                                 control_pipe, 
+                                 &parent_control_handler, 
+                                 control_pipe);
 }
 
+
 /**
  * Get process structure for current process
  *
@@ -129,28 +156,55 @@ GNUNET_OS_process_current ()
   return &current_process;
 }
 
+
 int
 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig)
 {
 #if ENABLE_WINDOWS_WORKAROUNDS
-  int res;
-  int ret;
+  int res = 0;
+  int ret = 0;
 
   ret = GNUNET_DISK_file_write (proc->control_pipe, &sig, sizeof(sig));
   if (ret != sizeof(sig))
   {
     if (errno == ECOMM)
+    {
       /* Child process is not controllable via pipe */
+#if DEBUG_OS
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
           "Child process is not controllable, will kill it directly\n");
+#endif
+    }
+    else if (errno == EPIPE)
+    {
+#if DEBUG_OS
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+          "Failed to write into control pipe, because pipe is invalid (the child is most likely dead)\n");
+#endif
+    }
     else
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
           "Failed to write into control pipe , errno is %d\n", errno);
-    res = PLIBC_KILL (proc->pid, sig);
+#if WINDOWS && !defined(__CYGWIN__)
+    TerminateProcess (proc->handle, 0);
+#else
+    PLIBC_KILL (proc->pid, sig);
+#endif
   }
   else
   {
-       struct GNUNET_NETWORK_FDSet *rfds;
+#if DEBUG_OS
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+        "Wrote control code into control pipe, now waiting\n");
+#endif
+
+#if WINDOWS
+    /* Give it 3 seconds to die, then kill it in a nice Windows-specific way */
+    if (WaitForSingleObject (proc->handle, 3000) != WAIT_OBJECT_0)
+      TerminateProcess (proc->handle, 0);
+    res = 0;
+#else
+    struct GNUNET_NETWORK_FDSet *rfds;
     struct GNUNET_NETWORK_FDSet *efds;
 
     rfds = GNUNET_NETWORK_fdset_create ();
@@ -159,33 +213,43 @@ GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig)
     GNUNET_NETWORK_fdset_handle_set (rfds, proc->control_pipe);
     GNUNET_NETWORK_fdset_handle_set (efds, proc->control_pipe);
 
- read_next:
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-            "Wrote control code into control pipe, now waiting\n");
-
-        ret = GNUNET_NETWORK_socket_select (rfds, NULL, efds,
-            GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_unit (),
-                5000));
-
-        if (ret < 1 || GNUNET_NETWORK_fdset_handle_isset (efds,
-            proc->control_pipe))
-          {
-            /* Just to be sure */
-            PLIBC_KILL (proc->pid, sig);
-            res = 0;
-          }
-        else
-          {
-            if (GNUNET_DISK_file_read (proc->control_pipe, &ret,
-                sizeof(ret)) != GNUNET_OK)
-              res = PLIBC_KILL (proc->pid, sig);
-
-            /* Child signaled shutdown is in progress */
-            goto read_next;
-          }
-      }
+    /* Ndurner thought this up, and i have no idea what it does.
+     * There's have never been any code to answer the shutdown call
+     * (write a single int into the pipe, so that this function can read it).
+     * On *nix select() will probably tell that pipe is ready
+     * for reading, once the other process shuts down,
+     * but the read () call will fail, triggering a kill ()
+     * on the pid that is already dead. This will probably result in non-0
+     * return from kill(), and therefore from this function.
+     */
+    while (1)
+    {
+      ret = GNUNET_NETWORK_socket_select (rfds, NULL, efds,
+          GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_unit (),
+              5000));
 
-    return res;
+      if (ret < 1 || GNUNET_NETWORK_fdset_handle_isset (efds,
+          proc->control_pipe))
+        {
+          /* Just to be sure */
+          PLIBC_KILL (proc->pid, sig);
+          res = 0;
+          break;
+        }
+      else
+        {
+          if (GNUNET_DISK_file_read (proc->control_pipe, &ret,
+              sizeof(ret)) != GNUNET_OK)
+            res = PLIBC_KILL (proc->pid, sig);
+
+          /* Child signaled shutdown is in progress */
+          continue;
+        }
+     }
+#endif
+  }
+
+  return res;
 #else
   return kill (proc->pid, sig);
 #endif
@@ -204,6 +268,7 @@ GNUNET_OS_process_get_pid (struct GNUNET_OS_Process *proc)
   return proc->pid;
 }
 
+
 void
 GNUNET_OS_process_close (struct GNUNET_OS_Process *proc)
 {
@@ -348,8 +413,10 @@ GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc,
         }
     }
 #else
+#if DEBUG_OS
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
              "Priority management not availabe for this platform\n");
+#endif
 #endif
   return GNUNET_OK;
 }
@@ -372,7 +439,7 @@ CreateCustomEnvTable (char **vars)
     return NULL;
   for (c = 0, var_ptr = vars; *var_ptr; var_ptr += 2, c++);
   n_var = c;
-  index = GNUNET_malloc (n_var);
+  index = GNUNET_malloc (sizeof (char *) * n_var);
   for (c = 0; c < n_var; c++)
     index[c] = 0;
   for (items_count = 0, ptr = win32_env_table; ptr[0] != 0; items_count++)
@@ -651,12 +718,12 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
   cmdlen = 0;
   va_copy (ap, va);
   while (NULL != (arg = va_arg (ap, char *)))
-  {
+    {
       if (cmdlen == 0)
         cmdlen = cmdlen + strlen (path) + 3;
       else
         cmdlen = cmdlen + strlen (arg) + 3;
-  }
+    }
   va_end (ap);
 
   cmd = idx = GNUNET_malloc (sizeof (char) * (cmdlen + 1));
@@ -698,9 +765,11 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
     return NULL;
   }
 
+#if DEBUG_OS
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
              "Opened the parent end of the pipe `%s'\n", 
              childpipename);
+#endif
 
   GNUNET_asprintf (&our_env[0], "%s=", GNUNET_OS_CONTROL_PIPE);
   GNUNET_asprintf (&our_env[1], "%s", childpipename);
@@ -1038,7 +1107,9 @@ GNUNET_OS_start_process_v (const int *lsocks,
     return NULL;
   }
 
+#if DEBUG_OS
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Opened the parent end of the pipe `%s'\n", childpipename);
+#endif
 
   GNUNET_asprintf (&our_env[0], "%s=", GNUNET_OS_CONTROL_PIPE);
   GNUNET_asprintf (&our_env[1], "%s", childpipename);
@@ -1173,6 +1244,7 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
   return GNUNET_OK;
 }
 
+
 /**
  * Wait for a process
  * @param proc pointer to process structure
@@ -1279,14 +1351,13 @@ struct GNUNET_OS_CommandHandle
 void
 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
 {
-  int killed;
 
   if (cmd->proc != NULL)
     {
       GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cmd->rtask);
       GNUNET_SCHEDULER_cancel (cmd->rtask);
     }
-  killed = GNUNET_OS_process_kill (cmd->eip, SIGKILL);
+  (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL);
   GNUNET_break (GNUNET_OK ==
                GNUNET_OS_process_wait (cmd->eip));
   GNUNET_OS_process_close (cmd->eip);