/[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 1745 - (show annotations) (download) (as text)
Wed Jun 9 10:09:23 2010 UTC (11 years, 7 months ago) by aramv
File MIME type: text/x-chdr
File size: 14564 byte(s)
Fixed seemingly working AOS
1 #include "plugin_manager.h"
2 #include "_aos.h"
3 #include "eef_aos.h"
4
5 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 static struct aos_state_s * _global_state;
8
9 /* internal methods */
10
11 /**
12 * returns _is_initialized
13 */
14 int aos_is_initialized(){
15 return _is_initialized;
16 }
17
18 /**
19 * sets threading flag
20 */
21 void aos_start_threading(){
22 _is_threading = 1;
23 }
24
25 /**
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 aos_state_t * aos_get_state(void){
29 pid_t tid;
30 aos_state_t * state = NULL;
31
32 tid = syscall(SYS_gettid);
33 /*tid = gettid();*/
34
35 if(!_is_threading){
36 return _global_state;
37 }
38
39 /* Try to get TLS */
40 state = pthread_getspecific(_aos_key);
41 if(state == 0) {
42 /* Create new TLS */
43 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 }
53 } else {
54 return state;
55 }
56 return NULL;
57 }
58
59 aos_storage_t* aos_get_storage(){
60 aos_state_t* iterator = aos_get_state();
61 return iterator->current_storage;
62 }
63
64 /**
65 * Frees storage struct pointed to by storage pointer
66 */
67 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
72 if(!storage){
73 return EES_FAILURE;
74 }
75
76 EEF_log(LOG_DEBUG, "Cleaning aos storage at %p\n", storage);
77
78 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 /*free(attribute->id);*/
86 free(attribute->issuer);
87 /*free(attribute->type);*/
88 if(attribute->needs_free){
89 EEF_log(LOG_DEBUG, "Freeing arg data %i at %p\n",attribute_count, attribute->data);
90 free(attribute->data);
91 }
92 free(attribute);
93 attribute = next_attribute;
94 attribute_count++;
95 }
96 /* free here */
97 EEF_log(LOG_DEBUG, "Freeing context at %p\n", context);
98 context->next = NULL;
99 context->list_attributes = NULL;
100 free(context);
101 context = next_context;
102 }
103 storage->list_contexts = NULL;
104 free(storage);
105 storage = NULL;
106 return EES_SUCCESS;
107 }
108
109 /* base functions */
110
111 /**
112 * Returns a pointer to a freshly allocated aos_context_t struct
113 */
114 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 context->last_attribute = NULL;
120 context->next = NULL;
121 }
122 /*printf("Context of type %i created at %p\n", context_class, context);*/
123 return context;
124 }
125
126 /**
127 * Adds supplied context to storage attained through aos_get_storage()
128 */
129 EES_RC addContext(aos_context_t* context){
130 aos_state_t* iterator = aos_get_state();
131 aos_storage_t* storage = iterator->current_storage;
132 aos_context_t* tmp_context = NULL;
133
134 EEF_log(LOG_DEBUG, "Adding context at %p to storage at %p\n", context, iterator->current_storage);
135 /*printf("Adding context at %p to storage at %p\n", context, storage);*/
136 if(!storage){
137 EEF_log(LOG_ERR, "No storage available at %p", storage);
138 return EES_FAILURE;
139 }
140 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 }
149 }
150 tmp_context->next = context;
151 EEF_log(LOG_DEBUG, "Inserted at %p\n", tmp_context);
152 /*printf("Inserted at %p\n", tmp_context);*/
153 } else {
154 storage->list_contexts = context;
155 EEF_log(LOG_DEBUG, "Created at %p\n", context);
156 /*printf("List created at %p\n", context);*/
157 }
158 aos_set_iterator(context);
159
160 /*eef_log(LOG_DEBUG, "Copied context to %p\n", storage->last_context);*/
161 return EES_SUCCESS;
162 }
163
164 EES_RC rewindContexts(aos_storage_t* storage){
165 aos_state_t* state = aos_get_state();
166 /* not using storage parameter anymore */
167
168 if(state){
169 state->current_storage = state->saved_storage;
170
171 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 }
178 return EES_FAILURE;
179 }
180
181 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 } else {
188 EEF_log(LOG_ERR, "Invalid storage passed to aos_set_iterator(): %p\n", state->current_storage);
189 }
190 return EES_FAILURE;
191 }
192
193 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 } else {
201 EEF_log(LOG_ERR, "Invalid storage passed to aos_set_iterator(): %p\n", state->current_storage);
202 }
203 return NULL;
204 }
205
206 /**
207 * returns the next aos_context_t pointer with the supplied context_class from the list of contexts in the supplied storage
208 */
209 aos_context_t * getNextContext(aos_context_class_t context_class, aos_storage_t* storage){
210 aos_state_t* state = aos_get_state();
211 aos_context_t* current_context = aos_get_iterator();
212
213 /* find context type */
214 if(context_class != ANY){
215 while(current_context != NULL){
216 if((current_context->context_class == context_class)){
217 /* Found context */
218 break;
219 }
220 current_context = current_context->next;
221 }
222 }
223
224 /* 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 /* Retry with global storage */
230 /*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 }
239
240 if(current_context){
241 aos_set_iterator(current_context->next);
242 } else {
243 aos_set_iterator(NULL);
244 }
245
246 /*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 return current_context;
259 }
260
261 /**
262 * Sets the obligation name in the struct pointed to by the supplied pointer context to supplied obligation
263 */
264 void setContextObligationId(aos_context_t* context, char * obligation){
265 if(context->context_class == OBLIGATION){
266 context->obligation_name = obligation;
267 }
268 }
269
270 /**
271 * Returns the obligation name from the supplied context
272 */
273 char* getContextObligationId(aos_context_t* context){
274 return context->obligation_name;
275 }
276
277 /**
278 * Returns a pointer to a freshly allocated aos_attribute_t struct
279 */
280 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 }
286 return attribute;
287 }
288
289 /**
290 * Adds supplied attribute to supplied context
291 */
292 EES_RC addAttribute(aos_context_t * context, aos_attribute_t* attribute){
293 if(context->last_attribute){
294 context->last_attribute->next = attribute;
295 } else {
296 context->list_attributes = attribute;
297 context->last_attribute = NULL;
298 }
299 context->last_attribute = attribute;
300 return EES_SUCCESS;
301 }
302
303 EES_RC rewindAttributes(aos_context_t* context){
304 context->last_attribute = NULL;
305 return EES_SUCCESS;
306 }
307
308 /**
309 * returns the next aos_attribute_t pointer from list of attributes in the supplied context
310 */
311 aos_attribute_t* getNextAttribute(aos_context_t* context){
312 aos_attribute_t* attribute = NULL;
313 if(context->last_attribute){
314 attribute = context->last_attribute->next;
315 } else {
316 attribute = context->list_attributes;
317 }
318 context->last_attribute = attribute;
319 return attribute;
320 }
321
322 /**
323 * removes a supplied attribute from the supplied context
324 */
325 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 EEF_log(LOG_ERR, "Argument %s is not owned by running plugin %s", current_attribute->id, get_running_plugin()->name);
331 return EES_FAILURE;
332 }
333 free(attribute->data);
334 free(attribute);
335 attribute = NULL;
336 if(last_attribute == NULL){
337 context->list_attributes = current_attribute->next;
338 } else {
339 last_attribute->next = current_attribute->next;
340 }
341 break;
342 }
343 }
344
345 if(current_attribute){
346 free(current_attribute->data);
347 free(current_attribute);
348 current_attribute = NULL;
349 return EES_SUCCESS;
350 }
351 return EES_FAILURE;
352 }
353
354 /**
355 * sets attribute id in supplied attribute
356 */
357 EES_RC setAttributeId(aos_attribute_t* attribute, char* id){
358 attribute->id = id;
359 return EES_SUCCESS;
360 }
361
362 /**
363 * sets attribute issuer in supplied attribute
364 */
365 EES_RC setAttributeIssuer(aos_attribute_t* attribute, char* issuer){
366 attribute->issuer = issuer;
367 return EES_SUCCESS;
368 }
369
370 /**
371 * sets attribute value in supplied attribute
372 */
373 EES_RC setAttributeValue(aos_attribute_t* attribute, const void* value, size_t size){
374 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 } else {
384 return EES_FAILURE;
385 }
386 return EES_SUCCESS;
387 }
388
389 /**
390 * sets attribute type in supplied attribute
391 */
392 EES_RC setAttributeType(aos_attribute_t * attribute, char* type){
393 attribute->type = type;
394 return EES_SUCCESS;
395 }
396
397 /**
398 * returns id from supplied attribute
399 */
400 char* getAttributeId(aos_attribute_t* attribute){
401 return attribute->id;
402 }
403
404 /**
405 * returns issuer from supplied attribute
406 */
407 char* getAttributeIssuer(aos_attribute_t* attribute){
408 return attribute->issuer;
409 }
410
411 /**
412 * returns data from supplied attribute casted to a char*
413 */
414 char* getAttributeValueAsString(aos_attribute_t* attribute){
415 return (char*) attribute->data;
416 }
417
418 /**
419 * returns data from supplied attribute casted to a int
420 */
421 int getAttributeValueAsInt(aos_attribute_t* attribute){
422 char* string = getAttributeValueAsString(attribute);
423 return strtol(string, NULL, 10);
424 }
425
426 /* AOS control functions */
427
428 /**
429 * Initializes the AOS and creates thread-local storage key
430 */
431 EES_RC AOS_Init (void){
432 _is_threading = 0;
433 pthread_once(&_aos_key_once, aos_make_key);
434
435 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 }
444
445 return EES_FAILURE;
446 }
447
448 /**
449 * Creates thread-local storage key
450 */
451 void aos_make_key(void){
452 pthread_key_create(&_aos_key, aos_free_key);
453 }
454
455 /**
456 * Frees thread-local storage key
457 */
458 void aos_free_key(void* iterator){
459 pid_t tid;
460 aos_storage_t* storage;
461 aos_state_t* tmp_iterator = (aos_state_t*) iterator;
462 /*tid = gettid();*/
463 tid = syscall(SYS_gettid);
464 EEF_log(LOG_DEBUG, "Killing thread %i\n", tid);
465 aos_free_storage(tmp_iterator->current_storage);
466 }
467
468 /**
469 * Frees thread-local storage for this thread
470 */
471 EES_RC AOS_Clean(void){
472 pid_t tid;
473 /*tid = gettid();*/
474 tid = syscall(SYS_gettid);
475 EEF_log(LOG_DEBUG, "Killing thread %i\n", tid);
476 if(!_is_threading){
477 return EES_FAILURE;
478 }
479 aos_free_storage(aos_get_storage());
480 return EES_SUCCESS;
481 }
482
483 /**
484 * Terminates AOS, deleting the thread-local storage key
485 */
486 EES_RC AOS_Term (void){
487 _is_threading = 0;
488 /*aos_free_storage(_global_storage);*/
489 pthread_setspecific(_aos_key, NULL);
490 pthread_key_delete(_aos_key);
491 _is_initialized = 0;
492 return EES_SUCCESS;
493 }
494
495 /**
496 * Dumps out all the data present in the AOS, as seen from the current thread's perspective
497 */
498 EES_RC aos_dump_argslist (void) {
499 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
505 EEF_log(LOG_DEBUG, "Dumping aos");
506
507 rewindContexts(NULL);
508
509 while((context = getNextContext(ANY, NULL)) != NULL){
510 EEF_log(LOG_DEBUG, "Context %p class: %i\n", context, context->context_class);
511 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
521 return EES_SUCCESS;
522 }
523

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