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 |
if((storage->list_contexts = calloc(1, sizeof(aos_context_t)))){ |
30 |
pthread_setspecific(_aos_key, storage); |
31 |
printf("Created new TLS at %p\n", storage); |
32 |
} |
33 |
} |
34 |
} else { |
35 |
printf("Found existing TLS at %p\n", storage); |
36 |
} |
37 |
return storage; |
38 |
} |
39 |
|
40 |
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 |
|
45 |
if(!storage){ |
46 |
return EES_FAILURE; |
47 |
} |
48 |
|
49 |
printf("Cleaning aos storage at %p\n", storage); |
50 |
if(storage == _global_storage){ |
51 |
printf("This is the GLOBAL storage\n"); |
52 |
} |
53 |
|
54 |
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 |
} |
79 |
free(storage); |
80 |
return EES_SUCCESS; |
81 |
} |
82 |
|
83 |
/* base functions */ |
84 |
|
85 |
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 |
context->next = NULL; |
91 |
} |
92 |
return context; |
93 |
} |
94 |
|
95 |
EES_RC addContext(aos_context_t* context){ |
96 |
aos_storage_t* storage = aos_get_storage(); |
97 |
printf("Adding context at %p to storage at %p\n", context, storage); |
98 |
if(storage->last_context){ |
99 |
storage->last_context->next = context; |
100 |
} else { |
101 |
storage->list_contexts = context; |
102 |
} |
103 |
storage->last_context = context; |
104 |
return EES_SUCCESS; |
105 |
} |
106 |
|
107 |
aos_context_t * getNextContext(aos_context_class_t context_class, aos_storage_t* storage){ |
108 |
aos_context_t* current_context; |
109 |
if(!storage){ |
110 |
storage = aos_get_storage(); |
111 |
} |
112 |
if(storage->last_context != NULL){ |
113 |
current_context = storage->last_context->next; |
114 |
storage->last_context = current_context; |
115 |
} else { |
116 |
current_context = storage->list_contexts; |
117 |
while(current_context){ |
118 |
if((current_context->context_class == context_class) || (context_class == ANY) ){ |
119 |
break; |
120 |
} |
121 |
current_context = current_context->next; |
122 |
} |
123 |
} |
124 |
if(current_context){ |
125 |
return current_context; |
126 |
} else { |
127 |
return getNextContext(context_class, _global_storage); |
128 |
} |
129 |
} |
130 |
|
131 |
void setContextObligation(aos_context_t* context, char * obligation){ |
132 |
if(context->context_class == OBLIGATION){ |
133 |
context->obligation_name = obligation; |
134 |
} |
135 |
} |
136 |
|
137 |
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 |
} |
143 |
return attribute; |
144 |
} |
145 |
|
146 |
EES_RC addAttribute(aos_context_t * context, aos_attribute_t* attribute){ |
147 |
printf("Adding attribute at %p to context at %p\n", attribute, context); |
148 |
if(context->last_attribute){ |
149 |
context->last_attribute->next = attribute; |
150 |
} else { |
151 |
context->list_attributes = attribute; |
152 |
} |
153 |
context->last_attribute = attribute; |
154 |
return EES_SUCCESS; |
155 |
} |
156 |
|
157 |
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 |
} |
163 |
return context->last_attribute; |
164 |
} |
165 |
|
166 |
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 |
return EES_FAILURE; |
173 |
} |
174 |
free(attribute->data); |
175 |
free(attribute); |
176 |
attribute = NULL; |
177 |
if(last_attribute == NULL){ |
178 |
context->list_attributes = current_attribute->next; |
179 |
} else { |
180 |
last_attribute->next = current_attribute->next; |
181 |
} |
182 |
break; |
183 |
} |
184 |
} |
185 |
|
186 |
if(current_attribute){ |
187 |
free(current_attribute->data); |
188 |
free(current_attribute); |
189 |
current_attribute = NULL; |
190 |
return EES_SUCCESS; |
191 |
} |
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 |
} else { |
210 |
attribute->data = calloc(1, size); |
211 |
memcpy(attribute->data, value, size); |
212 |
attribute->needs_free = 1; |
213 |
} |
214 |
return EES_SUCCESS; |
215 |
} |
216 |
|
217 |
EES_RC setAttributeType(aos_attribute_t * attribute, char* type){ |
218 |
attribute->type = type; |
219 |
return EES_SUCCESS; |
220 |
} |
221 |
|
222 |
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 |
/* 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 |
} |
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 |
unsigned int i; |
288 |
aos_context_t * context; |
289 |
aos_attribute_t * attribute; |
290 |
aos_storage_t * storage = aos_get_storage(); |
291 |
|
292 |
while((context = getNextContext(ANY, storage))){ |
293 |
while((attribute = getNextAttribute(context))){ |
294 |
if(strncmp(attribute->id, label, strlen(label)) ==0){ |
295 |
return attribute; |
296 |
} |
297 |
} |
298 |
} |
299 |
|
300 |
storage = _global_storage; |
301 |
|
302 |
while((context = getNextContext(ANY, storage))){ |
303 |
while((attribute = getNextAttribute(context))){ |
304 |
if(strncmp(attribute->id, label, strlen(label)) ==0){ |
305 |
return attribute; |
306 |
} |
307 |
} |
308 |
} |
309 |
|
310 |
return NULL; |
311 |
} |
312 |
|
313 |
/* initializer & terminator */ |
314 |
|
315 |
EES_RC AOS_Init (void){ |
316 |
_aos_key_once = PTHREAD_ONCE_INIT; |
317 |
_is_threading = 0; |
318 |
pthread_once(&_aos_key_once, aos_make_key); |
319 |
|
320 |
if((_global_storage = calloc(1, sizeof(aos_storage_t)))){ |
321 |
if(!(_global_storage->list_contexts = calloc(1, sizeof(aos_context_t)))){ |
322 |
return EES_FAILURE; |
323 |
} |
324 |
} else { |
325 |
return EES_FAILURE; |
326 |
} |
327 |
|
328 |
_is_initialized = 1; |
329 |
return EES_SUCCESS; |
330 |
} |
331 |
|
332 |
void aos_make_key(void){ |
333 |
pthread_key_create(&_aos_key, aos_free_key); |
334 |
} |
335 |
|
336 |
void aos_free_key(aos_storage_t* storage){ |
337 |
aos_free_storage(storage); |
338 |
} |
339 |
|
340 |
EES_RC AOS_Clean(void){ |
341 |
pid_t tid; |
342 |
tid = syscall(SYS_gettid); |
343 |
printf("Killing thread %i\n", tid); |
344 |
if(!_is_threading){ |
345 |
return EES_FAILURE; |
346 |
} |
347 |
/*aos_free_storage(aos_get_storage());*/ |
348 |
printf("Freed storage for thread %i\n", tid); |
349 |
return EES_SUCCESS; |
350 |
} |
351 |
|
352 |
EES_RC AOS_Term (void){ |
353 |
_is_threading = 0; |
354 |
aos_free_storage(_global_storage); |
355 |
pthread_setspecific(_aos_key, NULL); |
356 |
pthread_key_delete(_aos_key); |
357 |
_is_initialized = 0; |
358 |
return EES_SUCCESS; |
359 |
} |
360 |
|
361 |
EES_RC aos_dump_argslist (void) { |
362 |
unsigned int i; |
363 |
aos_context_t * context; |
364 |
aos_attribute_t* attribute; |
365 |
while((context = getNextContext(ANY, NULL))){ |
366 |
if(context->context_class == OBLIGATION){ |
367 |
printf("Context obligation: %s\n", context->obligation_name); |
368 |
} |
369 |
while((attribute = getNextAttribute(context))){ |
370 |
printf("Attribute: %s with value: %s\n", attribute->id, attribute->data); |
371 |
} |
372 |
} |
373 |
return EES_SUCCESS; |
374 |
} |
375 |
|