added multiple iteration support for averaging
[oweals/gnunet.git] / src / util / gnunet-uri.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file util/gnunet-uri.c
23  * @brief tool to dispatch URIs to the appropriate GNUnet helper process
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28
29 /**
30  * Handler exit code
31  */
32 static long unsigned int exit_code = 1;
33
34 /**
35  * Helper process we started.
36  */
37 static struct GNUNET_OS_Process *p;
38
39 /**
40  * Pipe used to communicate shutdown via signal.
41  */
42 static struct GNUNET_DISK_PipeHandle *sigpipe;
43
44
45 /**
46  * Task triggered whenever we receive a SIGCHLD (child
47  * process died) or when user presses CTRL-C.
48  *
49  * @param cls closure, NULL
50  * @param tc scheduler context
51  */
52 static void
53 maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
54 {
55   enum GNUNET_OS_ProcessStatusType type;
56   if ( (GNUNET_OK !=
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);
61 }
62
63
64 /**
65  * Main function that will be run by the scheduler.
66  *
67  * @param cls closure
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
71  */
72 static void
73 run (void *cls, char *const *args, const char *cfgfile,
74      const struct GNUNET_CONFIGURATION_Handle *cfg)
75 {
76   const char *uri;
77   const char *slash;
78   char *subsystem;
79   char *program;
80   GNUNET_SCHEDULER_TaskIdentifier rt;
81
82   if (NULL == (uri = args[0]))
83   {
84     fprintf (stderr, _("No URI specified on command line\n"));
85     return;
86   }
87   if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://")))
88   {
89     fprintf (stderr, _("Invalid URI: does not start with `%s'\n"),
90              "gnunet://");
91     return;
92   }
93   uri += strlen ("gnunet://");
94   if (NULL == (slash = strchr (uri, '/')))
95   {
96     fprintf (stderr, _("Invalid URI: fails to specify subsystem\n"));
97     return;
98   }
99   subsystem = GNUNET_strndup (uri, slash - uri);
100   if (GNUNET_OK !=
101       GNUNET_CONFIGURATION_get_value_string (cfg,
102                                              "uri",
103                                              subsystem,
104                                              &program))
105   {
106     fprintf (stderr, _("No handler known for subsystem `%s'\n"), subsystem);
107     GNUNET_free (subsystem);
108     return;
109   }
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,
116                                NULL, NULL,
117                                program,
118                                program,
119                                args[0],
120                                NULL);
121   GNUNET_free (program);
122   if (NULL == p)
123     GNUNET_SCHEDULER_cancel (rt);
124 }
125
126
127 /**
128  * Signal handler called for SIGCHLD.  Triggers the
129  * respective handler by writing to the trigger pipe.
130  */
131 static void
132 sighandler_child_death ()
133 {
134   static char c;
135   int old_errno = errno;        /* back-up errno */
136
137   GNUNET_break (1 ==
138                 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
139                                         (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
140                                         &c, sizeof (c)));
141   errno = old_errno;            /* restore errno */
142 }
143
144
145 /**
146  * The main function to handle gnunet://-URIs.
147  *
148  * @param argc number of arguments from the command line
149  * @param argv command line arguments
150  * @return 0 ok, 1 on error
151  */
152 int
153 main (int argc, char *const *argv)
154 {
155   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
156     GNUNET_GETOPT_OPTION_END
157   };
158   struct GNUNET_SIGNAL_Context *shc_chld;
159   int ret;
160
161   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
162     return 2;
163   sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
164   GNUNET_assert (sigpipe != NULL);
165   shc_chld =
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);
171   shc_chld = NULL;
172   GNUNET_DISK_pipe_close (sigpipe);
173   sigpipe = NULL;
174   GNUNET_free ((void *) argv);
175   return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;
176 }
177
178 /* end of gnunet-uri.c */