File:
[ELWIX - Embedded LightWeight unIX -] /
libaitio /
src /
sess.c
Revision
1.5:
download - view:
text,
annotated -
select for diffs -
revision graph
Sat Feb 8 22:06:17 2014 UTC (10 years, 6 months ago) by
misho
Branches:
MAIN
CVS tags:
io7_3,
io7_2,
io7_1,
io7_0,
IO7_2,
IO7_1,
IO7_0,
IO6_9,
HEAD
version 6.9
/*************************************************************************
* (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
* by Michael Pounov <misho@openbsd-bg.org>
*
* $Author: misho $
* $Id: sess.c,v 1.5 2014/02/08 22:06:17 misho Exp $
*
**************************************************************************
The ELWIX and AITNET software is distributed under the following
terms:
All of the documentation and software included in the ELWIX and AITNET
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
Copyright 2004 - 2014
by Michael Pounov <misho@elwix.org>. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by Michael Pounov <misho@elwix.org>
ELWIX - Embedded LightWeight unIX and its contributors.
4. Neither the name of AITNET nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "global.h"
/*
* sess_initSession() - Initializing session structure,
* if session file not exists creating with specified tech
*
* @id = Technology using in session. SHARED_IPC IPC tech or SHARED_MAP BSD MemoryMap tech
* @csFName = Session filename for build key and identified
* @Sess = Session item, if =NULL allocate memory for session after use must be free!
* return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded
*/
int
sess_initSession(int id, const char *csFName, sess_t ** __restrict Sess)
{
int h, ret = 0;
char szStr[STRSIZ];
if (!csFName) {
io_SetErr(EINVAL, "Filename is NULL");
return -1;
}
if (id < SHARED_UNKNOWN || id > SHARED_MAP) {
io_SetErr(EPROTONOSUPPORT, "Session type not supported");
return -1;
}
if (!*Sess) {
*Sess = e_malloc(sizeof(sess_t));
if (!*Sess) {
LOGERR;
return -1;
}
}
memset(*Sess, 0, sizeof(sess_t));
strlcpy((*Sess)->sess_name, csFName, sizeof (*Sess)->sess_name);
h = open((*Sess)->sess_name, O_WRONLY | O_CREAT | O_EXCL, SESS_MEM_MODE);
if (h == -1) {
if (errno != EEXIST) {
LOGERR;
e_free(*Sess);
return -1;
}
/* If key file exist, session already connected */
h = open((*Sess)->sess_name, O_RDONLY);
if (h == -1) {
LOGERR;
e_free(*Sess);
return -1;
}
ret = read(h, szStr, sizeof szStr);
if (ret == -1) {
LOGERR;
close(h);
e_free(*Sess);
return -1;
}
if (!strncmp(szStr, "IPC@", 4) && id == SHARED_IPC) {
ret = 1;
(*Sess)->sess.create = (int (*)(int, long, void*, ...)) ipc_createSession;
(*Sess)->sess.destroy = (void (*)(void*)) ipc_destroySession;
(*Sess)->sess.attach = (void* (*)(void*, void*)) ipc_attachSession;
(*Sess)->sess.detach = (void (*)(void*)) ipc_detachSession;
(*Sess)->sess.notSem = (void (*)(void*)) ipc_notSemaphore;
(*Sess)->sess.isSemOK = (int (*)(void*)) ipc_isSemaphoreOK;
(*Sess)->sess.incSem = (int (*)(void*)) ipc_incSemaphore;
(*Sess)->sess.decSem = (int (*)(void*)) ipc_decSemaphore;
} else if (!strncmp(szStr, "MAP@", 4) && id == SHARED_MAP) {
ret = 1;
(*Sess)->sess.create = (int (*)(int, long, void*, ...)) map_createSession;
(*Sess)->sess.destroy = (void (*)(void*)) map_destroySession;
(*Sess)->sess.attach = (void* (*)(void*, void*)) map_attachSession;
(*Sess)->sess.detach = (void (*)(void*)) map_detachSession;
(*Sess)->sess.notSem = (void (*)(void*)) map_notSemaphore;
(*Sess)->sess.isSemOK = (int (*)(void*)) map_isSemaphoreOK;
(*Sess)->sess.incSem = (int (*)(void*)) map_incSemaphore;
(*Sess)->sess.decSem = (int (*)(void*)) map_decSemaphore;
} else {
io_SetErr(EPROTONOSUPPORT,
"Session type not supported or wrong session type");
close(h);
e_free(*Sess);
return -1;
}
/* key found */
} else {
/* Build new key & new session */
if (id == SHARED_IPC) {
strlcpy(szStr, "IPC@", sizeof szStr);
(*Sess)->sess.create = (int (*)(int, long, void*, ...)) ipc_createSession;
(*Sess)->sess.destroy = (void (*)(void*)) ipc_destroySession;
(*Sess)->sess.attach = (void* (*)(void*, void*)) ipc_attachSession;
(*Sess)->sess.detach = (void (*)(void*)) ipc_detachSession;
(*Sess)->sess.notSem = (void (*)(void*)) ipc_notSemaphore;
(*Sess)->sess.isSemOK = (int (*)(void*)) ipc_isSemaphoreOK;
(*Sess)->sess.incSem = (int (*)(void*)) ipc_incSemaphore;
(*Sess)->sess.decSem = (int (*)(void*)) ipc_decSemaphore;
} else if (id == SHARED_MAP) {
strlcpy(szStr, "MAP@", sizeof szStr);
(*Sess)->sess.create = (int (*)(int, long, void*, ...)) map_createSession;
(*Sess)->sess.destroy = (void (*)(void*)) map_destroySession;
(*Sess)->sess.attach = (void* (*)(void*, void*)) map_attachSession;
(*Sess)->sess.detach = (void (*)(void*)) map_detachSession;
(*Sess)->sess.notSem = (void (*)(void*)) map_notSemaphore;
(*Sess)->sess.isSemOK = (int (*)(void*)) map_isSemaphoreOK;
(*Sess)->sess.incSem = (int (*)(void*)) map_incSemaphore;
(*Sess)->sess.decSem = (int (*)(void*)) map_decSemaphore;
} else {
io_SetErr(EINVAL, "Session type must be specified");
close(h);
unlink(csFName);
e_free(*Sess);
return -1;
}
strlcat(szStr, "ELWIX_Session ("PACKAGE_STRING")\n", sizeof szStr);
write(h, szStr, strlen(szStr));
ret = 0;
/* new key created */
}
close(h);
(*Sess)->sess_type = id;
(*Sess)->sess_zcpy = (char) ret;
return ret;
}
/*
* sess_freeSession() - Free allocated memory for session item and delete session file if present name
*
* @Sess = Session item
* return: none
*/
void
sess_freeSession(sess_t ** __restrict Sess)
{
if (!Sess || !(*Sess))
return;
if ((*Sess)->sess_addr)
DETACH_MEMORY(*Sess);
/*
memset(&(*Sess)->sess, 0, sizeof (*Sess)->sess);
(*Sess)->type = SHARED_UNKNOWN;
*/
e_free(*Sess);
*Sess = NULL;
}
/*
* map_createSession() - MMAP Created session and allocated resources
*
* @nSeed = Seed for securing key, if =SESS_OPT_SEED must add ready for use key
* @nSize = Allocated shared memory size in bytes
* @Sess = Session item
* @... = If nSeed == -1 add ready for use key value
* return: 0 Ok successful, -1 error: not allocated resources
*/
int
map_createSession(int nSeed, long nSize, sess_t * __restrict Sess, ...)
{
char szSName[2][FILENAME_MAX];
va_list lst;
if (!Sess || !*Sess->sess_name)
return -1;
if (nSeed != SESS_OPT_SEED) {
/* genkey */
Sess->sess_key = ftok(Sess->sess_name, nSeed);
if (Sess->sess_key == -1) {
LOGERR;
return -1;
}
} else {
/* get key from va_args */
va_start(lst, Sess);
Sess->sess_key = va_arg(lst, key_t);
va_end(lst);
}
/* build semaphore & shared memory name */
memset(szSName, 0, sizeof szSName);
snprintf(szSName[0], SEMNAME_MAX, "/%X.ANS", (u_int) Sess->sess_key);
#ifdef HAVE_SHM_OPEN
snprintf(szSName[1], FILENAME_MAX, "/%s-%x.ANM", Sess->sess_name, (u_int) Sess->sess_key);
#else
snprintf(szSName[1], FILENAME_MAX, "%s-%x.ANM", Sess->sess_name, (u_int) Sess->sess_key);
#endif
/* create semaphore & add 1 */
Sess->sess_id.sid = sem_open(szSName[0], O_CREAT, SESS_MEM_MODE);
if (Sess->sess_id.sid == SEM_FAILED) {
LOGERR;
map_destroySession(Sess);
return -1;
}
/* if is new shared memory session, init sempahore with 1 */
if (!Sess->sess_zcpy)
sem_post(Sess->sess_id.sid);
/* create file for shared memory storage */
#ifdef HAVE_SHM_OPEN
Sess->sess_mem.fd = shm_open(szSName[1], O_RDWR | O_CREAT, SESS_MEM_MODE);
#else
Sess->sess_mem.fd = open(szSName[1], O_RDWR | O_CREAT, SESS_MEM_MODE);
#endif
if (Sess->sess_mem.fd == -1) {
LOGERR;
map_destroySession(Sess);
return -1;
}
if (!Sess->sess_zcpy) {
/* #ifdef HAVE_SHM_OPEN */
if (ftruncate(Sess->sess_mem.fd, nSize) == -1) {
LOGERR;
map_destroySession(Sess);
return -1;
}
/* #else */
/* if is new shared memory session, fill file with zeros */
/*
if (lseek(Sess->sess_mem.fd, nSize - 1, SEEK_SET) == -1) {
LOGERR;
map_destroySession(Sess);
return -1;
} else
write(Sess->sess_mem.fd, "", 1);
lseek(Sess->sess_mem.fd, 0, SEEK_SET);
#endif
*/
}
Sess->sess_size = nSize;
return (int) Sess->sess_zcpy;
}
/*
* map_destroySession() - MMAP free shared resources
*
* @Sess = Session item
* return: none
*/
void
map_destroySession(sess_t * __restrict Sess)
{
char szSName[2][FILENAME_MAX];
if (!Sess || sess_isAttached(Sess) || !*Sess->sess_name)
return;
memset(szSName, 0, sizeof szSName);
snprintf(szSName[0], SEMNAME_MAX, "/%X.ANS", (u_int) Sess->sess_key);
#ifdef HAVE_SHM_UNLINK
snprintf(szSName[1], FILENAME_MAX, "/%s-%x.ANM", Sess->sess_name, (u_int) Sess->sess_key);
#else
snprintf(szSName[1], FILENAME_MAX, "%s-%x.ANM", Sess->sess_name, (u_int) Sess->sess_key);
#endif
if (Sess->sess_id.sid != SEM_FAILED) {
sem_close(Sess->sess_id.sid);
sem_unlink(szSName[0]);
}
if (Sess->sess_mem.fd > 2) {
close(Sess->sess_mem.fd);
#ifdef HAVE_SHM_UNLINK
shm_unlink(szSName[1]);
#else
unlink(szSName[1]);
#endif
}
unlink(Sess->sess_name);
memset(Sess->sess_name, 0, sizeof Sess->sess_name);
Sess->sess_size ^= Sess->sess_size;
Sess->sess_key ^= Sess->sess_key;
}
/*
* ipc_createSession() - IPC Created session and allocated resources
*
* @nSeed = Seed for securing key, if =SESS_OPT_SEED must add ready for use key
* @nSize = Allocated shared memory size in bytes
* @Sess = Session item
* @... = If nSeed == -1 add ready for use key value
* return: 0 Ok successful, -1 error: not allocated resources
*/
int
ipc_createSession(int nSeed, long nSize, sess_t * __restrict Sess, ...)
{
union semun sems;
va_list lst;
if (!Sess || !*Sess->sess_name)
return -1;
if (nSeed != -1) {
/* genkey */
Sess->sess_key = ftok(Sess->sess_name, nSeed);
if (Sess->sess_key == -1) {
LOGERR;
return -1;
}
} else {
/* get key from va_args */
va_start(lst, Sess);
Sess->sess_key = va_arg(lst, key_t);
va_end(lst);
}
/* create semaphore */
Sess->sess_id.semid = semget(Sess->sess_key, 1, SESS_MEM_MODE | IPC_CREAT);
if (Sess->sess_id.semid == -1) {
LOGERR;
ipc_destroySession(Sess);
return -1;
}
/* if is new shared memory session, init sempahore with 1 */
if (!Sess->sess_zcpy) {
sems.val = 1;
if (semctl(Sess->sess_id.semid, 0, SETVAL, sems) == -1) {
LOGERR;
ipc_destroySession(Sess);
return -1;
}
}
/* create shared memory object */
Sess->sess_mem.shmid = shmget(Sess->sess_key, nSize, SESS_MEM_MODE | IPC_CREAT);
if (Sess->sess_mem.shmid == -1) {
LOGERR;
ipc_destroySession(Sess);
return -1;
} else
Sess->sess_size = nSize;
return (int) Sess->sess_zcpy;
}
/*
* ipc_destroySession() - IPC free shared resources
*
* @Sess = Session item
* return: none
*/
void
ipc_destroySession(sess_t * __restrict Sess)
{
union semun sems;
struct shmid_ds ds;
if (!Sess || sess_isAttached(Sess))
return;
if (Sess->sess_id.semid != -1)
semctl(Sess->sess_id.semid, 0, IPC_RMID, &sems);
if (Sess->sess_mem.shmid != -1)
shmctl(Sess->sess_mem.shmid, IPC_RMID, &ds);
unlink(Sess->sess_name);
memset(Sess->sess_name, 0, sizeof Sess->sess_name);
Sess->sess_size ^= Sess->sess_size;
Sess->sess_key ^= Sess->sess_key;
}
/*
* map_attachSession() - MMAP Attach to shared memory & return begin address
*
* @s = Session item
* @procMem = Custom start address (optionl) *default must be 0*
* return: NULL failed attach, !=NULL begin address of memory
*/
void *
map_attachSession(sess_t * __restrict s, void *procMem)
{
if (!s)
return NULL;
sync();
/* attach to memory */
s->sess_addr = mmap(procMem, s->sess_size, PROT_READ | PROT_WRITE, MAP_SHARED, s->sess_mem.fd, 0);
if (s->sess_addr == MAP_FAILED) {
LOGERR;
s->sess_addr = NULL;
}
return s->sess_addr;
}
/*
* map_detachSession() - MMAP Detach from shared memory
*
* @s = Session item
* return: none
*/
void
map_detachSession(sess_t * __restrict s)
{
if (!s)
return;
msync(s->sess_addr, 0, MS_SYNC | MS_INVALIDATE);
if (s->sess_size) {
munmap(s->sess_addr, s->sess_size);
s->sess_addr = NULL;
}
}
/*
* ipc_attachSession() - IPC Attach to shared memory & return begin address
*
* @s = Session item
* @procMem = Custom start address (optionl) *default must be 0*
* return: NULL failed attach, !=NULL begin address of memory
*/
void *
ipc_attachSession(sess_t * __restrict s, void *procMem)
{
if (!s)
return NULL;
s->sess_addr = shmat(s->sess_mem.shmid, procMem, 0);
if (s->sess_addr == (void*) -1) {
LOGERR;
s->sess_addr = NULL;
}
return s->sess_addr;
}
/*
* ipc_detachSession() - IPC Detach from shared memory
*
* @s = Session item
* return: none
*/
void
ipc_detachSession(sess_t * __restrict s)
{
if (!s)
return;
if (s->sess_size) {
shmdt(s->sess_addr);
s->sess_addr = NULL;
}
}
/*
* sess_isAttached() - Check for attached shared memory
*
* @s = Session item
* return: -1 null session item, 0 not attached, 1 attached memory
*/
int
sess_isAttached(sess_t * __restrict s)
{
if (!s)
return -1;
return !!s->sess_addr;
}
/*
* map_notSemaphore() - MMAP negative block if semaphore isn`t signaled
*
* @s = Session item
* return: none
*/
void
map_notSemaphore(sess_t * __restrict s)
{
int i = -1;
if (!s)
return;
sem_getvalue(s->sess_id.sid, &i);
for (; i; i--)
sem_wait(s->sess_id.sid);
}
/*
* map_isSemaphoreOK() - MMAP Check semaphore
*
* @s = Session item
* return: -1 error: can`t return semaphore, 0 = false, 1 = true
*/
int
map_isSemaphoreOK(sess_t * __restrict s)
{
int val = -1;
if (!s)
return -1;
return sem_getvalue(s->sess_id.sid, &val) + 1;
}
/*
* map_incSemaphore() - MMAP unblock semaphore, increment semaphore
*
* @s = Session item
* return: 0 Ok, -1 error: can`t increment
*/
int
map_incSemaphore(sess_t * __restrict s)
{
if (!s)
return -1;
return sem_post(s->sess_id.sid);
}
/*
* map_decSemaphore() - MMAP block semaphore, decrement semaphore
*
* @s = Session item
* return: 0 Ok, -1 error: can`t decrement
*/
int
map_decSemaphore(sess_t * __restrict s)
{
if (!s)
return -1;
return sem_wait(s->sess_id.sid);
}
/*
* ipc_notSemaphore() - IPC negative block if semaphore isn`t signaled
*
* @s = Session item
* return: none
*/
void
ipc_notSemaphore(sess_t * __restrict s)
{
struct sembuf sb = { 0, 0, 0 };
if (s)
semop(s->sess_id.semid, &sb, 1);
}
/*
* ipc_isSemaphoreOK() - IPC Check semaphore
*
* @s = Session item
* return: -1 error: can`t return semaphore, 0 = false, 1 = true
*/
int
ipc_isSemaphoreOK(sess_t * __restrict s)
{
struct sembuf sb = { 0, 0, IPC_NOWAIT };
if (!s)
return -1;
return semop(s->sess_id.semid, &sb, 1) + 1;
}
/*
* ipc_incSemaphore() - IPC unblock semaphore, increment semaphore
*
* @s = Session item
* return: 0 Ok, -1 error: can`t increment
*/
int
ipc_incSemaphore(sess_t * __restrict s)
{
struct sembuf sb = { 0, 1, 0 };
if (!s)
return -1;
return semop(s->sess_id.semid, &sb, 1);
}
/*
* ipc_decSemaphore() - IPC block semaphore, decrement semaphore
*
* @s = Session item
* return: 0 Ok, -1 error: can`t decrement
*/
int
ipc_decSemaphore(sess_t * __restrict s)
{
struct sembuf sb = { 0, -1, 0 };
if (!s)
return -1;
return semop(s->sess_id.semid, &sb, 1);
}
/* --- A/V management --- */
/*
* sess_GetValues() - Get all values from session shared memory
*
* @s = Session item
* @Vals = Return array strings
* return: -1 error: in parameter, !=-1 count of returned strings in Vals
* (must call ait_freeVars() after use!)
*/
int
sess_GetValues(sess_t * __restrict s, array_t ** __restrict Vals)
{
register int i;
char *Shared = NULL;
char *peer, *p_brk;
ait_val_t *v;
if (!s || !Vals)
return -1;
*Vals = ait_allocVars(0);
if (!*Vals) {
io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
return -1;
}
/* allocated memory & mirrored shared memory into this */
Shared = e_malloc(s->sess_size);
if (!Shared) {
LOGERR;
ait_freeVars(Vals);
return -1;
}
DEC_SEM(s);
memcpy(Shared, s->sess_addr, s->sess_size);
INC_SEM(s);
for (i = 0, peer = strtok_r(Shared, SESS_MEM_DELIM"\r\n", &p_brk); peer;
peer = strtok_r(NULL, SESS_MEM_DELIM"\r\n", &p_brk)) {
if (!strchr(peer, '='))
continue;
else
i++;
if (!(v = ait_allocVar())) {
io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
e_free(Shared);
ait_freeVars(Vals);
return -1;
} else
AIT_SET_STR(v, peer);
array_Push(*Vals, v, 0);
}
e_free(Shared);
ait_hashKeyVars(*Vals);
return i;
}
/*
* sess_GetValue() - Get value from session shared memory from attribute
*
* @s = Session item
* @csAttr = Attribute for search
* @v = Return string value and after use must call ait_freeVar(), also may be =NULL
* return: 0 not found, -1 error: in parameter, >0 get position,
* if define item merged with IS_DEF
*/
int
sess_GetValue(sess_t * __restrict s, const char *csAttr, ait_val_t ** __restrict v)
{
register int i;
int def = 0;
char *Shared = NULL;
char *peer, *p_brk, *a_brk, *attr, *val;
if (!s || !csAttr || !*csAttr)
return -1;
if (v) {
if (!(*v = ait_allocVar())) {
io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
return -1;
} else
AIT_INIT_VAL2(*v, string);
}
/* allocated memory & mirrored shared memory into this */
Shared = e_malloc(s->sess_size);
if (!Shared) {
LOGERR;
ait_freeVar(v);
return -1;
}
DEC_SEM(s);
memcpy(Shared, s->sess_addr, s->sess_size);
INC_SEM(s);
for (i = 1, peer = strtok_r(Shared, SESS_MEM_DELIM"\r\n", &p_brk); peer;
i++, peer = strtok_r(NULL, SESS_MEM_DELIM"\r\n", &p_brk)) {
attr = strtok_r(peer, "=\r\n", &a_brk);
if (attr && !strncmp(attr, csAttr, SESS_MAX_ATTRIBUTE - 1)) {
val = strtok_r(NULL, "=\r\n", &a_brk);
if (val && strlen(val))
AIT_SET_STR(*v, val);
else
def = SESS_F_DEF;
e_free(Shared);
return i | def;
}
}
e_free(Shared);
return 0;
}
/*
* sess_DelValue() - Delete item from session shared memory
*
* @s = Session item
* @csAttr = Attribute for erasing
* return: -1 error: in parameter or !=-1 deleted items
*/
int
sess_DelValue(sess_t * __restrict s, const char *csAttr)
{
register int i;
int attrlen, d = 0;
char *Buffer, *Shared, szAttr[SESS_MAX_ATTRIBUTE];
char *peer, *p_brk;
if (!s || !csAttr || !*csAttr)
return -1;
else
attrlen = strlen(csAttr);
Buffer = Shared = NULL;
strlcpy(szAttr, csAttr, sizeof szAttr);
strlcat(szAttr, "=", sizeof szAttr);
Buffer = e_malloc(s->sess_size);
if (!Buffer) {
LOGERR;
return -1;
} else
memset(Buffer, 0, s->sess_size);
Shared = e_malloc(s->sess_size);
if (!Shared) {
LOGERR;
e_free(Buffer);
return -1;
} else {
DEC_SEM(s);
memcpy(Shared, s->sess_addr, s->sess_size);
}
for (i = 1, peer = strtok_r(Shared, SESS_MEM_DELIM"\r\n", &p_brk); peer;
i++, peer = strtok_r(NULL, SESS_MEM_DELIM"\r\n", &p_brk)) {
if (!strncmp(peer, csAttr, attrlen))
if (peer[attrlen] == '=' || !peer[attrlen]) {
d++;
continue;
}
strlcat(Buffer, peer, s->sess_size);
strlcat(Buffer, SESS_MEM_DELIM, s->sess_size);
}
memcpy(s->sess_addr, Buffer, s->sess_size);
if (s->sess_type == SHARED_MAP)
msync(s->sess_addr, 0, MS_SYNC | MS_INVALIDATE);
INC_SEM(s);
e_free(Shared);
e_free(Buffer);
return d;
}
/*
* sess_SetValue() - Set item into session shared memory or update if exists
*
* @s = Session item
* @csAttr = Attribute
* @psVal = Value
* return: 0 nothing, -1 error: in parameter,
>0 set position, if added new item merged with SESS_F_ADD and
if just define item merged with SESS_F_DEF
*/
int
sess_SetValue(sess_t * __restrict s, const char *csAttr, const char *psVal)
{
register int i;
int upd = 0, def = 0;
char *Buffer, *Shared, szAttr[SESS_MAX_ATTRIBUTE];
char *peer, *p_brk;
if (!s || !csAttr || !*csAttr)
return -1;
else
Buffer = Shared = NULL;
strlcpy(szAttr, csAttr, sizeof szAttr);
strlcat(szAttr, "=", sizeof szAttr);
Buffer = e_malloc(s->sess_size);
if (!Buffer) {
LOGERR;
return -1;
} else
memset(Buffer, 0, s->sess_size);
Shared = e_malloc(s->sess_size);
if (!Shared) {
LOGERR;
e_free(Buffer);
return -1;
} else {
DEC_SEM(s);
memcpy(Shared, s->sess_addr, s->sess_size);
}
for (i = 1, peer = strtok_r(Shared, SESS_MEM_DELIM"\r\n", &p_brk); peer;
i++, peer = strtok_r(NULL, SESS_MEM_DELIM"\r\n", &p_brk))
if (!strncmp(peer, szAttr, strlen(szAttr))) {
upd++;
if (psVal) {
strlcat(Buffer, szAttr, s->sess_size);
strlcat(Buffer, psVal, s->sess_size);
strlcat(Buffer, SESS_MEM_DELIM, s->sess_size);
} else {
strlcat(Buffer, csAttr, s->sess_size);
strlcat(Buffer, SESS_MEM_DELIM, s->sess_size);
def = SESS_F_DEF;
}
} else {
strlcat(Buffer, peer, s->sess_size);
strlcat(Buffer, SESS_MEM_DELIM, s->sess_size);
}
if (!upd) {
if (psVal) {
strlcat(Buffer, szAttr, s->sess_size);
strlcat(Buffer, psVal, s->sess_size);
strlcat(Buffer, SESS_MEM_DELIM, s->sess_size);
} else {
strlcat(Buffer, csAttr, s->sess_size);
strlcat(Buffer, SESS_MEM_DELIM, s->sess_size);
def = SESS_F_DEF;
}
def |= SESS_F_ADD;
}
memcpy(s->sess_addr, Buffer, s->sess_size);
if (s->sess_type == SHARED_MAP)
msync(s->sess_addr, 0, MS_SYNC | MS_INVALIDATE);
INC_SEM(s);
e_free(Shared);
e_free(Buffer);
return upd | def;
}
/*
* sess_prepareSession() - Attach to shared memory and de-marshaling data
*
* @s = Session
* @useDirect = Use direct shared memory if !=0 or snapshot of data to array
* return: NULL error or no data, !=NULL array with variables,
* after use must free resources with sess_doneSession()
*/
array_t *
sess_prepareSession(sess_t * __restrict s, char useDirect)
{
array_t *arr = NULL;
sess_hdr_t *hdr;
if (!s) {
io_SetErr(EINVAL, "Invalid argument\n");
return NULL;
}
if (s->sess_addr) {
io_SetErr(EINVAL, "Already attached memory\n");
return NULL;
}
if (!ATTACH_MEMORY(s))
return NULL;
else
hdr = (sess_hdr_t*) s->sess_addr;
if (hdr->hdr_magic != SESS_AIT_MAGIC) {
DETACH_MEMORY(s);
io_SetErr(EINVAL, "Shared memory not contains values with proper format\n");
return NULL;
}
DEC_SEM(s);
s->sess_zcpy = useDirect;
arr = ait_map2vars(s->sess_addr + sizeof(sess_hdr_t), s->sess_size - sizeof(sess_hdr_t),
hdr->hdr_argc, s->sess_zcpy);
INC_SEM(s);
if (!s->sess_zcpy)
DETACH_MEMORY(s);
return arr;
}
/*
* sess_doneSession() - Free resources allocated with sess_prepareSession()
*
* @s = Session
* @arr = Array with variables for free
* return: none
*/
void
sess_doneSession(sess_t * __restrict s, array_t ** __restrict arr)
{
if (!s) {
io_SetErr(EINVAL, "Invalid argument\n");
return;
}
if (!s->sess_zcpy)
array_Free(*arr);
else
DETACH_MEMORY(s);
array_Destroy(arr);
}
/*
* sess_commitSession() - Commit data to shared memory
*
* @s = Session
* @arr = Array with variables for save
* return -1 error or !=-1 size of stored variables into shared memory
*/
int
sess_commitSession(sess_t * __restrict s, array_t * __restrict arr)
{
sess_hdr_t *hdr;
int ret = 0;
if (!s || !arr) {
io_SetErr(EINVAL, "Invalid argument\n");
return -1;
}
if (!ATTACH_MEMORY(s)) {
DETACH_MEMORY(s);
return -1;
} else
hdr = (sess_hdr_t*) s->sess_addr;
DEC_SEM(s);
if ((ret = ait_vars2map(s->sess_addr + sizeof(sess_hdr_t),
s->sess_size - sizeof(sess_hdr_t), arr)) != -1) {
hdr->hdr_magic = SESS_AIT_MAGIC;
hdr->hdr_argc = array_Size(arr);
ret += sizeof(sess_hdr_t);
}
INC_SEM(s);
DETACH_MEMORY(s);
return ret;
}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>