-LRN: fix socket_close logging
[oweals/gnunet.git] / src / util / os_priority.c
index 55a728ce6eb0e892bf1807fb0a1fbd474b821451..3b3b022b918cfaaa7c222807875bc7fdca2fbac4 100644 (file)
 #include "gnunet_scheduler_lib.h"
 #include "disk.h"
 
+#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
+
+#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
+
+#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
+
 #define GNUNET_OS_CONTROL_PIPE "GNUNET_OS_CONTROL_PIPE"
 
 struct GNUNET_OS_Process
@@ -60,8 +66,8 @@ parent_control_handler (void *cls,
   int sig;
 
 #if DEBUG_OS
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' invoked because of %d\n",
-              __FUNCTION__, tc->reason);
+  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 |
@@ -74,14 +80,13 @@ parent_control_handler (void *cls,
     if (GNUNET_DISK_file_read (control_pipe, &sig, sizeof (sig)) !=
         sizeof (sig))
     {
-      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read");
+      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);
+      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,
@@ -106,9 +111,8 @@ GNUNET_OS_install_parent_control_handler (void *cls,
   env_buf = getenv (GNUNET_OS_CONTROL_PIPE);
   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);
+    LOG (GNUNET_ERROR_TYPE_INFO, _("Not installing a handler because $%s=%s\n"),
+         GNUNET_OS_CONTROL_PIPE, env_buf);
     return;
   }
   control_pipe =
@@ -117,13 +121,12 @@ GNUNET_OS_install_parent_control_handler (void *cls,
                               GNUNET_DISK_PERM_USER_WRITE);
   if (control_pipe == NULL)
   {
-    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", env_buf);
+    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);
+  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);
@@ -165,20 +168,20 @@ GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig)
     {
       /* 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");
+      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");
+      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_WARNING,
-                  "Failed to write into control pipe , errno is %d\n", errno);
+      LOG (GNUNET_ERROR_TYPE_WARNING,
+           "Failed to write into control pipe , errno is %d\n", errno);
 #if WINDOWS && !defined(__CYGWIN__)
     TerminateProcess (proc->handle, 0);
 #else
@@ -188,8 +191,8 @@ GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig)
   else
   {
 #if DEBUG_OS
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Wrote control code into control pipe, now waiting\n");
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Wrote control code into control pipe, now waiting\n");
 #endif
 
 #if WINDOWS
@@ -394,8 +397,7 @@ GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc,
     errno = 0;
     if ((delta != 0) && (rprio == nice (delta)) && (errno != 0))
     {
-      GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
-                           "nice");
+      LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, "nice");
       return GNUNET_SYSERR;
     }
   }
@@ -403,15 +405,15 @@ GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc,
   {
     if (0 != setpriority (PRIO_PROCESS, pid, rprio))
     {
-      GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
-                           "setpriority");
+      LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
+                    "setpriority");
       return GNUNET_SYSERR;
     }
   }
 #else
 #if DEBUG_OS
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
-              "Priority management not availabe for this platform\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
+       "Priority management not availabe for this platform\n");
 #endif
 #endif
   return GNUNET_OK;
@@ -593,38 +595,18 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
                                        &fd_stdin_write, sizeof (int));
   }
 
-#if HAVE_WORKING_VFORK
-  ret = vfork ();
-#else
   ret = fork ();
-#endif
   if (ret != 0)
   {
     if (ret == -1)
     {
-      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork");
+      LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
 #if ENABLE_WINDOWS_WORKAROUNDS
       GNUNET_DISK_npipe_close (control_pipe);
 #endif
     }
     else
     {
-
-#if HAVE_WORKING_VFORK
-      /* let's hope vfork actually works; for some extreme cases (including
-       * a testcase) we need 'execvp' to have run before we return, since
-       * we may send a signal to the process next and we don't want it
-       * to be caught by OUR signal handler (but either by the default
-       * handler or the actual handler as installed by the process itself). */
-#else
-      /* let's give the child process a chance to run execvp, 1s should
-       * be plenty in practice */
-      if (pipe_stdout != NULL)
-        GNUNET_DISK_pipe_close_end (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
-      if (pipe_stdin != NULL)
-        GNUNET_DISK_pipe_close_end (pipe_stdin, GNUNET_DISK_PIPE_END_READ);
-      sleep (1);
-#endif
       gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process));
       gnunet_proc->pid = ret;
 #if ENABLE_WINDOWS_WORKAROUNDS
@@ -647,7 +629,7 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
   {
     GNUNET_break (0 == close (fd_stdout_read));
     if (-1 == dup2 (fd_stdout_write, 1))
-      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
+      LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
     GNUNET_break (0 == close (fd_stdout_write));
   }
 
@@ -656,11 +638,11 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
 
     GNUNET_break (0 == close (fd_stdin_write));
     if (-1 == dup2 (fd_stdin_read, 0))
-      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
+      LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
     GNUNET_break (0 == close (fd_stdin_read));
   }
   execvp (filename, argv);
-  GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
+  LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
   _exit (1);
 #else
   char *arg;
@@ -722,8 +704,8 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
             path, NULL))
   {
     SetErrnoFromWinError (GetLastError ());
-    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "SearchPath",
-                              non_const_filename);
+    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath",
+                       non_const_filename);
     GNUNET_free (non_const_filename);
     GNUNET_free (pathbuf);
     return NULL;
@@ -788,8 +770,8 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
   }
 
 #if DEBUG_OS
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Opened the parent end of the pipe `%s'\n", childpipename);
+  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);
@@ -804,7 +786,7 @@ GNUNET_OS_start_process_va (struct GNUNET_DISK_PipeHandle *pipe_stdin,
        env_block, NULL, &start, &proc))
   {
     SetErrnoFromWinError (GetLastError ());
-    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "CreateProcess", path);
+    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "CreateProcess", path);
     GNUNET_free (env_block);
     GNUNET_free (cmd);
     return NULL;
@@ -904,33 +886,18 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
       GNUNET_array_append (lscp, ls, k);
     GNUNET_array_append (lscp, ls, -1);
   }
-#if HAVE_WORKING_VFORK
-  ret = vfork ();
-#else
   ret = fork ();
-#endif
   if (ret != 0)
   {
     if (ret == -1)
     {
-      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork");
+      LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
 #if ENABLE_WINDOWS_WORKAROUNDS
       GNUNET_DISK_npipe_close (control_pipe);
 #endif
     }
     else
     {
-#if HAVE_WORKING_VFORK
-      /* let's hope vfork actually works; for some extreme cases (including
-       * a testcase) we need 'execvp' to have run before we return, since
-       * we may send a signal to the process next and we don't want it
-       * to be caught by OUR signal handler (but either by the default
-       * handler or the actual handler as installed by the process itself). */
-#else
-      /* let's give the child process a chance to run execvp, 1s should
-       * be plenty in practice */
-      sleep (1);
-#endif
       gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process));
       gnunet_proc->pid = ret;
 #if ENABLE_WINDOWS_WORKAROUNDS
@@ -994,7 +961,7 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
   }
   GNUNET_array_grow (lscp, ls, 0);
   execvp (filename, argv);
-  GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
+  LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
   _exit (1);
 #else
   char **arg, **non_const_argv;
@@ -1062,8 +1029,8 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
             path, NULL))
   {
     SetErrnoFromWinError (GetLastError ());
-    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "SearchPath",
-                              non_const_filename);
+    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath",
+                       non_const_filename);
     GNUNET_free (non_const_filename);
     GNUNET_free (pathbuf);
     return NULL;
@@ -1133,8 +1100,8 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
   }
 
 #if DEBUG_OS
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Opened the parent end of the pipe `%s'\n", childpipename);
+  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);
@@ -1149,7 +1116,7 @@ GNUNET_OS_start_process_v (const int *lsocks, const char *filename,
        env_block, NULL, &start, &proc))
   {
     SetErrnoFromWinError (GetLastError ());
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "CreateProcess");
+    LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "CreateProcess");
     GNUNET_free (env_block);
     GNUNET_free (cmd);
     return NULL;
@@ -1193,7 +1160,7 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
   ret = waitpid (proc->pid, &status, WNOHANG);
   if (ret < 0)
   {
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
+    LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
     return GNUNET_SYSERR;
   }
   if (0 == ret)
@@ -1204,7 +1171,7 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
   }
   if (proc->pid != ret)
   {
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
+    LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
     return GNUNET_SYSERR;
   }
   if (WIFEXITED (status))
@@ -1242,8 +1209,8 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
   ret = proc->pid;
   if (h == NULL || ret == 0)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                "Invalid process information {%d, %08X}\n", ret, h);
+    LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid process information {%d, %08X}\n",
+         ret, h);
     return GNUNET_SYSERR;
   }
   if (h == NULL)
@@ -1255,7 +1222,7 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
   if (ret == 0 || error_code != NO_ERROR)
   {
     SetErrnoFromWinError (error_code);
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess");
+    LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess");
     return GNUNET_SYSERR;
   }
   if (STILL_ACTIVE == c)
@@ -1294,8 +1261,8 @@ GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
   h = proc->handle;
   if (NULL == h)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                "Invalid process information {%d, %08X}\n", proc->pid, h);
+    LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid process information {%d, %08X}\n",
+         proc->pid, h);
     return GNUNET_SYSERR;
   }
   if (h == NULL)