diff options
author | Omar Tarabai <tarabai@devegypt.com> | 2014-05-28 16:51:33 +0000 |
---|---|---|
committer | Omar Tarabai <tarabai@devegypt.com> | 2014-05-28 16:51:33 +0000 |
commit | 3663cace0b209eef3d7c4ed2e852e3548cf5ba38 (patch) | |
tree | c9245bd89929ddb196f9b89efb9a43eaf930571e /src/sensor | |
parent | e0321a814876233059f06587639c4d72842f5436 (diff) | |
download | gnunet-3663cace0b209eef3d7c4ed2e852e3548cf5ba38.tar.gz gnunet-3663cace0b209eef3d7c4ed2e852e3548cf5ba38.zip |
sensor: disabling sensors
Diffstat (limited to 'src/sensor')
-rw-r--r-- | src/sensor/gnunet-service-sensor.c | 100 |
1 files changed, 85 insertions, 15 deletions
diff --git a/src/sensor/gnunet-service-sensor.c b/src/sensor/gnunet-service-sensor.c index 4c5a66703..6d47cb1e2 100644 --- a/src/sensor/gnunet-service-sensor.c +++ b/src/sensor/gnunet-service-sensor.c | |||
@@ -40,6 +40,12 @@ | |||
40 | struct SensorInfo | 40 | struct SensorInfo |
41 | { | 41 | { |
42 | 42 | ||
43 | /** | ||
44 | * The configuration handle | ||
45 | * carrying sensor information | ||
46 | */ | ||
47 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
48 | |||
43 | /* | 49 | /* |
44 | * Sensor name | 50 | * Sensor name |
45 | */ | 51 | */ |
@@ -131,6 +137,11 @@ struct SensorInfo | |||
131 | char *ext_args; | 137 | char *ext_args; |
132 | 138 | ||
133 | /* | 139 | /* |
140 | * Handle to the external process | ||
141 | */ | ||
142 | struct GNUNET_OS_CommandHandle *ext_cmd; | ||
143 | |||
144 | /* | ||
134 | * The output datatype to be expected | 145 | * The output datatype to be expected |
135 | */ | 146 | */ |
136 | char *expected_datatype; | 147 | char *expected_datatype; |
@@ -202,23 +213,29 @@ struct GNUNET_STATISTICS_Handle *statistics; | |||
202 | * iterate, | 213 | * iterate, |
203 | * #GNUNET_NO if not. | 214 | * #GNUNET_NO if not. |
204 | */ | 215 | */ |
205 | int destroy_sensor(void *cls, | 216 | static int destroy_sensor(void *cls, |
206 | const struct GNUNET_HashCode *key, void *value) | 217 | const struct GNUNET_HashCode *key, void *value) |
207 | { | 218 | { |
208 | struct SensorInfo *sensorinfo = value; | 219 | struct SensorInfo *sensorinfo = value; |
209 | 220 | ||
210 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Destroying sensor `%s'\n", sensorinfo->name); | 221 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Destroying sensor `%s'\n", sensorinfo->name); |
222 | if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task) | ||
223 | { | ||
224 | GNUNET_SCHEDULER_cancel(sensorinfo->execution_task); | ||
225 | sensorinfo->execution_task = GNUNET_SCHEDULER_NO_TASK; | ||
226 | } | ||
211 | if(NULL != sensorinfo->gnunet_stat_get_handle) | 227 | if(NULL != sensorinfo->gnunet_stat_get_handle) |
212 | { | 228 | { |
213 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Canceling a statistics get request for sensor `%s'\n", sensorinfo->name); | ||
214 | GNUNET_STATISTICS_get_cancel(sensorinfo->gnunet_stat_get_handle); | 229 | GNUNET_STATISTICS_get_cancel(sensorinfo->gnunet_stat_get_handle); |
230 | sensorinfo->gnunet_stat_get_handle = NULL; | ||
215 | } | 231 | } |
216 | if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task) | 232 | if(NULL != sensorinfo->ext_cmd) |
217 | { | 233 | { |
218 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Unscheduling sensor `%s'\n", sensorinfo->name); | 234 | GNUNET_OS_command_stop(sensorinfo->ext_cmd); |
219 | GNUNET_SCHEDULER_cancel(sensorinfo->execution_task); | 235 | sensorinfo->ext_cmd = NULL; |
220 | sensorinfo->execution_task = GNUNET_SCHEDULER_NO_TASK; | ||
221 | } | 236 | } |
237 | if(NULL != sensorinfo->cfg) | ||
238 | GNUNET_CONFIGURATION_destroy(sensorinfo->cfg); | ||
222 | if(NULL != sensorinfo->name) | 239 | if(NULL != sensorinfo->name) |
223 | GNUNET_free(sensorinfo->name); | 240 | GNUNET_free(sensorinfo->name); |
224 | if(NULL != sensorinfo->def_file) | 241 | if(NULL != sensorinfo->def_file) |
@@ -242,6 +259,25 @@ int destroy_sensor(void *cls, | |||
242 | } | 259 | } |
243 | 260 | ||
244 | /** | 261 | /** |
262 | * Disable a sensor | ||
263 | * Sensor will not run again unless | ||
264 | * explicitly enabled or reloaded | ||
265 | * | ||
266 | * @param sensor sensor information | ||
267 | */ | ||
268 | static void set_sensor_enabled(struct SensorInfo *sensor, int state) | ||
269 | { | ||
270 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | ||
271 | "Sensor `%s': Setting enabled to %d.\n", | ||
272 | sensor->name, state); | ||
273 | sensor->enabled = GNUNET_NO; | ||
274 | GNUNET_assert(NULL != sensor->cfg); | ||
275 | GNUNET_CONFIGURATION_set_value_string(sensor->cfg, sensor->name, "ENABLED", | ||
276 | (GNUNET_YES == state)?"YES":"NO"); | ||
277 | GNUNET_CONFIGURATION_write(sensor->cfg, sensor->def_file); | ||
278 | } | ||
279 | |||
280 | /** | ||
245 | * Task run during shutdown. | 281 | * Task run during shutdown. |
246 | * | 282 | * |
247 | * @param cls unused | 283 | * @param cls unused |
@@ -473,9 +509,13 @@ load_sensor_from_file(const char *filename) | |||
473 | //configuration section should be the same as filename | 509 | //configuration section should be the same as filename |
474 | filebasename = GNUNET_STRINGS_get_short_name(filename); | 510 | filebasename = GNUNET_STRINGS_get_short_name(filename); |
475 | sensor = load_sensor_from_cfg(sensorcfg, filebasename); | 511 | sensor = load_sensor_from_cfg(sensorcfg, filebasename); |
512 | if(NULL == sensor) | ||
513 | { | ||
514 | GNUNET_CONFIGURATION_destroy(sensorcfg); | ||
515 | return NULL; | ||
516 | } | ||
476 | sensor->def_file = GNUNET_strdup(filename); | 517 | sensor->def_file = GNUNET_strdup(filename); |
477 | 518 | sensor->cfg = sensorcfg; | |
478 | GNUNET_CONFIGURATION_destroy(sensorcfg); | ||
479 | 519 | ||
480 | return sensor; | 520 | return sensor; |
481 | } | 521 | } |
@@ -735,7 +775,6 @@ handle_get_all_sensors (void *cls, struct GNUNET_SERVER_Client *client, | |||
735 | static int | 775 | static int |
736 | should_run_sensor(struct SensorInfo *sensorinfo) | 776 | should_run_sensor(struct SensorInfo *sensorinfo) |
737 | { | 777 | { |
738 | //FIXME: some checks should disable the sensor (e.g. expired) | ||
739 | struct GNUNET_TIME_Absolute now; | 778 | struct GNUNET_TIME_Absolute now; |
740 | 779 | ||
741 | if(GNUNET_NO == sensorinfo->enabled) | 780 | if(GNUNET_NO == sensorinfo->enabled) |
@@ -753,7 +792,8 @@ should_run_sensor(struct SensorInfo *sensorinfo) | |||
753 | if(NULL != sensorinfo->end_time | 792 | if(NULL != sensorinfo->end_time |
754 | && now.abs_value_us >= sensorinfo->end_time->abs_value_us) | 793 | && now.abs_value_us >= sensorinfo->end_time->abs_value_us) |
755 | { | 794 | { |
756 | GNUNET_log(GNUNET_ERROR_TYPE_INFO, "End time for sensor `%s' passed, will not run\n", sensorinfo->name); | 795 | GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' expired, disabling.\n", sensorinfo->name); |
796 | set_sensor_enabled(sensorinfo, GNUNET_NO); | ||
757 | return GNUNET_NO; | 797 | return GNUNET_NO; |
758 | } | 798 | } |
759 | return GNUNET_YES; | 799 | return GNUNET_YES; |
@@ -806,8 +846,10 @@ void sensor_process_callback (void *cls, const char *line) | |||
806 | { | 846 | { |
807 | struct SensorInfo *sensorinfo = cls; | 847 | struct SensorInfo *sensorinfo = cls; |
808 | 848 | ||
809 | if(NULL == line) | 849 | if(NULL == line) //end of output |
810 | { | 850 | { |
851 | GNUNET_OS_command_stop(sensorinfo->ext_cmd); | ||
852 | sensorinfo->ext_cmd = NULL; | ||
811 | sensorinfo->running = GNUNET_NO; | 853 | sensorinfo->running = GNUNET_NO; |
812 | return; | 854 | return; |
813 | } | 855 | } |
@@ -815,6 +857,26 @@ void sensor_process_callback (void *cls, const char *line) | |||
815 | } | 857 | } |
816 | 858 | ||
817 | /** | 859 | /** |
860 | * Checks if the given file is a path | ||
861 | * | ||
862 | * @return #GNUNET_YES / #GNUNET_NO | ||
863 | */ | ||
864 | static int | ||
865 | is_path(char *filename) | ||
866 | { | ||
867 | size_t filename_len; | ||
868 | int i; | ||
869 | |||
870 | filename_len = strlen(filename); | ||
871 | for(i = 0; i < filename_len; i++) | ||
872 | { | ||
873 | if(DIR_SEPARATOR == filename[i]) | ||
874 | return GNUNET_YES; | ||
875 | } | ||
876 | return GNUNET_NO; | ||
877 | } | ||
878 | |||
879 | /** | ||
818 | * Actual execution of a sensor | 880 | * Actual execution of a sensor |
819 | * | 881 | * |
820 | * @param cls 'struct SensorInfo' | 882 | * @param cls 'struct SensorInfo' |
@@ -855,11 +917,18 @@ sensor_run (void *cls, | |||
855 | } | 917 | } |
856 | else if(sources[1] == sensorinfo->source) | 918 | else if(sources[1] == sensorinfo->source) |
857 | { | 919 | { |
858 | //FIXME: break execution if process is a path | 920 | if(GNUNET_YES == is_path(sensorinfo->ext_process)) |
921 | { | ||
922 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, | ||
923 | "Sensor `%s': External process should not be a path, disabling sensor.\n", | ||
924 | sensorinfo->name); | ||
925 | set_sensor_enabled(sensorinfo, GNUNET_NO); | ||
926 | return; | ||
927 | } | ||
859 | //check if the process exists in $PATH | 928 | //check if the process exists in $PATH |
860 | process_path = GNUNET_strdup(sensorinfo->ext_process); | 929 | process_path = GNUNET_strdup(sensorinfo->ext_process); |
861 | check_result = | 930 | check_result = |
862 | GNUNET_OS_check_helper_binary(sensorinfo->ext_process, GNUNET_NO, NULL); //search in $PATH | 931 | GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL); |
863 | if(GNUNET_SYSERR == check_result) | 932 | if(GNUNET_SYSERR == check_result) |
864 | { | 933 | { |
865 | //search in sensor directory | 934 | //search in sensor directory |
@@ -879,12 +948,12 @@ sensor_run (void *cls, | |||
879 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sensor `%s' process `%s' problem: binary doesn't exist or not executable\n", | 948 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sensor `%s' process `%s' problem: binary doesn't exist or not executable\n", |
880 | sensorinfo->name, | 949 | sensorinfo->name, |
881 | sensorinfo->ext_process); | 950 | sensorinfo->ext_process); |
882 | //FIXME: disable sensor here? | 951 | set_sensor_enabled(sensorinfo, GNUNET_NO); |
883 | sensorinfo->running = GNUNET_NO; | 952 | sensorinfo->running = GNUNET_NO; |
884 | GNUNET_free(process_path); | 953 | GNUNET_free(process_path); |
885 | return; | 954 | return; |
886 | } | 955 | } |
887 | GNUNET_OS_command_run(&sensor_process_callback, | 956 | sensorinfo->ext_cmd = GNUNET_OS_command_run(&sensor_process_callback, |
888 | sensorinfo, | 957 | sensorinfo, |
889 | GNUNET_TIME_UNIT_FOREVER_REL, | 958 | GNUNET_TIME_UNIT_FOREVER_REL, |
890 | process_path, | 959 | process_path, |
@@ -892,6 +961,7 @@ sensor_run (void *cls, | |||
892 | sensorinfo->ext_args, | 961 | sensorinfo->ext_args, |
893 | NULL); | 962 | NULL); |
894 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Process started for sensor `%s'\n", sensorinfo->name); | 963 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Process started for sensor `%s'\n", sensorinfo->name); |
964 | GNUNET_free(process_path); | ||
895 | } | 965 | } |
896 | else | 966 | else |
897 | { | 967 | { |