gnunet-uri.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_OS_INHERIT_STD_NONE,
  105. NULL,
  106. NULL,
  107. NULL,
  108. program,
  109. program,
  110. args[0],
  111. NULL);
  112. GNUNET_free (program);
  113. if (NULL == p)
  114. GNUNET_SCHEDULER_cancel (rt);
  115. }
  116. /**
  117. * Signal handler called for SIGCHLD. Triggers the
  118. * respective handler by writing to the trigger pipe.
  119. */
  120. static void
  121. sighandler_child_death ()
  122. {
  123. static char c;
  124. int old_errno = errno; /* back-up errno */
  125. GNUNET_break (
  126. 1 ==
  127. GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe,
  128. GNUNET_DISK_PIPE_END_WRITE),
  129. &c,
  130. sizeof(c)));
  131. errno = old_errno; /* restore errno */
  132. }
  133. /**
  134. * The main function to handle gnunet://-URIs.
  135. *
  136. * @param argc number of arguments from the command line
  137. * @param argv command line arguments
  138. * @return 0 ok, 1 on error
  139. */
  140. int
  141. main (int argc, char *const *argv)
  142. {
  143. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  144. GNUNET_GETOPT_OPTION_END
  145. };
  146. struct GNUNET_SIGNAL_Context *shc_chld;
  147. int ret;
  148. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  149. return 2;
  150. sigpipe = GNUNET_DISK_pipe (GNUNET_DISK_PF_NONE);
  151. GNUNET_assert (sigpipe != NULL);
  152. shc_chld =
  153. GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
  154. ret = GNUNET_PROGRAM_run (argc,
  155. argv,
  156. "gnunet-uri URI",
  157. gettext_noop (
  158. "Perform default-actions for GNUnet URIs"),
  159. options,
  160. &run,
  161. NULL);
  162. GNUNET_SIGNAL_handler_uninstall (shc_chld);
  163. shc_chld = NULL;
  164. GNUNET_DISK_pipe_close (sigpipe);
  165. sigpipe = NULL;
  166. GNUNET_free_nz ((void *) argv);
  167. return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;
  168. }
  169. /* end of gnunet-uri.c */