/[pdpsoft]/trunk/grid-mw-security/ees/src/eval_man/pdl.c
ViewVC logotype

Contents of /trunk/grid-mw-security/ees/src/eval_man/pdl.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1069 - (show annotations) (download) (as text)
Mon Nov 9 13:34:12 2009 UTC (12 years, 2 months ago) by aramv
File MIME type: text/x-chdr
File size: 6931 byte(s)
Added logic to clean up parser memory when using older versions of (f)lex
1 #include "pdl.h"
2
3 /* Lex/Yacc stuff */
4 extern FILE * yyin;
5 extern int yylex_destroy(void);
6 extern int yyparse(void);
7 extern int yylex(void);
8 extern int yy_delete_buffer(YY_BUFFER_STATE);
9 extern void delete_lex_buffer(void);
10
11 /* internal data */
12 static char* _pdl_path;
13 FILE* config_file;
14 static var_t* plugin_list;
15 static var_t* plugin_list_last;
16 static var_t* current_plugin;
17 EES_RC config_file_ok;
18
19 EES_RC pdl_init(const char* name){
20 if((config_file = fopen(name,"r")) != NULL){
21 yyin = config_file;
22 yyparse();
23 if(config_file_ok){
24 initialize_plugins();
25 }
26 return EES_SUCCESS;
27 } else {
28 eef_log(LOG_ERR, "Failed to open policy file");
29 return EES_FAILURE;
30 }
31 }
32
33 record_t* concat_strings(record_t* r1, record_t* r2){
34 record_t* new_record;
35 eef_log(LOG_DEBUG, "Concating: %s with %s\n", r1->string, r2->string);
36 if((new_record = malloc(sizeof(record_t)))){
37 if((new_record->string =
38 calloc(1, sizeof(char)*(strlen(r1->string) + strlen(r2->string)+1)))){
39 strncat(new_record->string, r1->string, strlen(r1->string));
40 strncat(new_record->string, r2->string, strlen(r2->string));
41 free(r1->string);
42 free(r1);
43 free(r2->string);
44 free(r2);
45 return new_record;
46 }
47 }
48 return NULL;
49 }
50
51 record_t* concat_strings_with_space(record_t *r1, record_t* r2){
52 record_t *r;
53 eef_log(LOG_DEBUG, "Concating: %s with %s with spaces\n", r1->string, r2->string);
54 if((r = malloc(sizeof(record_t)))){
55 r->string = calloc(1,(sizeof(char)*(strlen(r1->string)+strlen(r2->string)+2)));
56 strncat(r->string, r1->string, strlen(r1->string));
57 strncat(r->string, " ", 1);
58 strncat(r->string, r2->string, strlen(r2->string));
59 free(r1->string);
60 free(r1);
61 free(r2->string);
62 free(r2);
63 return r;
64 }
65 return NULL;
66 }
67
68 int yyerror(char* string){
69 eef_log(LOG_ERR, string);
70 return 0;
71 }
72
73 void set_path(record_t* path){
74 size_t path_size = 0;
75 size_t string_size = (sizeof(char) * (strlen(path->string)+2)); /* size of string + extra slash + null byte */
76 _pdl_path = "";
77
78 /*struct stat sb;*/
79 /*if stat(MODUL*/
80
81 eef_log(LOG_DEBUG, "Found a new path: %s\n", path->string);
82 if(string_size < FILENAME_MAX){
83 path_size = string_size;
84 } else {
85 path_size = FILENAME_MAX;
86 }
87
88 eef_log(LOG_DEBUG,"Modules path: %s\n", path->string);
89
90 if((_pdl_path = calloc(1, path_size))){
91 strncpy(_pdl_path, path->string, path_size);
92 strncat(_pdl_path, "/", 1);
93 }
94 free(path->string);
95 free(path);
96 }
97
98 void add_policy(record_t* policy, rule_t* rule){
99 eef_log(LOG_DEBUG, "Found a new policy: %s\n", policy->string);
100 free(policy->string);
101 free(policy);
102
103 /*free(rule->true_branch);*/
104 /*free(rule->false_branch);*/
105 /*free(rule);*/
106 }
107
108 const char* get_pdl_path(){
109 if(_pdl_path == NULL){
110 return "";
111 }
112 return _pdl_path;
113 }
114
115 rule_t* add_rule(record_t* rule, record_t* true_branch, record_t* false_branch){
116 rule_t* new_rule = malloc(sizeof(rule_t));
117 if(rule != NULL){
118 eef_log(LOG_DEBUG, "Added a new rule: %s\n", rule->string);
119 }
120 if(true_branch != NULL){
121 eef_log(LOG_DEBUG, "True branch: %s\n", true_branch->string);
122 }
123 if(false_branch != NULL){
124 eef_log(LOG_DEBUG, "False branch: %s\n", false_branch->string);
125 }
126 if(rule != NULL){
127 free(rule->string);
128 }
129 free(rule);
130 if(true_branch != NULL){
131 free(true_branch->string);
132 }
133 free(true_branch);
134 if(false_branch != NULL){
135 free(false_branch->string);
136 }
137 free(false_branch);
138 free(new_rule);
139 return new_rule;
140 }
141
142 void remove_policy(record_t* policy){
143 eef_log(LOG_DEBUG, "Deleted policy: %s\n", policy->string);
144 free(policy->string);
145 free(policy);
146 }
147
148 char** _var_to_argv(char* value, int *argc){
149 char *start_of_arg = NULL, *copy_of_value = NULL, **argv = NULL;
150 char *delimiters = " \t";
151 size_t size_of_arg = 0, size_of_array, i = 0;
152 char *str_ptr;
153 copy_of_value = strdup(value);
154 size_of_array = (sizeof(char)*(strlen(copy_of_value)+1));
155 if((argv = calloc(1, size_of_array)) != NULL){
156 start_of_arg = strtok_r(copy_of_value, delimiters, &str_ptr);
157 while(start_of_arg != NULL){
158 size_of_arg = (sizeof(char)*(strlen(start_of_arg)+1));
159 if((argv[i] = calloc(1, size_of_arg))){
160 memcpy(argv[i], start_of_arg, size_of_arg);
161 start_of_arg = strtok_r(NULL, delimiters, &str_ptr);
162 i++;
163 }
164 }
165 }
166 free(copy_of_value);
167 if(i < ARG_MAX){
168 *argc = i;
169 } else {
170 *argc = ARG_MAX;
171 }
172 return argv;
173 }
174
175 void add_variable(record_t* name, record_t* value){
176 eef_log(LOG_DEBUG, "Added variable name: %s\n", name->string);
177 eef_log(LOG_DEBUG, "Added variable value: %s\n", value->string);
178
179 /* allocate and fill fields */
180
181 if((current_plugin = malloc(sizeof(var_t)))){
182 current_plugin->lineno = name->lineno;
183 current_plugin->name = name->string;
184 current_plugin->value = value->string;
185 current_plugin->next = NULL;
186 } else {
187 eef_log(LOG_ERR, "Out of memory!");
188 }
189
190 /* append to the end of the list */
191
192 if(plugin_list){
193 plugin_list_last->next = current_plugin;
194 } else {
195 plugin_list = current_plugin;
196 }
197 plugin_list_last = current_plugin;
198
199 /* clean up */
200
201 free(name->string);
202 free(name);
203 free(value->string);
204 free(value);
205 }
206
207 EES_RC initialize_plugins(){
208 char** argv;
209 int argc;
210
211 var_t* last_plugin = NULL;
212 current_plugin = plugin_list;
213 while(current_plugin && config_file_ok){
214 /* prepare arguments and add to plug-in manager */
215 argv = _var_to_argv(current_plugin->value, &argc);
216 config_file_ok = addPlugin(argc, argv);
217 last_plugin = current_plugin;
218 current_plugin = current_plugin->next;
219 }
220 return EES_SUCCESS;
221 }
222
223 EES_RC clean_variables(){
224 var_t* last_plugin = NULL;
225 current_plugin = plugin_list;
226 while(current_plugin){
227 last_plugin = current_plugin;
228 current_plugin = current_plugin->next;
229 free(last_plugin);
230 }
231 return EES_SUCCESS;
232 }
233
234 EES_RC pdl_term(){
235 free(_pdl_path);
236 clean_variables();
237 if(config_file != NULL){
238 /* This is the preferred way of cleaning up various flex versions - See: http://flex.sourceforge.net/manual/Memory-leak-_002d-16386-bytes-allocated-by-malloc_002e.html#Memory-leak-_002d-16386-bytes-allocated-by-malloc_002e */
239 #if HAVE_YYLEX_DESTROY
240 yylex_destroy();
241 #else
242 eef_log(LOG_WARNING, "Lex function yylex_destroy() not available - possibly unable to free allocated memory for evaluation manager\n");
243 #if HAVE_FLEX
244 delete_lex_buffer();
245 #endif
246 #endif
247 if((fclose(config_file)==0) ){
248 return EES_SUCCESS;
249 }
250 }
251 return EES_FAILURE;
252 }
253
254 EES_RC allow_rules(int val){
255 return EES_SUCCESS;
256 }
257
258 EES_RC allowed_policy_rule(const char* label){
259 return EES_SUCCESS;
260 }

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28