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