gnunet-qr.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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;
  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 = 0;
  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 (NULL == device)
  205. device = GNUNET_strdup ("/dev/video0");
  206. if (0 != (rc = zbar_processor_init (proc, device, 1)))
  207. {
  208. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  209. "Failed to open device `%s': %d\n",
  210. device,
  211. rc);
  212. return NULL;
  213. }
  214. /* enable the preview window */
  215. if ((0 != (rc = zbar_processor_set_visible (proc, 1))) ||
  216. (0 != (rc = zbar_processor_set_active (proc, 1))))
  217. {
  218. GNUNET_break (0);
  219. return NULL;
  220. }
  221. /* read at least one barcode (or until window closed) */
  222. LOG ("Capturing\n");
  223. n = zbar_process_one (proc, -1);
  224. /* hide the preview window */
  225. (void) zbar_processor_set_active (proc, 0);
  226. (void) zbar_processor_set_visible (proc, 0);
  227. if (-1 == n)
  228. return NULL; /* likely user closed the window */
  229. LOG ("Got %i images\n", n);
  230. /* extract results */
  231. symbols = zbar_processor_get_results (proc);
  232. if (NULL == symbols)
  233. {
  234. GNUNET_break (0);
  235. return NULL;
  236. }
  237. return zbar_symbol_set_first_symbol (symbols);
  238. }
  239. /**
  240. * Run zbar QR code parser.
  241. *
  242. * @return NULL on error, otherwise the URI that we found
  243. */
  244. static char *
  245. run_zbar ()
  246. {
  247. zbar_processor_t *proc;
  248. const char *data;
  249. char *ret;
  250. const zbar_symbol_t *symbol;
  251. /* configure the Processor */
  252. proc = zbar_processor_create (1);
  253. if (NULL == proc)
  254. {
  255. GNUNET_break (0);
  256. return NULL;
  257. }
  258. symbol = get_symbol (proc);
  259. if (NULL == symbol)
  260. {
  261. zbar_processor_destroy (proc);
  262. return NULL;
  263. }
  264. data = zbar_symbol_get_data (symbol);
  265. if (NULL == data)
  266. {
  267. GNUNET_break (0);
  268. zbar_processor_destroy (proc);
  269. return NULL;
  270. }
  271. LOG ("Found %s \"%s\"\n",
  272. zbar_get_symbol_name (zbar_symbol_get_type (symbol)),
  273. data);
  274. ret = GNUNET_strdup (data);
  275. /* clean up */
  276. zbar_processor_destroy (proc);
  277. GNUNET_free (device);
  278. return ret;
  279. }
  280. /**
  281. * Main function that will be run by the scheduler.
  282. *
  283. * @param cls closure
  284. * @param args remaining command-line arguments
  285. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  286. * @param cfg configuration
  287. */
  288. static void
  289. run (void *cls,
  290. char *const *args,
  291. const char *cfgfile,
  292. const struct GNUNET_CONFIGURATION_Handle *cfg)
  293. {
  294. char *data;
  295. data = run_zbar ();
  296. if (NULL == data)
  297. return;
  298. gnunet_uri (cls, data, cfgfile, cfg);
  299. if (exit_code != 0)
  300. {
  301. printf ("Failed to add URI %s\n", data);
  302. }
  303. else
  304. {
  305. printf ("Added URI %s\n", data);
  306. }
  307. GNUNET_free (data);
  308. };
  309. int
  310. main (int argc, char *const *argv)
  311. {
  312. int ret;
  313. struct GNUNET_GETOPT_CommandLineOption options[] = {
  314. GNUNET_GETOPT_option_string (
  315. 'd',
  316. "device",
  317. "DEVICE",
  318. gettext_noop ("use video-device DEVICE (default: /dev/video0"),
  319. &device),
  320. GNUNET_GETOPT_option_verbose (&verbose),
  321. GNUNET_GETOPT_option_flag ('s',
  322. "silent",
  323. gettext_noop ("do not show preview windows"),
  324. &silent),
  325. GNUNET_GETOPT_OPTION_END
  326. };
  327. ret = GNUNET_PROGRAM_run (
  328. argc,
  329. argv,
  330. "gnunet-qr",
  331. gettext_noop (
  332. "Scan a QR code using a video device and import the uri read"),
  333. options,
  334. &run,
  335. NULL);
  336. return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;
  337. }