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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1745 - (hide annotations) (download) (as text)
Wed Jun 9 10:09:23 2010 UTC (11 years, 11 months ago) by aramv
File MIME type: text/x-chdr
File size: 14564 byte(s)
Fixed seemingly working AOS
1 aramv 502 #include "plugin_manager.h"
2 aramv 484 #include "_aos.h"
3 aramv 1430 #include "eef_aos.h"
4 aramv 425
5 aramv 1695 static pthread_key_t _aos_key;
6     static pthread_once_t _aos_key_once = PTHREAD_ONCE_INIT; /* Normally not okay to do initialisation outside of a function, but this way it (hopefully) will only be set one - during compile time */
7 aramv 1745 static struct aos_state_s * _global_state;
8 aramv 1655
9 aramv 886 /* internal methods */
10    
11 aramv 1464 /**
12     * returns _is_initialized
13     */
14 aramv 1142 int aos_is_initialized(){
15 aramv 473 return _is_initialized;
16     }
17    
18 aramv 1464 /**
19     * sets threading flag
20     */
21 aramv 1334 void aos_start_threading(){
22     _is_threading = 1;
23     }
24    
25 aramv 1464 /**
26     * returns a pointer to an aos_storage_t struct that corresponds to the current thread context. If the current context is not threaded, a pointer to a global storage struct is returned.
27     */
28 aramv 1745 aos_state_t * aos_get_state(void){
29 aramv 1332 pid_t tid;
30 aramv 1745 aos_state_t * state = NULL;
31 aramv 1334
32 aramv 1464 tid = syscall(SYS_gettid);
33     /*tid = gettid();*/
34    
35 aramv 1334 if(!_is_threading){
36 aramv 1745 return _global_state;
37 aramv 1334 }
38    
39 aramv 1464 /* Try to get TLS */
40 aramv 1745 state = pthread_getspecific(_aos_key);
41     if(state == 0) {
42 aramv 1464 /* Create new TLS */
43 aramv 1745 if((state = calloc(1, sizeof(aos_state_t)))){
44     state->last_context = NULL;
45     if((state->current_storage = calloc(1, sizeof(aos_storage_t)))){
46     state->current_storage->list_contexts = NULL;
47     _is_initialized = 1;
48     pthread_setspecific(_aos_key, state);
49     state->saved_storage = state->current_storage;
50     return state;
51     }
52 aramv 1332 }
53 aramv 1569 } else {
54 aramv 1745 return state;
55 aramv 1332 }
56 aramv 1569 return NULL;
57 aramv 1332 }
58    
59 aramv 1745 aos_storage_t* aos_get_storage(){
60     aos_state_t* iterator = aos_get_state();
61     return iterator->current_storage;
62     }
63    
64 aramv 1464 /**
65     * Frees storage struct pointed to by storage pointer
66     */
67 aramv 1353 EES_RC aos_free_storage(aos_storage_t* storage){
68     aos_context_t *context = NULL, *next_context = NULL;
69     aos_attribute_t *attribute = NULL, *next_attribute = NULL;
70     int attribute_count = 0;
71 aramv 486
72 aramv 1353 if(!storage){
73 aramv 781 return EES_FAILURE;
74 aramv 486 }
75    
76 aramv 1615 EEF_log(LOG_DEBUG, "Cleaning aos storage at %p\n", storage);
77 aramv 425
78 aramv 1353 context = storage->list_contexts;
79    
80     while(context){
81     next_context = context->next;
82     attribute = context->list_attributes;
83     while(attribute){
84     next_attribute = attribute->next;
85 aramv 1569 /*free(attribute->id);*/
86     free(attribute->issuer);
87     /*free(attribute->type);*/
88 aramv 1353 if(attribute->needs_free){
89 aramv 1615 EEF_log(LOG_DEBUG, "Freeing arg data %i at %p\n",attribute_count, attribute->data);
90 aramv 1353 free(attribute->data);
91     }
92 aramv 1569 free(attribute);
93 aramv 1353 attribute = next_attribute;
94     attribute_count++;
95     }
96     /* free here */
97 aramv 1615 EEF_log(LOG_DEBUG, "Freeing context at %p\n", context);
98 aramv 1571 context->next = NULL;
99     context->list_attributes = NULL;
100 aramv 1353 free(context);
101     context = next_context;
102 aramv 425 }
103 aramv 1571 storage->list_contexts = NULL;
104 aramv 1353 free(storage);
105 aramv 1571 storage = NULL;
106 aramv 1353 return EES_SUCCESS;
107     }
108 aramv 473
109 aramv 1353 /* base functions */
110 aramv 425
111 aramv 1464 /**
112     * Returns a pointer to a freshly allocated aos_context_t struct
113     */
114 aramv 1345 aos_context_t * createContext(aos_context_class_t context_class){
115     aos_context_t * context;
116     if((context = calloc(1, sizeof(aos_context_t)))){
117     context->context_class = context_class;
118     context->list_attributes = NULL;
119 aramv 1390 context->last_attribute = NULL;
120 aramv 1352 context->next = NULL;
121 aramv 488 }
122 aramv 1743 /*printf("Context of type %i created at %p\n", context_class, context);*/
123 aramv 1345 return context;
124 aramv 488 }
125    
126 aramv 1464 /**
127     * Adds supplied context to storage attained through aos_get_storage()
128     */
129 aramv 1733 EES_RC addContext(aos_context_t* context){
130 aramv 1745 aos_state_t* iterator = aos_get_state();
131     aos_storage_t* storage = iterator->current_storage;
132 aramv 1733 aos_context_t* tmp_context = NULL;
133 aramv 1743
134 aramv 1745 EEF_log(LOG_DEBUG, "Adding context at %p to storage at %p\n", context, iterator->current_storage);
135 aramv 1743 /*printf("Adding context at %p to storage at %p\n", context, storage);*/
136 aramv 1745 if(!storage){
137     EEF_log(LOG_ERR, "No storage available at %p", storage);
138     return EES_FAILURE;
139     }
140 aramv 1743 if(storage->list_contexts != NULL){
141     if((tmp_context = storage->list_contexts)!=NULL){
142     while(tmp_context->next){
143     if(tmp_context == context){
144     EEF_log(LOG_ERR, "The context you're trying to add was already added!\n");
145     return EES_FAILURE;
146     }
147     tmp_context = tmp_context->next;
148 aramv 1735 }
149 aramv 1663 }
150 aramv 1733 tmp_context->next = context;
151     EEF_log(LOG_DEBUG, "Inserted at %p\n", tmp_context);
152 aramv 1743 /*printf("Inserted at %p\n", tmp_context);*/
153 aramv 486 } else {
154 aramv 1733 storage->list_contexts = context;
155 aramv 1743 EEF_log(LOG_DEBUG, "Created at %p\n", context);
156     /*printf("List created at %p\n", context);*/
157 aramv 486 }
158 aramv 1745 aos_set_iterator(context);
159 aramv 1733
160 aramv 1667 /*eef_log(LOG_DEBUG, "Copied context to %p\n", storage->last_context);*/
161 aramv 1345 return EES_SUCCESS;
162 aramv 425 }
163    
164 aramv 1557 EES_RC rewindContexts(aos_storage_t* storage){
165 aramv 1745 aos_state_t* state = aos_get_state();
166     /* not using storage parameter anymore */
167 aramv 1557
168 aramv 1745 if(state){
169     state->current_storage = state->saved_storage;
170 aramv 1743
171 aramv 1745 if(state->current_storage){
172     aos_set_iterator(state->current_storage->list_contexts);
173     return EES_SUCCESS;
174     } else {
175     EEF_log(LOG_ERR, "Unable to aquire AOS handle in rewindContexts()\n");
176     }
177 aramv 1743 }
178 aramv 1745 return EES_FAILURE;
179 aramv 1743 }
180    
181 aramv 1745 EES_RC aos_set_iterator(aos_context_t* context){
182     aos_state_t* state = aos_get_state();
183     if(state->current_storage){
184     /*printf("Setting iterator for %p to %p\n", state->current_storage, context);*/
185     state->last_context = context;
186     return EES_SUCCESS;
187 aramv 1743 } else {
188 aramv 1745 EEF_log(LOG_ERR, "Invalid storage passed to aos_set_iterator(): %p\n", state->current_storage);
189 aramv 1743 }
190 aramv 1745 return EES_FAILURE;
191 aramv 1743 }
192    
193 aramv 1745 aos_context_t* aos_get_iterator(void){
194     aos_state_t* state = aos_get_state();
195     if(state->current_storage){
196     /*printf("Getting iterator for %p as %p\n", state->current_storage, state->last_context);*/
197     if(state->last_context){
198     return state->last_context->next;
199     }
200 aramv 1743 } else {
201 aramv 1745 EEF_log(LOG_ERR, "Invalid storage passed to aos_set_iterator(): %p\n", state->current_storage);
202 aramv 1743 }
203 aramv 1745 return NULL;
204 aramv 1743 }
205    
206 aramv 1464 /**
207     * returns the next aos_context_t pointer with the supplied context_class from the list of contexts in the supplied storage
208     */
209 aramv 1345 aos_context_t * getNextContext(aos_context_class_t context_class, aos_storage_t* storage){
210 aramv 1745 aos_state_t* state = aos_get_state();
211     aos_context_t* current_context = aos_get_iterator();
212 aramv 1390
213 aramv 1743 /* find context type */
214 aramv 1745 if(context_class != ANY){
215     while(current_context != NULL){
216     if((current_context->context_class == context_class)){
217     /* Found context */
218     break;
219 aramv 1345 }
220 aramv 1745 current_context = current_context->next;
221 aramv 460 }
222     }
223 aramv 1569
224 aramv 1745 /* TODO -reliably- reset storage to saved pointer */
225    
226    
227     /* save state */
228     if((current_context == NULL) && (storage != _global_state->current_storage) && (state->current_storage != _global_state->current_storage)){
229 aramv 1743 /* Retry with global storage */
230 aramv 1745 /*state->current_storage = _global_state->current_storage;*/
231     /*rewindContexts(_global_state->current_storage);*/
232     /*state->saved_storage = state->current_storage;*/
233     state->current_storage = _global_state->current_storage;
234     aos_set_iterator(_global_state->current_storage->list_contexts);
235     /*rewindContexts(_global_state->current_storage);*/
236     current_context = getNextContext(context_class, _global_state->current_storage);
237     /*printf("Should really return %p haha\n", current_context);*/
238 aramv 1743 }
239 aramv 1390
240 aramv 1745 if(current_context){
241     aos_set_iterator(current_context->next);
242 aramv 1743 } else {
243 aramv 1745 aos_set_iterator(NULL);
244 aramv 1352 }
245 aramv 1739
246 aramv 1745 /*if(current_context == NULL){*/
247     /*if((storage == _global_state->current_storage) && (state->current_storage == _global_state->current_storage)){*/
248     /*if(state->saved_storage){*/
249     /*printf("Reset state\n");*/
250     /*state->current_storage = state->saved_storage;*/
251     /*state->last_context = NULL;*/
252     /*}*/
253    
254     /*}*/
255     /*}*/
256    
257    
258 aramv 1743 return current_context;
259 aramv 1345 }
260 aramv 1334
261 aramv 1464 /**
262     * Sets the obligation name in the struct pointed to by the supplied pointer context to supplied obligation
263     */
264 aramv 1370 void setContextObligationId(aos_context_t* context, char * obligation){
265 aramv 1345 if(context->context_class == OBLIGATION){
266     context->obligation_name = obligation;
267     }
268     }
269 aramv 1334
270 aramv 1464 /**
271     * Returns the obligation name from the supplied context
272     */
273 aramv 1390 char* getContextObligationId(aos_context_t* context){
274     return context->obligation_name;
275     }
276    
277 aramv 1464 /**
278     * Returns a pointer to a freshly allocated aos_attribute_t struct
279     */
280 aramv 1345 aos_attribute_t* createAttribute(void){
281     aos_attribute_t* attribute = NULL;
282     if((attribute = calloc(1, sizeof(aos_attribute_t)))){
283     attribute->next = NULL;
284     attribute->setting_plugin = get_running_plugin();
285 aramv 1334 }
286 aramv 1345 return attribute;
287     }
288 aramv 1334
289 aramv 1464 /**
290     * Adds supplied attribute to supplied context
291     */
292 aramv 1345 EES_RC addAttribute(aos_context_t * context, aos_attribute_t* attribute){
293 aramv 1352 if(context->last_attribute){
294 aramv 1345 context->last_attribute->next = attribute;
295     } else {
296     context->list_attributes = attribute;
297 aramv 1390 context->last_attribute = NULL;
298 aramv 1345 }
299     context->last_attribute = attribute;
300     return EES_SUCCESS;
301 aramv 460 }
302    
303 aramv 1557 EES_RC rewindAttributes(aos_context_t* context){
304 aramv 1677 context->last_attribute = NULL;
305 aramv 1557 return EES_SUCCESS;
306     }
307    
308 aramv 1464 /**
309     * returns the next aos_attribute_t pointer from list of attributes in the supplied context
310     */
311 aramv 1345 aos_attribute_t* getNextAttribute(aos_context_t* context){
312 aramv 1557 aos_attribute_t* attribute = NULL;
313 aramv 1667 if(context->last_attribute){
314 aramv 1557 attribute = context->last_attribute->next;
315 aramv 1345 } else {
316 aramv 1557 attribute = context->list_attributes;
317 aramv 425 }
318 aramv 1557 context->last_attribute = attribute;
319     return attribute;
320 aramv 425 }
321    
322 aramv 1464 /**
323     * removes a supplied attribute from the supplied context
324     */
325 aramv 1345 EES_RC destroyAttribute(aos_context_t* context, aos_attribute_t* attribute){
326     aos_attribute_t *last_attribute = NULL, *current_attribute = NULL;
327     for(current_attribute = context->list_attributes; current_attribute != NULL; last_attribute = current_attribute, current_attribute = current_attribute->next){
328     if(current_attribute == attribute){
329     if(current_attribute->setting_plugin != get_running_plugin()){
330 aramv 1615 EEF_log(LOG_ERR, "Argument %s is not owned by running plugin %s", current_attribute->id, get_running_plugin()->name);
331 aramv 781 return EES_FAILURE;
332 aramv 502 }
333 aramv 1733 free(attribute->data);
334     free(attribute);
335     attribute = NULL;
336 aramv 1345 if(last_attribute == NULL){
337 aramv 1733 context->list_attributes = current_attribute->next;
338 aramv 502 } else {
339 aramv 1733 last_attribute->next = current_attribute->next;
340 aramv 502 }
341 aramv 525 break;
342 aramv 488 }
343     }
344 aramv 502
345 aramv 1345 if(current_attribute){
346     free(current_attribute->data);
347     free(current_attribute);
348 aramv 1352 current_attribute = NULL;
349 aramv 1217 return EES_SUCCESS;
350 aramv 1345 }
351     return EES_FAILURE;
352     }
353    
354 aramv 1464 /**
355     * sets attribute id in supplied attribute
356     */
357 aramv 1345 EES_RC setAttributeId(aos_attribute_t* attribute, char* id){
358     attribute->id = id;
359     return EES_SUCCESS;
360     }
361    
362 aramv 1464 /**
363     * sets attribute issuer in supplied attribute
364     */
365 aramv 1345 EES_RC setAttributeIssuer(aos_attribute_t* attribute, char* issuer){
366     attribute->issuer = issuer;
367     return EES_SUCCESS;
368     }
369    
370 aramv 1464 /**
371     * sets attribute value in supplied attribute
372     */
373 aramv 1722 EES_RC setAttributeValue(aos_attribute_t* attribute, const void* value, size_t size){
374 aramv 1583 if(value != NULL){
375     if(size == 0) {
376     attribute->data = value;
377     attribute->needs_free = 0;
378     } else {
379     attribute->data = calloc(1, size+1);
380     memcpy(attribute->data, value, size);
381     attribute->needs_free = 1;
382     }
383 aramv 1217 } else {
384 aramv 1583 return EES_FAILURE;
385 aramv 1217 }
386 aramv 1352 return EES_SUCCESS;
387 aramv 1345 }
388 aramv 502
389 aramv 1464 /**
390     * sets attribute type in supplied attribute
391     */
392 aramv 1345 EES_RC setAttributeType(aos_attribute_t * attribute, char* type){
393     attribute->type = type;
394 aramv 1352 return EES_SUCCESS;
395 aramv 488 }
396    
397 aramv 1464 /**
398     * returns id from supplied attribute
399     */
400 aramv 1345 char* getAttributeId(aos_attribute_t* attribute){
401     return attribute->id;
402     }
403    
404 aramv 1464 /**
405     * returns issuer from supplied attribute
406     */
407 aramv 1345 char* getAttributeIssuer(aos_attribute_t* attribute){
408     return attribute->issuer;
409     }
410    
411 aramv 1464 /**
412     * returns data from supplied attribute casted to a char*
413     */
414 aramv 1345 char* getAttributeValueAsString(aos_attribute_t* attribute){
415     return (char*) attribute->data;
416     }
417    
418 aramv 1464 /**
419     * returns data from supplied attribute casted to a int
420     */
421 aramv 1345 int getAttributeValueAsInt(aos_attribute_t* attribute){
422 aramv 1583 char* string = getAttributeValueAsString(attribute);
423 aramv 1650 return strtol(string, NULL, 10);
424 aramv 1345 }
425    
426 aramv 1464 /* AOS control functions */
427 aramv 781
428 aramv 1464 /**
429     * Initializes the AOS and creates thread-local storage key
430     */
431 aramv 872 EES_RC AOS_Init (void){
432 aramv 1334 _is_threading = 0;
433 aramv 1332 pthread_once(&_aos_key_once, aos_make_key);
434 aramv 1334
435 aramv 1745 if((_global_state = calloc(1, sizeof(aos_state_t)))){
436     _global_state->last_context = NULL;
437     if((_global_state->current_storage = calloc(1, sizeof(aos_storage_t)))){
438     _global_state->current_storage->list_contexts = NULL;
439     _is_initialized = 1;
440     _global_state->saved_storage = _global_state->current_storage;
441     return EES_SUCCESS;
442     }
443 aramv 1334 }
444    
445 aramv 1745 return EES_FAILURE;
446 aramv 1332 }
447 aramv 781
448 aramv 1464 /**
449     * Creates thread-local storage key
450     */
451 aramv 1332 void aos_make_key(void){
452 aramv 1353 pthread_key_create(&_aos_key, aos_free_key);
453 aramv 425 }
454    
455 aramv 1464 /**
456     * Frees thread-local storage key
457     */
458 aramv 1745 void aos_free_key(void* iterator){
459 aramv 1390 pid_t tid;
460 aramv 1745 aos_storage_t* storage;
461     aos_state_t* tmp_iterator = (aos_state_t*) iterator;
462 aramv 1667 /*tid = gettid();*/
463 aramv 1390 tid = syscall(SYS_gettid);
464 aramv 1615 EEF_log(LOG_DEBUG, "Killing thread %i\n", tid);
465 aramv 1745 aos_free_storage(tmp_iterator->current_storage);
466 aramv 1352 }
467    
468 aramv 1464 /**
469     * Frees thread-local storage for this thread
470     */
471 aramv 1332 EES_RC AOS_Clean(void){
472 aramv 1352 pid_t tid;
473 aramv 1464 /*tid = gettid();*/
474 aramv 1352 tid = syscall(SYS_gettid);
475 aramv 1615 EEF_log(LOG_DEBUG, "Killing thread %i\n", tid);
476 aramv 1352 if(!_is_threading){
477     return EES_FAILURE;
478     }
479 aramv 1390 aos_free_storage(aos_get_storage());
480 aramv 1332 return EES_SUCCESS;
481 aramv 1265 }
482 aramv 425
483 aramv 1464 /**
484     * Terminates AOS, deleting the thread-local storage key
485     */
486 aramv 872 EES_RC AOS_Term (void){
487 aramv 1352 _is_threading = 0;
488 aramv 1745 /*aos_free_storage(_global_storage);*/
489 aramv 1352 pthread_setspecific(_aos_key, NULL);
490 aramv 1332 pthread_key_delete(_aos_key);
491     _is_initialized = 0;
492 aramv 1265 return EES_SUCCESS;
493 aramv 425 }
494 aramv 781
495 aramv 1464 /**
496     * Dumps out all the data present in the AOS, as seen from the current thread's perspective
497     */
498 aramv 1332 EES_RC aos_dump_argslist (void) {
499 aramv 1661 char *log_str = "aos_dump";
500     aos_context_t *context = NULL;
501     aos_attribute_t *attribute = NULL;
502     char *attribute_name = NULL;
503     char *attribute_value = NULL;
504 aramv 1583
505 aramv 1615 EEF_log(LOG_DEBUG, "Dumping aos");
506 aramv 1739
507 aramv 1583 rewindContexts(NULL);
508 aramv 1739
509 aramv 1735 while((context = getNextContext(ANY, NULL)) != NULL){
510 aramv 1739 EEF_log(LOG_DEBUG, "Context %p class: %i\n", context, context->context_class);
511 aramv 1735 rewindAttributes(context);
512     while((attribute = getNextAttribute(context)) != NULL){
513     attribute_name = getAttributeId(attribute);
514     attribute_value = getAttributeValueAsString(attribute);
515     if(attribute_name && attribute_value){
516     EEF_log(LOG_DEBUG, "\t%s=%s\n", attribute_name, attribute_value);
517     }
518     }
519     }
520 aramv 1661
521 okoeroo 1307 return EES_SUCCESS;
522     }
523    

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