2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/gnunet-uri.c
23 * @brief tool to dispatch URIs to the appropriate GNUnet helper process
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
32 static long unsigned int exit_code = 1;
35 * Helper process we started.
37 static struct GNUNET_OS_Process *p;
40 * Pipe used to communicate shutdown via signal.
42 static struct GNUNET_DISK_PipeHandle *sigpipe;
46 * Task triggered whenever we receive a SIGCHLD (child
47 * process died) or when user presses CTRL-C.
49 * @param cls closure, NULL
50 * @param tc scheduler context
53 maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
55 enum GNUNET_OS_ProcessStatusType type;
57 GNUNET_OS_process_status (p, &type, &exit_code)) ||
58 (type != GNUNET_OS_PROCESS_EXITED) )
59 GNUNET_break (0 == GNUNET_OS_process_kill (p, GNUNET_TERM_SIG));
60 GNUNET_OS_process_destroy (p);
65 * Main function that will be run by the scheduler.
68 * @param args remaining command-line arguments
69 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
70 * @param cfg configuration
73 run (void *cls, char *const *args, const char *cfgfile,
74 const struct GNUNET_CONFIGURATION_Handle *cfg)
80 GNUNET_SCHEDULER_TaskIdentifier rt;
82 if (NULL == (uri = args[0]))
84 fprintf (stderr, _("No URI specified on command line\n"));
87 if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://")))
89 fprintf (stderr, _("Invalid URI: does not start with `%s'\n"),
93 uri += strlen ("gnunet://");
94 if (NULL == (slash = strchr (uri, '/')))
96 fprintf (stderr, _("Invalid URI: fails to specify subsystem\n"));
99 subsystem = GNUNET_strndup (uri, slash - uri);
101 GNUNET_CONFIGURATION_get_value_string (cfg,
106 fprintf (stderr, _("No handler known for subsystem `%s'\n"), subsystem);
107 GNUNET_free (subsystem);
110 GNUNET_free (subsystem);
111 rt = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
112 GNUNET_DISK_pipe_handle (sigpipe,
113 GNUNET_DISK_PIPE_END_READ),
114 &maint_child_death, NULL);
115 p = GNUNET_OS_start_process (GNUNET_NO, 0,
121 GNUNET_free (program);
123 GNUNET_SCHEDULER_cancel (rt);
128 * Signal handler called for SIGCHLD. Triggers the
129 * respective handler by writing to the trigger pipe.
132 sighandler_child_death ()
135 int old_errno = errno; /* back-up errno */
138 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
139 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
141 errno = old_errno; /* restore errno */
146 * The main function to handle gnunet://-URIs.
148 * @param argc number of arguments from the command line
149 * @param argv command line arguments
150 * @return 0 ok, 1 on error
153 main (int argc, char *const *argv)
155 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
156 GNUNET_GETOPT_OPTION_END
158 struct GNUNET_SIGNAL_Context *shc_chld;
161 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
163 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
164 GNUNET_assert (sigpipe != NULL);
166 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
167 ret = GNUNET_PROGRAM_run (argc, argv, "gnunet-uri URI",
168 gettext_noop ("Perform default-actions for GNUnet URIs"),
169 options, &run, NULL);
170 GNUNET_SIGNAL_handler_uninstall (shc_chld);
172 GNUNET_DISK_pipe_close (sigpipe);
174 GNUNET_free ((void *) argv);
175 return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;
178 /* end of gnunet-uri.c */