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/gnunet-service-sensor.c
23 * @brief sensor service implementation
24 * @author Omar Tarabai
28 #include "gnunet_util_lib.h"
30 #include "gnunet_statistics_service.h"
31 #include "gnunet_peerstore_service.h"
34 * Minimum sensor execution interval (in seconds)
36 #define MIN_INTERVAL 30
39 * Structure containing sensor definition
45 * The configuration handle
46 * carrying sensor information
48 struct GNUNET_CONFIGURATION_Handle *cfg;
56 * Path to definition file
61 * First part of version number
63 uint16_t version_major;
66 * Second part of version number
68 uint16_t version_minor;
76 * Sensor currently enabled
81 * Category under which the sensor falls (e.g. tcp, datastore)
86 * When does the sensor become active
88 struct GNUNET_TIME_Absolute *start_time;
91 * When does the sensor expire
93 struct GNUNET_TIME_Absolute *end_time;
96 * Time interval to collect sensor information (e.g. every 1 min)
98 struct GNUNET_TIME_Relative interval;
101 * Lifetime of an information sample after which it is deleted from storage
103 struct GNUNET_TIME_Relative lifetime;
106 * A set of required peer capabilities for the sensor to collect meaningful information (e.g. ipv6)
111 * Either "gnunet-statistics" or external "process"
116 * Name of the GNUnet service that is the source for the gnunet-statistics entry
118 char *gnunet_stat_service;
121 * Name of the gnunet-statistics entry
123 char *gnunet_stat_name;
126 * Handle to statistics get request (OR GNUNET_SCHEDULER_NO_TASK)
128 struct GNUNET_STATISTICS_GetHandle *gnunet_stat_get_handle;
131 * Name of the external process to be executed
136 * Arguments to be passed to the external process
141 * Handle to the external process
143 struct GNUNET_OS_CommandHandle *ext_cmd;
146 * Did we already receive a value
147 * from the currently running external
148 * proccess ? #GNUNET_YES / #GNUNET_NO
150 int ext_cmd_value_received;
153 * The output datatype to be expected
155 char *expected_datatype;
158 * Peer-identity of peer running collection point
160 struct GNUNET_PeerIdentity *collection_point;
163 * Time interval to send sensor information to collection point (e.g. every 30 mins)
165 struct GNUNET_TIME_Relative *collection_interval;
168 * Flag specifying if value is to be communicated to the p2p network
173 * Time interval to communicate value to the p2p network
175 struct GNUNET_TIME_Relative *p2p_interval;
178 * Execution task (OR GNUNET_SCHEDULER_NO_TASK)
180 GNUNET_SCHEDULER_TaskIdentifier execution_task;
183 * Is the sensor being executed
192 static const struct GNUNET_CONFIGURATION_Handle *cfg;
195 * Hashmap of loaded sensor definitions
197 struct GNUNET_CONTAINER_MultiHashMap *sensors;
200 * Supported sources of sensor information
202 static const char *sources[] = { "gnunet-statistics", "process", NULL };
205 * Supported datatypes of sensor information
207 static const char *datatypes[] = { "uint64", "double", "string", NULL };
210 * Handle to statistics service
212 struct GNUNET_STATISTICS_Handle *statistics;
215 * Handle to peerstore service
217 struct GNUNET_PEERSTORE_Handle *peerstore;
222 char *subsystem = "sensor";
227 struct GNUNET_PeerIdentity peerid;
230 * Remove sensor execution from scheduler
233 * @param key hash of sensor name, key to hashmap
234 * @param value a 'struct SensorInfo *'
235 * @return #GNUNET_YES if we should continue to
239 static int destroy_sensor(void *cls,
240 const struct GNUNET_HashCode *key, void *value)
242 struct SensorInfo *sensorinfo = value;
244 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Destroying sensor `%s'\n", sensorinfo->name);
245 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
247 GNUNET_SCHEDULER_cancel(sensorinfo->execution_task);
248 sensorinfo->execution_task = GNUNET_SCHEDULER_NO_TASK;
250 if(NULL != sensorinfo->gnunet_stat_get_handle)
252 GNUNET_STATISTICS_get_cancel(sensorinfo->gnunet_stat_get_handle);
253 sensorinfo->gnunet_stat_get_handle = NULL;
255 if(NULL != sensorinfo->ext_cmd)
257 GNUNET_OS_command_stop(sensorinfo->ext_cmd);
258 sensorinfo->ext_cmd = NULL;
260 if(NULL != sensorinfo->cfg)
261 GNUNET_CONFIGURATION_destroy(sensorinfo->cfg);
262 if(NULL != sensorinfo->name)
263 GNUNET_free(sensorinfo->name);
264 if(NULL != sensorinfo->def_file)
265 GNUNET_free(sensorinfo->def_file);
266 if(NULL != sensorinfo->description)
267 GNUNET_free(sensorinfo->description);
268 if(NULL != sensorinfo->category)
269 GNUNET_free(sensorinfo->category);
270 if(NULL != sensorinfo->capabilities)
271 GNUNET_free(sensorinfo->capabilities);
272 if(NULL != sensorinfo->gnunet_stat_service)
273 GNUNET_free(sensorinfo->gnunet_stat_service);
274 if(NULL != sensorinfo->gnunet_stat_name)
275 GNUNET_free(sensorinfo->gnunet_stat_name);
276 if(NULL != sensorinfo->ext_process)
277 GNUNET_free(sensorinfo->ext_process);
278 if(NULL != sensorinfo->ext_args)
279 GNUNET_free(sensorinfo->ext_args);
280 GNUNET_free(sensorinfo);
286 * Sensor will not run again unless
287 * explicitly enabled or reloaded
289 * @param sensor sensor information
291 static void set_sensor_enabled(struct SensorInfo *sensor, int state)
293 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
294 "Sensor `%s': Setting enabled to %d.\n",
295 sensor->name, state);
296 sensor->enabled = GNUNET_NO;
297 GNUNET_assert(NULL != sensor->cfg);
298 GNUNET_CONFIGURATION_set_value_string(sensor->cfg, sensor->name, "ENABLED",
299 (GNUNET_YES == state)?"YES":"NO");
300 GNUNET_CONFIGURATION_write(sensor->cfg, sensor->def_file);
304 * Task run during shutdown.
310 shutdown_task (void *cls,
311 const struct GNUNET_SCHEDULER_TaskContext *tc)
313 GNUNET_CONTAINER_multihashmap_iterate(sensors, &destroy_sensor, NULL);
314 GNUNET_CONTAINER_multihashmap_destroy(sensors);
315 if(NULL != statistics)
317 GNUNET_STATISTICS_destroy(statistics, GNUNET_YES);
320 if(NULL != peerstore)
322 GNUNET_PEERSTORE_disconnect(peerstore);
325 GNUNET_SCHEDULER_shutdown();
330 * A client disconnected. Remove all of its data structure entries.
332 * @param cls closure, NULL
333 * @param client identification of the client
336 handle_client_disconnect (void *cls,
337 struct GNUNET_SERVER_Client
343 * Parses a version number string into major and minor
345 * @param version full version string
346 * @param major pointer to parsed major value
347 * @param minor pointer to parsed minor value
348 * @return #GNUNET_OK if parsing went ok, #GNUNET_SYSERROR in case of error
351 version_parse(char *version, uint16_t *major, uint16_t *minor)
356 for(; isdigit(*version); version++)
359 majorval += *version - '0';
362 return GNUNET_SYSERR;
364 for(; isdigit(*version); version++)
367 minorval += *version - '0';
370 return GNUNET_SYSERR;
378 * Load sensor definition from configuration
380 * @param cfg configuration handle
381 * @param sectionname configuration section containing definition
383 static struct SensorInfo *
384 load_sensor_from_cfg(struct GNUNET_CONFIGURATION_Handle *cfg, const char *sectionname)
386 struct SensorInfo *sensor;
390 unsigned long long time_sec;
392 sensor = GNUNET_new(struct SensorInfo);
394 sensor->name = GNUNET_strdup(sectionname);
396 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "VERSION", &version_str))
398 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor version\n"));
402 if(GNUNET_OK != version_parse(version_str, &(sensor->version_major), &(sensor->version_minor)))
404 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Invalid sensor version number, format should be major.minor\n"));
406 GNUNET_free(version_str);
409 GNUNET_free(version_str);
411 GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "DESCRIPTION", &sensor->description);
413 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "CATEGORY", &sensor->category) ||
414 NULL == sensor->category)
416 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor category\n"));
421 if(GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno(cfg, sectionname, "ENABLED"))
422 sensor->enabled = GNUNET_NO;
424 sensor->enabled = GNUNET_YES;
426 sensor->start_time = NULL;
427 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "START_TIME", &starttime_str))
429 GNUNET_STRINGS_fancy_time_to_absolute(starttime_str, sensor->start_time);
430 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Start time loaded: `%s'. Parsed: %d\n", starttime_str, (NULL != sensor->start_time));
431 GNUNET_free(starttime_str);
434 sensor->end_time = NULL;
435 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "END_TIME", &endtime_str))
437 GNUNET_STRINGS_fancy_time_to_absolute(endtime_str, sensor->end_time);
438 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "End time loaded: `%s'. Parsed: %d\n", endtime_str, (NULL != sensor->end_time));
439 GNUNET_free(endtime_str);
442 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "INTERVAL", &time_sec))
444 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor run interval\n"));
448 if(time_sec < MIN_INTERVAL)
450 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Sensor run interval too low (%" PRIu64 " < %d)\n"),
451 time_sec, MIN_INTERVAL);
455 sensor->interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
457 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "LIFETIME", &time_sec))
459 sensor->lifetime = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
462 sensor->lifetime = GNUNET_TIME_UNIT_FOREVER_REL;
465 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_choice(cfg, sectionname, "SOURCE", sources, (const char **)&sensor->source))
467 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor source\n"));
471 if(sources[0] == sensor->source) //gnunet-statistics
473 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "GNUNET_STAT_SERVICE", &sensor->gnunet_stat_service) ||
474 GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "GNUNET_STAT_NAME", &sensor->gnunet_stat_name))
476 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor gnunet-statistics source information\n"));
480 sensor->gnunet_stat_get_handle = NULL;
482 else if(sources[1] == sensor->source) //process
484 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "EXT_PROCESS", &sensor->ext_process))
486 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor process name\n"));
490 GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "EXT_ARGS", &sensor->ext_args);
493 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_choice(cfg, sectionname, "EXPECTED_DATATYPE", datatypes, (const char **)&sensor->expected_datatype))
495 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor expected datatype\n"));
499 if(sources[0] == sensor->source && datatypes[0] != sensor->expected_datatype)
501 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Invalid expected datatype, gnunet-statistics returns uint64 values\n"));
505 //TODO: reporting mechanism
507 sensor->execution_task = GNUNET_SCHEDULER_NO_TASK;
509 sensor->running = GNUNET_NO;
515 * Load sensor definition from file
517 * @param filename full path to file containing sensor definition
519 static struct SensorInfo *
520 load_sensor_from_file(const char *filename)
522 struct GNUNET_CONFIGURATION_Handle *sensorcfg;
523 const char *filebasename;
524 struct SensorInfo *sensor;
527 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
529 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to access sensor file: %s\n"), filename);
532 //load file as configuration
533 sensorcfg = GNUNET_CONFIGURATION_create();
534 if(GNUNET_SYSERR == GNUNET_CONFIGURATION_parse(sensorcfg, filename))
536 GNUNET_CONFIGURATION_destroy(sensorcfg);
537 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to load sensor definition: %s\n"), filename);
540 //configuration section should be the same as filename
541 filebasename = GNUNET_STRINGS_get_short_name(filename);
542 sensor = load_sensor_from_cfg(sensorcfg, filebasename);
545 GNUNET_CONFIGURATION_destroy(sensorcfg);
548 sensor->def_file = GNUNET_strdup(filename);
549 sensor->cfg = sensorcfg;
555 * Compares version numbers of two sensors
557 * @param s1 first sensor
558 * @param s2 second sensor
559 * @return 1: s1 > s2, 0: s1 == s2, -1: s1 < s2
562 sensor_version_compare(struct SensorInfo *s1, struct SensorInfo *s2)
564 if(s1->version_major == s2->version_major)
565 return (s1->version_minor < s2->version_minor) ? -1 : (s1->version_minor > s2->version_minor);
567 return (s1->version_major < s2->version_major) ? -1 : (s1->version_major > s2->version_major);
571 * Adds a new sensor to given hashmap.
572 * If the same name exist, compares versions and update if old.
574 * @param sensor Sensor structure to add
575 * @param map Hashmap to add to
576 * @return #GNUNET_YES if added, #GNUNET_NO if not added which is not necessarily an error
579 add_sensor_to_hashmap(struct SensorInfo *sensor, struct GNUNET_CONTAINER_MultiHashMap *map)
581 struct GNUNET_HashCode key;
582 struct SensorInfo *existing;
584 GNUNET_CRYPTO_hash(sensor->name, strlen(sensor->name), &key);
585 existing = GNUNET_CONTAINER_multihashmap_get(map, &key);
586 if(NULL != existing) //sensor with same name already exists
588 if(sensor_version_compare(existing, sensor) >= 0) //same or newer version already exist
590 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Sensor `%s' already exists with same or newer version\n"), sensor->name);
595 GNUNET_CONTAINER_multihashmap_remove(map, &key, existing); //remove the old version
596 GNUNET_free(existing);
597 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Upgrading sensor `%s' to a newer version\n", sensor->name);
600 if(GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put(map, &key, sensor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
602 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error adding new sensor `%s' to global hashmap, this should not happen\n"), sensor->name);
610 * Iterating over files in sensors directory
613 * @param filename complete filename (absolute path)
614 * @return #GNUNET_OK to continue to iterate
617 reload_sensors_dir_cb(void *cls, const char *filename)
619 struct SensorInfo *sensor;
621 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
623 sensor = load_sensor_from_file(filename);
626 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error loading sensor from file: %s\n"), filename);
629 if(GNUNET_YES == add_sensor_to_hashmap(sensor, sensors))
630 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, _("Sensor `%s' added to global hashmap\n"), sensor->name);
632 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, ("Could not add sensor `%s' to global hashmap\n"), sensor->name);
638 * Get path to the directory containing the sensor definition files
640 * @return sensor files directory
648 datadir = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_DATADIR);
649 GNUNET_asprintf(&sensordir, "%ssensors%s",
650 datadir, DIR_SEPARATOR_STR);
651 GNUNET_free(datadir);
657 * Reads sensor definitions from data files
665 sensordir = get_sensor_dir();
666 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Reloading sensor definitions from directory `%s'\n"), sensordir);
667 GNUNET_assert(GNUNET_YES == GNUNET_DISK_directory_test(sensordir, GNUNET_YES));
669 //read all files in sensors directory
670 GNUNET_DISK_directory_scan(sensordir, &reload_sensors_dir_cb, NULL);
671 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Loaded %d sensors from directory `%s'\n"),
672 GNUNET_CONTAINER_multihashmap_size(sensors), sensordir);
673 GNUNET_free(sensordir);
677 * Creates a structure with basic sensor info to be sent to a client
679 * @param sensor sensor information
680 * @return message ready to be sent to client
682 static struct SensorInfoMessage *
683 create_sensor_info_msg(struct SensorInfo *sensor)
685 struct SensorInfoMessage *msg;
691 name_len = strlen(sensor->name);
692 if(NULL == sensor->description)
695 desc_len = strlen(sensor->description) + 1;
697 len += sizeof(struct SensorInfoMessage);
700 msg = GNUNET_malloc(len);
701 msg->header.size = htons(len);
702 msg->header.type = htons(GNUNET_MESSAGE_TYPE_SENSOR_INFO);
703 msg->name_len = htons(name_len);
704 msg->description_len = htons(desc_len);
705 msg->version_major = htons(sensor->version_major);
706 msg->version_minor = htons(sensor->version_minor);
707 str_ptr = (char*) &msg[1];
708 memcpy(str_ptr, sensor->name, name_len);
709 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor name (%d): %.*s\n",
710 name_len, name_len, str_ptr);
712 memcpy(str_ptr, sensor->description, desc_len);
713 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor description (%d): %.*s\n",
714 desc_len, desc_len, str_ptr);
720 * Handle GET SENSOR message.
723 * @param client identification of the client
724 * @param message the actual message
727 handle_get_sensor (void *cls, struct GNUNET_SERVER_Client *client,
728 const struct GNUNET_MessageHeader *message)
730 struct GNUNET_SERVER_TransmitContext *tc;
732 size_t sensorname_len;
733 struct GNUNET_HashCode key;
734 struct SensorInfo *sensorinfo;
735 struct SensorInfoMessage *msg;
737 sensorname = (char *)&message[1];
738 sensorname_len = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
739 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received for sensor (%d) `%.*s'\n",
740 "GET SENSOR", sensorname_len, sensorname_len, sensorname);
741 tc = GNUNET_SERVER_transmit_context_create (client);
742 GNUNET_CRYPTO_hash(sensorname, sensorname_len, &key);
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created key hash for requested sensor\n");
744 sensorinfo = (struct SensorInfo *)GNUNET_CONTAINER_multihashmap_get(sensors, &key);
745 if(NULL != sensorinfo)
747 msg = create_sensor_info_msg(sensorinfo);
748 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
752 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Requested sensor `%.*s' was not found\n",
753 sensorname_len, sensorname);
754 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
755 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
759 * Iterator for sensors and adds them to transmit context
761 * @param cls a 'struct GNUNET_SERVER_TransmitContext *'
762 * @param key hash of sensor name, key to hashmap
763 * @param value a 'struct SensorInfo *'
765 int add_sensor_to_tc(void *cls,
766 const struct GNUNET_HashCode *key, void *value)
768 struct GNUNET_SERVER_TransmitContext *tc = cls;
769 struct SensorInfo *sensorinfo = value;
770 struct SensorInfoMessage *msg;
772 msg = create_sensor_info_msg(sensorinfo);
773 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
781 * Handle GET ALL SENSORS message.
784 * @param client identification of the client
785 * @param message the actual message
788 handle_get_all_sensors (void *cls, struct GNUNET_SERVER_Client *client,
789 const struct GNUNET_MessageHeader *message)
791 struct GNUNET_SERVER_TransmitContext *tc;
793 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received.\n",
795 tc = GNUNET_SERVER_transmit_context_create (client);
796 GNUNET_CONTAINER_multihashmap_iterate(sensors, &add_sensor_to_tc, tc);
797 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
798 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
802 * Do a series of checks to determine if sensor should execute
804 * @return #GNUNET_YES / #GNUNET_NO
807 should_run_sensor(struct SensorInfo *sensorinfo)
809 struct GNUNET_TIME_Absolute now;
811 if(GNUNET_NO == sensorinfo->enabled)
813 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' is disabled, will not run\n", sensorinfo->name);
816 now = GNUNET_TIME_absolute_get();
817 if(NULL != sensorinfo->start_time
818 && now.abs_value_us < sensorinfo->start_time->abs_value_us)
820 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Start time for sensor `%s' not reached yet, will not run\n", sensorinfo->name);
823 if(NULL != sensorinfo->end_time
824 && now.abs_value_us >= sensorinfo->end_time->abs_value_us)
826 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' expired, disabling.\n", sensorinfo->name);
827 set_sensor_enabled(sensorinfo, GNUNET_NO);
834 * Callback function to process statistic values
836 * @param cls 'struct SensorInfo *'
837 * @param subsystem name of subsystem that created the statistic
838 * @param name the name of the datum
839 * @param value the current value
840 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
841 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
843 int sensor_statistics_iterator (void *cls,
849 struct SensorInfo *sensorinfo = cls;
850 struct GNUNET_TIME_Absolute expiry;
852 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %" PRIu64 "\n", sensorinfo->name, value);
853 expiry = GNUNET_TIME_relative_to_absolute(sensorinfo->interval);
854 GNUNET_PEERSTORE_store(peerstore,
861 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
864 return GNUNET_SYSERR; /* We only want one value */
868 * Continuation called after sensor gets all gnunet statistics values
870 * @param cls 'struct SensorInfo *'
871 * @param success #GNUNET_OK if statistics were
872 * successfully obtained, #GNUNET_SYSERR if not.
874 void end_sensor_run_stat (void *cls, int success)
876 struct SensorInfo *sensorinfo = cls;
878 sensorinfo->gnunet_stat_get_handle = NULL;
879 sensorinfo->running = GNUNET_NO;
883 * Tries to parse a received sensor value to its
886 * @param value the string value received, should be null terminated
887 * @param sensor sensor information struct
888 * @param ret pointer to parsed value
889 * @return size of new parsed value, 0 for error
892 parse_sensor_value (const char *value, struct SensorInfo* sensor, void **ret)
901 //"uint64", "double", "string"
902 if (0 == strcmp("uint64", sensor->expected_datatype))
904 ullval = GNUNET_new(uint64_t);
905 *ullval = strtoull(value, &endptr, 10);
906 if ('\0' != *endptr &&
907 '\n' != *endptr) /* Invalid string */
910 return sizeof(uint64_t);
912 if(0 == strcmp("double", sensor->expected_datatype))
914 dval = GNUNET_new(double);
915 *dval = strtod(value, &endptr);
919 return sizeof(double);
921 if(0 == strcmp("string", sensor->expected_datatype))
923 *ret = GNUNET_strdup(value);
924 return strlen(value) + 1;
926 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
927 _("Unknown value type expected by sensor, this should not happen.\n"));
932 * Callback for output of executed sensor process
934 * @param cls 'struct SensorInfo *'
935 * @param line line of output from a command, NULL for the end
937 void sensor_process_callback (void *cls, const char *line)
939 struct SensorInfo *sensorinfo = cls;
942 struct GNUNET_TIME_Absolute expiry;
946 GNUNET_OS_command_stop(sensorinfo->ext_cmd);
947 sensorinfo->ext_cmd = NULL;
948 sensorinfo->running = GNUNET_NO;
949 sensorinfo->ext_cmd_value_received = GNUNET_NO;
952 if(GNUNET_YES == sensorinfo->ext_cmd_value_received)
953 return; /* We only want one *valid* value */
954 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %s\n", sensorinfo->name, line);
955 valsize = parse_sensor_value(line, sensorinfo, &value);
956 if (valsize == 0) /* invalid value, FIXME: should we disable the sensor now? */
958 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
959 _("Received an invalid value for sensor `%s': %s\n"),
960 sensorinfo->name, line);
964 sensorinfo->ext_cmd_value_received = GNUNET_YES;
965 expiry = GNUNET_TIME_relative_to_absolute(sensorinfo->interval);
966 GNUNET_PEERSTORE_store(peerstore,
973 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
980 * Checks if the given file is a path
982 * @return #GNUNET_YES / #GNUNET_NO
985 is_path(char *filename)
990 filename_len = strlen(filename);
991 for(i = 0; i < filename_len; i++)
993 if(DIR_SEPARATOR == filename[i])
1000 * Actual execution of a sensor
1002 * @param cls 'struct SensorInfo'
1006 sensor_run (void *cls,
1007 const struct GNUNET_SCHEDULER_TaskContext * tc)
1009 struct SensorInfo *sensorinfo = cls;
1014 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
1015 if(GNUNET_YES == sensorinfo->running) //FIXME: should we try to kill?
1017 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sensor `%s' running for too long, will try again next interval\n", sensorinfo->name);
1020 if(GNUNET_NO == should_run_sensor(sensorinfo))
1022 sensorinfo->running = GNUNET_YES;
1023 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting the execution of sensor `%s'\n", sensorinfo->name);
1024 if(sources[0] == sensorinfo->source) //gnunet-statistics
1026 sensorinfo->gnunet_stat_get_handle = GNUNET_STATISTICS_get(statistics,
1027 sensorinfo->gnunet_stat_service,
1028 sensorinfo->gnunet_stat_name,
1029 sensorinfo->interval, //try to get values only for the interval of the sensor
1030 &end_sensor_run_stat,
1031 &sensor_statistics_iterator,
1034 else if(sources[1] == sensorinfo->source)
1036 if(GNUNET_YES == is_path(sensorinfo->ext_process))
1038 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1039 _("Sensor `%s': External process should not be a path, disabling sensor.\n"),
1041 set_sensor_enabled(sensorinfo, GNUNET_NO);
1044 //check if the process exists in $PATH
1045 process_path = GNUNET_strdup(sensorinfo->ext_process);
1047 GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
1048 if(GNUNET_SYSERR == check_result)
1050 //search in sensor directory
1051 sensors_dir = get_sensor_dir();
1052 GNUNET_free(process_path);
1053 GNUNET_asprintf(&process_path, "%s%s-files%s%s",
1057 sensorinfo->ext_process);
1058 GNUNET_free(sensors_dir);
1060 GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
1062 if(GNUNET_SYSERR == check_result)
1064 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1065 _("Sensor `%s' process `%s' problem: binary doesn't exist or not executable\n"),
1067 sensorinfo->ext_process);
1068 set_sensor_enabled(sensorinfo, GNUNET_NO);
1069 sensorinfo->running = GNUNET_NO;
1070 GNUNET_free(process_path);
1073 sensorinfo->ext_cmd_value_received = GNUNET_NO;
1074 sensorinfo->ext_cmd = GNUNET_OS_command_run(&sensor_process_callback,
1076 GNUNET_TIME_UNIT_FOREVER_REL,
1078 sensorinfo->ext_process,
1079 sensorinfo->ext_args,
1081 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Process started for sensor `%s'\n", sensorinfo->name);
1082 GNUNET_free(process_path);
1086 sensorinfo->running = GNUNET_NO;
1087 GNUNET_break(0); //shouldn't happen
1092 * Starts the execution of a sensor
1095 * @param key hash of sensor name, key to hashmap (unused)
1096 * @param value a 'struct SensorInfo *'
1097 * @return #GNUNET_YES if we should continue to
1099 * #GNUNET_NO if not.
1101 int schedule_sensor(void *cls,
1102 const struct GNUNET_HashCode *key, void *value)
1104 struct SensorInfo *sensorinfo = value;
1106 if(GNUNET_NO == should_run_sensor(sensorinfo))
1108 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling sensor `%s' to run after %" PRIu64 " microseconds\n",
1109 sensorinfo->name, sensorinfo->interval.rel_value_us);
1110 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
1112 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1113 _("Sensor `%s' execution task already set, this should not happen\n"), sensorinfo->name);
1116 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
1121 * Starts the execution of all enabled sensors
1125 schedule_all_sensors()
1127 GNUNET_CONTAINER_multihashmap_iterate(sensors, &schedule_sensor, NULL);
1131 * Process statistics requests.
1133 * @param cls closure
1134 * @param server the initialized server
1135 * @param c configuration to use
1139 struct GNUNET_SERVER_Handle *server,
1140 const struct GNUNET_CONFIGURATION_Handle *c)
1142 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1143 {&handle_get_sensor, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GET,
1145 {&handle_get_all_sensors, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GETALL,
1146 sizeof (struct GNUNET_MessageHeader)},
1151 sensors = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
1153 schedule_all_sensors();
1154 statistics = GNUNET_STATISTICS_create("sensor", cfg);
1155 GNUNET_CRYPTO_get_peer_identity(cfg, &peerid);
1156 peerstore = GNUNET_PEERSTORE_connect(cfg);
1157 GNUNET_SERVER_add_handlers (server, handlers);
1158 GNUNET_SERVER_disconnect_notify (server,
1159 &handle_client_disconnect,
1161 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1168 * The main function for the sensor service.
1170 * @param argc number of arguments from the command line
1171 * @param argv command line arguments
1172 * @return 0 ok, 1 on error
1175 main (int argc, char *const *argv)
1177 return (GNUNET_OK ==
1178 GNUNET_SERVICE_run (argc,
1181 GNUNET_SERVICE_OPTION_NONE,
1182 &run, NULL)) ? 0 : 1;
1185 /* end of gnunet-service-sensor.c */