/[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 1358 - (hide annotations) (download) (as text)
Mon Jan 11 10:30:06 2010 UTC (12 years, 4 months ago) by aramv
File MIME type: text/x-chdr
File size: 9917 byte(s)
Moving initializer to header fixes issue
1 aramv 502 #include "plugin_manager.h"
2 aramv 425 #include "aos.h"
3 aramv 484 #include "_aos.h"
4 aramv 425
5 aramv 886 /* internal methods */
6    
7 aramv 1142 int aos_is_initialized(){
8 aramv 473 return _is_initialized;
9     }
10    
11 aramv 1334 void aos_start_threading(){
12     _is_threading = 1;
13     }
14    
15 aramv 1332 aos_storage_t * aos_get_storage(){
16     pid_t tid;
17     aos_storage_t * storage;
18 aramv 1334
19     if(!_is_threading){
20     printf("Accessed global storage\n");
21     return _global_storage;
22     }
23    
24 aramv 1332 tid = syscall(SYS_gettid);
25     printf("Accessed storage in thread %i\n", tid);
26    
27     if(!(storage = pthread_getspecific(_aos_key))) {
28     if((storage = calloc(1, sizeof(aos_storage_t)))){
29 aramv 1345 if((storage->list_contexts = calloc(1, sizeof(aos_context_t)))){
30 aramv 1353 pthread_setspecific(_aos_key, storage);
31     printf("Created new TLS at %p\n", storage);
32 aramv 1332 }
33     }
34 aramv 1352 } else {
35     printf("Found existing TLS at %p\n", storage);
36 aramv 1332 }
37     return storage;
38     }
39    
40 aramv 1353 EES_RC aos_free_storage(aos_storage_t* storage){
41     aos_context_t *context = NULL, *next_context = NULL;
42     aos_attribute_t *attribute = NULL, *next_attribute = NULL;
43     int attribute_count = 0;
44 aramv 486
45 aramv 1353 if(!storage){
46 aramv 781 return EES_FAILURE;
47 aramv 486 }
48    
49 aramv 1353 printf("Cleaning aos storage at %p\n", storage);
50     if(storage == _global_storage){
51     printf("This is the GLOBAL storage\n");
52 aramv 425 }
53    
54 aramv 1353 context = storage->list_contexts;
55    
56     while(context){
57     next_context = context->next;
58     printf("Checking context at %p\n", context);
59     attribute = context->list_attributes;
60     while(attribute){
61     next_attribute = attribute->next;
62     /* free here */
63     printf("Checking attribute at %p\n", attribute);
64     if(attribute->needs_free){
65     eef_log(LOG_DEBUG, "Freeing arg %i at %p\n",attribute_count, attribute);
66     free(attribute->data);
67     free(attribute);
68     } else {
69     eef_log(LOG_DEBUG, "Not freeing arg %i at %p\n",attribute_count, attribute);
70     }
71     attribute = next_attribute;
72     attribute_count++;
73     }
74     /* free here */
75     printf("Freeing context at %p\n", context);
76     free(context);
77     context = next_context;
78 aramv 425 }
79 aramv 1353 free(storage);
80     return EES_SUCCESS;
81     }
82 aramv 473
83 aramv 1353 /* base functions */
84 aramv 425
85 aramv 1345 aos_context_t * createContext(aos_context_class_t context_class){
86     aos_context_t * context;
87     if((context = calloc(1, sizeof(aos_context_t)))){
88     context->context_class = context_class;
89     context->list_attributes = NULL;
90 aramv 1352 context->next = NULL;
91 aramv 488 }
92 aramv 1345 return context;
93 aramv 488 }
94    
95 aramv 1345 EES_RC addContext(aos_context_t* context){
96     aos_storage_t* storage = aos_get_storage();
97 aramv 1352 printf("Adding context at %p to storage at %p\n", context, storage);
98     if(storage->last_context){
99 aramv 1345 storage->last_context->next = context;
100 aramv 486 } else {
101 aramv 1345 storage->list_contexts = context;
102 aramv 486 }
103 aramv 1345 storage->last_context = context;
104     return EES_SUCCESS;
105 aramv 425 }
106    
107 aramv 1345 aos_context_t * getNextContext(aos_context_class_t context_class, aos_storage_t* storage){
108 aramv 1352 aos_context_t* current_context;
109 aramv 1345 if(!storage){
110     storage = aos_get_storage();
111     }
112 aramv 1352 if(storage->last_context != NULL){
113     current_context = storage->last_context->next;
114 aramv 1345 storage->last_context = current_context;
115     } else {
116 aramv 1352 current_context = storage->list_contexts;
117     while(current_context){
118     if((current_context->context_class == context_class) || (context_class == ANY) ){
119     break;
120 aramv 1345 }
121 aramv 1352 current_context = current_context->next;
122 aramv 460 }
123     }
124 aramv 1352 if(current_context){
125     return current_context;
126     } else {
127     return getNextContext(context_class, _global_storage);
128     }
129 aramv 1345 }
130 aramv 1334
131 aramv 1345 void setContextObligation(aos_context_t* context, char * obligation){
132     if(context->context_class == OBLIGATION){
133     context->obligation_name = obligation;
134     }
135     }
136 aramv 1334
137 aramv 1345 aos_attribute_t* createAttribute(void){
138     aos_attribute_t* attribute = NULL;
139     if((attribute = calloc(1, sizeof(aos_attribute_t)))){
140     attribute->next = NULL;
141     attribute->setting_plugin = get_running_plugin();
142 aramv 1334 }
143 aramv 1345 return attribute;
144     }
145 aramv 1334
146 aramv 1345 EES_RC addAttribute(aos_context_t * context, aos_attribute_t* attribute){
147 aramv 1352 printf("Adding attribute at %p to context at %p\n", attribute, context);
148     if(context->last_attribute){
149 aramv 1345 context->last_attribute->next = attribute;
150     } else {
151     context->list_attributes = attribute;
152     }
153     context->last_attribute = attribute;
154     return EES_SUCCESS;
155 aramv 460 }
156    
157 aramv 1345 aos_attribute_t* getNextAttribute(aos_context_t* context){
158     if(!context->last_attribute){
159     context->last_attribute = context->list_attributes;
160     } else {
161     context->last_attribute = context->last_attribute->next;
162 aramv 425 }
163 aramv 1345 return context->last_attribute;
164 aramv 425 }
165    
166 aramv 1345 EES_RC destroyAttribute(aos_context_t* context, aos_attribute_t* attribute){
167     aos_attribute_t *last_attribute = NULL, *current_attribute = NULL;
168     for(current_attribute = context->list_attributes; current_attribute != NULL; last_attribute = current_attribute, current_attribute = current_attribute->next){
169     if(current_attribute == attribute){
170     if(current_attribute->setting_plugin != get_running_plugin()){
171     eef_log(LOG_ERR, "Argument %s is not owned by running plugin %s", current_attribute->id, get_running_plugin()->name);
172 aramv 781 return EES_FAILURE;
173 aramv 502 }
174 aramv 1352 free(attribute->data);
175     free(attribute);
176     attribute = NULL;
177 aramv 1345 if(last_attribute == NULL){
178     context->list_attributes = current_attribute->next;
179 aramv 502 } else {
180 aramv 1345 last_attribute->next = current_attribute->next;
181 aramv 502 }
182 aramv 525 break;
183 aramv 488 }
184     }
185 aramv 502
186 aramv 1345 if(current_attribute){
187     free(current_attribute->data);
188     free(current_attribute);
189 aramv 1352 current_attribute = NULL;
190 aramv 1217 return EES_SUCCESS;
191 aramv 1345 }
192     return EES_FAILURE;
193     }
194    
195     EES_RC setAttributeId(aos_attribute_t* attribute, char* id){
196     attribute->id = id;
197     return EES_SUCCESS;
198     }
199    
200     EES_RC setAttributeIssuer(aos_attribute_t* attribute, char* issuer){
201     attribute->issuer = issuer;
202     return EES_SUCCESS;
203     }
204    
205     EES_RC setAttributeValue(aos_attribute_t* attribute, void* value, size_t size){
206     if(size == 0) {
207     attribute->data = value;
208     attribute->needs_free = 0;
209 aramv 1217 } else {
210 aramv 1345 attribute->data = calloc(1, size);
211     memcpy(attribute->data, value, size);
212     attribute->needs_free = 1;
213 aramv 1217 }
214 aramv 1352 return EES_SUCCESS;
215 aramv 1345 }
216 aramv 502
217 aramv 1345 EES_RC setAttributeType(aos_attribute_t * attribute, char* type){
218     attribute->type = type;
219 aramv 1352 return EES_SUCCESS;
220 aramv 488 }
221    
222 aramv 1345 char* getAttributeId(aos_attribute_t* attribute){
223     return attribute->id;
224     }
225    
226     char* getAttributeIssuer(aos_attribute_t* attribute){
227     return attribute->issuer;
228     }
229    
230     char* getAttributeValueAsString(aos_attribute_t* attribute){
231     return (char*) attribute->data;
232     }
233    
234     int getAttributeValueAsInt(aos_attribute_t* attribute){
235     return (int) (attribute->data);
236     }
237    
238 aramv 1353 /* needs more work */
239     EES_RC aos_delete_attribute_by_label(const char* label){
240     aos_attribute_t * current_attr = NULL;
241     aos_attribute_t * previous_attr = NULL;
242     aos_storage_t * storage = aos_get_storage();
243    
244     if(label == NULL){
245     return EES_FAILURE;
246     }
247    
248     /*for(current_attr = storage->list_attributes; current_attr != NULL; previous_attr = current_attr, current_attr = current_attr->next){*/
249     /*if(strncmp(current_attr->id, label, strlen(label)) == 0){*/
250     /*if(current_attr->setting_plugin != get_running_plugin()){*/
251     /*eef_log(LOG_ERR, "Argument %s is not owned by running plugin %s", current_attr->id, get_running_plugin()->name);*/
252     /*return EES_FAILURE;*/
253     /*}*/
254     /*if(previous_attr == NULL){*/
255     /**//* head node deleted. linking start of list to next node */
256     /*storage->list_arguments = current_attr->next;*/
257     /*} else {*/
258     /**//* link previous to deletee's next */
259     /*previous_attr->next = current_attr->next;*/
260     /*}*/
261     /*break;*/
262     /*}*/
263     /*}*/
264    
265     if(current_attr){
266     /*eef_log(LOG_ERR, "Deleting attr:%s", label);*/
267     free(current_attr->data);
268     free(current_attr);
269     current_attr = NULL;
270     return EES_SUCCESS;
271     } else {
272     return EES_FAILURE;
273     }
274    
275 aramv 1345 }
276    
277     void* aos_get_value_by_label(const char *label){
278     aos_attribute_t* attr = aos_get_attribute_by_label(label);
279     if(attr == NULL){
280     return NULL;
281     } else {
282     return attr->data;
283     }
284     }
285    
286     aos_attribute_t * aos_get_attribute_by_label(const char *label){
287     aos_context_t * context;
288     aos_attribute_t * attribute;
289     aos_storage_t * storage = aos_get_storage();
290    
291     while((context = getNextContext(ANY, storage))){
292     while((attribute = getNextAttribute(context))){
293     if(strncmp(attribute->id, label, strlen(label)) ==0){
294     return attribute;
295     }
296     }
297     }
298    
299     storage = _global_storage;
300    
301     while((context = getNextContext(ANY, storage))){
302     while((attribute = getNextAttribute(context))){
303     if(strncmp(attribute->id, label, strlen(label)) ==0){
304     return attribute;
305     }
306     }
307     }
308    
309     return NULL;
310     }
311    
312 aramv 781 /* initializer & terminator */
313    
314 aramv 872 EES_RC AOS_Init (void){
315 aramv 1334 _is_threading = 0;
316 aramv 1332 pthread_once(&_aos_key_once, aos_make_key);
317 aramv 1334
318     if((_global_storage = calloc(1, sizeof(aos_storage_t)))){
319 aramv 1352 if(!(_global_storage->list_contexts = calloc(1, sizeof(aos_context_t)))){
320     return EES_FAILURE;
321 aramv 1334 }
322 aramv 1352 } else {
323     return EES_FAILURE;
324 aramv 1334 }
325    
326 aramv 1332 _is_initialized = 1;
327     return EES_SUCCESS;
328     }
329 aramv 781
330 aramv 1332 void aos_make_key(void){
331 aramv 1353 pthread_key_create(&_aos_key, aos_free_key);
332 aramv 425 }
333    
334 aramv 1356 void aos_free_key(void* storage){
335 aramv 1353 aos_free_storage(storage);
336 aramv 1352 }
337    
338 aramv 1332 EES_RC AOS_Clean(void){
339 aramv 1352 pid_t tid;
340     tid = syscall(SYS_gettid);
341     printf("Killing thread %i\n", tid);
342     if(!_is_threading){
343     return EES_FAILURE;
344     }
345 aramv 1353 /*aos_free_storage(aos_get_storage());*/
346 aramv 1352 printf("Freed storage for thread %i\n", tid);
347 aramv 1332 return EES_SUCCESS;
348 aramv 1265 }
349 aramv 425
350 aramv 872 EES_RC AOS_Term (void){
351 aramv 1352 _is_threading = 0;
352 aramv 1334 aos_free_storage(_global_storage);
353 aramv 1352 pthread_setspecific(_aos_key, NULL);
354 aramv 1332 pthread_key_delete(_aos_key);
355     _is_initialized = 0;
356 aramv 1265 return EES_SUCCESS;
357 aramv 425 }
358 aramv 781
359 aramv 1332 EES_RC aos_dump_argslist (void) {
360 aramv 1345 aos_context_t * context;
361     aos_attribute_t* attribute;
362 aramv 1352 while((context = getNextContext(ANY, NULL))){
363 aramv 1345 if(context->context_class == OBLIGATION){
364     printf("Context obligation: %s\n", context->obligation_name);
365     }
366 aramv 1352 while((attribute = getNextAttribute(context))){
367 aramv 1345 printf("Attribute: %s with value: %s\n", attribute->id, attribute->data);
368     }
369 okoeroo 1307 }
370     return EES_SUCCESS;
371     }
372    

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