2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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.
23 * @file testing/gnunet-testing-run-service.c
24 * @brief tool to start a service for testing
25 * @author Florian Dold
27 * Start a peer, running only the service specified on the command line.
28 * Outputs the path to the temporary configuration file to stdout.
30 * The peer will run until this program is killed,
31 * or stdin is closed. When reading the character 'r' from stdin,
32 * the running service is restarted with the same configuration.
34 * This executable is intended to be used by gnunet-java, in order to reliably
35 * start and stop services for test cases.
38 #include "gnunet_util_lib.h"
39 #include "gnunet_testing_lib.h"
41 #define LOG(kind,...) \
42 GNUNET_log_from (kind, "gnunet-testing", __VA_ARGS__)
46 * File handle to STDIN, for reading restart/quit commands.
48 static struct GNUNET_DISK_FileHandle *fh;
53 static char *tmpfilename;
58 static GNUNET_SCHEDULER_TaskIdentifier tid;
63 static struct GNUNET_TESTING_Peer *my_peer;
69 * Cleanup called by signal handlers and when stdin is closed.
70 * Removes the temporary file.
73 * @param tc scheduler context
76 cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
78 if (NULL != tmpfilename)
80 if (0 != UNLINK (tmpfilename))
81 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", tmpfilename);
83 if (GNUNET_SCHEDULER_NO_TASK != tid)
85 GNUNET_SCHEDULER_cancel (tid);
86 tid = GNUNET_SCHEDULER_NO_TASK;
90 GNUNET_DISK_file_close (fh);
97 * Called whenever we can read stdin non-blocking
100 * @param tc scheduler context
103 stdin_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
107 tid = GNUNET_SCHEDULER_NO_TASK;
108 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
110 GNUNET_assert (0 != (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason));
116 GNUNET_SCHEDULER_shutdown ();
119 if (GNUNET_OK != GNUNET_TESTING_peer_stop (my_peer))
120 LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to stop the peer\n");
121 if (GNUNET_OK != GNUNET_TESTING_peer_start (my_peer))
122 LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to start the peer\n");
123 printf ("restarted\n");
128 /* ignore whitespace */
131 fprintf (stderr, _("Unknown command, use 'q' to quit or 'r' to restart peer\n"));
134 tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fh,
140 * Main function called by the testing library.
141 * Executed inside a running scheduler.
144 * @param cfg configuration of the peer that was started
145 * @param peer handle to the peer
148 testing_main (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
149 struct GNUNET_TESTING_Peer *peer)
152 if (NULL == (tmpfilename = GNUNET_DISK_mktemp ("gnunet-testing")))
155 GNUNET_SCHEDULER_shutdown ();
159 GNUNET_CONFIGURATION_write ((struct GNUNET_CONFIGURATION_Handle *) cfg,
165 printf("ok\n%s\n", tmpfilename);
167 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, NULL);
168 fh = GNUNET_DISK_get_handle_from_native (stdin);
169 tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fh,
177 * @param argc number of arguments from the command line
178 * @param argv command line arguments
179 * @return 0 ok, 1 on error
182 main (int argc, char *const *argv)
184 static char *cfg_name;
185 static char *srv_name;
186 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
187 {'c', "config", "FILENAME",
188 gettext_noop ("name of the template configuration file to use (optional)"), GNUNET_NO,
189 &GNUNET_GETOPT_set_string, &cfg_name},
190 {'s', "service", "SERVICE",
191 gettext_noop ("name of the service to run"), GNUNET_YES,
192 &GNUNET_GETOPT_set_string, &srv_name},
193 GNUNET_GETOPT_OPTION_HELP ("tool to start a service for testing"),
194 GNUNET_GETOPT_OPTION_END
199 GNUNET_GETOPT_run ("gnunet-testing-run-service", options, argc, argv))
201 ret = GNUNET_TESTING_service_run ("gnunet_service_test", srv_name,
202 cfg_name, &testing_main, NULL);