2 This file is part of GNUnet.
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.
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.
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.
22 * @file sensor/sensor_util_lib.c
23 * @brief senor utilities
24 * @author Omar Tarabai
28 #include "gnunet_util_lib.h"
29 #include "gnunet_sensor_util_lib.h"
30 #include "gnunet_statistics_service.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "sensor-util",__VA_ARGS__)
35 * Minimum sensor execution interval (in seconds)
37 #define MIN_INTERVAL 30
40 * Supported sources of sensor information
42 static const char *sources[] = { "gnunet-statistics", "process", NULL };
45 * Supported datatypes of sensor information
47 static const char *datatypes[] = { "numeric", "string", NULL };
50 * Parses a version number string into major and minor
52 * @param version full version string
53 * @param major pointer to parsed major value
54 * @param minor pointer to parsed minor value
55 * @return #GNUNET_OK if parsing went ok, #GNUNET_SYSERROR in case of error
58 version_parse(char *version, uint16_t *major, uint16_t *minor)
63 for(; isdigit(*version); version++)
66 majorval += *version - '0';
71 for(; isdigit(*version); version++)
74 minorval += *version - '0';
85 * Load sensor definition from configuration
87 * @param cfg configuration handle
88 * @param sectionname configuration section containing definition
90 static struct GNUNET_SENSOR_SensorInfo *
91 load_sensor_from_cfg(struct GNUNET_CONFIGURATION_Handle *cfg, const char *sectionname)
93 struct GNUNET_SENSOR_SensorInfo *sensor;
97 unsigned long long time_sec;
99 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
101 sensor = GNUNET_new(struct GNUNET_SENSOR_SensorInfo);
103 sensor->name = GNUNET_strdup(sectionname);
105 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "VERSION", &version_str))
107 LOG (GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor version\n"));
111 if(GNUNET_OK != version_parse(version_str, &(sensor->version_major), &(sensor->version_minor)))
113 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid sensor version number, format should be major.minor\n"));
115 GNUNET_free(version_str);
118 GNUNET_free(version_str);
120 GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "DESCRIPTION", &sensor->description);
122 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "CATEGORY", &sensor->category) ||
123 NULL == sensor->category)
125 LOG (GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor category\n"));
130 if(GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno(cfg, sectionname, "ENABLED"))
131 sensor->enabled = GNUNET_NO;
133 sensor->enabled = GNUNET_YES;
135 sensor->start_time = NULL;
136 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "START_TIME", &starttime_str))
138 GNUNET_STRINGS_fancy_time_to_absolute(starttime_str, sensor->start_time);
139 LOG (GNUNET_ERROR_TYPE_DEBUG, "Start time loaded: `%s'. Parsed: %d\n", starttime_str, (NULL != sensor->start_time));
140 GNUNET_free(starttime_str);
143 sensor->end_time = NULL;
144 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "END_TIME", &endtime_str))
146 GNUNET_STRINGS_fancy_time_to_absolute(endtime_str, sensor->end_time);
147 LOG (GNUNET_ERROR_TYPE_DEBUG, "End time loaded: `%s'. Parsed: %d\n", endtime_str, (NULL != sensor->end_time));
148 GNUNET_free(endtime_str);
151 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "INTERVAL", &time_sec))
153 LOG (GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor run interval\n"));
157 if(time_sec < MIN_INTERVAL)
159 LOG (GNUNET_ERROR_TYPE_ERROR, _("Sensor run interval too low (%" PRIu64 " < %d)\n"),
160 time_sec, MIN_INTERVAL);
164 sensor->interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
166 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "LIFETIME", &time_sec))
168 sensor->lifetime = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
169 if (sensor->lifetime.rel_value_us < sensor->interval.rel_value_us)
170 LOG (GNUNET_ERROR_TYPE_WARNING,
171 "Lifetime of sensor data is preferred to be higher than interval for sensor `%s'.\n",
175 sensor->lifetime = GNUNET_TIME_UNIT_FOREVER_REL;
178 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_choice(cfg, sectionname, "SOURCE", sources, (const char **)&sensor->source))
180 LOG (GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor source\n"));
184 if(sources[0] == sensor->source) //gnunet-statistics
186 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "GNUNET_STAT_SERVICE", &sensor->gnunet_stat_service) ||
187 GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "GNUNET_STAT_NAME", &sensor->gnunet_stat_name))
189 LOG (GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor gnunet-statistics source information\n"));
193 sensor->gnunet_stat_get_handle = NULL;
195 else if(sources[1] == sensor->source) //process
197 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "EXT_PROCESS", &sensor->ext_process))
199 LOG (GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor process name\n"));
203 GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "EXT_ARGS", &sensor->ext_args);
206 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_choice(cfg, sectionname, "EXPECTED_DATATYPE", datatypes, (const char **)&sensor->expected_datatype))
208 LOG (GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor expected datatype\n"));
212 if(sources[0] == sensor->source && datatypes[0] != sensor->expected_datatype)
214 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid expected datatype, gnunet-statistics returns uint64 values\n"));
218 //reporting mechanism
219 sensor->collection_point = NULL;
220 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "COLLECTION_POINT", &dummy))
222 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "COLLECTION_INTERVAL", &time_sec))
224 LOG (GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor collection interval\n"));
228 sensor->collection_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
229 if (GNUNET_OK == GNUNET_CRYPTO_eddsa_public_key_from_string(dummy, strlen(dummy), &public_key))
231 sensor->collection_point = GNUNET_new(struct GNUNET_PeerIdentity);
232 sensor->collection_point->public_key = public_key;
237 sensor->p2p_report = GNUNET_NO;
238 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg, sectionname, "P2P_REPORT"))
240 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "P2P_INTERVAL", &time_sec))
242 LOG (GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor p2p reporting interval\n"));
246 sensor->p2p_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
247 sensor->p2p_report = GNUNET_YES;
251 sensor->execution_task = GNUNET_SCHEDULER_NO_TASK;
253 sensor->running = GNUNET_NO;
259 * Load sensor definition from file
261 * @param filename full path to file containing sensor definition
263 static struct GNUNET_SENSOR_SensorInfo *
264 load_sensor_from_file(const char *filename)
266 struct GNUNET_CONFIGURATION_Handle *sensorcfg;
267 const char *filebasename;
268 struct GNUNET_SENSOR_SensorInfo *sensor;
271 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
273 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to access sensor file: %s\n"), filename);
276 //load file as configuration
277 sensorcfg = GNUNET_CONFIGURATION_create();
278 if(GNUNET_SYSERR == GNUNET_CONFIGURATION_parse(sensorcfg, filename))
280 GNUNET_CONFIGURATION_destroy(sensorcfg);
281 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to load sensor definition: %s\n"), filename);
284 //configuration section should be the same as filename
285 filebasename = GNUNET_STRINGS_get_short_name(filename);
286 sensor = load_sensor_from_cfg(sensorcfg, filebasename);
289 GNUNET_CONFIGURATION_destroy(sensorcfg);
292 sensor->def_file = GNUNET_strdup(filename);
293 sensor->cfg = sensorcfg;
300 * Given two version numbers as major and minor, compare them.
302 * @param v1_major First part of first version number
303 * @param v1_minor Second part of first version number
304 * @param v2_major First part of second version number
305 * @param v2_minor Second part of second version number
308 GNUNET_SENSOR_version_compare (uint16_t v1_major, uint16_t v1_minor,
309 uint16_t v2_major, uint16_t v2_minor)
311 if (v1_major == v2_major)
312 return (v1_minor < v2_minor) ? -1 : (v1_minor > v2_minor);
314 return (v1_major < v2_major) ? -1 : (v1_major > v2_major);
319 * Adds a new sensor to given hashmap.
320 * If the same name exist, compares versions and update if old.
322 * @param sensor Sensor structure to add
323 * @param map Hashmap to add to
324 * @return #GNUNET_YES if added
325 * #GNUNET_NO if not added which is not necessarily an error
328 add_sensor_to_hashmap (struct GNUNET_SENSOR_SensorInfo *sensor,
329 struct GNUNET_CONTAINER_MultiHashMap *map)
331 struct GNUNET_HashCode key;
332 struct GNUNET_SENSOR_SensorInfo *existing;
334 GNUNET_CRYPTO_hash(sensor->name, strlen(sensor->name) + 1, &key);
335 existing = GNUNET_CONTAINER_multihashmap_get(map, &key);
336 if(NULL != existing) //sensor with same name already exists
338 if(GNUNET_SENSOR_version_compare (existing->version_major,
339 existing->version_minor,
340 sensor->version_major,
341 sensor->version_minor) >= 0)
343 LOG (GNUNET_ERROR_TYPE_INFO,
344 _("Sensor `%s' already exists with same or newer version\n"),
350 GNUNET_CONTAINER_multihashmap_remove(map, &key, existing); //remove the old version
351 GNUNET_free(existing);
352 LOG (GNUNET_ERROR_TYPE_INFO,
353 "Upgrading sensor `%s' to a newer version\n",
357 if(GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put(map, &key, sensor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
359 LOG (GNUNET_ERROR_TYPE_ERROR,
360 _("Error adding new sensor `%s' to global hashmap.\n"),
369 * Iterating over files in sensors directory
372 * @param filename complete filename (absolute path)
373 * @return #GNUNET_OK to continue to iterate
376 reload_sensors_dir_cb(void *cls, const char *filename)
378 struct GNUNET_CONTAINER_MultiHashMap *sensors = cls;
379 struct GNUNET_SENSOR_SensorInfo *sensor;
381 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
383 sensor = load_sensor_from_file(filename);
386 LOG (GNUNET_ERROR_TYPE_ERROR,
387 _("Error loading sensor from file: %s\n"), filename);
390 if(GNUNET_YES != add_sensor_to_hashmap(sensor, sensors))
391 LOG (GNUNET_ERROR_TYPE_WARNING,
392 "Could not add sensor `%s' to global hashmap\n", sensor->name);
398 * Get path to the directory containing the sensor definition files with a
399 * trailing directory separator.
401 * @return sensor files directory full path
404 GNUNET_SENSOR_get_sensor_dir ()
409 datadir = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_DATADIR);
410 GNUNET_asprintf (&sensordir,
414 GNUNET_free(datadir);
420 * Reads sensor definitions from local data files
422 * @return a multihashmap of loaded sensors
424 struct GNUNET_CONTAINER_MultiHashMap *
425 GNUNET_SENSOR_load_all_sensors ()
428 struct GNUNET_CONTAINER_MultiHashMap *sensors;
430 sensors = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
431 sensordir = GNUNET_SENSOR_get_sensor_dir ();
432 LOG (GNUNET_ERROR_TYPE_INFO,
433 "Loading sensor definitions from directory `%s'\n", sensordir);
434 GNUNET_assert(GNUNET_YES == GNUNET_DISK_directory_test(sensordir, GNUNET_YES));
436 //read all files in sensors directory
437 GNUNET_DISK_directory_scan(sensordir, &reload_sensors_dir_cb, sensors);
438 LOG (GNUNET_ERROR_TYPE_INFO, "Loaded %d sensors from directory `%s'\n",
439 GNUNET_CONTAINER_multihashmap_size(sensors), sensordir);
440 GNUNET_free(sensordir);
446 * Remove sensor execution from scheduler
449 * @param key hash of sensor name, key to hashmap
450 * @param value a `struct GNUNET_SENSOR_SensorInfo *`
451 * @return #GNUNET_YES if we should continue to
455 static int destroy_sensor(void *cls,
456 const struct GNUNET_HashCode *key,
459 struct GNUNET_SENSOR_SensorInfo *sensor = value;
461 if(GNUNET_SCHEDULER_NO_TASK != sensor->execution_task)
463 GNUNET_SCHEDULER_cancel(sensor->execution_task);
464 sensor->execution_task = GNUNET_SCHEDULER_NO_TASK;
466 if(NULL != sensor->gnunet_stat_get_handle)
468 GNUNET_STATISTICS_get_cancel(sensor->gnunet_stat_get_handle);
469 sensor->gnunet_stat_get_handle = NULL;
471 if(NULL != sensor->ext_cmd)
473 GNUNET_OS_command_stop(sensor->ext_cmd);
474 sensor->ext_cmd = NULL;
476 if(NULL != sensor->cfg)
477 GNUNET_CONFIGURATION_destroy(sensor->cfg);
478 if(NULL != sensor->name)
479 GNUNET_free (sensor->name);
480 if(NULL != sensor->def_file)
481 GNUNET_free (sensor->def_file);
482 if(NULL != sensor->description)
483 GNUNET_free (sensor->description);
484 if(NULL != sensor->category)
485 GNUNET_free (sensor->category);
486 if(NULL != sensor->capabilities)
487 GNUNET_free (sensor->capabilities);
488 if(NULL != sensor->gnunet_stat_service)
489 GNUNET_free (sensor->gnunet_stat_service);
490 if(NULL != sensor->gnunet_stat_name)
491 GNUNET_free (sensor->gnunet_stat_name);
492 if(NULL != sensor->ext_process)
493 GNUNET_free (sensor->ext_process);
494 if(NULL != sensor->ext_args)
495 GNUNET_free (sensor->ext_args);
496 if (NULL != sensor->collection_point)
497 GNUNET_free (sensor->collection_point);
498 GNUNET_free (sensor);
503 * Destroys a group of sensors in a hashmap and the hashmap itself
505 * @param sensors hashmap containing the sensors
508 GNUNET_SENSOR_destroy_sensors (struct GNUNET_CONTAINER_MultiHashMap *sensors)
510 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying sensor list.\n");
511 GNUNET_CONTAINER_multihashmap_iterate(sensors, &destroy_sensor, NULL);
512 GNUNET_CONTAINER_multihashmap_destroy(sensors);