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_stop ();
106 GNUNET_SENSOR_destroy_sensors (sensors);
111 * Task run during shutdown.
117 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
120 if (NULL != statistics)
122 GNUNET_STATISTICS_destroy (statistics, GNUNET_YES);
125 if (NULL != peerstore)
127 GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_YES);
130 if (NULL != sensor_dir)
132 GNUNET_free (sensor_dir);
135 GNUNET_SCHEDULER_shutdown ();
140 * A client disconnected. Remove all of its data structure entries.
142 * @param cls closure, NULL
143 * @param client identification of the client
146 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
152 * Creates a structure with basic sensor info to be sent to a client.
154 * @param sensor sensor information
155 * @return message ready to be sent to client
157 static struct SensorInfoMessage *
158 create_sensor_info_msg (struct GNUNET_SENSOR_SensorInfo *sensor)
160 struct SensorInfoMessage *msg;
166 name_len = strlen (sensor->name);
167 if (NULL == sensor->description)
170 desc_len = strlen (sensor->description) + 1;
172 len += sizeof (struct SensorInfoMessage);
175 msg = GNUNET_malloc (len);
176 msg->header.size = htons (len);
177 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SENSOR_INFO);
178 msg->name_len = htons (name_len);
179 msg->description_len = htons (desc_len);
180 msg->version_major = htons (sensor->version_major);
181 msg->version_minor = htons (sensor->version_minor);
182 str_ptr = (char *) &msg[1];
183 memcpy (str_ptr, sensor->name, name_len);
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending sensor name (%d): %.*s\n",
185 name_len, name_len, str_ptr);
187 memcpy (str_ptr, sensor->description, desc_len);
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
189 "Sending sensor description (%d): %.*s\n", desc_len, desc_len,
196 * Handle GET SENSOR message.
199 * @param client identification of the client
200 * @param message the actual message
203 handle_get_sensor (void *cls, struct GNUNET_SERVER_Client *client,
204 const struct GNUNET_MessageHeader *message)
206 struct GNUNET_SERVER_TransmitContext *tc;
208 size_t sensorname_len;
209 struct GNUNET_HashCode key;
210 struct GNUNET_SENSOR_SensorInfo *sensorinfo;
211 struct SensorInfoMessage *msg;
213 sensorname = (char *) &message[1];
214 sensorname_len = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
215 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
216 "`%s' message received for sensor (%d) `%.*s'\n", "GET SENSOR",
217 sensorname_len, sensorname_len, sensorname);
218 tc = GNUNET_SERVER_transmit_context_create (client);
219 GNUNET_CRYPTO_hash (sensorname, sensorname_len, &key);
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221 "Created key hash for requested sensor\n");
223 (struct GNUNET_SENSOR_SensorInfo *)
224 GNUNET_CONTAINER_multihashmap_get (sensors, &key);
225 if (NULL != sensorinfo)
227 msg = create_sensor_info_msg (sensorinfo);
228 GNUNET_SERVER_transmit_context_append_message (tc,
229 (struct GNUNET_MessageHeader
234 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
235 "Requested sensor `%.*s' was not found\n", sensorname_len,
237 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
238 GNUNET_MESSAGE_TYPE_SENSOR_END);
239 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
244 * Iterator for sensors and adds them to transmit context
246 * @param cls a `struct GNUNET_SERVER_TransmitContext *`
247 * @param key hash of sensor name, key to hashmap
248 * @param value a `struct GNUNET_SENSOR_SensorInfo *`
251 add_sensor_to_tc (void *cls, const struct GNUNET_HashCode *key, void *value)
253 struct GNUNET_SERVER_TransmitContext *tc = cls;
254 struct GNUNET_SENSOR_SensorInfo *sensorinfo = value;
255 struct SensorInfoMessage *msg;
257 msg = create_sensor_info_msg (sensorinfo);
258 GNUNET_SERVER_transmit_context_append_message (tc,
259 (struct GNUNET_MessageHeader *)
268 * Handle GET ALL SENSORS message.
271 * @param client identification of the client
272 * @param message the actual message
275 handle_get_all_sensors (void *cls, struct GNUNET_SERVER_Client *client,
276 const struct GNUNET_MessageHeader *message)
278 struct GNUNET_SERVER_TransmitContext *tc;
280 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received.\n",
282 tc = GNUNET_SERVER_transmit_context_create (client);
283 GNUNET_CONTAINER_multihashmap_iterate (sensors, &add_sensor_to_tc, tc);
284 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
285 GNUNET_MESSAGE_TYPE_SENSOR_END);
286 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
291 * Do a series of checks to determine if sensor should execute
293 * @return #GNUNET_YES / #GNUNET_NO
296 should_run_sensor (struct GNUNET_SENSOR_SensorInfo *sensorinfo)
298 struct GNUNET_TIME_Absolute now;
300 if (GNUNET_NO == sensorinfo->enabled)
302 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
303 "Sensor `%s' is disabled, will not run\n", sensorinfo->name);
306 now = GNUNET_TIME_absolute_get ();
307 if (NULL != sensorinfo->start_time &&
308 now.abs_value_us < sensorinfo->start_time->abs_value_us)
310 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
311 "Start time for sensor `%s' not reached yet, will not run\n",
315 if (NULL != sensorinfo->end_time &&
316 now.abs_value_us >= sensorinfo->end_time->abs_value_us)
318 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Sensor `%s' expired, disabling.\n",
320 set_sensor_enabled (sensorinfo, GNUNET_NO);
328 * Callback function to process statistic values
330 * @param cls `struct GNUNET_SENSOR_SensorInfo *`
331 * @param ss name of subsystem that created the statistic
332 * @param name the name of the datum
333 * @param value the current value
334 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
335 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
338 sensor_statistics_iterator (void *cls, const char *ss, const char *name,
339 uint64_t value, int is_persistent)
341 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
342 double dvalue = (double) value;
343 struct GNUNET_TIME_Absolute expiry;
345 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
346 "Received a value for sensor `%s': %" PRIu64 "\n",
347 sensorinfo->name, value);
348 expiry = GNUNET_TIME_relative_to_absolute (sensorinfo->lifetime);
349 GNUNET_PEERSTORE_store (peerstore, subsystem, &peerid, sensorinfo->name,
350 &dvalue, sizeof (dvalue), expiry,
351 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, NULL, NULL);
352 return GNUNET_SYSERR; /* We only want one value */
357 * Continuation called after sensor gets all gnunet statistics values
359 * @param cls `struct GNUNET_SENSOR_SensorInfo *`
360 * @param success #GNUNET_OK if statistics were
361 * successfully obtained, #GNUNET_SYSERR if not.
364 end_sensor_run_stat (void *cls, int success)
366 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
368 sensorinfo->gnunet_stat_get_handle = NULL;
369 sensorinfo->running = GNUNET_NO;
374 * Tries to parse a received sensor value to its
377 * @param value the string value received, should be null terminated
378 * @param sensor sensor information struct
379 * @param ret pointer to parsed value
380 * @return size of new parsed value, 0 for error
383 parse_sensor_value (const char *value, struct GNUNET_SENSOR_SensorInfo *sensor,
392 if (0 == strcmp ("numeric", sensor->expected_datatype))
394 dval = GNUNET_new (double);
396 *dval = strtod (value, &endptr);
400 return sizeof (double);
402 if (0 == strcmp ("string", sensor->expected_datatype))
404 *ret = GNUNET_strdup (value);
405 return strlen (value) + 1;
407 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
409 ("Unknown value type expected by sensor, this should not happen.\n"));
415 * Callback for output of executed sensor process
417 * @param cls `struct GNUNET_SENSOR_SensorInfo *`
418 * @param line line of output from a command, NULL for the end
421 sensor_process_callback (void *cls, const char *line)
423 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
426 struct GNUNET_TIME_Absolute expiry;
430 GNUNET_OS_command_stop (sensorinfo->ext_cmd);
431 sensorinfo->ext_cmd = NULL;
432 sensorinfo->running = GNUNET_NO;
433 sensorinfo->ext_cmd_value_received = GNUNET_NO;
436 if (GNUNET_YES == sensorinfo->ext_cmd_value_received)
437 return; /* We only want one *valid* value */
438 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %s\n",
439 sensorinfo->name, line);
440 valsize = parse_sensor_value (line, sensorinfo, &value);
441 if (valsize == 0) /* invalid value, FIXME: should we disable the sensor now? */
443 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
444 _("Received an invalid value for sensor `%s': %s\n"),
445 sensorinfo->name, line);
449 sensorinfo->ext_cmd_value_received = GNUNET_YES;
450 expiry = GNUNET_TIME_relative_to_absolute (sensorinfo->lifetime);
451 GNUNET_PEERSTORE_store (peerstore, subsystem, &peerid, sensorinfo->name,
452 value, valsize, expiry,
453 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, NULL, NULL);
460 * Checks if the given file is a path
462 * @return #GNUNET_YES / #GNUNET_NO
465 is_path (char *filename)
470 filename_len = strlen (filename);
471 for (i = 0; i < filename_len; i++)
473 if (DIR_SEPARATOR == filename[i])
481 * Actual execution of a sensor
483 * @param cls 'struct SensorInfo'
487 sensor_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
489 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
493 sensorinfo->execution_task =
494 GNUNET_SCHEDULER_add_delayed (sensorinfo->interval, &sensor_run,
496 if (GNUNET_YES == sensorinfo->running) //FIXME: should we try to kill?
498 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
499 "Sensor `%s' running for too long, will try again next interval\n",
503 if (GNUNET_NO == should_run_sensor (sensorinfo))
505 sensorinfo->running = GNUNET_YES;
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
507 "Starting the execution of sensor `%s'\n", sensorinfo->name);
508 if (0 == strcmp ("gnunet-statistics", sensorinfo->source))
510 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
511 &end_sensor_run_stat,
512 &sensor_statistics_iterator,
515 else if (0 == strcmp ("process", sensorinfo->source))
517 if (GNUNET_YES == is_path (sensorinfo->ext_process))
519 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
521 ("Sensor `%s': External process should not be a path, disabling sensor.\n"),
523 set_sensor_enabled (sensorinfo, GNUNET_NO);
526 //check if the process exists in $PATH
527 process_path = GNUNET_strdup (sensorinfo->ext_process);
529 GNUNET_OS_check_helper_binary (process_path, GNUNET_NO, NULL);
530 if (GNUNET_SYSERR == check_result)
532 //search in sensor directory
533 GNUNET_free (process_path);
534 GNUNET_asprintf (&process_path, "%s%s-files%s%s", sensor_dir,
535 sensorinfo->name, DIR_SEPARATOR_STR,
536 sensorinfo->ext_process);
537 GNUNET_free (sensor_dir);
539 GNUNET_OS_check_helper_binary (process_path, GNUNET_NO, NULL);
541 if (GNUNET_SYSERR == check_result)
543 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
545 ("Sensor `%s' process `%s' problem: binary doesn't exist or not executable\n"),
546 sensorinfo->name, sensorinfo->ext_process);
547 set_sensor_enabled (sensorinfo, GNUNET_NO);
548 sensorinfo->running = GNUNET_NO;
549 GNUNET_free (process_path);
552 sensorinfo->ext_cmd_value_received = GNUNET_NO;
553 sensorinfo->ext_cmd =
554 GNUNET_OS_command_run (&sensor_process_callback, sensorinfo,
555 GNUNET_TIME_UNIT_FOREVER_REL, process_path,
556 sensorinfo->ext_process, sensorinfo->ext_args,
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Process started for sensor `%s'\n",
560 GNUNET_free (process_path);
564 sensorinfo->running = GNUNET_NO;
565 GNUNET_break (0); /* shouldn't happen */
571 * Starts the execution of a sensor
574 * @param key hash of sensor name, key to hashmap (unused)
575 * @param value a `struct GNUNET_SENSOR_SensorInfo *`
576 * @return #GNUNET_YES if we should continue to
581 schedule_sensor (void *cls, const struct GNUNET_HashCode *key, void *value)
583 struct GNUNET_SENSOR_SensorInfo *sensorinfo = value;
585 if (GNUNET_NO == should_run_sensor (sensorinfo))
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588 "Scheduling sensor `%s' to run after %" PRIu64 " microseconds\n",
589 sensorinfo->name, sensorinfo->interval.rel_value_us);
590 if (GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
592 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
594 ("Sensor `%s' execution task already set, this should not happen\n"),
598 sensorinfo->execution_task =
599 GNUNET_SCHEDULER_add_delayed (sensorinfo->interval, &sensor_run,
606 * Starts the execution of all enabled sensors
609 schedule_all_sensors ()
611 GNUNET_CONTAINER_multihashmap_iterate (sensors, &schedule_sensor, NULL);
616 * Loads sensors and starts different service components
621 sensors = GNUNET_SENSOR_load_all_sensors (sensor_dir);
622 schedule_all_sensors ();
623 SENSOR_reporting_start (cfg, sensors);
624 SENSOR_analysis_start (cfg, sensors);
625 SENSOR_update_start (cfg, sensors, &reset);
630 * Process statistics requests.
633 * @param server the initialized server
634 * @param c configuration to use
637 run (void *cls, struct GNUNET_SERVER_Handle *server,
638 const struct GNUNET_CONFIGURATION_Handle *c)
640 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
641 {&handle_get_sensor, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GET,
643 {&handle_get_all_sensors, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GETALL,
644 sizeof (struct GNUNET_MessageHeader)},
650 GNUNET_CONFIGURATION_get_value_filename (cfg, "SENSOR", "SENSOR_DIR",
652 sensor_dir = GNUNET_SENSOR_get_default_sensor_dir ();
653 statistics = GNUNET_STATISTICS_create ("sensor", cfg);
654 GNUNET_CRYPTO_get_peer_identity (cfg, &peerid);
655 peerstore = GNUNET_PEERSTORE_connect (cfg);
656 GNUNET_SERVER_add_handlers (server, handlers);
657 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
658 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
665 * Resets the service by stopping components, reloading sensors and starting
666 * components. This is needed when we receive new sensor updates.
677 * The main function for the sensor service.
679 * @param argc number of arguments from the command line
680 * @param argv command line arguments
681 * @return 0 ok, 1 on error
684 main (int argc, char *const *argv)
687 GNUNET_SERVICE_run (argc, argv, "sensor", GNUNET_SERVICE_OPTION_NONE,
688 &run, NULL)) ? 0 : 1;
691 /* end of gnunet-service-sensor.c */