diff options
author | Christian Grothoff <christian@grothoff.org> | 2017-02-23 22:50:03 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2017-02-23 22:55:30 +0100 |
commit | b29c94bfac7c1820297f55448827be61ba709575 (patch) | |
tree | 799c15a2d22600ee9853f62fc47d6004a252d702 /src/identity/gnunet-identity.c | |
parent | caf375948ecc718bac6d75f415cc1c8324a9199c (diff) | |
download | gnunet-b29c94bfac7c1820297f55448827be61ba709575.tar.gz gnunet-b29c94bfac7c1820297f55448827be61ba709575.zip |
fix termination, proper shutdown on CTRL-C
Diffstat (limited to 'src/identity/gnunet-identity.c')
-rw-r--r-- | src/identity/gnunet-identity.c | 64 |
1 files changed, 49 insertions, 15 deletions
diff --git a/src/identity/gnunet-identity.c b/src/identity/gnunet-identity.c index f632aa0a8..8c8485249 100644 --- a/src/identity/gnunet-identity.c +++ b/src/identity/gnunet-identity.c | |||
@@ -29,6 +29,12 @@ | |||
29 | #include "gnunet_util_lib.h" | 29 | #include "gnunet_util_lib.h" |
30 | #include "gnunet_identity_service.h" | 30 | #include "gnunet_identity_service.h" |
31 | 31 | ||
32 | |||
33 | /** | ||
34 | * Return value from main on timeout. | ||
35 | */ | ||
36 | #define TIMEOUT_STATUS_CODE 40 | ||
37 | |||
32 | /** | 38 | /** |
33 | * Handle to IDENTITY service. | 39 | * Handle to IDENTITY service. |
34 | */ | 40 | */ |
@@ -79,6 +85,11 @@ static struct GNUNET_IDENTITY_Operation *create_op; | |||
79 | */ | 85 | */ |
80 | static struct GNUNET_IDENTITY_Operation *delete_op; | 86 | static struct GNUNET_IDENTITY_Operation *delete_op; |
81 | 87 | ||
88 | /** | ||
89 | * Value to return from #main(). | ||
90 | */ | ||
91 | static int global_ret; | ||
92 | |||
82 | 93 | ||
83 | /** | 94 | /** |
84 | * Task run on shutdown. | 95 | * Task run on shutdown. |
@@ -120,7 +131,11 @@ test_finished () | |||
120 | (NULL == set_ego) && | 131 | (NULL == set_ego) && |
121 | (! list) && | 132 | (! list) && |
122 | (! monitor) ) | 133 | (! monitor) ) |
134 | { | ||
135 | if (TIMEOUT_STATUS_CODE == global_ret) | ||
136 | global_ret = 0; | ||
123 | GNUNET_SCHEDULER_shutdown (); | 137 | GNUNET_SCHEDULER_shutdown (); |
138 | } | ||
124 | } | 139 | } |
125 | 140 | ||
126 | 141 | ||
@@ -159,9 +174,12 @@ create_finished (void *cls, | |||
159 | 174 | ||
160 | *op = NULL; | 175 | *op = NULL; |
161 | if (NULL != emsg) | 176 | if (NULL != emsg) |
177 | { | ||
162 | fprintf (stderr, | 178 | fprintf (stderr, |
163 | _("Failed to create ego: %s\n"), | 179 | _("Failed to create ego: %s\n"), |
164 | emsg); | 180 | emsg); |
181 | global_ret = 1; | ||
182 | } | ||
165 | test_finished (); | 183 | test_finished (); |
166 | } | 184 | } |
167 | 185 | ||
@@ -178,9 +196,12 @@ set_done (void *cls, | |||
178 | { | 196 | { |
179 | set_op = NULL; | 197 | set_op = NULL; |
180 | if (NULL != emsg) | 198 | if (NULL != emsg) |
199 | { | ||
181 | fprintf (stderr, | 200 | fprintf (stderr, |
182 | _("Failed to set default ego: %s\n"), | 201 | _("Failed to set default ego: %s\n"), |
183 | emsg); | 202 | emsg); |
203 | global_ret = 1; | ||
204 | } | ||
184 | test_finished (); | 205 | test_finished (); |
185 | } | 206 | } |
186 | 207 | ||
@@ -257,17 +278,23 @@ print_ego (void *cls, | |||
257 | } | 278 | } |
258 | if ( (NULL == ego) && (! monitor) ) | 279 | if ( (NULL == ego) && (! monitor) ) |
259 | { | 280 | { |
260 | GNUNET_SCHEDULER_shutdown (); | 281 | list = 0; |
282 | test_finished (); | ||
261 | return; | 283 | return; |
262 | } | 284 | } |
263 | if (! (list | monitor)) | 285 | if (! (list | monitor)) |
264 | return; | 286 | return; |
265 | if (NULL == ego) | 287 | if (NULL == ego) |
266 | return; | 288 | return; |
267 | GNUNET_IDENTITY_ego_get_public_key (ego, &pk); | 289 | GNUNET_IDENTITY_ego_get_public_key (ego, |
290 | &pk); | ||
268 | s = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk); | 291 | s = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk); |
269 | if ( (monitor) || (NULL != identifier) ) | 292 | if ( (monitor) || |
270 | fprintf (stdout, "%s - %s\n", identifier, s); | 293 | (NULL != identifier) ) |
294 | fprintf (stdout, | ||
295 | "%s - %s\n", | ||
296 | identifier, | ||
297 | s); | ||
271 | GNUNET_free (s); | 298 | GNUNET_free (s); |
272 | } | 299 | } |
273 | 300 | ||
@@ -281,7 +308,9 @@ print_ego (void *cls, | |||
281 | * @param cfg configuration | 308 | * @param cfg configuration |
282 | */ | 309 | */ |
283 | static void | 310 | static void |
284 | run (void *cls, char *const *args, const char *cfgfile, | 311 | run (void *cls, |
312 | char *const *args, | ||
313 | const char *cfgfile, | ||
285 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 314 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
286 | { | 315 | { |
287 | if ( (NULL == set_subsystem) ^ | 316 | if ( (NULL == set_subsystem) ^ |
@@ -291,7 +320,9 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
291 | "Options -e and -s must always be specified together\n"); | 320 | "Options -e and -s must always be specified together\n"); |
292 | return; | 321 | return; |
293 | } | 322 | } |
294 | sh = GNUNET_IDENTITY_connect (cfg, &print_ego, NULL); | 323 | sh = GNUNET_IDENTITY_connect (cfg, |
324 | &print_ego, | ||
325 | NULL); | ||
295 | if (NULL != delete_ego) | 326 | if (NULL != delete_ego) |
296 | delete_op = GNUNET_IDENTITY_delete (sh, | 327 | delete_op = GNUNET_IDENTITY_delete (sh, |
297 | delete_ego, | 328 | delete_ego, |
@@ -302,7 +333,8 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
302 | create_ego, | 333 | create_ego, |
303 | &create_finished, | 334 | &create_finished, |
304 | &create_op); | 335 | &create_op); |
305 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); | 336 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, |
337 | NULL); | ||
306 | test_finished (); | 338 | test_finished (); |
307 | } | 339 | } |
308 | 340 | ||
@@ -317,8 +349,6 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
317 | int | 349 | int |
318 | main (int argc, char *const *argv) | 350 | main (int argc, char *const *argv) |
319 | { | 351 | { |
320 | int res; | ||
321 | |||
322 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { | 352 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { |
323 | {'C', "create", "NAME", | 353 | {'C', "create", "NAME", |
324 | gettext_noop ("create ego NAME"), | 354 | gettext_noop ("create ego NAME"), |
@@ -340,19 +370,23 @@ main (int argc, char *const *argv) | |||
340 | 1, &GNUNET_GETOPT_set_string, &set_subsystem}, | 370 | 1, &GNUNET_GETOPT_set_string, &set_subsystem}, |
341 | GNUNET_GETOPT_OPTION_END | 371 | GNUNET_GETOPT_OPTION_END |
342 | }; | 372 | }; |
373 | int res; | ||
343 | 374 | ||
344 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | 375 | if (GNUNET_OK != |
345 | return 2; | 376 | GNUNET_STRINGS_get_utf8_args (argc, argv, |
346 | 377 | &argc, &argv)) | |
347 | res = GNUNET_PROGRAM_run (argc, argv, "gnunet-identity", | 378 | return 4; |
379 | global_ret = TIMEOUT_STATUS_CODE; /* timeout */ | ||
380 | res = GNUNET_PROGRAM_run (argc, argv, | ||
381 | "gnunet-identity", | ||
348 | gettext_noop ("Maintain egos"), | 382 | gettext_noop ("Maintain egos"), |
349 | options, &run, | 383 | options, &run, |
350 | NULL); | 384 | NULL); |
351 | GNUNET_free ((void *) argv); | 385 | GNUNET_free ((void *) argv); |
352 | 386 | ||
353 | if (GNUNET_OK != res) | 387 | if (GNUNET_OK != res) |
354 | return 1; | 388 | return 3; |
355 | return 0; | 389 | return global_ret; |
356 | } | 390 | } |
357 | 391 | ||
358 | /* end of gnunet-identity.c */ | 392 | /* end of gnunet-identity.c */ |