fix memleak
[oweals/gnunet.git] / src / arm / arm_api.c
index d6b65e8721ede1f38bf9e0362c18fe3ed9cc09d0..15563e93398b840d64cf6ed0e3b53b50fbc6e66b 100644 (file)
  */
 #include "platform.h"
 #include "gnunet_arm_service.h"
-#include "gnunet_client_lib.h"
-#include "gnunet_getopt_lib.h"
-#include "gnunet_os_lib.h"
+#include "gnunet_util_lib.h"
 #include "gnunet_protocols.h"
-#include "gnunet_server_lib.h"
 #include "arm.h"
 
 #define LOG(kind,...) GNUNET_log_from (kind, "arm-api",__VA_ARGS__)
@@ -295,6 +292,11 @@ struct RequestContext
    */
   uint16_t type;
 
+  /**
+   * Flags for passing std descriptors to ARM (when starting ARM).
+   */
+  enum GNUNET_OS_InheritStdioFlags std_inheritance;
+
 };
 
 #include "do_start_process.c"
@@ -344,10 +346,8 @@ arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "BINARY",
                                             &binary))
   {
-    LOG (GNUNET_ERROR_TYPE_WARNING,
-        _
-        ("Configuration failes to specify option `%s' in section `%s'!\n"),
-        "BINARY", "arm");
+    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
+                              "arm", "BINARY");
     if (pos->callback != NULL)
       pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
     GNUNET_free (pos);
@@ -359,9 +359,8 @@ arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg, "arm", "CONFIG",
                                               &config))
   {
-    LOG (GNUNET_ERROR_TYPE_WARNING,
-        _("Configuration fails to specify option `%s' in section `%s'!\n"),
-        "CONFIG", "arm");
+    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
+                              "arm", "CONFIG");
     if (pos->callback != NULL)
       pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
     GNUNET_free (binary);
@@ -381,21 +380,15 @@ arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   {
     /* Means we are ONLY running locally */
     /* we're clearly running a test, don't daemonize */
-    proc = do_start_process (GNUNET_NO,
+    proc = do_start_process (GNUNET_NO, pos->std_inheritance,
                             NULL, loprefix, binary, "-c", config,
-#if DEBUG_ARM
-                            "-L", "DEBUG",
-#endif
                             /* no daemonization! */
                             lopostfix, NULL);
   }
   else
   {
-    proc = do_start_process (GNUNET_NO,
+    proc = do_start_process (GNUNET_NO, pos->std_inheritance,
                             NULL, loprefix, binary, "-c", config,
-#if DEBUG_ARM
-                            "-L", "DEBUG",
-#endif
                             "-d", lopostfix, NULL);
   }
   GNUNET_free (binary);
@@ -433,11 +426,7 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
   if ((msg == NULL) ||
       (ntohs (msg->size) != sizeof (struct GNUNET_ARM_ResultMessage)))
   {
-    LOG (GNUNET_ERROR_TYPE_WARNING,
-        _
-        ("Error receiving response to `%s' request from ARM for service `%s'\n"),
-        (sc->type == GNUNET_MESSAGE_TYPE_ARM_START) ? "START" : "STOP",
-        (const char *) &sc[1]);
+    GNUNET_break (0);
     GNUNET_CLIENT_disconnect (sc->h->client);
     sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
     GNUNET_assert (NULL != sc->h->client);
@@ -488,8 +477,8 @@ change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        (type ==
        GNUNET_MESSAGE_TYPE_ARM_START) ?
-       _("Requesting start of service `%s'.\n") :
-       _("Requesting termination of service `%s'.\n"), service_name);
+       "Requesting start of service `%s'.\n" :
+       "Requesting termination of service `%s'.\n", service_name);
   sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
   sctx->h = h;
   sctx->callback = cb;
@@ -507,12 +496,7 @@ change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
                                               (sctx->timeout), GNUNET_YES,
                                               &handle_response, sctx))
   {
-    LOG (GNUNET_ERROR_TYPE_WARNING,
-        (type ==
-         GNUNET_MESSAGE_TYPE_ARM_START)
-        ? _("Error while trying to transmit request to start `%s' to ARM\n")    
-        : _("Error while trying to transmit request to stop `%s' to ARM\n"),
-        (const char *) &service_name);
+    GNUNET_break (0);
     if (cb != NULL)
       cb (cb_cls, GNUNET_SYSERR);
     GNUNET_free (sctx);
@@ -528,6 +512,7 @@ change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
  *
  * @param h handle to ARM
  * @param service_name name of the service
+ * @param std_inheritance inheritance of std streams
  * @param timeout how long to wait before failing for good
  * @param cb callback to invoke when service is ready
  * @param cb_cls closure for callback
@@ -535,6 +520,7 @@ change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
 void
 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
                          const char *service_name,
+                          enum GNUNET_OS_InheritStdioFlags std_inheritance,
                          struct GNUNET_TIME_Relative timeout,
                          GNUNET_ARM_Callback cb, void *cb_cls)
 {
@@ -543,8 +529,8 @@ GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
   size_t slen;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       _("Asked to start service `%s' within %llu ms\n"), service_name,
-       (unsigned long long) timeout.rel_value);
+       "Asked to start service `%s' within %s\n", service_name,
+       GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO));
   if (0 == strcasecmp ("arm", service_name))
   {
     slen = strlen ("arm") + 1;
@@ -553,12 +539,13 @@ GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
     sctx->callback = cb;
     sctx->cls = cb_cls;
     sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
+    sctx->std_inheritance = std_inheritance;
     memcpy (&sctx[1], service_name, slen);
     GNUNET_CLIENT_service_test ("arm", h->cfg, timeout, &arm_service_report,
                                sctx);
     return;
   }
-  if (h->client == NULL)
+  if (NULL == h->client)
   {
     client = GNUNET_CLIENT_connect ("arm", h->cfg);
     if (client == NULL)
@@ -592,7 +579,6 @@ arm_shutdown_callback (void *cls, enum GNUNET_ARM_ProcessStatus reason)
 
   if (arm_shutdown_ctx->cb != NULL)
     arm_shutdown_ctx->cb (arm_shutdown_ctx->cb_cls, reason);
-
   GNUNET_free (arm_shutdown_ctx);
 }
 
@@ -615,8 +601,10 @@ GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
   struct ARM_ShutdownContext *arm_shutdown_ctx;
   struct GNUNET_CLIENT_Connection *client;
 
-  LOG (GNUNET_ERROR_TYPE_INFO, _("Stopping service `%s' within %llu ms\n"),
-       service_name, (unsigned long long) timeout.rel_value);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, 
+       "Stopping service `%s' within %s\n",
+       service_name, 
+       GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO));
   if (h->client == NULL)
   {
     client = GNUNET_CLIENT_connect ("arm", h->cfg);
@@ -688,8 +676,7 @@ handle_list_response (void *cls, const struct GNUNET_MessageHeader *msg)
   
   if (NULL == msg)
   {
-    LOG (GNUNET_ERROR_TYPE_WARNING,
-        "Error receiving response to LIST request from ARM\n");
+    GNUNET_break (0);
     GNUNET_CLIENT_disconnect (sc->h->client);
     sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
     GNUNET_assert (NULL != sc->h->client);
@@ -764,13 +751,10 @@ GNUNET_ARM_list_running_services (struct GNUNET_ARM_Handle *h,
     client = GNUNET_CLIENT_connect ("arm", h->cfg);
     if (client == NULL)
     {
-      LOG (GNUNET_ERROR_TYPE_DEBUG,
-          "arm_api, GNUNET_CLIENT_connect returned NULL\n");
+      GNUNET_break (0);
       cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
       return;
     }
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-        "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
     h->client = client;
   }
   
@@ -783,8 +767,8 @@ GNUNET_ARM_list_running_services (struct GNUNET_ARM_Handle *h,
   msg.type = htons (GNUNET_MESSAGE_TYPE_ARM_LIST);
   
   LOG (GNUNET_ERROR_TYPE_DEBUG, 
-       "Requesting LIST from ARM service with timeout: %llu ms\n", 
-       (unsigned long long)timeout.rel_value);
+       "Requesting LIST from ARM service with timeout: %s\n", 
+       GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_YES));
   
   if (GNUNET_OK !=
       GNUNET_CLIENT_transmit_and_get_response (sctx->h->client, 
@@ -795,8 +779,7 @@ GNUNET_ARM_list_running_services (struct GNUNET_ARM_Handle *h,
                                                &handle_list_response, 
                                                sctx))
   {
-    LOG (GNUNET_ERROR_TYPE_WARNING, 
-        "Error while trying to transmit request to list services to ARM\n");
+    GNUNET_break (0);
     if (cb != NULL)
       cb (cb_cls, GNUNET_SYSERR, 0, NULL);
     GNUNET_free (sctx);