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"
33 * Minimum sensor execution interval (in seconds)
35 #define MIN_INTERVAL 30
38 * Structure containing sensor definition
44 * The configuration handle
45 * carrying sensor information
47 struct GNUNET_CONFIGURATION_Handle *cfg;
55 * Path to definition file
60 * First part of version number
62 uint16_t version_major;
65 * Second part of version number
67 uint16_t version_minor;
75 * Sensor currently enabled
80 * Category under which the sensor falls (e.g. tcp, datastore)
85 * When does the sensor become active
87 struct GNUNET_TIME_Absolute *start_time;
90 * When does the sensor expire
92 struct GNUNET_TIME_Absolute *end_time;
95 * Time interval to collect sensor information (e.g. every 1 min)
97 struct GNUNET_TIME_Relative interval;
100 * Lifetime of an information sample after which it is deleted from storage
102 struct GNUNET_TIME_Relative lifetime;
105 * A set of required peer capabilities for the sensor to collect meaningful information (e.g. ipv6)
110 * Either "gnunet-statistics" or external "process"
115 * Name of the GNUnet service that is the source for the gnunet-statistics entry
117 char *gnunet_stat_service;
120 * Name of the gnunet-statistics entry
122 char *gnunet_stat_name;
125 * Handle to statistics get request (OR GNUNET_SCHEDULER_NO_TASK)
127 struct GNUNET_STATISTICS_GetHandle *gnunet_stat_get_handle;
130 * Name of the external process to be executed
135 * Arguments to be passed to the external process
140 * Handle to the external process
142 struct GNUNET_OS_CommandHandle *ext_cmd;
145 * The output datatype to be expected
147 char *expected_datatype;
150 * Peer-identity of peer running collection point
152 struct GNUNET_PeerIdentity *collection_point;
155 * Time interval to send sensor information to collection point (e.g. every 30 mins)
157 struct GNUNET_TIME_Relative *collection_interval;
160 * Flag specifying if value is to be communicated to the p2p network
165 * Time interval to communicate value to the p2p network
167 struct GNUNET_TIME_Relative *p2p_interval;
170 * Execution task (OR GNUNET_SCHEDULER_NO_TASK)
172 GNUNET_SCHEDULER_TaskIdentifier execution_task;
175 * Is the sensor being executed
184 static const struct GNUNET_CONFIGURATION_Handle *cfg;
187 * Hashmap of loaded sensor definitions
189 struct GNUNET_CONTAINER_MultiHashMap *sensors;
192 * Supported sources of sensor information
194 static const char *sources[] = { "gnunet-statistics", "process", NULL };
197 * Supported datatypes of sensor information
199 static const char *datatypes[] = { "uint64", "double", "string", NULL };
202 * Handle to statistics service
204 struct GNUNET_STATISTICS_Handle *statistics;
207 * Remove sensor execution from scheduler
210 * @param key hash of sensor name, key to hashmap
211 * @param value a 'struct SensorInfo *'
212 * @return #GNUNET_YES if we should continue to
216 static int destroy_sensor(void *cls,
217 const struct GNUNET_HashCode *key, void *value)
219 struct SensorInfo *sensorinfo = value;
221 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Destroying sensor `%s'\n", sensorinfo->name);
222 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
224 GNUNET_SCHEDULER_cancel(sensorinfo->execution_task);
225 sensorinfo->execution_task = GNUNET_SCHEDULER_NO_TASK;
227 if(NULL != sensorinfo->gnunet_stat_get_handle)
229 GNUNET_STATISTICS_get_cancel(sensorinfo->gnunet_stat_get_handle);
230 sensorinfo->gnunet_stat_get_handle = NULL;
232 if(NULL != sensorinfo->ext_cmd)
234 GNUNET_OS_command_stop(sensorinfo->ext_cmd);
235 sensorinfo->ext_cmd = NULL;
237 if(NULL != sensorinfo->cfg)
238 GNUNET_CONFIGURATION_destroy(sensorinfo->cfg);
239 if(NULL != sensorinfo->name)
240 GNUNET_free(sensorinfo->name);
241 if(NULL != sensorinfo->def_file)
242 GNUNET_free(sensorinfo->def_file);
243 if(NULL != sensorinfo->description)
244 GNUNET_free(sensorinfo->description);
245 if(NULL != sensorinfo->category)
246 GNUNET_free(sensorinfo->category);
247 if(NULL != sensorinfo->capabilities)
248 GNUNET_free(sensorinfo->capabilities);
249 if(NULL != sensorinfo->gnunet_stat_service)
250 GNUNET_free(sensorinfo->gnunet_stat_service);
251 if(NULL != sensorinfo->gnunet_stat_name)
252 GNUNET_free(sensorinfo->gnunet_stat_name);
253 if(NULL != sensorinfo->ext_process)
254 GNUNET_free(sensorinfo->ext_process);
255 if(NULL != sensorinfo->ext_args)
256 GNUNET_free(sensorinfo->ext_args);
257 GNUNET_free(sensorinfo);
263 * Sensor will not run again unless
264 * explicitly enabled or reloaded
266 * @param sensor sensor information
268 static void set_sensor_enabled(struct SensorInfo *sensor, int state)
270 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
271 "Sensor `%s': Setting enabled to %d.\n",
272 sensor->name, state);
273 sensor->enabled = GNUNET_NO;
274 GNUNET_assert(NULL != sensor->cfg);
275 GNUNET_CONFIGURATION_set_value_string(sensor->cfg, sensor->name, "ENABLED",
276 (GNUNET_YES == state)?"YES":"NO");
277 GNUNET_CONFIGURATION_write(sensor->cfg, sensor->def_file);
281 * Task run during shutdown.
287 shutdown_task (void *cls,
288 const struct GNUNET_SCHEDULER_TaskContext *tc)
290 GNUNET_CONTAINER_multihashmap_iterate(sensors, &destroy_sensor, NULL);
291 GNUNET_CONTAINER_multihashmap_destroy(sensors);
292 if(NULL != statistics)
293 GNUNET_STATISTICS_destroy(statistics, GNUNET_YES);
294 GNUNET_SCHEDULER_shutdown();
299 * A client disconnected. Remove all of its data structure entries.
301 * @param cls closure, NULL
302 * @param client identification of the client
305 handle_client_disconnect (void *cls,
306 struct GNUNET_SERVER_Client
312 * Parses a version number string into major and minor
314 * @param version full version string
315 * @param major pointer to parsed major value
316 * @param minor pointer to parsed minor value
317 * @return #GNUNET_OK if parsing went ok, #GNUNET_SYSERROR in case of error
320 version_parse(char *version, uint16_t *major, uint16_t *minor)
325 for(; isdigit(*version); version++)
328 majorval += *version - '0';
331 return GNUNET_SYSERR;
333 for(; isdigit(*version); version++)
336 minorval += *version - '0';
339 return GNUNET_SYSERR;
347 * Load sensor definition from configuration
349 * @param cfg configuration handle
350 * @param sectionname configuration section containing definition
352 static struct SensorInfo *
353 load_sensor_from_cfg(struct GNUNET_CONFIGURATION_Handle *cfg, const char *sectionname)
355 struct SensorInfo *sensor;
359 unsigned long long time_sec;
361 sensor = GNUNET_new(struct SensorInfo);
363 sensor->name = GNUNET_strdup(sectionname);
365 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "VERSION", &version_str))
367 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor version\n"));
371 if(GNUNET_OK != version_parse(version_str, &(sensor->version_major), &(sensor->version_minor)))
373 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Invalid sensor version number, format should be major.minor\n"));
375 GNUNET_free(version_str);
378 GNUNET_free(version_str);
380 GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "DESCRIPTION", &sensor->description);
382 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "CATEGORY", &sensor->category) ||
383 NULL == sensor->category)
385 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor category\n"));
390 if(GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno(cfg, sectionname, "ENABLED"))
391 sensor->enabled = GNUNET_NO;
393 sensor->enabled = GNUNET_YES;
395 sensor->start_time = NULL;
396 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "START_TIME", &starttime_str))
398 GNUNET_STRINGS_fancy_time_to_absolute(starttime_str, sensor->start_time);
399 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Start time loaded: `%s'. Parsed: %d\n", starttime_str, (NULL != sensor->start_time));
400 GNUNET_free(starttime_str);
403 sensor->end_time = NULL;
404 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "END_TIME", &endtime_str))
406 GNUNET_STRINGS_fancy_time_to_absolute(endtime_str, sensor->end_time);
407 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "End time loaded: `%s'. Parsed: %d\n", endtime_str, (NULL != sensor->end_time));
408 GNUNET_free(endtime_str);
411 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "INTERVAL", &time_sec))
413 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor run interval\n"));
417 if(time_sec < MIN_INTERVAL)
419 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Sensor run interval too low (%" PRIu64 " < %d)\n"),
420 time_sec, MIN_INTERVAL);
424 sensor->interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
426 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "LIFETIME", &time_sec))
428 sensor->lifetime = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
431 sensor->lifetime = GNUNET_TIME_UNIT_FOREVER_REL;
434 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_choice(cfg, sectionname, "SOURCE", sources, (const char **)&sensor->source))
436 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor source\n"));
440 if(sources[0] == sensor->source) //gnunet-statistics
442 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "GNUNET_STAT_SERVICE", &sensor->gnunet_stat_service) ||
443 GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "GNUNET_STAT_NAME", &sensor->gnunet_stat_name))
445 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor gnunet-statistics source information\n"));
449 sensor->gnunet_stat_get_handle = NULL;
451 else if(sources[1] == sensor->source) //process
453 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "EXT_PROCESS", &sensor->ext_process))
455 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor process name\n"));
459 GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "EXT_ARGS", &sensor->ext_args);
462 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_choice(cfg, sectionname, "EXPECTED_DATATYPE", datatypes, (const char **)&sensor->expected_datatype))
464 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor expected datatype\n"));
468 if(sources[0] == sensor->source && datatypes[0] != sensor->expected_datatype)
470 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Invalid expected datatype, gnunet-statistics returns uint64 values\n"));
474 //TODO: reporting mechanism
476 sensor->execution_task = GNUNET_SCHEDULER_NO_TASK;
478 sensor->running = GNUNET_NO;
484 * Load sensor definition from file
486 * @param filename full path to file containing sensor definition
488 static struct SensorInfo *
489 load_sensor_from_file(const char *filename)
491 struct GNUNET_CONFIGURATION_Handle *sensorcfg;
492 const char *filebasename;
493 struct SensorInfo *sensor;
496 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
498 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to access sensor file: %s\n"), filename);
501 //load file as configuration
502 sensorcfg = GNUNET_CONFIGURATION_create();
503 if(GNUNET_SYSERR == GNUNET_CONFIGURATION_parse(sensorcfg, filename))
505 GNUNET_CONFIGURATION_destroy(sensorcfg);
506 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to load sensor definition: %s\n"), filename);
509 //configuration section should be the same as filename
510 filebasename = GNUNET_STRINGS_get_short_name(filename);
511 sensor = load_sensor_from_cfg(sensorcfg, filebasename);
514 GNUNET_CONFIGURATION_destroy(sensorcfg);
517 sensor->def_file = GNUNET_strdup(filename);
518 sensor->cfg = sensorcfg;
524 * Compares version numbers of two sensors
526 * @param s1 first sensor
527 * @param s2 second sensor
528 * @return 1: s1 > s2, 0: s1 == s2, -1: s1 < s2
531 sensor_version_compare(struct SensorInfo *s1, struct SensorInfo *s2)
533 if(s1->version_major == s2->version_major)
534 return (s1->version_minor < s2->version_minor) ? -1 : (s1->version_minor > s2->version_minor);
536 return (s1->version_major < s2->version_major) ? -1 : (s1->version_major > s2->version_major);
540 * Adds a new sensor to given hashmap.
541 * If the same name exist, compares versions and update if old.
543 * @param sensor Sensor structure to add
544 * @param map Hashmap to add to
545 * @return #GNUNET_YES if added, #GNUNET_NO if not added which is not necessarily an error
548 add_sensor_to_hashmap(struct SensorInfo *sensor, struct GNUNET_CONTAINER_MultiHashMap *map)
550 struct GNUNET_HashCode key;
551 struct SensorInfo *existing;
553 GNUNET_CRYPTO_hash(sensor->name, strlen(sensor->name), &key);
554 existing = GNUNET_CONTAINER_multihashmap_get(map, &key);
555 if(NULL != existing) //sensor with same name already exists
557 if(sensor_version_compare(existing, sensor) >= 0) //same or newer version already exist
559 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Sensor `%s' already exists with same or newer version\n"), sensor->name);
564 GNUNET_CONTAINER_multihashmap_remove(map, &key, existing); //remove the old version
565 GNUNET_free(existing);
566 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Upgrading sensor `%s' to a newer version\n"), sensor->name);
569 if(GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put(map, &key, sensor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
571 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error adding new sensor `%s' to global hashmap, this should not happen\n"), sensor->name);
579 * Iterating over files in sensors directory
582 * @param filename complete filename (absolute path)
583 * @return #GNUNET_OK to continue to iterate
586 reload_sensors_dir_cb(void *cls, const char *filename)
588 struct SensorInfo *sensor;
590 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
592 sensor = load_sensor_from_file(filename);
595 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error loading sensor from file: %s\n"), filename);
598 if(GNUNET_YES == add_sensor_to_hashmap(sensor, sensors))
599 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, _("Sensor `%s' added to global hashmap\n"), sensor->name);
601 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, ("Could not add sensor `%s' to global hashmap\n"), sensor->name);
607 * Get path to the directory containing the sensor definition files
609 * @return sensor files directory
617 datadir = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_DATADIR);
618 GNUNET_asprintf(&sensordir, "%ssensors%s",
619 datadir, DIR_SEPARATOR_STR);
620 GNUNET_free(datadir);
626 * Reads sensor definitions from data files
634 sensordir = get_sensor_dir();
635 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Reloading sensor definitions from directory `%s'\n"), sensordir);
636 GNUNET_assert(GNUNET_YES == GNUNET_DISK_directory_test(sensordir, GNUNET_YES));
638 //read all files in sensors directory
639 GNUNET_DISK_directory_scan(sensordir, &reload_sensors_dir_cb, NULL);
640 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Loaded %d sensors from directory `%s'\n"),
641 GNUNET_CONTAINER_multihashmap_size(sensors), sensordir);
642 GNUNET_free(sensordir);
646 * Creates a structure with basic sensor info to be sent to a client
648 * @param sensor sensor information
649 * @return message ready to be sent to client
651 static struct SensorInfoMessage *
652 create_sensor_info_msg(struct SensorInfo *sensor)
654 struct SensorInfoMessage *msg;
660 name_len = strlen(sensor->name);
661 if(NULL == sensor->description)
664 desc_len = strlen(sensor->description) + 1;
666 len += sizeof(struct SensorInfoMessage);
669 msg = GNUNET_malloc(len);
670 msg->header.size = htons(len);
671 msg->header.type = htons(GNUNET_MESSAGE_TYPE_SENSOR_INFO);
672 msg->name_len = htons(name_len);
673 msg->description_len = htons(desc_len);
674 msg->version_major = htons(sensor->version_major);
675 msg->version_minor = htons(sensor->version_minor);
676 str_ptr = (char*) &msg[1];
677 memcpy(str_ptr, sensor->name, name_len);
678 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor name (%d): %.*s\n",
679 name_len, name_len, str_ptr);
681 memcpy(str_ptr, sensor->description, desc_len);
682 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor description (%d): %.*s\n",
683 desc_len, desc_len, str_ptr);
689 * Handle GET SENSOR message.
692 * @param client identification of the client
693 * @param message the actual message
696 handle_get_sensor (void *cls, struct GNUNET_SERVER_Client *client,
697 const struct GNUNET_MessageHeader *message)
699 struct GNUNET_SERVER_TransmitContext *tc;
701 size_t sensorname_len;
702 struct GNUNET_HashCode key;
703 struct SensorInfo *sensorinfo;
704 struct SensorInfoMessage *msg;
706 sensorname = (char *)&message[1];
707 sensorname_len = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
708 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received for sensor (%d) `%.*s'\n",
709 "GET SENSOR", sensorname_len, sensorname_len, sensorname);
710 tc = GNUNET_SERVER_transmit_context_create (client);
711 GNUNET_CRYPTO_hash(sensorname, sensorname_len, &key);
712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created key hash for requested sensor\n");
713 sensorinfo = (struct SensorInfo *)GNUNET_CONTAINER_multihashmap_get(sensors, &key);
714 if(NULL != sensorinfo)
716 msg = create_sensor_info_msg(sensorinfo);
717 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
721 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Requested sensor `%.*s' was not found\n",
722 sensorname_len, sensorname);
723 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
724 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
728 * Iterator for sensors and adds them to transmit context
730 * @param cls a 'struct GNUNET_SERVER_TransmitContext *'
731 * @param key hash of sensor name, key to hashmap
732 * @param value a 'struct SensorInfo *'
734 int add_sensor_to_tc(void *cls,
735 const struct GNUNET_HashCode *key, void *value)
737 struct GNUNET_SERVER_TransmitContext *tc = cls;
738 struct SensorInfo *sensorinfo = value;
739 struct SensorInfoMessage *msg;
741 msg = create_sensor_info_msg(sensorinfo);
742 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
750 * Handle GET ALL SENSORS message.
753 * @param client identification of the client
754 * @param message the actual message
757 handle_get_all_sensors (void *cls, struct GNUNET_SERVER_Client *client,
758 const struct GNUNET_MessageHeader *message)
760 struct GNUNET_SERVER_TransmitContext *tc;
762 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received.\n",
764 tc = GNUNET_SERVER_transmit_context_create (client);
765 GNUNET_CONTAINER_multihashmap_iterate(sensors, &add_sensor_to_tc, tc);
766 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
767 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
771 * Do a series of checks to determine if sensor should execute
773 * @return #GNUNET_YES / #GNUNET_NO
776 should_run_sensor(struct SensorInfo *sensorinfo)
778 struct GNUNET_TIME_Absolute now;
780 if(GNUNET_NO == sensorinfo->enabled)
782 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' is disabled, will not run\n", sensorinfo->name);
785 now = GNUNET_TIME_absolute_get();
786 if(NULL != sensorinfo->start_time
787 && now.abs_value_us < sensorinfo->start_time->abs_value_us)
789 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Start time for sensor `%s' not reached yet, will not run\n", sensorinfo->name);
792 if(NULL != sensorinfo->end_time
793 && now.abs_value_us >= sensorinfo->end_time->abs_value_us)
795 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' expired, disabling.\n", sensorinfo->name);
796 set_sensor_enabled(sensorinfo, GNUNET_NO);
803 * Callback function to process statistic values
805 * @param cls 'struct SensorInfo *'
806 * @param subsystem name of subsystem that created the statistic
807 * @param name the name of the datum
808 * @param value the current value
809 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
810 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
812 int sensor_statistics_iterator (void *cls,
813 const char *subsystem,
818 struct SensorInfo *sensorinfo = cls;
820 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %" PRIu64 "\n", sensorinfo->name, value);
825 * Continuation called after sensor gets all gnunet statistics values
827 * @param cls 'struct SensorInfo *'
828 * @param success #GNUNET_OK if statistics were
829 * successfully obtained, #GNUNET_SYSERR if not.
831 void end_sensor_run_stat (void *cls, int success)
833 struct SensorInfo *sensorinfo = cls;
835 sensorinfo->gnunet_stat_get_handle = NULL;
836 sensorinfo->running = GNUNET_NO;
840 * Callback for output of executed sensor process
842 * @param cls 'struct SensorInfo *'
843 * @param line line of output from a command, NULL for the end
845 void sensor_process_callback (void *cls, const char *line)
847 struct SensorInfo *sensorinfo = cls;
849 if(NULL == line) //end of output
851 GNUNET_OS_command_stop(sensorinfo->ext_cmd);
852 sensorinfo->ext_cmd = NULL;
853 sensorinfo->running = GNUNET_NO;
856 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %s\n", sensorinfo->name, line);
860 * Checks if the given file is a path
862 * @return #GNUNET_YES / #GNUNET_NO
865 is_path(char *filename)
870 filename_len = strlen(filename);
871 for(i = 0; i < filename_len; i++)
873 if(DIR_SEPARATOR == filename[i])
880 * Actual execution of a sensor
882 * @param cls 'struct SensorInfo'
886 sensor_run (void *cls,
887 const struct GNUNET_SCHEDULER_TaskContext * tc)
889 struct SensorInfo *sensorinfo = cls;
894 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
895 if(GNUNET_YES == sensorinfo->running) //FIXME: should we try to kill?
897 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sensor `%s' running for too long, will try again next interval\n", sensorinfo->name);
900 if(GNUNET_NO == should_run_sensor(sensorinfo))
902 sensorinfo->running = GNUNET_YES;
903 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting the execution of sensor `%s'\n", sensorinfo->name);
904 if(sources[0] == sensorinfo->source) //gnunet-statistics
906 if(NULL == statistics)
908 statistics = GNUNET_STATISTICS_create("sensor", cfg);
910 sensorinfo->gnunet_stat_get_handle = GNUNET_STATISTICS_get(statistics,
911 sensorinfo->gnunet_stat_service,
912 sensorinfo->gnunet_stat_name,
913 sensorinfo->interval, //try to get values only for the interval of the sensor
914 &end_sensor_run_stat,
915 &sensor_statistics_iterator,
918 else if(sources[1] == sensorinfo->source)
920 if(GNUNET_YES == is_path(sensorinfo->ext_process))
922 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
923 "Sensor `%s': External process should not be a path, disabling sensor.\n",
925 set_sensor_enabled(sensorinfo, GNUNET_NO);
928 //check if the process exists in $PATH
929 process_path = GNUNET_strdup(sensorinfo->ext_process);
931 GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
932 if(GNUNET_SYSERR == check_result)
934 //search in sensor directory
935 sensors_dir = get_sensor_dir();
936 GNUNET_free(process_path);
937 GNUNET_asprintf(&process_path, "%s%s-files%s%s",
941 sensorinfo->ext_process);
942 GNUNET_free(sensors_dir);
944 GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
946 if(GNUNET_SYSERR == check_result)
948 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sensor `%s' process `%s' problem: binary doesn't exist or not executable\n",
950 sensorinfo->ext_process);
951 set_sensor_enabled(sensorinfo, GNUNET_NO);
952 sensorinfo->running = GNUNET_NO;
953 GNUNET_free(process_path);
956 sensorinfo->ext_cmd = GNUNET_OS_command_run(&sensor_process_callback,
958 GNUNET_TIME_UNIT_FOREVER_REL,
960 sensorinfo->ext_process,
961 sensorinfo->ext_args,
963 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Process started for sensor `%s'\n", sensorinfo->name);
964 GNUNET_free(process_path);
968 sensorinfo->running = GNUNET_NO;
969 GNUNET_break(0); //shouldn't happen
974 * Starts the execution of a sensor
977 * @param key hash of sensor name, key to hashmap (unused)
978 * @param value a 'struct SensorInfo *'
979 * @return #GNUNET_YES if we should continue to
983 int schedule_sensor(void *cls,
984 const struct GNUNET_HashCode *key, void *value)
986 struct SensorInfo *sensorinfo = value;
988 if(GNUNET_NO == should_run_sensor(sensorinfo))
990 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling sensor `%s' to run after %" PRIu64 " microseconds\n",
991 sensorinfo->name, sensorinfo->interval.rel_value_us);
992 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
994 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sensor `%s' execution task already set, this should not happen\n", sensorinfo->name);
997 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
1002 * Starts the execution of all enabled sensors
1006 schedule_all_sensors()
1008 GNUNET_CONTAINER_multihashmap_iterate(sensors, &schedule_sensor, NULL);
1012 * Process statistics requests.
1014 * @param cls closure
1015 * @param server the initialized server
1016 * @param c configuration to use
1020 struct GNUNET_SERVER_Handle *server,
1021 const struct GNUNET_CONFIGURATION_Handle *c)
1023 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1024 {&handle_get_sensor, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GET,
1026 {&handle_get_all_sensors, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GETALL,
1027 sizeof (struct GNUNET_MessageHeader)},
1032 sensors = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
1034 schedule_all_sensors();
1035 GNUNET_SERVER_add_handlers (server, handlers);
1036 GNUNET_SERVER_disconnect_notify (server,
1037 &handle_client_disconnect,
1039 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1046 * The main function for the sensor service.
1048 * @param argc number of arguments from the command line
1049 * @param argv command line arguments
1050 * @return 0 ok, 1 on error
1053 main (int argc, char *const *argv)
1055 return (GNUNET_OK ==
1056 GNUNET_SERVICE_run (argc,
1059 GNUNET_SERVICE_OPTION_NONE,
1060 &run, NULL)) ? 0 : 1;
1063 /* end of gnunet-service-sensor.c */