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.
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.
34 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
39 #include "gnunet_util_lib.h"
40 #include "gnunet_testing_lib.h"
41 #include "gnunet_testbed_service.h"
42 #include "testbed_helper.h"
43 #include "testbed_api.h"
47 * Generic logging shortcut
49 #define LOG(kind, ...) \
50 GNUNET_log (kind, __VA_ARGS__)
53 * Debug logging shorthand
55 #define LOG_DEBUG(...) \
56 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
60 * We need pipe control only on WINDOWS
63 #define PIPE_CONTROL GNUNET_YES
65 #define PIPE_CONTROL GNUNET_NO
70 * Context for a single write on a chunk of memory
80 * The length of the data
85 * The current position from where the write operation should begin
92 * Handle to the testing system
94 static struct GNUNET_TESTING_System *test_system;
97 * Our message stream tokenizer
99 struct GNUNET_SERVER_MessageStreamTokenizer *tokenizer;
102 * Disk handle from stdin
104 static struct GNUNET_DISK_FileHandle *stdin_fd;
107 * Disk handle for stdout
109 static struct GNUNET_DISK_FileHandle *stdout_fd;
112 * The process handle to the testbed service
114 static struct GNUNET_OS_Process *testbed;
117 * Pipe used to communicate shutdown via signal.
119 static struct GNUNET_DISK_PipeHandle *sigpipe;
122 * Task identifier for the read task
124 static GNUNET_SCHEDULER_TaskIdentifier read_task_id;
127 * Task identifier for the write task
129 static GNUNET_SCHEDULER_TaskIdentifier write_task_id;
132 * Task to kill the child
134 static GNUNET_SCHEDULER_TaskIdentifier child_death_task_id;
139 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
142 * Are we done reading messages from stdin?
144 static int done_reading;
147 * Result to return in case we fail
153 * Task to shut down cleanly
156 * @param tc the task context
159 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
161 LOG_DEBUG ("Shutting down\n");
162 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
165 LOG_DEBUG ("Killing testbed\n");
166 GNUNET_break (0 == GNUNET_OS_process_kill (testbed, SIGTERM));
168 if (GNUNET_SCHEDULER_NO_TASK != read_task_id)
170 GNUNET_SCHEDULER_cancel (read_task_id);
171 read_task_id = GNUNET_SCHEDULER_NO_TASK;
173 if (GNUNET_SCHEDULER_NO_TASK != write_task_id)
175 GNUNET_SCHEDULER_cancel (write_task_id);
176 write_task_id = GNUNET_SCHEDULER_NO_TASK;
178 if (GNUNET_SCHEDULER_NO_TASK != child_death_task_id)
180 GNUNET_SCHEDULER_cancel (child_death_task_id);
181 child_death_task_id = GNUNET_SCHEDULER_NO_TASK;
183 if (NULL != stdin_fd)
184 (void) GNUNET_DISK_file_close (stdin_fd);
185 if (NULL != stdout_fd)
186 (void) GNUNET_DISK_file_close (stdout_fd);
187 GNUNET_SERVER_mst_destroy (tokenizer);
191 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (testbed));
192 GNUNET_OS_process_destroy (testbed);
195 if (NULL != test_system)
197 GNUNET_TESTING_system_destroy (test_system, GNUNET_YES);
204 * Scheduler shutdown task to be run now.
209 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task_id)
210 GNUNET_SCHEDULER_cancel (shutdown_task_id);
211 shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
216 * Task to write to the standard out
218 * @param cls the WriteContext
219 * @param tc the TaskContext
222 write_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
224 struct WriteContext *wc = cls;
227 GNUNET_assert (NULL != wc);
228 write_task_id = GNUNET_SCHEDULER_NO_TASK;
229 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
231 GNUNET_free (wc->data);
236 GNUNET_DISK_file_write (stdout_fd, wc->data + wc->pos,
237 wc->length - wc->pos);
238 if (GNUNET_SYSERR == bytes_wrote)
240 LOG (GNUNET_ERROR_TYPE_WARNING, "Cannot reply back configuration\n");
241 GNUNET_free (wc->data);
245 wc->pos += bytes_wrote;
246 if (wc->pos == wc->length)
248 GNUNET_free (wc->data);
253 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, stdout_fd,
259 * Task triggered whenever we receive a SIGCHLD (child
262 * @param cls closure, NULL if we need to self-restart
266 child_death_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
268 const struct GNUNET_DISK_FileHandle *pr;
271 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
272 child_death_task_id = GNUNET_SCHEDULER_NO_TASK;
273 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
275 child_death_task_id =
276 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
277 pr, &child_death_task, NULL);
280 /* consume the signal */
281 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
282 LOG_DEBUG ("Child died\n");
285 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (testbed));
286 GNUNET_OS_process_destroy (testbed);
294 * Functions with this signature are called whenever a
295 * complete message is received by the tokenizer.
297 * Do not call GNUNET_SERVER_mst_destroy in callback
300 * @param client identification of the client
301 * @param message the actual message
303 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
306 tokenizer_cb (void *cls, void *client,
307 const struct GNUNET_MessageHeader *message)
309 const struct GNUNET_TESTBED_HelperInit *msg;
310 struct GNUNET_TESTBED_HelperReply *reply;
311 struct GNUNET_CONFIGURATION_Handle *cfg;
312 struct WriteContext *wc;
319 uLongf ul_config_size;
321 uint16_t trusted_ip_size;
322 uint16_t hostname_size;
325 msize = ntohs (message->size);
326 if ((sizeof (struct GNUNET_TESTBED_HelperInit) >= msize) ||
327 (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT != ntohs (message->type)))
329 LOG (GNUNET_ERROR_TYPE_WARNING, "Received unexpected message -- exiting\n");
332 msg = (const struct GNUNET_TESTBED_HelperInit *) message;
333 trusted_ip_size = ntohs (msg->trusted_ip_size);
334 trusted_ip = (char *) &msg[1];
335 if ('\0' != trusted_ip[trusted_ip_size])
337 LOG (GNUNET_ERROR_TYPE_WARNING, "Trusted IP cannot be empty -- exiting\n");
340 hostname_size = ntohs (msg->hostname_size);
341 if ((sizeof (struct GNUNET_TESTBED_HelperInit) + trusted_ip_size + 1 +
342 hostname_size) >= msize)
345 LOG (GNUNET_ERROR_TYPE_WARNING, "Received unexpected message -- exiting\n");
348 ul_config_size = (uLongf) ntohs (msg->config_size);
349 config = GNUNET_malloc (ul_config_size);
351 ntohs (message->size) - (trusted_ip_size + 1 +
352 sizeof (struct GNUNET_TESTBED_HelperInit));
354 uncompress ((Bytef *) config, &ul_config_size,
355 (const Bytef *) (trusted_ip + trusted_ip_size + 1 +
356 hostname_size), (uLongf) xconfig_size))
358 LOG (GNUNET_ERROR_TYPE_WARNING,
359 "Error while uncompressing config -- exiting\n");
360 GNUNET_free (config);
363 cfg = GNUNET_CONFIGURATION_create ();
365 GNUNET_CONFIGURATION_deserialize (cfg, config, ul_config_size, GNUNET_NO))
367 LOG (GNUNET_ERROR_TYPE_WARNING,
368 "Unable to deserialize config -- exiting\n");
369 GNUNET_free (config);
372 GNUNET_free (config);
374 if (0 != hostname_size)
376 hostname = GNUNET_malloc (hostname_size + 1);
377 (void) strncpy (hostname, ((char *) &msg[1]) + trusted_ip_size + 1,
379 hostname[hostname_size] = '\0';
382 GNUNET_TESTING_system_create ("testbed-helper", trusted_ip, hostname);
383 GNUNET_free_non_null (hostname);
385 GNUNET_assert (NULL != test_system);
386 GNUNET_assert (GNUNET_OK ==
387 GNUNET_TESTING_configuration_create (test_system, cfg));
388 GNUNET_assert (GNUNET_OK ==
389 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
392 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, config))
394 LOG (GNUNET_ERROR_TYPE_WARNING,
395 "Unable to write config file: %s -- exiting\n", config);
396 GNUNET_CONFIGURATION_destroy (cfg);
397 GNUNET_free (config);
400 LOG_DEBUG ("Staring testbed with config: %s\n", config);
401 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-testbed");
403 GNUNET_OS_start_process (PIPE_CONTROL,
404 GNUNET_OS_INHERIT_STD_ERR /*verbose? */ , NULL,
405 NULL, binary, "gnunet-service-testbed", "-c",
407 GNUNET_free (binary);
408 GNUNET_free (config);
411 LOG (GNUNET_ERROR_TYPE_WARNING,
412 "Error starting gnunet-service-testbed -- exiting\n");
413 GNUNET_CONFIGURATION_destroy (cfg);
416 done_reading = GNUNET_YES;
417 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
418 GNUNET_CONFIGURATION_destroy (cfg);
421 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
422 GNUNET_free (config);
423 wc = GNUNET_malloc (sizeof (struct WriteContext));
424 wc->length = xconfig_size + sizeof (struct GNUNET_TESTBED_HelperReply);
425 reply = GNUNET_realloc (xconfig, wc->length);
426 memmove (&reply[1], reply, xconfig_size);
427 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY);
428 reply->header.size = htons ((uint16_t) wc->length);
429 reply->config_size = htons ((uint16_t) config_size);
432 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, stdout_fd,
434 child_death_task_id =
435 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
436 GNUNET_DISK_pipe_handle (sigpipe,
437 GNUNET_DISK_PIPE_END_READ),
438 &child_death_task, NULL);
442 status = GNUNET_SYSERR;
444 return GNUNET_SYSERR;
449 * Task to read from stdin
452 * @param tc the task context
455 read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
457 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
460 read_task_id = GNUNET_SCHEDULER_NO_TASK;
461 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
463 sread = GNUNET_DISK_file_read (stdin_fd, buf, sizeof (buf));
464 if ((GNUNET_SYSERR == sread) || (0 == sread))
466 LOG_DEBUG ("STDIN closed\n");
470 if (GNUNET_YES == done_reading)
472 /* didn't expect any more data! */
477 LOG_DEBUG ("Read %u bytes\n", sread);
479 GNUNET_SERVER_mst_receive (tokenizer, NULL, buf, sread, GNUNET_NO,
486 read_task_id = /* No timeout while reading */
487 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fd,
493 * Main function that will be run.
496 * @param args remaining command-line arguments
497 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
498 * @param cfg configuration
501 run (void *cls, char *const *args, const char *cfgfile,
502 const struct GNUNET_CONFIGURATION_Handle *cfg)
504 LOG_DEBUG ("Starting testbed helper...\n");
505 tokenizer = GNUNET_SERVER_mst_create (&tokenizer_cb, NULL);
506 stdin_fd = GNUNET_DISK_get_handle_from_native (stdin);
507 stdout_fd = GNUNET_DISK_get_handle_from_native (stdout);
509 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fd,
512 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
518 * Signal handler called for SIGCHLD.
521 sighandler_child_death ()
524 int old_errno; /* back-up errno */
528 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
529 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
538 * @param argc the number of command line arguments
539 * @param argv command line arg array
540 * @return return code
543 main (int argc, char **argv)
545 struct GNUNET_SIGNAL_Context *shc_chld;
547 struct GNUNET_GETOPT_CommandLineOption options[] = {
548 GNUNET_GETOPT_OPTION_END
553 if (NULL == (sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
554 GNUNET_NO, GNUNET_NO)))
561 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
563 GNUNET_PROGRAM_run (argc, argv, "gnunet-helper-testbed",
564 "Helper for starting gnunet-service-testbed", options,
566 GNUNET_SIGNAL_handler_uninstall (shc_chld);
568 GNUNET_DISK_pipe_close (sigpipe);
569 if (GNUNET_OK != ret)
571 return (GNUNET_OK == status) ? 0 : 1;
574 /* end of gnunet-helper-testbed.c */