2 This file is part of GNUnet
3 Copyright (C) 2008--2013, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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, ...) GNUNET_log (kind, __VA_ARGS__)
55 * Debug logging shorthand
57 #define LOG_DEBUG(...) LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
61 * We need pipe control only on WINDOWS
64 #define PIPE_CONTROL GNUNET_YES
66 #define PIPE_CONTROL GNUNET_NO
71 * Context for a single write on a chunk of memory
81 * The length of the data
86 * The current position from where the write operation should begin
93 * Handle to the testing system
95 static struct GNUNET_TESTING_System *test_system;
98 * Our message stream tokenizer
100 struct GNUNET_MessageStreamTokenizer *tokenizer;
103 * Disk handle from stdin
105 static struct GNUNET_DISK_FileHandle *stdin_fd;
108 * Disk handle for stdout
110 static struct GNUNET_DISK_FileHandle *stdout_fd;
113 * The process handle to the testbed service
115 static struct GNUNET_OS_Process *testbed;
118 * Pipe used to communicate shutdown via signal.
120 static struct GNUNET_DISK_PipeHandle *sigpipe;
123 * Task identifier for the read task
125 static struct GNUNET_SCHEDULER_Task *read_task_id;
128 * Task identifier for the write task
130 static struct GNUNET_SCHEDULER_Task *write_task_id;
133 * Task to kill the child
135 static struct GNUNET_SCHEDULER_Task *child_death_task_id;
138 * Are we done reading messages from stdin?
140 static int done_reading;
143 * Result to return in case we fail
149 * Task to shut down cleanly
154 shutdown_task (void *cls)
156 LOG_DEBUG ("Shutting down\n");
159 LOG_DEBUG ("Killing testbed\n");
160 GNUNET_break (0 == GNUNET_OS_process_kill (testbed, GNUNET_TERM_SIG));
162 if (NULL != read_task_id)
164 GNUNET_SCHEDULER_cancel (read_task_id);
167 if (NULL != write_task_id)
169 struct WriteContext *wc;
171 wc = GNUNET_SCHEDULER_cancel (write_task_id);
172 write_task_id = NULL;
173 GNUNET_free (wc->data);
176 if (NULL != child_death_task_id)
178 GNUNET_SCHEDULER_cancel (child_death_task_id);
179 child_death_task_id = NULL;
181 if (NULL != stdin_fd)
182 (void) GNUNET_DISK_file_close (stdin_fd);
183 if (NULL != stdout_fd)
184 (void) GNUNET_DISK_file_close (stdout_fd);
185 GNUNET_MST_destroy (tokenizer);
189 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (testbed));
190 GNUNET_OS_process_destroy (testbed);
193 if (NULL != test_system)
195 GNUNET_TESTING_system_destroy (test_system, GNUNET_YES);
202 * Task to write to the standard out
204 * @param cls the WriteContext
207 write_task (void *cls)
209 struct WriteContext *wc = cls;
212 GNUNET_assert (NULL != wc);
213 write_task_id = NULL;
214 bytes_wrote = GNUNET_DISK_file_write (stdout_fd,
216 wc->length - wc->pos);
217 if (GNUNET_SYSERR == bytes_wrote)
219 LOG (GNUNET_ERROR_TYPE_WARNING, "Cannot reply back configuration\n");
220 GNUNET_free (wc->data);
224 wc->pos += bytes_wrote;
225 if (wc->pos == wc->length)
227 GNUNET_free (wc->data);
231 write_task_id = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
239 * Task triggered whenever we receive a SIGCHLD (child
242 * @param cls closure, NULL if we need to self-restart
245 child_death_task (void *cls)
247 const struct GNUNET_DISK_FileHandle *pr;
249 enum GNUNET_OS_ProcessStatusType type;
253 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
254 child_death_task_id = NULL;
255 /* consume the signal */
256 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
257 LOG_DEBUG ("Got SIGCHLD\n");
263 GNUNET_break (GNUNET_SYSERR !=
264 (ret = GNUNET_OS_process_status (testbed, &type, &code)));
265 if (GNUNET_NO != ret)
267 GNUNET_OS_process_destroy (testbed);
269 /* Send SIGTERM to our process group */
270 if (0 != PLIBC_KILL (0, GNUNET_TERM_SIG))
272 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "signal");
273 GNUNET_SCHEDULER_shutdown (); /* Couldn't send the signal, we shutdown frowning */
277 LOG_DEBUG ("Child hasn't died. Resuming to monitor its status\n");
278 child_death_task_id =
279 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
287 * Functions with this signature are called whenever a
288 * complete message is received by the tokenizer.
290 * Do not call #GNUNET_mst_destroy() in this callback
292 * @param cls identification of the client
293 * @param message the actual message
294 * @return #GNUNET_OK on success,
295 * #GNUNET_NO to stop further processing (no error)
296 * #GNUNET_SYSERR to stop further processing with error
299 tokenizer_cb (void *cls, const struct GNUNET_MessageHeader *message)
301 const struct GNUNET_TESTBED_HelperInit *msg;
302 struct GNUNET_TESTBED_HelperReply *reply;
303 struct GNUNET_CONFIGURATION_Handle *cfg;
304 struct WriteContext *wc;
313 uLongf ul_config_size;
315 uint16_t trusted_ip_size;
316 uint16_t hostname_size;
319 msize = ntohs (message->size);
320 if ((sizeof (struct GNUNET_TESTBED_HelperInit) >= msize) ||
321 (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT != ntohs (message->type)))
323 LOG (GNUNET_ERROR_TYPE_WARNING, "Received unexpected message -- exiting\n");
326 msg = (const struct GNUNET_TESTBED_HelperInit *) message;
327 trusted_ip_size = ntohs (msg->trusted_ip_size);
328 trusted_ip = (char *) &msg[1];
329 if ('\0' != trusted_ip[trusted_ip_size])
331 LOG (GNUNET_ERROR_TYPE_WARNING, "Trusted IP cannot be empty -- exiting\n");
334 hostname_size = ntohs (msg->hostname_size);
335 if ((sizeof (struct GNUNET_TESTBED_HelperInit) + trusted_ip_size + 1 +
336 hostname_size) >= msize)
339 LOG (GNUNET_ERROR_TYPE_WARNING, "Received unexpected message -- exiting\n");
342 ul_config_size = (uLongf) ntohs (msg->config_size);
343 config = GNUNET_malloc (ul_config_size);
344 xconfig_size = msize - (trusted_ip_size + 1 + hostname_size +
345 sizeof (struct GNUNET_TESTBED_HelperInit));
346 int ret = uncompress ((Bytef *) config,
348 (const Bytef *) (trusted_ip + trusted_ip_size + 1 +
350 (uLongf) xconfig_size);
356 LOG (GNUNET_ERROR_TYPE_ERROR, "Not enough memory for decompression\n");
359 LOG (GNUNET_ERROR_TYPE_ERROR, "Output buffer too small\n");
362 LOG (GNUNET_ERROR_TYPE_ERROR, "Data corrupted/incomplete\n");
367 LOG (GNUNET_ERROR_TYPE_ERROR,
368 "Error while uncompressing config -- exiting\n");
369 GNUNET_free (config);
372 cfg = GNUNET_CONFIGURATION_create ();
374 GNUNET_CONFIGURATION_deserialize (cfg, config, ul_config_size, NULL))
376 LOG (GNUNET_ERROR_TYPE_ERROR, "Unable to deserialize config -- exiting\n");
377 GNUNET_free (config);
380 GNUNET_free (config);
382 if (0 != hostname_size)
384 hostname = GNUNET_malloc (hostname_size + 1);
385 GNUNET_strlcpy (hostname,
386 ((char *) &msg[1]) + trusted_ip_size + 1,
389 /* unset GNUNET_TESTING_PREFIX if present as it is more relevant for testbed */
390 evstr = getenv (GNUNET_TESTING_PREFIX);
393 /* unsetting the variable will invalidate the pointer! */
394 evstr = GNUNET_strdup (evstr);
396 GNUNET_break (0 != SetEnvironmentVariable (GNUNET_TESTING_PREFIX, NULL));
398 GNUNET_break (0 == unsetenv (GNUNET_TESTING_PREFIX));
402 GNUNET_TESTING_system_create ("testbed-helper", trusted_ip, hostname, NULL);
406 GNUNET_assert (0 != SetEnvironmentVariable (GNUNET_TESTING_PREFIX, evstr));
410 GNUNET_asprintf (&evar, GNUNET_TESTING_PREFIX "=%s", evstr);
411 GNUNET_assert (0 == putenv (evar)); /* consumes 'evar',
412 see putenv(): becomes part of envrionment! */
417 GNUNET_free_non_null (hostname);
419 GNUNET_assert (NULL != test_system);
420 GNUNET_assert (GNUNET_OK ==
421 GNUNET_TESTING_configuration_create (test_system, cfg));
422 GNUNET_assert (GNUNET_OK ==
423 GNUNET_CONFIGURATION_get_value_filename (cfg,
427 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, config))
429 LOG (GNUNET_ERROR_TYPE_WARNING,
430 "Unable to write config file: %s -- exiting\n",
432 GNUNET_CONFIGURATION_destroy (cfg);
433 GNUNET_free (config);
436 LOG_DEBUG ("Staring testbed with config: %s\n", config);
437 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-testbed");
441 /* expose testbed configuration through env variable */
442 GNUNET_asprintf (&evar, "%s=%s", ENV_TESTBED_CONFIG, config);
443 GNUNET_assert (0 == putenv (evar)); /* consumes 'evar',
444 see putenv(): becomes part of envrionment! */
447 testbed = GNUNET_OS_start_process (PIPE_CONTROL,
448 GNUNET_OS_INHERIT_STD_ERR /*verbose? */,
453 "gnunet-service-testbed",
457 GNUNET_free (binary);
458 GNUNET_free (config);
461 LOG (GNUNET_ERROR_TYPE_WARNING,
462 "Error starting gnunet-service-testbed -- exiting\n");
463 GNUNET_CONFIGURATION_destroy (cfg);
466 done_reading = GNUNET_YES;
467 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
468 GNUNET_CONFIGURATION_destroy (cfg);
471 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
472 GNUNET_free (config);
473 wc = GNUNET_new (struct WriteContext);
474 wc->length = xconfig_size + sizeof (struct GNUNET_TESTBED_HelperReply);
475 reply = GNUNET_realloc (xconfig, wc->length);
476 memmove (&reply[1], reply, xconfig_size);
477 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY);
478 reply->header.size = htons ((uint16_t) wc->length);
479 reply->config_size = htons ((uint16_t) config_size);
481 write_task_id = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
485 child_death_task_id = GNUNET_SCHEDULER_add_read_file (
486 GNUNET_TIME_UNIT_FOREVER_REL,
487 GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ),
493 status = GNUNET_SYSERR;
494 GNUNET_SCHEDULER_shutdown ();
495 return GNUNET_SYSERR;
500 * Task to read from stdin
505 read_task (void *cls)
507 char buf[GNUNET_MAX_MESSAGE_SIZE];
511 sread = GNUNET_DISK_file_read (stdin_fd, buf, sizeof (buf));
512 if ((GNUNET_SYSERR == sread) || (0 == sread))
514 LOG_DEBUG ("STDIN closed\n");
515 GNUNET_SCHEDULER_shutdown ();
518 if (GNUNET_YES == done_reading)
520 /* didn't expect any more data! */
522 GNUNET_SCHEDULER_shutdown ();
525 LOG_DEBUG ("Read %u bytes\n", (unsigned int) sread);
526 /* FIXME: could introduce a GNUNET_MST_read2 to read
527 directly from 'stdin_fd' and save a memcpy() here */
529 GNUNET_MST_from_buffer (tokenizer, buf, sread, GNUNET_NO, GNUNET_NO))
532 GNUNET_SCHEDULER_shutdown ();
535 read_task_id /* No timeout while reading */
536 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
544 * Main function that will be run.
547 * @param args remaining command-line arguments
548 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
549 * @param cfg configuration
555 const struct GNUNET_CONFIGURATION_Handle *cfg)
557 LOG_DEBUG ("Starting testbed helper...\n");
558 tokenizer = GNUNET_MST_create (&tokenizer_cb, NULL);
559 stdin_fd = GNUNET_DISK_get_handle_from_native (stdin);
560 stdout_fd = GNUNET_DISK_get_handle_from_native (stdout);
561 read_task_id = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
565 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
570 * Signal handler called for SIGCHLD.
573 sighandler_child_death ()
576 int old_errno; /* back-up errno */
581 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe,
582 GNUNET_DISK_PIPE_END_WRITE),
592 * @param argc the number of command line arguments
593 * @param argv command line arg array
594 * @return return code
597 main (int argc, char **argv)
599 struct GNUNET_SIGNAL_Context *shc_chld;
600 struct GNUNET_GETOPT_CommandLineOption options[] = {GNUNET_GETOPT_OPTION_END};
605 (sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO)))
611 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
612 ret = GNUNET_PROGRAM_run (argc,
614 "gnunet-helper-testbed",
615 "Helper for starting gnunet-service-testbed",
619 GNUNET_SIGNAL_handler_uninstall (shc_chld);
621 GNUNET_DISK_pipe_close (sigpipe);
622 if (GNUNET_OK != ret)
624 return (GNUNET_OK == status) ? 0 : 1;
627 /* end of gnunet-helper-testbed.c */