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
102 * If not supplied, will default to the interval value
104 struct GNUNET_TIME_Relative lifetime;
107 * A set of required peer capabilities for the sensor to collect meaningful information (e.g. ipv6)
112 * Either "gnunet-statistics" or external "process"
117 * Name of the GNUnet service that is the source for the gnunet-statistics entry
119 char *gnunet_stat_service;
122 * Name of the gnunet-statistics entry
124 char *gnunet_stat_name;
127 * Handle to statistics get request (OR GNUNET_SCHEDULER_NO_TASK)
129 struct GNUNET_STATISTICS_GetHandle *gnunet_stat_get_handle;
132 * Name of the external process to be executed
137 * Arguments to be passed to the external process
142 * Handle to the external process
144 struct GNUNET_OS_CommandHandle *ext_cmd;
147 * Did we already receive a value
148 * from the currently running external
149 * proccess ? #GNUNET_YES / #GNUNET_NO
151 int ext_cmd_value_received;
154 * The output datatype to be expected
156 char *expected_datatype;
159 * Peer-identity of peer running collection point
161 struct GNUNET_PeerIdentity *collection_point;
164 * Time interval to send sensor information to collection point (e.g. every 30 mins)
166 struct GNUNET_TIME_Relative *collection_interval;
169 * Flag specifying if value is to be communicated to the p2p network
174 * Time interval to communicate value to the p2p network
176 struct GNUNET_TIME_Relative *p2p_interval;
179 * Execution task (OR GNUNET_SCHEDULER_NO_TASK)
181 GNUNET_SCHEDULER_TaskIdentifier execution_task;
184 * Is the sensor being executed
193 static const struct GNUNET_CONFIGURATION_Handle *cfg;
196 * Hashmap of loaded sensor definitions
198 struct GNUNET_CONTAINER_MultiHashMap *sensors;
201 * Supported sources of sensor information
203 static const char *sources[] = { "gnunet-statistics", "process", NULL };
206 * Supported datatypes of sensor information
208 static const char *datatypes[] = { "uint64", "double", "string", NULL };
211 * Handle to statistics service
213 struct GNUNET_STATISTICS_Handle *statistics;
216 * Handle to peerstore service
218 struct GNUNET_PEERSTORE_Handle *peerstore;
223 char *subsystem = "sensor";
228 struct GNUNET_PeerIdentity peerid;
231 * Remove sensor execution from scheduler
234 * @param key hash of sensor name, key to hashmap
235 * @param value a 'struct SensorInfo *'
236 * @return #GNUNET_YES if we should continue to
240 static int destroy_sensor(void *cls,
241 const struct GNUNET_HashCode *key, void *value)
243 struct SensorInfo *sensorinfo = value;
245 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Destroying sensor `%s'\n", sensorinfo->name);
246 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
248 GNUNET_SCHEDULER_cancel(sensorinfo->execution_task);
249 sensorinfo->execution_task = GNUNET_SCHEDULER_NO_TASK;
251 if(NULL != sensorinfo->gnunet_stat_get_handle)
253 GNUNET_STATISTICS_get_cancel(sensorinfo->gnunet_stat_get_handle);
254 sensorinfo->gnunet_stat_get_handle = NULL;
256 if(NULL != sensorinfo->ext_cmd)
258 GNUNET_OS_command_stop(sensorinfo->ext_cmd);
259 sensorinfo->ext_cmd = NULL;
261 if(NULL != sensorinfo->cfg)
262 GNUNET_CONFIGURATION_destroy(sensorinfo->cfg);
263 if(NULL != sensorinfo->name)
264 GNUNET_free(sensorinfo->name);
265 if(NULL != sensorinfo->def_file)
266 GNUNET_free(sensorinfo->def_file);
267 if(NULL != sensorinfo->description)
268 GNUNET_free(sensorinfo->description);
269 if(NULL != sensorinfo->category)
270 GNUNET_free(sensorinfo->category);
271 if(NULL != sensorinfo->capabilities)
272 GNUNET_free(sensorinfo->capabilities);
273 if(NULL != sensorinfo->gnunet_stat_service)
274 GNUNET_free(sensorinfo->gnunet_stat_service);
275 if(NULL != sensorinfo->gnunet_stat_name)
276 GNUNET_free(sensorinfo->gnunet_stat_name);
277 if(NULL != sensorinfo->ext_process)
278 GNUNET_free(sensorinfo->ext_process);
279 if(NULL != sensorinfo->ext_args)
280 GNUNET_free(sensorinfo->ext_args);
281 GNUNET_free(sensorinfo);
287 * Sensor will not run again unless
288 * explicitly enabled or reloaded
290 * @param sensor sensor information
292 static void set_sensor_enabled(struct SensorInfo *sensor, int state)
294 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
295 "Sensor `%s': Setting enabled to %d.\n",
296 sensor->name, state);
297 sensor->enabled = GNUNET_NO;
298 GNUNET_assert(NULL != sensor->cfg);
299 GNUNET_CONFIGURATION_set_value_string(sensor->cfg, sensor->name, "ENABLED",
300 (GNUNET_YES == state)?"YES":"NO");
301 GNUNET_CONFIGURATION_write(sensor->cfg, sensor->def_file);
305 * Task run during shutdown.
311 shutdown_task (void *cls,
312 const struct GNUNET_SCHEDULER_TaskContext *tc)
314 GNUNET_CONTAINER_multihashmap_iterate(sensors, &destroy_sensor, NULL);
315 GNUNET_CONTAINER_multihashmap_destroy(sensors);
316 if(NULL != statistics)
318 GNUNET_STATISTICS_destroy(statistics, GNUNET_YES);
321 if(NULL != peerstore)
323 GNUNET_PEERSTORE_disconnect(peerstore);
326 GNUNET_SCHEDULER_shutdown();
331 * A client disconnected. Remove all of its data structure entries.
333 * @param cls closure, NULL
334 * @param client identification of the client
337 handle_client_disconnect (void *cls,
338 struct GNUNET_SERVER_Client
344 * Parses a version number string into major and minor
346 * @param version full version string
347 * @param major pointer to parsed major value
348 * @param minor pointer to parsed minor value
349 * @return #GNUNET_OK if parsing went ok, #GNUNET_SYSERROR in case of error
352 version_parse(char *version, uint16_t *major, uint16_t *minor)
357 for(; isdigit(*version); version++)
360 majorval += *version - '0';
363 return GNUNET_SYSERR;
365 for(; isdigit(*version); version++)
368 minorval += *version - '0';
371 return GNUNET_SYSERR;
379 * Load sensor definition from configuration
381 * @param cfg configuration handle
382 * @param sectionname configuration section containing definition
384 static struct SensorInfo *
385 load_sensor_from_cfg(struct GNUNET_CONFIGURATION_Handle *cfg, const char *sectionname)
387 struct SensorInfo *sensor;
391 unsigned long long time_sec;
393 sensor = GNUNET_new(struct SensorInfo);
395 sensor->name = GNUNET_strdup(sectionname);
397 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "VERSION", &version_str))
399 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor version\n"));
403 if(GNUNET_OK != version_parse(version_str, &(sensor->version_major), &(sensor->version_minor)))
405 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Invalid sensor version number, format should be major.minor\n"));
407 GNUNET_free(version_str);
410 GNUNET_free(version_str);
412 GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "DESCRIPTION", &sensor->description);
414 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "CATEGORY", &sensor->category) ||
415 NULL == sensor->category)
417 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor category\n"));
422 if(GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno(cfg, sectionname, "ENABLED"))
423 sensor->enabled = GNUNET_NO;
425 sensor->enabled = GNUNET_YES;
427 sensor->start_time = NULL;
428 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "START_TIME", &starttime_str))
430 GNUNET_STRINGS_fancy_time_to_absolute(starttime_str, sensor->start_time);
431 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Start time loaded: `%s'. Parsed: %d\n", starttime_str, (NULL != sensor->start_time));
432 GNUNET_free(starttime_str);
435 sensor->end_time = NULL;
436 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "END_TIME", &endtime_str))
438 GNUNET_STRINGS_fancy_time_to_absolute(endtime_str, sensor->end_time);
439 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "End time loaded: `%s'. Parsed: %d\n", endtime_str, (NULL != sensor->end_time));
440 GNUNET_free(endtime_str);
443 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "INTERVAL", &time_sec))
445 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor run interval\n"));
449 if(time_sec < MIN_INTERVAL)
451 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Sensor run interval too low (%" PRIu64 " < %d)\n"),
452 time_sec, MIN_INTERVAL);
456 sensor->interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
458 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "LIFETIME", &time_sec))
460 sensor->lifetime = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
463 sensor->lifetime = sensor->interval;
466 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_choice(cfg, sectionname, "SOURCE", sources, (const char **)&sensor->source))
468 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor source\n"));
472 if(sources[0] == sensor->source) //gnunet-statistics
474 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "GNUNET_STAT_SERVICE", &sensor->gnunet_stat_service) ||
475 GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "GNUNET_STAT_NAME", &sensor->gnunet_stat_name))
477 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor gnunet-statistics source information\n"));
481 sensor->gnunet_stat_get_handle = NULL;
483 else if(sources[1] == sensor->source) //process
485 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "EXT_PROCESS", &sensor->ext_process))
487 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor process name\n"));
491 GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "EXT_ARGS", &sensor->ext_args);
494 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_choice(cfg, sectionname, "EXPECTED_DATATYPE", datatypes, (const char **)&sensor->expected_datatype))
496 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor expected datatype\n"));
500 if(sources[0] == sensor->source && datatypes[0] != sensor->expected_datatype)
502 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Invalid expected datatype, gnunet-statistics returns uint64 values\n"));
506 //TODO: reporting mechanism
508 sensor->execution_task = GNUNET_SCHEDULER_NO_TASK;
510 sensor->running = GNUNET_NO;
516 * Load sensor definition from file
518 * @param filename full path to file containing sensor definition
520 static struct SensorInfo *
521 load_sensor_from_file(const char *filename)
523 struct GNUNET_CONFIGURATION_Handle *sensorcfg;
524 const char *filebasename;
525 struct SensorInfo *sensor;
528 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
530 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to access sensor file: %s\n"), filename);
533 //load file as configuration
534 sensorcfg = GNUNET_CONFIGURATION_create();
535 if(GNUNET_SYSERR == GNUNET_CONFIGURATION_parse(sensorcfg, filename))
537 GNUNET_CONFIGURATION_destroy(sensorcfg);
538 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to load sensor definition: %s\n"), filename);
541 //configuration section should be the same as filename
542 filebasename = GNUNET_STRINGS_get_short_name(filename);
543 sensor = load_sensor_from_cfg(sensorcfg, filebasename);
546 GNUNET_CONFIGURATION_destroy(sensorcfg);
549 sensor->def_file = GNUNET_strdup(filename);
550 sensor->cfg = sensorcfg;
556 * Compares version numbers of two sensors
558 * @param s1 first sensor
559 * @param s2 second sensor
560 * @return 1: s1 > s2, 0: s1 == s2, -1: s1 < s2
563 sensor_version_compare(struct SensorInfo *s1, struct SensorInfo *s2)
565 if(s1->version_major == s2->version_major)
566 return (s1->version_minor < s2->version_minor) ? -1 : (s1->version_minor > s2->version_minor);
568 return (s1->version_major < s2->version_major) ? -1 : (s1->version_major > s2->version_major);
572 * Adds a new sensor to given hashmap.
573 * If the same name exist, compares versions and update if old.
575 * @param sensor Sensor structure to add
576 * @param map Hashmap to add to
577 * @return #GNUNET_YES if added, #GNUNET_NO if not added which is not necessarily an error
580 add_sensor_to_hashmap(struct SensorInfo *sensor, struct GNUNET_CONTAINER_MultiHashMap *map)
582 struct GNUNET_HashCode key;
583 struct SensorInfo *existing;
585 GNUNET_CRYPTO_hash(sensor->name, strlen(sensor->name), &key);
586 existing = GNUNET_CONTAINER_multihashmap_get(map, &key);
587 if(NULL != existing) //sensor with same name already exists
589 if(sensor_version_compare(existing, sensor) >= 0) //same or newer version already exist
591 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Sensor `%s' already exists with same or newer version\n"), sensor->name);
596 GNUNET_CONTAINER_multihashmap_remove(map, &key, existing); //remove the old version
597 GNUNET_free(existing);
598 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Upgrading sensor `%s' to a newer version\n", sensor->name);
601 if(GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put(map, &key, sensor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
603 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error adding new sensor `%s' to global hashmap, this should not happen\n"), sensor->name);
611 * Iterating over files in sensors directory
614 * @param filename complete filename (absolute path)
615 * @return #GNUNET_OK to continue to iterate
618 reload_sensors_dir_cb(void *cls, const char *filename)
620 struct SensorInfo *sensor;
622 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
624 sensor = load_sensor_from_file(filename);
627 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error loading sensor from file: %s\n"), filename);
630 if(GNUNET_YES == add_sensor_to_hashmap(sensor, sensors))
631 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, _("Sensor `%s' added to global hashmap\n"), sensor->name);
633 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, ("Could not add sensor `%s' to global hashmap\n"), sensor->name);
639 * Get path to the directory containing the sensor definition files
641 * @return sensor files directory
649 datadir = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_DATADIR);
650 GNUNET_asprintf(&sensordir, "%ssensors%s",
651 datadir, DIR_SEPARATOR_STR);
652 GNUNET_free(datadir);
658 * Reads sensor definitions from data files
666 sensordir = get_sensor_dir();
667 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Reloading sensor definitions from directory `%s'\n"), sensordir);
668 GNUNET_assert(GNUNET_YES == GNUNET_DISK_directory_test(sensordir, GNUNET_YES));
670 //read all files in sensors directory
671 GNUNET_DISK_directory_scan(sensordir, &reload_sensors_dir_cb, NULL);
672 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Loaded %d sensors from directory `%s'\n"),
673 GNUNET_CONTAINER_multihashmap_size(sensors), sensordir);
674 GNUNET_free(sensordir);
678 * Creates a structure with basic sensor info to be sent to a client
680 * @param sensor sensor information
681 * @return message ready to be sent to client
683 static struct SensorInfoMessage *
684 create_sensor_info_msg(struct SensorInfo *sensor)
686 struct SensorInfoMessage *msg;
692 name_len = strlen(sensor->name);
693 if(NULL == sensor->description)
696 desc_len = strlen(sensor->description) + 1;
698 len += sizeof(struct SensorInfoMessage);
701 msg = GNUNET_malloc(len);
702 msg->header.size = htons(len);
703 msg->header.type = htons(GNUNET_MESSAGE_TYPE_SENSOR_INFO);
704 msg->name_len = htons(name_len);
705 msg->description_len = htons(desc_len);
706 msg->version_major = htons(sensor->version_major);
707 msg->version_minor = htons(sensor->version_minor);
708 str_ptr = (char*) &msg[1];
709 memcpy(str_ptr, sensor->name, name_len);
710 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor name (%d): %.*s\n",
711 name_len, name_len, str_ptr);
713 memcpy(str_ptr, sensor->description, desc_len);
714 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor description (%d): %.*s\n",
715 desc_len, desc_len, str_ptr);
721 * Handle GET SENSOR message.
724 * @param client identification of the client
725 * @param message the actual message
728 handle_get_sensor (void *cls, struct GNUNET_SERVER_Client *client,
729 const struct GNUNET_MessageHeader *message)
731 struct GNUNET_SERVER_TransmitContext *tc;
733 size_t sensorname_len;
734 struct GNUNET_HashCode key;
735 struct SensorInfo *sensorinfo;
736 struct SensorInfoMessage *msg;
738 sensorname = (char *)&message[1];
739 sensorname_len = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
740 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received for sensor (%d) `%.*s'\n",
741 "GET SENSOR", sensorname_len, sensorname_len, sensorname);
742 tc = GNUNET_SERVER_transmit_context_create (client);
743 GNUNET_CRYPTO_hash(sensorname, sensorname_len, &key);
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created key hash for requested sensor\n");
745 sensorinfo = (struct SensorInfo *)GNUNET_CONTAINER_multihashmap_get(sensors, &key);
746 if(NULL != sensorinfo)
748 msg = create_sensor_info_msg(sensorinfo);
749 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
753 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Requested sensor `%.*s' was not found\n",
754 sensorname_len, sensorname);
755 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
756 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
760 * Iterator for sensors and adds them to transmit context
762 * @param cls a 'struct GNUNET_SERVER_TransmitContext *'
763 * @param key hash of sensor name, key to hashmap
764 * @param value a 'struct SensorInfo *'
766 int add_sensor_to_tc(void *cls,
767 const struct GNUNET_HashCode *key, void *value)
769 struct GNUNET_SERVER_TransmitContext *tc = cls;
770 struct SensorInfo *sensorinfo = value;
771 struct SensorInfoMessage *msg;
773 msg = create_sensor_info_msg(sensorinfo);
774 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
782 * Handle GET ALL SENSORS message.
785 * @param client identification of the client
786 * @param message the actual message
789 handle_get_all_sensors (void *cls, struct GNUNET_SERVER_Client *client,
790 const struct GNUNET_MessageHeader *message)
792 struct GNUNET_SERVER_TransmitContext *tc;
794 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received.\n",
796 tc = GNUNET_SERVER_transmit_context_create (client);
797 GNUNET_CONTAINER_multihashmap_iterate(sensors, &add_sensor_to_tc, tc);
798 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
799 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
803 * Do a series of checks to determine if sensor should execute
805 * @return #GNUNET_YES / #GNUNET_NO
808 should_run_sensor(struct SensorInfo *sensorinfo)
810 struct GNUNET_TIME_Absolute now;
812 if(GNUNET_NO == sensorinfo->enabled)
814 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' is disabled, will not run\n", sensorinfo->name);
817 now = GNUNET_TIME_absolute_get();
818 if(NULL != sensorinfo->start_time
819 && now.abs_value_us < sensorinfo->start_time->abs_value_us)
821 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Start time for sensor `%s' not reached yet, will not run\n", sensorinfo->name);
824 if(NULL != sensorinfo->end_time
825 && now.abs_value_us >= sensorinfo->end_time->abs_value_us)
827 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' expired, disabling.\n", sensorinfo->name);
828 set_sensor_enabled(sensorinfo, GNUNET_NO);
835 * Callback function to process statistic values
837 * @param cls 'struct SensorInfo *'
838 * @param subsystem name of subsystem that created the statistic
839 * @param name the name of the datum
840 * @param value the current value
841 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
842 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
844 int sensor_statistics_iterator (void *cls,
850 struct SensorInfo *sensorinfo = cls;
851 struct GNUNET_TIME_Absolute expiry;
853 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %" PRIu64 "\n", sensorinfo->name, value);
854 expiry = GNUNET_TIME_relative_to_absolute(sensorinfo->lifetime);
855 GNUNET_PEERSTORE_store(peerstore,
862 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
865 return GNUNET_SYSERR; /* We only want one value */
869 * Continuation called after sensor gets all gnunet statistics values
871 * @param cls 'struct SensorInfo *'
872 * @param success #GNUNET_OK if statistics were
873 * successfully obtained, #GNUNET_SYSERR if not.
875 void end_sensor_run_stat (void *cls, int success)
877 struct SensorInfo *sensorinfo = cls;
879 sensorinfo->gnunet_stat_get_handle = NULL;
880 sensorinfo->running = GNUNET_NO;
884 * Tries to parse a received sensor value to its
887 * @param value the string value received, should be null terminated
888 * @param sensor sensor information struct
889 * @param ret pointer to parsed value
890 * @return size of new parsed value, 0 for error
893 parse_sensor_value (const char *value, struct SensorInfo* sensor, void **ret)
902 //"uint64", "double", "string"
903 if (0 == strcmp("uint64", sensor->expected_datatype))
905 ullval = GNUNET_new(uint64_t);
906 *ullval = strtoull(value, &endptr, 10);
907 if ('\0' != *endptr &&
908 '\n' != *endptr) /* Invalid string */
911 return sizeof(uint64_t);
913 if(0 == strcmp("double", sensor->expected_datatype))
915 dval = GNUNET_new(double);
916 *dval = strtod(value, &endptr);
920 return sizeof(double);
922 if(0 == strcmp("string", sensor->expected_datatype))
924 *ret = GNUNET_strdup(value);
925 return strlen(value) + 1;
927 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
928 _("Unknown value type expected by sensor, this should not happen.\n"));
933 * Callback for output of executed sensor process
935 * @param cls 'struct SensorInfo *'
936 * @param line line of output from a command, NULL for the end
938 void sensor_process_callback (void *cls, const char *line)
940 struct SensorInfo *sensorinfo = cls;
943 struct GNUNET_TIME_Absolute expiry;
947 GNUNET_OS_command_stop(sensorinfo->ext_cmd);
948 sensorinfo->ext_cmd = NULL;
949 sensorinfo->running = GNUNET_NO;
950 sensorinfo->ext_cmd_value_received = GNUNET_NO;
953 if(GNUNET_YES == sensorinfo->ext_cmd_value_received)
954 return; /* We only want one *valid* value */
955 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %s\n", sensorinfo->name, line);
956 valsize = parse_sensor_value(line, sensorinfo, &value);
957 if (valsize == 0) /* invalid value, FIXME: should we disable the sensor now? */
959 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
960 _("Received an invalid value for sensor `%s': %s\n"),
961 sensorinfo->name, line);
965 sensorinfo->ext_cmd_value_received = GNUNET_YES;
966 expiry = GNUNET_TIME_relative_to_absolute(sensorinfo->lifetime);
967 GNUNET_PEERSTORE_store(peerstore,
974 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
981 * Checks if the given file is a path
983 * @return #GNUNET_YES / #GNUNET_NO
986 is_path(char *filename)
991 filename_len = strlen(filename);
992 for(i = 0; i < filename_len; i++)
994 if(DIR_SEPARATOR == filename[i])
1001 * Actual execution of a sensor
1003 * @param cls 'struct SensorInfo'
1007 sensor_run (void *cls,
1008 const struct GNUNET_SCHEDULER_TaskContext * tc)
1010 struct SensorInfo *sensorinfo = cls;
1015 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
1016 if(GNUNET_YES == sensorinfo->running) //FIXME: should we try to kill?
1018 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sensor `%s' running for too long, will try again next interval\n", sensorinfo->name);
1021 if(GNUNET_NO == should_run_sensor(sensorinfo))
1023 sensorinfo->running = GNUNET_YES;
1024 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting the execution of sensor `%s'\n", sensorinfo->name);
1025 if(sources[0] == sensorinfo->source) //gnunet-statistics
1027 sensorinfo->gnunet_stat_get_handle = GNUNET_STATISTICS_get(statistics,
1028 sensorinfo->gnunet_stat_service,
1029 sensorinfo->gnunet_stat_name,
1030 sensorinfo->interval, //try to get values only for the interval of the sensor
1031 &end_sensor_run_stat,
1032 &sensor_statistics_iterator,
1035 else if(sources[1] == sensorinfo->source)
1037 if(GNUNET_YES == is_path(sensorinfo->ext_process))
1039 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1040 _("Sensor `%s': External process should not be a path, disabling sensor.\n"),
1042 set_sensor_enabled(sensorinfo, GNUNET_NO);
1045 //check if the process exists in $PATH
1046 process_path = GNUNET_strdup(sensorinfo->ext_process);
1048 GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
1049 if(GNUNET_SYSERR == check_result)
1051 //search in sensor directory
1052 sensors_dir = get_sensor_dir();
1053 GNUNET_free(process_path);
1054 GNUNET_asprintf(&process_path, "%s%s-files%s%s",
1058 sensorinfo->ext_process);
1059 GNUNET_free(sensors_dir);
1061 GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
1063 if(GNUNET_SYSERR == check_result)
1065 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1066 _("Sensor `%s' process `%s' problem: binary doesn't exist or not executable\n"),
1068 sensorinfo->ext_process);
1069 set_sensor_enabled(sensorinfo, GNUNET_NO);
1070 sensorinfo->running = GNUNET_NO;
1071 GNUNET_free(process_path);
1074 sensorinfo->ext_cmd_value_received = GNUNET_NO;
1075 sensorinfo->ext_cmd = GNUNET_OS_command_run(&sensor_process_callback,
1077 GNUNET_TIME_UNIT_FOREVER_REL,
1079 sensorinfo->ext_process,
1080 sensorinfo->ext_args,
1082 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Process started for sensor `%s'\n", sensorinfo->name);
1083 GNUNET_free(process_path);
1087 sensorinfo->running = GNUNET_NO;
1088 GNUNET_break(0); //shouldn't happen
1093 * Starts the execution of a sensor
1096 * @param key hash of sensor name, key to hashmap (unused)
1097 * @param value a 'struct SensorInfo *'
1098 * @return #GNUNET_YES if we should continue to
1100 * #GNUNET_NO if not.
1102 int schedule_sensor(void *cls,
1103 const struct GNUNET_HashCode *key, void *value)
1105 struct SensorInfo *sensorinfo = value;
1107 if(GNUNET_NO == should_run_sensor(sensorinfo))
1109 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling sensor `%s' to run after %" PRIu64 " microseconds\n",
1110 sensorinfo->name, sensorinfo->interval.rel_value_us);
1111 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
1113 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1114 _("Sensor `%s' execution task already set, this should not happen\n"), sensorinfo->name);
1117 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
1122 * Starts the execution of all enabled sensors
1126 schedule_all_sensors()
1128 GNUNET_CONTAINER_multihashmap_iterate(sensors, &schedule_sensor, NULL);
1132 * Process statistics requests.
1134 * @param cls closure
1135 * @param server the initialized server
1136 * @param c configuration to use
1140 struct GNUNET_SERVER_Handle *server,
1141 const struct GNUNET_CONFIGURATION_Handle *c)
1143 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1144 {&handle_get_sensor, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GET,
1146 {&handle_get_all_sensors, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GETALL,
1147 sizeof (struct GNUNET_MessageHeader)},
1152 sensors = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
1154 schedule_all_sensors();
1155 statistics = GNUNET_STATISTICS_create("sensor", cfg);
1156 GNUNET_CRYPTO_get_peer_identity(cfg, &peerid);
1157 peerstore = GNUNET_PEERSTORE_connect(cfg);
1158 GNUNET_SERVER_add_handlers (server, handlers);
1159 GNUNET_SERVER_disconnect_notify (server,
1160 &handle_client_disconnect,
1162 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1169 * The main function for the sensor service.
1171 * @param argc number of arguments from the command line
1172 * @param argv command line arguments
1173 * @return 0 ok, 1 on error
1176 main (int argc, char *const *argv)
1178 return (GNUNET_OK ==
1179 GNUNET_SERVICE_run (argc,
1182 GNUNET_SERVICE_OPTION_NONE,
1183 &run, NULL)) ? 0 : 1;
1186 /* end of gnunet-service-sensor.c */