redundant
[oweals/gnunet.git] / src / arm / gnunet-arm.c
1 /*
2      This file is part of GNUnet.
3      (C) 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 2, 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 arm/gnunet-arm.c
23  * @brief arm for writing a tool
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_client_lib.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_program_lib.h"
31 #include "gnunet_time_lib.h"
32
33 /**
34  * Timeout for all operations.
35  */
36 #define TIMEOUT  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
37
38 /**
39  * Set if we are to shutdown all services (including ARM).
40  */
41 static int end;
42
43 /**
44  * Set if we are to start default services (including ARM).
45  */
46 static int start;
47
48 /**
49  * Set to the name of a service to start.
50  */
51 static char *init;
52
53 /**
54  * Set to the name of a service to kill.
55  */
56 static char *term;
57
58 /**
59  * Set to the name of a service to test.
60  */
61 static char *test;
62
63 /**
64  * Final status code.
65  */
66 static int ret;
67
68
69 static void
70 confirm_cb (void *cls, int success)
71 {
72   const char *service = cls;
73   switch (success)
74     {
75     case GNUNET_OK:
76       fprintf (stdout, _("Service `%s' is now running.\n"), service);
77       break;
78     case GNUNET_NO:
79       fprintf (stdout, _("Service `%s' is not running.\n"), service);
80       break;
81     case GNUNET_SYSERR:
82       fprintf (stdout,
83                _("Error updating service `%s': ARM not running\n"), service);
84       break;
85     }
86 }
87
88
89 static void
90 confirm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
91 {
92   const char *service = cls;
93
94   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
95     fprintf (stdout, _("Service `%s' is running.\n"), service);
96   else
97     fprintf (stdout, _("Service `%s' is not running.\n"), service);
98 }
99
100
101 /**
102  * Main function that will be run by the scheduler.
103  *
104  * @param cls closure
105  * @param sched the scheduler to use
106  * @param args remaining command-line arguments
107  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
108  * @param cfg configuration
109  */
110 static void
111 run (void *cls,
112      struct GNUNET_SCHEDULER_Handle *sched,
113      char *const *args,
114      const char *cfgfile, 
115      const struct GNUNET_CONFIGURATION_Handle *cfg)
116 {
117   if (term != NULL)
118     {
119       GNUNET_ARM_stop_service (term, cfg, sched, TIMEOUT, &confirm_cb, term);
120     }
121   if (end)
122     {
123       GNUNET_ARM_stop_service ("arm",
124                                cfg, sched, TIMEOUT, &confirm_cb, "arm");
125     }
126   if (start)
127     {
128       GNUNET_ARM_start_service ("arm",
129                                 cfg, sched, TIMEOUT, &confirm_cb, "arm");
130     }
131   if (init != NULL)
132     {
133       GNUNET_ARM_start_service (init, cfg, sched, TIMEOUT, &confirm_cb, init);
134     }
135   if (test != NULL)
136     {
137       GNUNET_CLIENT_service_test (sched,
138                                   test, cfg, TIMEOUT, &confirm_task, test);
139     }
140 }
141
142
143 /**
144  * gnunet-arm command line options
145  */
146 static struct GNUNET_GETOPT_CommandLineOption options[] = {
147   {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
148    GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
149   {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
150    GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
151   {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
152    GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
153   {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
154    GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
155   {'t', "test", "SERVICE",
156    gettext_noop ("test if a particular service is running"),
157    GNUNET_YES, &GNUNET_GETOPT_set_string, &test},
158   GNUNET_GETOPT_OPTION_END
159 };
160
161
162 /**
163  * The main function to obtain arm from gnunetd.
164  *
165  * @param argc number of arguments from the command line
166  * @param argv command line arguments
167  * @return 0 ok, 1 on error
168  */
169 int
170 main (int argc, char *const *argv)
171 {
172   return (GNUNET_OK ==
173           GNUNET_PROGRAM_run (argc,
174                               argv,
175                               "gnunet-arm",
176                               gettext_noop
177                               ("Control services and the Automated Restart Manager (ARM)"),
178                               options, &run, NULL)) ? ret : 1;
179 }
180
181 /* end of gnunet-arm.c */