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-testbed-helper.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. This binary also kills the testbed service
28 * should the connection from the remote controller is dropped
29 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
34 #include "gnunet_util_lib.h"
35 #include "gnunet_testing_lib-new.h"
36 #include "testbed_helper.h"
40 * Generic logging shortcut
42 #define LOG(kind, ...) \
43 GNUNET_log (kind, __VA_ARGS__)
46 * Debug logging shorthand
48 #define LOG_DEBUG(...) \
49 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
52 * Handle to the testing system
54 static struct GNUNET_TESTING_System *test_system;
57 * Our message stream tokenizer
59 struct GNUNET_SERVER_MessageStreamTokenizer *tokenizer;
62 * Disk handle from stdin
64 static struct GNUNET_DISK_FileHandle *stdin_fd;
67 * The process handle to the testbed service
69 static struct GNUNET_OS_Process *testbed;
72 * Pipe handle to child's stdin
74 static struct GNUNET_DISK_PipeHandle *pipe_in;
77 * Pipe handle to child's stdout
79 static struct GNUNET_DISK_PipeHandle *pipe_out;
82 * Task identifier for the read task
84 static GNUNET_SCHEDULER_TaskIdentifier read_task_id;
87 * Are we done reading messages from stdin?
89 static int done_reading;
92 * Result to return in case we fail
98 * Task to shutting down nicely
101 * @return tc the task context
104 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
106 LOG_DEBUG ("Shutting down\n");
107 if (GNUNET_SCHEDULER_NO_TASK != read_task_id)
109 GNUNET_SCHEDULER_cancel (read_task_id);
110 read_task_id = GNUNET_SCHEDULER_NO_TASK;
112 (void) GNUNET_DISK_file_close (stdin_fd);
113 GNUNET_SERVER_mst_destroy (tokenizer);
117 (void) GNUNET_OS_process_kill (testbed, SIGTERM);
118 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (testbed));
119 GNUNET_OS_process_destroy (testbed);
124 (void) GNUNET_DISK_pipe_close (pipe_in);
127 if (NULL != pipe_out)
129 (void) GNUNET_DISK_pipe_close (pipe_out);
132 if (NULL != test_system)
134 GNUNET_TESTING_system_destroy (test_system, GNUNET_YES);
141 * Functions with this signature are called whenever a
142 * complete message is received by the tokenizer.
144 * Do not call GNUNET_SERVER_mst_destroy in callback
147 * @param client identification of the client
148 * @param message the actual message
150 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
153 tokenizer_cb (void *cls, void *client,
154 const struct GNUNET_MessageHeader *message)
156 const struct GNUNET_TESTBED_HelperInit *msg;
157 struct GNUNET_CONFIGURATION_Handle *cfg;
161 uint16_t xconfig_size;
164 if ((sizeof (struct GNUNET_TESTBED_HelperInit) >= ntohs (message->size)) ||
165 (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT != ntohs (message->type)))
167 LOG (GNUNET_ERROR_TYPE_WARNING,
168 "Received unexpected message -- exiting\n");
171 msg = (const struct GNUNET_TESTBED_HelperInit *) message;
172 cname_size = ntohs (msg->cname_size);
173 controller = (char *) &msg[1];
174 if ('\0' != controller[cname_size])
176 LOG (GNUNET_ERROR_TYPE_WARNING,
177 "Controller name cannot be empty -- exiting\n");
180 config_size = (uLongf) ntohs (msg->config_size);
181 config = GNUNET_malloc (config_size);
182 xconfig_size = ntohs (message->size) - (cname_size + 1);
183 if (Z_OK != uncompress ((Bytef *) config, &config_size,
184 (const Bytef *) (controller + cname_size + 1),
185 (uLongf) xconfig_size))
187 LOG (GNUNET_ERROR_TYPE_WARNING,
188 "Error while uncompressing config -- exiting\n");
189 GNUNET_free (config);
192 cfg = GNUNET_CONFIGURATION_create ();
193 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
196 LOG (GNUNET_ERROR_TYPE_WARNING,
197 "Unable to deserialize config -- exiting\n");
198 GNUNET_free (config);
201 GNUNET_free (config);
202 test_system = GNUNET_TESTING_system_create ("testbed-helper", controller);
203 GNUNET_assert (NULL != test_system);
204 GNUNET_assert (GNUNET_OK == GNUNET_TESTING_configuration_create
206 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string
207 (cfg, "PATHS", "DEFAULTCONFIG", &config));
208 pipe_in = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
209 pipe_out = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_YES);
210 if ((NULL == pipe_in) || (NULL == pipe_out))
212 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe");
213 GNUNET_free (config);
216 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, config))
218 LOG (GNUNET_ERROR_TYPE_WARNING,
219 "Unable to write config file: %s -- exiting\n", config);
220 GNUNET_free (config);
223 LOG_DEBUG ("Staring testbed with config: %s\n", config);
224 testbed = GNUNET_OS_start_process
225 (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR /*verbose? */, pipe_in, pipe_out,
226 "gnunet-service-testbed", "gnunet-service-testbed", "-c", config, NULL);
227 GNUNET_free (config);
230 LOG (GNUNET_ERROR_TYPE_WARNING,
231 "Error staring gnunet-service-testbed -- exiting\n");
234 GNUNET_DISK_pipe_close_end (pipe_out, GNUNET_DISK_PIPE_END_WRITE);
235 GNUNET_DISK_pipe_close_end (pipe_in, GNUNET_DISK_PIPE_END_READ);
236 done_reading = GNUNET_YES;
241 GNUNET_SCHEDULER_shutdown ();
242 return GNUNET_SYSERR;
247 * Task to read from stdin
250 * @return tc the task context
253 read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
255 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
258 read_task_id = GNUNET_SCHEDULER_NO_TASK;
259 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
261 sread = GNUNET_DISK_file_read (stdin_fd, buf, sizeof (buf));
262 if (GNUNET_SYSERR == sread)
264 GNUNET_break (0); /* FIXME: stdin closed - kill child */
265 GNUNET_SCHEDULER_shutdown ();
268 if (GNUNET_YES == done_reading)
270 /* didn't expect any more data! */
272 GNUNET_SCHEDULER_shutdown ();
275 LOG_DEBUG ("Read %u bytes\n", sread);
277 GNUNET_SERVER_mst_receive (tokenizer, NULL, buf, sread,
278 GNUNET_NO, GNUNET_NO))
281 GNUNET_SCHEDULER_shutdown ();
284 read_task_id = /* No timeout while reading */
285 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
286 stdin_fd, &read_task, NULL);
291 * Main function that will be run.
294 * @param args remaining command-line arguments
295 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
296 * @param cfg configuration
299 run (void *cls, char *const *args, const char *cfgfile,
300 const struct GNUNET_CONFIGURATION_Handle * cfg)
302 LOG_DEBUG ("Starting testbed helper...\n");
303 tokenizer = GNUNET_SERVER_mst_create (&tokenizer_cb, NULL);
304 stdin_fd = GNUNET_DISK_get_handle_from_native (stdin);
306 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
307 stdin_fd, &read_task, NULL);
308 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
309 &shutdown_task, NULL);
316 * @param argc the number of command line arguments
317 * @param argv command line arg array
318 * @return return code
320 int main (int argc, char **argv)
322 struct GNUNET_GETOPT_CommandLineOption options[] = {
323 GNUNET_GETOPT_OPTION_END
328 GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-helper",
329 "Helper for starting gnunet-service-testbed",
330 options, &run, NULL))
332 return (GNUNET_OK == ret) ? 0 : 1;
335 /* end of gnunet-testbed-helper.c */