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.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 * We need pipe control only on WINDOWS
58 #define PIPE_CONTROL GNUNET_YES
60 #define PIPE_CONTROL GNUNET_NO
65 * Context for a single write on a chunk of memory
75 * The length of the data
80 * The current position from where the write operation should begin
87 * Handle to the testing system
89 static struct GNUNET_TESTING_System *test_system;
92 * Our message stream tokenizer
94 struct GNUNET_SERVER_MessageStreamTokenizer *tokenizer;
97 * Disk handle from stdin
99 static struct GNUNET_DISK_FileHandle *stdin_fd;
102 * Disk handle for stdout
104 static struct GNUNET_DISK_FileHandle *stdout_fd;
107 * The process handle to the testbed service
109 static struct GNUNET_OS_Process *testbed;
112 * Pipe used to communicate shutdown via signal.
114 static struct GNUNET_DISK_PipeHandle *sigpipe;
117 * Task identifier for the read task
119 static GNUNET_SCHEDULER_TaskIdentifier read_task_id;
122 * Task identifier for the write task
124 static GNUNET_SCHEDULER_TaskIdentifier write_task_id;
127 * Task to kill the child
129 static GNUNET_SCHEDULER_TaskIdentifier child_death_task_id;
132 * Are we done reading messages from stdin?
134 static int done_reading;
137 * Result to return in case we fail
143 * Are we shutting down
145 static int in_shutdown;
149 * Task to shutting down nicely
152 * @param tc the task context
155 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
157 LOG_DEBUG ("Shutting down\n");
158 in_shutdown = GNUNET_YES;
159 if (GNUNET_SCHEDULER_NO_TASK != read_task_id)
161 GNUNET_SCHEDULER_cancel (read_task_id);
162 read_task_id = GNUNET_SCHEDULER_NO_TASK;
164 if (GNUNET_SCHEDULER_NO_TASK != write_task_id)
166 GNUNET_SCHEDULER_cancel (write_task_id);
167 write_task_id = GNUNET_SCHEDULER_NO_TASK;
169 if (NULL != stdin_fd)
170 (void) GNUNET_DISK_file_close (stdin_fd);
171 if (NULL != stdout_fd)
172 (void) GNUNET_DISK_file_close (stdout_fd);
173 GNUNET_SERVER_mst_destroy (tokenizer);
177 LOG_DEBUG ("Killing testbed\n");
178 GNUNET_break (0 == GNUNET_OS_process_kill (testbed, SIGTERM));
184 * Task to write to the standard out
186 * @param cls the WriteContext
187 * @param tc the TaskContext
190 write_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
192 struct WriteContext *wc = cls;
195 GNUNET_assert (NULL != wc);
196 write_task_id = GNUNET_SCHEDULER_NO_TASK;
197 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
199 GNUNET_free (wc->data);
204 GNUNET_DISK_file_write (stdout_fd, wc->data + wc->pos,
205 wc->length - wc->pos);
206 if (GNUNET_SYSERR == bytes_wrote)
208 LOG (GNUNET_ERROR_TYPE_WARNING, "Cannot reply back configuration\n");
209 GNUNET_free (wc->data);
213 wc->pos += bytes_wrote;
214 if (wc->pos == wc->length)
216 GNUNET_free (wc->data);
221 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, stdout_fd,
227 * Functions with this signature are called whenever a
228 * complete message is received by the tokenizer.
230 * Do not call GNUNET_SERVER_mst_destroy in callback
233 * @param client identification of the client
234 * @param message the actual message
236 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
239 tokenizer_cb (void *cls, void *client,
240 const struct GNUNET_MessageHeader *message)
242 const struct GNUNET_TESTBED_HelperInit *msg;
243 struct GNUNET_TESTBED_HelperReply *reply;
244 struct GNUNET_CONFIGURATION_Handle *cfg;
245 struct WriteContext *wc;
252 uLongf ul_config_size;
254 uint16_t trusted_ip_size;
255 uint16_t hostname_size;
258 msize = ntohs (message->size);
259 if ((sizeof (struct GNUNET_TESTBED_HelperInit) >= msize) ||
260 (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT != ntohs (message->type)))
262 LOG (GNUNET_ERROR_TYPE_WARNING, "Received unexpected message -- exiting\n");
265 msg = (const struct GNUNET_TESTBED_HelperInit *) message;
266 trusted_ip_size = ntohs (msg->trusted_ip_size);
267 trusted_ip = (char *) &msg[1];
268 if ('\0' != trusted_ip[trusted_ip_size])
270 LOG (GNUNET_ERROR_TYPE_WARNING, "Trusted IP cannot be empty -- exiting\n");
273 hostname_size = ntohs (msg->hostname_size);
274 if ((sizeof (struct GNUNET_TESTBED_HelperInit) + trusted_ip_size + 1 +
275 hostname_size) >= msize)
278 LOG (GNUNET_ERROR_TYPE_WARNING, "Received unexpected message -- exiting\n");
281 ul_config_size = (uLongf) ntohs (msg->config_size);
282 config = GNUNET_malloc (ul_config_size);
284 ntohs (message->size) - (trusted_ip_size + 1 +
285 sizeof (struct GNUNET_TESTBED_HelperInit));
287 uncompress ((Bytef *) config, &ul_config_size,
288 (const Bytef *) (trusted_ip + trusted_ip_size + 1 +
289 hostname_size), (uLongf) xconfig_size))
291 LOG (GNUNET_ERROR_TYPE_WARNING,
292 "Error while uncompressing config -- exiting\n");
293 GNUNET_free (config);
296 cfg = GNUNET_CONFIGURATION_create ();
298 GNUNET_CONFIGURATION_deserialize (cfg, config, ul_config_size, GNUNET_NO))
300 LOG (GNUNET_ERROR_TYPE_WARNING,
301 "Unable to deserialize config -- exiting\n");
302 GNUNET_free (config);
305 GNUNET_free (config);
307 if (0 != hostname_size)
309 hostname = GNUNET_malloc (hostname_size + 1);
310 (void) strncpy (hostname, ((char *) &msg[1]) + trusted_ip_size + 1,
312 hostname[hostname_size] = '\0';
315 GNUNET_TESTING_system_create ("testbed-helper", trusted_ip, hostname);
316 GNUNET_free_non_null (hostname);
318 GNUNET_assert (NULL != test_system);
319 GNUNET_assert (GNUNET_OK ==
320 GNUNET_TESTING_configuration_create (test_system, cfg));
321 GNUNET_assert (GNUNET_OK ==
322 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
325 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, config))
327 LOG (GNUNET_ERROR_TYPE_WARNING,
328 "Unable to write config file: %s -- exiting\n", config);
329 GNUNET_CONFIGURATION_destroy (cfg);
330 GNUNET_free (config);
333 LOG_DEBUG ("Staring testbed with config: %s\n", config);
334 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-testbed");
336 GNUNET_OS_start_process (PIPE_CONTROL,
337 GNUNET_OS_INHERIT_STD_ERR /*verbose? */ , NULL,
338 NULL, binary, "gnunet-service-testbed", "-c",
340 GNUNET_free (binary);
341 GNUNET_free (config);
344 LOG (GNUNET_ERROR_TYPE_WARNING,
345 "Error starting gnunet-service-testbed -- exiting\n");
346 GNUNET_CONFIGURATION_destroy (cfg);
349 done_reading = GNUNET_YES;
350 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
351 GNUNET_CONFIGURATION_destroy (cfg);
354 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
355 GNUNET_free (config);
356 wc = GNUNET_malloc (sizeof (struct WriteContext));
357 wc->length = xconfig_size + sizeof (struct GNUNET_TESTBED_HelperReply);
358 reply = GNUNET_realloc (xconfig, wc->length);
359 memmove (&reply[1], reply, xconfig_size);
360 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY);
361 reply->header.size = htons ((uint16_t) wc->length);
362 reply->config_size = htons ((uint16_t) config_size);
365 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, stdout_fd,
370 status = GNUNET_SYSERR;
371 GNUNET_SCHEDULER_shutdown ();
372 return GNUNET_SYSERR;
377 * Task to read from stdin
380 * @param tc the task context
383 read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
385 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
388 read_task_id = GNUNET_SCHEDULER_NO_TASK;
389 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
391 sread = GNUNET_DISK_file_read (stdin_fd, buf, sizeof (buf));
392 if ((GNUNET_SYSERR == sread) || (0 == sread))
394 GNUNET_SCHEDULER_shutdown ();
397 if (GNUNET_YES == done_reading)
399 /* didn't expect any more data! */
400 GNUNET_SCHEDULER_shutdown ();
403 LOG_DEBUG ("Read %u bytes\n", sread);
405 GNUNET_SERVER_mst_receive (tokenizer, NULL, buf, sread, GNUNET_NO,
409 GNUNET_SCHEDULER_shutdown ();
412 read_task_id = /* No timeout while reading */
413 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fd,
419 * Task triggered whenever we receive a SIGCHLD (child
422 * @param cls closure, NULL if we need to self-restart
426 child_death_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
428 const struct GNUNET_DISK_FileHandle *pr;
431 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
432 child_death_task_id = GNUNET_SCHEDULER_NO_TASK;
433 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
435 child_death_task_id =
436 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
437 pr, &child_death_task, NULL);
440 /* consume the signal */
441 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
442 LOG_DEBUG ("Child died\n");
443 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (testbed));
444 GNUNET_OS_process_destroy (testbed);
446 if (NULL != test_system)
448 GNUNET_TESTING_system_destroy (test_system, GNUNET_YES);
455 * Main function that will be run.
458 * @param args remaining command-line arguments
459 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
460 * @param cfg configuration
463 run (void *cls, char *const *args, const char *cfgfile,
464 const struct GNUNET_CONFIGURATION_Handle *cfg)
466 LOG_DEBUG ("Starting testbed helper...\n");
467 tokenizer = GNUNET_SERVER_mst_create (&tokenizer_cb, NULL);
468 stdin_fd = GNUNET_DISK_get_handle_from_native (stdin);
469 stdout_fd = GNUNET_DISK_get_handle_from_native (stdout);
471 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fd,
473 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
475 child_death_task_id =
476 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
477 GNUNET_DISK_pipe_handle (sigpipe,
478 GNUNET_DISK_PIPE_END_READ),
479 &child_death_task, NULL);
484 * Signal handler called for SIGCHLD.
487 sighandler_child_death ()
490 int old_errno; /* back-up errno */
494 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
495 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
504 * @param argc the number of command line arguments
505 * @param argv command line arg array
506 * @return return code
509 main (int argc, char **argv)
511 struct GNUNET_SIGNAL_Context *shc_chld;
513 struct GNUNET_GETOPT_CommandLineOption options[] = {
514 GNUNET_GETOPT_OPTION_END
519 in_shutdown = GNUNET_NO;
520 if (NULL == (sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
521 GNUNET_NO, GNUNET_NO)))
528 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
530 GNUNET_PROGRAM_run (argc, argv, "gnunet-helper-testbed",
531 "Helper for starting gnunet-service-testbed", options,
533 GNUNET_SIGNAL_handler_uninstall (shc_chld);
535 GNUNET_DISK_pipe_close (sigpipe);
536 if (GNUNET_OK != ret)
538 return (GNUNET_OK == status) ? 0 : 1;
541 /* end of gnunet-helper-testbed.c */