fix
[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 with 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.
32  *
33  * This executable is intended to be used by gnunet-java, in order to reliably
34  * start and stop services for test cases.
35  */
36
37 #include "platform.h"
38 #include "gnunet_getopt_lib.h"
39 #include "gnunet_program_lib.h"
40 #include "gnunet_util_lib.h"
41 #include "gnunet_signal_lib.h"
42 #include "gnunet_testing_lib-new.h"
43 #include "gnunet_os_lib.h"
44
45
46 static struct GNUNET_DISK_FileHandle fh;
47 static char *tmpfilename = NULL;
48 static GNUNET_SCHEDULER_TaskIdentifier tid = GNUNET_SCHEDULER_NO_TASK;
49 static struct GNUNET_TESTING_Peer *my_peer = NULL;
50
51
52 #define LOG(kind,...)                                           \
53   GNUNET_log_from (kind, "gnunettestingnew", __VA_ARGS__)
54
55
56 /**
57  * Cleanup called by signal handlers and when stdin is closed.
58  * Removes the temporary file with the configuration and shuts down the scheduler.
59  */
60 void
61 cleanup (void)
62 {
63     if (NULL != tmpfilename)
64     {
65         remove (tmpfilename);
66     }
67     GNUNET_SCHEDULER_shutdown ();
68 }
69
70 /**
71  * Called whenever we can read stdin non-blocking 
72  */
73 void
74 stdin_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
75 {
76   int c;
77
78   if (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason)
79   {
80       return;
81   }
82   if (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason)
83   {
84     c = getchar ();
85     if (EOF == c)
86     {
87       tid = GNUNET_SCHEDULER_NO_TASK;
88       cleanup ();
89     }
90     else
91     {
92       if (c == 'r')
93       {
94         GNUNET_TESTING_peer_stop(my_peer); 
95         GNUNET_TESTING_peer_start(my_peer); 
96         printf("restarted\n");
97       }
98       tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, &fh, &stdin_cb, NULL);
99     }
100     return;
101   }
102   GNUNET_break (0);
103 }
104
105 /**
106  * Main function called by the testing library.
107  * Executed inside a running scheduler.
108  */
109 void
110 testing_main (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
111               const struct GNUNET_TESTING_Peer *peer)
112 {
113     my_peer = peer;
114     tmpfilename = tmpnam (NULL);
115     if (NULL == tmpfilename)
116     {
117         GNUNET_break (0);
118         cleanup ();
119         return;
120     }
121
122     if (GNUNET_SYSERR == 
123             GNUNET_CONFIGURATION_write((struct GNUNET_CONFIGURATION_Handle *) cfg, tmpfilename))
124     {
125         GNUNET_break (0);
126         return;
127     }
128
129     printf("%s\n", tmpfilename);
130     fflush(stdout);
131
132     GNUNET_break(NULL != GNUNET_SIGNAL_handler_install(SIGTERM, &cleanup));
133     GNUNET_break(NULL != GNUNET_SIGNAL_handler_install(SIGINT, &cleanup));
134
135     fh.fd = 0; /* 0=stdin */
136     tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, &fh, &stdin_cb, NULL);
137 }
138
139
140
141 /**
142  * The main function.
143  *
144  * @param argc number of arguments from the command line
145  * @param argv command line arguments
146  * @return 0 ok, 1 on error
147  */
148 int
149 main (int argc, char *const *argv)
150 {
151     static const struct GNUNET_GETOPT_CommandLineOption options[] = {
152         GNUNET_GETOPT_OPTION_HELP("tool to start a service for testing"),
153         GNUNET_GETOPT_OPTION_END
154     };
155     int arg_start;
156     int ret;
157
158     arg_start = GNUNET_GETOPT_run("gnunet-testing-run-service", options, argc, argv);
159     
160     if (arg_start == GNUNET_SYSERR) {
161         return 1;
162     }
163     
164     if (arg_start != 1 || argc != 2)
165     {
166         fprintf (stderr, "Invalid number of arguments\n");
167         return 1;
168     }
169
170     ret =  GNUNET_TESTING_service_run_restartable ("gnunet_service_test", argv[1],
171                                                    NULL, &testing_main, NULL);
172
173     printf ("bye\n");
174
175     return ret;
176 }