-remove debug message
[oweals/gnunet.git] / src / util / gnunet-qr.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013-2019 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  * @file util/gnunet-qr.c
22  * @author Hartmut Goebel (original implementation)
23  * @author Martin Schanzenbach (integrate gnunet-uri)
24  * @author Christian Grothoff (error handling)
25  */
26 #include <stdio.h>
27 #include <zbar.h>
28 #include <stdbool.h>
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31
32 #define LOG(fmt, ...)  \
33   if (verbose) \
34     printf (fmt, ## __VA_ARGS__)
35
36 /**
37  * Video device to capture from. Sane default for GNU/Linux systems.
38  */
39 static char *device = "/dev/video0";
40
41 /**
42  * --verbose option
43  */
44 static unsigned int verbose;
45
46 /**
47  * --silent option
48  */
49 static int silent = false;
50
51 /**
52  * Handler exit code
53  */
54 static long unsigned int exit_code = 1;
55
56 /**
57  * Helper process we started.
58  */
59 static struct GNUNET_OS_Process *p;
60
61 /**
62  * Child signal handler.
63  */
64 static struct GNUNET_SIGNAL_Context *shc_chld;
65
66 /**
67  * Pipe used to communicate child death via signal.
68  */
69 static struct GNUNET_DISK_PipeHandle *sigpipe;
70
71 /**
72  * Process ID of this process at the time we installed the various
73  * signal handlers.
74  */
75 static pid_t my_pid;
76
77 /**
78  * Task triggered whenever we receive a SIGCHLD (child
79  * process died) or when user presses CTRL-C.
80  *
81  * @param cls closure, NULL
82  */
83 static void
84 maint_child_death (void *cls)
85 {
86   enum GNUNET_OS_ProcessStatusType type;
87
88   if ((GNUNET_OK != GNUNET_OS_process_status (p, &type, &exit_code)) ||
89       (type != GNUNET_OS_PROCESS_EXITED))
90     GNUNET_break (0 == GNUNET_OS_process_kill (p, GNUNET_TERM_SIG));
91   GNUNET_SIGNAL_handler_uninstall (shc_chld);
92   shc_chld = NULL;
93   if (NULL != sigpipe)
94   {
95     GNUNET_DISK_pipe_close (sigpipe);
96     sigpipe = NULL;
97   }
98   GNUNET_OS_process_destroy (p);
99 }
100
101
102 /**
103  * Signal handler called for signals that causes us to wait for the child process.
104  */
105 static void
106 sighandler_chld ()
107 {
108   static char c;
109   int old_errno = errno;        /* backup errno */
110
111   if (getpid () != my_pid)
112     _exit (1);                   /* we have fork'ed since the signal handler was created,
113                                   * ignore the signal, see https://gnunet.org/vfork discussion */
114   GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
115                             (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
116                           &c, sizeof(c));
117   errno = old_errno;
118 }
119
120
121 /**
122  * Dispatch URIs to the appropriate GNUnet helper process
123  *
124  * @param cls closure
125  * @param uri uri to dispatch
126  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
127  * @param cfg configuration
128  */
129 static void
130 gnunet_uri (void *cls,
131             const char *uri,
132             const char *cfgfile,
133             const struct GNUNET_CONFIGURATION_Handle *cfg)
134 {
135   const char *orig_uri;
136   const char *slash;
137   char *subsystem;
138   char *program;
139   struct GNUNET_SCHEDULER_Task *rt;
140
141   orig_uri = uri;
142   if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://")))
143   {
144     fprintf (stderr,
145              _ ("Invalid URI: does not start with `%s'\n"),
146              "gnunet://");
147     return;
148   }
149   uri += strlen ("gnunet://");
150   if (NULL == (slash = strchr (uri, '/')))
151   {
152     fprintf (stderr, _ ("Invalid URI: fails to specify subsystem\n"));
153     return;
154   }
155   subsystem = GNUNET_strndup (uri, slash - uri);
156   if (GNUNET_OK !=
157       GNUNET_CONFIGURATION_get_value_string (cfg, "uri", subsystem, &program))
158   {
159     fprintf (stderr, _ ("No handler known for subsystem `%s'\n"), subsystem);
160     GNUNET_free (subsystem);
161     return;
162   }
163   GNUNET_free (subsystem);
164   sigpipe = GNUNET_DISK_pipe (GNUNET_NO,
165                               GNUNET_NO,
166                               GNUNET_NO,
167                               GNUNET_NO);
168   GNUNET_assert (NULL != sigpipe);
169   rt = GNUNET_SCHEDULER_add_read_file (
170     GNUNET_TIME_UNIT_FOREVER_REL,
171     GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ),
172     &maint_child_death,
173     NULL);
174   my_pid = getpid ();
175   shc_chld = GNUNET_SIGNAL_handler_install (SIGCHLD,
176                                             &sighandler_chld);
177
178   {
179     char **argv = NULL;
180     unsigned int argc = 0;
181     char *u = GNUNET_strdup (program);
182
183     for (const char *tok = strtok (u, " ");
184          NULL != tok;
185          tok = strtok (NULL, " "))
186       GNUNET_array_append (argv,
187                            argc,
188                            GNUNET_strdup (tok));
189     GNUNET_array_append (argv,
190                          argc,
191                          GNUNET_strdup (orig_uri));
192     GNUNET_array_append (argv,
193                          argc,
194                          NULL);
195     p = GNUNET_OS_start_process_vap (GNUNET_NO,
196                                      GNUNET_OS_INHERIT_STD_ALL,
197                                      NULL,
198                                      NULL,
199                                      NULL,
200                                      argv[0],
201                                      argv);
202     for (unsigned int i = 0; i<argc - 1; i++)
203       GNUNET_free (argv[i]);
204     GNUNET_array_grow (argv,
205                        argc,
206                        0);
207     GNUNET_free (u);
208   }
209   if (NULL == p)
210     GNUNET_SCHEDULER_cancel (rt);
211   GNUNET_free (program);
212 }
213
214
215 /**
216  * Obtain QR code 'symbol' from @a proc.
217  *
218  * @param proc zbar processor to use
219  * @return NULL on error
220  */
221 static const zbar_symbol_t *
222 get_symbol (zbar_processor_t *proc)
223 {
224   const zbar_symbol_set_t *symbols;
225   int rc;
226   int n;
227
228   if (0 != zbar_processor_parse_config (proc, "enable"))
229   {
230     GNUNET_break (0);
231     return NULL;
232   }
233
234   /* initialize the Processor */
235   if (0 != (rc = zbar_processor_init (proc, device, 1)))
236   {
237     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
238                 "Failed to open device `%s': %d\n",
239                 device,
240                 rc);
241     return NULL;
242   }
243
244   /* enable the preview window */
245   if ((0 != (rc = zbar_processor_set_visible (proc, 1))) ||
246       (0 != (rc = zbar_processor_set_active (proc, 1))))
247   {
248     GNUNET_break (0);
249     return NULL;
250   }
251
252   /* read at least one barcode (or until window closed) */
253   LOG ("Capturing\n");
254   n = zbar_process_one (proc, -1);
255
256   /* hide the preview window */
257   (void) zbar_processor_set_active (proc, 0);
258   (void) zbar_processor_set_visible (proc, 0);
259   if (-1 == n)
260     return NULL; /* likely user closed the window */
261   LOG ("Got %i images\n", n);
262   /* extract results */
263   symbols = zbar_processor_get_results (proc);
264   if (NULL == symbols)
265   {
266     GNUNET_break (0);
267     return NULL;
268   }
269   return zbar_symbol_set_first_symbol (symbols);
270 }
271
272
273 /**
274  * Run zbar QR code parser.
275  *
276  * @return NULL on error, otherwise the URI that we found
277  */
278 static char *
279 run_zbar ()
280 {
281   zbar_processor_t *proc;
282   const char *data;
283   char *ret;
284   const zbar_symbol_t *symbol;
285
286   /* configure the Processor */
287   proc = zbar_processor_create (1);
288   if (NULL == proc)
289   {
290     GNUNET_break (0);
291     return NULL;
292   }
293
294   symbol = get_symbol (proc);
295   if (NULL == symbol)
296   {
297     zbar_processor_destroy (proc);
298     return NULL;
299   }
300   data = zbar_symbol_get_data (symbol);
301   if (NULL == data)
302   {
303     GNUNET_break (0);
304     zbar_processor_destroy (proc);
305     return NULL;
306   }
307   LOG ("Found %s \"%s\"\n",
308        zbar_get_symbol_name (zbar_symbol_get_type (symbol)),
309        data);
310   ret = GNUNET_strdup (data);
311   /* clean up */
312   zbar_processor_destroy (proc);
313   return ret;
314 }
315
316
317 /**
318  * Main function that will be run by the scheduler.
319  *
320  * @param cls closure
321  * @param args remaining command-line arguments
322  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
323  * @param cfg configuration
324  */
325 static void
326 run (void *cls,
327      char *const *args,
328      const char *cfgfile,
329      const struct GNUNET_CONFIGURATION_Handle *cfg)
330 {
331   char *data;
332
333   data = run_zbar ();
334   if (NULL == data)
335     return;
336   gnunet_uri (cls, data, cfgfile, cfg);
337   if (exit_code != 0)
338   {
339     printf ("Failed to add URI %s\n", data);
340   }
341   else
342   {
343     printf ("Added URI %s\n", data);
344   }
345   GNUNET_free (data);
346 };
347
348
349 int
350 main (int argc, char *const *argv)
351 {
352   int ret;
353   struct GNUNET_GETOPT_CommandLineOption options[] = {
354     GNUNET_GETOPT_option_string (
355       'd',
356       "device",
357       "DEVICE",
358       gettext_noop ("use video-device DEVICE (default: /dev/video0"),
359       &device),
360     GNUNET_GETOPT_option_verbose (&verbose),
361     GNUNET_GETOPT_option_flag ('s',
362                                "silent",
363                                gettext_noop ("do not show preview windows"),
364                                &silent),
365     GNUNET_GETOPT_OPTION_END
366   };
367
368   ret = GNUNET_PROGRAM_run (
369     argc,
370     argv,
371     "gnunet-qr",
372     gettext_noop (
373       "Scan a QR code using a video device and import the uri read"),
374     options,
375     &run,
376     NULL);
377   return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;
378 }