#include "gnunet_server_lib.h"
#include "gnunet_service_lib.h"
+#if HAVE_MALLINFO
+#include <malloc.h>
+#include "gauger.h"
+#endif
+
+
#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
};
/**
+
* @brief network in CIDR notation for IPV6.
*/
struct IPv6NetworkSet
};
+int
+GNUNET_SPEEDUP_start_ (const struct GNUNET_CONFIGURATION_Handle *cfg);
+
+int
+GNUNET_SPEEDUP_stop_ (void);
+
+
/**
* Parse a network specification. The argument specifies
* a list of networks. The format is
/**
* Name of our service.
*/
- const char *serviceName;
+ const char *service_name;
/**
* Main service-specific task to run.
*/
struct GNUNET_NETWORK_Handle **lsocks;
+ /**
+ * Task ID of the shutdown task.
+ */
+ GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
+
/**
* Idle timeout for server.
*/
/* ****************** message handlers ****************** */
/**
+ * Send a 'TEST' message back to the client.
*
- * @param cls
- * @param size
- * @param buf
- * @return
+ * @param cls the 'struct GNUNET_SERVER_Client' to send TEST to
+ * @param size number of bytes available in 'buf'
+ * @param buf where to copy the message
+ * @return number of bytes written to 'buf'
*/
static size_t
write_test (void *cls, size_t size, void *buf)
if (GNUNET_YES == sctx->match_uid)
{
/* UID match required */
- ret = (NULL != uc) && (uc->uid == geteuid ());
+ ret = (NULL != uc) && ( (0 == uc->uid) || (uc->uid == geteuid ()) );
}
else if ( (GNUNET_YES == sctx->match_gid) &&
- ( (NULL == uc) || (uc->uid != geteuid ()) ) )
+ ( (NULL == uc) ||
+ ( (0 != uc->uid) &&
+ (uc->uid != geteuid ()) ) ) )
{
/* group match required and UID does not match */
if (NULL == uc)
LOG (GNUNET_ERROR_TYPE_WARNING,
_("Access from `%s' denied to service `%s'\n"),
GNUNET_a2s (addr, addrlen),
- sctx->serviceName);
+ sctx->service_name);
}
return ret;
}
char *pif;
if (GNUNET_OK !=
- GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->serviceName,
+ GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->service_name,
"PIDFILE", &pif))
return NULL;
return pif;
/**
* Parse an IPv4 access control list.
*
- * @param ret
- * @param sctx
- * @param option
- * @return
+ * @param ret location where to write the ACL (set)
+ * @param sctx service context to use to get the configuration
+ * @param option name of the ACL option to parse
+ * @return GNUNET_SYSERR on parse error, GNUNET_OK on success (including
+ * no ACL configured)
*/
static int
process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
{
char *opt;
- if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
+ if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
+ {
+ *ret = NULL;
return GNUNET_OK;
+ }
GNUNET_break (GNUNET_OK ==
GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
- sctx->serviceName,
+ sctx->service_name,
option, &opt));
if (NULL == (*ret = parse_ipv4_specification (opt)))
{
LOG (GNUNET_ERROR_TYPE_WARNING,
_("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
- opt, sctx->serviceName, option);
+ opt, sctx->service_name, option);
GNUNET_free (opt);
return GNUNET_SYSERR;
}
/**
* Parse an IPv6 access control list.
*
- * @param ret
- * @param sctx
- * @param option
- * @return
+ * @param ret location where to write the ACL (set)
+ * @param sctx service context to use to get the configuration
+ * @param option name of the ACL option to parse
+ * @return GNUNET_SYSERR on parse error, GNUNET_OK on success (including
+ * no ACL configured)
*/
static int
process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
{
char *opt;
- if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
+ if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
+ {
+ *ret = NULL;
return GNUNET_OK;
+ }
GNUNET_break (GNUNET_OK ==
GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
- sctx->serviceName,
+ sctx->service_name,
option, &opt));
if (NULL == (*ret = parse_ipv6_specification (opt)))
{
LOG (GNUNET_ERROR_TYPE_WARNING,
_("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
- opt, sctx->serviceName, option);
+ opt, sctx->service_name, option);
GNUNET_free (opt);
return GNUNET_SYSERR;
}
* Get the list of addresses that a server for the given service
* should bind to.
*
- * @param serviceName name of the service
+ * @param service_name name of the service
* @param cfg configuration (which specifies the addresses)
* @param addrs set (call by reference) to an array of pointers to the
* addresses the server should bind to and listen on; the
* set to NULL).
*/
int
-GNUNET_SERVICE_get_server_addresses (const char *serviceName,
+GNUNET_SERVICE_get_server_addresses (const char *service_name,
const struct GNUNET_CONFIGURATION_Handle
*cfg, struct sockaddr ***addrs,
socklen_t ** addr_lens)
*addrs = NULL;
*addr_lens = NULL;
desc = NULL;
- if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "DISABLEV6"))
+ if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "DISABLEV6"))
{
if (GNUNET_SYSERR ==
(disablev6 =
- GNUNET_CONFIGURATION_get_value_yesno (cfg, serviceName, "DISABLEV6")))
+ GNUNET_CONFIGURATION_get_value_yesno (cfg, service_name, "DISABLEV6")))
return GNUNET_SYSERR;
}
else
LOG (GNUNET_ERROR_TYPE_INFO,
_
("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
- serviceName, STRERROR (errno));
+ service_name, STRERROR (errno));
disablev6 = GNUNET_YES;
}
else
}
port = 0;
- if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
+ if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
{
- GNUNET_break (GNUNET_OK ==
- GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
- "PORT", &port));
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
+ "PORT", &port))
+ {
+ LOG (GNUNET_ERROR_TYPE_ERROR,
+ _("Require valid port number for service `%s' in configuration!\n"),
+ service_name);
+ }
if (port > 65535)
{
LOG (GNUNET_ERROR_TYPE_ERROR,
_("Require valid port number for service `%s' in configuration!\n"),
- serviceName);
+ service_name);
return GNUNET_SYSERR;
}
}
- if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
+ if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
{
GNUNET_break (GNUNET_OK ==
- GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
+ GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
"BINDTO", &hostname));
}
else
unixpath = NULL;
#ifdef AF_UNIX
if ((GNUNET_YES ==
- GNUNET_CONFIGURATION_have_value (cfg, serviceName, "UNIXPATH")) &&
+ GNUNET_CONFIGURATION_have_value (cfg, service_name, "UNIXPATH")) &&
(GNUNET_OK ==
- GNUNET_CONFIGURATION_get_value_string (cfg, serviceName, "UNIXPATH",
+ GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH",
&unixpath)) &&
(0 < strlen (unixpath)))
{
{
LOG (GNUNET_ERROR_TYPE_WARNING,
_("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
- sizeof (s_un.sun_path));
- GNUNET_free_non_null (hostname);
- GNUNET_free (unixpath);
- return GNUNET_SYSERR;
- }
+ (unsigned long long) sizeof (s_un.sun_path));
+ unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
+ LOG (GNUNET_ERROR_TYPE_INFO,
+ _("Using `%s' instead\n"), unixpath);
+ }
+ }
+ if (NULL != unixpath)
+ {
desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
if (NULL == desc)
{
LOG (GNUNET_ERROR_TYPE_INFO,
_
("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
- serviceName, STRERROR (errno));
+ service_name, STRERROR (errno));
GNUNET_free (unixpath);
unixpath = NULL;
}
LOG (GNUNET_ERROR_TYPE_ERROR,
_
("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
- serviceName);
+ service_name);
GNUNET_free_non_null (hostname);
return GNUNET_SYSERR;
}
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Resolving `%s' since that is where `%s' will bind to.\n", hostname,
- serviceName);
+ service_name);
memset (&hints, 0, sizeof (struct addrinfo));
if (disablev6)
hints.ai_family = AF_INET;
+ hints.ai_protocol = IPPROTO_TCP;
if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
(res == NULL))
{
if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
continue; /* huh? */
LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
- serviceName, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
+ service_name, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
if (AF_INET == pos->ai_family)
{
GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
#ifdef MINGW
/**
+ * Read listen sockets from the parent process (ARM).
*
- *
- * @param sctx
+ * @param sctx service context to initialize
* @return GNUNET_YES if ok, GNUNET_NO if not ok (must bind yourself),
* and GNUNET_SYSERR on error.
*/
*/
lsocks_pipe = (HANDLE) strtoul (env_buf, NULL, 10);
if ( (0 == lsocks_pipe) || (INVALID_HANDLE_VALUE == lsocks_pipe))
- return GNUNET_NO
+ return GNUNET_NO;
fail = 1;
do
{
* - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
* - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
*
- * @param sctx
+ * @param sctx service context to initialize
* @return GNUNET_OK if configuration succeeded
*/
static int
int flags;
#endif
- if (GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, "TIMEOUT"))
+ if (GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, "TIMEOUT"))
{
if (GNUNET_OK !=
- GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->serviceName,
+ GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->service_name,
"TIMEOUT", &idleout))
{
LOG (GNUNET_ERROR_TYPE_ERROR,
_("Specified value for `%s' of service `%s' is invalid\n"),
- "TIMEOUT", sctx->serviceName);
+ "TIMEOUT", sctx->service_name);
return GNUNET_SYSERR;
}
sctx->timeout = idleout;
sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
if (GNUNET_CONFIGURATION_have_value
- (sctx->cfg, sctx->serviceName, "TOLERANT"))
+ (sctx->cfg, sctx->service_name, "TOLERANT"))
{
if (GNUNET_SYSERR ==
(tolerant =
- GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
+ GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
"TOLERANT")))
{
LOG (GNUNET_ERROR_TYPE_ERROR,
_("Specified value for `%s' of service `%s' is invalid\n"),
- "TOLERANT", sctx->serviceName);
+ "TOLERANT", sctx->service_name);
return GNUNET_SYSERR;
}
}
if ((NULL == sctx->lsocks) &&
(GNUNET_SYSERR ==
- GNUNET_SERVICE_get_server_addresses (sctx->serviceName, sctx->cfg,
+ GNUNET_SERVICE_get_server_addresses (sctx->service_name, sctx->cfg,
&sctx->addrs, &sctx->addrlens)))
return GNUNET_SYSERR;
sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
sctx->match_uid =
- GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
+ GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
"UNIX_MATCH_UID");
sctx->match_gid =
- GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
+ GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
"UNIX_MATCH_GID");
process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
* Get the name of the user that'll be used
* to provide the service.
*
- * @param sctx
- * @return
+ * @param sctx service context
+ * @return value of the 'USERNAME' option
*/
static char *
get_user_name (struct GNUNET_SERVICE_Context *sctx)
char *un;
if (GNUNET_OK !=
- GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->serviceName,
+ GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->service_name,
"USERNAME", &un))
return NULL;
return un;
/**
* Write PID file.
*
- * @param sctx
- * @param pid
- * @return
+ * @param sctx service context
+ * @param pid PID to write (should be equal to 'getpid()'
+ * @return GNUNET_OK on success (including no work to be done)
*/
static int
write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
/**
- * Task run during shutdown.
+ * Task run during shutdown. Stops the server/service.
*
- * @param cls unused
+ * @param cls the 'struct GNUNET_SERVICE_Context'
* @param tc unused
*/
static void
shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
- struct GNUNET_SERVER_Handle *server = cls;
+ struct GNUNET_SERVICE_Context *service = cls;
+ struct GNUNET_SERVER_Handle *server = service->server;
- // FIXME: we should not unconditionally destroy the server
- // here (often only stopping 'listening' would be better)
- GNUNET_SERVER_destroy (server);
+ service->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
+ if (0 != (service->options & GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN))
+ GNUNET_SERVER_stop_listening (server);
+ else
+ GNUNET_SERVER_destroy (server);
}
while (NULL != sctx->addrs[i])
{
LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to start `%s' at `%s'\n"),
- sctx->serviceName, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
+ sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
i++;
}
}
{
/* install a task that will kill the server
* process if the scheduler ever gets a shutdown signal */
- GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
- sctx->server);
+ sctx->shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
+ sctx);
}
sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
while (NULL != sctx->addrs[i])
{
LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"),
- sctx->serviceName, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
+ sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
i++;
}
}
* Detach from terminal.
*
* @param sctx service context
- * @return
+ * @return GNUNET_OK on success, GNUNET_SYSERR on error
*/
static int
detach_terminal (struct GNUNET_SERVICE_Context *sctx)
* Set user ID.
*
* @param sctx service context
- * @return
+ * @return GNUNET_OK on success, GNUNET_SYSERR on error
*/
static int
set_user_id (struct GNUNET_SERVICE_Context *sctx)
*
* @param argc number of command line arguments
* @param argv command line arguments
- * @param serviceName our service name
- * @param opt service options
+ * @param service_name our service name
+ * @param options service options
* @param task main task of the service
* @param task_cls closure for task
* @return GNUNET_SYSERR on error, GNUNET_OK
* if we shutdown nicely
*/
int
-GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName,
- enum GNUNET_SERVICE_Options opt, GNUNET_SERVICE_Main task,
+GNUNET_SERVICE_run (int argc, char *const *argv, const char *service_name,
+ enum GNUNET_SERVICE_Options options, GNUNET_SERVICE_Main task,
void *task_cls)
{
#define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0)
int err;
+ int ret;
char *cfg_fn;
char *loglev;
char *logfile;
loglev = NULL;
cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
memset (&sctx, 0, sizeof (sctx));
- sctx.options = opt;
+ sctx.options = options;
sctx.ready_confirm_fd = -1;
sctx.ret = GNUNET_OK;
sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
sctx.task = task;
sctx.task_cls = task_cls;
- sctx.serviceName = serviceName;
+ sctx.service_name = service_name;
sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
+
/* setup subsystems */
- if (GNUNET_SYSERR ==
- GNUNET_GETOPT_run (serviceName, service_options, argc, argv))
+ ret = GNUNET_GETOPT_run (service_name, service_options, argc, argv);
+ if (GNUNET_SYSERR == ret)
goto shutdown;
- if (GNUNET_OK != GNUNET_log_setup (serviceName, loglev, logfile))
+ if (GNUNET_NO == ret)
+ {
+ err = 0;
+ goto shutdown;
+ }
+ if (GNUNET_OK != GNUNET_log_setup (service_name, loglev, logfile))
HANDLE_ERROR;
if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_fn))
goto shutdown;
if (GNUNET_OK != set_user_id (&sctx))
goto shutdown;
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Service `%s' runs with configuration from `%s'\n", serviceName, cfg_fn);
+ "Service `%s' runs with configuration from `%s'\n", service_name, cfg_fn);
if ((GNUNET_OK ==
GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
"SKEW_OFFSET", &skew_offset)) &&
/* actually run service */
err = 0;
GNUNET_SCHEDULER_run (&service_task, &sctx);
-
+ GNUNET_SPEEDUP_start_ (cfg);
/* shutdown */
if ((1 == do_daemonize) && (NULL != sctx.server))
pid_file_delete (&sctx);
LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
}
-
+#if HAVE_MALLINFO
+ {
+ char *counter;
+
+ if ( (GNUNET_YES ==
+ GNUNET_CONFIGURATION_have_value (sctx.cfg, service_name,
+ "GAUGER_HEAP")) &&
+ (GNUNET_OK ==
+ GNUNET_CONFIGURATION_get_value_string (sctx.cfg, service_name,
+ "GAUGER_HEAP",
+ &counter)) )
+ {
+ struct mallinfo mi;
+
+ mi = mallinfo ();
+ GAUGER (service_name, counter, mi.usmblks, "blocks");
+ GNUNET_free (counter);
+ }
+ }
+#endif
+ GNUNET_SPEEDUP_stop_ ();
GNUNET_CONFIGURATION_destroy (cfg);
i = 0;
if (NULL != sctx.addrs)
* Run a service startup sequence within an existing
* initialized system.
*
- * @param serviceName our service name
+ * @param service_name our service name
* @param cfg configuration to use
+ * @param options service options
* @return NULL on error, service handle
*/
struct GNUNET_SERVICE_Context *
-GNUNET_SERVICE_start (const char *serviceName,
- const struct GNUNET_CONFIGURATION_Handle *cfg)
+GNUNET_SERVICE_start (const char *service_name,
+ const struct GNUNET_CONFIGURATION_Handle *cfg,
+ enum GNUNET_SERVICE_Options options)
{
int i;
struct GNUNET_SERVICE_Context *sctx;
sctx->ready_confirm_fd = -1; /* no daemonizing */
sctx->ret = GNUNET_OK;
sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
- sctx->serviceName = serviceName;
+ sctx->service_name = service_name;
sctx->cfg = cfg;
+ sctx->options = options;
/* setup subsystems */
if (GNUNET_OK != setup_service (sctx))
{
unsigned int i;
+#if HAVE_MALLINFO
+ {
+ char *counter;
+
+ if ( (GNUNET_YES ==
+ GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name,
+ "GAUGER_HEAP")) &&
+ (GNUNET_OK ==
+ GNUNET_CONFIGURATION_get_value_string (sctx->cfg, sctx->service_name,
+ "GAUGER_HEAP",
+ &counter)) )
+ {
+ struct mallinfo mi;
+
+ mi = mallinfo ();
+ GAUGER (sctx->service_name, counter, mi.usmblks, "blocks");
+ GNUNET_free (counter);
+ }
+ }
+#endif
+ if (GNUNET_SCHEDULER_NO_TASK != sctx->shutdown_task)
+ {
+ GNUNET_SCHEDULER_cancel (sctx->shutdown_task);
+ sctx->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
+ }
if (NULL != sctx->server)
GNUNET_SERVER_destroy (sctx->server);
GNUNET_free_non_null (sctx->my_handlers);