2 This file is part of GNUnet.
3 Copyright (C) 2013-2019 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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)
30 #include "gnunet_util_lib.h"
32 #define LOG(fmt, ...) \
34 printf (fmt, ## __VA_ARGS__)
37 * Video device to capture from. Sane default for GNU/Linux systems.
39 static char *device = "/dev/video0";
44 static unsigned int verbose;
49 static int silent = false;
54 static long unsigned int exit_code = 1;
57 * Helper process we started.
59 static struct GNUNET_OS_Process *p;
62 * Child signal handler.
64 static struct GNUNET_SIGNAL_Context *shc_chld;
67 * Pipe used to communicate child death via signal.
69 static struct GNUNET_DISK_PipeHandle *sigpipe;
72 * Process ID of this process at the time we installed the various
78 * Task triggered whenever we receive a SIGCHLD (child
79 * process died) or when user presses CTRL-C.
81 * @param cls closure, NULL
84 maint_child_death (void *cls)
86 enum GNUNET_OS_ProcessStatusType type;
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);
95 GNUNET_DISK_pipe_close (sigpipe);
98 GNUNET_OS_process_destroy (p);
103 * Signal handler called for signals that causes us to wait for the child process.
109 int old_errno = errno; /* backup errno */
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),
122 * Dispatch URIs to the appropriate GNUnet helper process
125 * @param uri uri to dispatch
126 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
127 * @param cfg configuration
130 gnunet_uri (void *cls,
133 const struct GNUNET_CONFIGURATION_Handle *cfg)
135 const char *orig_uri;
139 struct GNUNET_SCHEDULER_Task *rt;
142 if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://")))
145 _ ("Invalid URI: does not start with `%s'\n"),
149 uri += strlen ("gnunet://");
150 if (NULL == (slash = strchr (uri, '/')))
152 fprintf (stderr, _ ("Invalid URI: fails to specify subsystem\n"));
155 subsystem = GNUNET_strndup (uri, slash - uri);
157 GNUNET_CONFIGURATION_get_value_string (cfg, "uri", subsystem, &program))
159 fprintf (stderr, _ ("No handler known for subsystem `%s'\n"), subsystem);
160 GNUNET_free (subsystem);
163 GNUNET_free (subsystem);
164 sigpipe = GNUNET_DISK_pipe (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),
175 shc_chld = GNUNET_SIGNAL_handler_install (SIGCHLD,
180 unsigned int argc = 0;
181 char *u = GNUNET_strdup (program);
183 for (const char *tok = strtok (u, " ");
185 tok = strtok (NULL, " "))
186 GNUNET_array_append (argv,
188 GNUNET_strdup (tok));
189 GNUNET_array_append (argv,
191 GNUNET_strdup (orig_uri));
192 GNUNET_array_append (argv,
195 p = GNUNET_OS_start_process_vap (GNUNET_NO,
196 GNUNET_OS_INHERIT_STD_ALL,
202 for (unsigned int i = 0; i<argc - 1; i++)
203 GNUNET_free (argv[i]);
204 GNUNET_array_grow (argv,
210 GNUNET_SCHEDULER_cancel (rt);
211 GNUNET_free (program);
216 * Obtain QR code 'symbol' from @a proc.
218 * @param proc zbar processor to use
219 * @return NULL on error
221 static const zbar_symbol_t *
222 get_symbol (zbar_processor_t *proc)
224 const zbar_symbol_set_t *symbols;
228 if (0 != zbar_processor_parse_config (proc, "enable"))
234 /* initialize the Processor */
235 if (0 != (rc = zbar_processor_init (proc, device, 1)))
237 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
238 "Failed to open device `%s': %d\n",
244 /* enable the preview window */
245 if ((0 != (rc = zbar_processor_set_visible (proc, 1))) ||
246 (0 != (rc = zbar_processor_set_active (proc, 1))))
252 /* read at least one barcode (or until window closed) */
254 n = zbar_process_one (proc, -1);
256 /* hide the preview window */
257 (void) zbar_processor_set_active (proc, 0);
258 (void) zbar_processor_set_visible (proc, 0);
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);
269 return zbar_symbol_set_first_symbol (symbols);
274 * Run zbar QR code parser.
276 * @return NULL on error, otherwise the URI that we found
281 zbar_processor_t *proc;
284 const zbar_symbol_t *symbol;
286 /* configure the Processor */
287 proc = zbar_processor_create (1);
294 symbol = get_symbol (proc);
297 zbar_processor_destroy (proc);
300 data = zbar_symbol_get_data (symbol);
304 zbar_processor_destroy (proc);
307 LOG ("Found %s \"%s\"\n",
308 zbar_get_symbol_name (zbar_symbol_get_type (symbol)),
310 ret = GNUNET_strdup (data);
312 zbar_processor_destroy (proc);
318 * Main function that will be run by the scheduler.
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
329 const struct GNUNET_CONFIGURATION_Handle *cfg)
336 gnunet_uri (cls, data, cfgfile, cfg);
339 printf ("Failed to add URI %s\n", data);
343 printf ("Added URI %s\n", data);
350 main (int argc, char *const *argv)
353 struct GNUNET_GETOPT_CommandLineOption options[] = {
354 GNUNET_GETOPT_option_string (
358 gettext_noop ("use video-device DEVICE (default: /dev/video0"),
360 GNUNET_GETOPT_option_verbose (&verbose),
361 GNUNET_GETOPT_option_flag ('s',
363 gettext_noop ("do not show preview windows"),
365 GNUNET_GETOPT_OPTION_END
368 ret = GNUNET_PROGRAM_run (
373 "Scan a QR code using a video device and import the uri read"),
377 return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;