aboutsummaryrefslogtreecommitdiff
path: root/src/util/gnunet-uri.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-06-26 21:07:02 +0000
committerChristian Grothoff <christian@grothoff.org>2012-06-26 21:07:02 +0000
commit4e06c259fea9d604b253b73a72c3f82eb8b15736 (patch)
treef27b509e8d83e178b455634ae84eb0a5a625e2b8 /src/util/gnunet-uri.c
parentf33c54cfc9793c8b513559f06abf91d96288f210 (diff)
downloadgnunet-4e06c259fea9d604b253b73a72c3f82eb8b15736.tar.gz
gnunet-4e06c259fea9d604b253b73a72c3f82eb8b15736.zip
-add tool to handle gnunet-uris
Diffstat (limited to 'src/util/gnunet-uri.c')
-rw-r--r--src/util/gnunet-uri.c181
1 files changed, 181 insertions, 0 deletions
diff --git a/src/util/gnunet-uri.c b/src/util/gnunet-uri.c
new file mode 100644
index 000000000..68e7fa04b
--- /dev/null
+++ b/src/util/gnunet-uri.c
@@ -0,0 +1,181 @@
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/**
31 * Global return value.
32 */
33static int ret = 1;
34
35/**
36 * Helper process we started.
37 */
38static struct GNUNET_OS_Process *p;
39
40/**
41 * Pipe used to communicate shutdown via signal.
42 */
43static struct GNUNET_DISK_PipeHandle *sigpipe;
44
45
46/**
47 * Task triggered whenever we receive a SIGCHLD (child
48 * process died) or when user presses CTRL-C.
49 *
50 * @param cls closure, NULL
51 * @param tc scheduler context
52 */
53static void
54maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
55{
56 enum GNUNET_OS_ProcessStatusType type;
57 unsigned long code;
58
59 if ( (GNUNET_OK ==
60 GNUNET_OS_process_status (p, &type, &code)) &&
61 (type == GNUNET_OS_PROCESS_EXITED) &&
62 (0 == code) )
63 ret = 0;
64 else
65 GNUNET_OS_process_kill (p, SIGTERM);
66 GNUNET_OS_process_destroy (p);
67}
68
69
70/**
71 * Main function that will be run by the scheduler.
72 *
73 * @param cls closure
74 * @param args remaining command-line arguments
75 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
76 * @param cfg configuration
77 */
78static void
79run (void *cls, char *const *args, const char *cfgfile,
80 const struct GNUNET_CONFIGURATION_Handle *cfg)
81{
82 const char *uri;
83 const char *slash;
84 char *subsystem;
85 char *program;
86 GNUNET_SCHEDULER_TaskIdentifier rt;
87
88 if (NULL == (uri = args[0]))
89 {
90 fprintf (stderr, _("No URI specified on command line\n"));
91 return;
92 }
93 if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://")))
94 {
95 fprintf (stderr, _("Invalid URI: does not start with `%s'\n"),
96 "gnunet://");
97 return;
98 }
99 uri += strlen ("gnunet://");
100 if (NULL == (slash = strchr (uri, '/')))
101 {
102 fprintf (stderr, _("Invalid URI: fails to specify subsystem\n"));
103 return;
104 }
105 subsystem = GNUNET_strndup (uri, slash - uri);
106 if (GNUNET_OK !=
107 GNUNET_CONFIGURATION_get_value_string (cfg,
108 "uri",
109 subsystem,
110 &program))
111 {
112 fprintf (stderr, _("No handler known for subsystem `%s'\n"), subsystem);
113 return;
114 }
115 rt = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
116 GNUNET_DISK_pipe_handle (sigpipe,
117 GNUNET_DISK_PIPE_END_READ),
118 &maint_child_death, NULL);
119 p = GNUNET_OS_start_process (GNUNET_NO,
120 NULL, NULL,
121 program,
122 program,
123 args[0],
124 NULL);
125 if (NULL == p)
126 GNUNET_SCHEDULER_cancel (rt);
127}
128
129
130/**
131 * Signal handler called for SIGCHLD. Triggers the
132 * respective handler by writing to the trigger pipe.
133 */
134static void
135sighandler_child_death ()
136{
137 static char c;
138 int old_errno = errno; /* back-up errno */
139
140 GNUNET_break (1 ==
141 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
142 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
143 &c, sizeof (c)));
144 errno = old_errno; /* restore errno */
145}
146
147
148/**
149 * The main function to handle gnunet://-URIs.
150 *
151 * @param argc number of arguments from the command line
152 * @param argv command line arguments
153 * @return 0 ok, 1 on error
154 */
155int
156main (int argc, char *const *argv)
157{
158 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
159 GNUNET_GETOPT_OPTION_END
160 };
161 struct GNUNET_SIGNAL_Context *shc_chld;
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 if (GNUNET_OK !=
170 GNUNET_PROGRAM_run (argc, argv, "gnunet-uri URI",
171 gettext_noop ("Perform default-actions for GNUnet URIs"),
172 options, &run, NULL))
173 return 1;
174 GNUNET_SIGNAL_handler_uninstall (shc_chld);
175 shc_chld = NULL;
176 GNUNET_DISK_pipe_close (sigpipe);
177 sigpipe = NULL;
178 return ret;
179}
180
181/* end of gnunet-uri.c */