fix test case, implement base32 argument parser logic
[oweals/gnunet.git] / src / util / gnunet-uri.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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  */
51 static void
52 maint_child_death (void *cls)
53 {
54   enum GNUNET_OS_ProcessStatusType type;
55
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   struct GNUNET_SCHEDULER_Task * rt;
81
82   if (NULL == (uri = args[0]))
83   {
84     fprintf (stderr,
85              _("No URI specified on command line\n"));
86     return;
87   }
88   if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://")))
89   {
90     fprintf (stderr,
91              _("Invalid URI: does not start with `%s'\n"),
92              "gnunet://");
93     return;
94   }
95   uri += strlen ("gnunet://");
96   if (NULL == (slash = strchr (uri, '/')))
97   {
98     fprintf (stderr, _("Invalid URI: fails to specify subsystem\n"));
99     return;
100   }
101   subsystem = GNUNET_strndup (uri, slash - uri);
102   if (GNUNET_OK !=
103       GNUNET_CONFIGURATION_get_value_string (cfg,
104                                              "uri",
105                                              subsystem,
106                                              &program))
107   {
108     fprintf (stderr, _("No handler known for subsystem `%s'\n"), subsystem);
109     GNUNET_free (subsystem);
110     return;
111   }
112   GNUNET_free (subsystem);
113   rt = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
114                                        GNUNET_DISK_pipe_handle (sigpipe,
115                                                                 GNUNET_DISK_PIPE_END_READ),
116                                        &maint_child_death, NULL);
117   p = GNUNET_OS_start_process (GNUNET_NO, 0,
118                                NULL, NULL, NULL,
119                                program,
120                                program,
121                                args[0],
122                                NULL);
123   GNUNET_free (program);
124   if (NULL == p)
125     GNUNET_SCHEDULER_cancel (rt);
126 }
127
128
129 /**
130  * Signal handler called for SIGCHLD.  Triggers the
131  * respective handler by writing to the trigger pipe.
132  */
133 static void
134 sighandler_child_death ()
135 {
136   static char c;
137   int old_errno = errno;        /* back-up errno */
138
139   GNUNET_break (1 ==
140                 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
141                                         (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
142                                         &c, sizeof (c)));
143   errno = old_errno;            /* restore errno */
144 }
145
146
147 /**
148  * The main function to handle gnunet://-URIs.
149  *
150  * @param argc number of arguments from the command line
151  * @param argv command line arguments
152  * @return 0 ok, 1 on error
153  */
154 int
155 main (int argc, char *const *argv)
156 {
157   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
158     GNUNET_GETOPT_OPTION_END
159   };
160   struct GNUNET_SIGNAL_Context *shc_chld;
161   int ret;
162
163   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
164     return 2;
165   sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
166   GNUNET_assert (sigpipe != NULL);
167   shc_chld =
168     GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
169   ret = GNUNET_PROGRAM_run (argc, argv, "gnunet-uri URI",
170                             gettext_noop ("Perform default-actions for GNUnet URIs"),
171                             options, &run, NULL);
172   GNUNET_SIGNAL_handler_uninstall (shc_chld);
173   shc_chld = NULL;
174   GNUNET_DISK_pipe_close (sigpipe);
175   sigpipe = NULL;
176   GNUNET_free ((void *) argv);
177   return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;
178 }
179
180 /* end of gnunet-uri.c */