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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1390 - (show annotations) (download) (as text)
Mon Jan 18 15:41:17 2010 UTC (12 years, 8 months ago) by aramv
File MIME type: text/x-chdr
File size: 10917 byte(s)
AOS seems to be valgrind clean
1 #include "plugin_manager.h"
2 #include "aos.h"
3 #include "_aos.h"
4
5 /* internal methods */
6
7 int aos_is_initialized(){
8 return _is_initialized;
9 }
10
11 void aos_start_threading(){
12 _is_threading = 1;
13 }
14
15 aos_storage_t * aos_get_storage(){
16 pid_t tid;
17 aos_storage_t * storage;
18
19 if(!_is_threading){
20 printf("Accessed global storage\n");
21 return _global_storage;
22 }
23
24 tid = syscall(SYS_gettid);
25 printf("Accessed storage in thread %i\n", tid);
26
27 if(!(storage = pthread_getspecific(_aos_key))) {
28 if((storage = calloc(1, sizeof(aos_storage_t)))){
29 storage->last_context = NULL;
30 pthread_setspecific(_aos_key, storage);
31 printf("Created new TLS at %p\n", storage);
32 }
33 } else {
34 printf("Found existing TLS at %p\n", storage);
35 }
36 return storage;
37 }
38
39 EES_RC aos_free_storage(aos_storage_t* storage){
40 aos_context_t *context = NULL, *next_context = NULL;
41 aos_attribute_t *attribute = NULL, *next_attribute = NULL;
42 int attribute_count = 0;
43
44 if(!storage){
45 return EES_FAILURE;
46 }
47
48 printf("Cleaning aos storage at %p\n", storage);
49 if(storage == _global_storage){
50 printf("This is the GLOBAL storage\n");
51 }
52
53 context = storage->list_contexts;
54
55 while(context){
56 next_context = context->next;
57 printf("Checking context at %p\n", context);
58 attribute = context->list_attributes;
59 while(attribute){
60 next_attribute = attribute->next;
61 /* free here */
62 printf("Checking attribute at %p\n", attribute);
63 if(attribute->needs_free){
64 eef_log(LOG_DEBUG, "Freeing arg data %i at %p\n",attribute_count, attribute->data);
65 free(attribute->data);
66 free(attribute);
67 } else {
68 /*eef_log(LOG_DEBUG, "Not freeing arg data %i at %p\n",attribute_count, attribute->data);*/
69 free(attribute);
70 }
71 attribute = next_attribute;
72 attribute_count++;
73 }
74 /* free here */
75 printf("Freeing context at %p\n", context);
76 free(context);
77 context = next_context;
78 }
79 free(storage);
80 return EES_SUCCESS;
81 }
82
83 /* base functions */
84
85 aos_context_t * createContext(aos_context_class_t context_class){
86 aos_context_t * context;
87 if((context = calloc(1, sizeof(aos_context_t)))){
88 context->context_class = context_class;
89 context->list_attributes = NULL;
90 context->last_attribute = NULL;
91 context->next = NULL;
92 }
93 return context;
94 }
95
96 EES_RC addContext(aos_context_t* context){
97 aos_storage_t* storage = aos_get_storage();
98 printf("Adding context at %p to storage at %p\n", context, storage);
99 if(storage->last_context){
100 printf("Inserted at %p\n", storage->last_context);
101 storage->last_context->next = context;
102 } else {
103 printf("Created at %p\n", storage->list_contexts);
104 storage->list_contexts = context;
105 storage->last_context = NULL;
106 }
107 printf("Copied context to %p\n", storage->last_context);
108 storage->last_context = context;
109 return EES_SUCCESS;
110 }
111
112 aos_context_t * getNextContext(aos_context_class_t context_class, aos_storage_t* storage){
113 aos_context_t* current_context;
114 aos_storage_t* current_storage;
115
116 if(storage){
117 printf("Using passed storage\n");
118 current_storage = storage;
119 } else {
120 printf("Using TLS getter\n");
121 current_storage = aos_get_storage();
122 }
123
124 if(current_storage->last_context != NULL){
125 printf("Picking up from last context\n");
126 current_context = current_storage->last_context->next;
127 } else {
128 printf("Walking list contexts\n");
129 current_context = current_storage->list_contexts;
130 while(current_context){
131 printf("Current context: %p\n", current_context);
132 if((current_context->context_class == context_class) || (context_class == ANY) ){
133 printf("Found context with class %i\n", context_class);
134 break;
135 }
136 current_context = current_context->next;
137 }
138 }
139 current_storage->last_context = current_context;
140
141 if(current_context){
142 printf("Found something on the first go\n");
143 return current_context;
144 } else {
145 if(current_storage != _global_storage){
146 printf("Retrying at global storage\n");
147 return getNextContext(context_class, _global_storage);
148 } else {
149 printf("Failed to find anything\n");
150 }
151 }
152 return NULL;
153 }
154
155 void setContextObligationId(aos_context_t* context, char * obligation){
156 if(context->context_class == OBLIGATION){
157 context->obligation_name = obligation;
158 }
159 }
160
161 char* getContextObligationId(aos_context_t* context){
162 return context->obligation_name;
163 }
164
165 aos_attribute_t* createAttribute(void){
166 aos_attribute_t* attribute = NULL;
167 if((attribute = calloc(1, sizeof(aos_attribute_t)))){
168 attribute->next = NULL;
169 attribute->setting_plugin = get_running_plugin();
170 }
171 return attribute;
172 }
173
174 EES_RC addAttribute(aos_context_t * context, aos_attribute_t* attribute){
175 printf("Adding attribute at %p to context at %p\n", attribute, context);
176 if(context->last_attribute){
177 context->last_attribute->next = attribute;
178 } else {
179 context->list_attributes = attribute;
180 context->last_attribute = NULL;
181 }
182 context->last_attribute = attribute;
183 return EES_SUCCESS;
184 }
185
186 aos_attribute_t* getNextAttribute(aos_context_t* context){
187 if(!context->last_attribute){
188 context->last_attribute = context->list_attributes;
189 } else {
190 context->last_attribute = context->last_attribute->next;
191 }
192 return context->last_attribute;
193 }
194
195 EES_RC destroyAttribute(aos_context_t* context, aos_attribute_t* attribute){
196 aos_attribute_t *last_attribute = NULL, *current_attribute = NULL;
197 for(current_attribute = context->list_attributes; current_attribute != NULL; last_attribute = current_attribute, current_attribute = current_attribute->next){
198 if(current_attribute == attribute){
199 if(current_attribute->setting_plugin != get_running_plugin()){
200 eef_log(LOG_ERR, "Argument %s is not owned by running plugin %s", current_attribute->id, get_running_plugin()->name);
201 return EES_FAILURE;
202 }
203 free(attribute->data);
204 free(attribute);
205 attribute = NULL;
206 if(last_attribute == NULL){
207 context->list_attributes = current_attribute->next;
208 } else {
209 last_attribute->next = current_attribute->next;
210 }
211 break;
212 }
213 }
214
215 if(current_attribute){
216 free(current_attribute->data);
217 free(current_attribute);
218 current_attribute = NULL;
219 return EES_SUCCESS;
220 }
221 return EES_FAILURE;
222 }
223
224 EES_RC setAttributeId(aos_attribute_t* attribute, char* id){
225 attribute->id = id;
226 return EES_SUCCESS;
227 }
228
229 EES_RC setAttributeIssuer(aos_attribute_t* attribute, char* issuer){
230 attribute->issuer = issuer;
231 return EES_SUCCESS;
232 }
233
234 EES_RC setAttributeValue(aos_attribute_t* attribute, void* value, size_t size){
235 if(size == 0) {
236 attribute->data = value;
237 attribute->needs_free = 0;
238 } else {
239 attribute->data = calloc(1, size);
240 memcpy(attribute->data, value, size);
241 attribute->needs_free = 1;
242 }
243 return EES_SUCCESS;
244 }
245
246 EES_RC setAttributeType(aos_attribute_t * attribute, char* type){
247 attribute->type = type;
248 return EES_SUCCESS;
249 }
250
251 char* getAttributeId(aos_attribute_t* attribute){
252 return attribute->id;
253 }
254
255 char* getAttributeIssuer(aos_attribute_t* attribute){
256 return attribute->issuer;
257 }
258
259 char* getAttributeValueAsString(aos_attribute_t* attribute){
260 return (char*) attribute->data;
261 }
262
263 int getAttributeValueAsInt(aos_attribute_t* attribute){
264 return (int) (attribute->data);
265 }
266
267 /* needs more work */
268 EES_RC aos_delete_attribute_by_label(const char* label){
269 aos_attribute_t * current_attr = NULL;
270 aos_attribute_t * previous_attr = NULL;
271 aos_storage_t * storage = aos_get_storage();
272
273 if(label == NULL){
274 return EES_FAILURE;
275 }
276
277 /*for(current_attr = storage->list_attributes; current_attr != NULL; previous_attr = current_attr, current_attr = current_attr->next){*/
278 /*if(strncmp(current_attr->id, label, strlen(label)) == 0){*/
279 /*if(current_attr->setting_plugin != get_running_plugin()){*/
280 /*eef_log(LOG_ERR, "Argument %s is not owned by running plugin %s", current_attr->id, get_running_plugin()->name);*/
281 /*return EES_FAILURE;*/
282 /*}*/
283 /*if(previous_attr == NULL){*/
284 /**//* head node deleted. linking start of list to next node */
285 /*storage->list_arguments = current_attr->next;*/
286 /*} else {*/
287 /**//* link previous to deletee's next */
288 /*previous_attr->next = current_attr->next;*/
289 /*}*/
290 /*break;*/
291 /*}*/
292 /*}*/
293
294 if(current_attr){
295 /*eef_log(LOG_ERR, "Deleting attr:%s", label);*/
296 free(current_attr->data);
297 free(current_attr);
298 current_attr = NULL;
299 return EES_SUCCESS;
300 } else {
301 return EES_FAILURE;
302 }
303
304 }
305
306 void* aos_get_value_by_label(const char *label){
307 aos_attribute_t* attr = aos_get_attribute_by_label(label);
308 if(attr == NULL){
309 return NULL;
310 } else {
311 return attr->data;
312 }
313 }
314
315 aos_attribute_t * aos_get_attribute_by_label(const char *label){
316 aos_context_t * context;
317 aos_attribute_t * attribute;
318 aos_storage_t * storage = aos_get_storage();
319
320 while((context = getNextContext(ANY, storage))){
321 while((attribute = getNextAttribute(context))){
322 if(strncmp(attribute->id, label, strlen(label)) ==0){
323 return attribute;
324 }
325 }
326 }
327
328 storage = _global_storage;
329
330 while((context = getNextContext(ANY, storage))){
331 while((attribute = getNextAttribute(context))){
332 if(strncmp(attribute->id, label, strlen(label)) ==0){
333 return attribute;
334 }
335 }
336 }
337
338 return NULL;
339 }
340
341 /* initializer & terminator */
342
343 EES_RC AOS_Init (void){
344 _is_threading = 0;
345 pthread_once(&_aos_key_once, aos_make_key);
346
347 if(!(_global_storage = calloc(1, sizeof(aos_storage_t)))){
348 return EES_FAILURE;
349 }
350
351 _is_initialized = 1;
352 return EES_SUCCESS;
353 }
354
355 void aos_make_key(void){
356 pthread_key_create(&_aos_key, aos_free_key);
357 }
358
359 void aos_free_key(void* storage){
360 pid_t tid;
361 tid = syscall(SYS_gettid);
362 printf("Killing thread %i\n", tid);
363 aos_free_storage(storage);
364 printf("Freed storage for thread %i\n", tid);
365 }
366
367 EES_RC AOS_Clean(void){
368 pid_t tid;
369 tid = syscall(SYS_gettid);
370 printf("Killing thread %i\n", tid);
371 if(!_is_threading){
372 return EES_FAILURE;
373 }
374 aos_free_storage(aos_get_storage());
375 printf("Freed storage for thread %i\n", tid);
376 return EES_SUCCESS;
377 }
378
379 EES_RC AOS_Term (void){
380 _is_threading = 0;
381 aos_free_storage(_global_storage);
382 pthread_setspecific(_aos_key, NULL);
383 pthread_key_delete(_aos_key);
384 _is_initialized = 0;
385 return EES_SUCCESS;
386 }
387
388 EES_RC aos_dump_argslist (void) {
389 aos_context_t * context;
390 aos_attribute_t* attribute;
391 while((context = getNextContext(ANY, NULL))){
392 if(context->context_class == OBLIGATION){
393 printf("Context obligation: %s\n", context->obligation_name);
394 }
395 while((attribute = getNextAttribute(context))){
396 printf("Attribute: %s with value: %s\n", attribute->id, attribute->data);
397 }
398 }
399 return EES_SUCCESS;
400 }
401

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