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
41 static const struct GNUNET_CONFIGURATION_Handle *cfg;
44 * Hashmap of loaded sensor definitions
46 static struct GNUNET_CONTAINER_MultiHashMap *sensors;
49 * Supported sources of sensor information
51 static const char *sources[] = { "gnunet-statistics", "process", NULL };
54 * Supported datatypes of sensor information
56 static const char *datatypes[] = { "uint64", "double", "string", NULL };
59 * Handle to statistics service
61 struct GNUNET_STATISTICS_Handle *statistics;
64 * Handle to peerstore service
66 struct GNUNET_PEERSTORE_Handle *peerstore;
71 char *subsystem = "sensor";
76 struct GNUNET_PeerIdentity peerid;
79 * Remove sensor execution from scheduler
82 * @param key hash of sensor name, key to hashmap
83 * @param value a 'struct SensorInfo *'
84 * @return #GNUNET_YES if we should continue to
88 static int destroy_sensor(void *cls,
89 const struct GNUNET_HashCode *key, void *value)
91 struct SensorInfo *sensorinfo = value;
93 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Destroying sensor `%s'\n", sensorinfo->name);
94 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
96 GNUNET_SCHEDULER_cancel(sensorinfo->execution_task);
97 sensorinfo->execution_task = GNUNET_SCHEDULER_NO_TASK;
99 if(NULL != sensorinfo->gnunet_stat_get_handle)
101 GNUNET_STATISTICS_get_cancel(sensorinfo->gnunet_stat_get_handle);
102 sensorinfo->gnunet_stat_get_handle = NULL;
104 if(NULL != sensorinfo->ext_cmd)
106 GNUNET_OS_command_stop(sensorinfo->ext_cmd);
107 sensorinfo->ext_cmd = NULL;
109 if(NULL != sensorinfo->cfg)
110 GNUNET_CONFIGURATION_destroy(sensorinfo->cfg);
111 if(NULL != sensorinfo->name)
112 GNUNET_free(sensorinfo->name);
113 if(NULL != sensorinfo->def_file)
114 GNUNET_free(sensorinfo->def_file);
115 if(NULL != sensorinfo->description)
116 GNUNET_free(sensorinfo->description);
117 if(NULL != sensorinfo->category)
118 GNUNET_free(sensorinfo->category);
119 if(NULL != sensorinfo->capabilities)
120 GNUNET_free(sensorinfo->capabilities);
121 if(NULL != sensorinfo->gnunet_stat_service)
122 GNUNET_free(sensorinfo->gnunet_stat_service);
123 if(NULL != sensorinfo->gnunet_stat_name)
124 GNUNET_free(sensorinfo->gnunet_stat_name);
125 if(NULL != sensorinfo->ext_process)
126 GNUNET_free(sensorinfo->ext_process);
127 if(NULL != sensorinfo->ext_args)
128 GNUNET_free(sensorinfo->ext_args);
129 GNUNET_free(sensorinfo);
135 * Sensor will not run again unless
136 * explicitly enabled or reloaded
138 * @param sensor sensor information
140 static void set_sensor_enabled(struct SensorInfo *sensor, int state)
142 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
143 "Sensor `%s': Setting enabled to %d.\n",
144 sensor->name, state);
145 sensor->enabled = GNUNET_NO;
146 GNUNET_assert(NULL != sensor->cfg);
147 GNUNET_CONFIGURATION_set_value_string(sensor->cfg, sensor->name, "ENABLED",
148 (GNUNET_YES == state)?"YES":"NO");
149 GNUNET_CONFIGURATION_write(sensor->cfg, sensor->def_file);
153 * Task run during shutdown.
159 shutdown_task (void *cls,
160 const struct GNUNET_SCHEDULER_TaskContext *tc)
162 SENSOR_analysis_stop();
163 GNUNET_CONTAINER_multihashmap_iterate(sensors, &destroy_sensor, NULL);
164 GNUNET_CONTAINER_multihashmap_destroy(sensors);
165 if(NULL != statistics)
167 GNUNET_STATISTICS_destroy(statistics, GNUNET_YES);
170 if(NULL != peerstore)
172 GNUNET_PEERSTORE_disconnect(peerstore);
175 GNUNET_SCHEDULER_shutdown();
180 * A client disconnected. Remove all of its data structure entries.
182 * @param cls closure, NULL
183 * @param client identification of the client
186 handle_client_disconnect (void *cls,
187 struct GNUNET_SERVER_Client
193 * Parses a version number string into major and minor
195 * @param version full version string
196 * @param major pointer to parsed major value
197 * @param minor pointer to parsed minor value
198 * @return #GNUNET_OK if parsing went ok, #GNUNET_SYSERROR in case of error
201 version_parse(char *version, uint16_t *major, uint16_t *minor)
206 for(; isdigit(*version); version++)
209 majorval += *version - '0';
212 return GNUNET_SYSERR;
214 for(; isdigit(*version); version++)
217 minorval += *version - '0';
220 return GNUNET_SYSERR;
228 * Load sensor definition from configuration
230 * @param cfg configuration handle
231 * @param sectionname configuration section containing definition
233 static struct SensorInfo *
234 load_sensor_from_cfg(struct GNUNET_CONFIGURATION_Handle *cfg, const char *sectionname)
236 struct SensorInfo *sensor;
240 unsigned long long time_sec;
242 sensor = GNUNET_new(struct SensorInfo);
244 sensor->name = GNUNET_strdup(sectionname);
246 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "VERSION", &version_str))
248 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor version\n"));
252 if(GNUNET_OK != version_parse(version_str, &(sensor->version_major), &(sensor->version_minor)))
254 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Invalid sensor version number, format should be major.minor\n"));
256 GNUNET_free(version_str);
259 GNUNET_free(version_str);
261 GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "DESCRIPTION", &sensor->description);
263 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "CATEGORY", &sensor->category) ||
264 NULL == sensor->category)
266 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor category\n"));
271 if(GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno(cfg, sectionname, "ENABLED"))
272 sensor->enabled = GNUNET_NO;
274 sensor->enabled = GNUNET_YES;
276 sensor->start_time = NULL;
277 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "START_TIME", &starttime_str))
279 GNUNET_STRINGS_fancy_time_to_absolute(starttime_str, sensor->start_time);
280 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Start time loaded: `%s'. Parsed: %d\n", starttime_str, (NULL != sensor->start_time));
281 GNUNET_free(starttime_str);
284 sensor->end_time = NULL;
285 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "END_TIME", &endtime_str))
287 GNUNET_STRINGS_fancy_time_to_absolute(endtime_str, sensor->end_time);
288 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "End time loaded: `%s'. Parsed: %d\n", endtime_str, (NULL != sensor->end_time));
289 GNUNET_free(endtime_str);
292 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "INTERVAL", &time_sec))
294 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor run interval\n"));
298 if(time_sec < MIN_INTERVAL)
300 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Sensor run interval too low (%" PRIu64 " < %d)\n"),
301 time_sec, MIN_INTERVAL);
305 sensor->interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
307 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "LIFETIME", &time_sec))
309 sensor->lifetime = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
312 sensor->lifetime = sensor->interval;
315 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_choice(cfg, sectionname, "SOURCE", sources, (const char **)&sensor->source))
317 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor source\n"));
321 if(sources[0] == sensor->source) //gnunet-statistics
323 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "GNUNET_STAT_SERVICE", &sensor->gnunet_stat_service) ||
324 GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "GNUNET_STAT_NAME", &sensor->gnunet_stat_name))
326 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor gnunet-statistics source information\n"));
330 sensor->gnunet_stat_get_handle = NULL;
332 else if(sources[1] == sensor->source) //process
334 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "EXT_PROCESS", &sensor->ext_process))
336 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor process name\n"));
340 GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "EXT_ARGS", &sensor->ext_args);
343 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_choice(cfg, sectionname, "EXPECTED_DATATYPE", datatypes, (const char **)&sensor->expected_datatype))
345 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor expected datatype\n"));
349 if(sources[0] == sensor->source && datatypes[0] != sensor->expected_datatype)
351 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Invalid expected datatype, gnunet-statistics returns uint64 values\n"));
355 //TODO: reporting mechanism
357 sensor->execution_task = GNUNET_SCHEDULER_NO_TASK;
359 sensor->running = GNUNET_NO;
365 * Load sensor definition from file
367 * @param filename full path to file containing sensor definition
369 static struct SensorInfo *
370 load_sensor_from_file(const char *filename)
372 struct GNUNET_CONFIGURATION_Handle *sensorcfg;
373 const char *filebasename;
374 struct SensorInfo *sensor;
377 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
379 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to access sensor file: %s\n"), filename);
382 //load file as configuration
383 sensorcfg = GNUNET_CONFIGURATION_create();
384 if(GNUNET_SYSERR == GNUNET_CONFIGURATION_parse(sensorcfg, filename))
386 GNUNET_CONFIGURATION_destroy(sensorcfg);
387 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to load sensor definition: %s\n"), filename);
390 //configuration section should be the same as filename
391 filebasename = GNUNET_STRINGS_get_short_name(filename);
392 sensor = load_sensor_from_cfg(sensorcfg, filebasename);
395 GNUNET_CONFIGURATION_destroy(sensorcfg);
398 sensor->def_file = GNUNET_strdup(filename);
399 sensor->cfg = sensorcfg;
405 * Compares version numbers of two sensors
407 * @param s1 first sensor
408 * @param s2 second sensor
409 * @return 1: s1 > s2, 0: s1 == s2, -1: s1 < s2
412 sensor_version_compare(struct SensorInfo *s1, struct SensorInfo *s2)
414 if(s1->version_major == s2->version_major)
415 return (s1->version_minor < s2->version_minor) ? -1 : (s1->version_minor > s2->version_minor);
417 return (s1->version_major < s2->version_major) ? -1 : (s1->version_major > s2->version_major);
421 * Adds a new sensor to given hashmap.
422 * If the same name exist, compares versions and update if old.
424 * @param sensor Sensor structure to add
425 * @param map Hashmap to add to
426 * @return #GNUNET_YES if added, #GNUNET_NO if not added which is not necessarily an error
429 add_sensor_to_hashmap(struct SensorInfo *sensor, struct GNUNET_CONTAINER_MultiHashMap *map)
431 struct GNUNET_HashCode key;
432 struct SensorInfo *existing;
434 GNUNET_CRYPTO_hash(sensor->name, strlen(sensor->name), &key);
435 existing = GNUNET_CONTAINER_multihashmap_get(map, &key);
436 if(NULL != existing) //sensor with same name already exists
438 if(sensor_version_compare(existing, sensor) >= 0) //same or newer version already exist
440 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Sensor `%s' already exists with same or newer version\n"), sensor->name);
445 GNUNET_CONTAINER_multihashmap_remove(map, &key, existing); //remove the old version
446 GNUNET_free(existing);
447 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Upgrading sensor `%s' to a newer version\n", sensor->name);
450 if(GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put(map, &key, sensor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
452 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error adding new sensor `%s' to global hashmap, this should not happen\n"), sensor->name);
460 * Iterating over files in sensors directory
463 * @param filename complete filename (absolute path)
464 * @return #GNUNET_OK to continue to iterate
467 reload_sensors_dir_cb(void *cls, const char *filename)
469 struct SensorInfo *sensor;
471 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
473 sensor = load_sensor_from_file(filename);
476 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error loading sensor from file: %s\n"), filename);
479 if(GNUNET_YES == add_sensor_to_hashmap(sensor, sensors))
480 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, _("Sensor `%s' added to global hashmap\n"), sensor->name);
482 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, ("Could not add sensor `%s' to global hashmap\n"), sensor->name);
488 * Get path to the directory containing the sensor definition files
490 * @return sensor files directory
498 datadir = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_DATADIR);
499 GNUNET_asprintf(&sensordir, "%ssensors%s",
500 datadir, DIR_SEPARATOR_STR);
501 GNUNET_free(datadir);
507 * Reads sensor definitions from data files
515 sensordir = get_sensor_dir();
516 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Reloading sensor definitions from directory `%s'\n"), sensordir);
517 GNUNET_assert(GNUNET_YES == GNUNET_DISK_directory_test(sensordir, GNUNET_YES));
519 //read all files in sensors directory
520 GNUNET_DISK_directory_scan(sensordir, &reload_sensors_dir_cb, NULL);
521 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Loaded %d sensors from directory `%s'\n"),
522 GNUNET_CONTAINER_multihashmap_size(sensors), sensordir);
523 GNUNET_free(sensordir);
527 * Creates a structure with basic sensor info to be sent to a client
529 * @param sensor sensor information
530 * @return message ready to be sent to client
532 static struct SensorInfoMessage *
533 create_sensor_info_msg(struct SensorInfo *sensor)
535 struct SensorInfoMessage *msg;
541 name_len = strlen(sensor->name);
542 if(NULL == sensor->description)
545 desc_len = strlen(sensor->description) + 1;
547 len += sizeof(struct SensorInfoMessage);
550 msg = GNUNET_malloc(len);
551 msg->header.size = htons(len);
552 msg->header.type = htons(GNUNET_MESSAGE_TYPE_SENSOR_INFO);
553 msg->name_len = htons(name_len);
554 msg->description_len = htons(desc_len);
555 msg->version_major = htons(sensor->version_major);
556 msg->version_minor = htons(sensor->version_minor);
557 str_ptr = (char*) &msg[1];
558 memcpy(str_ptr, sensor->name, name_len);
559 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor name (%d): %.*s\n",
560 name_len, name_len, str_ptr);
562 memcpy(str_ptr, sensor->description, desc_len);
563 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor description (%d): %.*s\n",
564 desc_len, desc_len, str_ptr);
570 * Handle GET SENSOR message.
573 * @param client identification of the client
574 * @param message the actual message
577 handle_get_sensor (void *cls, struct GNUNET_SERVER_Client *client,
578 const struct GNUNET_MessageHeader *message)
580 struct GNUNET_SERVER_TransmitContext *tc;
582 size_t sensorname_len;
583 struct GNUNET_HashCode key;
584 struct SensorInfo *sensorinfo;
585 struct SensorInfoMessage *msg;
587 sensorname = (char *)&message[1];
588 sensorname_len = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
589 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received for sensor (%d) `%.*s'\n",
590 "GET SENSOR", sensorname_len, sensorname_len, sensorname);
591 tc = GNUNET_SERVER_transmit_context_create (client);
592 GNUNET_CRYPTO_hash(sensorname, sensorname_len, &key);
593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created key hash for requested sensor\n");
594 sensorinfo = (struct SensorInfo *)GNUNET_CONTAINER_multihashmap_get(sensors, &key);
595 if(NULL != sensorinfo)
597 msg = create_sensor_info_msg(sensorinfo);
598 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
602 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Requested sensor `%.*s' was not found\n",
603 sensorname_len, sensorname);
604 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
605 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
609 * Iterator for sensors and adds them to transmit context
611 * @param cls a 'struct GNUNET_SERVER_TransmitContext *'
612 * @param key hash of sensor name, key to hashmap
613 * @param value a 'struct SensorInfo *'
615 int add_sensor_to_tc(void *cls,
616 const struct GNUNET_HashCode *key, void *value)
618 struct GNUNET_SERVER_TransmitContext *tc = cls;
619 struct SensorInfo *sensorinfo = value;
620 struct SensorInfoMessage *msg;
622 msg = create_sensor_info_msg(sensorinfo);
623 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
631 * Handle GET ALL SENSORS message.
634 * @param client identification of the client
635 * @param message the actual message
638 handle_get_all_sensors (void *cls, struct GNUNET_SERVER_Client *client,
639 const struct GNUNET_MessageHeader *message)
641 struct GNUNET_SERVER_TransmitContext *tc;
643 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received.\n",
645 tc = GNUNET_SERVER_transmit_context_create (client);
646 GNUNET_CONTAINER_multihashmap_iterate(sensors, &add_sensor_to_tc, tc);
647 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
648 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
652 * Do a series of checks to determine if sensor should execute
654 * @return #GNUNET_YES / #GNUNET_NO
657 should_run_sensor(struct SensorInfo *sensorinfo)
659 struct GNUNET_TIME_Absolute now;
661 if(GNUNET_NO == sensorinfo->enabled)
663 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' is disabled, will not run\n", sensorinfo->name);
666 now = GNUNET_TIME_absolute_get();
667 if(NULL != sensorinfo->start_time
668 && now.abs_value_us < sensorinfo->start_time->abs_value_us)
670 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Start time for sensor `%s' not reached yet, will not run\n", sensorinfo->name);
673 if(NULL != sensorinfo->end_time
674 && now.abs_value_us >= sensorinfo->end_time->abs_value_us)
676 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' expired, disabling.\n", sensorinfo->name);
677 set_sensor_enabled(sensorinfo, GNUNET_NO);
684 * Callback function to process statistic values
686 * @param cls 'struct SensorInfo *'
687 * @param subsystem name of subsystem that created the statistic
688 * @param name the name of the datum
689 * @param value the current value
690 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
691 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
693 int sensor_statistics_iterator (void *cls,
699 struct SensorInfo *sensorinfo = cls;
700 struct GNUNET_TIME_Absolute expiry;
702 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %" PRIu64 "\n", sensorinfo->name, value);
703 expiry = GNUNET_TIME_relative_to_absolute(sensorinfo->lifetime);
704 GNUNET_PEERSTORE_store(peerstore,
711 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
714 return GNUNET_SYSERR; /* We only want one value */
718 * Continuation called after sensor gets all gnunet statistics values
720 * @param cls 'struct SensorInfo *'
721 * @param success #GNUNET_OK if statistics were
722 * successfully obtained, #GNUNET_SYSERR if not.
724 void end_sensor_run_stat (void *cls, int success)
726 struct SensorInfo *sensorinfo = cls;
728 sensorinfo->gnunet_stat_get_handle = NULL;
729 sensorinfo->running = GNUNET_NO;
733 * Tries to parse a received sensor value to its
736 * @param value the string value received, should be null terminated
737 * @param sensor sensor information struct
738 * @param ret pointer to parsed value
739 * @return size of new parsed value, 0 for error
742 parse_sensor_value (const char *value, struct SensorInfo* sensor, void **ret)
751 //"uint64", "double", "string"
752 if (0 == strcmp("uint64", sensor->expected_datatype))
754 ullval = GNUNET_new(uint64_t);
755 *ullval = strtoull(value, &endptr, 10);
756 if ('\0' != *endptr &&
757 '\n' != *endptr) /* Invalid string */
760 return sizeof(uint64_t);
762 if(0 == strcmp("double", sensor->expected_datatype))
764 dval = GNUNET_new(double);
765 *dval = strtod(value, &endptr);
769 return sizeof(double);
771 if(0 == strcmp("string", sensor->expected_datatype))
773 *ret = GNUNET_strdup(value);
774 return strlen(value) + 1;
776 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
777 _("Unknown value type expected by sensor, this should not happen.\n"));
782 * Callback for output of executed sensor process
784 * @param cls 'struct SensorInfo *'
785 * @param line line of output from a command, NULL for the end
787 void sensor_process_callback (void *cls, const char *line)
789 struct SensorInfo *sensorinfo = cls;
792 struct GNUNET_TIME_Absolute expiry;
796 GNUNET_OS_command_stop(sensorinfo->ext_cmd);
797 sensorinfo->ext_cmd = NULL;
798 sensorinfo->running = GNUNET_NO;
799 sensorinfo->ext_cmd_value_received = GNUNET_NO;
802 if(GNUNET_YES == sensorinfo->ext_cmd_value_received)
803 return; /* We only want one *valid* value */
804 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %s\n", sensorinfo->name, line);
805 valsize = parse_sensor_value(line, sensorinfo, &value);
806 if (valsize == 0) /* invalid value, FIXME: should we disable the sensor now? */
808 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
809 _("Received an invalid value for sensor `%s': %s\n"),
810 sensorinfo->name, line);
814 sensorinfo->ext_cmd_value_received = GNUNET_YES;
815 expiry = GNUNET_TIME_relative_to_absolute(sensorinfo->lifetime);
816 GNUNET_PEERSTORE_store(peerstore,
823 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
830 * Checks if the given file is a path
832 * @return #GNUNET_YES / #GNUNET_NO
835 is_path(char *filename)
840 filename_len = strlen(filename);
841 for(i = 0; i < filename_len; i++)
843 if(DIR_SEPARATOR == filename[i])
850 * Actual execution of a sensor
852 * @param cls 'struct SensorInfo'
856 sensor_run (void *cls,
857 const struct GNUNET_SCHEDULER_TaskContext * tc)
859 struct SensorInfo *sensorinfo = cls;
864 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
865 if(GNUNET_YES == sensorinfo->running) //FIXME: should we try to kill?
867 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sensor `%s' running for too long, will try again next interval\n", sensorinfo->name);
870 if(GNUNET_NO == should_run_sensor(sensorinfo))
872 sensorinfo->running = GNUNET_YES;
873 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting the execution of sensor `%s'\n", sensorinfo->name);
874 if(sources[0] == sensorinfo->source) //gnunet-statistics
876 sensorinfo->gnunet_stat_get_handle = GNUNET_STATISTICS_get(statistics,
877 sensorinfo->gnunet_stat_service,
878 sensorinfo->gnunet_stat_name,
879 sensorinfo->interval, //try to get values only for the interval of the sensor
880 &end_sensor_run_stat,
881 &sensor_statistics_iterator,
884 else if(sources[1] == sensorinfo->source)
886 if(GNUNET_YES == is_path(sensorinfo->ext_process))
888 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
889 _("Sensor `%s': External process should not be a path, disabling sensor.\n"),
891 set_sensor_enabled(sensorinfo, GNUNET_NO);
894 //check if the process exists in $PATH
895 process_path = GNUNET_strdup(sensorinfo->ext_process);
897 GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
898 if(GNUNET_SYSERR == check_result)
900 //search in sensor directory
901 sensors_dir = get_sensor_dir();
902 GNUNET_free(process_path);
903 GNUNET_asprintf(&process_path, "%s%s-files%s%s",
907 sensorinfo->ext_process);
908 GNUNET_free(sensors_dir);
910 GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
912 if(GNUNET_SYSERR == check_result)
914 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
915 _("Sensor `%s' process `%s' problem: binary doesn't exist or not executable\n"),
917 sensorinfo->ext_process);
918 set_sensor_enabled(sensorinfo, GNUNET_NO);
919 sensorinfo->running = GNUNET_NO;
920 GNUNET_free(process_path);
923 sensorinfo->ext_cmd_value_received = GNUNET_NO;
924 sensorinfo->ext_cmd = GNUNET_OS_command_run(&sensor_process_callback,
926 GNUNET_TIME_UNIT_FOREVER_REL,
928 sensorinfo->ext_process,
929 sensorinfo->ext_args,
931 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Process started for sensor `%s'\n", sensorinfo->name);
932 GNUNET_free(process_path);
936 sensorinfo->running = GNUNET_NO;
937 GNUNET_break(0); //shouldn't happen
942 * Starts the execution of a sensor
945 * @param key hash of sensor name, key to hashmap (unused)
946 * @param value a 'struct SensorInfo *'
947 * @return #GNUNET_YES if we should continue to
951 int schedule_sensor(void *cls,
952 const struct GNUNET_HashCode *key, void *value)
954 struct SensorInfo *sensorinfo = value;
956 if(GNUNET_NO == should_run_sensor(sensorinfo))
958 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling sensor `%s' to run after %" PRIu64 " microseconds\n",
959 sensorinfo->name, sensorinfo->interval.rel_value_us);
960 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
962 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
963 _("Sensor `%s' execution task already set, this should not happen\n"), sensorinfo->name);
966 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
971 * Starts the execution of all enabled sensors
975 schedule_all_sensors()
977 GNUNET_CONTAINER_multihashmap_iterate(sensors, &schedule_sensor, NULL);
981 * Process statistics requests.
984 * @param server the initialized server
985 * @param c configuration to use
989 struct GNUNET_SERVER_Handle *server,
990 const struct GNUNET_CONFIGURATION_Handle *c)
992 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
993 {&handle_get_sensor, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GET,
995 {&handle_get_all_sensors, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GETALL,
996 sizeof (struct GNUNET_MessageHeader)},
1001 sensors = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
1003 schedule_all_sensors();
1004 SENSOR_analysis_start(c, sensors);
1005 statistics = GNUNET_STATISTICS_create("sensor", cfg);
1006 GNUNET_CRYPTO_get_peer_identity(cfg, &peerid);
1007 peerstore = GNUNET_PEERSTORE_connect(cfg);
1008 GNUNET_SERVER_add_handlers (server, handlers);
1009 GNUNET_SERVER_disconnect_notify (server,
1010 &handle_client_disconnect,
1012 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1019 * The main function for the sensor service.
1021 * @param argc number of arguments from the command line
1022 * @param argv command line arguments
1023 * @return 0 ok, 1 on error
1026 main (int argc, char *const *argv)
1028 return (GNUNET_OK ==
1029 GNUNET_SERVICE_run (argc,
1032 GNUNET_SERVICE_OPTION_NONE,
1033 &run, NULL)) ? 0 : 1;
1036 /* end of gnunet-service-sensor.c */