fa98e1a6c47b691987cf35773efccb60fb0fc616
[oweals/gnunet.git] / src / testing / gnunet-testing-run-service.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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 /**
23  * @file testing/gnunet-testing-run-service.c
24  * @brief tool to start a service for testing
25  * @author Florian Dold
26  *
27  * Start a peer, running only the service specified on the command line.
28  * Outputs the path to the temporary configuration file to stdout.
29  *
30  * The peer will run until this program is killed,
31  * or stdin is closed. When reading the character 'r' from stdin, the running service is
32  * restarted with the same configuration.
33  *
34  * This executable is intended to be used by gnunet-java, in order to reliably
35  * start and stop services for test cases.
36  */
37
38 #include "platform.h"
39 #include "gnunet_getopt_lib.h"
40 #include "gnunet_program_lib.h"
41 #include "gnunet_util_lib.h"
42 #include "gnunet_signal_lib.h"
43 #include "gnunet_testing_lib-new.h"
44 #include "gnunet_os_lib.h"
45
46
47 #define LOG(kind,...)                                           \
48   GNUNET_log_from (kind, "gnunet-testing", __VA_ARGS__)
49
50
51 /**
52  * FIXME
53  */
54 static struct GNUNET_DISK_FileHandle *fh;
55
56 /**
57  * FIXME
58  */
59 static char *tmpfilename;
60
61 /**
62  * FIXME
63  */
64 static GNUNET_SCHEDULER_TaskIdentifier tid;
65
66 /**
67  * FIXME
68  */
69 static struct GNUNET_TESTING_Peer *my_peer;
70
71
72
73
74 /**
75  * Cleanup called by signal handlers and when stdin is closed.
76  * Removes the temporary file.
77  *
78  * @param cls unused
79  * @param tc scheduler context 
80  */
81 static void
82 cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83 {
84   if (NULL != tmpfilename)
85   {
86     if (0 != UNLINK (tmpfilename))
87       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", tmpfilename);
88   }
89   if (GNUNET_SCHEDULER_NO_TASK != tid)
90   {
91     GNUNET_SCHEDULER_cancel (tid);
92     tid = GNUNET_SCHEDULER_NO_TASK;
93   }
94   if (NULL != fh)
95   {
96     GNUNET_DISK_file_close (fh);
97     fh = NULL;
98   }
99 }
100
101
102 /**
103  * Called whenever we can read stdin non-blocking 
104  *
105  * @param cls unused
106  * @param tc scheduler context 
107  */
108 static void
109 stdin_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
110 {
111   int c;
112
113   tid = GNUNET_SCHEDULER_NO_TASK;
114   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
115     return;
116   GNUNET_assert (0 != (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason));
117   c = getchar ();
118   switch (c)
119   {
120   case EOF:
121   case 'q':
122     GNUNET_SCHEDULER_shutdown ();
123     return;
124   case 'r':
125     GNUNET_TESTING_peer_stop (my_peer); 
126     GNUNET_TESTING_peer_start (my_peer); 
127     printf ("restarted\n");
128     fflush (stdout);
129     break;
130   case '\n':
131   case '\r':
132     /* ignore whitespace */
133     break;
134   default:
135     fprintf (stderr, _("Unknown command, use 'q' to quit or 'r' to restart peer\n"));
136     break;
137   }
138   tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, &fh, &stdin_cb, NULL);    
139 }
140
141
142 /**
143  * Main function called by the testing library.
144  * Executed inside a running scheduler.
145  *
146  * @param cls unused
147  * @param cfg configuration of the peer that was started
148  * @param peer handle to the peer
149  */
150 static void
151 testing_main (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
152               const struct GNUNET_TESTING_Peer *peer)
153 {
154   my_peer = (struct GNUNET_TESTING_Peer *) peer;
155   if (NULL == (tmpfilename = GNUNET_DISK_mktemp ("gnunet-testing")))
156   {
157     GNUNET_break (0);
158     cleanup ();
159     return;
160   }
161   if (GNUNET_SYSERR == 
162       GNUNET_CONFIGURATION_write ((struct GNUNET_CONFIGURATION_Handle *) cfg, tmpfilename))
163   {
164     GNUNET_break (0);
165     return;
166   }
167   printf("%s\n", tmpfilename);
168   fflush(stdout);
169   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREFER_REL, &cleanup);
170   fh = GNUNET_DISK_get_handle_from_native (stdin);
171   tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, &fh, &stdin_cb, NULL);
172 }
173
174
175 /**
176  * The main function.
177  *
178  * @param argc number of arguments from the command line
179  * @param argv command line arguments
180  * @return 0 ok, 1 on error
181  */
182 int
183 main (int argc, char *const *argv)
184 {
185   static char *cfg_name;
186   static char *srv_name;
187   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
188     {'c', "config", "FILENAME",
189      gettext_noop ("name of the configuration file to use"), 1,
190      &GNUNET_GETOPT_set_string, &cfg_name},
191     {'s', "service", "SERVICE",
192      gettext_noop ("name of the service to run"), 1,
193      &GNUNET_GETOPT_set_string, &srv_name},
194     GNUNET_GETOPT_OPTION_HELP ("tool to start a service for testing"),
195     GNUNET_GETOPT_OPTION_END
196   };
197   int ret;
198
199   if (GNUNET_SYSERR ==
200       GNUNET_GETOPT_run("gnunet-testing-run-service", options, argc, argv))
201     return 1;
202   ret = GNUNET_TESTING_service_run_restartable ("gnunet_service_test", srv_name,
203                                                 cfg_name, &testing_main, NULL);
204   if (0 != ret)
205   {
206     printf ("error\n");
207   }
208   else 
209   {
210     printf ("bye\n");
211   }
212   return ret;
213 }
214