aboutsummaryrefslogtreecommitdiff
path: root/src/util/gnunet-qr.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/gnunet-qr.c')
-rw-r--r--src/util/gnunet-qr.c103
1 files changed, 89 insertions, 14 deletions
diff --git a/src/util/gnunet-qr.c b/src/util/gnunet-qr.c
index b54f352d5..1106d5cb2 100644
--- a/src/util/gnunet-qr.c
+++ b/src/util/gnunet-qr.c
@@ -23,18 +23,102 @@
23#include <stdbool.h> 23#include <stdbool.h>
24#include "platform.h" 24#include "platform.h"
25#include "gnunet_util_lib.h" 25#include "gnunet_util_lib.h"
26#include "gnunet-qr-utils.h"
27 26
28#define LOG(fmt, ...) if (verbose == true) printf(fmt, ## __VA_ARGS__) 27#define LOG(fmt, ...) if (verbose == true) printf(fmt, ## __VA_ARGS__)
29 28
30// Command line options 29// Command line options
31// program exit code
32static long unsigned int exit_code = 1;
33
34static char* device = "/dev/video0"; 30static char* device = "/dev/video0";
35static int verbose = false; 31static int verbose = false;
36static int silent = false; 32static int silent = false;
37 33
34// Handler exit code
35static long unsigned int exit_code = 1;
36
37// Helper process we started.
38static struct GNUNET_OS_Process *p;
39
40// Pipe used to communicate shutdown via signal.
41static struct GNUNET_DISK_PipeHandle *sigpipe;
42
43
44/**
45 * Task triggered whenever we receive a SIGCHLD (child
46 * process died) or when user presses CTRL-C.
47 *
48 * @param cls closure, NULL
49 */
50static void
51maint_child_death (void *cls)
52{
53 enum GNUNET_OS_ProcessStatusType type;
54
55 if ( (GNUNET_OK !=
56 GNUNET_OS_process_status (p, &type, &exit_code)) ||
57 (type != GNUNET_OS_PROCESS_EXITED) )
58 GNUNET_break (0 == GNUNET_OS_process_kill (p, GNUNET_TERM_SIG));
59 GNUNET_OS_process_destroy (p);
60}
61
62
63/**
64 * Dispatch URIs to the appropriate GNUnet helper process
65 *
66 * @param cls closure
67 * @param uri uri to dispatch
68 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
69 * @param cfg configuration
70 */
71static void
72gnunet_uri (void *cls, const char *uri, const char *cfgfile,
73 const struct GNUNET_CONFIGURATION_Handle *cfg)
74{
75 const char *orig_uri;
76 const char *slash;
77 char *subsystem;
78 char *program;
79 struct GNUNET_SCHEDULER_Task * rt;
80
81 orig_uri = uri;
82 if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://"))) {
83 fprintf (stderr,
84 _("Invalid URI: does not start with `%s'\n"),
85 "gnunet://");
86 return;
87 }
88 uri += strlen ("gnunet://");
89 if (NULL == (slash = strchr (uri, '/')))
90 {
91 fprintf (stderr, _("Invalid URI: fails to specify subsystem\n"));
92 return;
93 }
94 subsystem = GNUNET_strndup (uri, slash - uri);
95 if (GNUNET_OK !=
96 GNUNET_CONFIGURATION_get_value_string (cfg,
97 "uri",
98 subsystem,
99 &program))
100 {
101 fprintf (stderr, _("No handler known for subsystem `%s'\n"), subsystem);
102 GNUNET_free (subsystem);
103 return;
104 }
105 GNUNET_free (subsystem);
106 rt = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
107 GNUNET_DISK_pipe_handle (sigpipe,
108 GNUNET_DISK_PIPE_END_READ),
109 &maint_child_death, NULL);
110 p = GNUNET_OS_start_process (GNUNET_NO, 0,
111 NULL, NULL, NULL,
112 program,
113 program,
114 orig_uri,
115 NULL);
116 GNUNET_free (program);
117 if (NULL == p)
118 GNUNET_SCHEDULER_cancel (rt);
119}
120
121
38/** 122/**
39 * Main function that will be run by the scheduler. 123 * Main function that will be run by the scheduler.
40 * 124 *
@@ -91,16 +175,7 @@ run (void *cls,
91 LOG("Found %s \"%s\"\n", 175 LOG("Found %s \"%s\"\n",
92 zbar_get_symbol_name(zbar_symbol_get_type(symbol)), data); 176 zbar_get_symbol_name(zbar_symbol_get_type(symbol)), data);
93 177
94 if (configuration == NULL) { 178 gnunet_uri(cls, data, cfgfile, cfg);
95 char* command_args[] = {"gnunet-uri", data, NULL };
96 LOG("Running `gnunet-uri %s`\n", data);
97 exit_code = fork_and_exec(BINDIR "gnunet-uri", command_args);
98 } else {
99 char* command_args[] = {"gnunet-uri", "-c", configuration, data, NULL };
100 LOG("Running `gnunet-uri -c '%s' %s`\n", configuration, data);
101 exit_code = fork_and_exec(BINDIR "gnunet-uri", command_args);
102 };
103
104 if (exit_code != 0) { 179 if (exit_code != 0) {
105 printf("Failed to add URI %s\n", data); 180 printf("Failed to add URI %s\n", data);
106 } else { 181 } else {