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, ...) \
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_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;
140 * Are we done reading messages from stdin?
142 static int done_reading;
145 * Result to return in case we fail
151 * Task to shut down cleanly
156 shutdown_task (void *cls)
158 LOG_DEBUG ("Shutting down\n");
161 LOG_DEBUG ("Killing testbed\n");
162 GNUNET_break (0 == GNUNET_OS_process_kill (testbed, GNUNET_TERM_SIG));
164 if (NULL != read_task_id)
166 GNUNET_SCHEDULER_cancel (read_task_id);
169 if (NULL != write_task_id)
171 struct WriteContext *wc;
173 wc = GNUNET_SCHEDULER_cancel (write_task_id);
174 write_task_id = NULL;
175 GNUNET_free (wc->data);
178 if (NULL != child_death_task_id)
180 GNUNET_SCHEDULER_cancel (child_death_task_id);
181 child_death_task_id = NULL;
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_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 * Task to write to the standard out
206 * @param cls the WriteContext
209 write_task (void *cls)
211 struct WriteContext *wc = cls;
214 GNUNET_assert (NULL != wc);
215 write_task_id = NULL;
217 GNUNET_DISK_file_write (stdout_fd, wc->data + wc->pos,
218 wc->length - wc->pos);
219 if (GNUNET_SYSERR == bytes_wrote)
221 LOG (GNUNET_ERROR_TYPE_WARNING,
222 "Cannot reply back configuration\n");
223 GNUNET_free (wc->data);
227 wc->pos += bytes_wrote;
228 if (wc->pos == wc->length)
230 GNUNET_free (wc->data);
235 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
242 * Task triggered whenever we receive a SIGCHLD (child
245 * @param cls closure, NULL if we need to self-restart
248 child_death_task (void *cls)
250 const struct GNUNET_DISK_FileHandle *pr;
252 enum GNUNET_OS_ProcessStatusType type;
256 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
257 child_death_task_id = NULL;
258 /* consume the signal */
259 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
260 LOG_DEBUG ("Got SIGCHLD\n");
266 GNUNET_break (GNUNET_SYSERR !=
267 (ret = GNUNET_OS_process_status (testbed, &type, &code)));
268 if (GNUNET_NO != ret)
270 GNUNET_OS_process_destroy (testbed);
272 /* Send SIGTERM to our process group */
273 if (0 != PLIBC_KILL (0, GNUNET_TERM_SIG))
275 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "signal");
276 GNUNET_SCHEDULER_shutdown (); /* Couldn't send the signal, we shutdown frowning */
280 LOG_DEBUG ("Child hasn't died. Resuming to monitor its status\n");
281 child_death_task_id =
282 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
283 pr, &child_death_task, NULL);
288 * Functions with this signature are called whenever a
289 * complete message is received by the tokenizer.
291 * Do not call #GNUNET_mst_destroy() in this callback
293 * @param cls identification of the client
294 * @param message the actual message
295 * @return #GNUNET_OK on success,
296 * #GNUNET_NO to stop further processing (no error)
297 * #GNUNET_SYSERR to stop further processing with error
300 tokenizer_cb (void *cls,
301 const struct GNUNET_MessageHeader *message)
303 const struct GNUNET_TESTBED_HelperInit *msg;
304 struct GNUNET_TESTBED_HelperReply *reply;
305 struct GNUNET_CONFIGURATION_Handle *cfg;
306 struct WriteContext *wc;
315 uLongf ul_config_size;
317 uint16_t trusted_ip_size;
318 uint16_t hostname_size;
321 msize = ntohs (message->size);
322 if ((sizeof (struct GNUNET_TESTBED_HelperInit) >= msize) ||
323 (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT != ntohs (message->type)))
325 LOG (GNUNET_ERROR_TYPE_WARNING,
326 "Received unexpected message -- exiting\n");
329 msg = (const struct GNUNET_TESTBED_HelperInit *) message;
330 trusted_ip_size = ntohs (msg->trusted_ip_size);
331 trusted_ip = (char *) &msg[1];
332 if ('\0' != trusted_ip[trusted_ip_size])
334 LOG (GNUNET_ERROR_TYPE_WARNING, "Trusted IP cannot be empty -- exiting\n");
337 hostname_size = ntohs (msg->hostname_size);
338 if ((sizeof (struct GNUNET_TESTBED_HelperInit) + trusted_ip_size + 1 +
339 hostname_size) >= msize)
342 LOG (GNUNET_ERROR_TYPE_WARNING, "Received unexpected message -- exiting\n");
345 ul_config_size = (uLongf) ntohs (msg->config_size);
346 config = GNUNET_malloc (ul_config_size);
348 ntohs (message->size) - (trusted_ip_size + 1 +
349 sizeof (struct GNUNET_TESTBED_HelperInit));
351 uncompress ((Bytef *) config, &ul_config_size,
352 (const Bytef *) (trusted_ip + trusted_ip_size + 1 +
353 hostname_size), (uLongf) xconfig_size))
355 LOG (GNUNET_ERROR_TYPE_WARNING,
356 "Error while uncompressing config -- exiting\n");
357 GNUNET_free (config);
360 cfg = GNUNET_CONFIGURATION_create ();
362 GNUNET_CONFIGURATION_deserialize (cfg,
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';
381 /* unset GNUNET_TESTING_PREFIX if present as it is more relevant for testbed */
382 evstr = getenv (GNUNET_TESTING_PREFIX);
385 /* unsetting the variable will invalidate the pointer! */
386 evstr = GNUNET_strdup (evstr);
388 GNUNET_break (0 != SetEnvironmentVariable (GNUNET_TESTING_PREFIX, NULL));
390 GNUNET_break (0 == unsetenv (GNUNET_TESTING_PREFIX));
394 GNUNET_TESTING_system_create ("testbed-helper", trusted_ip, hostname,
399 GNUNET_assert (0 != SetEnvironmentVariable (GNUNET_TESTING_PREFIX,
404 GNUNET_asprintf (&evar,
405 GNUNET_TESTING_PREFIX "=%s",
407 GNUNET_assert (0 == putenv (evar)); /* consumes 'evar',
408 see putenv(): becomes part of envrionment! */
413 GNUNET_free_non_null (hostname);
415 GNUNET_assert (NULL != test_system);
416 GNUNET_assert (GNUNET_OK ==
417 GNUNET_TESTING_configuration_create (test_system, cfg));
418 GNUNET_assert (GNUNET_OK ==
419 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS",
422 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, config))
424 LOG (GNUNET_ERROR_TYPE_WARNING,
425 "Unable to write config file: %s -- exiting\n", config);
426 GNUNET_CONFIGURATION_destroy (cfg);
427 GNUNET_free (config);
430 LOG_DEBUG ("Staring testbed with config: %s\n", config);
431 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-testbed");
435 /* expose testbed configuration through env variable */
436 GNUNET_asprintf (&evar,
440 GNUNET_assert (0 == putenv (evar)); /* consumes 'evar',
441 see putenv(): becomes part of envrionment! */
445 GNUNET_OS_start_process (PIPE_CONTROL,
446 GNUNET_OS_INHERIT_STD_ERR /*verbose? */ ,
449 "gnunet-service-testbed",
452 GNUNET_free (binary);
453 GNUNET_free (config);
456 LOG (GNUNET_ERROR_TYPE_WARNING,
457 "Error starting gnunet-service-testbed -- exiting\n");
458 GNUNET_CONFIGURATION_destroy (cfg);
461 done_reading = GNUNET_YES;
462 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
463 GNUNET_CONFIGURATION_destroy (cfg);
466 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
467 GNUNET_free (config);
468 wc = GNUNET_new (struct WriteContext);
469 wc->length = xconfig_size + sizeof (struct GNUNET_TESTBED_HelperReply);
470 reply = GNUNET_realloc (xconfig, wc->length);
471 memmove (&reply[1], reply, xconfig_size);
472 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY);
473 reply->header.size = htons ((uint16_t) wc->length);
474 reply->config_size = htons ((uint16_t) config_size);
477 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
480 child_death_task_id =
481 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
482 GNUNET_DISK_pipe_handle (sigpipe,
483 GNUNET_DISK_PIPE_END_READ),
484 &child_death_task, NULL);
488 status = GNUNET_SYSERR;
489 GNUNET_SCHEDULER_shutdown ();
490 return GNUNET_SYSERR;
495 * Task to read from stdin
500 read_task (void *cls)
502 char buf[GNUNET_MAX_MESSAGE_SIZE];
506 sread = GNUNET_DISK_file_read (stdin_fd,
509 if ( (GNUNET_SYSERR == sread) ||
512 LOG_DEBUG ("STDIN closed\n");
513 GNUNET_SCHEDULER_shutdown ();
516 if (GNUNET_YES == done_reading)
518 /* didn't expect any more data! */
520 GNUNET_SCHEDULER_shutdown ();
523 LOG_DEBUG ("Read %u bytes\n",
524 (unsigned int) sread);
525 /* FIXME: could introduce a GNUNET_MST_read2 to read
526 directly from 'stdin_fd' and save a memcpy() here */
528 GNUNET_MST_from_buffer (tokenizer,
535 GNUNET_SCHEDULER_shutdown ();
538 read_task_id /* No timeout while reading */
539 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
547 * Main function that will be run.
550 * @param args remaining command-line arguments
551 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
552 * @param cfg configuration
558 const struct GNUNET_CONFIGURATION_Handle *cfg)
560 LOG_DEBUG ("Starting testbed helper...\n");
561 tokenizer = GNUNET_MST_create (&tokenizer_cb, NULL);
562 stdin_fd = GNUNET_DISK_get_handle_from_native (stdin);
563 stdout_fd = GNUNET_DISK_get_handle_from_native (stdout);
565 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
568 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
574 * Signal handler called for SIGCHLD.
577 sighandler_child_death ()
580 int old_errno; /* back-up errno */
584 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
585 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
594 * @param argc the number of command line arguments
595 * @param argv command line arg array
596 * @return return code
602 struct GNUNET_SIGNAL_Context *shc_chld;
603 struct GNUNET_GETOPT_CommandLineOption options[] = {
604 GNUNET_GETOPT_OPTION_END
609 if (NULL == (sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
610 GNUNET_NO, GNUNET_NO)))
615 shc_chld = GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD,
616 &sighandler_child_death);
617 ret = GNUNET_PROGRAM_run (argc, argv,
618 "gnunet-helper-testbed",
619 "Helper for starting gnunet-service-testbed",
623 GNUNET_SIGNAL_handler_uninstall (shc_chld);
625 GNUNET_DISK_pipe_close (sigpipe);
626 if (GNUNET_OK != ret)
628 return (GNUNET_OK == status) ? 0 : 1;
631 /* end of gnunet-helper-testbed.c */