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

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