diff options
Diffstat (limited to 'src/arm/gnunet-arm.c')
-rw-r--r-- | src/arm/gnunet-arm.c | 319 |
1 files changed, 142 insertions, 177 deletions
diff --git a/src/arm/gnunet-arm.c b/src/arm/gnunet-arm.c index db5687dbc..13b1fdc17 100644 --- a/src/arm/gnunet-arm.c +++ b/src/arm/gnunet-arm.c | |||
@@ -49,13 +49,6 @@ | |||
49 | #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1) | 49 | #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1) |
50 | 50 | ||
51 | /** | 51 | /** |
52 | * Timeout for starting services, very short because of the strange way start works | ||
53 | * (by checking if running before starting, so really this time is always waited on | ||
54 | * startup (annoying)). | ||
55 | */ | ||
56 | #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2) | ||
57 | |||
58 | /** | ||
59 | * Set if we are to shutdown all services (including ARM). | 52 | * Set if we are to shutdown all services (including ARM). |
60 | */ | 53 | */ |
61 | static int end; | 54 | static int end; |
@@ -91,11 +84,6 @@ static char *init; | |||
91 | static char *term; | 84 | static char *term; |
92 | 85 | ||
93 | /** | 86 | /** |
94 | * Set to the name of a service to test. | ||
95 | */ | ||
96 | static char *test; | ||
97 | |||
98 | /** | ||
99 | * Set to the name of the config file used. | 87 | * Set to the name of the config file used. |
100 | */ | 88 | */ |
101 | static const char *config_file; | 89 | static const char *config_file; |
@@ -118,7 +106,7 @@ static struct GNUNET_ARM_Handle *h; | |||
118 | /** | 106 | /** |
119 | * Our configuration. | 107 | * Our configuration. |
120 | */ | 108 | */ |
121 | const struct GNUNET_CONFIGURATION_Handle *cfg; | 109 | static const struct GNUNET_CONFIGURATION_Handle *cfg; |
122 | 110 | ||
123 | /** | 111 | /** |
124 | * Processing stage that we are in. Simple counter. | 112 | * Processing stage that we are in. Simple counter. |
@@ -147,72 +135,62 @@ cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); | |||
147 | * user and then runs the next phase in the FSM. | 135 | * user and then runs the next phase in the FSM. |
148 | * | 136 | * |
149 | * @param cls pointer to "const char*" identifying service that was manipulated | 137 | * @param cls pointer to "const char*" identifying service that was manipulated |
150 | * @param success GNUNET_OK if service is now running, GNUNET_NO if not, GNUNET_SYSERR on error | 138 | * @param result result of the operation |
151 | */ | 139 | */ |
152 | static void | 140 | static void |
153 | confirm_cb (void *cls, int success) | 141 | confirm_cb (void *cls, |
142 | enum GNUNET_ARM_ProcessStatus result) | ||
154 | { | 143 | { |
155 | const char *service = cls; | 144 | const char *service = cls; |
156 | 145 | ||
157 | switch (success) | 146 | switch (result) |
158 | { | 147 | { |
159 | case GNUNET_OK: | 148 | case GNUNET_ARM_PROCESS_UNKNOWN: |
160 | if (quiet != GNUNET_YES) | 149 | fprintf (stderr, _("Service `%s' is unknown to ARM.\n"), service); |
161 | fprintf (stdout, _("Service `%s' has been started.\n"), service); | 150 | ret = 1; |
162 | if ((phase - 1 != 2) && (phase - 1 != 3)) | ||
163 | { | ||
164 | if (quiet != GNUNET_YES) | ||
165 | fprintf (stdout, _("Failed to stop service `%s'!\n"), service); | ||
166 | ret = 1; | ||
167 | } | ||
168 | break; | 151 | break; |
169 | case GNUNET_NO: | 152 | case GNUNET_ARM_PROCESS_DOWN: |
170 | if (quiet != GNUNET_YES) | 153 | if (quiet != GNUNET_YES) |
171 | fprintf (stdout, _("Service `%s' has been stopped.\n"), service); | 154 | fprintf (stdout, _("Service `%s' has been stopped.\n"), service); |
172 | if ((phase - 1 != 0) && (phase - 1 != 1)) | ||
173 | { | ||
174 | if (quiet != GNUNET_YES) | ||
175 | fprintf (stdout, _("Failed to start service `%s'!\n"), service); | ||
176 | ret = 1; | ||
177 | } | ||
178 | break; | 155 | break; |
179 | case GNUNET_SYSERR: | 156 | case GNUNET_ARM_PROCESS_ALREADY_RUNNING: |
180 | if (quiet != GNUNET_YES) | 157 | fprintf (stderr, _("Service `%s' was already running.\n"), service); |
181 | fprintf (stdout, _("Some error communicating with service `%s'.\n"), | ||
182 | service); | ||
183 | ret = 1; | 158 | ret = 1; |
184 | break; | 159 | break; |
185 | } | 160 | case GNUNET_ARM_PROCESS_STARTING: |
186 | |||
187 | GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL, | ||
188 | GNUNET_SCHEDULER_REASON_PREREQ_DONE); | ||
189 | } | ||
190 | |||
191 | |||
192 | /** | ||
193 | * Function called to confirm that a service is running (or that | ||
194 | * it is not running). | ||
195 | * | ||
196 | * @param cls pointer to "const char*" identifying service that was manipulated | ||
197 | * @param tc reason determines if service is now running | ||
198 | */ | ||
199 | static void | ||
200 | confirm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
201 | { | ||
202 | const char *service = cls; | ||
203 | |||
204 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)) | ||
205 | { | ||
206 | if (quiet != GNUNET_YES) | ||
207 | fprintf (stdout, _("Service `%s' is running.\n"), service); | ||
208 | } | ||
209 | else | ||
210 | { | ||
211 | if (quiet != GNUNET_YES) | 161 | if (quiet != GNUNET_YES) |
212 | fprintf (stdout, _("Service `%s' is not running.\n"), service); | 162 | fprintf (stdout, _("Service `%s' has been started.\n"), service); |
163 | break; | ||
164 | case GNUNET_ARM_PROCESS_ALREADY_STOPPING: | ||
165 | fprintf (stderr, _("Service `%s' was already being stopped.\n"), service); | ||
166 | ret = 1; | ||
167 | break; | ||
168 | case GNUNET_ARM_PROCESS_ALREADY_DOWN: | ||
169 | fprintf (stderr, _("Service `%s' was already not running.\n"), service); | ||
170 | ret = 1; | ||
171 | break; | ||
172 | case GNUNET_ARM_PROCESS_SHUTDOWN: | ||
173 | fprintf (stderr, _("Request ignored as ARM is shutting down.\n")); | ||
174 | ret = 1; | ||
175 | break; | ||
176 | case GNUNET_ARM_PROCESS_COMMUNICATION_ERROR: | ||
177 | fprintf (stderr, _("Error communicating with ARM service.\n")); | ||
178 | ret = 1; | ||
179 | break; | ||
180 | case GNUNET_ARM_PROCESS_COMMUNICATION_TIMEOUT: | ||
181 | fprintf (stderr, _("Timeout communicating with ARM service.\n")); | ||
182 | ret = 1; | ||
183 | break; | ||
184 | case GNUNET_ARM_PROCESS_FAILURE: | ||
185 | fprintf (stderr, _("Operation failed.\n")); | ||
186 | ret = 1; | ||
187 | break; | ||
188 | default: | ||
189 | fprintf (stderr, _("Unknown response code from ARM.\n")); | ||
190 | break; | ||
213 | } | 191 | } |
214 | GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL, | 192 | GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL, |
215 | GNUNET_SCHEDULER_REASON_PREREQ_DONE); | 193 | GNUNET_SCHEDULER_REASON_PREREQ_DONE); |
216 | } | 194 | } |
217 | 195 | ||
218 | 196 | ||
@@ -230,25 +208,25 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
230 | { | 208 | { |
231 | cfg = c; | 209 | cfg = c; |
232 | config_file = cfgfile; | 210 | config_file = cfgfile; |
233 | if (GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME", &dir) | 211 | if (GNUNET_CONFIGURATION_get_value_string |
234 | != GNUNET_OK) | 212 | (cfg, "PATHS", "SERVICEHOME", &dir) != GNUNET_OK) |
235 | { | 213 | { |
236 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 214 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
237 | _ | 215 | _ |
238 | ("Fatal configuration error: `%s' option in section `%s' missing.\n"), | 216 | ("Fatal configuration error: `%s' option in section `%s' missing.\n"), |
239 | "SERVICEHOME", "PATHS"); | 217 | "SERVICEHOME", "PATHS"); |
240 | return; | 218 | return; |
241 | } | 219 | } |
242 | h = GNUNET_ARM_connect (cfg, NULL); | 220 | h = GNUNET_ARM_connect (cfg, NULL); |
243 | if (h == NULL) | 221 | if (h == NULL) |
244 | { | 222 | { |
245 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 223 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
246 | _("Fatal error initializing ARM API.\n")); | 224 | _("Fatal error initializing ARM API.\n")); |
247 | ret = 1; | 225 | ret = 1; |
248 | return; | 226 | return; |
249 | } | 227 | } |
250 | GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL, | 228 | GNUNET_SCHEDULER_add_continuation (&cps_loop, NULL, |
251 | GNUNET_SCHEDULER_REASON_PREREQ_DONE); | 229 | GNUNET_SCHEDULER_REASON_PREREQ_DONE); |
252 | } | 230 | } |
253 | 231 | ||
254 | /** | 232 | /** |
@@ -260,21 +238,21 @@ static void | |||
260 | delete_files () | 238 | delete_files () |
261 | { | 239 | { |
262 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 240 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
263 | "Will attempt to remove configuration file %s and service directory %s\n", | 241 | "Will attempt to remove configuration file %s and service directory %s\n", |
264 | config_file, dir); | 242 | config_file, dir); |
265 | 243 | ||
266 | if (UNLINK (config_file) != 0) | 244 | if (UNLINK (config_file) != 0) |
267 | { | 245 | { |
268 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 246 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
269 | _("Failed to remove configuration file %s\n"), config_file); | 247 | _("Failed to remove configuration file %s\n"), config_file); |
270 | } | 248 | } |
271 | 249 | ||
272 | if (GNUNET_DISK_directory_remove (dir) != GNUNET_OK) | 250 | if (GNUNET_DISK_directory_remove (dir) != GNUNET_OK) |
273 | { | 251 | { |
274 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 252 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
275 | _("Failed to remove servicehome directory %s\n"), dir); | 253 | _("Failed to remove servicehome directory %s\n"), dir); |
276 | 254 | ||
277 | } | 255 | } |
278 | } | 256 | } |
279 | 257 | ||
280 | /** | 258 | /** |
@@ -288,86 +266,76 @@ static void | |||
288 | cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | 266 | cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
289 | { | 267 | { |
290 | while (1) | 268 | while (1) |
291 | { | ||
292 | switch (phase++) | ||
293 | { | 269 | { |
294 | case 0: | 270 | switch (phase++) |
295 | if (term != NULL) | 271 | { |
296 | { | 272 | case 0: |
297 | GNUNET_ARM_stop_service (h, term, | 273 | if (term != NULL) |
298 | (0 == | 274 | { |
299 | timeout.rel_value) ? STOP_TIMEOUT : timeout, | 275 | GNUNET_ARM_stop_service (h, term, |
300 | &confirm_cb, term); | 276 | (0 == |
301 | return; | 277 | timeout.rel_value) ? STOP_TIMEOUT : |
302 | } | 278 | timeout, &confirm_cb, term); |
303 | break; | 279 | return; |
304 | case 1: | 280 | } |
305 | if ((end) || (restart)) | 281 | break; |
306 | { | 282 | case 1: |
307 | GNUNET_ARM_stop_service (h, "arm", | 283 | if ((end) || (restart)) |
308 | (0 == | 284 | { |
309 | timeout.rel_value) ? STOP_TIMEOUT_ARM : | 285 | GNUNET_ARM_stop_service (h, "arm", |
310 | timeout, &confirm_cb, "arm"); | 286 | (0 == |
311 | return; | 287 | timeout.rel_value) ? STOP_TIMEOUT_ARM |
312 | } | 288 | : timeout, &confirm_cb, "arm"); |
313 | break; | 289 | return; |
314 | case 2: | 290 | } |
315 | if (start) | 291 | break; |
316 | { | 292 | case 2: |
317 | GNUNET_ARM_start_service (h, "arm", | 293 | if (start) |
318 | (0 == | 294 | { |
319 | timeout.rel_value) ? START_TIMEOUT : timeout, | 295 | GNUNET_ARM_start_service (h, "arm", |
320 | &confirm_cb, "arm"); | 296 | (0 == |
321 | return; | 297 | timeout.rel_value) ? START_TIMEOUT : |
322 | } | 298 | timeout, &confirm_cb, "arm"); |
323 | break; | 299 | return; |
324 | case 3: | 300 | } |
325 | if (init != NULL) | 301 | break; |
326 | { | 302 | case 3: |
327 | GNUNET_ARM_start_service (h, init, | 303 | if (init != NULL) |
328 | (0 == | 304 | { |
329 | timeout.rel_value) ? START_TIMEOUT : timeout, | 305 | GNUNET_ARM_start_service (h, init, |
330 | &confirm_cb, init); | 306 | (0 == |
331 | return; | 307 | timeout.rel_value) ? START_TIMEOUT : |
332 | } | 308 | timeout, &confirm_cb, init); |
333 | break; | 309 | return; |
334 | case 4: | 310 | } |
335 | if (test != NULL) | 311 | break; |
336 | { | 312 | case 4: |
337 | GNUNET_CLIENT_service_test (test, cfg, | 313 | if (restart) |
338 | (0 == | 314 | { |
339 | timeout.rel_value) ? TEST_TIMEOUT : | 315 | GNUNET_ARM_disconnect (h); |
340 | timeout, &confirm_task, test); | 316 | phase = 0; |
341 | return; | 317 | end = 0; |
342 | } | 318 | start = 1; |
343 | break; | 319 | restart = 0; |
344 | case 5: | 320 | h = GNUNET_ARM_connect (cfg, NULL); |
345 | if (restart) | 321 | if (h == NULL) |
346 | { | 322 | { |
347 | GNUNET_ARM_disconnect (h); | 323 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
348 | phase = 0; | 324 | _("Fatal error initializing ARM API.\n")); |
349 | end = 0; | 325 | ret = 1; |
350 | start = 1; | 326 | return; |
351 | restart = 0; | 327 | } |
352 | h = GNUNET_ARM_connect (cfg, NULL); | 328 | GNUNET_SCHEDULER_add_now (&cps_loop, NULL); |
353 | if (h == NULL) | 329 | return; |
354 | { | 330 | } |
355 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 331 | /* Fall through */ |
356 | _("Fatal error initializing ARM API.\n")); | 332 | default: /* last phase */ |
357 | ret = 1; | 333 | GNUNET_ARM_disconnect (h); |
358 | return; | 334 | if ((end == GNUNET_YES) && (delete == GNUNET_YES)) |
359 | } | 335 | delete_files (); |
360 | GNUNET_SCHEDULER_add_now (&cps_loop, NULL); | 336 | return; |
361 | return; | 337 | } |
362 | } | ||
363 | /* Fall through */ | ||
364 | default: /* last phase */ | ||
365 | GNUNET_ARM_disconnect (h); | ||
366 | if ((end == GNUNET_YES) && (delete == GNUNET_YES)) | ||
367 | delete_files (); | ||
368 | return; | ||
369 | } | 338 | } |
370 | } | ||
371 | } | 339 | } |
372 | 340 | ||
373 | 341 | ||
@@ -395,9 +363,6 @@ main (int argc, char *const *argv) | |||
395 | {'r', "restart", NULL, | 363 | {'r', "restart", NULL, |
396 | gettext_noop ("stop and start all GNUnet default services"), | 364 | gettext_noop ("stop and start all GNUnet default services"), |
397 | GNUNET_NO, &GNUNET_GETOPT_set_one, &restart}, | 365 | GNUNET_NO, &GNUNET_GETOPT_set_one, &restart}, |
398 | {'t', "test", "SERVICE", | ||
399 | gettext_noop ("test if a particular service is running"), | ||
400 | GNUNET_YES, &GNUNET_GETOPT_set_string, &test}, | ||
401 | {'d', "delete", NULL, | 366 | {'d', "delete", NULL, |
402 | gettext_noop ("delete config file and directory on exit"), | 367 | gettext_noop ("delete config file and directory on exit"), |
403 | GNUNET_NO, &GNUNET_GETOPT_set_one, &delete}, | 368 | GNUNET_NO, &GNUNET_GETOPT_set_one, &delete}, |
@@ -414,12 +379,12 @@ main (int argc, char *const *argv) | |||
414 | 379 | ||
415 | if (GNUNET_OK == | 380 | if (GNUNET_OK == |
416 | GNUNET_PROGRAM_run (argc, argv, "gnunet-arm", | 381 | GNUNET_PROGRAM_run (argc, argv, "gnunet-arm", |
417 | gettext_noop | 382 | gettext_noop |
418 | ("Control services and the Automated Restart Manager (ARM)"), | 383 | ("Control services and the Automated Restart Manager (ARM)"), |
419 | options, &run, NULL)) | 384 | options, &run, NULL)) |
420 | { | 385 | { |
421 | return ret; | 386 | return ret; |
422 | } | 387 | } |
423 | 388 | ||
424 | return 1; | 389 | return 1; |
425 | } | 390 | } |