2 This file is part of GNUnet
3 Copyright (C) 2008--2013 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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.
29 * This helper monitors for three termination events. They are: (1)The
30 * stdin of the helper is closed for reading; (2)the helper received
31 * SIGTERM/SIGINT; (3)the testbed crashed. In case of events 1 and 2
32 * the helper kills the testbed service. When testbed crashed (event
33 * 3), the helper should send a SIGTERM to its own process group; this
34 * behaviour will help terminate any child processes (peers) testbed
35 * has started and prevents them from leaking and running forever.
37 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
42 #include "gnunet_util_lib.h"
43 #include "gnunet_testing_lib.h"
44 #include "gnunet_testbed_service.h"
45 #include "testbed_helper.h"
46 #include "testbed_api.h"
50 * Generic logging shortcut
52 #define LOG(kind, ...) \
53 GNUNET_log (kind, __VA_ARGS__)
56 * Debug logging shorthand
58 #define LOG_DEBUG(...) \
59 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
63 * We need pipe control only on WINDOWS
66 #define PIPE_CONTROL GNUNET_YES
68 #define PIPE_CONTROL GNUNET_NO
73 * Context for a single write on a chunk of memory
83 * The length of the data
88 * The current position from where the write operation should begin
95 * Handle to the testing system
97 static struct GNUNET_TESTING_System *test_system;
100 * Our message stream tokenizer
102 struct GNUNET_SERVER_MessageStreamTokenizer *tokenizer;
105 * Disk handle from stdin
107 static struct GNUNET_DISK_FileHandle *stdin_fd;
110 * Disk handle for stdout
112 static struct GNUNET_DISK_FileHandle *stdout_fd;
115 * The process handle to the testbed service
117 static struct GNUNET_OS_Process *testbed;
120 * Pipe used to communicate shutdown via signal.
122 static struct GNUNET_DISK_PipeHandle *sigpipe;
125 * Task identifier for the read task
127 static struct GNUNET_SCHEDULER_Task * read_task_id;
130 * Task identifier for the write task
132 static struct GNUNET_SCHEDULER_Task * write_task_id;
135 * Task to kill the child
137 static struct GNUNET_SCHEDULER_Task * child_death_task_id;
142 static struct GNUNET_SCHEDULER_Task * shutdown_task_id;
145 * Are we done reading messages from stdin?
147 static int done_reading;
150 * Result to return in case we fail
156 * Task to shut down cleanly
159 * @param tc the task context
162 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
164 LOG_DEBUG ("Shutting down\n");
165 shutdown_task_id = NULL;
168 LOG_DEBUG ("Killing testbed\n");
169 GNUNET_break (0 == GNUNET_OS_process_kill (testbed, GNUNET_TERM_SIG));
171 if (NULL != read_task_id)
173 GNUNET_SCHEDULER_cancel (read_task_id);
176 if (NULL != write_task_id)
178 GNUNET_SCHEDULER_cancel (write_task_id);
179 write_task_id = NULL;
181 if (NULL != child_death_task_id)
183 GNUNET_SCHEDULER_cancel (child_death_task_id);
184 child_death_task_id = NULL;
186 if (NULL != stdin_fd)
187 (void) GNUNET_DISK_file_close (stdin_fd);
188 if (NULL != stdout_fd)
189 (void) GNUNET_DISK_file_close (stdout_fd);
190 GNUNET_SERVER_mst_destroy (tokenizer);
194 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (testbed));
195 GNUNET_OS_process_destroy (testbed);
198 if (NULL != test_system)
200 GNUNET_TESTING_system_destroy (test_system, GNUNET_YES);
207 * Scheduler shutdown task to be run now.
212 if (NULL != shutdown_task_id)
213 GNUNET_SCHEDULER_cancel (shutdown_task_id);
214 shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
219 * Task to write to the standard out
221 * @param cls the WriteContext
222 * @param tc the TaskContext
225 write_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
227 struct WriteContext *wc = cls;
230 GNUNET_assert (NULL != wc);
231 write_task_id = NULL;
232 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
234 GNUNET_free (wc->data);
239 GNUNET_DISK_file_write (stdout_fd, wc->data + wc->pos,
240 wc->length - wc->pos);
241 if (GNUNET_SYSERR == bytes_wrote)
243 LOG (GNUNET_ERROR_TYPE_WARNING, "Cannot reply back configuration\n");
244 GNUNET_free (wc->data);
248 wc->pos += bytes_wrote;
249 if (wc->pos == wc->length)
251 GNUNET_free (wc->data);
256 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, stdout_fd,
262 * Task triggered whenever we receive a SIGCHLD (child
265 * @param cls closure, NULL if we need to self-restart
269 child_death_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
271 const struct GNUNET_DISK_FileHandle *pr;
273 enum GNUNET_OS_ProcessStatusType type;
277 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
278 child_death_task_id = NULL;
279 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
281 child_death_task_id =
282 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
283 pr, &child_death_task, NULL);
286 /* consume the signal */
287 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
288 LOG_DEBUG ("Got SIGCHLD\n");
294 GNUNET_break (GNUNET_SYSERR !=
295 (ret = GNUNET_OS_process_status (testbed, &type, &code)));
296 if (GNUNET_NO != ret)
298 GNUNET_OS_process_destroy (testbed);
300 /* Send SIGTERM to our process group */
301 if (0 != PLIBC_KILL (0, GNUNET_TERM_SIG))
303 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "signal");
304 shutdown_now (); /* Couldn't send the signal, we shutdown frowning */
308 LOG_DEBUG ("Child hasn't died. Resuming to monitor its status\n");
309 child_death_task_id =
310 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
311 pr, &child_death_task, NULL);
316 * Functions with this signature are called whenever a
317 * complete message is received by the tokenizer.
319 * Do not call GNUNET_SERVER_mst_destroy in callback
322 * @param client identification of the client
323 * @param message the actual message
325 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
328 tokenizer_cb (void *cls, void *client,
329 const struct GNUNET_MessageHeader *message)
331 const struct GNUNET_TESTBED_HelperInit *msg;
332 struct GNUNET_TESTBED_HelperReply *reply;
333 struct GNUNET_CONFIGURATION_Handle *cfg;
334 struct WriteContext *wc;
343 uLongf ul_config_size;
345 uint16_t trusted_ip_size;
346 uint16_t hostname_size;
349 msize = ntohs (message->size);
350 if ((sizeof (struct GNUNET_TESTBED_HelperInit) >= msize) ||
351 (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT != ntohs (message->type)))
353 LOG (GNUNET_ERROR_TYPE_WARNING, "Received unexpected message -- exiting\n");
356 msg = (const struct GNUNET_TESTBED_HelperInit *) message;
357 trusted_ip_size = ntohs (msg->trusted_ip_size);
358 trusted_ip = (char *) &msg[1];
359 if ('\0' != trusted_ip[trusted_ip_size])
361 LOG (GNUNET_ERROR_TYPE_WARNING, "Trusted IP cannot be empty -- exiting\n");
364 hostname_size = ntohs (msg->hostname_size);
365 if ((sizeof (struct GNUNET_TESTBED_HelperInit) + trusted_ip_size + 1 +
366 hostname_size) >= msize)
369 LOG (GNUNET_ERROR_TYPE_WARNING, "Received unexpected message -- exiting\n");
372 ul_config_size = (uLongf) ntohs (msg->config_size);
373 config = GNUNET_malloc (ul_config_size);
375 ntohs (message->size) - (trusted_ip_size + 1 +
376 sizeof (struct GNUNET_TESTBED_HelperInit));
378 uncompress ((Bytef *) config, &ul_config_size,
379 (const Bytef *) (trusted_ip + trusted_ip_size + 1 +
380 hostname_size), (uLongf) xconfig_size))
382 LOG (GNUNET_ERROR_TYPE_WARNING,
383 "Error while uncompressing config -- exiting\n");
384 GNUNET_free (config);
387 cfg = GNUNET_CONFIGURATION_create ();
389 GNUNET_CONFIGURATION_deserialize (cfg, config, ul_config_size, GNUNET_NO))
391 LOG (GNUNET_ERROR_TYPE_WARNING,
392 "Unable to deserialize config -- exiting\n");
393 GNUNET_free (config);
396 GNUNET_free (config);
398 if (0 != hostname_size)
400 hostname = GNUNET_malloc (hostname_size + 1);
401 (void) strncpy (hostname, ((char *) &msg[1]) + trusted_ip_size + 1,
403 hostname[hostname_size] = '\0';
405 /* unset GNUNET_TESTING_PREFIX if present as it is more relevant for testbed */
406 evstr = getenv (GNUNET_TESTING_PREFIX);
409 /* unsetting the variable will invalidate the pointer! */
410 evstr = GNUNET_strdup (evstr);
412 GNUNET_break (0 != SetEnvironmentVariable (GNUNET_TESTING_PREFIX, NULL));
414 GNUNET_break (0 == unsetenv (GNUNET_TESTING_PREFIX));
418 GNUNET_TESTING_system_create ("testbed-helper", trusted_ip, hostname,
423 GNUNET_assert (0 != SetEnvironmentVariable (GNUNET_TESTING_PREFIX, evstr));
425 static char evar[2* PATH_MAX];
427 GNUNET_assert (0 < GNUNET_snprintf (evar, sizeof (evar),
428 GNUNET_TESTING_PREFIX "=%s", evstr));
434 GNUNET_free_non_null (hostname);
436 GNUNET_assert (NULL != test_system);
437 GNUNET_assert (GNUNET_OK ==
438 GNUNET_TESTING_configuration_create (test_system, cfg));
439 GNUNET_assert (GNUNET_OK ==
440 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS",
443 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, config))
445 LOG (GNUNET_ERROR_TYPE_WARNING,
446 "Unable to write config file: %s -- exiting\n", config);
447 GNUNET_CONFIGURATION_destroy (cfg);
448 GNUNET_free (config);
451 LOG_DEBUG ("Staring testbed with config: %s\n", config);
452 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-testbed");
454 static char evar[2 * PATH_MAX];
456 /* expose testbed configuration through env variable */
457 GNUNET_assert (0 < GNUNET_snprintf (evar, sizeof (evar),
458 "%s=%s", ENV_TESTBED_CONFIG, config));
459 GNUNET_assert (0 == putenv (evar));
463 GNUNET_OS_start_process (PIPE_CONTROL,
464 GNUNET_OS_INHERIT_STD_ERR /*verbose? */ ,
466 binary, "gnunet-service-testbed", "-c",
468 GNUNET_free (binary);
469 GNUNET_free (config);
472 LOG (GNUNET_ERROR_TYPE_WARNING,
473 "Error starting gnunet-service-testbed -- exiting\n");
474 GNUNET_CONFIGURATION_destroy (cfg);
477 done_reading = GNUNET_YES;
478 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
479 GNUNET_CONFIGURATION_destroy (cfg);
482 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
483 GNUNET_free (config);
484 wc = GNUNET_new (struct WriteContext);
485 wc->length = xconfig_size + sizeof (struct GNUNET_TESTBED_HelperReply);
486 reply = GNUNET_realloc (xconfig, wc->length);
487 memmove (&reply[1], reply, xconfig_size);
488 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY);
489 reply->header.size = htons ((uint16_t) wc->length);
490 reply->config_size = htons ((uint16_t) config_size);
493 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, stdout_fd,
495 child_death_task_id =
496 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
497 GNUNET_DISK_pipe_handle (sigpipe,
498 GNUNET_DISK_PIPE_END_READ),
499 &child_death_task, NULL);
503 status = GNUNET_SYSERR;
505 return GNUNET_SYSERR;
510 * Task to read from stdin
513 * @param tc the task context
516 read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
518 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
522 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
524 sread = GNUNET_DISK_file_read (stdin_fd, buf, sizeof (buf));
525 if ((GNUNET_SYSERR == sread) || (0 == sread))
527 LOG_DEBUG ("STDIN closed\n");
531 if (GNUNET_YES == done_reading)
533 /* didn't expect any more data! */
538 LOG_DEBUG ("Read %u bytes\n", sread);
540 GNUNET_SERVER_mst_receive (tokenizer, NULL, buf, sread, GNUNET_NO,
547 read_task_id = /* No timeout while reading */
548 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fd,
554 * Main function that will be run.
557 * @param args remaining command-line arguments
558 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
559 * @param cfg configuration
562 run (void *cls, char *const *args, const char *cfgfile,
563 const struct GNUNET_CONFIGURATION_Handle *cfg)
565 LOG_DEBUG ("Starting testbed helper...\n");
566 tokenizer = GNUNET_SERVER_mst_create (&tokenizer_cb, NULL);
567 stdin_fd = GNUNET_DISK_get_handle_from_native (stdin);
568 stdout_fd = GNUNET_DISK_get_handle_from_native (stdout);
570 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fd,
573 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
579 * Signal handler called for SIGCHLD.
582 sighandler_child_death ()
585 int old_errno; /* back-up errno */
589 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
590 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
599 * @param argc the number of command line arguments
600 * @param argv command line arg array
601 * @return return code
604 main (int argc, char **argv)
606 struct GNUNET_SIGNAL_Context *shc_chld;
608 struct GNUNET_GETOPT_CommandLineOption options[] = {
609 GNUNET_GETOPT_OPTION_END
614 if (NULL == (sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
615 GNUNET_NO, GNUNET_NO)))
621 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
623 GNUNET_PROGRAM_run (argc, argv, "gnunet-helper-testbed",
624 "Helper for starting gnunet-service-testbed", options,
626 GNUNET_SIGNAL_handler_uninstall (shc_chld);
628 GNUNET_DISK_pipe_close (sigpipe);
629 if (GNUNET_OK != ret)
631 return (GNUNET_OK == status) ? 0 : 1;
634 /* end of gnunet-helper-testbed.c */