config localization and service startup
[oweals/gnunet.git] / src / testbed / gnunet-testbed-helper.c
1 /*
2       This file is part of GNUnet
3       (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20
21 /**
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> 
30  */
31
32
33 #include "platform.h"
34 #include "gnunet_util_lib.h"
35 #include "gnunet_testing_lib-new.h"
36 #include "testbed_helper.h"
37 #include <zlib.h>
38
39 /**
40  * Generic logging shortcut
41  */
42 #define LOG(kind, ...)                                   \
43   GNUNET_log (kind, __VA_ARGS__)
44
45 /**
46  * Debug logging shorthand
47  */
48 #define LOG_DEBUG(...)                          \
49   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
50
51 /**
52  * Handle to the testing system
53  */
54 static struct GNUNET_TESTING_System *test_system;
55
56 /**
57  * Our message stream tokenizer
58  */
59 struct GNUNET_SERVER_MessageStreamTokenizer *tokenizer;
60
61 /**
62  * Disk handle from stdin
63  */
64 static struct GNUNET_DISK_FileHandle *stdin_fd;
65
66 /**
67  * The process handle to the testbed service
68  */
69 static struct GNUNET_OS_Process *testbed;
70
71 /**
72  * Pipe handle to child's stdin
73  */
74 static struct GNUNET_DISK_PipeHandle *pipe_in;
75
76 /**
77  * Pipe handle to child's stdout
78  */
79 static struct GNUNET_DISK_PipeHandle *pipe_out;
80
81 /**
82  * Task identifier for the read task
83  */
84 static GNUNET_SCHEDULER_TaskIdentifier read_task_id;
85
86 /**
87  * Are we done reading messages from stdin?
88  */
89 static int done_reading;
90
91 /**
92  * Result to return in case we fail
93  */
94 static int ret;
95
96
97 /**
98  * Task to shutting down nicely
99  *
100  * @param cls NULL
101  * @return tc the task context
102  */
103 static void
104 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
105 {
106   LOG_DEBUG ("Shutting down\n");
107   if (GNUNET_SCHEDULER_NO_TASK != read_task_id)
108   {
109     GNUNET_SCHEDULER_cancel (read_task_id);
110     read_task_id = GNUNET_SCHEDULER_NO_TASK;
111   }
112   (void) GNUNET_DISK_file_close (stdin_fd);
113   GNUNET_SERVER_mst_destroy (tokenizer);  
114   tokenizer = NULL;
115   if (NULL != testbed)
116   {
117     (void) GNUNET_OS_process_kill (testbed, SIGTERM);
118     GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (testbed));
119     GNUNET_OS_process_destroy (testbed);
120     testbed = NULL;
121   }
122   if (NULL != pipe_in)
123   {
124     (void) GNUNET_DISK_pipe_close (pipe_in);
125     pipe_in = NULL;
126   }
127   if (NULL != pipe_out)
128   {
129     (void) GNUNET_DISK_pipe_close (pipe_out);
130     pipe_out = NULL;
131   }
132   if (NULL != test_system)
133   {
134     GNUNET_TESTING_system_destroy (test_system, GNUNET_YES);
135     test_system = NULL;
136   }
137 }
138
139
140 /**
141  * Functions with this signature are called whenever a
142  * complete message is received by the tokenizer.
143  *
144  * Do not call GNUNET_SERVER_mst_destroy in callback
145  *
146  * @param cls closure
147  * @param client identification of the client
148  * @param message the actual message
149  *
150  * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
151  */
152 static int 
153 tokenizer_cb (void *cls, void *client,
154               const struct GNUNET_MessageHeader *message)
155 {
156   const struct GNUNET_TESTBED_HelperInit *msg;
157   struct GNUNET_CONFIGURATION_Handle *cfg;
158   char *controller;
159   char *config;
160   uLongf config_size;
161   uint16_t xconfig_size;
162   uint16_t cname_size;
163
164   if ((sizeof (struct GNUNET_TESTBED_HelperInit) >= ntohs (message->size)) ||
165       (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT != ntohs (message->type)))
166   {
167     LOG (GNUNET_ERROR_TYPE_WARNING,
168          "Received unexpected message -- exiting\n");
169     goto error;
170   }
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])
175   {
176     LOG (GNUNET_ERROR_TYPE_WARNING, 
177          "Controller name cannot be empty -- exiting\n");
178     goto error;
179   }
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))
186   {
187     LOG (GNUNET_ERROR_TYPE_WARNING, 
188          "Error while uncompressing config -- exiting\n");
189     GNUNET_free (config);
190     goto error;
191   }
192   cfg = GNUNET_CONFIGURATION_create ();  
193   if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
194                                                      GNUNET_NO))
195   {
196     LOG (GNUNET_ERROR_TYPE_WARNING, 
197          "Unable to deserialize config -- exiting\n");
198     GNUNET_free (config);
199     goto error;
200   }
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
205                  (test_system, cfg));
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))
211   {
212     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe");
213     GNUNET_free (config);
214     goto error;
215   }
216   if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, config))
217   {
218     LOG (GNUNET_ERROR_TYPE_WARNING, 
219          "Unable to write config file: %s -- exiting\n", config);
220     GNUNET_free (config);
221     goto error;
222   }
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);
228   if (NULL == testbed)
229   {
230     LOG (GNUNET_ERROR_TYPE_WARNING, 
231          "Error staring gnunet-service-testbed -- exiting\n");
232     goto error;
233   }
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;
237   return GNUNET_OK;
238   
239  error:
240   ret = GNUNET_SYSERR;
241   GNUNET_SCHEDULER_shutdown ();
242   return GNUNET_SYSERR;
243 }
244
245
246 /**
247  * Task to read from stdin
248  *
249  * @param cls NULL
250  * @return tc the task context
251  */
252 static void
253 read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
254 {
255   char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
256   ssize_t sread;
257
258   read_task_id = GNUNET_SCHEDULER_NO_TASK;
259   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
260     return;  
261   sread = GNUNET_DISK_file_read (stdin_fd, buf, sizeof (buf));
262   if (GNUNET_SYSERR == sread)
263   {
264     GNUNET_break (0);           /* FIXME: stdin closed - kill child */
265     GNUNET_SCHEDULER_shutdown ();
266     return;
267   }
268   if (GNUNET_YES == done_reading)
269   {
270     /* didn't expect any more data! */
271     GNUNET_break (0);
272     GNUNET_SCHEDULER_shutdown ();
273     return;
274   }
275   LOG_DEBUG ("Read %u bytes\n", sread);
276   if (GNUNET_OK !=
277       GNUNET_SERVER_mst_receive (tokenizer, NULL, buf, sread,
278                                  GNUNET_NO, GNUNET_NO))
279   {
280     GNUNET_break (0);
281     GNUNET_SCHEDULER_shutdown ();
282     return;
283   }
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);
287 }
288
289
290 /**
291  * Main function that will be run.
292  *
293  * @param cls closure
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
297  */
298 static void 
299 run (void *cls, char *const *args, const char *cfgfile,
300      const struct GNUNET_CONFIGURATION_Handle * cfg)
301 {
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);
305   read_task_id =
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);
310 }
311
312
313 /**
314  * Main function
315  *
316  * @param argc the number of command line arguments
317  * @param argv command line arg array
318  * @return return code
319  */
320 int main (int argc, char **argv)
321 {
322   struct GNUNET_GETOPT_CommandLineOption options[] = {
323     GNUNET_GETOPT_OPTION_END
324   };
325
326   ret = GNUNET_OK;
327   if (GNUNET_OK != 
328       GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-helper",
329                           "Helper for starting gnunet-service-testbed",
330                           options, &run, NULL))
331     return 1;
332   return (GNUNET_OK == ret) ? 0 : 1;
333 }
334
335 /* end of gnunet-testbed-helper.c */