/************************************************************************* * (C) 2008 AITNET ltd - Sofia/Bulgaria - * by Michael Pounov * * $Author: misho $ * $Id: aitsess.h,v 1.6 2012/07/22 22:13:48 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 Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 by Michael Pounov . 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 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. */ #ifndef __AITSESS_H #define __AITSESS_H #include #include #include #include #define SHARED_UNKNOWN -1 #define SHARED_IPC 0 #define SHARED_MAP 1 #define IS_VAL 0x0 #define IS_ADD 0x40000000 #define IS_DEF 0x80000000 #define ISNEW(x) (((x) & IS_ADD) == IS_ADD) #define ISDEF(x) (((x) & IS_DEF) == IS_DEF) #define MAX_ATTRIBUTE 64 #define MAX_SEMNAME 14 /* Shared memory session */ typedef struct tagSess { key_t key; char type; char zcpy; char name[BUFSIZ]; off_t eom; void *addr; off_t offset; union { int shmid; int fd; } mem; union { int semid; sem_t *sid; } id; struct { int (*create)(int, long, void *, ...); void (*destroy)(void *); void *(*attach)(void *, void *); void (*detach)(void *); void (*notSem)(void *); int (*isSemOK)(void *); int (*incSem)(void *); int (*decSem)(void *); } sess; } ait_sess_t; // sess_GetErrno() Get error code of last operation inline int sess_GetErrno(); // sess_GetError() Get error text of last operation inline const char *sess_GetError(); // sess_SetErr() Set error to variables for internal use!!! inline void sess_SetErr(int eno, char *estr, ...); /* * sess_initSession() Initializing session structure, if session file not exists creating with specified tech * * @id = Technology using in session. SHARED_IPC IPC tech orSHARED_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, ait_sess_t ** __restrict Sess); /* * sess_freeSession() Free allocated memory for session item and delete session file if present name * * @Sess = Session item * return: none */ void sess_freeSession(ait_sess_t ** __restrict Sess); /* * map_createSession() MMAP Created session and allocated resources * * @nSeed = Seed for securing key, if =-1 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, ait_sess_t * __restrict Sess, ...); /* * map_destroySession() MMAP free shared resources * * @Sess = Session item * return: none */ void map_destroySession(ait_sess_t * __restrict Sess); /* * ipc_createSession() IPC Created session and allocated resources * * @nSeed = Seed for securing key, if =-1 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, ait_sess_t * __restrict Sess, ...); /* * ipc_destroySession() IPC free shared resources * * @Sess = Session item * return: none */ void ipc_destroySession(ait_sess_t * __restrict Sess); /* * 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(ait_sess_t * __restrict s, void *procMem); /* * map_detachSession() MMAP Detach from shared memory * * @s = Session item * return: none */ void map_detachSession(ait_sess_t * __restrict s); /* * 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(ait_sess_t * __restrict s, void *procMem); /* * ipc_detachSession() IPC Detach from shared memory * * @s = Session item * return: none */ void ipc_detachSession(ait_sess_t * __restrict s); /* * sess_isAttached() Check for attached shared memory * * @s = Session item * return: -1 null session item, 0 not attached, 1 attached memory */ inline int sess_isAttached(ait_sess_t * __restrict s); /* * map_notSemaphore() MMAP negative block if semaphore isn`t signaled * * @s = Session item * return: none */ void map_notSemaphore(ait_sess_t * __restrict s); /* * map_isSemaphoreOK() MMAP Check semaphore * * @s = Session item * return: -1 error: can`t return semaphore, 0 = false, 1 = true */ int map_isSemaphoreOK(ait_sess_t * __restrict s); /* * map_incSemaphore() MMAP unblock semaphore, increment semaphore * * @s = Session item * return: 0 Ok, -1 error: can`t increment */ int map_incSemaphore(ait_sess_t * __restrict s); /* * map_decSemaphore() MMAP block semaphore, decrement semaphore * * @s = Session item * return: 0 Ok, -1 error: can`t decrement */ int map_decSemaphore(ait_sess_t * __restrict s); /* * ipc_notSemaphore() IPC negative block if semaphore isn`t signaled * * @s = Session item * return: none */ void ipc_notSemaphore(ait_sess_t * __restrict s); /* * ipc_isSemaphoreOK() IPC Check semaphore * * @s = Session item * return: -1 error: can`t return semaphore, 0 = false, 1 = true */ int ipc_isSemaphoreOK(ait_sess_t * __restrict s); /* * ipc_incSemaphore() IPC unblock semaphore, increment semaphore * * @s = Session item * return: 0 Ok, -1 error: can`t increment */ int ipc_incSemaphore(ait_sess_t * __restrict s); /* * ipc_decSemaphore() IPC block semaphore, decrement semaphore * * @s = Session item * return: 0 Ok, -1 error: can`t decrement */ int ipc_decSemaphore(ait_sess_t * __restrict s); /* --------------------------------------------------------- */ #define ALLOC_MEMORY(sd, siz, s, ...) (assert((s)), (s)->sess.create((sd), \ (siz), (s), ## __VA_ARGS__)) #define ATTACH_MEMORY(s) (assert((s)), (s)->sess.attach((s), NULL)) #define DETACH_MEMORY(s) do { assert((s)); (s)->sess.detach((s)); } while(0) #define FREE_MEMORY(s) do { assert((s)); (s)->sess.destroy((s)); } while(0) #define IS_SEMOK(s) (assert((s)), (s)->sess.isSemOK((s))) #define INC_SEM(s) (assert((s)), (s)->sess.incSem((s))) #define DEC_SEM(s) (assert((s)), (s)->sess.decSem((s))) #define NOT_SEM(s) do { assert((s)); (s)->sess.notSem((s)); } while(0) /* --------------------------------------------------------- */ /* * sess_FreeValues() Free all values from value array allocated from sess_GetValues() * * @ppsVals = Array strings * return: none */ inline void sess_FreeValues(char *** __restrict ppsVals); /* * sess_GetValues() Get all values from session shared memory * * @s = Session item * @ppsVals = Return array strings * return: -1 error: in parameter, !=-1 count of returned strings in ppsVals (must be free after use!) */ int sess_GetValues(ait_sess_t * __restrict s, char ***ppsVals); /* * sess_GetValue() Get value from session shared memory from attribute * * @s = Session item * @csAttr = Attribute for search * @psVal = Return string buffer * @pnLen = Length of return string buffer, // *{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 */ int sess_GetValue(ait_sess_t * __restrict s, const char *csAttr, char *psVal, int *pnLen); /* * sess_DelValue() Delete item from session shared memory * * @s = Session item * @csAttr = Attribute for erasing * return: 0 Ok, -1 error: in parameter */ int sess_DelValue(ait_sess_t * __restrict s, const char *csAttr); /* * sess_SetValue() Set item into session shared memory or update if find it * * @s = Session item * @csAttr = Attribute * @psVal = Value * 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 */ int sess_SetValue(ait_sess_t * __restrict s, const char *csAttr, const char *psVal); /* * 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(ait_sess_t * __restrict s, char useDirect); /* * sess_doneSession() Free resources allocated with sess_prepareSession() * * @s = Session * @arr = Array with variables for free * return: none */ void sess_doneSession(ait_sess_t * __restrict s, array_t ** __restrict 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(ait_sess_t * __restrict s, array_t * __restrict arr); #endif