/[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 1345 - (show annotations) (download) (as text)
Thu Jan 7 16:38:30 2010 UTC (12 years ago) by aramv
File MIME type: text/x-chdr
File size: 12304 byte(s)
Implemented redesigned AOS API - Removed references to older version
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 size_t aos_buffer_size(eef_arg_type argtype){
16 switch (argtype){
17 case TYPE_NONE :
18 return 0;
19 case TYPE_SCHAR :
20 return sizeof(int);
21 case TYPE_UCHAR :
22 return sizeof(unsigned int);
23 case TYPE_SHORT :
24 return sizeof(int);
25 case TYPE_USHORT :
26 return sizeof(unsigned int);
27 case TYPE_INT :
28 return sizeof(int);
29 case TYPE_UINT :
30 return sizeof(unsigned int);
31 case TYPE_LONGINT :
32 return sizeof(long int);
33 case TYPE_ULONGINT :
34 return sizeof(unsigned long int);
35 #ifdef HAVE_LONG_LONG_INT
36 case TYPE_LONGLONGINT :
37 return sizeof(long long int);
38 case TYPE_ULONGLONGINT :
39 return sizeof(unsigned long long int);
40 #endif
41 case TYPE_FLOAT :
42 return sizeof(double);
43 case TYPE_DOUBLE :
44 return sizeof(double);
45 #ifdef HAVE_LONG_DOUBLE
46 case TYPE_LONGDOUBLE :
47 return sizeof(long double);
48 #endif
49 case TYPE_CHAR :
50 return sizeof(int);
51 #ifdef HAVE_WINT_T
52 case TYPE_WIDE_CHAR :
53 return sizeof(wint_t);
54 #endif
55 case TYPE_STRING :
56 return sizeof(const char*);
57 #ifdef HAVE_WCHAR_T
58 case TYPE_WIDE_STRING :
59 return sizeof(const wchar_t*);
60 #endif
61 case TYPE_POINTER :
62 return sizeof(void*);
63 case TYPE_COUNT_SCHAR_POINTER :
64 return sizeof(signed char*);
65 case TYPE_COUNT_SHORT_POINTER :
66 return sizeof(short *);
67 case TYPE_COUNT_INT_POINTER :
68 return sizeof(int*);
69 case TYPE_COUNT_LONGINT_POINTER :
70 return sizeof(long int*);
71 #ifdef HAVE_LONG_LONG_INT
72 case TYPE_COUNT_LONGLONGINT_POINTER :
73 return sizeof(long long int*);
74 #endif
75 default:
76 return 0;
77 }
78 }
79
80 aos_storage_t * aos_get_storage(){
81 pid_t tid;
82 aos_storage_t * storage;
83
84 if(!_is_threading){
85 printf("Accessed global storage\n");
86 return _global_storage;
87 }
88
89 tid = syscall(SYS_gettid);
90 printf("Accessed storage in thread %i\n", tid);
91
92 if(!(storage = pthread_getspecific(_aos_key))) {
93 if((storage = calloc(1, sizeof(aos_storage_t)))){
94 if((storage->list_contexts = calloc(1, sizeof(aos_context_t)))){
95 }
96 pthread_setspecific(_aos_key, storage);
97 }
98 }
99 return storage;
100 }
101
102 /* base functions */
103
104 EES_RC aos_set(const char *label, void *value, eef_arg_type type, size_t arg_bufsize, aos_attribute_t * parent, aos_attribute_t * child){
105 aos_argument_t * args_new = NULL;
106 aos_attribute_t* attr_new = NULL;
107 aos_argument_t * last_node = NULL;
108 aos_storage_t * storage = aos_get_storage();
109 size_t buf_size = arg_bufsize;
110
111 if(!aos_is_initialized()){
112 eef_log(LOG_ERR, "AOS is not yet initialized. Aborting.");
113 return EES_FAILURE;
114 }
115
116 if(!(aos_get_value_by_label(label) == NULL)){
117 eef_log(LOG_ERR, "Block with label %s already exists.", label);
118 return EES_FAILURE;
119 }
120
121 /* allocate an extra argument in the list */
122 if((attr_new = calloc(1, (sizeof(aos_attribute_t)))) == NULL){
123 eef_log(LOG_ERR, "Failed to allocate a new argument on the list. %s", strerror(errno));
124 return EES_FAILURE;
125 } else {
126 /*eef_log(LOG_DEBUG, "Malloc succeeded. Argument lists is now %lu elements.\n", (unsigned long) (*args_size));*/
127 }
128
129 if(buf_size == 0) {
130 attr_new->data = value;
131 attr_new->needs_free = 0;
132 /*eef_log(LOG_DEBUG, "1 pointer allocated at %p. No need to free!\n", args_new->data);*/
133 } else {
134 attr_new->data = calloc(1, buf_size);
135 /*eef_log(LOG_DEBUG, "Storing bytes:%i\n", buf_size);*/
136 memcpy(attr_new->data, value, buf_size);
137 attr_new->needs_free = 1;
138 /*eef_log(LOG_DEBUG, "%i bytes allocated at %p. (index %lu) Remember to free it!\n", buf_size, args_new->data, (unsigned long) (*args_size));*/
139 }
140
141 /*attr_new->type = type;*/
142 /*attr_new->id = label;*/
143 attr_new->parent = parent;
144 attr_new->child = child;
145 attr_new->next = NULL;
146 attr_new->setting_plugin = get_running_plugin();
147
148 /*last_node = aos_get_last_node();*/
149 /*if(last_node != NULL){*/
150 /*(last_node)->next = attr_new;*/
151 /*} else {*/
152 /*storage->list_attributes = attr_new;*/
153 /*}*/
154
155 return EES_SUCCESS;
156 }
157
158 aos_context_t * createContext(aos_context_class_t context_class){
159 aos_context_t * context;
160 if((context = calloc(1, sizeof(aos_context_t)))){
161 context->context_class = context_class;
162 context->list_attributes = NULL;
163 }
164 return context;
165 }
166
167 EES_RC addContext(aos_context_t* context){
168 aos_storage_t* storage = aos_get_storage();
169 if(storage->list_contexts){
170 storage->last_context->next = context;
171 } else {
172 storage->list_contexts = context;
173 }
174 storage->last_context = context;
175 return EES_SUCCESS;
176 }
177
178 aos_context_t * getNextContext(aos_context_class_t context_class, aos_storage_t* storage){
179 aos_context_t* current_context = storage->list_contexts;
180 if(!storage){
181 storage = aos_get_storage();
182 }
183 if(storage->last_context == NULL && current_context->context_class == context_class){
184 storage->last_context = current_context;
185 } else {
186 if(context_class != ANY){
187 while(storage->last_context->context_class != context_class){
188 storage->last_context = storage->last_context->next;
189 }
190 }
191 }
192 return storage->last_context;
193 }
194
195 void setContextObligation(aos_context_t* context, char * obligation){
196 if(context->context_class == OBLIGATION){
197 context->obligation_name = obligation;
198 }
199 }
200
201 aos_attribute_t* createAttribute(void){
202 aos_attribute_t* attribute = NULL;
203 if((attribute = calloc(1, sizeof(aos_attribute_t)))){
204 attribute->next = NULL;
205 attribute->setting_plugin = get_running_plugin();
206 }
207 return attribute;
208 }
209
210 EES_RC addAttribute(aos_context_t * context, aos_attribute_t* attribute){
211 if(context->list_attributes){
212 context->last_attribute->next = attribute;
213 } else {
214 context->list_attributes = attribute;
215 }
216 context->last_attribute = attribute;
217 return EES_SUCCESS;
218 }
219
220 aos_attribute_t* getNextAttribute(aos_context_t* context){
221 if(!context->last_attribute){
222 context->last_attribute = context->list_attributes;
223 } else {
224 context->last_attribute = context->last_attribute->next;
225 }
226 return context->last_attribute;
227 }
228
229
230 EES_RC aos_delete_attribute_by_label(const char* label){
231 aos_attribute_t * current_attr = NULL;
232 aos_attribute_t * previous_attr = NULL;
233 aos_storage_t * storage = aos_get_storage();
234
235 if(label == NULL){
236 return EES_FAILURE;
237 }
238
239 /*for(current_attr = storage->list_attributes; current_attr != NULL; previous_attr = current_attr, current_attr = current_attr->next){*/
240 /*if(strncmp(current_attr->id, label, strlen(label)) == 0){*/
241 /*if(current_attr->setting_plugin != get_running_plugin()){*/
242 /*eef_log(LOG_ERR, "Argument %s is not owned by running plugin %s", current_attr->id, get_running_plugin()->name);*/
243 /*return EES_FAILURE;*/
244 /*}*/
245 /*if(previous_attr == NULL){*/
246 /**//* head node deleted. linking start of list to next node */
247 /*storage->list_arguments = current_attr->next;*/
248 /*} else {*/
249 /**//* link previous to deletee's next */
250 /*previous_attr->next = current_attr->next;*/
251 /*}*/
252 /*break;*/
253 /*}*/
254 /*}*/
255
256 if(current_attr){
257 /*eef_log(LOG_ERR, "Deleting attr:%s", label);*/
258 free(current_attr->data);
259 free(current_attr);
260 return EES_SUCCESS;
261 } else {
262 return EES_FAILURE;
263 }
264
265 }
266
267
268 EES_RC destroyAttribute(aos_context_t* context, aos_attribute_t* attribute){
269 aos_attribute_t *last_attribute = NULL, *current_attribute = NULL;
270 for(current_attribute = context->list_attributes; current_attribute != NULL; last_attribute = current_attribute, current_attribute = current_attribute->next){
271 if(current_attribute == attribute){
272 if(current_attribute->setting_plugin != get_running_plugin()){
273 eef_log(LOG_ERR, "Argument %s is not owned by running plugin %s", current_attribute->id, get_running_plugin()->name);
274 return EES_FAILURE;
275 }
276 if(last_attribute == NULL){
277 context->list_attributes = current_attribute->next;
278 } else {
279 last_attribute->next = current_attribute->next;
280 }
281 break;
282 }
283 }
284
285 if(current_attribute){
286 free(current_attribute->data);
287 free(current_attribute);
288 return EES_SUCCESS;
289 }
290 return EES_FAILURE;
291 }
292
293 EES_RC setAttributeId(aos_attribute_t* attribute, char* id){
294 attribute->id = id;
295 return EES_SUCCESS;
296 }
297
298 EES_RC setAttributeIssuer(aos_attribute_t* attribute, char* issuer){
299 attribute->issuer = issuer;
300 return EES_SUCCESS;
301 }
302
303 EES_RC setAttributeValue(aos_attribute_t* attribute, void* value, size_t size){
304 if(size == 0) {
305 attribute->data = value;
306 attribute->needs_free = 0;
307 } else {
308 attribute->data = calloc(1, size);
309 memcpy(attribute->data, value, size);
310 attribute->needs_free = 1;
311 }
312 }
313
314 EES_RC setAttributeType(aos_attribute_t * attribute, char* type){
315 attribute->type = type;
316 }
317
318 char* getAttributeId(aos_attribute_t* attribute){
319 return attribute->id;
320 }
321
322 char* getAttributeIssuer(aos_attribute_t* attribute){
323 return attribute->issuer;
324 }
325
326 char* getAttributeValueAsString(aos_attribute_t* attribute){
327 return (char*) attribute->data;
328 }
329
330 int getAttributeValueAsInt(aos_attribute_t* attribute){
331 return (int) (attribute->data);
332 }
333
334 aos_attribute_t * aos_get_last_node(void){
335 aos_attribute_t * current_attr;
336 aos_storage_t * storage = aos_get_storage();
337 unsigned int i;
338 return current_attr;
339 }
340
341 void* aos_get_value_by_label(const char *label){
342 aos_attribute_t* attr = aos_get_attribute_by_label(label);
343 if(attr == NULL){
344 return NULL;
345 } else {
346 return attr->data;
347 }
348 }
349
350 aos_attribute_t * aos_get_attribute_by_label(const char *label){
351 unsigned int i;
352 aos_context_t * context;
353 aos_attribute_t * attribute;
354 aos_storage_t * storage = aos_get_storage();
355
356 while((context = getNextContext(ANY, storage))){
357 while((attribute = getNextAttribute(context))){
358 if(strncmp(attribute->id, label, strlen(label)) ==0){
359 return attribute;
360 }
361 }
362 }
363
364 storage = _global_storage;
365
366 while((context = getNextContext(ANY, storage))){
367 while((attribute = getNextAttribute(context))){
368 if(strncmp(attribute->id, label, strlen(label)) ==0){
369 return attribute;
370 }
371 }
372 }
373
374 return NULL;
375 }
376
377 EES_RC aos_free_storage(aos_storage_t* storage){
378 aos_context_t *context = NULL, *next_context = NULL;
379 aos_attribute_t *attribute = NULL, *next_attribute = NULL;
380 int attribute_count = 0;
381 context = storage->list_contexts;
382 while(context){
383 next_context = context->next;
384 while(attribute){
385 next_attribute = attribute->next;
386 /* free here */
387 if(attribute->needs_free){
388 eef_log(LOG_DEBUG, "Freeing arg %i\n",attribute_count);
389 }
390 attribute = next_attribute;
391 attribute_count++;
392 }
393 /* free here */
394 context = next_context;
395 }
396 return EES_SUCCESS;
397 }
398
399 /* initializer & terminator */
400
401 EES_RC AOS_Init (void){
402 _aos_key_once = PTHREAD_ONCE_INIT;
403 _is_threading = 0;
404 pthread_once(&_aos_key_once, aos_make_key);
405
406 if((_global_storage = calloc(1, sizeof(aos_storage_t)))){
407 if((_global_storage->list_contexts = calloc(1, sizeof(aos_context_t)))){
408 }
409 }
410
411 _is_initialized = 1;
412 return EES_SUCCESS;
413 }
414
415 void aos_make_key(void){
416 pthread_key_create(&_aos_key, NULL);
417 }
418
419 EES_RC AOS_Clean(void){
420 aos_free_storage(aos_get_storage());
421 return EES_SUCCESS;
422 }
423
424 EES_RC AOS_Term (void){
425 aos_free_storage(_global_storage);
426 pthread_key_delete(_aos_key);
427 _is_initialized = 0;
428 return EES_SUCCESS;
429 }
430
431 EES_RC aos_dump_argslists (void){
432 eef_log(LOG_ERR, "Dumping AOS\n");
433 aos_dump_argslist();
434 return EES_SUCCESS;
435 }
436
437 EES_RC aos_dump_argslist (void) {
438 unsigned int i;
439 aos_context_t * context;
440 aos_attribute_t* attribute;
441 while(context = getNextContext(ANY, NULL)){
442 if(context->context_class == OBLIGATION){
443 printf("Context obligation: %s\n", context->obligation_name);
444 }
445 while(attribute = getNextAttribute(context)){
446 printf("Attribute: %s with value: %s\n", attribute->id, attribute->data);
447 }
448 }
449 return EES_SUCCESS;
450 }
451

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