/** * Test killing via pipe. */ static int check_instant_kill () { char *fn; #if !WINDOWS GNUNET_asprintf (&fn, "cat"); #else GNUNET_asprintf (&fn, "w32cat"); #endif hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO); hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL)) { GNUNET_free (fn); return 1; } proc = GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR, hello_pipe_stdin, hello_pipe_stdout, fn, "gnunet-service-resolver", "-", NULL); if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) { GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); } GNUNET_free (fn); GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc)); GNUNET_OS_process_destroy (proc); proc = NULL; GNUNET_DISK_pipe_close (hello_pipe_stdout); GNUNET_DISK_pipe_close (hello_pipe_stdin); return 0; }
/** * Test killing via pipe. */ static int check_instant_kill () { char *fn; hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO); hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL)) { return 1; } fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver"); proc = GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ERR, hello_pipe_stdin, hello_pipe_stdout, NULL, fn, "gnunet-service-resolver", "-", NULL); if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG)) { GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); } GNUNET_free (fn); GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc)); GNUNET_OS_process_destroy (proc); proc = NULL; GNUNET_DISK_pipe_close (hello_pipe_stdout); GNUNET_DISK_pipe_close (hello_pipe_stdin); return 0; }
static void end_task (void *cls) { if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG)) { GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); } GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc)); GNUNET_OS_process_destroy (proc); proc = NULL; GNUNET_DISK_pipe_close (hello_pipe_stdout); GNUNET_DISK_pipe_close (hello_pipe_stdin); }
static void end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) { GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); } GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc)); GNUNET_OS_process_destroy (proc); proc = NULL; GNUNET_DISK_pipe_close (hello_pipe_stdout); GNUNET_DISK_pipe_close (hello_pipe_stdin); }
/** * Reap the helper process. This call is blocking(!). The helper process * should either be sent a termination signal before or should be dead before * calling this function * * @param h the helper handle * @return #GNUNET_OK on success; #GNUNET_SYSERR on error */ int GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h) { struct GNUNET_HELPER_SendHandle *sh; int ret; ret = GNUNET_SYSERR; if (NULL != h->helper_proc) { ret = GNUNET_OS_process_wait (h->helper_proc); GNUNET_OS_process_destroy (h->helper_proc); h->helper_proc = NULL; } if (NULL != h->read_task) { GNUNET_SCHEDULER_cancel (h->read_task); h->read_task = NULL; } if (NULL != h->write_task) { GNUNET_SCHEDULER_cancel (h->write_task); h->write_task = NULL; } if (NULL != h->helper_in) { GNUNET_DISK_pipe_close (h->helper_in); h->helper_in = NULL; h->fh_to_helper = NULL; } if (NULL != h->helper_out) { GNUNET_DISK_pipe_close (h->helper_out); h->helper_out = NULL; h->fh_from_helper = NULL; } while (NULL != (sh = h->sh_head)) { GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh); if (NULL != sh->cont) sh->cont (sh->cont_cls, GNUNET_NO); GNUNET_free (sh); } /* purge MST buffer */ if (NULL != h->mst) (void) GNUNET_SERVER_mst_receive (h->mst, NULL, NULL, 0, GNUNET_YES, GNUNET_NO); return ret; }
/** * The main function to handle gnunet://-URIs. * * @param argc number of arguments from the command line * @param argv command line arguments * @return 0 ok, 1 on error */ int main (int argc, char *const *argv) { static const struct GNUNET_GETOPT_CommandLineOption options[] = { GNUNET_GETOPT_OPTION_END }; struct GNUNET_SIGNAL_Context *shc_chld; int ret; if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) return 2; sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO); GNUNET_assert (sigpipe != NULL); shc_chld = GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death); ret = GNUNET_PROGRAM_run (argc, argv, "gnunet-uri URI", gettext_noop ("Perform default-actions for GNUnet URIs"), options, &run, NULL); GNUNET_SIGNAL_handler_uninstall (shc_chld); shc_chld = NULL; GNUNET_DISK_pipe_close (sigpipe); sigpipe = NULL; GNUNET_free ((void *) argv); return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1; }
static void end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending phase %d, ok is %d\n", phase, ok); if (NULL != proc) { if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG)) { GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); } GNUNET_OS_process_wait (proc); GNUNET_OS_process_destroy (proc); proc = NULL; } if (NULL != read_task) { GNUNET_SCHEDULER_cancel (read_task); read_task = NULL; } GNUNET_DISK_pipe_close (pipe_stdout); if (ok == 1) { if (phase < 9) { phase += 1; runone (); } else ok = 0; } else GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failing\n"); }
/** * Test killing via pipe. */ static int check_instant_kill () { hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO); hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL)) { return 1; } proc = GNUNET_OS_start_process (GNUNET_YES, hello_pipe_stdin, hello_pipe_stdout, "cat", "gnunet-service-resolver", "-", NULL); if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) { GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); } GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proc)); GNUNET_OS_process_destroy (proc); proc = NULL; GNUNET_DISK_pipe_close (hello_pipe_stdout); GNUNET_DISK_pipe_close (hello_pipe_stdin); return 0; }
/** * Execution start point */ int main (int argc, char *argv[]) { struct GNUNET_SIGNAL_Context *shc_chld; unsigned int cnt; ret = -1; if (argc < 2) { printf ("Need arguments: gnunet-testbed-mpi-spawn <cmd> <cmd_args>"); return 1; } if (GNUNET_OK != GNUNET_log_setup ("gnunet-testbed-spawn", NULL, NULL)) { GNUNET_break (0); return 1; } if (NULL == (sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO))) { GNUNET_break (0); ret = GNUNET_SYSERR; return 1; } shc_chld = GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death); if (NULL == shc_chld) { LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot install a signal handler\n"); return 1; } argv2 = GNUNET_malloc (sizeof (char *) * argc); for (cnt = 1; cnt < argc; cnt++) argv2[cnt - 1] = argv[cnt]; GNUNET_SCHEDULER_run (run, NULL); GNUNET_free (argv2); GNUNET_SIGNAL_handler_uninstall (shc_chld); shc_chld = NULL; GNUNET_DISK_pipe_close (sigpipe); GNUNET_free_non_null (fn); if (GNUNET_OK != ret) return ret; return 0; }
/** * The main function for the arm service. * * @param argc number of arguments from the command line * @param argv command line arguments * @return 0 ok, 1 on error */ int main (int argc, char *const *argv) { int ret; struct GNUNET_SIGNAL_Context *shc_chld; sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO); GNUNET_assert (sigpipe != NULL); shc_chld = GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death); ret = (GNUNET_OK == GNUNET_SERVICE_run (argc, argv, "arm", GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN, &run, NULL)) ? 0 : 1; GNUNET_SIGNAL_handler_uninstall (shc_chld); shc_chld = NULL; GNUNET_DISK_pipe_close (sigpipe); sigpipe = NULL; return ret; }
/** * Sends termination signal to the helper process. The helper process is not * reaped; call GNUNET_HELPER_wait() for reaping the dead helper process. * * @param h the helper handle * @param soft_kill if GNUNET_YES, signals termination by closing the helper's * stdin; GNUNET_NO to signal termination by sending SIGTERM to helper * @return #GNUNET_OK on success; #GNUNET_SYSERR on error */ int GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h, int soft_kill) { struct GNUNET_HELPER_SendHandle *sh; int ret; while (NULL != (sh = h->sh_head)) { GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh); if (NULL != sh->cont) sh->cont (sh->cont_cls, GNUNET_NO); GNUNET_free (sh); } if (NULL != h->restart_task) { GNUNET_SCHEDULER_cancel (h->restart_task); h->restart_task = NULL; } if (NULL != h->read_task) { GNUNET_SCHEDULER_cancel (h->read_task); h->read_task = NULL; } if (NULL == h->helper_proc) return GNUNET_SYSERR; if (GNUNET_YES == soft_kill) { /* soft-kill only possible with pipes */ GNUNET_assert (NULL != h->helper_in); ret = GNUNET_DISK_pipe_close (h->helper_in); h->helper_in = NULL; h->fh_to_helper = NULL; return ret; } if (0 != GNUNET_OS_process_kill (h->helper_proc, GNUNET_TERM_SIG)) return GNUNET_SYSERR; return GNUNET_OK; }
/** * Cancel operation. * * @param eh operation to cancel */ void GNUNET_NAT_mini_get_external_ipv4_cancel_ (struct GNUNET_NAT_ExternalHandle *eh) { if (NULL != eh->eip) { (void) GNUNET_OS_process_kill (eh->eip, SIGKILL); GNUNET_OS_process_destroy (eh->eip); } if (NULL != eh->opipe) { GNUNET_DISK_pipe_close (eh->opipe); eh->opipe = NULL; } if (NULL != eh->task) { GNUNET_SCHEDULER_cancel (eh->task); eh->task = NULL; } GNUNET_free (eh); }
/** * Try to get the external IPv4 address of this peer. * * @param cb function to call with result * @param cb_cls closure for @a cb * @return handle for cancellation (can only be used until @a cb is called), never NULL */ struct GNUNET_NAT_ExternalHandle * GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb, void *cb_cls) { struct GNUNET_NAT_ExternalHandle *eh; eh = GNUNET_new (struct GNUNET_NAT_ExternalHandle); eh->cb = cb; eh->cb_cls = cb_cls; eh->ret = GNUNET_NAT_ERROR_SUCCESS; if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("external-ip", GNUNET_NO, NULL)) { LOG (GNUNET_ERROR_TYPE_INFO, _("`external-ip' command not found\n")); eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND; eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh); return eh; } LOG (GNUNET_ERROR_TYPE_DEBUG, "Running `external-ip' to determine our external IP\n"); eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); if (NULL == eh->opipe) { eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE; eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh); return eh; } eh->eip = GNUNET_OS_start_process (GNUNET_NO, 0, NULL, eh->opipe, NULL, "external-ip", "external-ip", NULL); if (NULL == eh->eip) { GNUNET_DISK_pipe_close (eh->opipe); eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED; eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh); return eh; } GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE); eh->r = GNUNET_DISK_pipe_handle (eh->opipe, GNUNET_DISK_PIPE_END_READ); eh->task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, eh->r, &read_external_ipv4, eh); return eh; }