GNUnet  0.11.x
gnunet-service-testbed-logger.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2008--2013 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 
33 #define LOG(type, ...) \
34  GNUNET_log (type, __VA_ARGS__)
35 
39 #define LOG_DEBUG(...) \
40  LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
41 
46 
50 static unsigned int nconn;
51 
55 static int in_shutdown;
56 
57 
65 static int
66 check_log_msg (void *cls,
67  const struct GNUNET_MessageHeader *msg)
68 {
69  return GNUNET_OK;
70 }
71 
72 
79 static void
80 handle_log_msg (void *cls,
81  const struct GNUNET_MessageHeader *msg)
82 {
83  struct GNUNET_SERVICE_Client *client = cls;
84  uint16_t ms;
85 
86  ms = ntohs (msg->size) - sizeof(struct GNUNET_MessageHeader);
87  GNUNET_BIO_write (bio,
88  &msg[1],
89  ms);
91 }
92 
93 
99 static void
100 shutdown_task (void *cls)
101 {
103  if (0 != nconn)
104  {
105  /* Delay shutdown if there are active connections */
107  NULL);
108  return;
109  }
111  GNUNET_BIO_write_close (bio));
112 }
113 
114 
123 static void *
124 client_connect_cb (void *cls,
125  struct GNUNET_SERVICE_Client *c,
126  struct GNUNET_MQ_Handle *mq)
127 {
128  /* FIXME: is this really what we want here? */
130  nconn++;
131  return c;
132 }
133 
134 
142 static void
144  struct GNUNET_SERVICE_Client *c,
145  void *internal_cls)
146 {
147  nconn--;
148  if (GNUNET_YES == in_shutdown)
150  GNUNET_assert (c == internal_cls);
151 }
152 
153 
161 static void
162 logger_run (void *cls,
163  const struct GNUNET_CONFIGURATION_Handle *cfg,
165 {
166  char *dir;
167  char *fn;
168  char *hname;
169  size_t hname_len;
170  pid_t pid;
171 
172  if (GNUNET_OK !=
174  "TESTBED-LOGGER",
175  "DIR",
176  &dir))
177  {
179  "TESTBED-LOGGER",
180  "DIR");
182  return;
183  }
184  pid = getpid ();
185  hname_len = GNUNET_OS_get_hostname_max_length ();
186  hname = GNUNET_malloc (hname_len);
187  if (0 != gethostname (hname,
188  hname_len))
189  {
191  "Cannot get hostname. Exiting\n");
192  GNUNET_free (hname);
193  GNUNET_free (dir);
195  return;
196  }
197  GNUNET_asprintf (&fn,
198  "%s/%.*s_%jd.dat",
199  dir,
200  hname_len,
201  hname,
202  (intmax_t) pid);
203  GNUNET_free (hname);
204  GNUNET_free (dir);
205  if (NULL == (bio = GNUNET_BIO_write_open (fn)))
206  {
207  GNUNET_free (fn);
209  return;
210  }
211  GNUNET_free (fn);
213  NULL);
214  LOG_DEBUG ("TESTBED-LOGGER startup complete\n");
215 }
216 
217 
222  ("testbed-logger",
224  &logger_run,
227  NULL,
228  GNUNET_MQ_hd_var_size (log_msg,
230  struct GNUNET_MessageHeader,
231  NULL),
233 
234 
235 /* end of gnunet-service-testbed-logger.c */
static char * dir
Set to the directory where runtime files are stored.
Definition: gnunet-arm.c:89
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
Handle to a service.
Definition: service.c:116
static int check_log_msg(void *cls, const struct GNUNET_MessageHeader *msg)
Check GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG messages.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1300
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define LOG_DEBUG(...)
Debug logging shorthand.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
GNUNET_SERVICE_MAIN("testbed-logger", GNUNET_SERVICE_OPTION_NONE, &logger_run, &client_connect_cb, &client_disconnect_cb, NULL, GNUNET_MQ_hd_var_size(log_msg, GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG, struct GNUNET_MessageHeader, NULL), GNUNET_MQ_handler_end())
Define "main" method using service macro.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
static void shutdown_task(void *cls)
Task to clean up and shutdown nicely.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:526
static void client_disconnect_cb(void *cls, struct GNUNET_SERVICE_Client *c, void *internal_cls)
Callback called when a client disconnected from the service.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
Handle to a client that is connected to a service.
Definition: service.c:250
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
#define GNUNET_OS_get_hostname_max_length()
Get maximum string length returned by gethostname()
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
static char * fn
Filename of the unique file.
#define LOG(type,...)
Generic logging shorthand.
struct GNUNET_BIO_WriteHandle * bio
Handle for buffered writing.
Handle for buffered writing.
Definition: bio.c:379
static void * client_connect_cb(void *cls, struct GNUNET_SERVICE_Client *c, struct GNUNET_MQ_Handle *mq)
Callback called when a client connects to the service.
int GNUNET_BIO_write_close(struct GNUNET_BIO_WriteHandle *h)
Close an open file for writing.
Definition: bio.c:438
Handle to a message queue.
Definition: mq.c:85
configuration data
Definition: configuration.c:85
struct GNUNET_BIO_WriteHandle * GNUNET_BIO_write_open(const char *fn)
Open a file for writing.
Definition: bio.c:410
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
int GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
Header for all communications.
#define GNUNET_YES
Definition: gnunet_common.h:77
int GNUNET_BIO_write(struct GNUNET_BIO_WriteHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: bio.c:483
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
static int in_shutdown
Are we shutting down?
static void handle_log_msg(void *cls, const struct GNUNET_MessageHeader *msg)
Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG messages.
static void logger_run(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_SERVICE_Handle *service)
Testbed setup.
void GNUNET_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition: service.c:2243
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_malloc(size)
Wrapper around malloc.
void GNUNET_SERVICE_client_persist(struct GNUNET_SERVICE_Client *c)
Set the persist option on this client.
Definition: service.c:2424
#define GNUNET_free(ptr)
Wrapper around free.
static unsigned int nconn
The number of connections we have.
#define GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG
Message for TESTBED LOGGER.