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