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 hurdels. 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>
32 #include "gnunet_util_lib.h"
33 #include "gnunet_testing_lib-new.h"
34 #include "testbed_helper.h"
38 * Generic logging shortcut
40 #define LOG(kind, ...) \
41 GNUNET_log (kind, __VA_ARGS__)
44 * Debug logging shorthand
46 #define LOG_DEBUG(...) \
47 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
50 * Handle to the testing system
52 static struct GNUNET_TESTING_System *test_system;
55 * Our message stream tokenizer
57 struct GNUNET_SERVER_MessageStreamTokenizer *tokenizer;
60 * Disk handle from stdin
62 static struct GNUNET_DISK_FileHandle *stdin_fd;
65 * The process handle to the testbed service
67 static struct GNUNET_OS_Process *testbed;
70 * Pipe handle to child's stdin
72 static struct GNUNET_DISK_PipeHandle *pipe_in;
75 * Pipe handle to child's stdout
77 static struct GNUNET_DISK_PipeHandle *pipe_out;
80 * Task identifier for the read task
82 static GNUNET_SCHEDULER_TaskIdentifier read_task_id;
85 * Are we done reading messages from stdin?
87 static int done_reading;
90 * Result to return in case we fail
96 * Task to shutting down nicely
99 * @return tc the task context
102 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
104 if (GNUNET_SCHEDULER_NO_TASK != read_task_id)
106 GNUNET_SCHEDULER_cancel (read_task_id);
107 read_task_id = GNUNET_SCHEDULER_NO_TASK;
109 (void) GNUNET_DISK_file_close (stdin_fd);
110 GNUNET_SERVER_mst_destroy (tokenizer);
112 if (NULL != test_system)
114 GNUNET_TESTING_system_destroy (test_system, GNUNET_YES);
121 * Functions with this signature are called whenever a
122 * complete message is received by the tokenizer.
124 * Do not call GNUNET_SERVER_mst_destroy in callback
127 * @param client identification of the client
128 * @param message the actual message
130 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
133 tokenizer_cb (void *cls, void *client,
134 const struct GNUNET_MessageHeader *message)
136 const struct GNUNET_TESTBED_HelperInit *msg;
137 struct GNUNET_CONFIGURATION_Handle *cfg;
141 uint16_t xconfig_size;
144 if ((sizeof (struct GNUNET_TESTBED_HelperInit) >= ntohs (message->size)) ||
145 (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT != ntohs (message->type)))
147 LOG (GNUNET_ERROR_TYPE_WARNING,
148 "Received unexpected message -- exiting\n");
151 msg = (const struct GNUNET_TESTBED_HelperInit *) message;
152 cname_size = ntohs (msg->cname_size);
153 controller = (char *) &msg[1];
154 if ('\0' != controller[cname_size])
156 LOG (GNUNET_ERROR_TYPE_WARNING,
157 "Controller name cannot be empty -- exiting\n");
160 config_size = (uLongf) ntohs (msg->config_size);
161 config = GNUNET_malloc (config_size);
162 xconfig_size = ntohs (message->size) - (cname_size + 1);
163 if (Z_OK != uncompress ((Bytef *) config, &config_size,
164 (const Bytef *) (controller + cname_size + 1),
165 (uLongf) xconfig_size))
167 LOG (GNUNET_ERROR_TYPE_WARNING,
168 "Error while uncompressing config -- exiting\n");
169 GNUNET_free (config);
172 cfg = GNUNET_CONFIGURATION_create ();
173 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
176 LOG (GNUNET_ERROR_TYPE_WARNING,
177 "Unable to deserialize config -- exiting\n");
178 GNUNET_free (config);
181 GNUNET_free (config);
182 test_system = GNUNET_TESTING_system_create ("testbed-helper", controller);
183 GNUNET_assert (NULL != test_system);
184 GNUNET_assert (GNUNET_OK == GNUNET_TESTING_configuration_create
186 pipe_in = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
187 pipe_out = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_YES);
188 if ((NULL == pipe_in) || (NULL == pipe_out))
190 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe");
191 GNUNET_free (config);
194 testbed = GNUNET_OS_start_process
195 (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR /*verbose? */, pipe_in, pipe_out,
196 "gnunet-service-testbed", "gnunet-service-testbed", "-c", config, NULL);
199 LOG (GNUNET_ERROR_TYPE_WARNING,
200 "Unable to deserialize config -- exiting\n");
203 GNUNET_DISK_pipe_close_end (pipe_out, GNUNET_DISK_PIPE_END_WRITE);
204 GNUNET_DISK_pipe_close_end (pipe_in, GNUNET_DISK_PIPE_END_READ);
205 done_reading = GNUNET_YES;
210 GNUNET_SCHEDULER_shutdown ();
211 return GNUNET_SYSERR;
216 * Task to read from stdin
219 * @return tc the task context
222 read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
224 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
227 read_task_id = GNUNET_SCHEDULER_NO_TASK;
228 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
230 sread = GNUNET_DISK_file_read (stdin_fd, buf, sizeof (buf));
231 if (GNUNET_SYSERR == sread)
233 GNUNET_break (0); /* FIXME: stdin closed - kill child */
234 GNUNET_SCHEDULER_shutdown ();
237 if (GNUNET_YES == done_reading)
239 /* didn't expect any more data! */
241 GNUNET_SCHEDULER_shutdown ();
244 LOG_DEBUG ("Read %u bytes\n", sread);
246 GNUNET_SERVER_mst_receive (tokenizer, NULL, buf, sread,
247 GNUNET_NO, GNUNET_NO))
250 GNUNET_SCHEDULER_shutdown ();
253 read_task_id = /* No timeout while reading */
254 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
255 stdin_fd, &read_task, NULL);
260 * Main function that will be run.
263 * @param args remaining command-line arguments
264 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
265 * @param cfg configuration
268 run (void *cls, char *const *args, const char *cfgfile,
269 const struct GNUNET_CONFIGURATION_Handle * cfg)
271 LOG_DEBUG ("Starting testbed helper...\n");
272 tokenizer = GNUNET_SERVER_mst_create (&tokenizer_cb, NULL);
273 stdin_fd = GNUNET_DISK_get_handle_from_native (stdin);
275 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
276 stdin_fd, &read_task, NULL);
277 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
278 &shutdown_task, NULL);
285 * @param argc the number of command line arguments
286 * @param argv command line arg array
287 * @return return code
289 int main (int argc, char **argv)
291 struct GNUNET_GETOPT_CommandLineOption options[] = {
292 GNUNET_GETOPT_OPTION_END
297 GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-helper",
298 "Helper for starting gnunet-service-testbed",
299 options, &run, NULL))
301 return (GNUNET_OK == ret) ? 0 : 1;
304 /* end of gnunet-testbed-helper.c */