2 This file is part of GNUnet
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/gnunet-helper-testbed.c
23 * @brief Helper binary that is started from a remote controller to start
24 * gnunet-service-testbed. This binary also receives configuration
25 * from the remove controller which is put in a temporary location
26 * with ports and paths fixed so that gnunet-service-testbed runs
27 * without any hurdles. This binary also kills the testbed service
28 * should the connection from the remote controller is dropped
29 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
34 #include "gnunet_util_lib.h"
35 #include "gnunet_testing_lib-new.h"
36 #include "gnunet_testbed_service.h"
37 #include "testbed_helper.h"
38 #include "testbed_api.h"
42 * Generic logging shortcut
44 #define LOG(kind, ...) \
45 GNUNET_log (kind, __VA_ARGS__)
48 * Debug logging shorthand
50 #define LOG_DEBUG(...) \
51 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
55 * Context for a single write on a chunk of memory
65 * The length of the data
70 * The current position from where the write operation should begin
77 * Handle to the testing system
79 static struct GNUNET_TESTING_System *test_system;
82 * Our message stream tokenizer
84 struct GNUNET_SERVER_MessageStreamTokenizer *tokenizer;
87 * Disk handle from stdin
89 static struct GNUNET_DISK_FileHandle *stdin_fd;
92 * Disk handle for stdout
94 static struct GNUNET_DISK_FileHandle *stdout_fd;
97 * The process handle to the testbed service
99 static struct GNUNET_OS_Process *testbed;
102 * Task identifier for the read task
104 static GNUNET_SCHEDULER_TaskIdentifier read_task_id;
107 * Task identifier for the write task
109 static GNUNET_SCHEDULER_TaskIdentifier write_task_id;
112 * Are we done reading messages from stdin?
114 static int done_reading;
117 * Result to return in case we fail
123 * Are we shutting down
125 static int in_shutdown;
129 * Task to shutting down nicely
132 * @param tc the task context
135 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
137 LOG_DEBUG ("Shutting down\n");
138 in_shutdown = GNUNET_YES;
139 if (GNUNET_SCHEDULER_NO_TASK != read_task_id)
141 GNUNET_SCHEDULER_cancel (read_task_id);
142 read_task_id = GNUNET_SCHEDULER_NO_TASK;
144 if (GNUNET_SCHEDULER_NO_TASK != write_task_id)
146 GNUNET_SCHEDULER_cancel (write_task_id);
147 write_task_id = GNUNET_SCHEDULER_NO_TASK;
149 if (NULL != stdin_fd)
150 (void) GNUNET_DISK_file_close (stdin_fd);
151 if (NULL != stdout_fd)
152 (void) GNUNET_DISK_file_close (stdout_fd);
153 GNUNET_SERVER_mst_destroy (tokenizer);
157 LOG_DEBUG ("Killing testbed\n");
158 GNUNET_break (0 == GNUNET_OS_process_kill (testbed, SIGTERM));
159 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (testbed));
160 GNUNET_OS_process_destroy (testbed);
163 if (NULL != test_system)
165 GNUNET_TESTING_system_destroy (test_system, GNUNET_YES);
172 * Task to write to the standard out
174 * @param cls the WriteContext
175 * @param tc the TaskContext
178 write_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
180 struct WriteContext *wc = cls;
183 GNUNET_assert (NULL != wc);
184 write_task_id = GNUNET_SCHEDULER_NO_TASK;
185 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
187 GNUNET_free (wc->data);
192 GNUNET_DISK_file_write (stdout_fd, wc->data + wc->pos,
193 wc->length - wc->pos);
194 GNUNET_assert (GNUNET_SYSERR != bytes_wrote);
195 wc->pos += bytes_wrote;
196 if (wc->pos == wc->length)
198 GNUNET_free (wc->data);
203 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, stdout_fd,
209 * Functions with this signature are called whenever a
210 * complete message is received by the tokenizer.
212 * Do not call GNUNET_SERVER_mst_destroy in callback
215 * @param client identification of the client
216 * @param message the actual message
218 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
221 tokenizer_cb (void *cls, void *client,
222 const struct GNUNET_MessageHeader *message)
224 const struct GNUNET_TESTBED_HelperInit *msg;
225 struct GNUNET_TESTBED_HelperReply *reply;
226 struct GNUNET_CONFIGURATION_Handle *cfg;
227 struct WriteContext *wc;
232 uLongf ul_config_size;
236 if ((sizeof (struct GNUNET_TESTBED_HelperInit) >= ntohs (message->size)) ||
237 (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT != ntohs (message->type)))
239 LOG (GNUNET_ERROR_TYPE_WARNING, "Received unexpected message -- exiting\n");
242 msg = (const struct GNUNET_TESTBED_HelperInit *) message;
243 cname_size = ntohs (msg->cname_size);
244 controller = (char *) &msg[1];
245 if ('\0' != controller[cname_size])
247 LOG (GNUNET_ERROR_TYPE_WARNING,
248 "Controller name cannot be empty -- exiting\n");
251 ul_config_size = (uLongf) ntohs (msg->config_size);
252 config = GNUNET_malloc (ul_config_size);
254 ntohs (message->size) - (cname_size + 1 +
255 sizeof (struct GNUNET_TESTBED_HelperInit));
257 uncompress ((Bytef *) config, &ul_config_size,
258 (const Bytef *) (controller + cname_size + 1),
259 (uLongf) xconfig_size))
261 LOG (GNUNET_ERROR_TYPE_WARNING,
262 "Error while uncompressing config -- exiting\n");
263 GNUNET_free (config);
266 cfg = GNUNET_CONFIGURATION_create ();
268 GNUNET_CONFIGURATION_deserialize (cfg, config, ul_config_size, GNUNET_NO))
270 LOG (GNUNET_ERROR_TYPE_WARNING,
271 "Unable to deserialize config -- exiting\n");
272 GNUNET_free (config);
275 GNUNET_free (config);
276 test_system = GNUNET_TESTING_system_create ("testbed-helper", controller);
277 GNUNET_assert (NULL != test_system);
278 GNUNET_assert (GNUNET_OK ==
279 GNUNET_TESTING_configuration_create (test_system, cfg));
280 GNUNET_assert (GNUNET_OK ==
281 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
284 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, config))
286 LOG (GNUNET_ERROR_TYPE_WARNING,
287 "Unable to write config file: %s -- exiting\n", config);
288 GNUNET_CONFIGURATION_destroy (cfg);
289 GNUNET_free (config);
292 LOG_DEBUG ("Staring testbed with config: %s\n", config);
294 GNUNET_OS_start_process (GNUNET_YES,
295 GNUNET_OS_INHERIT_STD_ERR /*verbose? */ , NULL,
296 NULL, "gnunet-service-testbed",
297 "gnunet-service-testbed", "-c", config, NULL);
298 GNUNET_free (config);
301 LOG (GNUNET_ERROR_TYPE_WARNING,
302 "Error staring gnunet-service-testbed -- exiting\n");
303 GNUNET_CONFIGURATION_destroy (cfg);
306 done_reading = GNUNET_YES;
307 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
308 GNUNET_CONFIGURATION_destroy (cfg);
311 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
312 GNUNET_free (config);
313 wc = GNUNET_malloc (sizeof (struct WriteContext));
314 wc->length = xconfig_size + sizeof (struct GNUNET_TESTBED_HelperReply);
315 reply = GNUNET_realloc (xconfig, wc->length);
316 memmove (&reply[1], reply, xconfig_size);
317 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY);
318 reply->header.size = htons ((uint16_t) wc->length);
319 reply->config_size = htons ((uint16_t) config_size);
322 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, stdout_fd,
327 status = GNUNET_SYSERR;
328 GNUNET_SCHEDULER_shutdown ();
329 return GNUNET_SYSERR;
334 * Task to read from stdin
337 * @param tc the task context
340 read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
342 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
345 read_task_id = GNUNET_SCHEDULER_NO_TASK;
346 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
348 sread = GNUNET_DISK_file_read (stdin_fd, buf, sizeof (buf));
349 if ((GNUNET_SYSERR == sread) || (0 == sread))
351 GNUNET_SCHEDULER_shutdown ();
354 if (GNUNET_YES == done_reading)
356 /* didn't expect any more data! */
357 GNUNET_SCHEDULER_shutdown ();
360 LOG_DEBUG ("Read %u bytes\n", sread);
362 GNUNET_SERVER_mst_receive (tokenizer, NULL, buf, sread, GNUNET_NO,
366 GNUNET_SCHEDULER_shutdown ();
369 read_task_id = /* No timeout while reading */
370 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fd,
376 * Main function that will be run.
379 * @param args remaining command-line arguments
380 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
381 * @param cfg configuration
384 run (void *cls, char *const *args, const char *cfgfile,
385 const struct GNUNET_CONFIGURATION_Handle *cfg)
387 LOG_DEBUG ("Starting testbed helper...\n");
388 tokenizer = GNUNET_SERVER_mst_create (&tokenizer_cb, NULL);
389 stdin_fd = GNUNET_DISK_get_handle_from_native (stdin);
390 stdout_fd = GNUNET_DISK_get_handle_from_native (stdout);
392 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fd,
394 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
400 * Signal handler called for SIGCHLD.
403 sighandler_child_death ()
405 if ((NULL != testbed) && (GNUNET_NO == in_shutdown))
407 LOG_DEBUG ("Child died\n");
408 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (testbed));
409 GNUNET_OS_process_destroy (testbed);
411 GNUNET_SCHEDULER_shutdown (); /* We are done too! */
419 * @param argc the number of command line arguments
420 * @param argv command line arg array
421 * @return return code
424 main (int argc, char **argv)
426 struct GNUNET_SIGNAL_Context *shc_chld;
428 struct GNUNET_GETOPT_CommandLineOption options[] = {
429 GNUNET_GETOPT_OPTION_END
434 in_shutdown = GNUNET_NO;
436 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
438 GNUNET_PROGRAM_run (argc, argv, "gnunet-helper-testbed",
439 "Helper for starting gnunet-service-testbed", options,
441 GNUNET_SIGNAL_handler_uninstall (shc_chld);
443 if (GNUNET_OK != ret)
445 return (GNUNET_OK == status) ? 0 : 1;
448 /* end of gnunet-helper-testbed.c */