[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r33438 - gnunet/src/sensor
From: |
gnunet |
Subject: |
[GNUnet-SVN] r33438 - gnunet/src/sensor |
Date: |
Wed, 28 May 2014 18:51:33 +0200 |
Author: otarabai
Date: 2014-05-28 18:51:33 +0200 (Wed, 28 May 2014)
New Revision: 33438
Modified:
gnunet/src/sensor/gnunet-service-sensor.c
Log:
sensor: disabling sensors
Modified: gnunet/src/sensor/gnunet-service-sensor.c
===================================================================
--- gnunet/src/sensor/gnunet-service-sensor.c 2014-05-28 14:55:36 UTC (rev
33437)
+++ gnunet/src/sensor/gnunet-service-sensor.c 2014-05-28 16:51:33 UTC (rev
33438)
@@ -40,6 +40,12 @@
struct SensorInfo
{
+ /**
+ * The configuration handle
+ * carrying sensor information
+ */
+ struct GNUNET_CONFIGURATION_Handle *cfg;
+
/*
* Sensor name
*/
@@ -131,6 +137,11 @@
char *ext_args;
/*
+ * Handle to the external process
+ */
+ struct GNUNET_OS_CommandHandle *ext_cmd;
+
+ /*
* The output datatype to be expected
*/
char *expected_datatype;
@@ -202,23 +213,29 @@
* iterate,
* #GNUNET_NO if not.
*/
-int destroy_sensor(void *cls,
+static int destroy_sensor(void *cls,
const struct GNUNET_HashCode *key, void *value)
{
struct SensorInfo *sensorinfo = value;
GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Destroying sensor `%s'\n",
sensorinfo->name);
+ if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
+ {
+ GNUNET_SCHEDULER_cancel(sensorinfo->execution_task);
+ sensorinfo->execution_task = GNUNET_SCHEDULER_NO_TASK;
+ }
if(NULL != sensorinfo->gnunet_stat_get_handle)
{
- GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Canceling a statistics get request
for sensor `%s'\n", sensorinfo->name);
GNUNET_STATISTICS_get_cancel(sensorinfo->gnunet_stat_get_handle);
+ sensorinfo->gnunet_stat_get_handle = NULL;
}
- if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
+ if(NULL != sensorinfo->ext_cmd)
{
- GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Unscheduling sensor `%s'\n",
sensorinfo->name);
- GNUNET_SCHEDULER_cancel(sensorinfo->execution_task);
- sensorinfo->execution_task = GNUNET_SCHEDULER_NO_TASK;
+ GNUNET_OS_command_stop(sensorinfo->ext_cmd);
+ sensorinfo->ext_cmd = NULL;
}
+ if(NULL != sensorinfo->cfg)
+ GNUNET_CONFIGURATION_destroy(sensorinfo->cfg);
if(NULL != sensorinfo->name)
GNUNET_free(sensorinfo->name);
if(NULL != sensorinfo->def_file)
@@ -242,6 +259,25 @@
}
/**
+ * Disable a sensor
+ * Sensor will not run again unless
+ * explicitly enabled or reloaded
+ *
+ * @param sensor sensor information
+ */
+static void set_sensor_enabled(struct SensorInfo *sensor, int state)
+{
+ GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+ "Sensor `%s': Setting enabled to %d.\n",
+ sensor->name, state);
+ sensor->enabled = GNUNET_NO;
+ GNUNET_assert(NULL != sensor->cfg);
+ GNUNET_CONFIGURATION_set_value_string(sensor->cfg, sensor->name, "ENABLED",
+ (GNUNET_YES == state)?"YES":"NO");
+ GNUNET_CONFIGURATION_write(sensor->cfg, sensor->def_file);
+}
+
+/**
* Task run during shutdown.
*
* @param cls unused
@@ -473,10 +509,14 @@
//configuration section should be the same as filename
filebasename = GNUNET_STRINGS_get_short_name(filename);
sensor = load_sensor_from_cfg(sensorcfg, filebasename);
+ if(NULL == sensor)
+ {
+ GNUNET_CONFIGURATION_destroy(sensorcfg);
+ return NULL;
+ }
sensor->def_file = GNUNET_strdup(filename);
+ sensor->cfg = sensorcfg;
- GNUNET_CONFIGURATION_destroy(sensorcfg);
-
return sensor;
}
@@ -735,7 +775,6 @@
static int
should_run_sensor(struct SensorInfo *sensorinfo)
{
- //FIXME: some checks should disable the sensor (e.g. expired)
struct GNUNET_TIME_Absolute now;
if(GNUNET_NO == sensorinfo->enabled)
@@ -753,7 +792,8 @@
if(NULL != sensorinfo->end_time
&& now.abs_value_us >= sensorinfo->end_time->abs_value_us)
{
- GNUNET_log(GNUNET_ERROR_TYPE_INFO, "End time for sensor `%s' passed, will
not run\n", sensorinfo->name);
+ GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' expired, disabling.\n",
sensorinfo->name);
+ set_sensor_enabled(sensorinfo, GNUNET_NO);
return GNUNET_NO;
}
return GNUNET_YES;
@@ -806,8 +846,10 @@
{
struct SensorInfo *sensorinfo = cls;
- if(NULL == line)
+ if(NULL == line) //end of output
{
+ GNUNET_OS_command_stop(sensorinfo->ext_cmd);
+ sensorinfo->ext_cmd = NULL;
sensorinfo->running = GNUNET_NO;
return;
}
@@ -815,6 +857,26 @@
}
/**
+ * Checks if the given file is a path
+ *
+ * @return #GNUNET_YES / #GNUNET_NO
+ */
+static int
+is_path(char *filename)
+{
+ size_t filename_len;
+ int i;
+
+ filename_len = strlen(filename);
+ for(i = 0; i < filename_len; i++)
+ {
+ if(DIR_SEPARATOR == filename[i])
+ return GNUNET_YES;
+ }
+ return GNUNET_NO;
+}
+
+/**
* Actual execution of a sensor
*
* @param cls 'struct SensorInfo'
@@ -855,11 +917,18 @@
}
else if(sources[1] == sensorinfo->source)
{
- //FIXME: break execution if process is a path
+ if(GNUNET_YES == is_path(sensorinfo->ext_process))
+ {
+ GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
+ "Sensor `%s': External process should not be a path, disabling
sensor.\n",
+ sensorinfo->name);
+ set_sensor_enabled(sensorinfo, GNUNET_NO);
+ return;
+ }
//check if the process exists in $PATH
process_path = GNUNET_strdup(sensorinfo->ext_process);
check_result =
- GNUNET_OS_check_helper_binary(sensorinfo->ext_process, GNUNET_NO,
NULL); //search in $PATH
+ GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
if(GNUNET_SYSERR == check_result)
{
//search in sensor directory
@@ -879,12 +948,12 @@
GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sensor `%s' process `%s' problem:
binary doesn't exist or not executable\n",
sensorinfo->name,
sensorinfo->ext_process);
- //FIXME: disable sensor here?
+ set_sensor_enabled(sensorinfo, GNUNET_NO);
sensorinfo->running = GNUNET_NO;
GNUNET_free(process_path);
return;
}
- GNUNET_OS_command_run(&sensor_process_callback,
+ sensorinfo->ext_cmd = GNUNET_OS_command_run(&sensor_process_callback,
sensorinfo,
GNUNET_TIME_UNIT_FOREVER_REL,
process_path,
@@ -892,6 +961,7 @@
sensorinfo->ext_args,
NULL);
GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Process started for sensor `%s'\n",
sensorinfo->name);
+ GNUNET_free(process_path);
}
else
{
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r33438 - gnunet/src/sensor,
gnunet <=