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 * Sensor will not run again unless
66 * explicitly enabled or reloaded
68 * @param sensor sensor information
70 static void set_sensor_enabled(struct SensorInfo *sensor, int state)
72 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
73 "Sensor `%s': Setting enabled to %d.\n",
75 sensor->enabled = GNUNET_NO;
76 GNUNET_assert(NULL != sensor->cfg);
77 GNUNET_CONFIGURATION_set_value_string(sensor->cfg, sensor->name, "ENABLED",
78 (GNUNET_YES == state)?"YES":"NO");
79 GNUNET_CONFIGURATION_write(sensor->cfg, sensor->def_file);
83 * Task run during shutdown.
89 shutdown_task (void *cls,
90 const struct GNUNET_SCHEDULER_TaskContext *tc)
92 SENSOR_update_stop ();
93 SENSOR_reporting_stop();
94 SENSOR_analysis_stop();
95 GNUNET_SENSOR_destroy_sensors (sensors);
96 if(NULL != statistics)
98 GNUNET_STATISTICS_destroy(statistics, GNUNET_YES);
101 if(NULL != peerstore)
103 GNUNET_PEERSTORE_disconnect(peerstore, GNUNET_YES);
106 GNUNET_SCHEDULER_shutdown();
111 * A client disconnected. Remove all of its data structure entries.
113 * @param cls closure, NULL
114 * @param client identification of the client
117 handle_client_disconnect (void *cls,
118 struct GNUNET_SERVER_Client
124 * Creates a structure with basic sensor info to be sent to a client
126 * @param sensor sensor information
127 * @return message ready to be sent to client
129 static struct SensorInfoMessage *
130 create_sensor_info_msg(struct SensorInfo *sensor)
132 struct SensorInfoMessage *msg;
138 name_len = strlen(sensor->name);
139 if(NULL == sensor->description)
142 desc_len = strlen(sensor->description) + 1;
144 len += sizeof(struct SensorInfoMessage);
147 msg = GNUNET_malloc(len);
148 msg->header.size = htons(len);
149 msg->header.type = htons(GNUNET_MESSAGE_TYPE_SENSOR_INFO);
150 msg->name_len = htons(name_len);
151 msg->description_len = htons(desc_len);
152 msg->version_major = htons(sensor->version_major);
153 msg->version_minor = htons(sensor->version_minor);
154 str_ptr = (char*) &msg[1];
155 memcpy(str_ptr, sensor->name, name_len);
156 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor name (%d): %.*s\n",
157 name_len, name_len, str_ptr);
159 memcpy(str_ptr, sensor->description, desc_len);
160 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor description (%d): %.*s\n",
161 desc_len, desc_len, str_ptr);
167 * Handle GET SENSOR message.
170 * @param client identification of the client
171 * @param message the actual message
174 handle_get_sensor (void *cls, struct GNUNET_SERVER_Client *client,
175 const struct GNUNET_MessageHeader *message)
177 struct GNUNET_SERVER_TransmitContext *tc;
179 size_t sensorname_len;
180 struct GNUNET_HashCode key;
181 struct SensorInfo *sensorinfo;
182 struct SensorInfoMessage *msg;
184 sensorname = (char *)&message[1];
185 sensorname_len = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
186 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received for sensor (%d) `%.*s'\n",
187 "GET SENSOR", sensorname_len, sensorname_len, sensorname);
188 tc = GNUNET_SERVER_transmit_context_create (client);
189 GNUNET_CRYPTO_hash(sensorname, sensorname_len, &key);
190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created key hash for requested sensor\n");
191 sensorinfo = (struct SensorInfo *)GNUNET_CONTAINER_multihashmap_get(sensors, &key);
192 if(NULL != sensorinfo)
194 msg = create_sensor_info_msg(sensorinfo);
195 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
199 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Requested sensor `%.*s' was not found\n",
200 sensorname_len, sensorname);
201 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
202 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
206 * Iterator for sensors and adds them to transmit context
208 * @param cls a 'struct GNUNET_SERVER_TransmitContext *'
209 * @param key hash of sensor name, key to hashmap
210 * @param value a 'struct SensorInfo *'
212 int add_sensor_to_tc(void *cls,
213 const struct GNUNET_HashCode *key, void *value)
215 struct GNUNET_SERVER_TransmitContext *tc = cls;
216 struct SensorInfo *sensorinfo = value;
217 struct SensorInfoMessage *msg;
219 msg = create_sensor_info_msg(sensorinfo);
220 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
228 * Handle GET ALL SENSORS message.
231 * @param client identification of the client
232 * @param message the actual message
235 handle_get_all_sensors (void *cls, struct GNUNET_SERVER_Client *client,
236 const struct GNUNET_MessageHeader *message)
238 struct GNUNET_SERVER_TransmitContext *tc;
240 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received.\n",
242 tc = GNUNET_SERVER_transmit_context_create (client);
243 GNUNET_CONTAINER_multihashmap_iterate(sensors, &add_sensor_to_tc, tc);
244 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
245 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
249 * Do a series of checks to determine if sensor should execute
251 * @return #GNUNET_YES / #GNUNET_NO
254 should_run_sensor(struct SensorInfo *sensorinfo)
256 struct GNUNET_TIME_Absolute now;
258 if(GNUNET_NO == sensorinfo->enabled)
260 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' is disabled, will not run\n", sensorinfo->name);
263 now = GNUNET_TIME_absolute_get();
264 if(NULL != sensorinfo->start_time
265 && now.abs_value_us < sensorinfo->start_time->abs_value_us)
267 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Start time for sensor `%s' not reached yet, will not run\n", sensorinfo->name);
270 if(NULL != sensorinfo->end_time
271 && now.abs_value_us >= sensorinfo->end_time->abs_value_us)
273 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' expired, disabling.\n", sensorinfo->name);
274 set_sensor_enabled(sensorinfo, GNUNET_NO);
281 * Callback function to process statistic values
283 * @param cls 'struct SensorInfo *'
284 * @param subsystem name of subsystem that created the statistic
285 * @param name the name of the datum
286 * @param value the current value
287 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
288 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
290 int sensor_statistics_iterator (void *cls,
296 struct SensorInfo *sensorinfo = cls;
297 double dvalue = (double)value;
298 struct GNUNET_TIME_Absolute expiry;
300 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %" PRIu64 "\n", sensorinfo->name, value);
301 expiry = GNUNET_TIME_relative_to_absolute(sensorinfo->lifetime);
302 GNUNET_PEERSTORE_store(peerstore,
309 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
312 return GNUNET_SYSERR; /* We only want one value */
316 * Continuation called after sensor gets all gnunet statistics values
318 * @param cls 'struct SensorInfo *'
319 * @param success #GNUNET_OK if statistics were
320 * successfully obtained, #GNUNET_SYSERR if not.
322 void end_sensor_run_stat (void *cls, int success)
324 struct SensorInfo *sensorinfo = cls;
326 sensorinfo->gnunet_stat_get_handle = NULL;
327 sensorinfo->running = GNUNET_NO;
331 * Tries to parse a received sensor value to its
334 * @param value the string value received, should be null terminated
335 * @param sensor sensor information struct
336 * @param ret pointer to parsed value
337 * @return size of new parsed value, 0 for error
340 parse_sensor_value (const char *value, struct SensorInfo* sensor, void **ret)
348 if(0 == strcmp("numeric", sensor->expected_datatype))
350 dval = GNUNET_new(double);
351 *dval = strtod(value, &endptr);
355 return sizeof(double);
357 if(0 == strcmp("string", sensor->expected_datatype))
359 *ret = GNUNET_strdup(value);
360 return strlen(value) + 1;
362 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
363 _("Unknown value type expected by sensor, this should not happen.\n"));
368 * Callback for output of executed sensor process
370 * @param cls 'struct SensorInfo *'
371 * @param line line of output from a command, NULL for the end
373 void sensor_process_callback (void *cls, const char *line)
375 struct SensorInfo *sensorinfo = cls;
378 struct GNUNET_TIME_Absolute expiry;
382 GNUNET_OS_command_stop(sensorinfo->ext_cmd);
383 sensorinfo->ext_cmd = NULL;
384 sensorinfo->running = GNUNET_NO;
385 sensorinfo->ext_cmd_value_received = GNUNET_NO;
388 if(GNUNET_YES == sensorinfo->ext_cmd_value_received)
389 return; /* We only want one *valid* value */
390 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %s\n", sensorinfo->name, line);
391 valsize = parse_sensor_value(line, sensorinfo, &value);
392 if (valsize == 0) /* invalid value, FIXME: should we disable the sensor now? */
394 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
395 _("Received an invalid value for sensor `%s': %s\n"),
396 sensorinfo->name, line);
400 sensorinfo->ext_cmd_value_received = GNUNET_YES;
401 expiry = GNUNET_TIME_relative_to_absolute(sensorinfo->lifetime);
402 GNUNET_PEERSTORE_store(peerstore,
409 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
417 * Checks if the given file is a path
419 * @return #GNUNET_YES / #GNUNET_NO
422 is_path(char *filename)
427 filename_len = strlen(filename);
428 for(i = 0; i < filename_len; i++)
430 if(DIR_SEPARATOR == filename[i])
437 * Actual execution of a sensor
439 * @param cls 'struct SensorInfo'
443 sensor_run (void *cls,
444 const struct GNUNET_SCHEDULER_TaskContext * tc)
446 struct SensorInfo *sensorinfo = cls;
451 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
452 if(GNUNET_YES == sensorinfo->running) //FIXME: should we try to kill?
454 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sensor `%s' running for too long, will try again next interval\n", sensorinfo->name);
457 if(GNUNET_NO == should_run_sensor(sensorinfo))
459 sensorinfo->running = GNUNET_YES;
460 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting the execution of sensor `%s'\n", sensorinfo->name);
461 if(0 == strcmp ("gnunet-statistics", sensorinfo->source))
463 sensorinfo->gnunet_stat_get_handle = GNUNET_STATISTICS_get(statistics,
464 sensorinfo->gnunet_stat_service,
465 sensorinfo->gnunet_stat_name,
466 sensorinfo->interval, //try to get values only for the interval of the sensor
467 &end_sensor_run_stat,
468 &sensor_statistics_iterator,
471 else if(0 == strcmp ("process", sensorinfo->source))
473 if(GNUNET_YES == is_path(sensorinfo->ext_process))
475 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
476 _("Sensor `%s': External process should not be a path, disabling sensor.\n"),
478 set_sensor_enabled(sensorinfo, GNUNET_NO);
481 //check if the process exists in $PATH
482 process_path = GNUNET_strdup(sensorinfo->ext_process);
484 GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
485 if(GNUNET_SYSERR == check_result)
487 //search in sensor directory
488 sensors_dir = GNUNET_SENSOR_get_sensor_dir ();
489 GNUNET_free(process_path);
490 GNUNET_asprintf(&process_path, "%s%s-files%s%s",
494 sensorinfo->ext_process);
495 GNUNET_free(sensors_dir);
497 GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
499 if(GNUNET_SYSERR == check_result)
501 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
502 _("Sensor `%s' process `%s' problem: binary doesn't exist or not executable\n"),
504 sensorinfo->ext_process);
505 set_sensor_enabled(sensorinfo, GNUNET_NO);
506 sensorinfo->running = GNUNET_NO;
507 GNUNET_free(process_path);
510 sensorinfo->ext_cmd_value_received = GNUNET_NO;
511 sensorinfo->ext_cmd = GNUNET_OS_command_run(&sensor_process_callback,
513 GNUNET_TIME_UNIT_FOREVER_REL,
515 sensorinfo->ext_process,
516 sensorinfo->ext_args,
518 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Process started for sensor `%s'\n", sensorinfo->name);
519 GNUNET_free(process_path);
523 sensorinfo->running = GNUNET_NO;
524 GNUNET_break(0); //shouldn't happen
529 * Starts the execution of a sensor
532 * @param key hash of sensor name, key to hashmap (unused)
533 * @param value a 'struct SensorInfo *'
534 * @return #GNUNET_YES if we should continue to
538 int schedule_sensor(void *cls,
539 const struct GNUNET_HashCode *key, void *value)
541 struct SensorInfo *sensorinfo = value;
543 if(GNUNET_NO == should_run_sensor(sensorinfo))
545 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling sensor `%s' to run after %" PRIu64 " microseconds\n",
546 sensorinfo->name, sensorinfo->interval.rel_value_us);
547 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
549 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
550 _("Sensor `%s' execution task already set, this should not happen\n"), sensorinfo->name);
553 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
558 * Starts the execution of all enabled sensors
562 schedule_all_sensors()
564 GNUNET_CONTAINER_multihashmap_iterate(sensors, &schedule_sensor, NULL);
568 * Process statistics requests.
571 * @param server the initialized server
572 * @param c configuration to use
576 struct GNUNET_SERVER_Handle *server,
577 const struct GNUNET_CONFIGURATION_Handle *c)
579 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
580 {&handle_get_sensor, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GET,
582 {&handle_get_all_sensors, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GETALL,
583 sizeof (struct GNUNET_MessageHeader)},
588 sensors = GNUNET_SENSOR_load_all_sensors ();
589 schedule_all_sensors();
590 SENSOR_analysis_start(c, sensors);
591 SENSOR_reporting_start(c, sensors);
592 SENSOR_update_start (c, sensors);
593 statistics = GNUNET_STATISTICS_create("sensor", cfg);
594 GNUNET_CRYPTO_get_peer_identity(cfg, &peerid);
595 peerstore = GNUNET_PEERSTORE_connect(cfg);
596 GNUNET_SERVER_add_handlers (server, handlers);
597 GNUNET_SERVER_disconnect_notify (server,
598 &handle_client_disconnect,
600 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
607 * The main function for the sensor service.
609 * @param argc number of arguments from the command line
610 * @param argv command line arguments
611 * @return 0 ok, 1 on error
614 main (int argc, char *const *argv)
617 GNUNET_SERVICE_run (argc,
620 GNUNET_SERVICE_OPTION_NONE,
621 &run, NULL)) ? 0 : 1;
624 /* end of gnunet-service-sensor.c */