/[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 1583 - (show annotations) (download) (as text)
Fri Mar 19 17:29:41 2010 UTC (12 years, 6 months ago) by aramv
File MIME type: text/x-chdr
File size: 12321 byte(s)
Fixed a read error. Added signature for fork function
1 #include "plugin_manager.h"
2 #include "_aos.h"
3 #include "eef_aos.h"
4
5 /* internal methods */
6
7 /**
8 * returns _is_initialized
9 */
10 int aos_is_initialized(){
11 return _is_initialized;
12 }
13
14 /**
15 * sets threading flag
16 */
17 void aos_start_threading(){
18 _is_threading = 1;
19 }
20
21 /**
22 * 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.
23 */
24 aos_storage_t * aos_get_storage(){
25 pid_t tid;
26 aos_storage_t * storage = NULL;
27
28 tid = syscall(SYS_gettid);
29 /*tid = gettid();*/
30
31 if(!_is_threading){
32 return _global_storage;
33 }
34
35 /* Try to get TLS */
36 storage = pthread_getspecific(_aos_key);
37 if(storage == 0) {
38 /* Create new TLS */
39 if((storage = calloc(1, sizeof(aos_storage_t)))){
40 storage->last_context = NULL;
41 pthread_setspecific(_aos_key, storage);
42 return storage;
43 }
44 } else {
45 return storage;
46 }
47 return NULL;
48 }
49
50 /**
51 * Frees storage struct pointed to by storage pointer
52 */
53 EES_RC aos_free_storage(aos_storage_t* storage){
54 aos_context_t *context = NULL, *next_context = NULL;
55 aos_attribute_t *attribute = NULL, *next_attribute = NULL;
56 int attribute_count = 0;
57
58 if(!storage){
59 return EES_FAILURE;
60 }
61
62 eef_log(LOG_DEBUG, "Cleaning aos storage at %p\n", storage);
63
64 context = storage->list_contexts;
65
66 while(context){
67 next_context = context->next;
68 attribute = context->list_attributes;
69 while(attribute){
70 next_attribute = attribute->next;
71 /*free(attribute->id);*/
72 free(attribute->issuer);
73 /*free(attribute->type);*/
74 if(attribute->needs_free){
75 eef_log(LOG_DEBUG, "Freeing arg data %i at %p\n",attribute_count, attribute->data);
76 free(attribute->data);
77 }
78 free(attribute);
79 attribute = next_attribute;
80 attribute_count++;
81 }
82 /* free here */
83 eef_log(LOG_DEBUG, "Freeing context at %p\n", context);
84 context->next = NULL;
85 context->list_attributes = NULL;
86 free(context);
87 context = next_context;
88 }
89 storage->list_contexts = NULL;
90 free(storage);
91 storage = NULL;
92 return EES_SUCCESS;
93 }
94
95 /* base functions */
96
97 /**
98 * Returns a pointer to a freshly allocated aos_context_t struct
99 */
100 aos_context_t * createContext(aos_context_class_t context_class){
101 aos_context_t * context;
102 if((context = calloc(1, sizeof(aos_context_t)))){
103 context->context_class = context_class;
104 context->list_attributes = NULL;
105 context->last_attribute = NULL;
106 context->next = NULL;
107 }
108 return context;
109 }
110
111 /**
112 * Adds supplied context to storage attained through aos_get_storage()
113 */
114 EES_RC addContext(aos_context_t* context){
115 aos_storage_t* storage = aos_get_storage();
116 /*eef_log(LOG_DEBUG, "Adding context at %p to storage at %p\n", context, storage);*/
117 if(storage->last_context){
118 /*eef_log(LOG_DEBUG, "Inserted at %p\n", storage->last_context);*/
119 storage->last_context->next = context;
120 } else {
121 /*eef_log(LOG_DEBUG, "Created at %p\n", storage->list_contexts);*/
122 storage->list_contexts = context;
123 storage->last_context = NULL;
124 }
125 /*eef_log(LOG_DEBUG, "Copied context to %p\n", storage->last_context);*/
126 storage->last_context = context;
127 return EES_SUCCESS;
128 }
129
130 EES_RC rewindContexts(aos_storage_t* storage){
131 if(storage == NULL){
132 storage = aos_get_storage();
133 }
134 storage->last_context = NULL;
135 return EES_SUCCESS;
136 }
137
138 /**
139 * returns the next aos_context_t pointer with the supplied context_class from the list of contexts in the supplied storage
140 */
141 aos_context_t * getNextContext(aos_context_class_t context_class, aos_storage_t* storage){
142 aos_context_t* current_context = NULL;
143 aos_storage_t* current_storage = NULL;
144
145 if(storage != NULL){
146 /* Using passed storage */
147 current_storage = storage;
148 } else {
149 /* Using TLS getter */
150 current_storage = aos_get_storage();
151 }
152
153 if(current_storage != NULL){
154 if(current_storage->last_context != NULL){
155 /* Pick up from last context */
156 current_context = current_storage->last_context->next;
157 } else {
158 /* Reset current_context to start of list */
159 current_context = current_storage->list_contexts;
160 }
161 }
162
163 if(current_context != NULL){
164 if(context_class != ANY){
165 while(current_context != NULL){
166 /*printf("Checking context at %p\n", current_context);*/
167 /*printf("This one has %i\n", current_context->context_class);*/
168 if((current_context->context_class == context_class)){
169 /* Found context */
170 /*printf("Found context\n");*/
171 break;
172 }
173 current_context = current_context->next;
174 }
175 }
176 }
177
178 current_storage->last_context = current_context;
179
180 if(current_context != NULL){
181 /* Found something on the first go */
182 return current_context;
183 } else {
184 if(current_storage != _global_storage){
185 /* Retry with global storage */
186 return getNextContext(context_class, _global_storage);
187 }
188 }
189 return NULL;
190 }
191
192 /**
193 * Sets the obligation name in the struct pointed to by the supplied pointer context to supplied obligation
194 */
195 void setContextObligationId(aos_context_t* context, char * obligation){
196 if(context->context_class == OBLIGATION){
197 context->obligation_name = obligation;
198 }
199 }
200
201 /**
202 * Returns the obligation name from the supplied context
203 */
204 char* getContextObligationId(aos_context_t* context){
205 return context->obligation_name;
206 }
207
208 /**
209 * Returns a pointer to a freshly allocated aos_attribute_t struct
210 */
211 aos_attribute_t* createAttribute(void){
212 aos_attribute_t* attribute = NULL;
213 if((attribute = calloc(1, sizeof(aos_attribute_t)))){
214 attribute->next = NULL;
215 attribute->setting_plugin = get_running_plugin();
216 }
217 return attribute;
218 }
219
220 /**
221 * Adds supplied attribute to supplied context
222 */
223 EES_RC addAttribute(aos_context_t * context, aos_attribute_t* attribute){
224 if(context->last_attribute){
225 context->last_attribute->next = attribute;
226 } else {
227 context->list_attributes = attribute;
228 context->last_attribute = NULL;
229 }
230 context->last_attribute = attribute;
231 return EES_SUCCESS;
232 }
233
234 EES_RC rewindAttributes(aos_context_t* context){
235 context->last_attribute = NULL;
236 return EES_SUCCESS;
237 }
238
239 /**
240 * returns the next aos_attribute_t pointer from list of attributes in the supplied context
241 */
242 aos_attribute_t* getNextAttribute(aos_context_t* context){
243 aos_attribute_t* attribute = NULL;
244 if(context->last_attribute != NULL){
245 attribute = context->last_attribute->next;
246 } else {
247 attribute = context->list_attributes;
248 }
249 context->last_attribute = attribute;
250 return attribute;
251 }
252
253 /**
254 * removes a supplied attribute from the supplied context
255 */
256 EES_RC destroyAttribute(aos_context_t* context, aos_attribute_t* attribute){
257 aos_attribute_t *last_attribute = NULL, *current_attribute = NULL;
258 for(current_attribute = context->list_attributes; current_attribute != NULL; last_attribute = current_attribute, current_attribute = current_attribute->next){
259 if(current_attribute == attribute){
260 if(current_attribute->setting_plugin != get_running_plugin()){
261 eef_log(LOG_ERR, "Argument %s is not owned by running plugin %s", current_attribute->id, get_running_plugin()->name);
262 return EES_FAILURE;
263 }
264 free(attribute->data);
265 free(attribute);
266 attribute = NULL;
267 if(last_attribute == NULL){
268 context->list_attributes = current_attribute->next;
269 } else {
270 last_attribute->next = current_attribute->next;
271 }
272 break;
273 }
274 }
275
276 if(current_attribute){
277 free(current_attribute->data);
278 free(current_attribute);
279 current_attribute = NULL;
280 return EES_SUCCESS;
281 }
282 return EES_FAILURE;
283 }
284
285 /**
286 * sets attribute id in supplied attribute
287 */
288 EES_RC setAttributeId(aos_attribute_t* attribute, char* id){
289 attribute->id = id;
290 return EES_SUCCESS;
291 }
292
293 /**
294 * sets attribute issuer in supplied attribute
295 */
296 EES_RC setAttributeIssuer(aos_attribute_t* attribute, char* issuer){
297 attribute->issuer = issuer;
298 return EES_SUCCESS;
299 }
300
301 /**
302 * sets attribute value in supplied attribute
303 */
304 EES_RC setAttributeValue(aos_attribute_t* attribute, void* value, size_t size){
305 if(value != NULL){
306 if(size == 0) {
307 attribute->data = value;
308 attribute->needs_free = 0;
309 } else {
310 attribute->data = calloc(1, size+1);
311 memcpy(attribute->data, value, size);
312 attribute->needs_free = 1;
313 }
314 } else {
315 return EES_FAILURE;
316 }
317 return EES_SUCCESS;
318 }
319
320 /**
321 * sets attribute type in supplied attribute
322 */
323 EES_RC setAttributeType(aos_attribute_t * attribute, char* type){
324 attribute->type = type;
325 return EES_SUCCESS;
326 }
327
328 /**
329 * returns id from supplied attribute
330 */
331 char* getAttributeId(aos_attribute_t* attribute){
332 return attribute->id;
333 }
334
335 /**
336 * returns issuer from supplied attribute
337 */
338 char* getAttributeIssuer(aos_attribute_t* attribute){
339 return attribute->issuer;
340 }
341
342 /**
343 * returns data from supplied attribute casted to a char*
344 */
345 char* getAttributeValueAsString(aos_attribute_t* attribute){
346 return (char*) attribute->data;
347 }
348
349 /**
350 * returns data from supplied attribute casted to a int
351 */
352 int getAttributeValueAsInt(aos_attribute_t* attribute){
353 char* string = getAttributeValueAsString(attribute);
354 return (int) strtol(string, NULL, 10);
355 }
356
357 void* aos_get_value_by_label(const char *label){
358 aos_attribute_t* attr = aos_get_attribute_by_label(label);
359 if(attr == NULL){
360 return NULL;
361 } else {
362 return attr->data;
363 }
364 }
365
366 aos_attribute_t * aos_get_attribute_by_label(const char *label){
367 aos_context_t * context;
368 aos_attribute_t * attribute;
369 aos_storage_t * storage = aos_get_storage();
370
371 while((context = getNextContext(ANY, storage))){
372 while((attribute = getNextAttribute(context))){
373 if(strncmp(attribute->id, label, strlen(label)) ==0){
374 return attribute;
375 }
376 }
377 }
378
379 storage = _global_storage;
380
381 while((context = getNextContext(ANY, storage))){
382 while((attribute = getNextAttribute(context))){
383 if(strncmp(attribute->id, label, strlen(label)) ==0){
384 return attribute;
385 }
386 }
387 }
388
389 return NULL;
390 }
391
392 /* AOS control functions */
393
394 /**
395 * Initializes the AOS and creates thread-local storage key
396 */
397 EES_RC AOS_Init (void){
398 _is_threading = 0;
399 _aos_key_once = PTHREAD_ONCE_INIT;
400 pthread_once(&_aos_key_once, aos_make_key);
401
402 if(!(_global_storage = calloc(1, sizeof(aos_storage_t)))){
403 return EES_FAILURE;
404 }
405
406 _is_initialized = 1;
407 return EES_SUCCESS;
408 }
409
410 /**
411 * Creates thread-local storage key
412 */
413 void aos_make_key(void){
414 pthread_key_create(&_aos_key, aos_free_key);
415 }
416
417 /**
418 * Frees thread-local storage key
419 */
420 void aos_free_key(void* storage){
421 pid_t tid;
422 /*tid = gettid();*/
423 tid = syscall(SYS_gettid);
424 eef_log(LOG_DEBUG, "Killing thread %i\n", tid);
425 aos_free_storage(storage);
426 }
427
428 /**
429 * Frees thread-local storage for this thread
430 */
431 EES_RC AOS_Clean(void){
432 pid_t tid;
433 /*tid = gettid();*/
434 tid = syscall(SYS_gettid);
435 eef_log(LOG_DEBUG, "Killing thread %i\n", tid);
436 if(!_is_threading){
437 return EES_FAILURE;
438 }
439 aos_free_storage(aos_get_storage());
440 return EES_SUCCESS;
441 }
442
443 /**
444 * Terminates AOS, deleting the thread-local storage key
445 */
446 EES_RC AOS_Term (void){
447 _is_threading = 0;
448 aos_free_storage(_global_storage);
449 pthread_setspecific(_aos_key, NULL);
450 pthread_key_delete(_aos_key);
451 _is_initialized = 0;
452 return EES_SUCCESS;
453 }
454
455 /**
456 * Dumps out all the data present in the AOS, as seen from the current thread's perspective
457 */
458 EES_RC aos_dump_argslist (void) {
459 aos_context_t * context;
460 aos_attribute_t* attribute;
461 char *log_str = "aos_dump";
462 char *attribute_name;
463 char *attribute_value;
464
465 eef_log(LOG_DEBUG, "Dumping aos");
466 rewindContexts(NULL);
467 while((context = getNextContext(ANY, NULL)) != NULL){
468 eef_log(LOG_DEBUG, "Context class: %i\n", context->context_class);
469 rewindAttributes(context);
470 while((attribute = getNextAttribute(context))){
471 attribute_name = getAttributeId(attribute);
472 attribute_value = getAttributeValueAsString(attribute);
473 if(attribute_name && attribute_value){
474 eef_log(LOG_DEBUG, "\t%s=%s\n", attribute_name, attribute_value);
475 }
476 }
477 }
478 return EES_SUCCESS;
479 }
480

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