2 This file is part of GNUnet
3 (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., 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. 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__)
62 * Log an error message at log-level 'level' that indicates a failure of the
63 * command 'cmd' with the message given by gai_strerror(rc).
65 #define LOG_GAI(level, cmd, rc) do { LOG(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gai_strerror(rc)); } while(0)
69 * We need pipe control only on WINDOWS
72 #define PIPE_CONTROL GNUNET_YES
74 #define PIPE_CONTROL GNUNET_NO
79 * Context for a single write on a chunk of memory
89 * The length of the data
94 * The current position from where the write operation should begin
101 * Handle to the testing system
103 static struct GNUNET_TESTING_System *test_system;
106 * Our message stream tokenizer
108 struct GNUNET_SERVER_MessageStreamTokenizer *tokenizer;
111 * Disk handle from stdin
113 static struct GNUNET_DISK_FileHandle *stdin_fd;
116 * Disk handle for stdout
118 static struct GNUNET_DISK_FileHandle *stdout_fd;
121 * The process handle to the testbed service
123 static struct GNUNET_OS_Process *testbed;
126 * Pipe used to communicate shutdown via signal.
128 static struct GNUNET_DISK_PipeHandle *sigpipe;
131 * Task identifier for the read task
133 static GNUNET_SCHEDULER_TaskIdentifier read_task_id;
136 * Task identifier for the write task
138 static GNUNET_SCHEDULER_TaskIdentifier write_task_id;
141 * Task to kill the child
143 static GNUNET_SCHEDULER_TaskIdentifier child_death_task_id;
148 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
151 * Are we done reading messages from stdin?
153 static int done_reading;
156 * Result to return in case we fail
162 * Task to shut down cleanly
165 * @param tc the task context
168 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
170 LOG_DEBUG ("Shutting down\n");
171 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
174 LOG_DEBUG ("Killing testbed\n");
175 GNUNET_break (0 == GNUNET_OS_process_kill (testbed, SIGTERM));
177 if (GNUNET_SCHEDULER_NO_TASK != read_task_id)
179 GNUNET_SCHEDULER_cancel (read_task_id);
180 read_task_id = GNUNET_SCHEDULER_NO_TASK;
182 if (GNUNET_SCHEDULER_NO_TASK != write_task_id)
184 GNUNET_SCHEDULER_cancel (write_task_id);
185 write_task_id = GNUNET_SCHEDULER_NO_TASK;
187 if (GNUNET_SCHEDULER_NO_TASK != child_death_task_id)
189 GNUNET_SCHEDULER_cancel (child_death_task_id);
190 child_death_task_id = GNUNET_SCHEDULER_NO_TASK;
192 if (NULL != stdin_fd)
193 (void) GNUNET_DISK_file_close (stdin_fd);
194 if (NULL != stdout_fd)
195 (void) GNUNET_DISK_file_close (stdout_fd);
196 GNUNET_SERVER_mst_destroy (tokenizer);
200 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (testbed));
201 GNUNET_OS_process_destroy (testbed);
204 if (NULL != test_system)
206 GNUNET_TESTING_system_destroy (test_system, GNUNET_YES);
213 * Scheduler shutdown task to be run now.
218 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task_id)
219 GNUNET_SCHEDULER_cancel (shutdown_task_id);
220 shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
225 * Task to write to the standard out
227 * @param cls the WriteContext
228 * @param tc the TaskContext
231 write_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
233 struct WriteContext *wc = cls;
236 GNUNET_assert (NULL != wc);
237 write_task_id = GNUNET_SCHEDULER_NO_TASK;
238 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
240 GNUNET_free (wc->data);
245 GNUNET_DISK_file_write (stdout_fd, wc->data + wc->pos,
246 wc->length - wc->pos);
247 if (GNUNET_SYSERR == bytes_wrote)
249 LOG (GNUNET_ERROR_TYPE_WARNING, "Cannot reply back configuration\n");
250 GNUNET_free (wc->data);
254 wc->pos += bytes_wrote;
255 if (wc->pos == wc->length)
257 GNUNET_free (wc->data);
262 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, stdout_fd,
268 * Task triggered whenever we receive a SIGCHLD (child
271 * @param cls closure, NULL if we need to self-restart
275 child_death_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
277 const struct GNUNET_DISK_FileHandle *pr;
279 enum GNUNET_OS_ProcessStatusType type;
283 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
284 child_death_task_id = GNUNET_SCHEDULER_NO_TASK;
285 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
287 child_death_task_id =
288 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
289 pr, &child_death_task, NULL);
292 /* consume the signal */
293 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
294 LOG_DEBUG ("Got SIGCHLD\n");
300 GNUNET_break (GNUNET_SYSERR !=
301 (ret = GNUNET_OS_process_status (testbed, &type, &code)));
302 if (GNUNET_NO != ret)
304 GNUNET_OS_process_destroy (testbed);
306 /* Send SIGTERM to our process group */
307 if (0 != PLIBC_KILL (0, SIGTERM))
309 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "signal");
310 shutdown_now (); /* Couldn't send the signal, we shutdown frowning */
314 LOG_DEBUG ("Child hasn't died. Resuming to monitor its status\n");
315 child_death_task_id =
316 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
317 pr, &child_death_task, NULL);
322 * Resolves a hostname using getaddrinfo
324 * @param host the hostname
325 * @return the string representing the IPv4 address of the given host; NULL upon error
328 simple_resolve (const char *host)
330 struct addrinfo *res;
331 const struct sockaddr_in *in_addr;
333 struct addrinfo hint;
336 hint.ai_family = AF_INET; /* IPv4 */
337 hint.ai_socktype = 0;
338 hint.ai_protocol = 0;
341 hint.ai_canonname = NULL;
343 hint.ai_flags = AI_NUMERICSERV;
345 LOG_DEBUG ("Resolving [%s]\n", host);
346 if (0 != (rc = getaddrinfo (host, "22", &hint, &res)))
348 LOG_GAI (GNUNET_ERROR_TYPE_ERROR, "getaddrinfo", rc);
351 GNUNET_assert (NULL != res);
352 GNUNET_assert (NULL != res->ai_addr);
353 GNUNET_assert (sizeof (struct sockaddr_in) == res->ai_addrlen);
354 in_addr = (const struct sockaddr_in *) res->ai_addr;
355 hostip = inet_ntoa (in_addr->sin_addr);
356 GNUNET_assert (NULL != hostip);
358 LOG_DEBUG ("Resolved [%s] to [%s]\n", host, hostip);
364 * Functions with this signature are called whenever a
365 * complete message is received by the tokenizer.
367 * Do not call GNUNET_SERVER_mst_destroy in callback
370 * @param client identification of the client
371 * @param message the actual message
373 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
376 tokenizer_cb (void *cls, void *client,
377 const struct GNUNET_MessageHeader *message)
379 const struct GNUNET_TESTBED_HelperInit *msg;
380 struct GNUNET_TESTBED_HelperReply *reply;
381 struct GNUNET_CONFIGURATION_Handle *cfg;
382 struct WriteContext *wc;
390 uLongf ul_config_size;
392 uint16_t trusted_ip_size;
393 uint16_t hostname_size;
396 msize = ntohs (message->size);
397 if ((sizeof (struct GNUNET_TESTBED_HelperInit) >= msize) ||
398 (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT != ntohs (message->type)))
400 LOG (GNUNET_ERROR_TYPE_WARNING, "Received unexpected message -- exiting\n");
403 msg = (const struct GNUNET_TESTBED_HelperInit *) message;
404 trusted_ip_size = ntohs (msg->trusted_ip_size);
405 trusted_ip = (char *) &msg[1];
406 if ('\0' != trusted_ip[trusted_ip_size])
408 LOG (GNUNET_ERROR_TYPE_WARNING, "Trusted IP cannot be empty -- exiting\n");
411 hostname_size = ntohs (msg->hostname_size);
412 if ((sizeof (struct GNUNET_TESTBED_HelperInit) + trusted_ip_size + 1 +
413 hostname_size) >= msize)
416 LOG (GNUNET_ERROR_TYPE_WARNING, "Received unexpected message -- exiting\n");
419 ul_config_size = (uLongf) ntohs (msg->config_size);
420 config = GNUNET_malloc (ul_config_size);
422 ntohs (message->size) - (trusted_ip_size + 1 +
423 sizeof (struct GNUNET_TESTBED_HelperInit));
425 uncompress ((Bytef *) config, &ul_config_size,
426 (const Bytef *) (trusted_ip + trusted_ip_size + 1 +
427 hostname_size), (uLongf) xconfig_size))
429 LOG (GNUNET_ERROR_TYPE_WARNING,
430 "Error while uncompressing config -- exiting\n");
431 GNUNET_free (config);
434 cfg = GNUNET_CONFIGURATION_create ();
436 GNUNET_CONFIGURATION_deserialize (cfg, config, ul_config_size, GNUNET_NO))
438 LOG (GNUNET_ERROR_TYPE_WARNING,
439 "Unable to deserialize config -- exiting\n");
440 GNUNET_free (config);
443 GNUNET_free (config);
445 if (0 != hostname_size)
447 hostname = GNUNET_malloc (hostname_size + 1);
448 (void) strncpy (hostname, ((char *) &msg[1]) + trusted_ip_size + 1,
450 hostname[hostname_size] = '\0';
453 if ( (NULL != hostname) && (NULL == (hostip = simple_resolve (hostname))) )
455 GNUNET_free (hostname);
459 GNUNET_TESTING_system_create ("testbed-helper", trusted_ip, hostip, NULL);
460 GNUNET_free_non_null (hostname);
463 GNUNET_assert (NULL != test_system);
464 GNUNET_assert (GNUNET_OK ==
465 GNUNET_TESTING_configuration_create (test_system, cfg));
466 GNUNET_assert (GNUNET_OK ==
467 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
470 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, config))
472 LOG (GNUNET_ERROR_TYPE_WARNING,
473 "Unable to write config file: %s -- exiting\n", config);
474 GNUNET_CONFIGURATION_destroy (cfg);
475 GNUNET_free (config);
478 LOG_DEBUG ("Staring testbed with config: %s\n", config);
479 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-testbed");
481 GNUNET_OS_start_process (PIPE_CONTROL,
482 GNUNET_OS_INHERIT_STD_ERR /*verbose? */ , NULL,
483 NULL, binary, "gnunet-service-testbed", "-c",
485 GNUNET_free (binary);
486 GNUNET_free (config);
489 LOG (GNUNET_ERROR_TYPE_WARNING,
490 "Error starting gnunet-service-testbed -- exiting\n");
491 GNUNET_CONFIGURATION_destroy (cfg);
494 done_reading = GNUNET_YES;
495 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
496 GNUNET_CONFIGURATION_destroy (cfg);
499 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
500 GNUNET_free (config);
501 wc = GNUNET_malloc (sizeof (struct WriteContext));
502 wc->length = xconfig_size + sizeof (struct GNUNET_TESTBED_HelperReply);
503 reply = GNUNET_realloc (xconfig, wc->length);
504 memmove (&reply[1], reply, xconfig_size);
505 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY);
506 reply->header.size = htons ((uint16_t) wc->length);
507 reply->config_size = htons ((uint16_t) config_size);
510 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, stdout_fd,
512 child_death_task_id =
513 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
514 GNUNET_DISK_pipe_handle (sigpipe,
515 GNUNET_DISK_PIPE_END_READ),
516 &child_death_task, NULL);
520 status = GNUNET_SYSERR;
522 return GNUNET_SYSERR;
527 * Task to read from stdin
530 * @param tc the task context
533 read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
535 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
538 read_task_id = GNUNET_SCHEDULER_NO_TASK;
539 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
541 sread = GNUNET_DISK_file_read (stdin_fd, buf, sizeof (buf));
542 if ((GNUNET_SYSERR == sread) || (0 == sread))
544 LOG_DEBUG ("STDIN closed\n");
548 if (GNUNET_YES == done_reading)
550 /* didn't expect any more data! */
555 LOG_DEBUG ("Read %u bytes\n", sread);
557 GNUNET_SERVER_mst_receive (tokenizer, NULL, buf, sread, GNUNET_NO,
564 read_task_id = /* No timeout while reading */
565 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fd,
571 * Main function that will be run.
574 * @param args remaining command-line arguments
575 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
576 * @param cfg configuration
579 run (void *cls, char *const *args, const char *cfgfile,
580 const struct GNUNET_CONFIGURATION_Handle *cfg)
582 LOG_DEBUG ("Starting testbed helper...\n");
583 tokenizer = GNUNET_SERVER_mst_create (&tokenizer_cb, NULL);
584 stdin_fd = GNUNET_DISK_get_handle_from_native (stdin);
585 stdout_fd = GNUNET_DISK_get_handle_from_native (stdout);
587 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fd,
590 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
596 * Signal handler called for SIGCHLD.
599 sighandler_child_death ()
602 int old_errno; /* back-up errno */
606 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
607 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
616 * @param argc the number of command line arguments
617 * @param argv command line arg array
618 * @return return code
621 main (int argc, char **argv)
623 struct GNUNET_SIGNAL_Context *shc_chld;
625 struct GNUNET_GETOPT_CommandLineOption options[] = {
626 GNUNET_GETOPT_OPTION_END
631 if (NULL == (sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
632 GNUNET_NO, GNUNET_NO)))
639 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
641 GNUNET_PROGRAM_run (argc, argv, "gnunet-helper-testbed",
642 "Helper for starting gnunet-service-testbed", options,
644 GNUNET_SIGNAL_handler_uninstall (shc_chld);
646 GNUNET_DISK_pipe_close (sigpipe);
647 if (GNUNET_OK != ret)
649 return (GNUNET_OK == status) ? 0 : 1;
652 /* end of gnunet-helper-testbed.c */