gnunet-uri.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/gnunet-uri.c
  18. * @brief tool to dispatch URIs to the appropriate GNUnet helper process
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. /**
  24. * Handler exit code
  25. */
  26. static long unsigned int exit_code = 1;
  27. /**
  28. * Helper process we started.
  29. */
  30. static struct GNUNET_OS_Process *p;
  31. /**
  32. * Pipe used to communicate shutdown via signal.
  33. */
  34. static struct GNUNET_DISK_PipeHandle *sigpipe;
  35. /**
  36. * Task triggered whenever we receive a SIGCHLD (child
  37. * process died) or when user presses CTRL-C.
  38. *
  39. * @param cls closure, NULL
  40. */
  41. static void
  42. maint_child_death (void *cls)
  43. {
  44. enum GNUNET_OS_ProcessStatusType type;
  45. (void) cls;
  46. if ((GNUNET_OK != GNUNET_OS_process_status (p, &type, &exit_code)) ||
  47. (type != GNUNET_OS_PROCESS_EXITED))
  48. GNUNET_break (0 == GNUNET_OS_process_kill (p, GNUNET_TERM_SIG));
  49. GNUNET_OS_process_destroy (p);
  50. }
  51. /**
  52. * Main function that will be run by the scheduler.
  53. *
  54. * @param cls closure
  55. * @param args remaining command-line arguments
  56. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  57. * @param cfg configuration
  58. */
  59. static void
  60. run (void *cls,
  61. char *const *args,
  62. const char *cfgfile,
  63. const struct GNUNET_CONFIGURATION_Handle *cfg)
  64. {
  65. const char *uri;
  66. const char *slash;
  67. char *subsystem;
  68. char *program;
  69. struct GNUNET_SCHEDULER_Task *rt;
  70. (void) cls;
  71. (void) cfgfile;
  72. if (NULL == (uri = args[0]))
  73. {
  74. fprintf (stderr, _ ("No URI specified on command line\n"));
  75. return;
  76. }
  77. if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://")))
  78. {
  79. fprintf (stderr,
  80. _ ("Invalid URI: does not start with `%s'\n"),
  81. "gnunet://");
  82. return;
  83. }
  84. uri += strlen ("gnunet://");
  85. if (NULL == (slash = strchr (uri, '/')))
  86. {
  87. fprintf (stderr, _ ("Invalid URI: fails to specify subsystem\n"));
  88. return;
  89. }
  90. subsystem = GNUNET_strndup (uri, slash - uri);
  91. if (GNUNET_OK !=
  92. GNUNET_CONFIGURATION_get_value_string (cfg, "uri", subsystem, &program))
  93. {
  94. fprintf (stderr, _ ("No handler known for subsystem `%s'\n"), subsystem);
  95. GNUNET_free (subsystem);
  96. return;
  97. }
  98. GNUNET_free (subsystem);
  99. rt = GNUNET_SCHEDULER_add_read_file (
  100. GNUNET_TIME_UNIT_FOREVER_REL,
  101. GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ),
  102. &maint_child_death,
  103. NULL);
  104. p = GNUNET_OS_start_process (GNUNET_NO,
  105. 0,
  106. NULL,
  107. NULL,
  108. NULL,
  109. program,
  110. program,
  111. args[0],
  112. NULL);
  113. GNUNET_free (program);
  114. if (NULL == p)
  115. GNUNET_SCHEDULER_cancel (rt);
  116. }
  117. /**
  118. * Signal handler called for SIGCHLD. Triggers the
  119. * respective handler by writing to the trigger pipe.
  120. */
  121. static void
  122. sighandler_child_death ()
  123. {
  124. static char c;
  125. int old_errno = errno; /* back-up errno */
  126. GNUNET_break (
  127. 1 ==
  128. GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe,
  129. GNUNET_DISK_PIPE_END_WRITE),
  130. &c,
  131. sizeof(c)));
  132. errno = old_errno; /* restore errno */
  133. }
  134. /**
  135. * The main function to handle gnunet://-URIs.
  136. *
  137. * @param argc number of arguments from the command line
  138. * @param argv command line arguments
  139. * @return 0 ok, 1 on error
  140. */
  141. int
  142. main (int argc, char *const *argv)
  143. {
  144. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  145. GNUNET_GETOPT_OPTION_END
  146. };
  147. struct GNUNET_SIGNAL_Context *shc_chld;
  148. int ret;
  149. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  150. return 2;
  151. sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
  152. GNUNET_assert (sigpipe != NULL);
  153. shc_chld =
  154. GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
  155. ret = GNUNET_PROGRAM_run (argc,
  156. argv,
  157. "gnunet-uri URI",
  158. gettext_noop (
  159. "Perform default-actions for GNUnet URIs"),
  160. options,
  161. &run,
  162. NULL);
  163. GNUNET_SIGNAL_handler_uninstall (shc_chld);
  164. shc_chld = NULL;
  165. GNUNET_DISK_pipe_close (sigpipe);
  166. sigpipe = NULL;
  167. GNUNET_free ((void *) argv);
  168. return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;
  169. }
  170. /* end of gnunet-uri.c */