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 struct GNUNET_STATISTICS_Handle *statistics;
49 * Handle to peerstore service
51 struct GNUNET_PEERSTORE_Handle *peerstore;
56 char *subsystem = "sensor";
61 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_reporting_stop ();
100 SENSOR_analysis_stop ();
101 GNUNET_SENSOR_destroy_sensors (sensors);
106 * Task run during shutdown.
112 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
115 if (NULL != statistics)
117 GNUNET_STATISTICS_destroy (statistics, GNUNET_YES);
120 if (NULL != peerstore)
122 GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_YES);
125 GNUNET_SCHEDULER_shutdown ();
130 * A client disconnected. Remove all of its data structure entries.
132 * @param cls closure, NULL
133 * @param client identification of the client
136 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
142 * Creates a structure with basic sensor info to be sent to a client.
144 * @param sensor sensor information
145 * @return message ready to be sent to client
147 static struct SensorInfoMessage *
148 create_sensor_info_msg (struct GNUNET_SENSOR_SensorInfo *sensor)
150 struct SensorInfoMessage *msg;
156 name_len = strlen (sensor->name);
157 if (NULL == sensor->description)
160 desc_len = strlen (sensor->description) + 1;
162 len += sizeof (struct SensorInfoMessage);
165 msg = GNUNET_malloc (len);
166 msg->header.size = htons (len);
167 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SENSOR_INFO);
168 msg->name_len = htons (name_len);
169 msg->description_len = htons (desc_len);
170 msg->version_major = htons (sensor->version_major);
171 msg->version_minor = htons (sensor->version_minor);
172 str_ptr = (char *) &msg[1];
173 memcpy (str_ptr, sensor->name, name_len);
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending sensor name (%d): %.*s\n",
175 name_len, name_len, str_ptr);
177 memcpy (str_ptr, sensor->description, desc_len);
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 "Sending sensor description (%d): %.*s\n", desc_len, desc_len,
186 * Handle GET SENSOR message.
189 * @param client identification of the client
190 * @param message the actual message
193 handle_get_sensor (void *cls, struct GNUNET_SERVER_Client *client,
194 const struct GNUNET_MessageHeader *message)
196 struct GNUNET_SERVER_TransmitContext *tc;
198 size_t sensorname_len;
199 struct GNUNET_HashCode key;
200 struct GNUNET_SENSOR_SensorInfo *sensorinfo;
201 struct SensorInfoMessage *msg;
203 sensorname = (char *) &message[1];
204 sensorname_len = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
205 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
206 "`%s' message received for sensor (%d) `%.*s'\n", "GET SENSOR",
207 sensorname_len, sensorname_len, sensorname);
208 tc = GNUNET_SERVER_transmit_context_create (client);
209 GNUNET_CRYPTO_hash (sensorname, sensorname_len, &key);
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211 "Created key hash for requested sensor\n");
213 (struct GNUNET_SENSOR_SensorInfo *)
214 GNUNET_CONTAINER_multihashmap_get (sensors, &key);
215 if (NULL != sensorinfo)
217 msg = create_sensor_info_msg (sensorinfo);
218 GNUNET_SERVER_transmit_context_append_message (tc,
219 (struct GNUNET_MessageHeader
224 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
225 "Requested sensor `%.*s' was not found\n", sensorname_len,
227 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
228 GNUNET_MESSAGE_TYPE_SENSOR_END);
229 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
234 * Iterator for sensors and adds them to transmit context
236 * @param cls a `struct GNUNET_SERVER_TransmitContext *`
237 * @param key hash of sensor name, key to hashmap
238 * @param value a `struct GNUNET_SENSOR_SensorInfo *`
241 add_sensor_to_tc (void *cls, const struct GNUNET_HashCode *key, void *value)
243 struct GNUNET_SERVER_TransmitContext *tc = cls;
244 struct GNUNET_SENSOR_SensorInfo *sensorinfo = value;
245 struct SensorInfoMessage *msg;
247 msg = create_sensor_info_msg (sensorinfo);
248 GNUNET_SERVER_transmit_context_append_message (tc,
249 (struct GNUNET_MessageHeader *)
258 * Handle GET ALL SENSORS message.
261 * @param client identification of the client
262 * @param message the actual message
265 handle_get_all_sensors (void *cls, struct GNUNET_SERVER_Client *client,
266 const struct GNUNET_MessageHeader *message)
268 struct GNUNET_SERVER_TransmitContext *tc;
270 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received.\n",
272 tc = GNUNET_SERVER_transmit_context_create (client);
273 GNUNET_CONTAINER_multihashmap_iterate (sensors, &add_sensor_to_tc, tc);
274 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
275 GNUNET_MESSAGE_TYPE_SENSOR_END);
276 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
281 * Do a series of checks to determine if sensor should execute
283 * @return #GNUNET_YES / #GNUNET_NO
286 should_run_sensor (struct GNUNET_SENSOR_SensorInfo *sensorinfo)
288 struct GNUNET_TIME_Absolute now;
290 if (GNUNET_NO == sensorinfo->enabled)
292 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
293 "Sensor `%s' is disabled, will not run\n", sensorinfo->name);
296 now = GNUNET_TIME_absolute_get ();
297 if (NULL != sensorinfo->start_time &&
298 now.abs_value_us < sensorinfo->start_time->abs_value_us)
300 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
301 "Start time for sensor `%s' not reached yet, will not run\n",
305 if (NULL != sensorinfo->end_time &&
306 now.abs_value_us >= sensorinfo->end_time->abs_value_us)
308 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Sensor `%s' expired, disabling.\n",
310 set_sensor_enabled (sensorinfo, GNUNET_NO);
318 * Callback function to process statistic values
320 * @param cls `struct GNUNET_SENSOR_SensorInfo *`
321 * @param ss name of subsystem that created the statistic
322 * @param name the name of the datum
323 * @param value the current value
324 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
325 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
328 sensor_statistics_iterator (void *cls, const char *ss, const char *name,
329 uint64_t value, int is_persistent)
331 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
332 double dvalue = (double) value;
333 struct GNUNET_TIME_Absolute expiry;
335 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
336 "Received a value for sensor `%s': %" PRIu64 "\n",
337 sensorinfo->name, value);
338 expiry = GNUNET_TIME_relative_to_absolute (sensorinfo->lifetime);
339 GNUNET_PEERSTORE_store (peerstore, subsystem, &peerid, sensorinfo->name,
340 &dvalue, sizeof (dvalue), expiry,
341 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, NULL, NULL);
342 return GNUNET_SYSERR; /* We only want one value */
347 * Continuation called after sensor gets all gnunet statistics values
349 * @param cls `struct GNUNET_SENSOR_SensorInfo *`
350 * @param success #GNUNET_OK if statistics were
351 * successfully obtained, #GNUNET_SYSERR if not.
354 end_sensor_run_stat (void *cls, int success)
356 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
358 sensorinfo->gnunet_stat_get_handle = NULL;
359 sensorinfo->running = GNUNET_NO;
364 * Tries to parse a received sensor value to its
367 * @param value the string value received, should be null terminated
368 * @param sensor sensor information struct
369 * @param ret pointer to parsed value
370 * @return size of new parsed value, 0 for error
373 parse_sensor_value (const char *value, struct GNUNET_SENSOR_SensorInfo *sensor,
382 if (0 == strcmp ("numeric", sensor->expected_datatype))
384 dval = GNUNET_new (double);
386 *dval = strtod (value, &endptr);
390 return sizeof (double);
392 if (0 == strcmp ("string", sensor->expected_datatype))
394 *ret = GNUNET_strdup (value);
395 return strlen (value) + 1;
397 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
399 ("Unknown value type expected by sensor, this should not happen.\n"));
405 * Callback for output of executed sensor process
407 * @param cls `struct GNUNET_SENSOR_SensorInfo *`
408 * @param line line of output from a command, NULL for the end
411 sensor_process_callback (void *cls, const char *line)
413 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
416 struct GNUNET_TIME_Absolute expiry;
420 GNUNET_OS_command_stop (sensorinfo->ext_cmd);
421 sensorinfo->ext_cmd = NULL;
422 sensorinfo->running = GNUNET_NO;
423 sensorinfo->ext_cmd_value_received = GNUNET_NO;
426 if (GNUNET_YES == sensorinfo->ext_cmd_value_received)
427 return; /* We only want one *valid* value */
428 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %s\n",
429 sensorinfo->name, line);
430 valsize = parse_sensor_value (line, sensorinfo, &value);
431 if (valsize == 0) /* invalid value, FIXME: should we disable the sensor now? */
433 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
434 _("Received an invalid value for sensor `%s': %s\n"),
435 sensorinfo->name, line);
439 sensorinfo->ext_cmd_value_received = GNUNET_YES;
440 expiry = GNUNET_TIME_relative_to_absolute (sensorinfo->lifetime);
441 GNUNET_PEERSTORE_store (peerstore, subsystem, &peerid, sensorinfo->name,
442 value, valsize, expiry,
443 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, NULL, NULL);
450 * Checks if the given file is a path
452 * @return #GNUNET_YES / #GNUNET_NO
455 is_path (char *filename)
460 filename_len = strlen (filename);
461 for (i = 0; i < filename_len; i++)
463 if (DIR_SEPARATOR == filename[i])
471 * Actual execution of a sensor
473 * @param cls 'struct SensorInfo'
477 sensor_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
479 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
484 sensorinfo->execution_task =
485 GNUNET_SCHEDULER_add_delayed (sensorinfo->interval, &sensor_run,
487 if (GNUNET_YES == sensorinfo->running) //FIXME: should we try to kill?
489 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
490 "Sensor `%s' running for too long, will try again next interval\n",
494 if (GNUNET_NO == should_run_sensor (sensorinfo))
496 sensorinfo->running = GNUNET_YES;
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Starting the execution of sensor `%s'\n", sensorinfo->name);
499 if (0 == strcmp ("gnunet-statistics", sensorinfo->source))
501 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
502 &end_sensor_run_stat,
503 &sensor_statistics_iterator,
506 else if (0 == strcmp ("process", sensorinfo->source))
508 if (GNUNET_YES == is_path (sensorinfo->ext_process))
510 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
512 ("Sensor `%s': External process should not be a path, disabling sensor.\n"),
514 set_sensor_enabled (sensorinfo, GNUNET_NO);
517 //check if the process exists in $PATH
518 process_path = GNUNET_strdup (sensorinfo->ext_process);
520 GNUNET_OS_check_helper_binary (process_path, GNUNET_NO, NULL);
521 if (GNUNET_SYSERR == check_result)
523 //search in sensor directory
524 sensors_dir = GNUNET_SENSOR_get_sensor_dir ();
525 GNUNET_free (process_path);
526 GNUNET_asprintf (&process_path, "%s%s-files%s%s", sensors_dir,
527 sensorinfo->name, DIR_SEPARATOR_STR,
528 sensorinfo->ext_process);
529 GNUNET_free (sensors_dir);
531 GNUNET_OS_check_helper_binary (process_path, GNUNET_NO, NULL);
533 if (GNUNET_SYSERR == check_result)
535 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
537 ("Sensor `%s' process `%s' problem: binary doesn't exist or not executable\n"),
538 sensorinfo->name, sensorinfo->ext_process);
539 set_sensor_enabled (sensorinfo, GNUNET_NO);
540 sensorinfo->running = GNUNET_NO;
541 GNUNET_free (process_path);
544 sensorinfo->ext_cmd_value_received = GNUNET_NO;
545 sensorinfo->ext_cmd =
546 GNUNET_OS_command_run (&sensor_process_callback, sensorinfo,
547 GNUNET_TIME_UNIT_FOREVER_REL, process_path,
548 sensorinfo->ext_process, sensorinfo->ext_args,
550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Process started for sensor `%s'\n",
552 GNUNET_free (process_path);
556 sensorinfo->running = GNUNET_NO;
557 GNUNET_break (0); /* shouldn't happen */
563 * Starts the execution of a sensor
566 * @param key hash of sensor name, key to hashmap (unused)
567 * @param value a `struct GNUNET_SENSOR_SensorInfo *`
568 * @return #GNUNET_YES if we should continue to
573 schedule_sensor (void *cls, const struct GNUNET_HashCode *key, void *value)
575 struct GNUNET_SENSOR_SensorInfo *sensorinfo = value;
577 if (GNUNET_NO == should_run_sensor (sensorinfo))
579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
580 "Scheduling sensor `%s' to run after %" PRIu64 " microseconds\n",
581 sensorinfo->name, sensorinfo->interval.rel_value_us);
582 if (GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
584 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
586 ("Sensor `%s' execution task already set, this should not happen\n"),
590 sensorinfo->execution_task =
591 GNUNET_SCHEDULER_add_delayed (sensorinfo->interval, &sensor_run,
598 * Starts the execution of all enabled sensors
601 schedule_all_sensors ()
603 GNUNET_CONTAINER_multihashmap_iterate (sensors, &schedule_sensor, NULL);
608 * Loads sensors and starts different service components
613 sensors = GNUNET_SENSOR_load_all_sensors ();
614 schedule_all_sensors ();
615 SENSOR_analysis_start (cfg, sensors);
616 SENSOR_reporting_start (cfg, sensors);
617 SENSOR_update_start (cfg, sensors, &reset);
622 * Process statistics requests.
625 * @param server the initialized server
626 * @param c configuration to use
629 run (void *cls, struct GNUNET_SERVER_Handle *server,
630 const struct GNUNET_CONFIGURATION_Handle *c)
632 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
633 {&handle_get_sensor, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GET,
635 {&handle_get_all_sensors, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GETALL,
636 sizeof (struct GNUNET_MessageHeader)},
641 statistics = GNUNET_STATISTICS_create ("sensor", cfg);
642 GNUNET_CRYPTO_get_peer_identity (cfg, &peerid);
643 peerstore = GNUNET_PEERSTORE_connect (cfg);
644 GNUNET_SERVER_add_handlers (server, handlers);
645 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
646 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
653 * Resets the service by stopping components, reloading sensors and starting
654 * components. This is needed when we receive new sensor updates.
665 * The main function for the sensor service.
667 * @param argc number of arguments from the command line
668 * @param argv command line arguments
669 * @return 0 ok, 1 on error
672 main (int argc, char *const *argv)
675 GNUNET_SERVICE_run (argc, argv, "sensor", GNUNET_SERVICE_OPTION_NONE,
676 &run, NULL)) ? 0 : 1;
679 /* end of gnunet-service-sensor.c */