remove 'illegal' (non-reentrant) log logic from signal handler
[oweals/gnunet.git] / src / datacache / datacache.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2015 GNUnet e.V.
4
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.
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      Affero General Public License for more details.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 /**
21  * @file datacache/datacache.c
22  * @brief datacache API implementation
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_datacache_lib.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet_datacache_plugin.h"
30
31
32 #define LOG(kind, ...) GNUNET_log_from (kind, "datacache", __VA_ARGS__)
33
34 #define LOG_STRERROR_FILE(kind, op, fn) \
35   GNUNET_log_from_strerror_file (kind, "datacache", op, fn)
36
37 /**
38  * Internal state of the datacache library.
39  */
40 struct GNUNET_DATACACHE_Handle
41 {
42   /**
43    * Bloomfilter to quickly tell if we don't have the content.
44    */
45   struct GNUNET_CONTAINER_BloomFilter *filter;
46
47   /**
48    * Our configuration.
49    */
50   const struct GNUNET_CONFIGURATION_Handle *cfg;
51
52   /**
53    * Opaque handle for the statistics service.
54    */
55   struct GNUNET_STATISTICS_Handle *stats;
56
57   /**
58    * Configuration section to use.
59    */
60   char *section;
61
62   /**
63    * API of the transport as returned by the plugin's
64    * initialization function.
65    */
66   struct GNUNET_DATACACHE_PluginFunctions *api;
67
68   /**
69    * Short name for the plugin (i.e. "sqlite").
70    */
71   char *short_name;
72
73   /**
74    * Name of the library (i.e. "gnunet_plugin_datacache_sqlite").
75    */
76   char *lib_name;
77
78   /**
79    * Name for the bloom filter file.
80    */
81   char *bloom_name;
82
83   /**
84    * Environment provided to our plugin.
85    */
86   struct GNUNET_DATACACHE_PluginEnvironment env;
87
88   /**
89    * How much space is in use right now?
90    */
91   unsigned long long utilization;
92 };
93
94
95 /**
96  * Function called by plugins to notify the datacache
97  * about content deletions.
98  *
99  * @param cls closure
100  * @param key key of the content that was deleted
101  * @param size number of bytes that were made available
102  */
103 static void
104 env_delete_notify (void *cls, const struct GNUNET_HashCode *key, size_t size)
105 {
106   struct GNUNET_DATACACHE_Handle *h = cls;
107
108   LOG (GNUNET_ERROR_TYPE_DEBUG,
109        "Content under key `%s' discarded\n",
110        GNUNET_h2s (key));
111   GNUNET_assert (h->utilization >= size);
112   h->utilization -= size;
113   GNUNET_CONTAINER_bloomfilter_remove (h->filter, key);
114   GNUNET_STATISTICS_update (h->stats,
115                             gettext_noop ("# bytes stored"),
116                             -(long long) size,
117                             GNUNET_NO);
118   GNUNET_STATISTICS_update (h->stats,
119                             gettext_noop ("# items stored"),
120                             -1,
121                             GNUNET_NO);
122 }
123
124
125 /**
126  * Create a data cache.
127  *
128  * @param cfg configuration to use
129  * @param section section in the configuration that contains our options
130  * @return handle to use to access the service
131  */
132 struct GNUNET_DATACACHE_Handle *
133 GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
134                          const char *section)
135 {
136   unsigned int bf_size;
137   unsigned long long quota;
138   struct GNUNET_DATACACHE_Handle *ret;
139   char *libname;
140   char *name;
141
142   if (GNUNET_OK !=
143       GNUNET_CONFIGURATION_get_value_size (cfg, section, "QUOTA", &quota))
144   {
145     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, "QUOTA");
146     return NULL;
147   }
148   if (GNUNET_OK !=
149       GNUNET_CONFIGURATION_get_value_string (cfg, section, "DATABASE", &name))
150   {
151     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, "DATABASE");
152     return NULL;
153   }
154   bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
155
156   ret = GNUNET_new (struct GNUNET_DATACACHE_Handle);
157
158   if (GNUNET_YES !=
159       GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF"))
160   {
161     if (GNUNET_YES !=
162         GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF_RC"))
163     {
164       ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
165     }
166     if (NULL != ret->bloom_name)
167     {
168       ret->filter = GNUNET_CONTAINER_bloomfilter_load (
169         ret->bloom_name,
170         quota / 1024,     /* 8 bit per entry in DB, expect 1k entries */
171         5);
172     }
173     if (NULL == ret->filter)
174     {
175       ret->filter =
176         GNUNET_CONTAINER_bloomfilter_init (NULL,
177                                            bf_size,
178                                            5);    /* approx. 3% false positives at max use */
179     }
180   }
181   ret->stats = GNUNET_STATISTICS_create ("datacache", cfg);
182   ret->section = GNUNET_strdup (section);
183   ret->env.cfg = cfg;
184   ret->env.delete_notify = &env_delete_notify;
185   ret->env.section = ret->section;
186   ret->env.cls = ret;
187   ret->env.delete_notify = &env_delete_notify;
188   ret->env.quota = quota;
189   LOG (GNUNET_ERROR_TYPE_INFO, _ ("Loading `%s' datacache plugin\n"), name);
190   GNUNET_asprintf (&libname, "libgnunet_plugin_datacache_%s", name);
191   ret->short_name = name;
192   ret->lib_name = libname;
193   ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
194   if (ret->api == NULL)
195   {
196     LOG (GNUNET_ERROR_TYPE_ERROR,
197          _ ("Failed to load datacache plugin for `%s'\n"),
198          name);
199     GNUNET_DATACACHE_destroy (ret);
200     return NULL;
201   }
202   return ret;
203 }
204
205
206 /**
207  * Destroy a data cache (and free associated resources).
208  *
209  * @param h handle to the datastore
210  */
211 void
212 GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
213 {
214   if (NULL != h->filter)
215     GNUNET_CONTAINER_bloomfilter_free (h->filter);
216   if (NULL != h->api)
217     GNUNET_break (NULL == GNUNET_PLUGIN_unload (h->lib_name, h->api));
218   GNUNET_free (h->lib_name);
219   GNUNET_free (h->short_name);
220   GNUNET_free (h->section);
221   if (NULL != h->bloom_name)
222   {
223     if (0 != unlink (h->bloom_name))
224       GNUNET_log_from_strerror_file (GNUNET_ERROR_TYPE_WARNING,
225                                      "datacache",
226                                      "unlink",
227                                      h->bloom_name);
228     GNUNET_free (h->bloom_name);
229   }
230   GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO);
231   GNUNET_free (h);
232 }
233
234
235 /**
236  * Store an item in the datastore.
237  *
238  * @param h handle to the datacache
239  * @param key key to store data under
240  * @param xor_distance distance of @a key to our PID
241  * @param data_size number of bytes in @a data
242  * @param data data to store
243  * @param type type of the value
244  * @param discard_time when to discard the value in any case
245  * @param path_info_len number of entries in @a path_info
246  * @param path_info a path through the network
247  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error, #GNUNET_NO if duplicate
248  */
249 int
250 GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
251                       const struct GNUNET_HashCode *key,
252                       uint32_t xor_distance,
253                       size_t data_size,
254                       const char *data,
255                       enum GNUNET_BLOCK_Type type,
256                       struct GNUNET_TIME_Absolute discard_time,
257                       unsigned int path_info_len,
258                       const struct GNUNET_PeerIdentity *path_info)
259 {
260   ssize_t used;
261
262   used = h->api->put (h->api->cls,
263                       key,
264                       xor_distance,
265                       data_size,
266                       data,
267                       type,
268                       discard_time,
269                       path_info_len,
270                       path_info);
271   if (-1 == used)
272   {
273     GNUNET_break (0);
274     return GNUNET_SYSERR;
275   }
276   if (0 == used)
277   {
278     /* duplicate */
279     return GNUNET_NO;
280   }
281   LOG (GNUNET_ERROR_TYPE_DEBUG,
282        "Stored data under key `%s' in cache\n",
283        GNUNET_h2s (key));
284   if (NULL != h->filter)
285     GNUNET_CONTAINER_bloomfilter_add (h->filter, key);
286   GNUNET_STATISTICS_update (h->stats,
287                             gettext_noop ("# bytes stored"),
288                             used,
289                             GNUNET_NO);
290   GNUNET_STATISTICS_update (h->stats,
291                             gettext_noop ("# items stored"),
292                             1,
293                             GNUNET_NO);
294   while (h->utilization + used > h->env.quota)
295     GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls));
296   h->utilization += used;
297   return GNUNET_OK;
298 }
299
300
301 /**
302  * Iterate over the results for a particular key
303  * in the datacache.
304  *
305  * @param h handle to the datacache
306  * @param key what to look up
307  * @param type entries of which type are relevant?
308  * @param iter maybe NULL (to just count)
309  * @param iter_cls closure for @a iter
310  * @return the number of results found
311  */
312 unsigned int
313 GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
314                       const struct GNUNET_HashCode *key,
315                       enum GNUNET_BLOCK_Type type,
316                       GNUNET_DATACACHE_Iterator iter,
317                       void *iter_cls)
318 {
319   GNUNET_STATISTICS_update (h->stats,
320                             gettext_noop ("# requests received"),
321                             1,
322                             GNUNET_NO);
323   LOG (GNUNET_ERROR_TYPE_DEBUG,
324        "Processing request for key `%s'\n",
325        GNUNET_h2s (key));
326   if ((NULL != h->filter) &&
327       (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key)))
328   {
329     GNUNET_STATISTICS_update (h->stats,
330                               gettext_noop (
331                                 "# requests filtered by bloom filter"),
332                               1,
333                               GNUNET_NO);
334     LOG (GNUNET_ERROR_TYPE_DEBUG,
335          "Bloomfilter filters request for key `%s'\n",
336          GNUNET_h2s (key));
337     return 0;   /* can not be present */
338   }
339   return h->api->get (h->api->cls, key, type, iter, iter_cls);
340 }
341
342
343 /**
344  * Obtain a random element from the datacache.
345  *
346  * @param h handle to the datacache
347  * @param iter maybe NULL (to just count)
348  * @param iter_cls closure for @a iter
349  * @return the number of results found (zero or 1)
350  */
351 unsigned int
352 GNUNET_DATACACHE_get_random (struct GNUNET_DATACACHE_Handle *h,
353                              GNUNET_DATACACHE_Iterator iter,
354                              void *iter_cls)
355 {
356   GNUNET_STATISTICS_update (h->stats,
357                             gettext_noop (
358                               "# requests for random value received"),
359                             1,
360                             GNUNET_NO);
361   LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing request for random value\n");
362   return h->api->get_random (h->api->cls, iter, iter_cls);
363 }
364
365
366 /**
367  * Iterate over the results that are "close" to a particular key in
368  * the datacache.  "close" is defined as numerically larger than @a
369  * key (when interpreted as a circular address space), with small
370  * distance.
371  *
372  * @param h handle to the datacache
373  * @param key area of the keyspace to look into
374  * @param num_results number of results that should be returned to @a iter
375  * @param iter maybe NULL (to just count)
376  * @param iter_cls closure for @a iter
377  * @return the number of results found
378  */
379 unsigned int
380 GNUNET_DATACACHE_get_closest (struct GNUNET_DATACACHE_Handle *h,
381                               const struct GNUNET_HashCode *key,
382                               unsigned int num_results,
383                               GNUNET_DATACACHE_Iterator iter,
384                               void *iter_cls)
385 {
386   GNUNET_STATISTICS_update (h->stats,
387                             gettext_noop (
388                               "# proximity search requests received"),
389                             1,
390                             GNUNET_NO);
391   LOG (GNUNET_ERROR_TYPE_DEBUG,
392        "Processing proximity search at `%s'\n",
393        GNUNET_h2s (key));
394   return h->api->get_closest (h->api->cls, key, num_results, iter, iter_cls);
395 }
396
397
398 /* end of datacache.c */