|
version 1.3.2.5, 2011/09/07 23:38:39
|
version 1.5.4.1, 2013/01/17 13:10:49
|
|
Line 12 terms:
|
Line 12 terms:
|
| All of the documentation and software included in the ELWIX and AITNET |
All of the documentation and software included in the ELWIX and AITNET |
| Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org> |
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org> |
| |
|
| Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 | Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013 |
| by Michael Pounov <misho@elwix.org>. All rights reserved. |
by Michael Pounov <misho@elwix.org>. All rights reserved. |
| |
|
| Redistribution and use in source and binary forms, with or without |
Redistribution and use in source and binary forms, with or without |
|
Line 44 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF TH
|
Line 44 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF TH
|
| SUCH DAMAGE. |
SUCH DAMAGE. |
| */ |
*/ |
| #include "global.h" |
#include "global.h" |
| #include "aitsess.h" |
|
| |
|
| |
|
| /* |
/* |
| * sess_FreeValues() Free all values from value array allocated from sess_GetValues() |
* sess_FreeValues() Free all values from value array allocated from sess_GetValues() |
| |
* |
| * @ppsVals = Array strings |
* @ppsVals = Array strings |
| * return: none |
* return: none |
| */ | */ |
| inline void |
inline void |
| sess_FreeValues(char *** __restrict ppsVals) |
sess_FreeValues(char *** __restrict ppsVals) |
| { |
{ |
|
Line 62 sess_FreeValues(char *** __restrict ppsVals)
|
Line 62 sess_FreeValues(char *** __restrict ppsVals)
|
| return; |
return; |
| |
|
| for (ptr = *ppsVals; *ptr; ptr++) |
for (ptr = *ppsVals; *ptr; ptr++) |
| free(*ptr); | e_free(*ptr); |
| free(*ppsVals); | e_free(*ppsVals); |
| *ppsVals = NULL; |
*ppsVals = NULL; |
| } |
} |
| |
|
| /* |
/* |
| * sess_GetValues() Get all values from session shared memory |
* sess_GetValues() Get all values from session shared memory |
| |
* |
| * @s = Session item |
* @s = Session item |
| * @ppsVals = Return array strings |
* @ppsVals = Return array strings |
| * return: -1 error: in parameter, !=-1 count of returned strings in ppsVals (must be free after use!) |
* return: -1 error: in parameter, !=-1 count of returned strings in ppsVals (must be free after use!) |
| */ | */ |
| int |
int |
| sess_GetValues(tagSess * __restrict s, char ***ppsVals) | sess_GetValues(ait_sess_t * __restrict s, char ***ppsVals) |
| { |
{ |
| register int i; |
register int i; |
| char **valz, *Shared = NULL; |
char **valz, *Shared = NULL; |
|
Line 82 sess_GetValues(tagSess * __restrict s, char ***ppsVals
|
Line 83 sess_GetValues(tagSess * __restrict s, char ***ppsVals
|
| |
|
| if (!s || !ppsVals) |
if (!s || !ppsVals) |
| return -1; |
return -1; |
| valz = malloc(sizeof(caddr_t)); | valz = e_malloc(sizeof(caddr_t)); |
| if (!valz) { |
if (!valz) { |
| LOGERR; |
LOGERR; |
| return -1; |
return -1; |
|
Line 90 sess_GetValues(tagSess * __restrict s, char ***ppsVals
|
Line 91 sess_GetValues(tagSess * __restrict s, char ***ppsVals
|
| *valz = NULL; |
*valz = NULL; |
| |
|
| // allocated memory & mirrored shared memory into this |
// allocated memory & mirrored shared memory into this |
| Shared = malloc(s->eom); | Shared = e_malloc(s->eom); |
| if (!Shared) { |
if (!Shared) { |
| LOGERR; |
LOGERR; |
| free(valz); | e_free(valz); |
| return -1; |
return -1; |
| } else |
} else |
| memcpy(Shared, s->addr, s->eom); |
memcpy(Shared, s->addr, s->eom); |
|
Line 105 sess_GetValues(tagSess * __restrict s, char ***ppsVals
|
Line 106 sess_GetValues(tagSess * __restrict s, char ***ppsVals
|
| else |
else |
| i++; |
i++; |
| |
|
| valz = realloc(valz, (i + 1) * sizeof(caddr_t)); | valz = e_realloc(valz, (i + 1) * sizeof(caddr_t)); |
| if (!valz) { |
if (!valz) { |
| LOGERR; |
LOGERR; |
| free(Shared); | e_free(Shared); |
| return -1; |
return -1; |
| } else |
} else |
| valz[i] = NULL; |
valz[i] = NULL; |
| |
|
| valz[i - 1] = strdup(peer); | valz[i - 1] = e_strdup(peer); |
| } |
} |
| |
|
| free(Shared); | e_free(Shared); |
| *ppsVals = valz; |
*ppsVals = valz; |
| return i; |
return i; |
| } |
} |
| |
|
| /* |
/* |
| * sess_GetValue() Get value from session shared memory from attribute |
* sess_GetValue() Get value from session shared memory from attribute |
| |
* |
| * @s = Session item |
* @s = Session item |
| * @csAttr = Attribute for search |
* @csAttr = Attribute for search |
| * @psVal = Return string buffer |
* @psVal = Return string buffer |
| * @pnLen = Length of return string buffer, |
* @pnLen = Length of return string buffer, |
| // *{pnLen} input is max_size of buffer & output is really taken bytes |
// *{pnLen} input is max_size of buffer & output is really taken bytes |
| * return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF |
* return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF |
| */ | */ |
| int |
int |
| sess_GetValue(tagSess * __restrict s, const char *csAttr, char *psVal, int *pnLen) | sess_GetValue(ait_sess_t * __restrict s, const char *csAttr, char *psVal, int *pnLen) |
| { |
{ |
| register int i; |
register int i; |
| int def = IS_VAL; |
int def = IS_VAL; |
|
Line 148 sess_GetValue(tagSess * __restrict s, const char *csAt
|
Line 150 sess_GetValue(tagSess * __restrict s, const char *csAt
|
| } |
} |
| |
|
| // allocated memory & mirrored shared memory into this |
// allocated memory & mirrored shared memory into this |
| Shared = malloc(s->eom); | Shared = e_malloc(s->eom); |
| if (!Shared) { |
if (!Shared) { |
| LOGERR; |
LOGERR; |
| return -1; |
return -1; |
|
Line 168 sess_GetValue(tagSess * __restrict s, const char *csAt
|
Line 170 sess_GetValue(tagSess * __restrict s, const char *csAt
|
| } else |
} else |
| def = IS_DEF; |
def = IS_DEF; |
| |
|
| free(Shared); | e_free(Shared); |
| return i | def; |
return i | def; |
| } |
} |
| } |
} |
| |
|
| free(Shared); | e_free(Shared); |
| return 0; |
return 0; |
| } |
} |
| |
|
| /* |
/* |
| * sess_DelValue() Delete item from session shared memory |
* sess_DelValue() Delete item from session shared memory |
| |
* |
| * @s = Session item |
* @s = Session item |
| * @csAttr = Attribute for erasing |
* @csAttr = Attribute for erasing |
| * return: 0 Ok, -1 error: in parameter |
* return: 0 Ok, -1 error: in parameter |
| */ | */ |
| int |
int |
| sess_DelValue(tagSess * __restrict s, const char *csAttr) | sess_DelValue(ait_sess_t * __restrict s, const char *csAttr) |
| { |
{ |
| register int i; |
register int i; |
| int ret, attrlen; | int attrlen; |
| char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE]; |
char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE]; |
| char *peer, *p_brk; |
char *peer, *p_brk; |
| |
|
|
Line 199 sess_DelValue(tagSess * __restrict s, const char *csAt
|
Line 202 sess_DelValue(tagSess * __restrict s, const char *csAt
|
| strlcpy(szAttr, csAttr, sizeof szAttr); |
strlcpy(szAttr, csAttr, sizeof szAttr); |
| strlcat(szAttr, "=", sizeof szAttr); |
strlcat(szAttr, "=", sizeof szAttr); |
| |
|
| Buffer = malloc(s->eom); | Buffer = e_malloc(s->eom); |
| if (!Buffer) { |
if (!Buffer) { |
| LOGERR; |
LOGERR; |
| return -1; |
return -1; |
| } else |
} else |
| memset(Buffer, 0, s->eom); |
memset(Buffer, 0, s->eom); |
| Shared = malloc(s->eom); | Shared = e_malloc(s->eom); |
| if (!Shared) { |
if (!Shared) { |
| LOGERR; |
LOGERR; |
| free(Buffer); | e_free(Buffer); |
| return -1; |
return -1; |
| } else { |
} else { |
| DEC_SEMAPHORE(s, ret); | DEC_SEM(s); |
| memcpy(Shared, s->addr, s->eom); |
memcpy(Shared, s->addr, s->eom); |
| } |
} |
| |
|
|
Line 231 sess_DelValue(tagSess * __restrict s, const char *csAt
|
Line 234 sess_DelValue(tagSess * __restrict s, const char *csAt
|
| if (s->type == SHARED_MAP) |
if (s->type == SHARED_MAP) |
| msync(s->addr, 0, MS_SYNC | MS_INVALIDATE); |
msync(s->addr, 0, MS_SYNC | MS_INVALIDATE); |
| |
|
| ADD_SEMAPHORE(s, ret); | INC_SEM(s); |
| free(Shared); | e_free(Shared); |
| free(Buffer); | e_free(Buffer); |
| return 0; |
return 0; |
| } |
} |
| |
|
| /* |
/* |
| * sess_SetValue() Set item into session shared memory or update if find it |
* sess_SetValue() Set item into session shared memory or update if find it |
| |
* |
| * @s = Session item |
* @s = Session item |
| * @csAttr = Attribute |
* @csAttr = Attribute |
| * @psVal = Value |
* @psVal = Value |
| * return: 0 nothing, -1 error: in parameter, |
* return: 0 nothing, -1 error: in parameter, |
| >0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF |
>0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF |
| */ | */ |
| int |
int |
| sess_SetValue(tagSess * __restrict s, const char *csAttr, const char *psVal) | sess_SetValue(ait_sess_t * __restrict s, const char *csAttr, const char *psVal) |
| { |
{ |
| register int i; |
register int i; |
| int upd, ret, def = IS_VAL; | int upd, def = IS_VAL; |
| char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE]; |
char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE]; |
| char *peer, *p_brk; |
char *peer, *p_brk; |
| |
|
|
Line 260 sess_SetValue(tagSess * __restrict s, const char *csAt
|
Line 264 sess_SetValue(tagSess * __restrict s, const char *csAt
|
| strlcpy(szAttr, csAttr, sizeof szAttr); |
strlcpy(szAttr, csAttr, sizeof szAttr); |
| strlcat(szAttr, "=", sizeof szAttr); |
strlcat(szAttr, "=", sizeof szAttr); |
| |
|
| Buffer = malloc(s->eom); | Buffer = e_malloc(s->eom); |
| if (!Buffer) { |
if (!Buffer) { |
| LOGERR; |
LOGERR; |
| return -1; |
return -1; |
| } else |
} else |
| memset(Buffer, 0, s->eom); |
memset(Buffer, 0, s->eom); |
| Shared = malloc(s->eom); | Shared = e_malloc(s->eom); |
| if (!Shared) { |
if (!Shared) { |
| LOGERR; |
LOGERR; |
| free(Buffer); | e_free(Buffer); |
| return -1; |
return -1; |
| } else { |
} else { |
| DEC_SEMAPHORE(s, ret); | DEC_SEM(s); |
| memcpy(Shared, s->addr, s->eom); |
memcpy(Shared, s->addr, s->eom); |
| } |
} |
| |
|
|
Line 314 sess_SetValue(tagSess * __restrict s, const char *csAt
|
Line 318 sess_SetValue(tagSess * __restrict s, const char *csAt
|
| if (s->type == SHARED_MAP) |
if (s->type == SHARED_MAP) |
| msync(s->addr, 0, MS_SYNC | MS_INVALIDATE); |
msync(s->addr, 0, MS_SYNC | MS_INVALIDATE); |
| |
|
| ADD_SEMAPHORE(s, ret); | INC_SEM(s); |
| free(Shared); | e_free(Shared); |
| free(Buffer); | e_free(Buffer); |
| return upd | def; |
return upd | def; |
| } |
} |
| |
|
| |
|
| /* |
/* |
| * sess_prepareSession() Attach to shared memory and de-marshaling data |
* sess_prepareSession() Attach to shared memory and de-marshaling data |
| |
* |
| * @s = Session |
* @s = Session |
| * @useDirect = Use direct shared memory if !=0 or snapshot of data to array |
* @useDirect = Use direct shared memory if !=0 or snapshot of data to array |
| * return: NULL error or no data, !=NULL array with variables, |
* return: NULL error or no data, !=NULL array with variables, |
| * after use must free resources with sess_doneSession() |
* after use must free resources with sess_doneSession() |
| */ |
*/ |
| array_t * |
array_t * |
| sess_prepareSession(tagSess * __restrict s, u_char useDirect) | sess_prepareSession(ait_sess_t * __restrict s, char useDirect) |
| { |
{ |
| array_t *arr = NULL; |
array_t *arr = NULL; |
| sess_hdr_t *hdr; |
sess_hdr_t *hdr; |
| int ret; |
|
| |
|
| assert(s); |
assert(s); |
| if (!s) { |
if (!s) { |
|
Line 357 sess_prepareSession(tagSess * __restrict s, u_char use
|
Line 361 sess_prepareSession(tagSess * __restrict s, u_char use
|
| return NULL; |
return NULL; |
| } |
} |
| |
|
| DEC_SEMAPHORE(s, ret); | DEC_SEM(s); |
| s->zcopy = useDirect; | s->zcpy = useDirect; |
| arr = io_map2vals(s->addr + sizeof(sess_hdr_t), s->eom - sizeof(sess_hdr_t), | arr = ait_map2vars(s->addr + sizeof(sess_hdr_t), s->eom - sizeof(sess_hdr_t), |
| hdr->hdr_argc, useDirect); |
hdr->hdr_argc, useDirect); |
| ADD_SEMAPHORE(s, ret); | INC_SEM(s); |
| |
|
| DETACH_MEMORY(s); | if (!s->zcpy) |
| | DETACH_MEMORY(s); |
| return arr; |
return arr; |
| } |
} |
| |
|
| /* |
/* |
| * sess_doneSession() Free resources allocated with sess_prepareSession() |
* sess_doneSession() Free resources allocated with sess_prepareSession() |
| |
* |
| * @s = Session |
* @s = Session |
| * @arr = Array with variables for free |
* @arr = Array with variables for free |
| * return: none |
* return: none |
| */ |
*/ |
| void |
void |
| sess_doneSession(tagSess * __restrict s, array_t ** __restrict arr) | sess_doneSession(ait_sess_t * __restrict s, array_t ** __restrict arr) |
| { |
{ |
| assert(s); |
assert(s); |
| if (!s) { |
if (!s) { |
|
Line 382 sess_doneSession(tagSess * __restrict s, array_t ** __
|
Line 388 sess_doneSession(tagSess * __restrict s, array_t ** __
|
| return; |
return; |
| } |
} |
| |
|
| if (!s->zcopy) | if (!s->zcpy) |
| io_arrayFree(*arr); | array_Free(*arr); |
| io_arrayDestroy(arr); | else |
| | DETACH_MEMORY(s); |
| | array_Destroy(arr); |
| } |
} |
| |
|
| /* |
/* |
| * sess_commitSession() Commit data to shared memory |
* sess_commitSession() Commit data to shared memory |
| |
* |
| * @s = Session |
* @s = Session |
| * @arr = Array with variables for save |
* @arr = Array with variables for save |
| * return -1 error or !=-1 size of stored variables into shared memory |
* return -1 error or !=-1 size of stored variables into shared memory |
| */ |
*/ |
| int |
int |
| sess_commitSession(tagSess * __restrict s, array_t * __restrict arr) | sess_commitSession(ait_sess_t * __restrict s, array_t * __restrict arr) |
| { |
{ |
| sess_hdr_t *hdr; |
sess_hdr_t *hdr; |
| int rs, ret = 0; | int ret = 0; |
| |
|
| assert(s && arr); |
assert(s && arr); |
| if (!s || !arr) { |
if (!s || !arr) { |
|
Line 412 sess_commitSession(tagSess * __restrict s, array_t * _
|
Line 421 sess_commitSession(tagSess * __restrict s, array_t * _
|
| } else |
} else |
| hdr = (sess_hdr_t*) s->addr; |
hdr = (sess_hdr_t*) s->addr; |
| |
|
| DEC_SEMAPHORE(s, rs); | DEC_SEM(s); |
| if ((ret = io_vals2map(s->addr + sizeof(sess_hdr_t), s->eom - sizeof(sess_hdr_t), arr)) != -1) { | if ((ret = ait_vars2map(s->addr + sizeof(sess_hdr_t), |
| | s->eom - sizeof(sess_hdr_t), arr)) != -1) { |
| hdr->hdr_magic = SESS_AIT_MAGIC; |
hdr->hdr_magic = SESS_AIT_MAGIC; |
| hdr->hdr_argc = io_arraySize(arr); | hdr->hdr_argc = array_Size(arr); |
| ret += sizeof(sess_hdr_t); |
ret += sizeof(sess_hdr_t); |
| } |
} |
| ADD_SEMAPHORE(s, rs); | INC_SEM(s); |
| |
|
| DETACH_MEMORY(s); |
DETACH_MEMORY(s); |
| return ret; |
return ret; |