aboutsummaryrefslogtreecommitdiff
path: root/src/util/gnunet-uri.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/gnunet-uri.c')
-rw-r--r--src/util/gnunet-uri.c191
1 files changed, 0 insertions, 191 deletions
diff --git a/src/util/gnunet-uri.c b/src/util/gnunet-uri.c
deleted file mode 100644
index de0ff1f92..000000000
--- a/src/util/gnunet-uri.c
+++ /dev/null
@@ -1,191 +0,0 @@
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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
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 */
32static long unsigned int exit_code = 0;
33
34/**
35 * Helper process we started.
36 */
37static struct GNUNET_OS_Process *p;
38
39/**
40 * Pipe used to communicate shutdown via signal.
41 */
42static 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 */
51static void
52maint_child_death (void *cls)
53{
54 enum GNUNET_OS_ProcessStatusType type;
55
56 (void) cls;
57 if ((GNUNET_OK != 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 */
72static void
73run (void *cls,
74 char *const *args,
75 const char *cfgfile,
76 const struct GNUNET_CONFIGURATION_Handle *cfg)
77{
78 const char *uri;
79 const char *slash;
80 char *subsystem;
81 char *program;
82 struct GNUNET_SCHEDULER_Task *rt;
83
84 (void) cls;
85 (void) cfgfile;
86 if (NULL == (uri = args[0]))
87 {
88 fprintf (stderr, _ ("No URI specified on command line\n"));
89 return;
90 }
91 if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://")))
92 {
93 fprintf (stderr,
94 _ ("Invalid URI: does not start with `%s'\n"),
95 "gnunet://");
96 return;
97 }
98 uri += strlen ("gnunet://");
99 if (NULL == (slash = strchr (uri, '/')))
100 {
101 fprintf (stderr, _ ("Invalid URI: fails to specify subsystem\n"));
102 return;
103 }
104 subsystem = GNUNET_strndup (uri, slash - uri);
105 if (GNUNET_OK !=
106 GNUNET_CONFIGURATION_get_value_string (cfg, "uri", subsystem, &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 (
114 GNUNET_TIME_UNIT_FOREVER_REL,
115 GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ),
116 &maint_child_death,
117 NULL);
118 p = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_NONE,
119 NULL,
120 NULL,
121 NULL,
122 program,
123 program,
124 args[0],
125 NULL);
126 GNUNET_free (program);
127 if (NULL == p)
128 GNUNET_SCHEDULER_cancel (rt);
129}
130
131
132/**
133 * Signal handler called for SIGCHLD. Triggers the
134 * respective handler by writing to the trigger pipe.
135 */
136static void
137sighandler_child_death ()
138{
139 static char c;
140 int old_errno = errno; /* back-up errno */
141
142 GNUNET_break (
143 1 ==
144 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe,
145 GNUNET_DISK_PIPE_END_WRITE),
146 &c,
147 sizeof(c)));
148 errno = old_errno; /* restore errno */
149}
150
151
152/**
153 * The main function to handle gnunet://-URIs.
154 *
155 * @param argc number of arguments from the command line
156 * @param argv command line arguments
157 * @return 0 ok, 1 on error
158 */
159int
160main (int argc, char *const *argv)
161{
162 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
163 GNUNET_GETOPT_OPTION_END
164 };
165 struct GNUNET_SIGNAL_Context *shc_chld;
166 int ret;
167
168 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
169 return 2;
170 sigpipe = GNUNET_DISK_pipe (GNUNET_DISK_PF_NONE);
171 GNUNET_assert (sigpipe != NULL);
172 shc_chld =
173 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
174 ret = GNUNET_PROGRAM_run (argc,
175 argv,
176 "gnunet-uri URI",
177 gettext_noop (
178 "Perform default-actions for GNUnet URIs"),
179 options,
180 &run,
181 NULL);
182 GNUNET_SIGNAL_handler_uninstall (shc_chld);
183 shc_chld = NULL;
184 GNUNET_DISK_pipe_close (sigpipe);
185 sigpipe = NULL;
186 GNUNET_free_nz ((void *) argv);
187 return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;
188}
189
190
191/* end of gnunet-uri.c */