/[pdpsoft]/trunk/grid-mw-security/ees/src/aos/aos.c
ViewVC logotype

Diff of /trunk/grid-mw-security/ees/src/aos/aos.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1344 by aramv, Tue Jan 5 14:43:37 2010 UTC revision 1345 by aramv, Thu Jan 7 16:38:30 2010 UTC
# Line 91  aos_storage_t * aos_get_storage(){ Line 91  aos_storage_t * aos_get_storage(){
91    
92    if(!(storage = pthread_getspecific(_aos_key))) {    if(!(storage = pthread_getspecific(_aos_key))) {
93      if((storage = calloc(1, sizeof(aos_storage_t)))){      if((storage = calloc(1, sizeof(aos_storage_t)))){
94        storage->list_arguments = NULL;        if((storage->list_contexts = calloc(1, sizeof(aos_context_t)))){
       if((storage->size_arguments = calloc(1, sizeof(int)))){  
         *(storage->size_arguments) = 0;  
95        }        }
96        pthread_setspecific(_aos_key, storage);        pthread_setspecific(_aos_key, storage);
97      }      }
# Line 101  aos_storage_t * aos_get_storage(){ Line 99  aos_storage_t * aos_get_storage(){
99    return storage;    return storage;
100  }  }
101    
 EES_RC aos_free_storage(aos_storage_t * storage){  
   free(storage->size_arguments);  
   free(storage);  
   return EES_SUCCESS;  
 }  
   
102  /* base functions */  /* base functions */
103    
104  EES_RC aos_set(const char *label, void *value, eef_arg_type type, size_t arg_bufsize, aos_argument_t * parent, aos_argument_t * child){  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;    aos_argument_t *  args_new  = NULL;
106      aos_attribute_t*  attr_new  = NULL;
107    aos_argument_t *  last_node = NULL;    aos_argument_t *  last_node = NULL;
108    aos_storage_t  *  storage   = aos_get_storage();    aos_storage_t  *  storage   = aos_get_storage();
109    size_t            buf_size  = arg_bufsize;    size_t            buf_size  = arg_bufsize;
# Line 126  EES_RC aos_set(const char *label, void * Line 119  EES_RC aos_set(const char *label, void *
119    }    }
120    
121    /* allocate an extra argument in the list */    /* allocate an extra argument in the list */
122    if((args_new = calloc(1, (sizeof(aos_argument_t)))) == NULL){    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));                  eef_log(LOG_ERR, "Failed to allocate a new argument on the list. %s", strerror(errno));
124      return EES_FAILURE;      return EES_FAILURE;
125    } else {    } else {
# Line 134  EES_RC aos_set(const char *label, void * Line 127  EES_RC aos_set(const char *label, void *
127    }    }
128    
129    if(buf_size == 0) {    if(buf_size == 0) {
130      args_new->data = value;      attr_new->data = value;
131      args_new->needs_free = 0;      attr_new->needs_free = 0;
132      /*eef_log(LOG_DEBUG, "1 pointer allocated at %p. No need to free!\n", args_new->data);*/      /*eef_log(LOG_DEBUG, "1 pointer allocated at %p. No need to free!\n", args_new->data);*/
133    } else {    } else {
134      args_new->data = calloc(1, buf_size);      attr_new->data = calloc(1, buf_size);
135      /*eef_log(LOG_DEBUG, "Storing bytes:%i\n", buf_size);*/      /*eef_log(LOG_DEBUG, "Storing bytes:%i\n", buf_size);*/
136      memcpy(args_new->data, value, buf_size);      memcpy(attr_new->data, value, buf_size);
137      args_new->needs_free = 1;      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));*/      /*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          args_new->type           = type;    /*attr_new->type           = type;*/
142          args_new->label          = label;    /*attr_new->id             = label;*/
143          args_new->parent         = parent;          attr_new->parent         = parent;
144          args_new->child          = child;          attr_new->child          = child;
145    args_new->next           = NULL;    attr_new->next           = NULL;
146    args_new->setting_plugin = get_running_plugin();    attr_new->setting_plugin = get_running_plugin();
147    
148    last_node = aos_get_last_node();    /*last_node = aos_get_last_node();*/
149    if(last_node != NULL){    /*if(last_node != NULL){*/
150      (last_node)->next = args_new;    /*(last_node)->next = attr_new;*/
151    } else {    /*} else {*/
152      storage->list_arguments = args_new;    /*storage->list_attributes = attr_new;*/
153    }    /*}*/
154    
   (*(storage->size_arguments))++;  
155          return EES_SUCCESS;          return EES_SUCCESS;
156  }  }
157    
158  aos_argument_t * aos_get_last_node(void){  aos_context_t * createContext(aos_context_class_t context_class){
159    aos_argument_t * current_arg;    aos_context_t * context;
160    aos_storage_t  * storage   = aos_get_storage();    if((context = calloc(1, sizeof(aos_context_t)))){
161    unsigned int i;      context->context_class = context_class;
162    current_arg = storage->list_arguments;      context->list_attributes = NULL;
163    for(i = 0; i < (*(storage->size_arguments)); i++){    }
164      if(current_arg->next != NULL){    return context;
       current_arg = current_arg->next;  
     }  
   }  
   return current_arg;  
165  }  }
166    
167  void* aos_get_value_by_label(const char *label){  EES_RC addContext(aos_context_t* context){
168          aos_argument_t* arg = aos_get_argument_by_label(label);    aos_storage_t* storage = aos_get_storage();
169    if(arg == NULL){    if(storage->list_contexts){
170      return NULL;      storage->last_context->next = context;
171    } else {    } else {
172      return arg->data;      storage->list_contexts = context;
173    }    }
174      storage->last_context = context;
175      return EES_SUCCESS;
176  }  }
177    
178  aos_argument_t * aos_get_argument_by_label(const char *label){  aos_context_t * getNextContext(aos_context_class_t context_class, aos_storage_t* storage){
179    unsigned int     i;    aos_context_t* current_context = storage->list_contexts;
180    aos_storage_t  * storage = aos_get_storage();    if(!storage){
181    aos_argument_t * current_arg = storage->list_arguments;      storage = aos_get_storage();
182      }
183    for(i = 0; i < (*(storage->size_arguments)); i++){    if(storage->last_context == NULL && current_context->context_class == context_class){
184      if(strncmp(current_arg->label, label, strlen(label))==0){      storage->last_context = current_context;
185        return current_arg;    } else {
186      }      if(context_class != ANY){
187      if(current_arg->next != NULL){        while(storage->last_context->context_class != context_class){
188        current_arg = current_arg->next;          storage->last_context = storage->last_context->next;
189          }
190      }      }
191    }    }
192      return storage->last_context;
193    }
194    
195    storage = _global_storage;  void setContextObligation(aos_context_t* context, char * obligation){
196    current_arg = storage->list_arguments;    if(context->context_class == OBLIGATION){
197        context->obligation_name = obligation;
   for(i = 0; i < (*(storage->size_arguments)); i++){  
     if(strncmp(current_arg->label, label, strlen(label))==0){  
       return current_arg;  
     }  
     if(current_arg->next != NULL){  
       current_arg = current_arg->next;  
     }  
198    }    }
199    }
200    
201    return NULL;  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  /* frees every copied byte */  EES_RC addAttribute(aos_context_t * context, aos_attribute_t* attribute){
211  void aos_free_argslist(aos_storage_t* storage) {    if(context->list_attributes){
212    unsigned int i;      context->last_attribute->next = attribute;
213    aos_argument_t * current_arg = storage->list_arguments;    } else {
214    aos_argument_t * next_arg;      context->list_attributes = attribute;
215      }
216      context->last_attribute = attribute;
217      return EES_SUCCESS;
218    }
219    
220    for(i = 0; i < (*(storage->size_arguments)); i++){  aos_attribute_t* getNextAttribute(aos_context_t* context){
221      eef_log(LOG_DEBUG, "Element %i points to %p\n", i, current_arg->data);    if(!context->last_attribute){
222      next_arg = current_arg->next;      context->last_attribute = context->list_attributes;
223      if(current_arg->needs_free){    } else {
224        eef_log(LOG_DEBUG, "Freeing arg %i\n",i);      context->last_attribute = context->last_attribute->next;
       free(current_arg->data);  
       free(current_arg);  
     }  
     current_arg = next_arg;  
225    }    }
226    (*(storage->size_arguments)) = 0;    return context->last_attribute;
227  }  }
228    
229  EES_RC aos_delete_argument_by_label(const char* label){  
230    aos_argument_t * current_arg = NULL;  EES_RC aos_delete_attribute_by_label(const char* label){
231    aos_argument_t * previous_arg = NULL;    aos_attribute_t * current_attr  = NULL;
232    aos_storage_t  * storage = aos_get_storage();    aos_attribute_t * previous_attr = NULL;
233      aos_storage_t   * storage       = aos_get_storage();
234    
235    if(label == NULL){    if(label == NULL){
236      return EES_FAILURE;      return EES_FAILURE;
237    }    }
238    
239    for(current_arg = storage->list_arguments; current_arg != NULL; previous_arg = current_arg, current_arg = current_arg->next){    /*for(current_attr = storage->list_attributes; current_attr != NULL; previous_attr = current_attr, current_attr = current_attr->next){*/
240      if(strncmp(current_arg->label, label, strlen(label)) == 0){    /*if(strncmp(current_attr->id, label, strlen(label)) == 0){*/
241        if(current_arg->setting_plugin != get_running_plugin()){    /*if(current_attr->setting_plugin != get_running_plugin()){*/
242          eef_log(LOG_ERR, "Argument %s is not owned by running plugin %s", current_arg->label, get_running_plugin()->name);    /*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;          return EES_FAILURE;
275        }        }
276        if(previous_arg == NULL){        if(last_attribute == NULL){
277          /* head node deleted. linking start of list to next node */          context->list_attributes = current_attribute->next;
         storage->list_arguments = current_arg->next;  
278        } else {        } else {
279          /* link previous to deletee's next */          last_attribute->next = current_attribute->next;
         previous_arg->next = current_arg->next;  
280        }        }
281        break;        break;
282      }      }
283    }    }
284    
285    if(current_arg){    if(current_attribute){
286      /*eef_log(LOG_ERR, "Deleting arg:%s", label);*/      free(current_attribute->data);
287      free(current_arg->data);      free(current_attribute);
     free(current_arg);  
     (*(storage)->size_arguments)--;  
288      return EES_SUCCESS;      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 {    } else {
308      return EES_FAILURE;      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 */  /* initializer & terminator */
# Line 279  EES_RC AOS_Init (void){ Line 404  EES_RC AOS_Init (void){
404    pthread_once(&_aos_key_once, aos_make_key);    pthread_once(&_aos_key_once, aos_make_key);
405    
406    if((_global_storage = calloc(1, sizeof(aos_storage_t)))){    if((_global_storage = calloc(1, sizeof(aos_storage_t)))){
407      _global_storage->list_arguments = NULL;      if((_global_storage->list_contexts = calloc(1, sizeof(aos_context_t)))){
     if((_global_storage->size_arguments = calloc(1, sizeof(int)))){  
       *(_global_storage->size_arguments) = 0;  
408      }      }
409    }    }
410    
# Line 294  void aos_make_key(void){ Line 417  void aos_make_key(void){
417  }  }
418    
419  EES_RC AOS_Clean(void){  EES_RC AOS_Clean(void){
   aos_free_argslist(aos_get_storage());  
420    aos_free_storage(aos_get_storage());    aos_free_storage(aos_get_storage());
421    return EES_SUCCESS;    return EES_SUCCESS;
422  }  }
423    
424  EES_RC AOS_Term (void){  EES_RC AOS_Term (void){
   aos_free_argslist(_global_storage);  
425    aos_free_storage(_global_storage);    aos_free_storage(_global_storage);
426    pthread_key_delete(_aos_key);    pthread_key_delete(_aos_key);
427    _is_initialized = 0;    _is_initialized = 0;
428    return EES_SUCCESS;    return EES_SUCCESS;
429  }  }
430    
 /* general high-level wrapper functions */  
   
 EES_RC aos_set_int(const char* label, int value){  
   int* stack_value = &value;  
   return aos_set(label, stack_value, TYPE_INT, 0, NULL, NULL);  
 }  
   
 EES_RC aos_set_string(const char* label, char* value){  
   return aos_set(label, value, TYPE_STRING, strlen(value)+1, NULL, NULL);  
 }  
   
 EES_RC aos_set_pem(const char* label, char* value){  
   return aos_set(label, value, TYPE_PEM, strlen(value)+1, NULL, NULL);  
 }  
   
 /* AOS high-level wrapper functions */  
   
 EES_RC setAssertion(const char* label, char* value){  
   return aos_set(label, value, TYPE_STRING, strlen(value)+1, NULL, NULL);  
 }  
   
 char* getAssertion(const char* label){  
   return aos_get_value_by_label(label);  
 }  
   
 EES_RC setObligation(const char* label, char* value){  
   if(aos_set(label, value, TYPE_STRING, strlen(value)+1, NULL, NULL)){  
     return EES_SUCCESS;  
   } else {  
     return EES_FAILURE;  
   }  
 }  
   
 char* getObligation(const char* label){  
   return aos_get_value_by_label(label);  
 }  
   
 EES_RC destroyObligation(const char* label){  
   return aos_delete_argument_by_label(label);  
 }  
   
431  EES_RC aos_dump_argslists (void){  EES_RC aos_dump_argslists (void){
432    eef_log(LOG_ERR, "Dumping AOS\n");    eef_log(LOG_ERR, "Dumping AOS\n");
433    aos_dump_argslist();    aos_dump_argslist();
# Line 356  EES_RC aos_dump_argslists (void){ Line 436  EES_RC aos_dump_argslists (void){
436    
437  EES_RC aos_dump_argslist (void) {  EES_RC aos_dump_argslist (void) {
438    unsigned int i;    unsigned int i;
439    aos_storage_t  * storage = aos_get_storage();    aos_context_t  * context;
440    aos_argument_t * current_arg = storage->list_arguments;    aos_attribute_t* attribute;
441    for(i = 0; i < (*(storage->size_arguments)); i++){    while(context = getNextContext(ANY, NULL)){
442      eef_log(LOG_ERR, "Element %03i - at point %p - label \"%s\" - needs_free %s\n", i, current_arg, current_arg->label, current_arg -> needs_free ? "yes" : "no");      if(context->context_class == OBLIGATION){
443      eef_log(LOG_ERR, "            - type %d - \"%s\"", current_arg -> type,  current_arg -> type == TYPE_STRING ? (char *) current_arg -> data : "non-string data");        printf("Context obligation: %s\n", context->obligation_name);
444      current_arg = current_arg->next;      }
445        while(attribute = getNextAttribute(context)){
446          printf("Attribute: %s with value: %s\n", attribute->id, attribute->data);
447        }
448    }    }
449    return EES_SUCCESS;    return EES_SUCCESS;
450  }  }

Legend:
Removed from v.1344  
changed lines
  Added in v.1345

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