Add missing include
[oweals/gnunet.git] / src / arm / gnunet-service-arm.c
index 3c495b9eb069e43ac263b3b71f1f4bf1600593cc..1196b20aa7242716a4bc1910348684d70a92a9c0 100644 (file)
@@ -240,10 +240,10 @@ static struct GNUNET_SERVER_NotificationContext *notifier;
 /**
  * Transmit a status result message.
  *
- * @param cls pointer to "unit16_t*" with message type
- * @param size number of bytes available in buf
+ * @param cls a `unit16_t *` with message type
+ * @param size number of bytes available in @a buf
  * @param buf where to copy the message, NULL on error
- * @return number of bytes copied to buf
+ * @return number of bytes copied to @a buf
  */
 static size_t
 write_result (void *cls, size_t size, void *buf)
@@ -259,7 +259,8 @@ write_result (void *cls, size_t size, void *buf)
     return 0;                  /* error, not much we can do */
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Sending status response %u to client\n", (unsigned int) msg->result);
+             "Sending status response %u to client\n",
+             (unsigned int) msg->result);
   msize = msg->arm_msg.header.size;
   GNUNET_assert (size >= msize);
   msg->arm_msg.header.size = htons (msg->arm_msg.header.size);
@@ -271,20 +272,21 @@ write_result (void *cls, size_t size, void *buf)
   return msize;
 }
 
+
 /**
  * Transmit the list of running services.
- * 
- * @param cls pointer to struct GNUNET_ARM_ListResultMessage with the message
- * @param size number of bytes available in buf
+ *
+ * @param cls pointer to `struct GNUNET_ARM_ListResultMessage` with the message
+ * @param size number of bytes available in @a buf
  * @param buf where to copy the message, NULL on error
- * @return number of bytes copied to buf
+ * @return number of bytes copied to @a buf
  */
 static size_t
 write_list_result (void *cls, size_t size, void *buf)
 {
   struct GNUNET_ARM_ListResultMessage *msg = cls;
   size_t rslt_size;
-  
+
   if (buf == NULL)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
@@ -292,14 +294,14 @@ write_list_result (void *cls, size_t size, void *buf)
     GNUNET_free (msg);
     return 0;                   /* error, not much we can do */
   }
-  
+
   rslt_size = msg->arm_msg.header.size;
   GNUNET_assert (size >= rslt_size);
   msg->arm_msg.header.size = htons (msg->arm_msg.header.size);
   msg->arm_msg.header.type = htons (msg->arm_msg.header.type);
   msg->arm_msg.request_id = GNUNET_htonll (msg->arm_msg.request_id);
   msg->count = htons (msg->count);
-  
+
   memcpy (buf, msg, rslt_size);
   GNUNET_free (msg);
   return rslt_size;
@@ -312,12 +314,15 @@ write_list_result (void *cls, size_t size, void *buf)
  *
  * @param client who is being signalled
  * @param name name of the service
+ * @param request_id id of the request that is being responded to.
  * @param result message type to send
  * @return NULL if it was not found
  */
 static void
-signal_result (struct GNUNET_SERVER_Client *client, const char *name,
-              uint64_t request_id, enum GNUNET_ARM_Result result)
+signal_result (struct GNUNET_SERVER_Client *client,
+              const char *name,
+              uint64_t request_id,
+              enum GNUNET_ARM_Result result)
 {
   struct GNUNET_ARM_ResultMessage *msg;
   size_t msize;
@@ -330,7 +335,8 @@ signal_result (struct GNUNET_SERVER_Client *client, const char *name,
   msg->arm_msg.request_id = request_id;
 
   GNUNET_SERVER_notify_transmit_ready (client, msize,
-      GNUNET_TIME_UNIT_FOREVER_REL, write_result, msg);
+                                      GNUNET_TIME_UNIT_FOREVER_REL,
+                                      write_result, msg);
 }
 
 
@@ -339,10 +345,13 @@ signal_result (struct GNUNET_SERVER_Client *client, const char *name,
  *
  * @param name name of the service
  * @param status message type to send
+ * @param unicast if not NULL, send to this client only.
+ *                otherwise, send to all clients in the notifier
  */
 static void
-broadcast_status (const char *name, enum GNUNET_ARM_ServiceStatus status,
-    struct GNUNET_SERVER_Client *unicast)
+broadcast_status (const char *name,
+                 enum GNUNET_ARM_ServiceStatus status,
+                 struct GNUNET_SERVER_Client *unicast)
 {
   struct GNUNET_ARM_StatusMessage *msg;
   size_t namelen;
@@ -374,6 +383,8 @@ broadcast_status (const char *name, enum GNUNET_ARM_ServiceStatus status,
  *
  * @param sl identifies service to start
  * @param client that asked to start the service (may be NULL)
+ * @param request_id id of the request in response to which the process is
+ *                   being started. 0 if starting was not requested.
  */
 static void
 start_process (struct ServiceList *sl, struct GNUNET_SERVER_Client *client, uint64_t request_id)
@@ -481,7 +492,7 @@ start_process (struct ServiceList *sl, struct GNUNET_SERVER_Client *client, uint
     if (NULL == sl->config)
       sl->proc =
        do_start_process (sl->pipe_control, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
-                         lsocks, loprefix, binary, 
+                         lsocks, loprefix, binary,
                          options, NULL);
     else
       sl->proc =
@@ -539,7 +550,7 @@ find_service (const char *name)
  * First connection has come to the listening socket associated with the service,
  * create the service in order to relay the incoming connection to it
  *
- * @param cls callback data, struct ServiceListeningInfo describing a listen socket
+ * @param cls callback data, `struct ServiceListeningInfo` describing a listen socket
  * @param tc context
  */
 static void
@@ -561,7 +572,7 @@ accept_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * wait for the first incoming connection to it
  *
  * @param sa address associated with the service
- * @param addr_len length of sa
+ * @param addr_len length of @a sa
  * @param sl service entry for the service in question
  */
 static void
@@ -612,7 +623,7 @@ create_listen_socket (struct sockaddr *sa, socklen_t addr_len,
 #endif
 
   if (GNUNET_NETWORK_socket_bind
-      (sock, (const struct sockaddr *) sa, addr_len) != GNUNET_OK)
+      (sock, (const struct sockaddr *) sa, addr_len, 0) != GNUNET_OK)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  _
@@ -669,8 +680,8 @@ free_service (struct ServiceList *sl)
  * @param cls closure (always NULL)
  * @param client identification of the client
  * @param message the actual message
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
+ * @return #GNUNET_OK to keep the connection open,
+ *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static void
 handle_start (void *cls, struct GNUNET_SERVER_Client *client,
@@ -682,7 +693,7 @@ handle_start (void *cls, struct GNUNET_SERVER_Client *client,
   uint64_t request_id;
   struct GNUNET_ARM_Message *amsg;
 
-  amsg = (struct GNUNET_ARM_Message *) message;  
+  amsg = (struct GNUNET_ARM_Message *) message;
   request_id = GNUNET_ntohll (amsg->request_id);
   size = ntohs (amsg->header.size);
   size -= sizeof (struct GNUNET_ARM_Message);
@@ -738,8 +749,8 @@ trigger_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @param cls closure (always NULL)
  * @param client identification of the client
  * @param message the actual message
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
+ * @return #GNUNET_OK to keep the connection open,
+ *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static void
 handle_stop (void *cls, struct GNUNET_SERVER_Client *client,
@@ -751,7 +762,7 @@ handle_stop (void *cls, struct GNUNET_SERVER_Client *client,
   uint64_t request_id;
   struct GNUNET_ARM_Message *amsg;
 
-  amsg = (struct GNUNET_ARM_Message *) message;  
+  amsg = (struct GNUNET_ARM_Message *) message;
   request_id = GNUNET_ntohll (amsg->request_id);
   size = ntohs (amsg->header.size);
   size -= sizeof (struct GNUNET_ARM_Message);
@@ -818,6 +829,7 @@ handle_stop (void *cls, struct GNUNET_SERVER_Client *client,
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
+
 /**
  * Handle LIST-message.
  *
@@ -835,10 +847,10 @@ handle_list (void *cls, struct GNUNET_SERVER_Client *client,
   size_t total_size;
   struct ServiceList *sl;
   uint16_t count;
-  
+
   if (NULL == client)
     return;
-  
+
   request = (struct GNUNET_ARM_Message *) message;
   count = 0;
   string_list_size = 0;
@@ -854,16 +866,16 @@ handle_list (void *cls, struct GNUNET_SERVER_Client *client,
     }
   }
 
-  total_size = sizeof (struct GNUNET_ARM_ListResultMessage) 
+  total_size = sizeof (struct GNUNET_ARM_ListResultMessage)
                + string_list_size;
   msg = GNUNET_malloc (total_size);
   msg->arm_msg.header.size = total_size;
   msg->arm_msg.header.type = GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT;
   msg->arm_msg.request_id = GNUNET_ntohll (request->request_id);
   msg->count = count;
-  
+
   char *pos = (char *)&msg[1];
-  for (sl = running_head; sl != NULL; sl = sl->next) 
+  for (sl = running_head; sl != NULL; sl = sl->next)
   {
     if (sl->proc != NULL)
     {
@@ -872,7 +884,7 @@ handle_list (void *cls, struct GNUNET_SERVER_Client *client,
       pos += s;
     }
   }
-  
+
   GNUNET_SERVER_notify_transmit_ready (client,
                                        total_size,
                                        GNUNET_TIME_UNIT_FOREVER_REL,
@@ -880,6 +892,7 @@ handle_list (void *cls, struct GNUNET_SERVER_Client *client,
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
+
 /**
  * We are done with everything.  Stop remaining
  * tasks, signal handler and the server.
@@ -905,16 +918,21 @@ do_shutdown ()
     }
 }
 
-unsigned int
+
+static unsigned int
 list_count (struct ServiceList *running_head)
 {
   struct ServiceList *i;
   unsigned int res = 0;
+
   for (res = 0, i = running_head; i; i = i->next, res++)
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s\n", i->name);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+               "%s\n",
+               i->name);
   return res;
 }
 
+
 /**
  * Task run for shutdown.
  *
@@ -928,7 +946,8 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   struct ServiceList *nxt;
   struct ServiceListeningInfo *sli;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "First shutdown phase\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "First shutdown phase\n");
   if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
   {
     GNUNET_SCHEDULER_cancel (child_restart_task);
@@ -960,7 +979,8 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     nxt = pos->next;
     if (pos->proc != NULL)
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping service `%s'\n",
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                 "Stopping service `%s'\n",
                  pos->name);
       pos->killed_at = GNUNET_TIME_absolute_get ();
       if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM))
@@ -976,7 +996,8 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     do_shutdown ();
   else
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-        "Delaying shutdown, have %u childs still running\n", list_count (running_head));
+               "Delaying shutdown, have %u childs still running\n",
+               list_count (running_head));
 }
 
 
@@ -1007,8 +1028,7 @@ delayed_restart_task (void *cls,
     if (NULL != sl->proc)
       continue;
     /* service is currently not running */
-    if (GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value ==
-       0)
+    if (0 == GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value_us)
     {
       /* restart is now allowed */
       if (sl->is_default)
@@ -1041,14 +1061,14 @@ delayed_restart_task (void *cls,
                                  (sl->restart_at));
     }
   }
-  if (lowestRestartDelay.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
+  if (lowestRestartDelay.rel_value_us != GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Will restart process in %s\n",
                GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay, GNUNET_YES));
     child_restart_task =
       GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
-                                                 GNUNET_SCHEDULER_PRIORITY_IDLE, 
+                                                 GNUNET_SCHEDULER_PRIORITY_IDLE,
                                                  &delayed_restart_task, NULL);
   }
 }
@@ -1121,7 +1141,7 @@ maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
        statstr = _( /* process termination method */ "unknown");
        statcode = 0;
       }
-      if (0 != pos->killed_at.abs_value)
+      if (0 != pos->killed_at.abs_value_us)
       {
        GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                    _("Service `%s' took %s to terminate\n"),
@@ -1138,24 +1158,24 @@ maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
         GNUNET_SERVER_client_drop (pos->killing_client);
         pos->killing_client = NULL;
         pos->killing_client_request_id = 0;
-        /* process can still be re-started on-demand, ensure it is re-started if there is demand */
-        for (sli = pos->listen_head; NULL != sli; sli = sli->next)
-        {
-          GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sli->accept_task);
-          sli->accept_task =
-              GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
-                  sli->listen_socket, &accept_connection, sli);
-        }
       }
       if (GNUNET_YES != in_shutdown)
       {
         if ((statusType == GNUNET_OS_PROCESS_EXITED) && (statcode == 0))
         {
           /* process terminated normally, allow restart at any time */
-          pos->restart_at.abs_value = 0;
+          pos->restart_at.abs_value_us = 0;
           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               _("Service `%s' terminated normally, will restart at any time\n"),
               pos->name);
+          /* process can still be re-started on-demand, ensure it is re-started if there is demand */
+          for (sli = pos->listen_head; NULL != sli; sli = sli->next)
+          {
+            GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sli->accept_task);
+            sli->accept_task =
+                GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
+                    sli->listen_socket, &accept_connection, sli);
+          }
        }
         else
         {
@@ -1167,11 +1187,11 @@ maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
          /* schedule restart */
          pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
          pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
-        }
-       if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
-         GNUNET_SCHEDULER_cancel (child_restart_task);
-       child_restart_task = GNUNET_SCHEDULER_add_with_priority (
+          if (GNUNET_SCHEDULER_NO_TASK != child_restart_task)
+            GNUNET_SCHEDULER_cancel (child_restart_task);
+          child_restart_task = GNUNET_SCHEDULER_add_with_priority (
             GNUNET_SCHEDULER_PRIORITY_IDLE, &delayed_restart_task, NULL);
+        }
       }
       else
       {
@@ -1214,7 +1234,7 @@ sighandler_child_death ()
  *
  * @param cls unused
  * @param section a section in the configuration file
- * @return GNUNET_OK (continue)
+ * @return #GNUNET_OK (continue)
  */
 static void
 setup_service (void *cls, const char *section)
@@ -1241,6 +1261,7 @@ setup_service (void *cls, const char *section)
   {
     /* got the same section twice!? */
     GNUNET_break (0);
+    GNUNET_free (binary);
     return;
   }
   config = NULL;
@@ -1254,14 +1275,14 @@ setup_service (void *cls, const char *section)
   {
     if (NULL != config)
     {
-      GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING, 
+      GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
                                 section, "CONFIG",
                                 STRERROR (errno));
       GNUNET_free (config);
       config = NULL;
     }
   }
-  sl = GNUNET_malloc (sizeof (struct ServiceList));
+  sl = GNUNET_new (struct ServiceList);
   sl->name = GNUNET_strdup (section);
   sl->binary = binary;
   sl->config = config;
@@ -1272,7 +1293,7 @@ setup_service (void *cls, const char *section)
 #else
   if (GNUNET_CONFIGURATION_have_value (cfg, section, "PIPECONTROL"))
     sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "PIPECONTROL");
-#endif  
+#endif
   GNUNET_CONTAINER_DLL_insert (running_head, running_tail, sl);
   if (GNUNET_YES !=
       GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "AUTOSTART"))
@@ -1304,14 +1325,15 @@ handle_client_connecting (void *cls, struct GNUNET_SERVER_Client *client)
     GNUNET_SERVER_client_mark_monitor (client);
 }
 
+
 /**
  * Handle MONITOR-message.
  *
  * @param cls closure (always NULL)
  * @param client identification of the client
  * @param message the actual message
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
+ * @return #GNUNET_OK to keep the connection open,
+ *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static void
 handle_monitor (void *cls, struct GNUNET_SERVER_Client *client,
@@ -1326,6 +1348,7 @@ handle_monitor (void *cls, struct GNUNET_SERVER_Client *client,
   }
 }
 
+
 /**
  * Process arm requests.
  *
@@ -1340,9 +1363,9 @@ run (void *cls, struct GNUNET_SERVER_Handle *serv,
   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
     {&handle_start, NULL, GNUNET_MESSAGE_TYPE_ARM_START, 0},
     {&handle_stop, NULL, GNUNET_MESSAGE_TYPE_ARM_STOP, 0},
-    {&handle_monitor, NULL, GNUNET_MESSAGE_TYPE_ARM_MONITOR, 
-     sizeof (struct GNUNET_ARM_Message)},
-    {&handle_list, NULL, GNUNET_MESSAGE_TYPE_ARM_LIST, 
+    {&handle_monitor, NULL, GNUNET_MESSAGE_TYPE_ARM_MONITOR,
+     sizeof (struct GNUNET_MessageHeader)},
+    {&handle_list, NULL, GNUNET_MESSAGE_TYPE_ARM_LIST,
      sizeof (struct GNUNET_ARM_Message)},
     {NULL, NULL, 0, 0}
   };
@@ -1433,7 +1456,7 @@ main (int argc, char *const *argv)
     GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
   ret =
     (GNUNET_OK ==
-     GNUNET_SERVICE_run (argc, argv, "arm", 
+     GNUNET_SERVICE_run (argc, argv, "arm",
                         GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN, &run, NULL)) ? 0 : 1;
   GNUNET_SIGNAL_handler_uninstall (shc_chld);
   shc_chld = NULL;