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 * Path to sensor definitions directory
41 static char *sensor_dir;
44 * Hashmap of loaded sensor definitions
46 static struct GNUNET_CONTAINER_MultiHashMap *sensors;
49 * Handle to statistics service
51 static struct GNUNET_STATISTICS_Handle *statistics;
54 * Handle to peerstore service
56 static struct GNUNET_PEERSTORE_Handle *peerstore;
61 static char *subsystem = "sensor";
66 static struct GNUNET_PeerIdentity peerid;
70 * Resets the service by stopping components, reloading sensors and starting
71 * components. This is needed when we receive new sensor updates.
78 * Change the state of the sensor.
79 * Write the change to file to make it persistent.
81 * @param sensor sensor info struct
82 * @param state new enabled state: #GNUNET_YES / #GNUNET_NO
85 set_sensor_enabled (struct GNUNET_SENSOR_SensorInfo *sensor, int state)
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sensor `%s': Setting enabled to %d.\n",
89 sensor->enabled = GNUNET_NO;
90 GNUNET_assert (NULL != sensor->cfg);
91 GNUNET_CONFIGURATION_set_value_string (sensor->cfg, sensor->name, "ENABLED",
92 (GNUNET_YES == state) ? "YES" : "NO");
93 GNUNET_CONFIGURATION_write (sensor->cfg, sensor->def_file);
98 * Stops components and destroys sensors
103 SENSOR_update_stop ();
104 SENSOR_analysis_stop ();
105 SENSOR_reporting_value_stop ();
106 SENSOR_reporting_anomaly_stop ();
107 GNUNET_SENSOR_destroy_sensors (sensors);
112 * Task run during shutdown.
118 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
121 if (NULL != statistics)
123 GNUNET_STATISTICS_destroy (statistics, GNUNET_YES);
126 if (NULL != peerstore)
128 GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_YES);
131 if (NULL != sensor_dir)
133 GNUNET_free (sensor_dir);
136 GNUNET_SCHEDULER_shutdown ();
141 * A client disconnected. Remove all of its data structure entries.
143 * @param cls closure, NULL
144 * @param client identification of the client
147 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
153 * Creates a structure with basic sensor info to be sent to a client.
155 * @param sensor sensor information
156 * @return message ready to be sent to client
158 static struct SensorInfoMessage *
159 create_sensor_info_msg (struct GNUNET_SENSOR_SensorInfo *sensor)
161 struct SensorInfoMessage *msg;
167 name_len = strlen (sensor->name);
168 if (NULL == sensor->description)
171 desc_len = strlen (sensor->description) + 1;
173 len += sizeof (struct SensorInfoMessage);
176 msg = GNUNET_malloc (len);
177 msg->header.size = htons (len);
178 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SENSOR_INFO);
179 msg->name_len = htons (name_len);
180 msg->description_len = htons (desc_len);
181 msg->version_major = htons (sensor->version_major);
182 msg->version_minor = htons (sensor->version_minor);
183 str_ptr = (char *) &msg[1];
184 memcpy (str_ptr, sensor->name, name_len);
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending sensor name (%d): %.*s\n",
186 name_len, name_len, str_ptr);
188 memcpy (str_ptr, sensor->description, desc_len);
189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
190 "Sending sensor description (%d): %.*s\n", desc_len, desc_len,
197 * Handle GET SENSOR message.
200 * @param client identification of the client
201 * @param message the actual message
204 handle_get_sensor (void *cls, struct GNUNET_SERVER_Client *client,
205 const struct GNUNET_MessageHeader *message)
207 struct GNUNET_SERVER_TransmitContext *tc;
209 size_t sensorname_len;
210 struct GNUNET_HashCode key;
211 struct GNUNET_SENSOR_SensorInfo *sensorinfo;
212 struct SensorInfoMessage *msg;
214 sensorname = (char *) &message[1];
215 sensorname_len = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
216 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
217 "`%s' message received for sensor (%d) `%.*s'\n", "GET SENSOR",
218 sensorname_len, sensorname_len, sensorname);
219 tc = GNUNET_SERVER_transmit_context_create (client);
220 GNUNET_CRYPTO_hash (sensorname, sensorname_len, &key);
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222 "Created key hash for requested sensor\n");
224 (struct GNUNET_SENSOR_SensorInfo *)
225 GNUNET_CONTAINER_multihashmap_get (sensors, &key);
226 if (NULL != sensorinfo)
228 msg = create_sensor_info_msg (sensorinfo);
229 GNUNET_SERVER_transmit_context_append_message (tc,
230 (struct GNUNET_MessageHeader
235 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
236 "Requested sensor `%.*s' was not found\n", sensorname_len,
238 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
239 GNUNET_MESSAGE_TYPE_SENSOR_END);
240 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
245 * Iterator for sensors and adds them to transmit context
247 * @param cls a `struct GNUNET_SERVER_TransmitContext *`
248 * @param key hash of sensor name, key to hashmap
249 * @param value a `struct GNUNET_SENSOR_SensorInfo *`
252 add_sensor_to_tc (void *cls, const struct GNUNET_HashCode *key, void *value)
254 struct GNUNET_SERVER_TransmitContext *tc = cls;
255 struct GNUNET_SENSOR_SensorInfo *sensorinfo = value;
256 struct SensorInfoMessage *msg;
258 msg = create_sensor_info_msg (sensorinfo);
259 GNUNET_SERVER_transmit_context_append_message (tc,
260 (struct GNUNET_MessageHeader *)
269 * Handle GET ALL SENSORS message.
272 * @param client identification of the client
273 * @param message the actual message
276 handle_get_all_sensors (void *cls, struct GNUNET_SERVER_Client *client,
277 const struct GNUNET_MessageHeader *message)
279 struct GNUNET_SERVER_TransmitContext *tc;
281 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received.\n",
283 tc = GNUNET_SERVER_transmit_context_create (client);
284 GNUNET_CONTAINER_multihashmap_iterate (sensors, &add_sensor_to_tc, tc);
285 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
286 GNUNET_MESSAGE_TYPE_SENSOR_END);
287 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
292 * Do a series of checks to determine if sensor should execute
294 * @return #GNUNET_YES / #GNUNET_NO
297 should_run_sensor (struct GNUNET_SENSOR_SensorInfo *sensorinfo)
299 struct GNUNET_TIME_Absolute now;
301 if (GNUNET_NO == sensorinfo->enabled)
303 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
304 "Sensor `%s' is disabled, will not run\n", sensorinfo->name);
307 now = GNUNET_TIME_absolute_get ();
308 if (NULL != sensorinfo->start_time &&
309 now.abs_value_us < sensorinfo->start_time->abs_value_us)
311 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
312 "Start time for sensor `%s' not reached yet, will not run\n",
316 if (NULL != sensorinfo->end_time &&
317 now.abs_value_us >= sensorinfo->end_time->abs_value_us)
319 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Sensor `%s' expired, disabling.\n",
321 set_sensor_enabled (sensorinfo, GNUNET_NO);
329 * Callback function to process statistic values
331 * @param cls `struct GNUNET_SENSOR_SensorInfo *`
332 * @param ss name of subsystem that created the statistic
333 * @param name the name of the datum
334 * @param value the current value
335 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
336 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
339 sensor_statistics_iterator (void *cls, const char *ss, const char *name,
340 uint64_t value, int is_persistent)
342 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
343 double dvalue = (double) value;
344 struct GNUNET_TIME_Absolute expiry;
346 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
347 "Received a value for sensor `%s': %" PRIu64 "\n",
348 sensorinfo->name, value);
349 expiry = GNUNET_TIME_relative_to_absolute (sensorinfo->lifetime);
350 GNUNET_PEERSTORE_store (peerstore, subsystem, &peerid, sensorinfo->name,
351 &dvalue, sizeof (dvalue), expiry,
352 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, NULL, NULL);
353 return GNUNET_SYSERR; /* We only want one value */
358 * Continuation called after sensor gets all gnunet statistics values
360 * @param cls `struct GNUNET_SENSOR_SensorInfo *`
361 * @param success #GNUNET_OK if statistics were
362 * successfully obtained, #GNUNET_SYSERR if not.
365 end_sensor_run_stat (void *cls, int success)
367 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
369 sensorinfo->gnunet_stat_get_handle = NULL;
370 sensorinfo->running = GNUNET_NO;
375 * Tries to parse a received sensor value to its
378 * @param value the string value received, should be null terminated
379 * @param sensor sensor information struct
380 * @param ret pointer to parsed value
381 * @return size of new parsed value, 0 for error
384 parse_sensor_value (const char *value, struct GNUNET_SENSOR_SensorInfo *sensor,
393 if (0 == strcmp ("numeric", sensor->expected_datatype))
395 dval = GNUNET_new (double);
397 *dval = strtod (value, &endptr);
401 return sizeof (double);
403 if (0 == strcmp ("string", sensor->expected_datatype))
405 *ret = GNUNET_strdup (value);
406 return strlen (value) + 1;
408 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
410 ("Unknown value type expected by sensor, this should not happen.\n"));
416 * Callback for output of executed sensor process
418 * @param cls `struct GNUNET_SENSOR_SensorInfo *`
419 * @param line line of output from a command, NULL for the end
422 sensor_process_callback (void *cls, const char *line)
424 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
427 struct GNUNET_TIME_Absolute expiry;
431 GNUNET_OS_command_stop (sensorinfo->ext_cmd);
432 sensorinfo->ext_cmd = NULL;
433 sensorinfo->running = GNUNET_NO;
434 sensorinfo->ext_cmd_value_received = GNUNET_NO;
437 if (GNUNET_YES == sensorinfo->ext_cmd_value_received)
438 return; /* We only want one *valid* value */
439 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %s\n",
440 sensorinfo->name, line);
441 valsize = parse_sensor_value (line, sensorinfo, &value);
442 if (valsize == 0) /* invalid value, FIXME: should we disable the sensor now? */
444 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
445 _("Received an invalid value for sensor `%s': %s\n"),
446 sensorinfo->name, line);
450 sensorinfo->ext_cmd_value_received = GNUNET_YES;
451 expiry = GNUNET_TIME_relative_to_absolute (sensorinfo->lifetime);
452 GNUNET_PEERSTORE_store (peerstore, subsystem, &peerid, sensorinfo->name,
453 value, valsize, expiry,
454 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, NULL, NULL);
461 * Checks if the given file is a path
463 * @return #GNUNET_YES / #GNUNET_NO
466 is_path (char *filename)
471 filename_len = strlen (filename);
472 for (i = 0; i < filename_len; i++)
474 if (DIR_SEPARATOR == filename[i])
482 * Actual execution of a sensor
484 * @param cls 'struct SensorInfo'
488 sensor_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
490 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
494 sensorinfo->execution_task =
495 GNUNET_SCHEDULER_add_delayed (sensorinfo->interval, &sensor_run,
497 if (GNUNET_YES == sensorinfo->running) //FIXME: should we try to kill?
499 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
500 "Sensor `%s' running for too long, will try again next interval\n",
504 if (GNUNET_NO == should_run_sensor (sensorinfo))
506 sensorinfo->running = GNUNET_YES;
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 "Starting the execution of sensor `%s'\n", sensorinfo->name);
509 if (0 == strcmp ("gnunet-statistics", sensorinfo->source))
511 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
512 &end_sensor_run_stat,
513 &sensor_statistics_iterator,
516 else if (0 == strcmp ("process", sensorinfo->source))
518 if (GNUNET_YES == is_path (sensorinfo->ext_process))
520 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
522 ("Sensor `%s': External process should not be a path, disabling sensor.\n"),
524 set_sensor_enabled (sensorinfo, GNUNET_NO);
527 //check if the process exists in $PATH
528 process_path = GNUNET_strdup (sensorinfo->ext_process);
530 GNUNET_OS_check_helper_binary (process_path, GNUNET_NO, NULL);
531 if (GNUNET_SYSERR == check_result)
533 //search in sensor directory
534 GNUNET_free (process_path);
535 GNUNET_asprintf (&process_path, "%s%s-files%s%s", sensor_dir,
536 sensorinfo->name, DIR_SEPARATOR_STR,
537 sensorinfo->ext_process);
538 GNUNET_free (sensor_dir);
540 GNUNET_OS_check_helper_binary (process_path, GNUNET_NO, NULL);
542 if (GNUNET_SYSERR == check_result)
544 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
546 ("Sensor `%s' process `%s' problem: binary doesn't exist or not executable\n"),
547 sensorinfo->name, sensorinfo->ext_process);
548 set_sensor_enabled (sensorinfo, GNUNET_NO);
549 sensorinfo->running = GNUNET_NO;
550 GNUNET_free (process_path);
553 sensorinfo->ext_cmd_value_received = GNUNET_NO;
554 sensorinfo->ext_cmd =
555 GNUNET_OS_command_run (&sensor_process_callback, sensorinfo,
556 GNUNET_TIME_UNIT_FOREVER_REL, process_path,
557 sensorinfo->ext_process, sensorinfo->ext_args,
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Process started for sensor `%s'\n",
561 GNUNET_free (process_path);
565 sensorinfo->running = GNUNET_NO;
566 GNUNET_break (0); /* shouldn't happen */
572 * Starts the execution of a sensor
575 * @param key hash of sensor name, key to hashmap (unused)
576 * @param value a `struct GNUNET_SENSOR_SensorInfo *`
577 * @return #GNUNET_YES if we should continue to
582 schedule_sensor (void *cls, const struct GNUNET_HashCode *key, void *value)
584 struct GNUNET_SENSOR_SensorInfo *sensorinfo = value;
586 if (GNUNET_NO == should_run_sensor (sensorinfo))
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
589 "Scheduling sensor `%s' to run after %" PRIu64 " microseconds\n",
590 sensorinfo->name, sensorinfo->interval.rel_value_us);
591 if (GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
593 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
595 ("Sensor `%s' execution task already set, this should not happen\n"),
599 sensorinfo->execution_task =
600 GNUNET_SCHEDULER_add_delayed (sensorinfo->interval, &sensor_run,
607 * Starts the execution of all enabled sensors
610 schedule_all_sensors ()
612 GNUNET_CONTAINER_multihashmap_iterate (sensors, &schedule_sensor, NULL);
617 * Loads sensors and starts different service components
622 sensors = GNUNET_SENSOR_load_all_sensors (sensor_dir);
623 schedule_all_sensors ();
624 SENSOR_reporting_value_start (cfg, sensors);
625 SENSOR_reporting_anomaly_start (cfg, sensors);
626 SENSOR_analysis_start (cfg, sensors);
627 SENSOR_update_start (cfg, sensors, &reset);
632 * Process statistics requests.
635 * @param server the initialized server
636 * @param c configuration to use
639 run (void *cls, struct GNUNET_SERVER_Handle *server,
640 const struct GNUNET_CONFIGURATION_Handle *c)
642 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
643 {&handle_get_sensor, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GET,
645 {&handle_get_all_sensors, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GETALL,
646 sizeof (struct GNUNET_MessageHeader)},
652 GNUNET_CONFIGURATION_get_value_filename (cfg, "SENSOR", "SENSOR_DIR",
654 sensor_dir = GNUNET_SENSOR_get_default_sensor_dir ();
655 statistics = GNUNET_STATISTICS_create ("sensor", cfg);
656 GNUNET_CRYPTO_get_peer_identity (cfg, &peerid);
657 peerstore = GNUNET_PEERSTORE_connect (cfg);
658 GNUNET_SERVER_add_handlers (server, handlers);
659 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
660 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
667 * Resets the service by stopping components, reloading sensors and starting
668 * components. This is needed when we receive new sensor updates.
679 * The main function for the sensor service.
681 * @param argc number of arguments from the command line
682 * @param argv command line arguments
683 * @return 0 ok, 1 on error
686 main (int argc, char *const *argv)
689 GNUNET_SERVICE_run (argc, argv, "sensor", GNUNET_SERVICE_OPTION_NONE,
690 &run, NULL)) ? 0 : 1;
693 /* end of gnunet-service-sensor.c */