/[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 1747 - (show annotations) (download) (as text)
Wed Jun 9 12:33:41 2010 UTC (11 years, 7 months ago) by aramv
File MIME type: text/x-chdr
File size: 13896 byte(s)
Fixed small bug in addContext that added every new context to the global storage (oops)
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->saved_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 if(!storage){
136 EEF_log(LOG_ERR, "No storage available at %p", storage);
137 return EES_FAILURE;
138 }
139 if(storage->list_contexts != NULL){
140 if((tmp_context = storage->list_contexts)!=NULL){
141 while(tmp_context->next){
142 if(tmp_context == context){
143 EEF_log(LOG_ERR, "The context you're trying to add was already added!\n");
144 return EES_FAILURE;
145 }
146 tmp_context = tmp_context->next;
147 }
148 }
149 tmp_context->next = context;
150 EEF_log(LOG_DEBUG, "Inserted at %p\n", tmp_context);
151 /*printf("Inserted at %p\n", tmp_context);*/
152 } else {
153 storage->list_contexts = context;
154 EEF_log(LOG_DEBUG, "Created at %p\n", context);
155 /*printf("List created at %p\n", context);*/
156 }
157 aos_set_iterator(context);
158
159 /*eef_log(LOG_DEBUG, "Copied context to %p\n", storage->last_context);*/
160 return EES_SUCCESS;
161 }
162
163 EES_RC rewindContexts(aos_storage_t* storage){
164 aos_state_t* state = aos_get_state();
165 /* not using storage parameter anymore */
166
167 if(state){
168 state->current_storage = state->saved_storage;
169
170 if(state->current_storage){
171 aos_set_iterator(state->current_storage->list_contexts);
172 return EES_SUCCESS;
173 } else {
174 EEF_log(LOG_ERR, "Unable to aquire AOS handle in rewindContexts()\n");
175 }
176 }
177 return EES_FAILURE;
178 }
179
180 EES_RC aos_set_iterator(aos_context_t* context){
181 aos_state_t* state = aos_get_state();
182 if(state->current_storage){
183 /*printf("Setting iterator for %p to %p\n", state->current_storage, context);*/
184 state->last_context = context;
185 return EES_SUCCESS;
186 } else {
187 EEF_log(LOG_ERR, "Invalid storage passed to aos_set_iterator(): %p\n", state->current_storage);
188 }
189 return EES_FAILURE;
190 }
191
192 aos_context_t* aos_get_iterator(void){
193 aos_state_t* state = aos_get_state();
194 if(state->current_storage){
195 /*printf("Getting iterator for %p as %p\n", state->current_storage, state->last_context);*/
196 if(state->last_context){
197 return state->last_context;
198 }
199 } else {
200 EEF_log(LOG_ERR, "Invalid storage passed to aos_set_iterator(): %p\n", state->current_storage);
201 }
202 return NULL;
203 }
204
205 /**
206 * returns the next aos_context_t pointer with the supplied context_class from the list of contexts in the supplied storage.
207 * will fall through and try to use global storage in case nothing was found
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 /* save state */
225 if((current_context == NULL) && (storage != _global_state->current_storage) && (state->current_storage != _global_state->current_storage)){
226 /* Retry with global storage */
227 state->current_storage = _global_state->current_storage;
228 aos_set_iterator(_global_state->current_storage->list_contexts);
229 current_context = getNextContext(context_class, _global_state->current_storage);
230 }
231
232 /* save iterator */
233 if(current_context){
234 aos_set_iterator(current_context->next);
235 } else {
236 aos_set_iterator(NULL);
237 }
238
239 return current_context;
240 }
241
242 /**
243 * Sets the obligation name in the struct pointed to by the supplied pointer context to supplied obligation
244 */
245 void setContextObligationId(aos_context_t* context, char * obligation){
246 if(context->context_class == OBLIGATION){
247 context->obligation_name = obligation;
248 }
249 }
250
251 /**
252 * Returns the obligation name from the supplied context
253 */
254 char* getContextObligationId(aos_context_t* context){
255 return context->obligation_name;
256 }
257
258 /**
259 * Returns a pointer to a freshly allocated aos_attribute_t struct
260 */
261 aos_attribute_t* createAttribute(void){
262 aos_attribute_t* attribute = NULL;
263 if((attribute = calloc(1, sizeof(aos_attribute_t)))){
264 attribute->next = NULL;
265 attribute->setting_plugin = get_running_plugin();
266 }
267 return attribute;
268 }
269
270 /**
271 * Adds supplied attribute to supplied context
272 */
273 EES_RC addAttribute(aos_context_t * context, aos_attribute_t* attribute){
274 if(context->last_attribute){
275 context->last_attribute->next = attribute;
276 } else {
277 context->list_attributes = attribute;
278 context->last_attribute = NULL;
279 }
280 context->last_attribute = attribute;
281 return EES_SUCCESS;
282 }
283
284 EES_RC rewindAttributes(aos_context_t* context){
285 context->last_attribute = NULL;
286 return EES_SUCCESS;
287 }
288
289 /**
290 * returns the next aos_attribute_t pointer from list of attributes in the supplied context
291 */
292 aos_attribute_t* getNextAttribute(aos_context_t* context){
293 aos_attribute_t* attribute = NULL;
294 if(context->last_attribute){
295 attribute = context->last_attribute->next;
296 } else {
297 attribute = context->list_attributes;
298 }
299 context->last_attribute = attribute;
300 return attribute;
301 }
302
303 /**
304 * removes a supplied attribute from the supplied context
305 */
306 EES_RC destroyAttribute(aos_context_t* context, aos_attribute_t* attribute){
307 aos_attribute_t *last_attribute = NULL, *current_attribute = NULL;
308 for(current_attribute = context->list_attributes; current_attribute != NULL; last_attribute = current_attribute, current_attribute = current_attribute->next){
309 if(current_attribute == attribute){
310 if(current_attribute->setting_plugin != get_running_plugin()){
311 EEF_log(LOG_ERR, "Argument %s is not owned by running plugin %s", current_attribute->id, get_running_plugin()->name);
312 return EES_FAILURE;
313 }
314 free(attribute->data);
315 free(attribute);
316 attribute = NULL;
317 if(last_attribute == NULL){
318 context->list_attributes = current_attribute->next;
319 } else {
320 last_attribute->next = current_attribute->next;
321 }
322 break;
323 }
324 }
325
326 if(current_attribute){
327 free(current_attribute->data);
328 free(current_attribute);
329 current_attribute = NULL;
330 return EES_SUCCESS;
331 }
332 return EES_FAILURE;
333 }
334
335 /**
336 * sets attribute id in supplied attribute
337 */
338 EES_RC setAttributeId(aos_attribute_t* attribute, char* id){
339 attribute->id = id;
340 return EES_SUCCESS;
341 }
342
343 /**
344 * sets attribute issuer in supplied attribute
345 */
346 EES_RC setAttributeIssuer(aos_attribute_t* attribute, char* issuer){
347 attribute->issuer = issuer;
348 return EES_SUCCESS;
349 }
350
351 /**
352 * sets attribute value in supplied attribute
353 */
354 EES_RC setAttributeValue(aos_attribute_t* attribute, const void* value, size_t size){
355 if(value != NULL){
356 if(size == 0) {
357 attribute->data = value;
358 attribute->needs_free = 0;
359 } else {
360 attribute->data = calloc(1, size+1);
361 memcpy(attribute->data, value, size);
362 attribute->needs_free = 1;
363 }
364 } else {
365 return EES_FAILURE;
366 }
367 return EES_SUCCESS;
368 }
369
370 /**
371 * sets attribute type in supplied attribute
372 */
373 EES_RC setAttributeType(aos_attribute_t * attribute, char* type){
374 attribute->type = type;
375 return EES_SUCCESS;
376 }
377
378 /**
379 * returns id from supplied attribute
380 */
381 char* getAttributeId(aos_attribute_t* attribute){
382 return attribute->id;
383 }
384
385 /**
386 * returns issuer from supplied attribute
387 */
388 char* getAttributeIssuer(aos_attribute_t* attribute){
389 return attribute->issuer;
390 }
391
392 /**
393 * returns data from supplied attribute casted to a char*
394 */
395 char* getAttributeValueAsString(aos_attribute_t* attribute){
396 return (char*) attribute->data;
397 }
398
399 /**
400 * returns data from supplied attribute casted to a int
401 */
402 int getAttributeValueAsInt(aos_attribute_t* attribute){
403 char* string = getAttributeValueAsString(attribute);
404 return strtol(string, NULL, 10);
405 }
406
407 /* AOS control functions */
408
409 /**
410 * Initializes the AOS and creates thread-local storage key
411 */
412 EES_RC AOS_Init (void){
413 _is_threading = 0;
414 pthread_once(&_aos_key_once, aos_make_key);
415
416 if((_global_state = calloc(1, sizeof(aos_state_t)))){
417 _global_state->last_context = NULL;
418 if((_global_state->current_storage = calloc(1, sizeof(aos_storage_t)))){
419 _global_state->current_storage->list_contexts = NULL;
420 _is_initialized = 1;
421 _global_state->saved_storage = _global_state->current_storage;
422 return EES_SUCCESS;
423 }
424 }
425
426 return EES_FAILURE;
427 }
428
429 /**
430 * Creates thread-local storage key
431 */
432 void aos_make_key(void){
433 pthread_key_create(&_aos_key, aos_free_key);
434 }
435
436 /**
437 * Frees thread-local storage key
438 */
439 void aos_free_key(void* iterator){
440 pid_t tid;
441 aos_storage_t* storage;
442 aos_state_t* tmp_iterator = (aos_state_t*) iterator;
443 /*tid = gettid();*/
444 tid = syscall(SYS_gettid);
445 EEF_log(LOG_DEBUG, "Killing thread %i\n", tid);
446 aos_free_storage(tmp_iterator->current_storage);
447 }
448
449 /**
450 * Frees thread-local storage for this thread
451 */
452 EES_RC AOS_Clean(void){
453 pid_t tid;
454 /*tid = gettid();*/
455 tid = syscall(SYS_gettid);
456 EEF_log(LOG_DEBUG, "Killing thread %i\n", tid);
457 if(!_is_threading){
458 return EES_FAILURE;
459 }
460 aos_free_storage(aos_get_storage());
461 return EES_SUCCESS;
462 }
463
464 /**
465 * Terminates AOS, deleting the thread-local storage key
466 */
467 EES_RC AOS_Term (void){
468 _is_threading = 0;
469 /*aos_free_storage(_global_storage);*/
470 pthread_setspecific(_aos_key, NULL);
471 pthread_key_delete(_aos_key);
472 _is_initialized = 0;
473 return EES_SUCCESS;
474 }
475
476 /**
477 * Dumps out all the data present in the AOS, as seen from the current thread's perspective
478 */
479 EES_RC aos_dump_argslist (void) {
480 char *log_str = "aos_dump";
481 aos_context_t *context = NULL;
482 aos_attribute_t *attribute = NULL;
483 char *attribute_name = NULL;
484 char *attribute_value = NULL;
485
486 EEF_log(LOG_DEBUG, "Dumping aos");
487
488 rewindContexts(NULL);
489
490 while((context = getNextContext(ANY, NULL)) != NULL){
491 EEF_log(LOG_DEBUG, "Context %p class: %i\n", context, context->context_class);
492 rewindAttributes(context);
493 while((attribute = getNextAttribute(context)) != NULL){
494 attribute_name = getAttributeId(attribute);
495 attribute_value = getAttributeValueAsString(attribute);
496 if(attribute_name && attribute_value){
497 EEF_log(LOG_DEBUG, "\t%s=%s\n", attribute_name, attribute_value);
498 }
499 }
500 }
501
502 return EES_SUCCESS;
503 }
504

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