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 * The output datatype to be expected
148 char *expected_datatype;
151 * Peer-identity of peer running collection point
153 struct GNUNET_PeerIdentity *collection_point;
156 * Time interval to send sensor information to collection point (e.g. every 30 mins)
158 struct GNUNET_TIME_Relative *collection_interval;
161 * Flag specifying if value is to be communicated to the p2p network
166 * Time interval to communicate value to the p2p network
168 struct GNUNET_TIME_Relative *p2p_interval;
171 * Execution task (OR GNUNET_SCHEDULER_NO_TASK)
173 GNUNET_SCHEDULER_TaskIdentifier execution_task;
176 * Is the sensor being executed
185 static const struct GNUNET_CONFIGURATION_Handle *cfg;
188 * Hashmap of loaded sensor definitions
190 struct GNUNET_CONTAINER_MultiHashMap *sensors;
193 * Supported sources of sensor information
195 static const char *sources[] = { "gnunet-statistics", "process", NULL };
198 * Supported datatypes of sensor information
200 static const char *datatypes[] = { "uint64", "double", "string", NULL };
203 * Handle to statistics service
205 struct GNUNET_STATISTICS_Handle *statistics;
208 * Handle to peerstore service
210 struct GNUNET_PEERSTORE_Handle *peerstore;
215 char *subsystem = "sensor";
220 struct GNUNET_PeerIdentity peerid;
223 * Remove sensor execution from scheduler
226 * @param key hash of sensor name, key to hashmap
227 * @param value a 'struct SensorInfo *'
228 * @return #GNUNET_YES if we should continue to
232 static int destroy_sensor(void *cls,
233 const struct GNUNET_HashCode *key, void *value)
235 struct SensorInfo *sensorinfo = value;
237 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Destroying sensor `%s'\n", sensorinfo->name);
238 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
240 GNUNET_SCHEDULER_cancel(sensorinfo->execution_task);
241 sensorinfo->execution_task = GNUNET_SCHEDULER_NO_TASK;
243 if(NULL != sensorinfo->gnunet_stat_get_handle)
245 GNUNET_STATISTICS_get_cancel(sensorinfo->gnunet_stat_get_handle);
246 sensorinfo->gnunet_stat_get_handle = NULL;
248 if(NULL != sensorinfo->ext_cmd)
250 GNUNET_OS_command_stop(sensorinfo->ext_cmd);
251 sensorinfo->ext_cmd = NULL;
253 if(NULL != sensorinfo->cfg)
254 GNUNET_CONFIGURATION_destroy(sensorinfo->cfg);
255 if(NULL != sensorinfo->name)
256 GNUNET_free(sensorinfo->name);
257 if(NULL != sensorinfo->def_file)
258 GNUNET_free(sensorinfo->def_file);
259 if(NULL != sensorinfo->description)
260 GNUNET_free(sensorinfo->description);
261 if(NULL != sensorinfo->category)
262 GNUNET_free(sensorinfo->category);
263 if(NULL != sensorinfo->capabilities)
264 GNUNET_free(sensorinfo->capabilities);
265 if(NULL != sensorinfo->gnunet_stat_service)
266 GNUNET_free(sensorinfo->gnunet_stat_service);
267 if(NULL != sensorinfo->gnunet_stat_name)
268 GNUNET_free(sensorinfo->gnunet_stat_name);
269 if(NULL != sensorinfo->ext_process)
270 GNUNET_free(sensorinfo->ext_process);
271 if(NULL != sensorinfo->ext_args)
272 GNUNET_free(sensorinfo->ext_args);
273 GNUNET_free(sensorinfo);
279 * Sensor will not run again unless
280 * explicitly enabled or reloaded
282 * @param sensor sensor information
284 static void set_sensor_enabled(struct SensorInfo *sensor, int state)
286 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
287 "Sensor `%s': Setting enabled to %d.\n",
288 sensor->name, state);
289 sensor->enabled = GNUNET_NO;
290 GNUNET_assert(NULL != sensor->cfg);
291 GNUNET_CONFIGURATION_set_value_string(sensor->cfg, sensor->name, "ENABLED",
292 (GNUNET_YES == state)?"YES":"NO");
293 GNUNET_CONFIGURATION_write(sensor->cfg, sensor->def_file);
297 * Task run during shutdown.
303 shutdown_task (void *cls,
304 const struct GNUNET_SCHEDULER_TaskContext *tc)
306 GNUNET_CONTAINER_multihashmap_iterate(sensors, &destroy_sensor, NULL);
307 GNUNET_CONTAINER_multihashmap_destroy(sensors);
308 if(NULL != statistics)
310 GNUNET_STATISTICS_destroy(statistics, GNUNET_YES);
313 if(NULL != peerstore)
315 GNUNET_PEERSTORE_disconnect(peerstore);
318 GNUNET_SCHEDULER_shutdown();
323 * A client disconnected. Remove all of its data structure entries.
325 * @param cls closure, NULL
326 * @param client identification of the client
329 handle_client_disconnect (void *cls,
330 struct GNUNET_SERVER_Client
336 * Parses a version number string into major and minor
338 * @param version full version string
339 * @param major pointer to parsed major value
340 * @param minor pointer to parsed minor value
341 * @return #GNUNET_OK if parsing went ok, #GNUNET_SYSERROR in case of error
344 version_parse(char *version, uint16_t *major, uint16_t *minor)
349 for(; isdigit(*version); version++)
352 majorval += *version - '0';
355 return GNUNET_SYSERR;
357 for(; isdigit(*version); version++)
360 minorval += *version - '0';
363 return GNUNET_SYSERR;
371 * Load sensor definition from configuration
373 * @param cfg configuration handle
374 * @param sectionname configuration section containing definition
376 static struct SensorInfo *
377 load_sensor_from_cfg(struct GNUNET_CONFIGURATION_Handle *cfg, const char *sectionname)
379 struct SensorInfo *sensor;
383 unsigned long long time_sec;
385 sensor = GNUNET_new(struct SensorInfo);
387 sensor->name = GNUNET_strdup(sectionname);
389 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "VERSION", &version_str))
391 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor version\n"));
395 if(GNUNET_OK != version_parse(version_str, &(sensor->version_major), &(sensor->version_minor)))
397 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Invalid sensor version number, format should be major.minor\n"));
399 GNUNET_free(version_str);
402 GNUNET_free(version_str);
404 GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "DESCRIPTION", &sensor->description);
406 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "CATEGORY", &sensor->category) ||
407 NULL == sensor->category)
409 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor category\n"));
414 if(GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno(cfg, sectionname, "ENABLED"))
415 sensor->enabled = GNUNET_NO;
417 sensor->enabled = GNUNET_YES;
419 sensor->start_time = NULL;
420 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "START_TIME", &starttime_str))
422 GNUNET_STRINGS_fancy_time_to_absolute(starttime_str, sensor->start_time);
423 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Start time loaded: `%s'. Parsed: %d\n", starttime_str, (NULL != sensor->start_time));
424 GNUNET_free(starttime_str);
427 sensor->end_time = NULL;
428 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "END_TIME", &endtime_str))
430 GNUNET_STRINGS_fancy_time_to_absolute(endtime_str, sensor->end_time);
431 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "End time loaded: `%s'. Parsed: %d\n", endtime_str, (NULL != sensor->end_time));
432 GNUNET_free(endtime_str);
435 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "INTERVAL", &time_sec))
437 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor run interval\n"));
441 if(time_sec < MIN_INTERVAL)
443 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Sensor run interval too low (%" PRIu64 " < %d)\n"),
444 time_sec, MIN_INTERVAL);
448 sensor->interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
450 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "LIFETIME", &time_sec))
452 sensor->lifetime = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
455 sensor->lifetime = GNUNET_TIME_UNIT_FOREVER_REL;
458 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_choice(cfg, sectionname, "SOURCE", sources, (const char **)&sensor->source))
460 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor source\n"));
464 if(sources[0] == sensor->source) //gnunet-statistics
466 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "GNUNET_STAT_SERVICE", &sensor->gnunet_stat_service) ||
467 GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "GNUNET_STAT_NAME", &sensor->gnunet_stat_name))
469 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor gnunet-statistics source information\n"));
473 sensor->gnunet_stat_get_handle = NULL;
475 else if(sources[1] == sensor->source) //process
477 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "EXT_PROCESS", &sensor->ext_process))
479 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor process name\n"));
483 GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "EXT_ARGS", &sensor->ext_args);
486 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_choice(cfg, sectionname, "EXPECTED_DATATYPE", datatypes, (const char **)&sensor->expected_datatype))
488 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor expected datatype\n"));
492 if(sources[0] == sensor->source && datatypes[0] != sensor->expected_datatype)
494 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Invalid expected datatype, gnunet-statistics returns uint64 values\n"));
498 //TODO: reporting mechanism
500 sensor->execution_task = GNUNET_SCHEDULER_NO_TASK;
502 sensor->running = GNUNET_NO;
508 * Load sensor definition from file
510 * @param filename full path to file containing sensor definition
512 static struct SensorInfo *
513 load_sensor_from_file(const char *filename)
515 struct GNUNET_CONFIGURATION_Handle *sensorcfg;
516 const char *filebasename;
517 struct SensorInfo *sensor;
520 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
522 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to access sensor file: %s\n"), filename);
525 //load file as configuration
526 sensorcfg = GNUNET_CONFIGURATION_create();
527 if(GNUNET_SYSERR == GNUNET_CONFIGURATION_parse(sensorcfg, filename))
529 GNUNET_CONFIGURATION_destroy(sensorcfg);
530 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to load sensor definition: %s\n"), filename);
533 //configuration section should be the same as filename
534 filebasename = GNUNET_STRINGS_get_short_name(filename);
535 sensor = load_sensor_from_cfg(sensorcfg, filebasename);
538 GNUNET_CONFIGURATION_destroy(sensorcfg);
541 sensor->def_file = GNUNET_strdup(filename);
542 sensor->cfg = sensorcfg;
548 * Compares version numbers of two sensors
550 * @param s1 first sensor
551 * @param s2 second sensor
552 * @return 1: s1 > s2, 0: s1 == s2, -1: s1 < s2
555 sensor_version_compare(struct SensorInfo *s1, struct SensorInfo *s2)
557 if(s1->version_major == s2->version_major)
558 return (s1->version_minor < s2->version_minor) ? -1 : (s1->version_minor > s2->version_minor);
560 return (s1->version_major < s2->version_major) ? -1 : (s1->version_major > s2->version_major);
564 * Adds a new sensor to given hashmap.
565 * If the same name exist, compares versions and update if old.
567 * @param sensor Sensor structure to add
568 * @param map Hashmap to add to
569 * @return #GNUNET_YES if added, #GNUNET_NO if not added which is not necessarily an error
572 add_sensor_to_hashmap(struct SensorInfo *sensor, struct GNUNET_CONTAINER_MultiHashMap *map)
574 struct GNUNET_HashCode key;
575 struct SensorInfo *existing;
577 GNUNET_CRYPTO_hash(sensor->name, strlen(sensor->name), &key);
578 existing = GNUNET_CONTAINER_multihashmap_get(map, &key);
579 if(NULL != existing) //sensor with same name already exists
581 if(sensor_version_compare(existing, sensor) >= 0) //same or newer version already exist
583 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Sensor `%s' already exists with same or newer version\n"), sensor->name);
588 GNUNET_CONTAINER_multihashmap_remove(map, &key, existing); //remove the old version
589 GNUNET_free(existing);
590 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Upgrading sensor `%s' to a newer version\n"), sensor->name);
593 if(GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put(map, &key, sensor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
595 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error adding new sensor `%s' to global hashmap, this should not happen\n"), sensor->name);
603 * Iterating over files in sensors directory
606 * @param filename complete filename (absolute path)
607 * @return #GNUNET_OK to continue to iterate
610 reload_sensors_dir_cb(void *cls, const char *filename)
612 struct SensorInfo *sensor;
614 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
616 sensor = load_sensor_from_file(filename);
619 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error loading sensor from file: %s\n"), filename);
622 if(GNUNET_YES == add_sensor_to_hashmap(sensor, sensors))
623 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, _("Sensor `%s' added to global hashmap\n"), sensor->name);
625 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, ("Could not add sensor `%s' to global hashmap\n"), sensor->name);
631 * Get path to the directory containing the sensor definition files
633 * @return sensor files directory
641 datadir = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_DATADIR);
642 GNUNET_asprintf(&sensordir, "%ssensors%s",
643 datadir, DIR_SEPARATOR_STR);
644 GNUNET_free(datadir);
650 * Reads sensor definitions from data files
658 sensordir = get_sensor_dir();
659 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Reloading sensor definitions from directory `%s'\n"), sensordir);
660 GNUNET_assert(GNUNET_YES == GNUNET_DISK_directory_test(sensordir, GNUNET_YES));
662 //read all files in sensors directory
663 GNUNET_DISK_directory_scan(sensordir, &reload_sensors_dir_cb, NULL);
664 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Loaded %d sensors from directory `%s'\n"),
665 GNUNET_CONTAINER_multihashmap_size(sensors), sensordir);
666 GNUNET_free(sensordir);
670 * Creates a structure with basic sensor info to be sent to a client
672 * @param sensor sensor information
673 * @return message ready to be sent to client
675 static struct SensorInfoMessage *
676 create_sensor_info_msg(struct SensorInfo *sensor)
678 struct SensorInfoMessage *msg;
684 name_len = strlen(sensor->name);
685 if(NULL == sensor->description)
688 desc_len = strlen(sensor->description) + 1;
690 len += sizeof(struct SensorInfoMessage);
693 msg = GNUNET_malloc(len);
694 msg->header.size = htons(len);
695 msg->header.type = htons(GNUNET_MESSAGE_TYPE_SENSOR_INFO);
696 msg->name_len = htons(name_len);
697 msg->description_len = htons(desc_len);
698 msg->version_major = htons(sensor->version_major);
699 msg->version_minor = htons(sensor->version_minor);
700 str_ptr = (char*) &msg[1];
701 memcpy(str_ptr, sensor->name, name_len);
702 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor name (%d): %.*s\n",
703 name_len, name_len, str_ptr);
705 memcpy(str_ptr, sensor->description, desc_len);
706 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor description (%d): %.*s\n",
707 desc_len, desc_len, str_ptr);
713 * Handle GET SENSOR message.
716 * @param client identification of the client
717 * @param message the actual message
720 handle_get_sensor (void *cls, struct GNUNET_SERVER_Client *client,
721 const struct GNUNET_MessageHeader *message)
723 struct GNUNET_SERVER_TransmitContext *tc;
725 size_t sensorname_len;
726 struct GNUNET_HashCode key;
727 struct SensorInfo *sensorinfo;
728 struct SensorInfoMessage *msg;
730 sensorname = (char *)&message[1];
731 sensorname_len = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
732 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received for sensor (%d) `%.*s'\n",
733 "GET SENSOR", sensorname_len, sensorname_len, sensorname);
734 tc = GNUNET_SERVER_transmit_context_create (client);
735 GNUNET_CRYPTO_hash(sensorname, sensorname_len, &key);
736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created key hash for requested sensor\n");
737 sensorinfo = (struct SensorInfo *)GNUNET_CONTAINER_multihashmap_get(sensors, &key);
738 if(NULL != sensorinfo)
740 msg = create_sensor_info_msg(sensorinfo);
741 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
745 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Requested sensor `%.*s' was not found\n",
746 sensorname_len, sensorname);
747 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
748 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
752 * Iterator for sensors and adds them to transmit context
754 * @param cls a 'struct GNUNET_SERVER_TransmitContext *'
755 * @param key hash of sensor name, key to hashmap
756 * @param value a 'struct SensorInfo *'
758 int add_sensor_to_tc(void *cls,
759 const struct GNUNET_HashCode *key, void *value)
761 struct GNUNET_SERVER_TransmitContext *tc = cls;
762 struct SensorInfo *sensorinfo = value;
763 struct SensorInfoMessage *msg;
765 msg = create_sensor_info_msg(sensorinfo);
766 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
774 * Handle GET ALL SENSORS message.
777 * @param client identification of the client
778 * @param message the actual message
781 handle_get_all_sensors (void *cls, struct GNUNET_SERVER_Client *client,
782 const struct GNUNET_MessageHeader *message)
784 struct GNUNET_SERVER_TransmitContext *tc;
786 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received.\n",
788 tc = GNUNET_SERVER_transmit_context_create (client);
789 GNUNET_CONTAINER_multihashmap_iterate(sensors, &add_sensor_to_tc, tc);
790 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
791 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
795 * Do a series of checks to determine if sensor should execute
797 * @return #GNUNET_YES / #GNUNET_NO
800 should_run_sensor(struct SensorInfo *sensorinfo)
802 struct GNUNET_TIME_Absolute now;
804 if(GNUNET_NO == sensorinfo->enabled)
806 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' is disabled, will not run\n", sensorinfo->name);
809 now = GNUNET_TIME_absolute_get();
810 if(NULL != sensorinfo->start_time
811 && now.abs_value_us < sensorinfo->start_time->abs_value_us)
813 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Start time for sensor `%s' not reached yet, will not run\n", sensorinfo->name);
816 if(NULL != sensorinfo->end_time
817 && now.abs_value_us >= sensorinfo->end_time->abs_value_us)
819 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' expired, disabling.\n", sensorinfo->name);
820 set_sensor_enabled(sensorinfo, GNUNET_NO);
827 * Callback function to process statistic values
829 * @param cls 'struct SensorInfo *'
830 * @param subsystem name of subsystem that created the statistic
831 * @param name the name of the datum
832 * @param value the current value
833 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
834 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
836 int sensor_statistics_iterator (void *cls,
837 const char *subsystem,
842 struct SensorInfo *sensorinfo = cls;
843 struct GNUNET_TIME_Absolute expiry;
845 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %" PRIu64 "\n", sensorinfo->name, value);
846 //FIXME: store first line, last line or all ??
847 expiry = GNUNET_TIME_relative_to_absolute(sensorinfo->interval);
848 GNUNET_PEERSTORE_store(peerstore,
855 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
862 * Continuation called after sensor gets all gnunet statistics values
864 * @param cls 'struct SensorInfo *'
865 * @param success #GNUNET_OK if statistics were
866 * successfully obtained, #GNUNET_SYSERR if not.
868 void end_sensor_run_stat (void *cls, int success)
870 struct SensorInfo *sensorinfo = cls;
872 sensorinfo->gnunet_stat_get_handle = NULL;
873 sensorinfo->running = GNUNET_NO;
877 * Callback for output of executed sensor process
879 * @param cls 'struct SensorInfo *'
880 * @param line line of output from a command, NULL for the end
882 void sensor_process_callback (void *cls, const char *line)
884 struct SensorInfo *sensorinfo = cls;
885 struct GNUNET_TIME_Absolute expiry;
887 if(NULL == line) //end of output
889 GNUNET_OS_command_stop(sensorinfo->ext_cmd);
890 sensorinfo->ext_cmd = NULL;
891 sensorinfo->running = GNUNET_NO;
894 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %s\n", sensorinfo->name, line);
895 //FIXME: store first line, last line or all ??
896 expiry = GNUNET_TIME_relative_to_absolute(sensorinfo->interval);
897 GNUNET_PEERSTORE_store(peerstore,
904 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
910 * Checks if the given file is a path
912 * @return #GNUNET_YES / #GNUNET_NO
915 is_path(char *filename)
920 filename_len = strlen(filename);
921 for(i = 0; i < filename_len; i++)
923 if(DIR_SEPARATOR == filename[i])
930 * Actual execution of a sensor
932 * @param cls 'struct SensorInfo'
936 sensor_run (void *cls,
937 const struct GNUNET_SCHEDULER_TaskContext * tc)
939 struct SensorInfo *sensorinfo = cls;
944 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
945 if(GNUNET_YES == sensorinfo->running) //FIXME: should we try to kill?
947 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sensor `%s' running for too long, will try again next interval\n", sensorinfo->name);
950 if(GNUNET_NO == should_run_sensor(sensorinfo))
952 sensorinfo->running = GNUNET_YES;
953 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting the execution of sensor `%s'\n", sensorinfo->name);
954 if(sources[0] == sensorinfo->source) //gnunet-statistics
956 sensorinfo->gnunet_stat_get_handle = GNUNET_STATISTICS_get(statistics,
957 sensorinfo->gnunet_stat_service,
958 sensorinfo->gnunet_stat_name,
959 sensorinfo->interval, //try to get values only for the interval of the sensor
960 &end_sensor_run_stat,
961 &sensor_statistics_iterator,
964 else if(sources[1] == sensorinfo->source)
966 if(GNUNET_YES == is_path(sensorinfo->ext_process))
968 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
969 "Sensor `%s': External process should not be a path, disabling sensor.\n",
971 set_sensor_enabled(sensorinfo, GNUNET_NO);
974 //check if the process exists in $PATH
975 process_path = GNUNET_strdup(sensorinfo->ext_process);
977 GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
978 if(GNUNET_SYSERR == check_result)
980 //search in sensor directory
981 sensors_dir = get_sensor_dir();
982 GNUNET_free(process_path);
983 GNUNET_asprintf(&process_path, "%s%s-files%s%s",
987 sensorinfo->ext_process);
988 GNUNET_free(sensors_dir);
990 GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
992 if(GNUNET_SYSERR == check_result)
994 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sensor `%s' process `%s' problem: binary doesn't exist or not executable\n",
996 sensorinfo->ext_process);
997 set_sensor_enabled(sensorinfo, GNUNET_NO);
998 sensorinfo->running = GNUNET_NO;
999 GNUNET_free(process_path);
1002 sensorinfo->ext_cmd = GNUNET_OS_command_run(&sensor_process_callback,
1004 GNUNET_TIME_UNIT_FOREVER_REL,
1006 sensorinfo->ext_process,
1007 sensorinfo->ext_args,
1009 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Process started for sensor `%s'\n", sensorinfo->name);
1010 GNUNET_free(process_path);
1014 sensorinfo->running = GNUNET_NO;
1015 GNUNET_break(0); //shouldn't happen
1020 * Starts the execution of a sensor
1023 * @param key hash of sensor name, key to hashmap (unused)
1024 * @param value a 'struct SensorInfo *'
1025 * @return #GNUNET_YES if we should continue to
1027 * #GNUNET_NO if not.
1029 int schedule_sensor(void *cls,
1030 const struct GNUNET_HashCode *key, void *value)
1032 struct SensorInfo *sensorinfo = value;
1034 if(GNUNET_NO == should_run_sensor(sensorinfo))
1036 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling sensor `%s' to run after %" PRIu64 " microseconds\n",
1037 sensorinfo->name, sensorinfo->interval.rel_value_us);
1038 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
1040 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sensor `%s' execution task already set, this should not happen\n", sensorinfo->name);
1043 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
1048 * Starts the execution of all enabled sensors
1052 schedule_all_sensors()
1054 GNUNET_CONTAINER_multihashmap_iterate(sensors, &schedule_sensor, NULL);
1058 * Process statistics requests.
1060 * @param cls closure
1061 * @param server the initialized server
1062 * @param c configuration to use
1066 struct GNUNET_SERVER_Handle *server,
1067 const struct GNUNET_CONFIGURATION_Handle *c)
1069 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1070 {&handle_get_sensor, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GET,
1072 {&handle_get_all_sensors, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GETALL,
1073 sizeof (struct GNUNET_MessageHeader)},
1078 sensors = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
1080 schedule_all_sensors();
1081 statistics = GNUNET_STATISTICS_create("sensor", cfg);
1082 GNUNET_CRYPTO_get_peer_identity(cfg, &peerid);
1083 peerstore = GNUNET_PEERSTORE_connect(cfg);
1084 GNUNET_SERVER_add_handlers (server, handlers);
1085 GNUNET_SERVER_disconnect_notify (server,
1086 &handle_client_disconnect,
1088 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1095 * The main function for the sensor service.
1097 * @param argc number of arguments from the command line
1098 * @param argv command line arguments
1099 * @return 0 ok, 1 on error
1102 main (int argc, char *const *argv)
1104 return (GNUNET_OK ==
1105 GNUNET_SERVICE_run (argc,
1108 GNUNET_SERVICE_OPTION_NONE,
1109 &run, NULL)) ? 0 : 1;
1112 /* end of gnunet-service-sensor.c */