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

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