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"
36 static const struct GNUNET_CONFIGURATION_Handle *cfg;
39 * Hashmap of loaded sensor definitions
41 static struct GNUNET_CONTAINER_MultiHashMap *sensors;
44 * Handle to statistics service
46 static struct GNUNET_STATISTICS_Handle *statistics;
49 * Handle to peerstore service
51 static struct GNUNET_PEERSTORE_Handle *peerstore;
56 static char *subsystem = "sensor";
61 static struct GNUNET_PeerIdentity peerid;
65 * Resets the service by stopping components, reloading sensors and starting
66 * components. This is needed when we receive new sensor updates.
73 * Change the state of the sensor.
74 * Write the change to file to make it persistent.
76 * @param sensor sensor info struct
77 * @param state new enabled state: #GNUNET_YES / #GNUNET_NO
80 set_sensor_enabled (struct GNUNET_SENSOR_SensorInfo *sensor, int state)
82 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sensor `%s': Setting enabled to %d.\n",
84 sensor->enabled = GNUNET_NO;
85 GNUNET_assert (NULL != sensor->cfg);
86 GNUNET_CONFIGURATION_set_value_string (sensor->cfg, sensor->name, "ENABLED",
87 (GNUNET_YES == state) ? "YES" : "NO");
88 GNUNET_CONFIGURATION_write (sensor->cfg, sensor->def_file);
93 * Stops components and destroys sensors
98 SENSOR_update_stop ();
99 SENSOR_analysis_stop ();
100 SENSOR_reporting_value_stop ();
101 SENSOR_reporting_anomaly_stop ();
102 GNUNET_SENSOR_destroy_sensors (sensors);
107 * Task run during shutdown.
113 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 if (NULL != statistics)
118 GNUNET_STATISTICS_destroy (statistics, GNUNET_YES);
121 if (NULL != peerstore)
123 GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_YES);
126 GNUNET_SCHEDULER_shutdown ();
131 * A client disconnected. Remove all of its data structure entries.
133 * @param cls closure, NULL
134 * @param client identification of the client
137 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
143 * Creates a structure with basic sensor info to be sent to a client.
145 * @param sensor sensor information
146 * @return message ready to be sent to client
148 static struct SensorInfoMessage *
149 create_sensor_info_msg (struct GNUNET_SENSOR_SensorInfo *sensor)
151 struct SensorInfoMessage *msg;
157 name_len = strlen (sensor->name);
158 if (NULL == sensor->description)
161 desc_len = strlen (sensor->description) + 1;
163 len += sizeof (struct SensorInfoMessage);
166 msg = GNUNET_malloc (len);
167 msg->header.size = htons (len);
168 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SENSOR_INFO);
169 msg->name_len = htons (name_len);
170 msg->description_len = htons (desc_len);
171 msg->version_major = htons (sensor->version_major);
172 msg->version_minor = htons (sensor->version_minor);
173 str_ptr = (char *) &msg[1];
174 memcpy (str_ptr, sensor->name, name_len);
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending sensor name (%d): %.*s\n",
176 name_len, name_len, str_ptr);
178 memcpy (str_ptr, sensor->description, desc_len);
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180 "Sending sensor description (%d): %.*s\n", desc_len, desc_len,
187 * Handle GET SENSOR message.
190 * @param client identification of the client
191 * @param message the actual message
194 handle_get_sensor (void *cls, struct GNUNET_SERVER_Client *client,
195 const struct GNUNET_MessageHeader *message)
197 struct GNUNET_SERVER_TransmitContext *tc;
199 size_t sensorname_len;
200 struct GNUNET_HashCode key;
201 struct GNUNET_SENSOR_SensorInfo *sensorinfo;
202 struct SensorInfoMessage *msg;
204 sensorname = (char *) &message[1];
205 sensorname_len = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
206 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
207 "`%s' message received for sensor (%d) `%.*s'\n", "GET SENSOR",
208 sensorname_len, sensorname_len, sensorname);
209 tc = GNUNET_SERVER_transmit_context_create (client);
210 GNUNET_CRYPTO_hash (sensorname, sensorname_len, &key);
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "Created key hash for requested sensor\n");
214 (struct GNUNET_SENSOR_SensorInfo *)
215 GNUNET_CONTAINER_multihashmap_get (sensors, &key);
216 if (NULL != sensorinfo)
218 msg = create_sensor_info_msg (sensorinfo);
219 GNUNET_SERVER_transmit_context_append_message (tc,
220 (struct GNUNET_MessageHeader
225 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
226 "Requested sensor `%.*s' was not found\n", sensorname_len,
228 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
229 GNUNET_MESSAGE_TYPE_SENSOR_END);
230 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
235 * Iterator for sensors and adds them to transmit context
237 * @param cls a `struct GNUNET_SERVER_TransmitContext *`
238 * @param key hash of sensor name, key to hashmap
239 * @param value a `struct GNUNET_SENSOR_SensorInfo *`
242 add_sensor_to_tc (void *cls, const struct GNUNET_HashCode *key, void *value)
244 struct GNUNET_SERVER_TransmitContext *tc = cls;
245 struct GNUNET_SENSOR_SensorInfo *sensorinfo = value;
246 struct SensorInfoMessage *msg;
248 msg = create_sensor_info_msg (sensorinfo);
249 GNUNET_SERVER_transmit_context_append_message (tc,
250 (struct GNUNET_MessageHeader *)
259 * Handle GET ALL SENSORS message.
262 * @param client identification of the client
263 * @param message the actual message
266 handle_get_all_sensors (void *cls, struct GNUNET_SERVER_Client *client,
267 const struct GNUNET_MessageHeader *message)
269 struct GNUNET_SERVER_TransmitContext *tc;
271 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received.\n",
273 tc = GNUNET_SERVER_transmit_context_create (client);
274 GNUNET_CONTAINER_multihashmap_iterate (sensors, &add_sensor_to_tc, tc);
275 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
276 GNUNET_MESSAGE_TYPE_SENSOR_END);
277 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
282 * Do a series of checks to determine if sensor should execute
284 * @return #GNUNET_YES / #GNUNET_NO
287 should_run_sensor (struct GNUNET_SENSOR_SensorInfo *sensorinfo)
289 struct GNUNET_TIME_Absolute now;
291 if (GNUNET_NO == sensorinfo->enabled)
293 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
294 "Sensor `%s' is disabled, will not run\n", sensorinfo->name);
297 now = GNUNET_TIME_absolute_get ();
298 if (NULL != sensorinfo->start_time &&
299 now.abs_value_us < sensorinfo->start_time->abs_value_us)
301 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
302 "Start time for sensor `%s' not reached yet, will not run\n",
306 if (NULL != sensorinfo->end_time &&
307 now.abs_value_us >= sensorinfo->end_time->abs_value_us)
309 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Sensor `%s' expired, disabling.\n",
311 set_sensor_enabled (sensorinfo, GNUNET_NO);
319 * Callback function to process statistic values
321 * @param cls `struct GNUNET_SENSOR_SensorInfo *`
322 * @param ss name of subsystem that created the statistic
323 * @param name the name of the datum
324 * @param value the current value
325 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
326 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
329 sensor_statistics_iterator (void *cls, const char *ss, const char *name,
330 uint64_t value, int is_persistent)
332 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
333 double dvalue = (double) value;
334 struct GNUNET_TIME_Absolute expiry;
336 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
337 "Received a value for sensor `%s': %" PRIu64 "\n",
338 sensorinfo->name, value);
339 expiry = GNUNET_TIME_relative_to_absolute (sensorinfo->lifetime);
340 GNUNET_PEERSTORE_store (peerstore, subsystem, &peerid, sensorinfo->name,
341 &dvalue, sizeof (dvalue), expiry,
342 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, NULL, NULL);
343 return GNUNET_SYSERR; /* We only want one value */
348 * Continuation called after sensor gets all gnunet statistics values
350 * @param cls `struct GNUNET_SENSOR_SensorInfo *`
351 * @param success #GNUNET_OK if statistics were
352 * successfully obtained, #GNUNET_SYSERR if not.
355 end_sensor_run_stat (void *cls, int success)
357 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
359 sensorinfo->gnunet_stat_get_handle = NULL;
360 sensorinfo->running = GNUNET_NO;
365 * Tries to parse a received sensor value to its
368 * @param value the string value received, should be null terminated
369 * @param sensor sensor information struct
370 * @param ret pointer to parsed value
371 * @return size of new parsed value, 0 for error
374 parse_sensor_value (const char *value, struct GNUNET_SENSOR_SensorInfo *sensor,
383 if (0 == strcmp ("numeric", sensor->expected_datatype))
385 dval = GNUNET_new (double);
387 *dval = strtod (value, &endptr);
391 return sizeof (double);
393 if (0 == strcmp ("string", sensor->expected_datatype))
395 *ret = GNUNET_strdup (value);
396 return strlen (value) + 1;
398 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
400 ("Unknown value type expected by sensor, this should not happen.\n"));
406 * Callback for output of executed sensor process
408 * @param cls `struct GNUNET_SENSOR_SensorInfo *`
409 * @param line line of output from a command, NULL for the end
412 sensor_process_callback (void *cls, const char *line)
414 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
417 struct GNUNET_TIME_Absolute expiry;
421 GNUNET_OS_command_stop (sensorinfo->ext_cmd);
422 sensorinfo->ext_cmd = NULL;
423 sensorinfo->running = GNUNET_NO;
424 sensorinfo->ext_cmd_value_received = GNUNET_NO;
427 if (GNUNET_YES == sensorinfo->ext_cmd_value_received)
428 return; /* We only want one *valid* value */
429 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %s\n",
430 sensorinfo->name, line);
431 valsize = parse_sensor_value (line, sensorinfo, &value);
432 if (valsize == 0) /* invalid value, FIXME: should we disable the sensor now? */
434 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
435 _("Received an invalid value for sensor `%s': %s\n"),
436 sensorinfo->name, line);
440 sensorinfo->ext_cmd_value_received = GNUNET_YES;
441 expiry = GNUNET_TIME_relative_to_absolute (sensorinfo->lifetime);
442 GNUNET_PEERSTORE_store (peerstore, subsystem, &peerid, sensorinfo->name,
443 value, valsize, expiry,
444 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, NULL, NULL);
451 * Checks if the given file is a path
453 * @return #GNUNET_YES / #GNUNET_NO
456 is_path (char *filename)
461 filename_len = strlen (filename);
462 for (i = 0; i < filename_len; i++)
464 if (DIR_SEPARATOR == filename[i])
472 * Actual execution of a sensor
474 * @param cls 'struct SensorInfo'
478 sensor_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
480 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
485 sensorinfo->execution_task =
486 GNUNET_SCHEDULER_add_delayed (sensorinfo->interval, &sensor_run,
488 if (GNUNET_YES == sensorinfo->running) //FIXME: should we try to kill?
490 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
491 "Sensor `%s' running for too long, will try again next interval\n",
495 if (GNUNET_NO == should_run_sensor (sensorinfo))
497 sensorinfo->running = GNUNET_YES;
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
499 "Starting the execution of sensor `%s'\n", sensorinfo->name);
500 if (0 == strcmp ("gnunet-statistics", sensorinfo->source))
502 sensorinfo->gnunet_stat_get_handle = GNUNET_STATISTICS_get (statistics, sensorinfo->gnunet_stat_service, sensorinfo->gnunet_stat_name, sensorinfo->interval, //try to get values only for the interval of the sensor
503 &end_sensor_run_stat,
504 &sensor_statistics_iterator,
507 else if (0 == strcmp ("process", sensorinfo->source))
509 if (GNUNET_YES == is_path (sensorinfo->ext_process))
511 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
513 ("Sensor `%s': External process should not be a path, disabling sensor.\n"),
515 set_sensor_enabled (sensorinfo, GNUNET_NO);
518 //check if the process exists in $PATH
519 process_path = GNUNET_strdup (sensorinfo->ext_process);
521 GNUNET_OS_check_helper_binary (process_path, GNUNET_NO, NULL);
522 if (GNUNET_SYSERR == check_result)
524 //search in sensor directory
525 sensors_dir = GNUNET_SENSOR_get_sensor_dir ();
526 GNUNET_free (process_path);
527 GNUNET_asprintf (&process_path, "%s%s-files%s%s", sensors_dir,
528 sensorinfo->name, DIR_SEPARATOR_STR,
529 sensorinfo->ext_process);
530 GNUNET_free (sensors_dir);
532 GNUNET_OS_check_helper_binary (process_path, GNUNET_NO, NULL);
534 if (GNUNET_SYSERR == check_result)
536 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
538 ("Sensor `%s' process `%s' problem: binary doesn't exist or not executable\n"),
539 sensorinfo->name, sensorinfo->ext_process);
540 set_sensor_enabled (sensorinfo, GNUNET_NO);
541 sensorinfo->running = GNUNET_NO;
542 GNUNET_free (process_path);
545 sensorinfo->ext_cmd_value_received = GNUNET_NO;
546 sensorinfo->ext_cmd =
547 GNUNET_OS_command_run (&sensor_process_callback, sensorinfo,
548 GNUNET_TIME_UNIT_FOREVER_REL, process_path,
549 sensorinfo->ext_process, sensorinfo->ext_args,
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Process started for sensor `%s'\n",
553 GNUNET_free (process_path);
557 sensorinfo->running = GNUNET_NO;
558 GNUNET_break (0); /* shouldn't happen */
564 * Starts the execution of a sensor
567 * @param key hash of sensor name, key to hashmap (unused)
568 * @param value a `struct GNUNET_SENSOR_SensorInfo *`
569 * @return #GNUNET_YES if we should continue to
574 schedule_sensor (void *cls, const struct GNUNET_HashCode *key, void *value)
576 struct GNUNET_SENSOR_SensorInfo *sensorinfo = value;
578 if (GNUNET_NO == should_run_sensor (sensorinfo))
580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
581 "Scheduling sensor `%s' to run after %" PRIu64 " microseconds\n",
582 sensorinfo->name, sensorinfo->interval.rel_value_us);
583 if (GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
585 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
587 ("Sensor `%s' execution task already set, this should not happen\n"),
591 sensorinfo->execution_task =
592 GNUNET_SCHEDULER_add_delayed (sensorinfo->interval, &sensor_run,
599 * Starts the execution of all enabled sensors
602 schedule_all_sensors ()
604 GNUNET_CONTAINER_multihashmap_iterate (sensors, &schedule_sensor, NULL);
609 * Loads sensors and starts different service components
614 sensors = GNUNET_SENSOR_load_all_sensors ();
615 schedule_all_sensors ();
616 SENSOR_reporting_value_start (cfg, sensors);
617 SENSOR_reporting_anomaly_start (cfg, sensors);
618 SENSOR_analysis_start (cfg, sensors);
619 SENSOR_update_start (cfg, sensors, &reset);
624 * Process statistics requests.
627 * @param server the initialized server
628 * @param c configuration to use
631 run (void *cls, struct GNUNET_SERVER_Handle *server,
632 const struct GNUNET_CONFIGURATION_Handle *c)
634 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
635 {&handle_get_sensor, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GET,
637 {&handle_get_all_sensors, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GETALL,
638 sizeof (struct GNUNET_MessageHeader)},
643 statistics = GNUNET_STATISTICS_create ("sensor", cfg);
644 GNUNET_CRYPTO_get_peer_identity (cfg, &peerid);
645 peerstore = GNUNET_PEERSTORE_connect (cfg);
646 GNUNET_SERVER_add_handlers (server, handlers);
647 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
648 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
655 * Resets the service by stopping components, reloading sensors and starting
656 * components. This is needed when we receive new sensor updates.
667 * The main function for the sensor service.
669 * @param argc number of arguments from the command line
670 * @param argv command line arguments
671 * @return 0 ok, 1 on error
674 main (int argc, char *const *argv)
677 GNUNET_SERVICE_run (argc, argv, "sensor", GNUNET_SERVICE_OPTION_NONE,
678 &run, NULL)) ? 0 : 1;
681 /* end of gnunet-service-sensor.c */