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 |
if(storage != _global_storage){ |
126 |
return getNextContext(context_class, _global_storage); |
127 |
} else { |
128 |
return NULL; |
129 |
} |
130 |
} |
131 |
} |
132 |
|
133 |
void setContextObligationId(aos_context_t* context, char * obligation){ |
134 |
if(context->context_class == OBLIGATION){ |
135 |
context->obligation_name = obligation; |
136 |
} |
137 |
} |
138 |
|
139 |
aos_attribute_t* createAttribute(void){ |
140 |
aos_attribute_t* attribute = NULL; |
141 |
if((attribute = calloc(1, sizeof(aos_attribute_t)))){ |
142 |
attribute->next = NULL; |
143 |
attribute->setting_plugin = get_running_plugin(); |
144 |
} |
145 |
return attribute; |
146 |
} |
147 |
|
148 |
EES_RC addAttribute(aos_context_t * context, aos_attribute_t* attribute){ |
149 |
printf("Adding attribute at %p to context at %p\n", attribute, context); |
150 |
if(context->last_attribute){ |
151 |
context->last_attribute->next = attribute; |
152 |
} else { |
153 |
context->list_attributes = attribute; |
154 |
} |
155 |
context->last_attribute = attribute; |
156 |
return EES_SUCCESS; |
157 |
} |
158 |
|
159 |
aos_attribute_t* getNextAttribute(aos_context_t* context){ |
160 |
if(!context->last_attribute){ |
161 |
context->last_attribute = context->list_attributes; |
162 |
} else { |
163 |
context->last_attribute = context->last_attribute->next; |
164 |
} |
165 |
return context->last_attribute; |
166 |
} |
167 |
|
168 |
EES_RC destroyAttribute(aos_context_t* context, aos_attribute_t* attribute){ |
169 |
aos_attribute_t *last_attribute = NULL, *current_attribute = NULL; |
170 |
for(current_attribute = context->list_attributes; current_attribute != NULL; last_attribute = current_attribute, current_attribute = current_attribute->next){ |
171 |
if(current_attribute == attribute){ |
172 |
if(current_attribute->setting_plugin != get_running_plugin()){ |
173 |
eef_log(LOG_ERR, "Argument %s is not owned by running plugin %s", current_attribute->id, get_running_plugin()->name); |
174 |
return EES_FAILURE; |
175 |
} |
176 |
free(attribute->data); |
177 |
free(attribute); |
178 |
attribute = NULL; |
179 |
if(last_attribute == NULL){ |
180 |
context->list_attributes = current_attribute->next; |
181 |
} else { |
182 |
last_attribute->next = current_attribute->next; |
183 |
} |
184 |
break; |
185 |
} |
186 |
} |
187 |
|
188 |
if(current_attribute){ |
189 |
free(current_attribute->data); |
190 |
free(current_attribute); |
191 |
current_attribute = NULL; |
192 |
return EES_SUCCESS; |
193 |
} |
194 |
return EES_FAILURE; |
195 |
} |
196 |
|
197 |
EES_RC setAttributeId(aos_attribute_t* attribute, char* id){ |
198 |
attribute->id = id; |
199 |
return EES_SUCCESS; |
200 |
} |
201 |
|
202 |
EES_RC setAttributeIssuer(aos_attribute_t* attribute, char* issuer){ |
203 |
attribute->issuer = issuer; |
204 |
return EES_SUCCESS; |
205 |
} |
206 |
|
207 |
EES_RC setAttributeValue(aos_attribute_t* attribute, void* value, size_t size){ |
208 |
if(size == 0) { |
209 |
attribute->data = value; |
210 |
attribute->needs_free = 0; |
211 |
} else { |
212 |
attribute->data = calloc(1, size); |
213 |
memcpy(attribute->data, value, size); |
214 |
attribute->needs_free = 1; |
215 |
} |
216 |
return EES_SUCCESS; |
217 |
} |
218 |
|
219 |
EES_RC setAttributeType(aos_attribute_t * attribute, char* type){ |
220 |
attribute->type = type; |
221 |
return EES_SUCCESS; |
222 |
} |
223 |
|
224 |
char* getAttributeId(aos_attribute_t* attribute){ |
225 |
return attribute->id; |
226 |
} |
227 |
|
228 |
char* getAttributeIssuer(aos_attribute_t* attribute){ |
229 |
return attribute->issuer; |
230 |
} |
231 |
|
232 |
char* getAttributeValueAsString(aos_attribute_t* attribute){ |
233 |
return (char*) attribute->data; |
234 |
} |
235 |
|
236 |
int getAttributeValueAsInt(aos_attribute_t* attribute){ |
237 |
return (int) (attribute->data); |
238 |
} |
239 |
|
240 |
/* needs more work */ |
241 |
EES_RC aos_delete_attribute_by_label(const char* label){ |
242 |
aos_attribute_t * current_attr = NULL; |
243 |
aos_attribute_t * previous_attr = NULL; |
244 |
aos_storage_t * storage = aos_get_storage(); |
245 |
|
246 |
if(label == NULL){ |
247 |
return EES_FAILURE; |
248 |
} |
249 |
|
250 |
/*for(current_attr = storage->list_attributes; current_attr != NULL; previous_attr = current_attr, current_attr = current_attr->next){*/ |
251 |
/*if(strncmp(current_attr->id, label, strlen(label)) == 0){*/ |
252 |
/*if(current_attr->setting_plugin != get_running_plugin()){*/ |
253 |
/*eef_log(LOG_ERR, "Argument %s is not owned by running plugin %s", current_attr->id, get_running_plugin()->name);*/ |
254 |
/*return EES_FAILURE;*/ |
255 |
/*}*/ |
256 |
/*if(previous_attr == NULL){*/ |
257 |
/**//* head node deleted. linking start of list to next node */ |
258 |
/*storage->list_arguments = current_attr->next;*/ |
259 |
/*} else {*/ |
260 |
/**//* link previous to deletee's next */ |
261 |
/*previous_attr->next = current_attr->next;*/ |
262 |
/*}*/ |
263 |
/*break;*/ |
264 |
/*}*/ |
265 |
/*}*/ |
266 |
|
267 |
if(current_attr){ |
268 |
/*eef_log(LOG_ERR, "Deleting attr:%s", label);*/ |
269 |
free(current_attr->data); |
270 |
free(current_attr); |
271 |
current_attr = NULL; |
272 |
return EES_SUCCESS; |
273 |
} else { |
274 |
return EES_FAILURE; |
275 |
} |
276 |
|
277 |
} |
278 |
|
279 |
void* aos_get_value_by_label(const char *label){ |
280 |
aos_attribute_t* attr = aos_get_attribute_by_label(label); |
281 |
if(attr == NULL){ |
282 |
return NULL; |
283 |
} else { |
284 |
return attr->data; |
285 |
} |
286 |
} |
287 |
|
288 |
aos_attribute_t * aos_get_attribute_by_label(const char *label){ |
289 |
aos_context_t * context; |
290 |
aos_attribute_t * attribute; |
291 |
aos_storage_t * storage = aos_get_storage(); |
292 |
|
293 |
while((context = getNextContext(ANY, storage))){ |
294 |
while((attribute = getNextAttribute(context))){ |
295 |
if(strncmp(attribute->id, label, strlen(label)) ==0){ |
296 |
return attribute; |
297 |
} |
298 |
} |
299 |
} |
300 |
|
301 |
storage = _global_storage; |
302 |
|
303 |
while((context = getNextContext(ANY, storage))){ |
304 |
while((attribute = getNextAttribute(context))){ |
305 |
if(strncmp(attribute->id, label, strlen(label)) ==0){ |
306 |
return attribute; |
307 |
} |
308 |
} |
309 |
} |
310 |
|
311 |
return NULL; |
312 |
} |
313 |
|
314 |
/* initializer & terminator */ |
315 |
|
316 |
EES_RC AOS_Init (void){ |
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 |
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 |
|