- valgrind fix
[oweals/gnunet.git] / src / regex / gnunet-daemon-regexprofiler.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012, 2013 Christian Grothoff
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file regex/gnunet-daemon-regexprofiler.c
23  * @brief daemon that uses mesh to announce a regular expression. Used in
24  * conjunction with gnunet-regex-profiler to announce regexes on serveral peers
25  * without the need to explicitly connect to the mesh service running on the
26  * peer from within the profiler.
27  * @author Maximilian Szengel
28  * @author Bartlomiej Polot
29  */
30 #include "platform.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_regex_lib.h"
33 #include "regex_test_lib.h"
34 #include "gnunet_dht_service.h"
35 #include "gnunet_statistics_service.h"
36
37 /**
38  * Return value from 'main'.
39  */
40 static int global_ret;
41
42 /**
43  * Configuration we use.
44  */
45 static const struct GNUNET_CONFIGURATION_Handle *cfg;
46
47 /**
48  * Handle to the statistics service.
49  */
50 static struct GNUNET_STATISTICS_Handle *stats_handle;
51
52 /**
53  * Peer's dht handle.
54  */
55 static struct GNUNET_DHT_Handle *dht_handle;
56
57 /**
58  * Peer's regex announce handle.
59  */
60 static struct GNUNET_REGEX_announce_handle *announce_handle;
61
62 /**
63  * Periodically reannounce regex.
64  */
65 static GNUNET_SCHEDULER_TaskIdentifier reannounce_task;
66
67 /**
68  * What's the maximum reannounce period.
69  */
70 static struct GNUNET_TIME_Relative reannounce_period_max;
71
72 /**
73  * Maximal path compression length for regex announcing.
74  */
75 static unsigned long long max_path_compression;
76
77 /**
78  * Name of the file containing policies that this peer should announce. One
79  * policy per line.
80  */
81 static char * policy_filename;
82
83 /**
84  * Prefix to add before every regex we're announcing.
85  */
86 static char * regex_prefix;
87
88 /**
89  * Regex with prefix.
90  */
91 static char *rx_with_pfx;
92
93 /**
94  * How many put rounds should we do.
95  */
96 static unsigned int rounds = 5;
97
98
99 /**
100  * Task run during shutdown.
101  *
102  * @param cls unused
103  * @param tc unused
104  */
105 static void
106 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
107 {
108   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
109
110   if (NULL != announce_handle)
111   {
112     GNUNET_REGEX_announce_cancel (announce_handle);
113     announce_handle = NULL;
114   }
115
116   if (NULL != dht_handle)
117   {
118     GNUNET_DHT_disconnect (dht_handle);
119     dht_handle = NULL;
120   }
121
122   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
123               "Daemon for %s shutting down\n",
124               policy_filename);
125 }
126
127
128 /**
129  * Announce a previously announced regex re-using cached data.
130  * 
131  * @param cls Closure (regex to announce if needed).
132  * @param tc TaskContext.
133  */
134 static void
135 reannounce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
136 {
137   struct GNUNET_PeerIdentity id;
138   struct GNUNET_TIME_Relative random_delay;
139   char *regex = cls;
140
141   reannounce_task = GNUNET_SCHEDULER_NO_TASK;
142   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
143   {
144     GNUNET_free (regex);
145     return;
146   }
147
148   if (0 == rounds--)
149   {
150     global_ret = 0;
151     GNUNET_SCHEDULER_shutdown ();
152     GNUNET_free (regex);
153     return;
154   }
155   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
156   GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
157   if (NULL == announce_handle && NULL != regex)
158   {
159     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160                 "First time, creating regex: %s\n",
161                 regex);
162     memset (&id, 0, sizeof (struct GNUNET_PeerIdentity));
163     announce_handle = GNUNET_REGEX_announce (dht_handle,
164                                             &id,
165                                             regex,
166                                             (unsigned int) max_path_compression,
167                                             stats_handle);
168   }
169   else
170   {
171     GNUNET_assert (NULL != announce_handle);
172     GNUNET_REGEX_reannounce (announce_handle);
173   }
174
175   random_delay =
176     GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
177                                    GNUNET_CRYPTO_random_u32 (
178                                      GNUNET_CRYPTO_QUALITY_WEAK,
179                                      reannounce_period_max.rel_value));
180   reannounce_task = GNUNET_SCHEDULER_add_delayed (random_delay,
181                                                   &reannounce_regex, cls);
182 }
183
184
185 /**
186  * Announce the given regular expression using regex and the path compression
187  * length read from config.
188  *
189  * @param regex regular expression to announce on this peer's mesh.
190  */
191 static void
192 announce_regex (const char * regex)
193 {
194   char *copy;
195
196   if (NULL == regex || 0 == strlen (regex))
197   {
198     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
199     return;
200   }
201
202   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
203               "Daemon for %s starting\n",
204               policy_filename);
205   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == reannounce_task);
206   copy = GNUNET_strdup (regex);
207   reannounce_task = GNUNET_SCHEDULER_add_now (reannounce_regex, (void *) copy);
208 }
209
210
211 /**
212  * Scan through the policy_dir looking for the n-th filename.
213  *
214  * @param cls Closure (target number n).
215  * @param filename complete filename (absolute path).
216  * @return GNUNET_OK to continue to iterate,
217  *  GNUNET_NO to stop when found
218  */
219 static int
220 scan (void *cls, const char *filename)
221 {
222   long n = (long) cls;
223   static long c = 0;
224
225   if (c == n)
226   {
227     policy_filename = GNUNET_strdup (filename);
228     return GNUNET_NO;
229   }
230   c++;
231   return GNUNET_OK;
232 }
233
234
235 /**
236  * @brief Main function that will be run by the scheduler.
237  *
238  * @param cls closure
239  * @param args remaining command-line arguments
240  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
241  * @param cfg_ configuration
242  */
243 static void
244 run (void *cls, char *const *args GNUNET_UNUSED,
245      const char *cfgfile GNUNET_UNUSED,
246      const struct GNUNET_CONFIGURATION_Handle *cfg_)
247 {
248   char *regex = NULL;
249   char **components;
250   char *policy_dir;
251   long long unsigned int peer_id;
252
253   cfg = cfg_;
254
255   if (GNUNET_OK !=
256       GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER",
257                                              "MAX_PATH_COMPRESSION",
258                                              &max_path_compression))
259   {
260     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
261                 _
262                 ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
263                 "regexprofiler", "max_path_compression");
264     global_ret = GNUNET_SYSERR;
265     GNUNET_SCHEDULER_shutdown ();
266     return;
267   }
268   if (GNUNET_OK !=
269       GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
270                                              "POLICY_DIR", &policy_dir))
271   {
272     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
273                 _
274                 ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
275                 "regexprofiler", "policy_dir");
276     global_ret = GNUNET_SYSERR;
277     GNUNET_SCHEDULER_shutdown ();
278     return;
279   }
280   if (GNUNET_OK !=
281       GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
282                                              "PEERID", &peer_id))
283   {
284     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
285                 _
286                 ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
287                 "regexprofiler", "policy_file");
288     global_ret = GNUNET_SYSERR;
289     GNUNET_SCHEDULER_shutdown ();
290     return;
291   }
292
293   if (GNUNET_OK !=
294       GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
295                                              "REGEX_PREFIX", &regex_prefix))
296   {
297     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
298                 _
299                 ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
300                 "regexprofiler", "regex_prefix");
301     global_ret = GNUNET_SYSERR;
302     GNUNET_SCHEDULER_shutdown ();
303     return;
304   }
305
306   if (GNUNET_OK !=
307       GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
308                                            "REANNOUNCE_PERIOD_MAX",
309                                            &reannounce_period_max))
310   {
311     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
312                 "reannounce_period_max not given. Using 10 minutes.\n");
313     reannounce_period_max =
314       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
315   }
316
317   stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg);
318
319   dht_handle = GNUNET_DHT_connect (cfg, 1);
320
321   if (NULL == dht_handle)
322   {
323     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
324                 "Could not acquire dht handle. Exiting.\n");
325     global_ret = GNUNET_SYSERR;
326     GNUNET_SCHEDULER_shutdown ();
327     return;
328   }
329
330   /* Read regexes from policy files */
331   GNUNET_assert (-1 != GNUNET_DISK_directory_scan (policy_dir, &scan,
332                                                    (void *) (long) peer_id));
333   if (NULL == (components = GNUNET_REGEX_read_from_file (policy_filename)))
334   {
335     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
336                 "Policy file %s contains no policies. Exiting.\n",
337                 policy_filename);
338     global_ret = GNUNET_SYSERR;
339     GNUNET_SCHEDULER_shutdown ();
340     return;
341   }
342   regex = GNUNET_REGEX_combine (components);
343   GNUNET_REGEX_free_from_file (components);
344
345   /* Announcing regexes from policy_filename */
346   GNUNET_asprintf (&rx_with_pfx, "%s(%s)(0|1)*", regex_prefix, regex);
347   announce_regex (rx_with_pfx);
348   GNUNET_free (regex);
349   GNUNET_free (rx_with_pfx);
350
351   /* Scheduled the task to clean up when shutdown is called */
352   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
353                                 NULL);
354 }
355
356
357 /**
358  * The main function of the regexprofiler service.
359  *
360  * @param argc number of arguments from the command line
361  * @param argv command line arguments
362  * @return 0 ok, 1 on error
363  */
364 int
365 main (int argc, char *const *argv)
366 {
367   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
368     GNUNET_GETOPT_OPTION_END
369   };
370
371   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
372     return 2;
373   return (GNUNET_OK ==
374           GNUNET_PROGRAM_run (argc, argv, "regexprofiler",
375                               gettext_noop
376                               ("Daemon to announce regular expressions for the peer using mesh."),
377                               options, &run, NULL)) ? global_ret : 1;
378 }
379
380
381 #ifdef LINUX
382 #include <malloc.h>
383
384 /**
385  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
386  */
387 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
388 {
389   mallopt (M_TRIM_THRESHOLD, 4 * 1024);
390   mallopt (M_TOP_PAD, 1 * 1024);
391   malloc_trim (0);
392 }
393 #endif
394
395
396 /* end of gnunet-daemon-regexprofiler.c */