From 502af2167f7c218366666ca4944bd7cc54b5b19a Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 15 Aug 2011 21:46:35 +0000 Subject: indentation --- src/arm/arm_api.c | 508 +++++++------- src/arm/do_start_process.c | 79 ++- src/arm/gnunet-arm.c | 294 +++++---- src/arm/gnunet-service-arm.c | 744 ++++++++++----------- src/arm/gnunet-service-arm.h | 5 +- src/arm/gnunet-service-arm_interceptor.c | 1055 ++++++++++++++---------------- src/arm/mockup-service.c | 30 +- src/arm/test_arm_api.c | 44 +- src/arm/test_exponential_backoff.c | 292 ++++----- src/arm/test_gnunet_service_manager.c | 123 ++-- 10 files changed, 1525 insertions(+), 1649 deletions(-) (limited to 'src/arm') diff --git a/src/arm/arm_api.c b/src/arm/arm_api.c index 4114b8528..0db9e5c3e 100644 --- a/src/arm/arm_api.c +++ b/src/arm/arm_api.c @@ -106,61 +106,58 @@ service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg) struct ShutdownContext *shutdown_ctx = cls; if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES)) - { + { #if DEBUG_ARM - /* Means the other side closed the connection and never confirmed a shutdown */ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Service handle shutdown before ACK!\n"); + /* Means the other side closed the connection and never confirmed a shutdown */ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Service handle shutdown before ACK!\n"); #endif - if (shutdown_ctx->cont != NULL) - shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR); - GNUNET_SCHEDULER_cancel(shutdown_ctx->cancel_task); - GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); - GNUNET_free(shutdown_ctx); - } + if (shutdown_ctx->cont != NULL) + shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR); + GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task); + GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); + GNUNET_free (shutdown_ctx); + } else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES)) - { + { #if DEBUG_ARM - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "Service shutdown complete.\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n"); #endif - if (shutdown_ctx->cont != NULL) - shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_NO); + if (shutdown_ctx->cont != NULL) + shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_NO); - GNUNET_SCHEDULER_cancel(shutdown_ctx->cancel_task); - GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); - GNUNET_free(shutdown_ctx); - } + GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task); + GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); + GNUNET_free (shutdown_ctx); + } else + { + GNUNET_assert (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader)); + switch (ntohs (msg->type)) { - GNUNET_assert(ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader)); - switch (ntohs(msg->type)) - { - case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK: + case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK: #if DEBUG_ARM - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "Received confirmation for service shutdown.\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received confirmation for service shutdown.\n"); #endif - shutdown_ctx->confirmed = GNUNET_YES; - GNUNET_CLIENT_receive (shutdown_ctx->sock, - &service_shutdown_handler, - shutdown_ctx, - GNUNET_TIME_UNIT_FOREVER_REL); - break; - default: /* Fall through */ + shutdown_ctx->confirmed = GNUNET_YES; + GNUNET_CLIENT_receive (shutdown_ctx->sock, + &service_shutdown_handler, + shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL); + break; + default: /* Fall through */ #if DEBUG_ARM - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "Service shutdown refused!\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown refused!\n"); #endif - if (shutdown_ctx->cont != NULL) - shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_YES); - - GNUNET_SCHEDULER_cancel(shutdown_ctx->cancel_task); - GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); - GNUNET_free(shutdown_ctx); - break; - } + if (shutdown_ctx->cont != NULL) + shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_YES); + + GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task); + GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); + GNUNET_free (shutdown_ctx); + break; } + } } /** @@ -169,16 +166,18 @@ service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg) * @param cls closure * @param tc context information (why was this task triggered now) */ -void service_shutdown_cancel (void *cls, - const struct GNUNET_SCHEDULER_TaskContext * tc) +void +service_shutdown_cancel (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) { struct ShutdownContext *shutdown_ctx = cls; + #if DEBUG_ARM - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "service_shutdown_cancel called!\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "service_shutdown_cancel called!\n"); #endif - shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR); + shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR); GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); - GNUNET_free(shutdown_ctx); + GNUNET_free (shutdown_ctx); } @@ -198,21 +197,22 @@ write_shutdown (void *cls, size_t size, void *buf) struct ShutdownContext *shutdown_ctx = cls; if (size < sizeof (struct GNUNET_MessageHeader)) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Failed to transmit shutdown request to client.\n")); - shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR); - GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); - GNUNET_free(shutdown_ctx); - return 0; /* client disconnected */ - } + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _("Failed to transmit shutdown request to client.\n")); + shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR); + GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); + GNUNET_free (shutdown_ctx); + return 0; /* client disconnected */ + } GNUNET_CLIENT_receive (shutdown_ctx->sock, - &service_shutdown_handler, shutdown_ctx, - GNUNET_TIME_UNIT_FOREVER_REL); - shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining(shutdown_ctx->timeout), - &service_shutdown_cancel, - shutdown_ctx); + &service_shutdown_handler, shutdown_ctx, + GNUNET_TIME_UNIT_FOREVER_REL); + shutdown_ctx->cancel_task = + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining + (shutdown_ctx->timeout), + &service_shutdown_cancel, shutdown_ctx); msg = (struct GNUNET_MessageHeader *) buf; msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN); msg->size = htons (sizeof (struct GNUNET_MessageHeader)); @@ -235,22 +235,22 @@ write_shutdown (void *cls, size_t size, void *buf) */ static void arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock, - struct GNUNET_TIME_Relative timeout, - GNUNET_CLIENT_ShutdownTask cont, - void *cont_cls) + struct GNUNET_TIME_Relative timeout, + GNUNET_CLIENT_ShutdownTask cont, void *cont_cls) { struct ShutdownContext *shutdown_ctx; - shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext)); + + shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext)); shutdown_ctx->cont = cont; shutdown_ctx->cont_cls = cont_cls; shutdown_ctx->sock = sock; - shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute(timeout); + shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout); GNUNET_CLIENT_notify_transmit_ready (sock, - sizeof (struct - GNUNET_MessageHeader), - timeout, - GNUNET_YES, - &write_shutdown, shutdown_ctx); + sizeof (struct + GNUNET_MessageHeader), + timeout, + GNUNET_YES, + &write_shutdown, shutdown_ctx); } @@ -266,7 +266,7 @@ arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock, */ struct GNUNET_ARM_Handle * GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, - const char *service) + const char *service) { struct GNUNET_ARM_Handle *ret; @@ -351,8 +351,7 @@ struct RequestContext * @param tc why were we called (reason says if ARM is running) */ static void -arm_service_report (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) +arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { struct RequestContext *pos = cls; struct GNUNET_OS_Process *proc; @@ -362,111 +361,91 @@ arm_service_report (void *cls, char *lopostfix; if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)) - { + { #if DEBUG_ARM - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Looks like `%s' is already running.\n", - "gnunet-service-arm"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Looks like `%s' is already running.\n", "gnunet-service-arm"); #endif - /* arm is running! */ - if (pos->callback != NULL) - pos->callback (pos->cls, GNUNET_YES); - GNUNET_free (pos); - return; - } + /* arm is running! */ + if (pos->callback != NULL) + pos->callback (pos->cls, GNUNET_YES); + GNUNET_free (pos); + return; + } #if DEBUG_ARM GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Looks like `%s' is not running, will start it.\n", - "gnunet-service-arm"); + "Looks like `%s' is not running, will start it.\n", + "gnunet-service-arm"); #endif - if (GNUNET_OK != + if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, - "arm", "PREFIX", &loprefix)) + "arm", "PREFIX", &loprefix)) loprefix = GNUNET_strdup (""); if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, - "arm", "OPTIONS", &lopostfix)) + "arm", "OPTIONS", &lopostfix)) lopostfix = GNUNET_strdup (""); if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, - "arm", - "BINARY", - &binary)) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Configuration failes to specify option `%s' in section `%s'!\n"), - "BINARY", - "arm"); - if (pos->callback != NULL) - pos->callback (pos->cls, GNUNET_SYSERR); - GNUNET_free (pos); - GNUNET_free (loprefix); - GNUNET_free (lopostfix); - return; - } + "arm", "BINARY", &binary)) + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _ + ("Configuration failes to specify option `%s' in section `%s'!\n"), + "BINARY", "arm"); + if (pos->callback != NULL) + pos->callback (pos->cls, GNUNET_SYSERR); + GNUNET_free (pos); + GNUNET_free (loprefix); + GNUNET_free (lopostfix); + return; + } if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg, - "arm", "CONFIG", &config)) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Configuration fails to specify option `%s' in section `%s'!\n"), - "CONFIG", - "arm"); - if (pos->callback != NULL) - pos->callback (pos->cls, GNUNET_SYSERR); - GNUNET_free (binary); - GNUNET_free (pos); - GNUNET_free (loprefix); - GNUNET_free (lopostfix); - return; - } - if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (pos->h->cfg, - "TESTING", - "WEAKRANDOM")) && - (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pos->h->cfg, - "TESTING", - "WEAKRANDOM")) && - (GNUNET_NO == GNUNET_CONFIGURATION_have_value (pos->h->cfg, - "TESTING", - "HOSTFILE")) /* Means we are ONLY running locally */ - ) - { - /* we're clearly running a test, don't daemonize */ - proc = do_start_process (NULL, - loprefix, - binary, - "-c", config, + "arm", "CONFIG", &config)) + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _ + ("Configuration fails to specify option `%s' in section `%s'!\n"), + "CONFIG", "arm"); + if (pos->callback != NULL) + pos->callback (pos->cls, GNUNET_SYSERR); + GNUNET_free (binary); + GNUNET_free (pos); + GNUNET_free (loprefix); + GNUNET_free (lopostfix); + return; + } + if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING", "WEAKRANDOM")) && (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (pos->h->cfg, "TESTING", "WEAKRANDOM")) && (GNUNET_NO == GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING", "HOSTFILE")) /* Means we are ONLY running locally */ + ) + { + /* we're clearly running a test, don't daemonize */ + proc = do_start_process (NULL, loprefix, binary, "-c", config, #if DEBUG_ARM - "-L", "DEBUG", + "-L", "DEBUG", #endif - /* no daemonization! */ - lopostfix, - NULL); - } + /* no daemonization! */ + lopostfix, NULL); + } else - { - proc = do_start_process (NULL, - loprefix, - binary, - "-c", config, + { + proc = do_start_process (NULL, loprefix, binary, "-c", config, #if DEBUG_ARM - "-L", "DEBUG", + "-L", "DEBUG", #endif - "-d", - lopostfix, - NULL); - } + "-d", lopostfix, NULL); + } GNUNET_free (binary); GNUNET_free (config); GNUNET_free (loprefix); GNUNET_free (lopostfix); if (proc == NULL) - { - if (pos->callback != NULL) - pos->callback (pos->cls, GNUNET_SYSERR); - GNUNET_free (pos); - return; - } + { + if (pos->callback != NULL) + pos->callback (pos->cls, GNUNET_SYSERR); + GNUNET_free (pos); + return; + } if (pos->callback != NULL) pos->callback (pos->cls, GNUNET_YES); GNUNET_free (proc); @@ -488,44 +467,41 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg) int ret; if (msg == NULL) - { - GNUNET_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_CLIENT_disconnect (sc->h->client, GNUNET_NO); - sc->h->client = GNUNET_CLIENT_connect ("arm", - sc->h->cfg); - GNUNET_assert (NULL != sc->h->client); - GNUNET_CLIENT_ignore_shutdown (sc->h->client, GNUNET_YES); - if (sc->callback != NULL) - sc->callback (sc->cls, GNUNET_SYSERR); - GNUNET_free (sc); - return; - } + { + GNUNET_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_CLIENT_disconnect (sc->h->client, GNUNET_NO); + sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg); + GNUNET_assert (NULL != sc->h->client); + GNUNET_CLIENT_ignore_shutdown (sc->h->client, GNUNET_YES); + if (sc->callback != NULL) + sc->callback (sc->cls, GNUNET_SYSERR); + GNUNET_free (sc); + return; + } #if DEBUG_ARM GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received response from ARM for service `%s': %u\n", - (const char*) &sc[1], - ntohs(msg->type)); + (const char *) &sc[1], ntohs (msg->type)); #endif switch (ntohs (msg->type)) - { - case GNUNET_MESSAGE_TYPE_ARM_IS_UP: - ret = GNUNET_YES; - break; - case GNUNET_MESSAGE_TYPE_ARM_IS_DOWN: - ret = GNUNET_NO; - break; - case GNUNET_MESSAGE_TYPE_ARM_IS_UNKNOWN: - ret = GNUNET_SYSERR; - break; - default: - GNUNET_break (0); - ret = GNUNET_SYSERR; - } + { + case GNUNET_MESSAGE_TYPE_ARM_IS_UP: + ret = GNUNET_YES; + break; + case GNUNET_MESSAGE_TYPE_ARM_IS_DOWN: + ret = GNUNET_NO; + break; + case GNUNET_MESSAGE_TYPE_ARM_IS_UNKNOWN: + ret = GNUNET_SYSERR; + break; + default: + GNUNET_break (0); + ret = GNUNET_SYSERR; + } if (sc->callback != NULL) sc->callback (sc->cls, ret); GNUNET_free (sc); @@ -544,7 +520,7 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg) */ static void change_service (struct GNUNET_ARM_Handle *h, - const char *service_name, + const char *service_name, struct GNUNET_TIME_Relative timeout, GNUNET_ARM_Callback cb, void *cb_cls, uint16_t type) { @@ -553,19 +529,19 @@ change_service (struct GNUNET_ARM_Handle *h, struct GNUNET_MessageHeader *msg; slen = strlen (service_name) + 1; - if (slen + sizeof (struct GNUNET_MessageHeader) >= GNUNET_SERVER_MAX_MESSAGE_SIZE) - { - GNUNET_break (0); - if (cb != NULL) - cb (cb_cls, GNUNET_NO); - return; - } + if (slen + sizeof (struct GNUNET_MessageHeader) >= + GNUNET_SERVER_MAX_MESSAGE_SIZE) + { + GNUNET_break (0); + if (cb != NULL) + cb (cb_cls, GNUNET_NO); + return; + } #if DEBUG_ARM GNUNET_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); #endif sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen); sctx->h = h; @@ -580,23 +556,26 @@ change_service (struct GNUNET_ARM_Handle *h, memcpy (&msg[1], service_name, slen); if (GNUNET_OK != GNUNET_CLIENT_transmit_and_get_response (sctx->h->client, - msg, - GNUNET_TIME_absolute_get_remaining (sctx->timeout), - GNUNET_YES, - &handle_response, - sctx)) - { - GNUNET_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); - if (cb != NULL) - cb (cb_cls, GNUNET_SYSERR); - GNUNET_free (sctx); - GNUNET_free (msg); - return; - } + msg, + GNUNET_TIME_absolute_get_remaining + (sctx->timeout), GNUNET_YES, + &handle_response, sctx)) + { + GNUNET_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); + if (cb != NULL) + cb (cb_cls, GNUNET_SYSERR); + GNUNET_free (sctx); + GNUNET_free (msg); + return; + } GNUNET_free (msg); } @@ -612,7 +591,7 @@ change_service (struct GNUNET_ARM_Handle *h, */ void GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h, - const char *service_name, + const char *service_name, struct GNUNET_TIME_Relative timeout, GNUNET_ARM_Callback cb, void *cb_cls) { @@ -623,39 +602,42 @@ GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h, #if DEBUG_ARM GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Asked to start service `%s' within %llu ms\n"), service_name, - (unsigned long long) timeout.rel_value); + (unsigned long long) timeout.rel_value); #endif GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Asked to start service `%s' within %llu ms\n"), service_name, (unsigned long long) timeout.rel_value); if (0 == strcasecmp ("arm", service_name)) - { - slen = strlen ("arm") + 1; - sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen); - sctx->h = h; - sctx->callback = cb; - sctx->cls = cb_cls; - sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout); - memcpy (&sctx[1], service_name, slen); - GNUNET_CLIENT_service_test ("arm", - h->cfg, timeout, &arm_service_report, sctx); - return; - } + { + slen = strlen ("arm") + 1; + sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen); + sctx->h = h; + sctx->callback = cb; + sctx->cls = cb_cls; + sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout); + memcpy (&sctx[1], service_name, slen); + GNUNET_CLIENT_service_test ("arm", + h->cfg, timeout, &arm_service_report, sctx); + return; + } if (h->client == NULL) + { + client = GNUNET_CLIENT_connect ("arm", h->cfg); + if (client == NULL) { - client = GNUNET_CLIENT_connect ("arm", h->cfg); - if (client == NULL) - { - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "arm_api, GNUNET_CLIENT_connect returned NULL\n"); - cb (cb_cls, GNUNET_SYSERR); - return; - } - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "arm_api, GNUNET_CLIENT_connect returned non-NULL\n"); - GNUNET_CLIENT_ignore_shutdown (client, GNUNET_YES); - h->client = client; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "arm_api, GNUNET_CLIENT_connect returned NULL\n"); + cb (cb_cls, GNUNET_SYSERR); + return; } - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "arm_api, h->client non-NULL\n"); - change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_START); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "arm_api, GNUNET_CLIENT_connect returned non-NULL\n"); + GNUNET_CLIENT_ignore_shutdown (client, GNUNET_YES); + h->client = client; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "arm_api, h->client non-NULL\n"); + change_service (h, service_name, timeout, cb, cb_cls, + GNUNET_MESSAGE_TYPE_ARM_START); } /** @@ -666,15 +648,15 @@ GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h, * @param reason reason for callback, GNUNET_NO if arm is shutdown * GNUNET_YES if arm remains running, and GNUNET_SYSERR on error */ -void arm_shutdown_callback (void *cls, - int reason) +void +arm_shutdown_callback (void *cls, int reason) { struct ARM_ShutdownContext *arm_shutdown_ctx = cls; if (arm_shutdown_ctx->cb != NULL) arm_shutdown_ctx->cb (arm_shutdown_ctx->cb_cls, reason); - GNUNET_free(arm_shutdown_ctx); + GNUNET_free (arm_shutdown_ctx); } @@ -698,28 +680,30 @@ GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h, GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Stopping service `%s' within %llu ms\n"), service_name, - (unsigned long long) timeout.rel_value); + (unsigned long long) timeout.rel_value); if (h->client == NULL) + { + client = GNUNET_CLIENT_connect ("arm", h->cfg); + if (client == NULL) { - client = GNUNET_CLIENT_connect ("arm", h->cfg); - if (client == NULL) - { - cb (cb_cls, GNUNET_SYSERR); - return; - } - GNUNET_CLIENT_ignore_shutdown (client, GNUNET_YES); - h->client = client; - } - if (0 == strcasecmp ("arm", service_name)) - { - arm_shutdown_ctx = GNUNET_malloc(sizeof(struct ARM_ShutdownContext)); - arm_shutdown_ctx->cb = cb; - arm_shutdown_ctx->cb_cls = cb_cls; - arm_service_shutdown (h->client, timeout, &arm_shutdown_callback, arm_shutdown_ctx); - h->client = NULL; + cb (cb_cls, GNUNET_SYSERR); return; } - change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_STOP); + GNUNET_CLIENT_ignore_shutdown (client, GNUNET_YES); + h->client = client; + } + if (0 == strcasecmp ("arm", service_name)) + { + arm_shutdown_ctx = GNUNET_malloc (sizeof (struct ARM_ShutdownContext)); + arm_shutdown_ctx->cb = cb; + arm_shutdown_ctx->cb_cls = cb_cls; + arm_service_shutdown (h->client, timeout, &arm_shutdown_callback, + arm_shutdown_ctx); + h->client = NULL; + return; + } + change_service (h, service_name, timeout, cb, cb_cls, + GNUNET_MESSAGE_TYPE_ARM_STOP); } diff --git a/src/arm/do_start_process.c b/src/arm/do_start_process.c index a2aca9dea..19b3bc224 100644 --- a/src/arm/do_start_process.c +++ b/src/arm/do_start_process.c @@ -13,8 +13,7 @@ * @return PID of the started process, -1 on error */ static struct GNUNET_OS_Process * -do_start_process (const int *lsocks, - const char *first_arg, ...) +do_start_process (const int *lsocks, const char *first_arg, ...) { va_list ap; char **argv; @@ -34,24 +33,24 @@ do_start_process (const int *lsocks, do { /* *INDENT-ON* */ - rpos = arg; - while ('\0' != *rpos) - { - if (' ' == *rpos) - { - if (last != NULL) - argv_size++; - last = NULL; - while (' ' == *rpos) - rpos++; - } - if ( (last == NULL) && (*rpos != '\0') ) - last = rpos; - if (*rpos != '\0') - rpos++; - } + rpos = arg; + while ('\0' != *rpos) + { + if (' ' == *rpos) + { if (last != NULL) - argv_size++; + argv_size++; + last = NULL; + while (' ' == *rpos) + rpos++; + } + if ((last == NULL) && (*rpos != '\0')) + last = rpos; + if (*rpos != '\0') + rpos++; + } + if (last != NULL) + argv_size++; /* *INDENT-OFF* */ } while (NULL != (arg = (va_arg (ap, const char*)))); @@ -67,29 +66,29 @@ do_start_process (const int *lsocks, do { /* *INDENT-ON* */ - cp = GNUNET_strdup (arg); - pos = cp; - while ('\0' != *pos) - { - if (' ' == *pos) - { - *pos = '\0'; - if (last != NULL) - argv[argv_size++] = GNUNET_strdup (last); - last = NULL; - pos++; - while (' ' == *pos) - pos++; - } - if ( (last == NULL) && (*pos != '\0') ) - last = pos; - if (*pos != '\0') - pos++; - } + cp = GNUNET_strdup (arg); + pos = cp; + while ('\0' != *pos) + { + if (' ' == *pos) + { + *pos = '\0'; if (last != NULL) - argv[argv_size++] = GNUNET_strdup (last); + argv[argv_size++] = GNUNET_strdup (last); last = NULL; - GNUNET_free (cp); + pos++; + while (' ' == *pos) + pos++; + } + if ((last == NULL) && (*pos != '\0')) + last = pos; + if (*pos != '\0') + pos++; + } + if (last != NULL) + argv[argv_size++] = GNUNET_strdup (last); + last = NULL; + GNUNET_free (cp); /* *INDENT-OFF* */ } while (NULL != (arg = (va_arg (ap, const char*)))); diff --git a/src/arm/gnunet-arm.c b/src/arm/gnunet-arm.c index ccad0f21f..f7ff536be 100644 --- a/src/arm/gnunet-arm.c +++ b/src/arm/gnunet-arm.c @@ -138,9 +138,7 @@ static struct GNUNET_TIME_Relative timeout; * @param cls closure, unused * @param tc context, unused */ -static void -cps_loop (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc); +static void cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); /** @@ -154,39 +152,39 @@ static void confirm_cb (void *cls, int success) { const char *service = cls; + switch (success) + { + case GNUNET_OK: + if (quiet != GNUNET_YES) + fprintf (stdout, _("Service `%s' has been started.\n"), service); + if ((phase - 1 != 2) && (phase - 1 != 3)) { - case GNUNET_OK: - if (quiet != GNUNET_YES) - fprintf(stdout, _("Service `%s' has been started.\n"), service); - if ((phase - 1 != 2) && (phase - 1 != 3)) - { - if (quiet != GNUNET_YES) - fprintf(stdout, _("Failed to stop service `%s'!\n"), service); - ret = 1; - } - break; - case GNUNET_NO: if (quiet != GNUNET_YES) - fprintf(stdout, _("Service `%s' has been stopped.\n"), service); - if ((phase - 1 != 0) && (phase - 1 != 1)) - { - if (quiet != GNUNET_YES) - fprintf(stdout, _("Failed to start service `%s'!\n"), service); - ret = 1; - } - break; - case GNUNET_SYSERR: + fprintf (stdout, _("Failed to stop service `%s'!\n"), service); + ret = 1; + } + break; + case GNUNET_NO: + if (quiet != GNUNET_YES) + fprintf (stdout, _("Service `%s' has been stopped.\n"), service); + if ((phase - 1 != 0) && (phase - 1 != 1)) + { if (quiet != GNUNET_YES) - fprintf(stdout, - _("Some error communicating with service `%s'.\n"), service); + fprintf (stdout, _("Failed to start service `%s'!\n"), service); ret = 1; - break; } + break; + case GNUNET_SYSERR: + if (quiet != GNUNET_YES) + fprintf (stdout, + _("Some error communicating with service `%s'.\n"), service); + ret = 1; + break; + } GNUNET_SCHEDULER_add_continuation (&cps_loop, - NULL, - GNUNET_SCHEDULER_REASON_PREREQ_DONE); + NULL, GNUNET_SCHEDULER_REASON_PREREQ_DONE); } @@ -203,18 +201,17 @@ confirm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) const char *service = cls; if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)) - { - if (quiet != GNUNET_YES) - fprintf(stdout, _("Service `%s' is running.\n"), service); - } + { + if (quiet != GNUNET_YES) + fprintf (stdout, _("Service `%s' is running.\n"), service); + } else - { - if (quiet != GNUNET_YES) - fprintf(stdout, _("Service `%s' is not running.\n"), service); - } + { + if (quiet != GNUNET_YES) + fprintf (stdout, _("Service `%s' is not running.\n"), service); + } GNUNET_SCHEDULER_add_continuation (&cps_loop, - NULL, - GNUNET_SCHEDULER_REASON_PREREQ_DONE); + NULL, GNUNET_SCHEDULER_REASON_PREREQ_DONE); } @@ -229,30 +226,29 @@ confirm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) static void run (void *cls, char *const *args, - const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *c) + const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c) { cfg = c; config_file = cfgfile; - if (GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS", "SERVICEHOME", &dir) != GNUNET_OK) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Fatal configuration error: `%s' option in section `%s' missing.\n"), - "SERVICEHOME", - "PATHS"); - return; - } + if (GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME", &dir) + != GNUNET_OK) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _ + ("Fatal configuration error: `%s' option in section `%s' missing.\n"), + "SERVICEHOME", "PATHS"); + return; + } h = GNUNET_ARM_connect (cfg, NULL); if (h == NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Fatal error initializing ARM API.\n")); - ret = 1; - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Fatal error initializing ARM API.\n")); + ret = 1; + return; + } GNUNET_SCHEDULER_add_continuation (&cps_loop, - NULL, - GNUNET_SCHEDULER_REASON_PREREQ_DONE); + NULL, GNUNET_SCHEDULER_REASON_PREREQ_DONE); } /** @@ -260,20 +256,23 @@ run (void *cls, * on arm shutdown provided the end and delete options * were specified when gnunet-arm was run. */ -static void delete_files() +static void +delete_files () { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will attempt to remove configuration file %s and service directory %s\n", config_file, dir); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Will attempt to remove configuration file %s and service directory %s\n", + config_file, dir); - if (UNLINK(config_file) != 0) + if (UNLINK (config_file) != 0) { GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Failed to remove configuration file %s\n"), config_file); + _("Failed to remove configuration file %s\n"), config_file); } - if (GNUNET_DISK_directory_remove(dir) != GNUNET_OK) + if (GNUNET_DISK_directory_remove (dir) != GNUNET_OK) { GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Failed to remove servicehome directory %s\n"), dir); + _("Failed to remove servicehome directory %s\n"), dir); } } @@ -286,75 +285,89 @@ static void delete_files() * @param tc context, unused */ static void -cps_loop (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) +cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { while (1) + { + switch (phase++) { - switch (phase++) - { - case 0: - if (term != NULL) - { - GNUNET_ARM_stop_service (h, term, (0 == timeout.rel_value) ? STOP_TIMEOUT : timeout, &confirm_cb, term); - return; - } - break; - case 1: - if ((end) || (restart)) - { - GNUNET_ARM_stop_service (h, "arm", (0 == timeout.rel_value) ? STOP_TIMEOUT_ARM : timeout, &confirm_cb, "arm"); - return; - } - break; - case 2: - if (start) - { - GNUNET_ARM_start_service (h, "arm", (0 == timeout.rel_value) ? START_TIMEOUT : timeout, &confirm_cb, "arm"); - return; - } - break; - case 3: - if (init != NULL) - { - GNUNET_ARM_start_service (h, init, (0 == timeout.rel_value) ? START_TIMEOUT : timeout, &confirm_cb, init); - return; - } - break; - case 4: - if (test != NULL) - { - GNUNET_CLIENT_service_test (test, cfg, (0 == timeout.rel_value) ? TEST_TIMEOUT : timeout, &confirm_task, test); - return; - } - break; - case 5: - if (restart) - { - GNUNET_ARM_disconnect (h); - phase = 0; - end = 0; - start = 1; - restart = 0; - h = GNUNET_ARM_connect (cfg, NULL); - if (h == NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Fatal error initializing ARM API.\n")); - ret = 1; - return; - } - GNUNET_SCHEDULER_add_now(&cps_loop, NULL); - return; - } - /* Fall through */ - default: /* last phase */ - GNUNET_ARM_disconnect (h); - if ((end == GNUNET_YES) && (delete == GNUNET_YES)) - delete_files(); - return; - } + case 0: + if (term != NULL) + { + GNUNET_ARM_stop_service (h, term, + (0 == + timeout.rel_value) ? STOP_TIMEOUT : timeout, + &confirm_cb, term); + return; + } + break; + case 1: + if ((end) || (restart)) + { + GNUNET_ARM_stop_service (h, "arm", + (0 == + timeout.rel_value) ? STOP_TIMEOUT_ARM : + timeout, &confirm_cb, "arm"); + return; + } + break; + case 2: + if (start) + { + GNUNET_ARM_start_service (h, "arm", + (0 == + timeout.rel_value) ? START_TIMEOUT : timeout, + &confirm_cb, "arm"); + return; + } + break; + case 3: + if (init != NULL) + { + GNUNET_ARM_start_service (h, init, + (0 == + timeout.rel_value) ? START_TIMEOUT : timeout, + &confirm_cb, init); + return; + } + break; + case 4: + if (test != NULL) + { + GNUNET_CLIENT_service_test (test, cfg, + (0 == + timeout.rel_value) ? TEST_TIMEOUT : + timeout, &confirm_task, test); + return; + } + break; + case 5: + if (restart) + { + GNUNET_ARM_disconnect (h); + phase = 0; + end = 0; + start = 1; + restart = 0; + h = GNUNET_ARM_connect (cfg, NULL); + if (h == NULL) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Fatal error initializing ARM API.\n")); + ret = 1; + return; + } + GNUNET_SCHEDULER_add_now (&cps_loop, NULL); + return; + } + /* Fall through */ + default: /* last phase */ + GNUNET_ARM_disconnect (h); + if ((end == GNUNET_YES) && (delete == GNUNET_YES)) + delete_files (); + return; } + } } @@ -379,16 +392,19 @@ main (int argc, char *const *argv) GNUNET_YES, &GNUNET_GETOPT_set_string, &term}, {'s', "start", NULL, gettext_noop ("start all GNUnet default services"), GNUNET_NO, &GNUNET_GETOPT_set_one, &start}, - {'r', "restart", NULL, gettext_noop ("stop and start all GNUnet default services"), + {'r', "restart", NULL, + gettext_noop ("stop and start all GNUnet default services"), GNUNET_NO, &GNUNET_GETOPT_set_one, &restart}, {'t', "test", "SERVICE", gettext_noop ("test if a particular service is running"), GNUNET_YES, &GNUNET_GETOPT_set_string, &test}, - {'d', "delete", NULL, gettext_noop ("delete config file and directory on exit"), + {'d', "delete", NULL, + gettext_noop ("delete config file and directory on exit"), GNUNET_NO, &GNUNET_GETOPT_set_one, &delete}, {'q', "quiet", NULL, gettext_noop ("don't print status messages"), GNUNET_NO, &GNUNET_GETOPT_set_one, &quiet}, - {'T', "timeout", NULL, gettext_noop ("timeout for completing current operation"), + {'T', "timeout", NULL, + gettext_noop ("timeout for completing current operation"), GNUNET_YES, &GNUNET_GETOPT_set_ulong, &temp_timeout_ms}, GNUNET_GETOPT_OPTION_END }; @@ -397,16 +413,16 @@ main (int argc, char *const *argv) timeout.rel_value = temp_timeout_ms; if (GNUNET_OK == GNUNET_PROGRAM_run (argc, - argv, - "gnunet-arm", - gettext_noop - ("Control services and the Automated Restart Manager (ARM)"), - options, &run, NULL)) - { - return ret; - } + argv, + "gnunet-arm", + gettext_noop + ("Control services and the Automated Restart Manager (ARM)"), + options, &run, NULL)) + { + return ret; + } - return 1; + return 1; } /* end of gnunet-arm.c */ diff --git a/src/arm/gnunet-service-arm.c b/src/arm/gnunet-service-arm.c index 05c6fb438..e4a8e7c6c 100644 --- a/src/arm/gnunet-service-arm.c +++ b/src/arm/gnunet-service-arm.c @@ -188,7 +188,7 @@ static struct GNUNET_SERVER_Handle *server; * @param cls closure, NULL if we need to self-restart * @param tc context */ -static void +static void config_change_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { struct ServiceList *pos; @@ -196,21 +196,21 @@ config_change_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) pos = running_head; while (pos != NULL) + { + /* FIXME: this test for config change may be a bit too coarse grained */ + if ((0 == STAT (pos->config, &sbuf)) && + (pos->mtime < sbuf.st_mtime) && (pos->proc != NULL)) { - /* FIXME: this test for config change may be a bit too coarse grained */ - if ( (0 == STAT (pos->config, &sbuf)) && - (pos->mtime < sbuf.st_mtime) && - (pos->proc != NULL) ) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Restarting service `%s' due to configuration file change.\n")); - if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM)) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); - else - pos->backoff = GNUNET_TIME_UNIT_MILLISECONDS; - } - pos = pos->next; + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _ + ("Restarting service `%s' due to configuration file change.\n")); + if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM)) + GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); + else + pos->backoff = GNUNET_TIME_UNIT_MILLISECONDS; } + pos = pos->next; + } } @@ -229,15 +229,14 @@ write_result (void *cls, size_t size, void *buf) struct GNUNET_MessageHeader *msg; if (buf == NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Could not send status result to client\n")); - return 0; /* error, not much we can do */ - } + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _("Could not send status result to client\n")); + return 0; /* error, not much we can do */ + } #if DEBUG_ARM GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Sending status response %u to client\n", - (unsigned int) *res); + "Sending status response %u to client\n", (unsigned int) *res); #endif GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader)); msg = buf; @@ -259,29 +258,27 @@ write_result (void *cls, size_t size, void *buf) */ static void signal_result (struct GNUNET_SERVER_Client *client, - const char *name, uint16_t result) + const char *name, uint16_t result) { uint16_t *res; if (NULL == client) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _ - ("Not sending status result to client: no client known\n")); - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("Not sending status result to client: no client known\n")); + return; + } #if DEBUG_ARM GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Telling client that service `%s' is now %s\n", - name, - result == GNUNET_MESSAGE_TYPE_ARM_IS_DOWN ? "down" : "up"); + "Telling client that service `%s' is now %s\n", + name, result == GNUNET_MESSAGE_TYPE_ARM_IS_DOWN ? "down" : "up"); #endif res = GNUNET_malloc (sizeof (uint16_t)); *res = result; GNUNET_SERVER_notify_transmit_ready (client, - sizeof (struct GNUNET_MessageHeader), - GNUNET_TIME_UNIT_FOREVER_REL, - &write_result, res); + sizeof (struct GNUNET_MessageHeader), + GNUNET_TIME_UNIT_FOREVER_REL, + &write_result, res); } @@ -299,11 +296,11 @@ find_service (const char *name) pos = running_head; while (pos != NULL) - { - if (0 == strcmp (pos->name, name)) - return pos; - pos = pos->next; - } + { + if (0 == strcmp (pos->name, name)) + return pos; + pos = pos->next; + } return NULL; } @@ -316,9 +313,7 @@ find_service (const char *name) static void free_service (struct ServiceList *pos) { - GNUNET_CONTAINER_DLL_remove (running_head, - running_tail, - pos); + GNUNET_CONTAINER_DLL_remove (running_head, running_tail, pos); GNUNET_free_non_null (pos->config); GNUNET_free_non_null (pos->binary); GNUNET_free (pos->name); @@ -336,8 +331,7 @@ free_service (struct ServiceList *pos) * @param lsocks -1 terminated list of listen sockets to pass (systemd style), or NULL */ static void -start_process (struct ServiceList *sl, - const int *lsocks) +start_process (struct ServiceList *sl, const int *lsocks) { char *loprefix; char *options; @@ -351,85 +345,70 @@ start_process (struct ServiceList *sl, /* start service */ if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, - sl->name, "PREFIX", &loprefix)) + sl->name, "PREFIX", &loprefix)) loprefix = GNUNET_strdup (prefix_command); if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, - sl->name, "OPTIONS", &options)) - { - options = GNUNET_strdup (final_option); - if (NULL == strstr (options, "%")) - { - /* replace '{}' with service name */ - while (NULL != (optpos = strstr (options, "{}"))) - { - optpos[0] = '%'; - optpos[1] = 's'; - GNUNET_asprintf (&optpos, - options, - sl->name); - GNUNET_free (options); - options = optpos; - } - /* replace '$PATH' with value associated with "PATH" */ - while (NULL != (optpos = strstr (options, "$"))) - { - optend = optpos + 1; - while (isupper ( (unsigned char) *optend)) optend++; - b = *optend; - if ('\0' == b) - next = ""; - else - next = optend+1; - *optend = '\0'; - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", - optpos+1, - &val)) - val = GNUNET_strdup (""); - *optpos = '\0'; - GNUNET_asprintf (&optpos, - "%s%s%c%s", - options, - val, - b, - next); - GNUNET_free (options); - GNUNET_free (val); - options = optpos; - } - } + sl->name, "OPTIONS", &options)) + { + options = GNUNET_strdup (final_option); + if (NULL == strstr (options, "%")) + { + /* replace '{}' with service name */ + while (NULL != (optpos = strstr (options, "{}"))) + { + optpos[0] = '%'; + optpos[1] = 's'; + GNUNET_asprintf (&optpos, options, sl->name); + GNUNET_free (options); + options = optpos; + } + /* replace '$PATH' with value associated with "PATH" */ + while (NULL != (optpos = strstr (options, "$"))) + { + optend = optpos + 1; + while (isupper ((unsigned char) *optend)) + optend++; + b = *optend; + if ('\0' == b) + next = ""; + else + next = optend + 1; + *optend = '\0'; + if (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", + optpos + 1, &val)) + val = GNUNET_strdup (""); + *optpos = '\0'; + GNUNET_asprintf (&optpos, "%s%s%c%s", options, val, b, next); + GNUNET_free (options); + GNUNET_free (val); + options = optpos; + } } + } use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name, "DEBUG"); #if DEBUG_ARM GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Starting service `%s' using binary `%s' and configuration `%s'\n", - sl->name, sl->binary, sl->config); + "Starting service `%s' using binary `%s' and configuration `%s'\n", + sl->name, sl->binary, sl->config); #endif if (GNUNET_YES == use_debug) sl->proc = do_start_process (lsocks, - loprefix, - sl->binary, - "-c", sl->config, - "-L", "DEBUG", - options, - NULL); + loprefix, + sl->binary, + "-c", sl->config, + "-L", "DEBUG", options, NULL); else sl->proc = do_start_process (lsocks, - loprefix, - sl->binary, - "-c", sl->config, - options, - NULL); + loprefix, + sl->binary, "-c", sl->config, options, NULL); if (sl->proc == NULL) - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Failed to start service `%s'\n"), - sl->name); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Failed to start service `%s'\n"), sl->name); else - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Starting service `%s'\n"), - sl->name); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting service `%s'\n"), sl->name); GNUNET_free (loprefix); GNUNET_free (options); } @@ -444,9 +423,8 @@ start_process (struct ServiceList *sl, * @return GNUNET_OK on success, GNUNET_SYSERR on error */ int -start_service (struct GNUNET_SERVER_Client *client, - const char *servicename, - const int *lsocks) +start_service (struct GNUNET_SERVER_Client *client, + const char *servicename, const int *lsocks) { struct ServiceList *sl; char *binary; @@ -454,46 +432,46 @@ start_service (struct GNUNET_SERVER_Client *client, struct stat sbuf; if (GNUNET_YES == in_shutdown) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("ARM is shutting down, service `%s' not started.\n"), - servicename); - signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN); - return GNUNET_SYSERR; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("ARM is shutting down, service `%s' not started.\n"), + servicename); + signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN); + return GNUNET_SYSERR; + } sl = find_service (servicename); if (sl != NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - _("Service `%s' already running.\n"), servicename); - signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_UP); - return GNUNET_SYSERR; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + _("Service `%s' already running.\n"), servicename); + signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_UP); + return GNUNET_SYSERR; + } if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, - servicename, "BINARY", &binary)) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Binary implementing service `%s' not known!\n"), - servicename); - signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN); - return GNUNET_SYSERR; - } + servicename, "BINARY", &binary)) + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _("Binary implementing service `%s' not known!\n"), + servicename); + signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN); + return GNUNET_SYSERR; + } if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, - servicename, - "CONFIG", - &config)) || + servicename, + "CONFIG", + &config)) || (0 != STAT (config, &sbuf))) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Configuration file `%s' for service `%s' not known!\n"), - config, servicename); - signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN); - GNUNET_free (binary); - GNUNET_free_non_null (config); - return GNUNET_SYSERR; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _("Configuration file `%s' for service `%s' not known!\n"), + config, servicename); + signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN); + GNUNET_free (binary); + GNUNET_free_non_null (config); + return GNUNET_SYSERR; + } (void) stop_listening (servicename); sl = GNUNET_malloc (sizeof (struct ServiceList)); sl->name = GNUNET_strdup (servicename); @@ -502,9 +480,7 @@ start_service (struct GNUNET_SERVER_Client *client, sl->mtime = sbuf.st_mtime; sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS; sl->restartAt = GNUNET_TIME_UNIT_FOREVER_ABS; - GNUNET_CONTAINER_DLL_insert (running_head, - running_tail, - sl); + GNUNET_CONTAINER_DLL_insert (running_head, running_tail, sl); start_process (sl, lsocks); if (NULL != client) signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_UP); @@ -519,58 +495,57 @@ start_service (struct GNUNET_SERVER_Client *client, * @param servicename name of the service to stop */ static void -stop_service (struct GNUNET_SERVER_Client *client, - const char *servicename) +stop_service (struct GNUNET_SERVER_Client *client, const char *servicename) { struct ServiceList *pos; GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Preparing to stop `%s'\n"), servicename); + _("Preparing to stop `%s'\n"), servicename); pos = find_service (servicename); if (pos == NULL) - { - if (GNUNET_OK == stop_listening (servicename)) - signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN); - else - signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_UNKNOWN); - GNUNET_SERVER_receive_done (client, GNUNET_OK); - return; - } + { + if (GNUNET_OK == stop_listening (servicename)) + signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN); + else + signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_UNKNOWN); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } if (pos->killing_client != NULL) - { - /* killing already in progress */ + { + /* killing already in progress */ #if DEBUG_ARM - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Service `%s' is already down\n", servicename); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Service `%s' is already down\n", servicename); #endif - signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN); - GNUNET_SERVER_receive_done (client, GNUNET_OK); - return; - } + signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } if (GNUNET_YES == in_shutdown) - { + { #if DEBUG_ARM - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Termination request already sent to `%s' (since ARM is in shutdown).\n", - servicename); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Termination request already sent to `%s' (since ARM is in shutdown).\n", + servicename); #endif - signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN); - GNUNET_SERVER_receive_done (client, GNUNET_OK); - return; - } + signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } if (pos->proc == NULL) - { - /* process is in delayed restart, simply remove it! */ - free_service (pos); - signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN); - GNUNET_SERVER_receive_done (client, GNUNET_OK); - return; - } + { + /* process is in delayed restart, simply remove it! */ + free_service (pos); + signal_result (client, servicename, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } #if DEBUG_ARM GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Sending kill signal to service `%s', waiting for process to die.\n", - servicename); + "Sending kill signal to service `%s', waiting for process to die.\n", + servicename); #endif if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM)) GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); @@ -590,8 +565,8 @@ stop_service (struct GNUNET_SERVER_Client *client, */ static void handle_start (void *cls, - struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) + struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) { const char *servicename; uint16_t size; @@ -600,11 +575,11 @@ handle_start (void *cls, size -= sizeof (struct GNUNET_MessageHeader); servicename = (const char *) &message[1]; if ((size == 0) || (servicename[size - 1] != '\0')) - { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; - } + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } start_service (client, servicename, NULL); GNUNET_SERVER_receive_done (client, GNUNET_OK); } @@ -621,8 +596,8 @@ handle_start (void *cls, */ static void handle_stop (void *cls, - struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) + struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) { const char *servicename; uint16_t size; @@ -631,11 +606,11 @@ handle_stop (void *cls, size -= sizeof (struct GNUNET_MessageHeader); servicename = (const char *) &message[1]; if ((size == 0) || (servicename[size - 1] != '\0')) - { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; - } + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } stop_service (client, servicename); } @@ -650,14 +625,14 @@ clean_up_running () { struct ServiceList *pos; struct ServiceList *next; - + next = running_head; while (NULL != (pos = next)) - { - next = pos->next; - if (pos->proc == NULL) - free_service (pos); - } + { + next = pos->next; + if (pos->proc == NULL) + free_service (pos); + } } @@ -669,15 +644,15 @@ static void do_shutdown () { if (NULL != server) - { - GNUNET_SERVER_destroy (server); - server = NULL; - } + { + GNUNET_SERVER_destroy (server); + server = NULL; + } if (GNUNET_SCHEDULER_NO_TASK != child_death_task) - { - GNUNET_SCHEDULER_cancel (child_death_task); - child_death_task = GNUNET_SCHEDULER_NO_TASK; - } + { + GNUNET_SCHEDULER_cancel (child_death_task); + child_death_task = GNUNET_SCHEDULER_NO_TASK; + } } @@ -688,35 +663,31 @@ do_shutdown () * @param tc context */ static void -shutdown_task (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) +shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { struct ServiceList *pos; #if DEBUG_ARM - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - _("Stopping all services\n")); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Stopping all services\n")); #endif if (GNUNET_SCHEDULER_NO_TASK != child_restart_task) - { - GNUNET_SCHEDULER_cancel (child_restart_task); - child_restart_task = GNUNET_SCHEDULER_NO_TASK; - } + { + GNUNET_SCHEDULER_cancel (child_restart_task); + child_restart_task = GNUNET_SCHEDULER_NO_TASK; + } in_shutdown = GNUNET_YES; stop_listening (NULL); pos = running_head; while (NULL != pos) + { + if (pos->proc != NULL) { - if (pos->proc != NULL) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Stopping service `%s'\n", - pos->name); - if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM)) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); - } - pos = pos->next; + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping service `%s'\n", pos->name); + if (0 != GNUNET_OS_process_kill (pos->proc, SIGTERM)) + GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); } + pos = pos->next; + } if (running_head == NULL) do_shutdown (); } @@ -741,40 +712,39 @@ delayed_restart_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL; /* check for services that need to be restarted due to - configuration changes or because the last restart failed */ + * configuration changes or because the last restart failed */ pos = running_head; while (pos != NULL) + { + if (pos->proc == NULL) { - if (pos->proc == NULL) - { - if (GNUNET_TIME_absolute_get_remaining (pos->restartAt).rel_value == 0) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Restarting service `%s'.\n"), pos->name); - start_process (pos, NULL); - } - else - { - lowestRestartDelay - = GNUNET_TIME_relative_min (lowestRestartDelay, - GNUNET_TIME_absolute_get_remaining - (pos->restartAt)); - } - } - pos = pos->next; - } + if (GNUNET_TIME_absolute_get_remaining (pos->restartAt).rel_value == 0) + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("Restarting service `%s'.\n"), pos->name); + start_process (pos, NULL); + } + else + { + lowestRestartDelay + = GNUNET_TIME_relative_min (lowestRestartDelay, + GNUNET_TIME_absolute_get_remaining + (pos->restartAt)); + } + } + pos = pos->next; + } if (lowestRestartDelay.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value) - { + { #if DEBUG_ARM - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Will restart process in %llums\n", - (unsigned long long) lowestRestartDelay.rel_value); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Will restart process in %llums\n", + (unsigned long long) lowestRestartDelay.rel_value); #endif - child_restart_task - = GNUNET_SCHEDULER_add_delayed (lowestRestartDelay, - &delayed_restart_task, - NULL); - } + child_restart_task + = GNUNET_SCHEDULER_add_delayed (lowestRestartDelay, + &delayed_restart_task, NULL); + } } @@ -786,8 +756,7 @@ delayed_restart_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) * @param tc context */ static void -maint_child_death (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) +maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { struct ServiceList *pos; struct ServiceList *next; @@ -800,93 +769,89 @@ maint_child_death (void *cls, child_death_task = GNUNET_SCHEDULER_NO_TASK; if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) - { - /* shutdown scheduled us, ignore! */ - child_death_task = - GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, pr, - &maint_child_death, NULL); - return; - } + { + /* shutdown scheduled us, ignore! */ + child_death_task = + GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, pr, + &maint_child_death, NULL); + return; + } /* consume the signal */ GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c))); /* check for services that died (WAITPID) */ next = running_head; while (NULL != (pos = next)) + { + next = pos->next; + if (pos->proc == NULL) + continue; + if ((GNUNET_SYSERR == (ret = GNUNET_OS_process_status (pos->proc, + &statusType, + &statusCode))) || + ((ret == GNUNET_NO) || + (statusType == GNUNET_OS_PROCESS_STOPPED) || + (statusType == GNUNET_OS_PROCESS_RUNNING))) + continue; + + if (statusType == GNUNET_OS_PROCESS_EXITED) { - next = pos->next; - if (pos->proc == NULL) - continue; - if ((GNUNET_SYSERR == (ret = GNUNET_OS_process_status (pos->proc, - &statusType, - &statusCode))) || - ( (ret == GNUNET_NO) || - (statusType == GNUNET_OS_PROCESS_STOPPED) || - (statusType == GNUNET_OS_PROCESS_RUNNING)) ) - continue; - - if (statusType == GNUNET_OS_PROCESS_EXITED) - { - statstr = _( /* process termination method */ "exit"); - statcode = statusCode; - } - else if (statusType == GNUNET_OS_PROCESS_SIGNALED) - { - statstr = _( /* process termination method */ "signal"); - statcode = statusCode; - } - else - { - statstr = _( /* process termination method */ "unknown"); - statcode = 0; - } - GNUNET_OS_process_close (pos->proc); - pos->proc = NULL; - if (NULL != pos->killing_client) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Service `%s' stopped\n"), - pos->name); - signal_result (pos->killing_client, - pos->name, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN); - GNUNET_SERVER_receive_done (pos->killing_client, GNUNET_OK); - GNUNET_SERVER_client_drop (pos->killing_client); - free_service (pos); - continue; - } - if (GNUNET_YES != in_shutdown) - { - if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Service `%s' terminated with status %s/%d, will try to restart it!\n"), - pos->name, statstr, statcode); - /* schedule restart */ - pos->restartAt - = GNUNET_TIME_relative_to_absolute (pos->backoff); - if (pos->backoff.rel_value < EXPONENTIAL_BACKOFF_THRESHOLD) - pos->backoff - = GNUNET_TIME_relative_multiply (pos->backoff, 2); - 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); - } + statstr = _( /* process termination method */ "exit"); + statcode = statusCode; + } + else if (statusType == GNUNET_OS_PROCESS_SIGNALED) + { + statstr = _( /* process termination method */ "signal"); + statcode = statusCode; + } + else + { + statstr = _( /* process termination method */ "unknown"); + statcode = 0; + } + GNUNET_OS_process_close (pos->proc); + pos->proc = NULL; + if (NULL != pos->killing_client) + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("Service `%s' stopped\n"), pos->name); + signal_result (pos->killing_client, + pos->name, GNUNET_MESSAGE_TYPE_ARM_IS_DOWN); + GNUNET_SERVER_receive_done (pos->killing_client, GNUNET_OK); + GNUNET_SERVER_client_drop (pos->killing_client); + free_service (pos); + continue; + } + if (GNUNET_YES != in_shutdown) + { + if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _ + ("Service `%s' terminated with status %s/%d, will try to restart it!\n"), + pos->name, statstr, statcode); + /* schedule restart */ + pos->restartAt = GNUNET_TIME_relative_to_absolute (pos->backoff); + if (pos->backoff.rel_value < EXPONENTIAL_BACKOFF_THRESHOLD) + pos->backoff = GNUNET_TIME_relative_multiply (pos->backoff, 2); + 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); + } #if DEBUG_ARM - else - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Service `%s' terminated with status %s/%d\n", - pos->name, statstr, statcode); + else + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Service `%s' terminated with status %s/%d\n", + pos->name, statstr, statcode); #endif - } + } child_death_task = - GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, pr, - &maint_child_death, NULL); + GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, pr, + &maint_child_death, NULL); if (GNUNET_YES == in_shutdown) clean_up_running (); - if ( (NULL == running_head) && - (GNUNET_YES == in_shutdown) ) + if ((NULL == running_head) && (GNUNET_YES == in_shutdown)) do_shutdown (); } @@ -898,25 +863,24 @@ transmit_shutdown_ack (void *cls, size_t size, void *buf) struct GNUNET_MessageHeader *msg; if (size < sizeof (struct GNUNET_MessageHeader)) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Failed to transmit shutdown ACK.\n")); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return 0; /* client disconnected */ - } + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("Failed to transmit shutdown ACK.\n")); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return 0; /* client disconnected */ + } - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Transmitting shutdown ACK.\n")); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transmitting shutdown ACK.\n")); /* Make the connection flushing for the purpose of ACK transmitting, - needed on W32 to ensure that the message is even received, harmless - on other platforms... */ + * needed on W32 to ensure that the message is even received, harmless + * on other platforms... */ GNUNET_break (GNUNET_OK == GNUNET_SERVER_client_disable_corking (client)); msg = (struct GNUNET_MessageHeader *) buf; msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK); msg->size = htons (sizeof (struct GNUNET_MessageHeader)); GNUNET_SERVER_receive_done (client, GNUNET_OK); - GNUNET_SERVER_client_drop(client); + GNUNET_SERVER_client_drop (client); return sizeof (struct GNUNET_MessageHeader); } @@ -933,11 +897,11 @@ handle_shutdown (void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message) { - GNUNET_SERVER_client_keep(client); + GNUNET_SERVER_client_keep (client); GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Initiating shutdown as requested by client.\n")); GNUNET_SERVER_notify_transmit_ready (client, - sizeof(struct GNUNET_MessageHeader), + sizeof (struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, &transmit_shutdown_ack, client); GNUNET_SERVER_client_persist_ (client); @@ -953,12 +917,13 @@ static void sighandler_child_death () { static char c; - int old_errno = errno; /* back-up errno */ - GNUNET_break (1 == - GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle - (sigpipe, GNUNET_DISK_PIPE_END_WRITE), &c, - sizeof (c))); - errno = old_errno; /* restore errno */ + int old_errno = errno; /* back-up errno */ + + GNUNET_break (1 == + GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle + (sigpipe, GNUNET_DISK_PIPE_END_WRITE), + &c, sizeof (c))); + errno = old_errno; /* restore errno */ } @@ -991,63 +956,59 @@ run (void *cls, GNUNET_assert (pr != NULL); GNUNET_SERVER_ignore_shutdown (serv, GNUNET_YES); GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, - &shutdown_task, - NULL); + &shutdown_task, NULL); child_death_task = - GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, pr, - &maint_child_death, NULL); + GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, pr, + &maint_child_death, NULL); if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, - "ARM", - "GLOBAL_PREFIX", - &prefix_command)) + "ARM", + "GLOBAL_PREFIX", &prefix_command)) prefix_command = GNUNET_strdup (""); if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, - "ARM", - "GLOBAL_POSTFIX", - &final_option)) + "ARM", + "GLOBAL_POSTFIX", &final_option)) final_option = GNUNET_strdup (""); /* start default services... */ if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, - "ARM", - "DEFAULTSERVICES", - &defaultservices)) - { + "ARM", + "DEFAULTSERVICES", + &defaultservices)) + { #if DEBUG_ARM - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Starting default services `%s'\n", defaultservices); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Starting default services `%s'\n", defaultservices); #endif - if (0 < strlen (defaultservices)) - { - pos = strtok (defaultservices, " "); - while (pos != NULL) - { - start_service (NULL, pos, NULL); - pos = strtok (NULL, " "); - } - } - GNUNET_free (defaultservices); + if (0 < strlen (defaultservices)) + { + pos = strtok (defaultservices, " "); + while (pos != NULL) + { + start_service (NULL, pos, NULL); + pos = strtok (NULL, " "); + } } + GNUNET_free (defaultservices); + } else - { + { #if DEBUG_ARM - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "No default services configured.\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No default services configured.\n"); #endif - } + } - /* create listening sockets for future services*/ + /* create listening sockets for future services */ prepareServices (cfg); - + /* process client requests */ GNUNET_SERVER_add_handlers (server, handlers); /* manage services */ GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, - &config_change_task, NULL); + &config_change_task, NULL); } @@ -1066,10 +1027,11 @@ main (int argc, char *const *argv) sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO); GNUNET_assert (sigpipe != NULL); - shc_chld = GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death); - ret = (GNUNET_OK == - GNUNET_SERVICE_run (argc, - argv, "arm", GNUNET_YES, &run, NULL)) ? 0 : 1; + shc_chld = + GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death); + ret = + (GNUNET_OK == + GNUNET_SERVICE_run (argc, argv, "arm", GNUNET_YES, &run, NULL)) ? 0 : 1; GNUNET_SIGNAL_handler_uninstall (shc_chld); shc_chld = NULL; GNUNET_DISK_pipe_close (sigpipe); @@ -1085,8 +1047,8 @@ main (int argc, char *const *argv) */ void __attribute__ ((constructor)) GNUNET_ARM_memory_init () { - mallopt (M_TRIM_THRESHOLD, 4*1024); - mallopt (M_TOP_PAD, 1*1024); + mallopt (M_TRIM_THRESHOLD, 4 * 1024); + mallopt (M_TOP_PAD, 1 * 1024); malloc_trim (0); } #endif diff --git a/src/arm/gnunet-service-arm.h b/src/arm/gnunet-service-arm.h index 0ef557157..a9f72b512 100644 --- a/src/arm/gnunet-service-arm.h +++ b/src/arm/gnunet-service-arm.h @@ -36,8 +36,7 @@ * @return GNUNET_OK on success */ int start_service (struct GNUNET_SERVER_Client *client, - const char *servicename, - const int *lsocks); + const char *servicename, const int *lsocks); /** * Stop listening for connections to a service. @@ -49,6 +48,6 @@ int start_service (struct GNUNET_SERVER_Client *client, int stop_listening (const char *serviceName); void prepareServices (const struct GNUNET_CONFIGURATION_Handle - *configurationHandle); + *configurationHandle); #endif diff --git a/src/arm/gnunet-service-arm_interceptor.c b/src/arm/gnunet-service-arm_interceptor.c index 8f4a4731c..b99b076da 100644 --- a/src/arm/gnunet-service-arm_interceptor.c +++ b/src/arm/gnunet-service-arm_interceptor.c @@ -153,12 +153,12 @@ struct ForwardedConnection * Timeout for forwarding. */ struct GNUNET_TIME_Absolute timeout; - + /** * Current back-off value. */ struct GNUNET_TIME_Relative back_off; - + /** * Task that tries to initiate forwarding. */ @@ -243,10 +243,10 @@ addDefaultServicesToList (const char *services) s = GNUNET_strdup (services); token = strtok (s, " "); while (NULL != token) - { - numDefaultServices++; - token = strtok (NULL, " "); - } + { + numDefaultServices++; + token = strtok (NULL, " "); + } GNUNET_free (s); defaultServicesList = GNUNET_malloc (numDefaultServices * sizeof (char *)); @@ -254,10 +254,10 @@ addDefaultServicesToList (const char *services) s = GNUNET_strdup (services); token = strtok (s, " "); while (NULL != token) - { - defaultServicesList[i++] = GNUNET_strdup (token); - token = strtok (NULL, " "); - } + { + defaultServicesList[i++] = GNUNET_strdup (token); + token = strtok (NULL, " "); + } GNUNET_free (s); GNUNET_assert (i == numDefaultServices); } @@ -272,9 +272,10 @@ static int isInDefaultList (const char *serviceName) { unsigned int i; + for (i = 0; i < numDefaultServices; i++) if (strcmp (serviceName, defaultServicesList[i]) == 0) - return GNUNET_YES; + return GNUNET_YES; return GNUNET_NO; } @@ -286,52 +287,45 @@ isInDefaultList (const char *serviceName) * @param reason which direction to close */ static void -closeClientAndServiceSockets (struct ForwardedConnection *fc, - int reason) +closeClientAndServiceSockets (struct ForwardedConnection *fc, int reason) { - if (0 != (REASON_SERVICE_TO_CLIENT & reason)) - { + if (0 != (REASON_SERVICE_TO_CLIENT & reason)) + { #if DEBUG_SERVICE_MANAGER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Stopping forwarding from service to client\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Stopping forwarding from service to client\n"); #endif - if (fc->armClientSocket != NULL) - GNUNET_NETWORK_socket_shutdown (fc->armClientSocket, - SHUT_WR); - if (fc->armServiceSocket != NULL) - GNUNET_NETWORK_socket_shutdown (fc->armServiceSocket, - SHUT_RD); - } - if (0 != (REASON_CLIENT_TO_SERVICE & reason)) - { + if (fc->armClientSocket != NULL) + GNUNET_NETWORK_socket_shutdown (fc->armClientSocket, SHUT_WR); + if (fc->armServiceSocket != NULL) + GNUNET_NETWORK_socket_shutdown (fc->armServiceSocket, SHUT_RD); + } + if (0 != (REASON_CLIENT_TO_SERVICE & reason)) + { #if DEBUG_SERVICE_MANAGER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Stopping forwarding from client to service\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Stopping forwarding from client to service\n"); #endif - if (fc->armClientSocket != NULL) - GNUNET_NETWORK_socket_shutdown (fc->armClientSocket, - SHUT_RD); - if (fc->armServiceSocket != NULL) - GNUNET_NETWORK_socket_shutdown (fc->armServiceSocket, - SHUT_WR); - } + if (fc->armClientSocket != NULL) + GNUNET_NETWORK_socket_shutdown (fc->armClientSocket, SHUT_RD); + if (fc->armServiceSocket != NULL) + GNUNET_NETWORK_socket_shutdown (fc->armServiceSocket, SHUT_WR); + } #if DEBUG_SERVICE_MANAGER GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Closing forwarding connection (done with both directions)\n"); + "Closing forwarding connection (done with both directions)\n"); #endif fc->reference_count -= 1; if (fc->reference_count <= 0) { - if ( (NULL != fc->armClientSocket) && - (GNUNET_SYSERR == - GNUNET_NETWORK_socket_close (fc->armClientSocket)) ) + if ((NULL != fc->armClientSocket) && + (GNUNET_SYSERR == GNUNET_NETWORK_socket_close (fc->armClientSocket))) { GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "close"); fc->armClientSocket = NULL; } - if ( (NULL != fc->armServiceSocket) && - (GNUNET_SYSERR == - GNUNET_NETWORK_socket_close (fc->armServiceSocket)) ) + if ((NULL != fc->armServiceSocket) && + (GNUNET_SYSERR == GNUNET_NETWORK_socket_close (fc->armServiceSocket))) { GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "close"); fc->armServiceSocket = NULL; @@ -380,8 +374,7 @@ receiveFromService (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); * */ static void -start_forwarding (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc); +start_forwarding (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); @@ -399,54 +392,46 @@ forwardToClient (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) fc->service_to_client_task = GNUNET_SCHEDULER_NO_TASK; if (GNUNET_YES != GNUNET_NETWORK_fdset_isset (tc->write_ready, - fc->armClientSocket)) - { - fc->service_to_client_task = - GNUNET_SCHEDULER_add_write_net ( - GNUNET_TIME_UNIT_FOREVER_REL, - fc->armClientSocket, - &forwardToClient, fc); - return; - } + fc->armClientSocket)) + { + fc->service_to_client_task = + GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, + fc->armClientSocket, + &forwardToClient, fc); + return; + } /* Forwarding service response to client */ numberOfBytesSent = - GNUNET_NETWORK_socket_send (fc->armClientSocket, - fc->service_to_client_bufferPos, - fc->service_to_client_bufferDataLength); + GNUNET_NETWORK_socket_send (fc->armClientSocket, + fc->service_to_client_bufferPos, + fc->service_to_client_bufferDataLength); if (numberOfBytesSent <= 0) - { - if ( (errno != EPIPE) && - (errno != ECONNRESET) ) - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Failed to forward %u bytes of data to client: %s\n", - fc->service_to_client_bufferDataLength, - STRERROR (errno)); - closeClientAndServiceSockets (fc, - REASON_SERVICE_TO_CLIENT); - return; - } + { + if ((errno != EPIPE) && (errno != ECONNRESET)) + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Failed to forward %u bytes of data to client: %s\n", + fc->service_to_client_bufferDataLength, STRERROR (errno)); + closeClientAndServiceSockets (fc, REASON_SERVICE_TO_CLIENT); + return; + } #if DEBUG_SERVICE_MANAGER GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Forwarded %d bytes to client\n", - numberOfBytesSent); + "Forwarded %d bytes to client\n", numberOfBytesSent); #endif if (numberOfBytesSent < fc->service_to_client_bufferDataLength) - { - fc->service_to_client_bufferPos += numberOfBytesSent; - fc->service_to_client_bufferDataLength -= numberOfBytesSent; - fc->service_to_client_task = - GNUNET_SCHEDULER_add_write_net ( - GNUNET_TIME_UNIT_FOREVER_REL, - fc->armClientSocket, - &forwardToClient, - fc); - return; - } + { + fc->service_to_client_bufferPos += numberOfBytesSent; + fc->service_to_client_bufferDataLength -= numberOfBytesSent; + fc->service_to_client_task = + GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, + fc->armClientSocket, + &forwardToClient, fc); + return; + } fc->service_to_client_task = - GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, - fc->armServiceSocket, - &receiveFromService, - fc); + GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, + fc->armServiceSocket, + &receiveFromService, fc); } @@ -457,98 +442,92 @@ forwardToClient (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) * @param tc scheduler context */ static void -receiveFromService (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) +receiveFromService (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { struct ForwardedConnection *fc = cls; struct GNUNET_TIME_Relative rem; fc->service_to_client_task = GNUNET_SCHEDULER_NO_TASK; - if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) && - (fc->first_write_done != GNUNET_YES) ) - { - closeClientAndServiceSockets (fc, REASON_ERROR); - return; - } + if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) && + (fc->first_write_done != GNUNET_YES)) + { + closeClientAndServiceSockets (fc, REASON_ERROR); + return; + } if (GNUNET_YES != GNUNET_NETWORK_fdset_isset (tc->read_ready, - fc->armServiceSocket)) - { - fc->service_to_client_task = - GNUNET_SCHEDULER_add_read_net ( - GNUNET_TIME_UNIT_FOREVER_REL, - fc->armServiceSocket, - &receiveFromService, fc); - return; - } + fc->armServiceSocket)) + { + fc->service_to_client_task = + GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, + fc->armServiceSocket, + &receiveFromService, fc); + return; + } fc->service_to_client_bufferPos = fc->service_to_client_buffer; fc->service_to_client_bufferDataLength = - GNUNET_NETWORK_socket_recv (fc->armServiceSocket, - fc->service_to_client_buffer, - BUFFER_SIZE); + GNUNET_NETWORK_socket_recv (fc->armServiceSocket, + fc->service_to_client_buffer, BUFFER_SIZE); if (fc->service_to_client_bufferDataLength <= 0) - { + { #if DEBUG_SERVICE_MANAGER - if (fc->service_to_client_bufferDataLength == 0) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Service `%s' stopped sending data.\n", - fc->listen_info->serviceName); - } + if (fc->service_to_client_bufferDataLength == 0) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Service `%s' stopped sending data.\n", + fc->listen_info->serviceName); + } #endif - if (fc->first_write_done != GNUNET_YES) - { - fc->service_to_client_bufferDataLength = 0; - GNUNET_break (GNUNET_OK == - GNUNET_NETWORK_socket_close (fc->armServiceSocket)); - fc->armServiceSocket = NULL; - if ( (fc->client_to_service_bufferDataLength > 0) && - (fc->client_to_service_task != GNUNET_SCHEDULER_NO_TASK) ) - { - GNUNET_SCHEDULER_cancel (fc->client_to_service_task); - fc->client_to_service_task = GNUNET_SCHEDULER_NO_TASK; - } - fc->back_off = GNUNET_TIME_relative_multiply (fc->back_off, 2); + if (fc->first_write_done != GNUNET_YES) + { + fc->service_to_client_bufferDataLength = 0; + GNUNET_break (GNUNET_OK == + GNUNET_NETWORK_socket_close (fc->armServiceSocket)); + fc->armServiceSocket = NULL; + if ((fc->client_to_service_bufferDataLength > 0) && + (fc->client_to_service_task != GNUNET_SCHEDULER_NO_TASK)) + { + GNUNET_SCHEDULER_cancel (fc->client_to_service_task); + fc->client_to_service_task = GNUNET_SCHEDULER_NO_TASK; + } + fc->back_off = GNUNET_TIME_relative_multiply (fc->back_off, 2); #if DEBUG_SERVICE_MANAGER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Failed to connected to service `%s' at `%s', will try again in %llu ms\n", - fc->listen_info->serviceName, - GNUNET_a2s (fc->listen_info->service_addr, - fc->listen_info->service_addr_len), - (unsigned long long) GNUNET_TIME_relative_min (fc->back_off, - rem).rel_value); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Failed to connected to service `%s' at `%s', will try again in %llu ms\n", + fc->listen_info->serviceName, + GNUNET_a2s (fc->listen_info->service_addr, + fc->listen_info->service_addr_len), + (unsigned long long) GNUNET_TIME_relative_min (fc->back_off, + rem).rel_value); #endif - rem = GNUNET_TIME_absolute_get_remaining (fc->timeout); - GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == fc->start_task); - fc->start_task - = GNUNET_SCHEDULER_add_delayed ( - GNUNET_TIME_relative_min (fc->back_off, - rem), - &start_forwarding, - fc); - } - else - { + rem = GNUNET_TIME_absolute_get_remaining (fc->timeout); + GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == fc->start_task); + fc->start_task + = + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_min + (fc->back_off, rem), &start_forwarding, + fc); + } + else + { #if DEBUG_SERVICE_MANAGER - if (fc->service_to_client_bufferDataLength != 0) - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Error receiving from service: %s\n", - STRERROR (errno)); + if (fc->service_to_client_bufferDataLength != 0) + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Error receiving from service: %s\n", STRERROR (errno)); #endif - closeClientAndServiceSockets (fc, REASON_SERVICE_TO_CLIENT); - } - return; + closeClientAndServiceSockets (fc, REASON_SERVICE_TO_CLIENT); } + return; + } fc->first_write_done = GNUNET_YES; #if DEBUG_SERVICE_MANAGER GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received %d bytes for client\n", - fc->service_to_client_bufferDataLength); + "Received %d bytes for client\n", + fc->service_to_client_bufferDataLength); #endif - fc->service_to_client_task = - GNUNET_SCHEDULER_add_write_net ( - GNUNET_TIME_UNIT_FOREVER_REL, - fc->armClientSocket, - &forwardToClient, fc); + fc->service_to_client_task = + GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, + fc->armClientSocket, + &forwardToClient, fc); } @@ -559,98 +538,92 @@ receiveFromService (void *cls, * @param tc scheduler context */ static void -forwardToService (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) +forwardToService (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { struct ForwardedConnection *fc = cls; ssize_t numberOfBytesSent; struct GNUNET_TIME_Relative rem; fc->client_to_service_task = GNUNET_SCHEDULER_NO_TASK; - if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) && - (fc->first_write_done != GNUNET_YES) ) - { - closeClientAndServiceSockets (fc, REASON_ERROR); - return; - } + if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) && + (fc->first_write_done != GNUNET_YES)) + { + closeClientAndServiceSockets (fc, REASON_ERROR); + return; + } if (GNUNET_YES != GNUNET_NETWORK_fdset_isset (tc->write_ready, - fc->armServiceSocket)) - { - fc->client_to_service_task = - GNUNET_SCHEDULER_add_write_net ( - GNUNET_TIME_UNIT_FOREVER_REL, - fc->armServiceSocket, - &forwardToService, fc); - return; - } + fc->armServiceSocket)) + { + fc->client_to_service_task = + GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, + fc->armServiceSocket, + &forwardToService, fc); + return; + } numberOfBytesSent = - GNUNET_NETWORK_socket_send (fc->armServiceSocket, - fc->client_to_service_bufferPos, - fc->client_to_service_bufferDataLength); + GNUNET_NETWORK_socket_send (fc->armServiceSocket, + fc->client_to_service_bufferPos, + fc->client_to_service_bufferDataLength); if (numberOfBytesSent <= 0) + { + if (GNUNET_YES != fc->first_write_done) { - if (GNUNET_YES != fc->first_write_done) - { - GNUNET_break (GNUNET_OK == - GNUNET_NETWORK_socket_close (fc->armServiceSocket)); - fc->armServiceSocket = NULL; - if ( (fc->service_to_client_bufferDataLength == 0) && - (fc->service_to_client_task != GNUNET_SCHEDULER_NO_TASK) ) - { - GNUNET_SCHEDULER_cancel (fc->service_to_client_task); - fc->service_to_client_task = GNUNET_SCHEDULER_NO_TASK; - } - fc->back_off = GNUNET_TIME_relative_multiply (fc->back_off, 2); + GNUNET_break (GNUNET_OK == + GNUNET_NETWORK_socket_close (fc->armServiceSocket)); + fc->armServiceSocket = NULL; + if ((fc->service_to_client_bufferDataLength == 0) && + (fc->service_to_client_task != GNUNET_SCHEDULER_NO_TASK)) + { + GNUNET_SCHEDULER_cancel (fc->service_to_client_task); + fc->service_to_client_task = GNUNET_SCHEDULER_NO_TASK; + } + fc->back_off = GNUNET_TIME_relative_multiply (fc->back_off, 2); #if DEBUG_SERVICE_MANAGER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Failed to connect to service `%s' at `%s', will try again in %llu ms\n", - fc->listen_info->serviceName, - GNUNET_a2s (fc->listen_info->service_addr, - fc->listen_info->service_addr_len), - (unsigned long long) GNUNET_TIME_relative_min (fc->back_off, - rem).rel_value); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Failed to connect to service `%s' at `%s', will try again in %llu ms\n", + fc->listen_info->serviceName, + GNUNET_a2s (fc->listen_info->service_addr, + fc->listen_info->service_addr_len), + (unsigned long long) GNUNET_TIME_relative_min (fc->back_off, + rem).rel_value); #endif - rem = GNUNET_TIME_absolute_get_remaining (fc->timeout); - GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == fc->start_task); - fc->start_task - = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_min (fc->back_off, - rem), - &start_forwarding, - fc); - } - else - { - if ( (errno != EPIPE) && - (errno != ECONNRESET) ) - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Failed to forward data to service: %s\n", - STRERROR (errno)); - closeClientAndServiceSockets (fc, - REASON_CLIENT_TO_SERVICE); - } - return; + rem = GNUNET_TIME_absolute_get_remaining (fc->timeout); + GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == fc->start_task); + fc->start_task + = + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_min + (fc->back_off, rem), &start_forwarding, + fc); + } + else + { + if ((errno != EPIPE) && (errno != ECONNRESET)) + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Failed to forward data to service: %s\n", + STRERROR (errno)); + closeClientAndServiceSockets (fc, REASON_CLIENT_TO_SERVICE); } + return; + } #if DEBUG_SERVICE_MANAGER GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Forwarded %d bytes to service\n", - numberOfBytesSent); + "Forwarded %d bytes to service\n", numberOfBytesSent); #endif fc->first_write_done = GNUNET_YES; if (numberOfBytesSent < fc->client_to_service_bufferDataLength) - { - fc->client_to_service_bufferPos += numberOfBytesSent; - fc->client_to_service_bufferDataLength -= numberOfBytesSent; - fc->client_to_service_task = - GNUNET_SCHEDULER_add_write_net ( - GNUNET_TIME_UNIT_FOREVER_REL, - fc->armServiceSocket, - &forwardToService, fc); - return; - } + { + fc->client_to_service_bufferPos += numberOfBytesSent; + fc->client_to_service_bufferDataLength -= numberOfBytesSent; + fc->client_to_service_task = + GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, + fc->armServiceSocket, + &forwardToService, fc); + return; + } fc->client_to_service_task = - GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, - fc->armClientSocket, - &receiveFromClient, fc); + GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, + fc->armClientSocket, + &receiveFromClient, fc); } @@ -667,76 +640,73 @@ receiveFromClient (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) fc->client_to_service_task = GNUNET_SCHEDULER_NO_TASK; if (GNUNET_YES != GNUNET_NETWORK_fdset_isset (tc->read_ready, - fc->armClientSocket)) - { - fc->client_to_service_task = - GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, - fc->armClientSocket, - &receiveFromClient, fc); - return; - } + fc->armClientSocket)) + { + fc->client_to_service_task = + GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, + fc->armClientSocket, + &receiveFromClient, fc); + return; + } fc->client_to_service_bufferPos = fc->client_to_service_buffer; fc->client_to_service_bufferDataLength = - GNUNET_NETWORK_socket_recv (fc->armClientSocket, - fc->client_to_service_buffer, - BUFFER_SIZE); + GNUNET_NETWORK_socket_recv (fc->armClientSocket, + fc->client_to_service_buffer, BUFFER_SIZE); if (fc->client_to_service_bufferDataLength <= 0) + { + if (fc->client_to_service_bufferDataLength == 0) { - if (fc->client_to_service_bufferDataLength == 0) - { #if DEBUG_SERVICE_MANAGER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Client closed connection with service `%s'\n", - fc->listen_info->serviceName); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Client closed connection with service `%s'\n", + fc->listen_info->serviceName); #endif - } - else - { + } + else + { #if DEBUG_SERVICE_MANAGER - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Error receiving from client: %s\n", - STRERROR (errno)); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Error receiving from client: %s\n", STRERROR (errno)); #endif - } - closeClientAndServiceSockets (fc, REASON_CLIENT_TO_SERVICE); - return; } + closeClientAndServiceSockets (fc, REASON_CLIENT_TO_SERVICE); + return; + } #if DEBUG_SERVICE_MANAGER GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received %d bytes for service\n", - fc->client_to_service_bufferDataLength); + "Received %d bytes for service\n", + fc->client_to_service_bufferDataLength); #endif - if (fc->armServiceSocket != NULL) - fc->client_to_service_task = - GNUNET_SCHEDULER_add_write_net ( - GNUNET_TIME_UNIT_FOREVER_REL, - fc->armServiceSocket, - &forwardToService, fc); + if (fc->armServiceSocket != NULL) + fc->client_to_service_task = + GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, + fc->armServiceSocket, + &forwardToService, fc); else - /* We have not added any task with fc as a closure, so we're - * dropping our reference to fc - */ + /* We have not added any task with fc as a closure, so we're + * dropping our reference to fc + */ fc->reference_count -= 1; } static void -fc_acceptConnection (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) +fc_acceptConnection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { struct ServiceListeningInfo *sli = cls; struct ForwardedConnection *fc = sli->fc; if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) - { - GNUNET_assert (GNUNET_OK == GNUNET_NETWORK_socket_close (sli->listeningSocket)); - closeClientAndServiceSockets (fc, REASON_ERROR); - GNUNET_free (sli); - return; - } + { + GNUNET_assert (GNUNET_OK == + GNUNET_NETWORK_socket_close (sli->listeningSocket)); + closeClientAndServiceSockets (fc, REASON_ERROR); + GNUNET_free (sli); + return; + } #if DEBUG_SERVICE_MANAGER GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Connected to service, now starting forwarding\n"); + "Connected to service, now starting forwarding\n"); #endif fc->armServiceSocket = sli->listeningSocket; GNUNET_free (fc->listen_info->service_addr); @@ -749,72 +719,70 @@ fc_acceptConnection (void *cls, */ fc->reference_count -= 1; if (fc->client_to_service_task == GNUNET_SCHEDULER_NO_TASK) - { - if (fc->client_to_service_bufferDataLength == 0) - fc->client_to_service_task = - GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, - fc->armClientSocket, - &receiveFromClient, fc); - else - fc->client_to_service_task = - GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, - fc->armServiceSocket, - &forwardToService, fc); - fc->reference_count += 1; - } + { + if (fc->client_to_service_bufferDataLength == 0) + fc->client_to_service_task = + GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, + fc->armClientSocket, + &receiveFromClient, fc); + else + fc->client_to_service_task = + GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, + fc->armServiceSocket, + &forwardToService, fc); + fc->reference_count += 1; + } if (fc->service_to_client_task == GNUNET_SCHEDULER_NO_TASK) - { - if (fc->service_to_client_bufferDataLength == 0) - fc->service_to_client_task = - GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, - fc->armServiceSocket, - &receiveFromService, fc); - else - fc->service_to_client_task = - GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, - fc->armClientSocket, - &forwardToClient, fc); - fc->reference_count += 1; - } + { + if (fc->service_to_client_bufferDataLength == 0) + fc->service_to_client_task = + GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, + fc->armServiceSocket, + &receiveFromService, fc); + else + fc->service_to_client_task = + GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, + fc->armClientSocket, + &forwardToClient, fc); + fc->reference_count += 1; + } GNUNET_free (sli); } static struct ServiceListeningInfo * -service_try_to_connect (const struct sockaddr *addr, - int pf, - socklen_t addrlen, - struct ForwardedConnection *fc) +service_try_to_connect (const struct sockaddr *addr, + int pf, + socklen_t addrlen, struct ForwardedConnection *fc) { struct GNUNET_NETWORK_Handle *sock; struct ServiceListeningInfo *serviceListeningInfo; sock = GNUNET_NETWORK_socket_create (pf, SOCK_STREAM, 0); if (sock == NULL) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket"); - return NULL; - } - if ( (GNUNET_SYSERR == GNUNET_NETWORK_socket_connect (sock, addr, addrlen)) && - (errno != EINPROGRESS) ) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect"); - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); - return NULL; - } + { + GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket"); + return NULL; + } + if ((GNUNET_SYSERR == GNUNET_NETWORK_socket_connect (sock, addr, addrlen)) && + (errno != EINPROGRESS)) + { + GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect"); + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); + return NULL; + } serviceListeningInfo = GNUNET_malloc (sizeof (struct ServiceListeningInfo)); serviceListeningInfo->serviceName = NULL; serviceListeningInfo->service_addr = GNUNET_malloc (addrlen); - memcpy (serviceListeningInfo->service_addr, - addr, - addrlen); + memcpy (serviceListeningInfo->service_addr, addr, addrlen); serviceListeningInfo->service_addr_len = addrlen; serviceListeningInfo->listeningSocket = sock; serviceListeningInfo->fc = fc; serviceListeningInfo->acceptTask = - GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, - serviceListeningInfo->listeningSocket, - &fc_acceptConnection, serviceListeningInfo); + GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, + serviceListeningInfo->listeningSocket, + &fc_acceptConnection, + serviceListeningInfo); return serviceListeningInfo; } @@ -823,8 +791,7 @@ service_try_to_connect (const struct sockaddr *addr, * */ static void -start_forwarding (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) +start_forwarding (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { struct ForwardedConnection *fc = cls; struct ServiceListeningInfo *sc; @@ -835,91 +802,80 @@ start_forwarding (void *cls, char listen_address[INET6_ADDRSTRLEN]; fc->start_task = GNUNET_SCHEDULER_NO_TASK; - if ( (NULL != tc) && - (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) ) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Unable to forward to service `%s': shutdown\n"), - fc->listen_info->serviceName); - closeClientAndServiceSockets (fc, REASON_ERROR); - return; - } + if ((NULL != tc) && (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("Unable to forward to service `%s': shutdown\n"), + fc->listen_info->serviceName); + closeClientAndServiceSockets (fc, REASON_ERROR); + return; + } if (0 == GNUNET_TIME_absolute_get_remaining (fc->timeout).rel_value) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Unable to forward to service `%s': timeout before connect\n"), - fc->listen_info->serviceName); - closeClientAndServiceSockets (fc, REASON_ERROR); - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _ + ("Unable to forward to service `%s': timeout before connect\n"), + fc->listen_info->serviceName); + closeClientAndServiceSockets (fc, REASON_ERROR); + return; + } switch (fc->listen_info->service_addr->sa_family) + { + case AF_UNSPEC: + GNUNET_break (0); + closeClientAndServiceSockets (fc, REASON_ERROR); + return; + case AF_INET: + v4 = (const struct sockaddr_in *) fc->listen_info->service_addr; + inet_ntop (fc->listen_info->service_addr->sa_family, + (const void *) &v4->sin_addr, listen_address, INET_ADDRSTRLEN); + if (0 == strncmp (listen_address, "0.0.0.0", 7)) { - case AF_UNSPEC: - GNUNET_break (0); - closeClientAndServiceSockets (fc, REASON_ERROR); - return; - case AF_INET: - v4 = (const struct sockaddr_in *) fc->listen_info->service_addr; - inet_ntop (fc->listen_info->service_addr->sa_family, - (const void *) &v4->sin_addr, - listen_address, - INET_ADDRSTRLEN); - if (0 == strncmp (listen_address, "0.0.0.0", 7)) - { - /* connect to [::1] and 127.0.0.1 instead of [::] and 0.0.0.0 */ - memset (&target_ipv4, 0, sizeof (target_ipv4)); - GNUNET_assert (1 == - inet_pton (AF_INET, - "127.0.0.1", - &target_ipv4.sin_addr)); - target_ipv4.sin_family = AF_INET; - target_ipv4.sin_port = v4->sin_port; - v4 = &target_ipv4; - } - sc = service_try_to_connect ((const struct sockaddr*) v4, - PF_INET, - sizeof (struct sockaddr_in), - fc); - break; - case AF_INET6: - v6 = (struct sockaddr_in6 *)fc->listen_info->service_addr; - inet_ntop (fc->listen_info->service_addr->sa_family, - (const void *) &v6->sin6_addr, - listen_address, - INET6_ADDRSTRLEN); - if ( (strncmp (listen_address, "[::]:", 5) == 0) || (strncmp (listen_address, "::", 2) == 0) ) - { - memset (&target_ipv6, 0, sizeof (target_ipv6)); - target_ipv6.sin6_addr = in6addr_loopback; - target_ipv6.sin6_family = AF_INET6; - target_ipv6.sin6_port = v6->sin6_port; - v6 = &target_ipv6; - } - sc = service_try_to_connect ((const struct sockaddr*) v6, - PF_INET6, - sizeof (struct sockaddr_in6), - fc); - break; - case AF_UNIX: - sc = service_try_to_connect (fc->listen_info->service_addr, - PF_UNIX, - fc->listen_info->service_addr_len, - fc); - break; - default: - GNUNET_break (0); - closeClientAndServiceSockets (fc, REASON_ERROR); - return; - } - if (NULL == sc) + /* connect to [::1] and 127.0.0.1 instead of [::] and 0.0.0.0 */ + memset (&target_ipv4, 0, sizeof (target_ipv4)); + GNUNET_assert (1 == + inet_pton (AF_INET, "127.0.0.1", &target_ipv4.sin_addr)); + target_ipv4.sin_family = AF_INET; + target_ipv4.sin_port = v4->sin_port; + v4 = &target_ipv4; + } + sc = service_try_to_connect ((const struct sockaddr *) v4, + PF_INET, sizeof (struct sockaddr_in), fc); + break; + case AF_INET6: + v6 = (struct sockaddr_in6 *) fc->listen_info->service_addr; + inet_ntop (fc->listen_info->service_addr->sa_family, + (const void *) &v6->sin6_addr, listen_address, INET6_ADDRSTRLEN); + if ((strncmp (listen_address, "[::]:", 5) == 0) || + (strncmp (listen_address, "::", 2) == 0)) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ ("Unable to start service `%s': %s\n"), - fc->listen_info->serviceName, - STRERROR (errno)); - closeClientAndServiceSockets (fc, REASON_ERROR); - return; + memset (&target_ipv6, 0, sizeof (target_ipv6)); + target_ipv6.sin6_addr = in6addr_loopback; + target_ipv6.sin6_family = AF_INET6; + target_ipv6.sin6_port = v6->sin6_port; + v6 = &target_ipv6; } + sc = service_try_to_connect ((const struct sockaddr *) v6, + PF_INET6, sizeof (struct sockaddr_in6), fc); + break; + case AF_UNIX: + sc = service_try_to_connect (fc->listen_info->service_addr, + PF_UNIX, + fc->listen_info->service_addr_len, fc); + break; + default: + GNUNET_break (0); + closeClientAndServiceSockets (fc, REASON_ERROR); + return; + } + if (NULL == sc) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Unable to start service `%s': %s\n"), + fc->listen_info->serviceName, STRERROR (errno)); + closeClientAndServiceSockets (fc, REASON_ERROR); + return; + } } @@ -932,27 +888,25 @@ stop_listening (const char *serviceName) struct ServiceListeningInfo *pos; struct ServiceListeningInfo *next; int ret; - + ret = GNUNET_NO; next = serviceListeningInfoList_head; while (NULL != (pos = next)) - { - next = pos->next; - if ( (serviceName != NULL) && - (strcmp (pos->serviceName, serviceName) != 0) ) - continue; - if (pos->acceptTask != GNUNET_SCHEDULER_NO_TASK) - GNUNET_SCHEDULER_cancel (pos->acceptTask); - GNUNET_break (GNUNET_OK == - GNUNET_NETWORK_socket_close (pos->listeningSocket)); - GNUNET_CONTAINER_DLL_remove (serviceListeningInfoList_head, - serviceListeningInfoList_tail, - pos); - GNUNET_free (pos->serviceName); - GNUNET_free (pos->service_addr); - GNUNET_free (pos); - ret = GNUNET_OK; - } + { + next = pos->next; + if ((serviceName != NULL) && (strcmp (pos->serviceName, serviceName) != 0)) + continue; + if (pos->acceptTask != GNUNET_SCHEDULER_NO_TASK) + GNUNET_SCHEDULER_cancel (pos->acceptTask); + GNUNET_break (GNUNET_OK == + GNUNET_NETWORK_socket_close (pos->listeningSocket)); + GNUNET_CONTAINER_DLL_remove (serviceListeningInfoList_head, + serviceListeningInfoList_tail, pos); + GNUNET_free (pos->serviceName); + GNUNET_free (pos->service_addr); + GNUNET_free (pos); + ret = GNUNET_OK; + } return ret; } @@ -978,46 +932,44 @@ accept_and_forward (struct ServiceListeningInfo *serviceListeningInfo) fc->service_to_client_bufferPos = fc->service_to_client_buffer; fc->client_to_service_bufferPos = fc->client_to_service_buffer; fc->client_addr_len = sizeof (fc->client_addr); - fc->armClientSocket = GNUNET_NETWORK_socket_accept (serviceListeningInfo->listeningSocket, - (struct sockaddr*) fc->client_addr, - &fc->client_addr_len); + fc->armClientSocket = + GNUNET_NETWORK_socket_accept (serviceListeningInfo->listeningSocket, + (struct sockaddr *) fc->client_addr, + &fc->client_addr_len); if (NULL == fc->armClientSocket) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Unable to accept connection for service `%s': %s\n"), - serviceListeningInfo->serviceName, - STRERROR (errno)); - GNUNET_free (fc); - GNUNET_CONTAINER_DLL_insert (serviceListeningInfoList_head, - serviceListeningInfoList_tail, - serviceListeningInfo); - serviceListeningInfo->acceptTask = - GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, - serviceListeningInfo->listeningSocket, - &acceptConnection, - serviceListeningInfo); - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Unable to accept connection for service `%s': %s\n"), + serviceListeningInfo->serviceName, STRERROR (errno)); + GNUNET_free (fc); + GNUNET_CONTAINER_DLL_insert (serviceListeningInfoList_head, + serviceListeningInfoList_tail, + serviceListeningInfo); + serviceListeningInfo->acceptTask = + GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, + serviceListeningInfo->listeningSocket, + &acceptConnection, serviceListeningInfo); + return; + } GNUNET_break (GNUNET_OK == - GNUNET_NETWORK_socket_close (serviceListeningInfo->listeningSocket)); + GNUNET_NETWORK_socket_close + (serviceListeningInfo->listeningSocket)); start_service (NULL, serviceListeningInfo->serviceName, NULL); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Service `%s' started\n"), - fc->listen_info->serviceName); - fc->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_SERVICE_TIMEOUT); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("Service `%s' started\n"), fc->listen_info->serviceName); + fc->timeout = + GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_SERVICE_TIMEOUT); fc->back_off = GNUNET_TIME_UNIT_MILLISECONDS; fc->client_to_service_task = - GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, - fc->armClientSocket, - &receiveFromClient, fc); + GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, + fc->armClientSocket, + &receiveFromClient, fc); GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == fc->start_task); /* We're creating another chain of tasks for this fc that * will have its own reference to it. */ fc->reference_count += 1; - fc->start_task - = GNUNET_SCHEDULER_add_now (&start_forwarding, - fc); + fc->start_task = GNUNET_SCHEDULER_add_now (&start_forwarding, fc); } @@ -1042,59 +994,54 @@ acceptConnection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason)) return; GNUNET_CONTAINER_DLL_remove (serviceListeningInfoList_head, - serviceListeningInfoList_tail, - sli); + serviceListeningInfoList_tail, sli); #ifndef MINGW use_lsocks = GNUNET_NO; if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg, - sli->serviceName, - "DISABLE_SOCKET_FORWARDING")) + sli->serviceName, + "DISABLE_SOCKET_FORWARDING")) use_lsocks = GNUNET_CONFIGURATION_get_value_yesno (cfg, - sli->serviceName, - "DISABLE_SOCKET_FORWARDING"); + sli->serviceName, + "DISABLE_SOCKET_FORWARDING"); #else use_lsocks = GNUNET_YES; #endif if (GNUNET_NO != use_lsocks) - { - accept_and_forward (sli); - return; - } + { + accept_and_forward (sli); + return; + } lsocks = NULL; ls = 0; next = serviceListeningInfoList_head; while (NULL != (pos = next)) + { + next = pos->next; + if (0 == strcmp (pos->serviceName, sli->serviceName)) { - next = pos->next; - if (0 == strcmp (pos->serviceName, - sli->serviceName)) - { - GNUNET_array_append (lsocks, ls, - GNUNET_NETWORK_get_fd (pos->listeningSocket)); - GNUNET_free (pos->listeningSocket); /* deliberately no closing! */ - GNUNET_free (pos->service_addr); - GNUNET_free (pos->serviceName); - GNUNET_SCHEDULER_cancel (pos->acceptTask); - GNUNET_CONTAINER_DLL_remove (serviceListeningInfoList_head, - serviceListeningInfoList_tail, - pos); - GNUNET_free (pos); - } + GNUNET_array_append (lsocks, ls, + GNUNET_NETWORK_get_fd (pos->listeningSocket)); + GNUNET_free (pos->listeningSocket); /* deliberately no closing! */ + GNUNET_free (pos->service_addr); + GNUNET_free (pos->serviceName); + GNUNET_SCHEDULER_cancel (pos->acceptTask); + GNUNET_CONTAINER_DLL_remove (serviceListeningInfoList_head, + serviceListeningInfoList_tail, pos); + GNUNET_free (pos); } - GNUNET_array_append (lsocks, ls, - GNUNET_NETWORK_get_fd (sli->listeningSocket)); - GNUNET_free (sli->listeningSocket); /* deliberately no closing! */ + } + GNUNET_array_append (lsocks, ls, + GNUNET_NETWORK_get_fd (sli->listeningSocket)); + GNUNET_free (sli->listeningSocket); /* deliberately no closing! */ GNUNET_free (sli->service_addr); GNUNET_array_append (lsocks, ls, -1); - start_service (NULL, - sli->serviceName, - lsocks); + start_service (NULL, sli->serviceName, lsocks); ls = 0; while (lsocks[ls] != -1) - GNUNET_break (0 == close (lsocks[ls++])); + GNUNET_break (0 == close (lsocks[ls++])); GNUNET_array_grow (lsocks, ls, 0); GNUNET_free (sli->serviceName); - GNUNET_free (sli); + GNUNET_free (sli); } @@ -1107,89 +1054,84 @@ acceptConnection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) * @param serviceName the name of the service in question */ static void -createListeningSocket(struct sockaddr *sa, socklen_t addr_len, - const char *serviceName) +createListeningSocket (struct sockaddr *sa, socklen_t addr_len, + const char *serviceName) { const static int on = 1; struct GNUNET_NETWORK_Handle *sock; struct ServiceListeningInfo *serviceListeningInfo; switch (sa->sa_family) - { - case AF_INET: - sock = GNUNET_NETWORK_socket_create (PF_INET, SOCK_STREAM, 0); - break; - case AF_INET6: - sock = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0); - break; - case AF_UNIX: - if (strcmp(GNUNET_a2s (sa, addr_len), "@") == 0) /* Do not bind to blank UNIX path! */ - return; - sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0); - break; - default: - GNUNET_break (0); - sock = NULL; - errno = EAFNOSUPPORT; - break; - } - if (NULL == sock) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Unable to create socket for service `%s': %s\n"), - serviceName, - STRERROR (errno)); - GNUNET_free (sa); + { + case AF_INET: + sock = GNUNET_NETWORK_socket_create (PF_INET, SOCK_STREAM, 0); + break; + case AF_INET6: + sock = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0); + break; + case AF_UNIX: + if (strcmp (GNUNET_a2s (sa, addr_len), "@") == 0) /* Do not bind to blank UNIX path! */ return; - } + sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0); + break; + default: + GNUNET_break (0); + sock = NULL; + errno = EAFNOSUPPORT; + break; + } + if (NULL == sock) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Unable to create socket for service `%s': %s\n"), + serviceName, STRERROR (errno)); + GNUNET_free (sa); + return; + } if (GNUNET_NETWORK_socket_setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt"); #ifdef IPV6_V6ONLY - if ( (sa->sa_family == AF_INET6) && - (GNUNET_NETWORK_socket_setsockopt - (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK)) + if ((sa->sa_family == AF_INET6) && + (GNUNET_NETWORK_socket_setsockopt + (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK)) GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, - "setsockopt"); + "setsockopt"); #endif if (GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) sa, addr_len) != GNUNET_OK) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Unable to bind listening socket for service `%s' to address `%s': %s\n"), - serviceName, - GNUNET_a2s (sa, addr_len), - STRERROR (errno)); - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); - GNUNET_free (sa); - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _ + ("Unable to bind listening socket for service `%s' to address `%s': %s\n"), + serviceName, GNUNET_a2s (sa, addr_len), STRERROR (errno)); + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); + GNUNET_free (sa); + return; + } if (GNUNET_NETWORK_socket_listen (sock, 5) != GNUNET_OK) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, - "listen"); - GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); - GNUNET_free (sa); - return; - } + { + GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen"); + GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); + GNUNET_free (sa); + return; + } GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("ARM now monitors connections to service `%s' at `%s'\n"), - serviceName, - GNUNET_a2s (sa, addr_len)); + _("ARM now monitors connections to service `%s' at `%s'\n"), + serviceName, GNUNET_a2s (sa, addr_len)); serviceListeningInfo = GNUNET_malloc (sizeof (struct ServiceListeningInfo)); serviceListeningInfo->serviceName = GNUNET_strdup (serviceName); serviceListeningInfo->service_addr = sa; serviceListeningInfo->service_addr_len = addr_len; serviceListeningInfo->listeningSocket = sock; serviceListeningInfo->acceptTask = - GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, sock, - &acceptConnection, - serviceListeningInfo); + GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, sock, + &acceptConnection, serviceListeningInfo); GNUNET_CONTAINER_DLL_insert (serviceListeningInfoList_head, - serviceListeningInfoList_tail, - serviceListeningInfo); + serviceListeningInfoList_tail, + serviceListeningInfo); } @@ -1204,22 +1146,20 @@ createListeningSocket(struct sockaddr *sa, socklen_t addr_len, */ static void checkPortNumberCB (void *cls, - const char *section, - const char *option, - const char *value) + const char *section, const char *option, const char *value) { struct sockaddr **addrs; socklen_t *addr_lens; int ret; unsigned int i; - - if ( (strcasecmp (section, "arm") == 0) || - (strcasecmp (option, "AUTOSTART") != 0) || - (strcasecmp (value, "YES") != 0) || - (isInDefaultList (section) == GNUNET_YES) ) + + if ((strcasecmp (section, "arm") == 0) || + (strcasecmp (option, "AUTOSTART") != 0) || + (strcasecmp (value, "YES") != 0) || + (isInDefaultList (section) == GNUNET_YES)) return; if (0 >= (ret = GNUNET_SERVICE_get_server_addresses (section, cfg, &addrs, - &addr_lens))) + &addr_lens))) return; /* this will free (or capture) addrs[i] */ for (i = 0; i < ret; i++) @@ -1235,8 +1175,7 @@ checkPortNumberCB (void *cls, * @param configurationHandle configuration to use to get services */ void -prepareServices (const struct GNUNET_CONFIGURATION_Handle - *configurationHandle) +prepareServices (const struct GNUNET_CONFIGURATION_Handle *configurationHandle) { char *defaultServicesString; @@ -1244,13 +1183,13 @@ prepareServices (const struct GNUNET_CONFIGURATION_Handle /* Split the default services into a list */ if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "arm", "DEFAULTSERVICES", - &defaultServicesString)) - { - addDefaultServicesToList (defaultServicesString); - GNUNET_free (defaultServicesString); - } + &defaultServicesString)) + { + addDefaultServicesToList (defaultServicesString); + GNUNET_free (defaultServicesString); + } /* Spot the services from the configuration and create a listening - socket for each */ + * socket for each */ GNUNET_CONFIGURATION_iterate (cfg, &checkPortNumberCB, NULL); } diff --git a/src/arm/mockup-service.c b/src/arm/mockup-service.c index 4dae8ffb4..0a0e1de5a 100644 --- a/src/arm/mockup-service.c +++ b/src/arm/mockup-service.c @@ -36,21 +36,20 @@ transmit_shutdown_ack (void *cls, size_t size, void *buf) struct GNUNET_MessageHeader *msg; if (size < sizeof (struct GNUNET_MessageHeader)) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Failed to transmit shutdown ACK.\n")); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return 0; /* client disconnected */ - } + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("Failed to transmit shutdown ACK.\n")); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return 0; /* client disconnected */ + } - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Transmitting shutdown ACK.\n")); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transmitting shutdown ACK.\n")); msg = (struct GNUNET_MessageHeader *) buf; msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK); msg->size = htons (sizeof (struct GNUNET_MessageHeader)); GNUNET_SERVER_receive_done (client, GNUNET_OK); - GNUNET_SERVER_client_drop(client); + GNUNET_SERVER_client_drop (client); return sizeof (struct GNUNET_MessageHeader); } @@ -66,12 +65,12 @@ handle_shutdown (void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message) { - GNUNET_SERVER_client_keep(client); + GNUNET_SERVER_client_keep (client); GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Initiating shutdown as requested by client.\n")); GNUNET_SERVER_notify_transmit_ready (client, - sizeof(struct GNUNET_MessageHeader), + sizeof (struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, &transmit_shutdown_ack, client); GNUNET_SERVER_client_persist_ (client); @@ -95,14 +94,15 @@ run (void *cls, } -int main(int argc, char *const *argv) +int +main (int argc, char *const *argv) { int ret; - + ret = (GNUNET_OK == - GNUNET_SERVICE_run (argc, + GNUNET_SERVICE_run (argc, argv, "do-nothing", GNUNET_SERVICE_OPTION_NONE, - &run, NULL)) ? 0 : 1; + &run, NULL)) ? 0 : 1; return ret; } diff --git a/src/arm/test_arm_api.c b/src/arm/test_arm_api.c index cce5867f1..1e9d84202 100644 --- a/src/arm/test_arm_api.c +++ b/src/arm/test_arm_api.c @@ -47,7 +47,7 @@ static int ok = 1; static void arm_stopped (void *cls, int success) { - if (success != GNUNET_NO) + if (success != GNUNET_NO) ok = 3; else if (ok == 1) ok = 0; @@ -67,15 +67,15 @@ static void dns_notify (void *cls, const struct sockaddr *addr, socklen_t addrlen) { if (addr == NULL) + { + if (ok != 0) { - if (ok != 0) - { - GNUNET_break (0); - ok = 2; - } - GNUNET_ARM_stop_service (arm, "resolver", TIMEOUT, &arm_notify_stop, NULL); - return; + GNUNET_break (0); + ok = 2; } + GNUNET_ARM_stop_service (arm, "resolver", TIMEOUT, &arm_notify_stop, NULL); + return; + } GNUNET_assert (addr != NULL); ok = 0; } @@ -85,14 +85,14 @@ static void resolver_notify (void *cls, int success) { if (success != GNUNET_YES) - { - GNUNET_break (0); - ok = 2; + { + GNUNET_break (0); + ok = 2; #if START_ARM - GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL); + GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL); #endif - return; - } + return; + } GNUNET_RESOLVER_ip_get ("localhost", AF_INET, TIMEOUT, &dns_notify, NULL); } @@ -101,22 +101,22 @@ static void arm_notify (void *cls, int success) { if (success != GNUNET_YES) - { - GNUNET_break (0); - ok = 2; + { + GNUNET_break (0); + ok = 2; #if START_ARM - GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL); + GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL); #endif - } - GNUNET_ARM_start_service (arm, "resolver", START_TIMEOUT, &resolver_notify, NULL); + } + GNUNET_ARM_start_service (arm, "resolver", START_TIMEOUT, &resolver_notify, + NULL); } static void task (void *cls, char *const *args, - const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *c) + const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c) { cfg = c; arm = GNUNET_ARM_connect (cfg, NULL); diff --git a/src/arm/test_exponential_backoff.c b/src/arm/test_exponential_backoff.c index f40252a03..eb6421d5f 100644 --- a/src/arm/test_exponential_backoff.c +++ b/src/arm/test_exponential_backoff.c @@ -102,58 +102,55 @@ service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg) { struct ShutdownContext *shutdown_ctx = cls; - if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES)) - { - /* Means the other side closed the connection and never confirmed a shutdown */ - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Service handle shutdown before ACK!\n"); - if (shutdown_ctx->cont != NULL) - shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR); - GNUNET_SCHEDULER_cancel(shutdown_ctx->cancel_task); - GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); - GNUNET_free(shutdown_ctx); - } + if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES)) + { + /* Means the other side closed the connection and never confirmed a shutdown */ + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Service handle shutdown before ACK!\n"); + if (shutdown_ctx->cont != NULL) + shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR); + GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task); + GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); + GNUNET_free (shutdown_ctx); + } else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES)) - { + { #if VERBOSE - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "Service shutdown complete.\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n"); #endif - if (shutdown_ctx->cont != NULL) - shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_NO); + if (shutdown_ctx->cont != NULL) + shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_NO); - GNUNET_SCHEDULER_cancel(shutdown_ctx->cancel_task); - GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); - GNUNET_free(shutdown_ctx); - } + GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task); + GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); + GNUNET_free (shutdown_ctx); + } else + { + GNUNET_assert (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader)); + switch (ntohs (msg->type)) { - GNUNET_assert(ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader)); - switch (ntohs(msg->type)) - { - case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK: + case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK: #if VERBOSE - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "Received confirmation for service shutdown.\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received confirmation for service shutdown.\n"); #endif - shutdown_ctx->confirmed = GNUNET_YES; - GNUNET_CLIENT_receive (shutdown_ctx->sock, - &service_shutdown_handler, - shutdown_ctx, - GNUNET_TIME_UNIT_FOREVER_REL); - break; - default: /* Fall through */ - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - "Service shutdown refused!\n"); - if (shutdown_ctx->cont != NULL) - shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_YES); - - GNUNET_SCHEDULER_cancel(shutdown_ctx->cancel_task); - GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); - GNUNET_free(shutdown_ctx); - break; - } + shutdown_ctx->confirmed = GNUNET_YES; + GNUNET_CLIENT_receive (shutdown_ctx->sock, + &service_shutdown_handler, + shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL); + break; + default: /* Fall through */ + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Service shutdown refused!\n"); + if (shutdown_ctx->cont != NULL) + shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_YES); + + GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task); + GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); + GNUNET_free (shutdown_ctx); + break; } + } } /** @@ -162,14 +159,16 @@ service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg) * @param cls closure * @param tc context information (why was this task triggered now) */ -void service_shutdown_cancel (void *cls, - const struct GNUNET_SCHEDULER_TaskContext * tc) +void +service_shutdown_cancel (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) { struct ShutdownContext *shutdown_ctx = cls; - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n"); - shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR); + + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n"); + shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR); GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); - GNUNET_free(shutdown_ctx); + GNUNET_free (shutdown_ctx); } @@ -189,21 +188,22 @@ write_shutdown (void *cls, size_t size, void *buf) struct ShutdownContext *shutdown_ctx = cls; if (size < sizeof (struct GNUNET_MessageHeader)) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Failed to transmit shutdown request to client.\n")); - shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR); - GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); - GNUNET_free(shutdown_ctx); - return 0; /* client disconnected */ - } + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _("Failed to transmit shutdown request to client.\n")); + shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR); + GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); + GNUNET_free (shutdown_ctx); + return 0; /* client disconnected */ + } GNUNET_CLIENT_receive (shutdown_ctx->sock, - &service_shutdown_handler, shutdown_ctx, - GNUNET_TIME_UNIT_FOREVER_REL); - shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining(shutdown_ctx->timeout), - &service_shutdown_cancel, - shutdown_ctx); + &service_shutdown_handler, shutdown_ctx, + GNUNET_TIME_UNIT_FOREVER_REL); + shutdown_ctx->cancel_task = + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining + (shutdown_ctx->timeout), + &service_shutdown_cancel, shutdown_ctx); msg = (struct GNUNET_MessageHeader *) buf; msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN); msg->size = htons (sizeof (struct GNUNET_MessageHeader)); @@ -226,22 +226,22 @@ write_shutdown (void *cls, size_t size, void *buf) */ static void arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock, - struct GNUNET_TIME_Relative timeout, - GNUNET_CLIENT_ShutdownTask cont, - void *cont_cls) + struct GNUNET_TIME_Relative timeout, + GNUNET_CLIENT_ShutdownTask cont, void *cont_cls) { struct ShutdownContext *shutdown_ctx; - shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext)); + + shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext)); shutdown_ctx->cont = cont; shutdown_ctx->cont_cls = cont_cls; shutdown_ctx->sock = sock; - shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute(timeout); + shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout); GNUNET_CLIENT_notify_transmit_ready (sock, - sizeof (struct - GNUNET_MessageHeader), - timeout, - GNUNET_NO, - &write_shutdown, shutdown_ctx); + sizeof (struct + GNUNET_MessageHeader), + timeout, + GNUNET_NO, + &write_shutdown, shutdown_ctx); } @@ -256,8 +256,7 @@ arm_notify_stop (void *cls, int success) static void -kill_task (void *cbData, - const struct GNUNET_SCHEDULER_TaskContext *tc); +kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc); static void @@ -265,65 +264,55 @@ do_nothing_notify (void *cls, int success) { GNUNET_assert (success == GNUNET_YES); ok = 1; - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, - &kill_task, NULL); + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL); } static void arm_notify (void *cls, int success) -{ +{ GNUNET_assert (success == GNUNET_YES); - GNUNET_ARM_start_service (arm, - "do-nothing", TIMEOUT, - &do_nothing_notify, NULL); + GNUNET_ARM_start_service (arm, + "do-nothing", TIMEOUT, &do_nothing_notify, NULL); } static void -kill_task (void *cbData, - const struct GNUNET_SCHEDULER_TaskContext *tc); +kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc); static void do_nothing_restarted_notify_task (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) -{ + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ static char a; - + trialCount++; -#if LOG_BACKOFF - if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) - { - fprintf(killLogFilePtr, - "%d.Reason is shutdown!\n", - trialCount); - } - else if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0) - { - fprintf(killLogFilePtr, - "%d.Reason is timeout!\n", - trialCount); - } - else if ((tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE) != 0) - { - fprintf(killLogFilePtr, - "%d.Service is running!\n", - trialCount); - } +#if LOG_BACKOFF + if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) + { + fprintf (killLogFilePtr, "%d.Reason is shutdown!\n", trialCount); + } + else if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0) + { + fprintf (killLogFilePtr, "%d.Reason is timeout!\n", trialCount); + } + else if ((tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE) != 0) + { + fprintf (killLogFilePtr, "%d.Service is running!\n", trialCount); + } #endif GNUNET_SCHEDULER_add_now (&kill_task, &a); } static void -do_test (void *cbData, - const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - GNUNET_CLIENT_service_test("do-nothing", - cfg, TIMEOUT, - &do_nothing_restarted_notify_task, NULL); +do_test (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + GNUNET_CLIENT_service_test ("do-nothing", + cfg, TIMEOUT, + &do_nothing_restarted_notify_task, NULL); } @@ -331,63 +320,56 @@ static void shutdown_cont (void *cls, int reason) { trialCount++; - startedWaitingAt = GNUNET_TIME_absolute_get(); - GNUNET_SCHEDULER_add_delayed (waitedFor, - &do_test, - NULL); + startedWaitingAt = GNUNET_TIME_absolute_get (); + GNUNET_SCHEDULER_add_delayed (waitedFor, &do_test, NULL); } static void -kill_task (void *cbData, - const struct GNUNET_SCHEDULER_TaskContext *tc) +kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc) { - static struct GNUNET_CLIENT_Connection * doNothingConnection = NULL; + static struct GNUNET_CLIENT_Connection *doNothingConnection = NULL; + + if (NULL != cbData) + { + waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt); - if (NULL != cbData) - { - waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt); - #if LOG_BACKOFF - fprintf(killLogFilePtr, - "Waited for: %llu ms\n", - (unsigned long long) waitedFor.rel_value); + fprintf (killLogFilePtr, + "Waited for: %llu ms\n", (unsigned long long) waitedFor.rel_value); #endif - } + } else - { - waitedFor.rel_value = 0; - } + { + waitedFor.rel_value = 0; + } /* Connect to the doNothing task */ doNothingConnection = GNUNET_CLIENT_connect ("do-nothing", cfg); GNUNET_assert (doNothingConnection != NULL); - if (trialCount == 12) { + if (trialCount == 12) + { GNUNET_CLIENT_disconnect (doNothingConnection, GNUNET_NO); - GNUNET_ARM_stop_service (arm, - "do-nothing", - TIMEOUT, - &arm_notify_stop, NULL); + GNUNET_ARM_stop_service (arm, + "do-nothing", TIMEOUT, &arm_notify_stop, NULL); ok = 0; return; } /* Use the created connection to kill the doNothingTask */ - arm_service_shutdown(doNothingConnection, - TIMEOUT, - &shutdown_cont, NULL); + arm_service_shutdown (doNothingConnection, TIMEOUT, &shutdown_cont, NULL); } - + static void task (void *cls, char *const *args, - const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *c) + const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c) { cfg = c; - - arm = GNUNET_ARM_connect (cfg,NULL); + + arm = GNUNET_ARM_connect (cfg, NULL); #if START_ARM - GNUNET_ARM_start_service (arm, "arm", GNUNET_TIME_UNIT_ZERO, &arm_notify, NULL); + GNUNET_ARM_start_service (arm, "arm", GNUNET_TIME_UNIT_ZERO, &arm_notify, + NULL); #else arm_do_nothing (NULL, GNUNET_YES); #endif @@ -407,39 +389,41 @@ check () struct GNUNET_GETOPT_CommandLineOption options[] = { GNUNET_GETOPT_OPTION_END }; - + /* Running ARM and running the do_nothing task */ GNUNET_assert (GNUNET_OK == GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-exponential-backoff", "nohelp", options, &task, NULL)); - - + + return ok; } static int -init() +init () { #if LOG_BACKOFF - killLogFileName = GNUNET_DISK_mktemp("exponential-backoff-waiting.log"); - if (NULL == (killLogFilePtr = FOPEN(killLogFileName, "w"))) { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", killLogFileName); + killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log"); + if (NULL == (killLogFilePtr = FOPEN (killLogFileName, "w"))) + { + GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", + killLogFileName); GNUNET_free (killLogFileName); return GNUNET_SYSERR; - } + } #endif return GNUNET_OK; } static void -houseKeep() +houseKeep () { #if LOG_BACKOFF GNUNET_assert (0 == fclose (killLogFilePtr)); - GNUNET_free(killLogFileName); + GNUNET_free (killLogFileName); #endif } @@ -456,10 +440,10 @@ main (int argc, char *argv[]) "WARNING", #endif NULL); - - init(); + + init (); ret = check (); - houseKeep(); + houseKeep (); return ret; } diff --git a/src/arm/test_gnunet_service_manager.c b/src/arm/test_gnunet_service_manager.c index 087e01ccb..4c4de9242 100644 --- a/src/arm/test_gnunet_service_manager.c +++ b/src/arm/test_gnunet_service_manager.c @@ -53,41 +53,36 @@ static struct GNUNET_ARM_Handle *arm; static void arm_stopped (void *cls, int success) { - if (success != GNUNET_NO) - { - GNUNET_break (0); - ret = 4; - } + if (success != GNUNET_NO) + { + GNUNET_break (0); + ret = 4; + } else - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "ARM stopped\n"); - } + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM stopped\n"); + } #if START_ARM GNUNET_ARM_disconnect (arm); arm = NULL; #endif } -static void -hostNameResolveCB(void *cls, - const struct sockaddr *addr, - socklen_t addrlen) +static void +hostNameResolveCB (void *cls, const struct sockaddr *addr, socklen_t addrlen) { - if ( (ret == 0) || (ret == 4) ) + if ((ret == 0) || (ret == 4)) return; if (NULL == addr) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Name not resolved!\n"); + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Name not resolved!\n"); #if START_ARM - GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL); + GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL); #endif - ret = 3; - return; - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Resolved hostname, now stopping ARM\n"); + ret = 3; + return; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Resolved hostname, now stopping ARM\n"); ret = 0; #if START_ARM GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL); @@ -99,34 +94,31 @@ static void arm_notify (void *cls, int success) { if (success != GNUNET_YES) - { - GNUNET_break (0); - ret = 1; - return; - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Trying to resolve our own hostname!\n"); + { + GNUNET_break (0); + ret = 1; + return; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to resolve our own hostname!\n"); /* connect to the resolver service */ if (NULL == GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, - TIMEOUT, - &hostNameResolveCB, - NULL)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Unable initiate connection to resolver service\n"); - ret = 2; + TIMEOUT, + &hostNameResolveCB, NULL)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unable initiate connection to resolver service\n"); + ret = 2; #if START_ARM - GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL); + GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL); #endif - } + } } static void -run(void *cls, - char * const *args, - const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *c) +run (void *cls, + char *const *args, + const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c) { cfg = c; #if START_ARM @@ -139,7 +131,7 @@ run(void *cls, static void -check() +check () { char *const argv[] = { "test-gnunet-service-manager", @@ -153,39 +145,40 @@ check() GNUNET_GETOPT_OPTION_END }; GNUNET_assert (GNUNET_OK == - GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, - argv, - "test-gnunet-service-manager", - "nohelp", options, &run, NULL)); + GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, + argv, + "test-gnunet-service-manager", + "nohelp", options, &run, NULL)); } int main (int argc, char *argv[]) { - char hostname[GNUNET_OS_get_hostname_max_length() + 1]; + char hostname[GNUNET_OS_get_hostname_max_length () + 1]; if (0 != gethostname (hostname, sizeof (hostname) - 1)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | - GNUNET_ERROR_TYPE_BULK, "gethostname"); - fprintf (stderr, "Failed to determine my own hostname, testcase not run.\n"); - return 0; - } + { + GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | + GNUNET_ERROR_TYPE_BULK, "gethostname"); + fprintf (stderr, + "Failed to determine my own hostname, testcase not run.\n"); + return 0; + } if (NULL == gethostbyname (hostname)) - { - fprintf (stderr, "Failed to resolve my hostname `%s', testcase not run.\n", - hostname); - return 0; - } + { + fprintf (stderr, "Failed to resolve my hostname `%s', testcase not run.\n", + hostname); + return 0; + } - GNUNET_log_setup("test-gnunet-service-manager", + GNUNET_log_setup ("test-gnunet-service-manager", #if VERBOSE - "DEBUG", + "DEBUG", #else - "WARNING", + "WARNING", #endif - NULL); - check(); + NULL); + check (); return ret; } -- cgit v1.2.3