gnunet-qr.c 9.0 KB

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