Annotation of libaitio/inc/aitsess.h, revision 1.3.2.1

1.2       misho       1: /*************************************************************************
                      2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
                      3: *  by Michael Pounov <misho@openbsd-bg.org>
                      4: *
                      5: * $Author: misho $
1.3.2.1 ! misho       6: * $Id: aitsess.h,v 1.3 2013/03/19 17:31:38 misho Exp $
1.2       misho       7: *
                      8: **************************************************************************
                      9: The ELWIX and AITNET software is distributed under the following
                     10: terms:
                     11: 
                     12: All of the documentation and software included in the ELWIX and AITNET
                     13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
                     14: 
                     15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
                     16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
                     17: 
                     18: Redistribution and use in source and binary forms, with or without
                     19: modification, are permitted provided that the following conditions
                     20: are met:
                     21: 1. Redistributions of source code must retain the above copyright
                     22:    notice, this list of conditions and the following disclaimer.
                     23: 2. Redistributions in binary form must reproduce the above copyright
                     24:    notice, this list of conditions and the following disclaimer in the
                     25:    documentation and/or other materials provided with the distribution.
                     26: 3. All advertising materials mentioning features or use of this software
                     27:    must display the following acknowledgement:
                     28: This product includes software developed by Michael Pounov <misho@elwix.org>
                     29: ELWIX - Embedded LightWeight unIX and its contributors.
                     30: 4. Neither the name of AITNET nor the names of its contributors
                     31:    may be used to endorse or promote products derived from this software
                     32:    without specific prior written permission.
                     33: 
                     34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
                     35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44: SUCH DAMAGE.
                     45: */
                     46: #ifndef __AITSESS_H
                     47: #define __AITSESS_H
                     48: 
                     49: 
                     50: #include <pthread.h>
                     51: #include <semaphore.h>
                     52: #include <elwix.h>
                     53: 
                     54: #define SHARED_UNKNOWN -1
                     55: #define SHARED_IPC     0
                     56: #define SHARED_MAP     1
                     57: 
1.3       misho      58: #define SESS_OPT_SEED  -1
1.2       misho      59: 
1.3       misho      60: #define SESS_F_ADD     0x10000000
                     61: #define SESS_F_DEF     0x20000000
1.2       misho      62: 
1.3       misho      63: #define SESS_RET_NEW(x)        (((x) & SESS_F_ADD) == SESS_F_ADD)
                     64: #define SESS_RET_DEF(x)        (((x) & SESS_F_DEF) == SESS_F_DEF)
                     65: #define SESS_RET_VAL(x)        ((x) & 0x0fffffff)
1.2       misho      66: 
                     67: /* Shared memory session */
                     68: 
                     69: typedef struct tagSess {
1.3       misho      70:        key_t   sess_key;
                     71:        char    sess_type;
                     72:        char    sess_zcpy;
                     73: 
                     74:        char    sess_name[STRSIZ];
                     75:        off_t   sess_size;
                     76:        void    *sess_addr;
                     77:        off_t   sess_offset;
                     78: 
1.2       misho      79:        union {
                     80:                int     shmid;
                     81:                int     fd;
1.3       misho      82:        } sess_mem;
1.2       misho      83:        union {
                     84:                int     semid;
                     85:                sem_t   *sid;
1.3       misho      86:        } sess_id;
1.2       misho      87: 
1.3       misho      88:        /* Session callbacks */
1.2       misho      89:        struct {
                     90:                int (*create)(int, long, void *, ...);
                     91:                void (*destroy)(void *);
                     92:                void *(*attach)(void *, void *);
                     93:                void (*detach)(void *);
                     94:                void (*notSem)(void *);
                     95:                int (*isSemOK)(void *);
                     96:                int (*incSem)(void *);
                     97:                int (*decSem)(void *);
                     98:        } sess;
1.3       misho      99: } sess_t;
                    100: 
                    101: /* --------------------------------------------------------- */
                    102: 
                    103: #define ALLOC_MEMORY(sd, siz, s, ...)  (assert((s) && (s)->sess.create), \
                    104:                                                (s)->sess.create((sd), (siz), (s), ## __VA_ARGS__))
                    105: #define ATTACH_MEMORY(s)               (assert((s) && (s)->sess.attach), (s)->sess.attach((s), NULL))
                    106: #define DETACH_MEMORY(s)               (assert((s) && (s)->sess.detach), (s)->sess.detach((s)))
                    107: #define FREE_MEMORY(s)                 (assert((s) && (s)->sess.destroy), (s)->sess.destroy((s)))
                    108: 
                    109: #define IS_SEMOK(s)                    (assert((s) && (s)->sess.isSemOK), (s)->sess.isSemOK((s)))
                    110: #define INC_SEM(s)                     (assert((s) && (s)->sess.incSem), (s)->sess.incSem((s)))
                    111: #define DEC_SEM(s)                     (assert((s) && (s)->sess.decSem), (s)->sess.decSem((s)))
                    112: #define NOT_SEM(s)                     (assert((s) && (s)->sess.notSem), (s)->sess.notSem((s)))
                    113: 
                    114: /* --------------------------------------------------------- */
1.2       misho     115: 
                    116: 
                    117: /*
                    118:  * sess_initSession() - Initializing session structure, 
                    119:  *                     if session file not exists creating with specified tech
                    120:  *
                    121:  * @id = Technology using in session. SHARED_IPC IPC tech or SHARED_MAP BSD MemoryMap tech
                    122:  * @csFName = Session filename for build key and identified
                    123:  * @Sess = Session item, if =NULL allocate memory for session after use must be free!
                    124:  * return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded
                    125:  */
1.3       misho     126: int sess_initSession(int id, const char *csFName, sess_t ** __restrict Sess);
1.2       misho     127: /*
                    128:  * sess_freeSession() - Free allocated memory for session item and delete session file if present name
                    129:  *
                    130:  * @Sess = Session item
                    131:  * return: none
                    132:  */
1.3       misho     133: void sess_freeSession(sess_t ** __restrict Sess);
1.2       misho     134: 
                    135: /*
                    136:  * map_createSession() - MMAP Created session and allocated resources
                    137:  *
1.3       misho     138:  * @nSeed = Seed for securing key, if =SESS_OPT_SEED must add ready for use key
1.2       misho     139:  * @nSize = Allocated shared memory size in bytes
                    140:  * @Sess = Session item
                    141:  * @... = If nSeed == -1 add ready for use key value
                    142:  * return: 0 Ok successful, -1 error: not allocated resources
                    143:  */
1.3       misho     144: int map_createSession(int nSeed, long nSize, sess_t * __restrict Sess, ...);
1.2       misho     145: /*
                    146:  * map_destroySession() - MMAP free shared resources
                    147:  *
                    148:  * @Sess = Session item
                    149:  * return: none
                    150:  */
1.3       misho     151: void map_destroySession(sess_t * __restrict Sess);
1.2       misho     152: 
                    153: /*
                    154:  * ipc_createSession() - IPC Created session and allocated resources
                    155:  *
1.3       misho     156:  * @nSeed = Seed for securing key, if =SESS_OPT_SEED must add ready for use key
1.2       misho     157:  * @nSize = Allocated shared memory size in bytes
                    158:  * @Sess = Session item
                    159:  * @... = If nSeed == -1 add ready for use key value
                    160:  * return: 0 Ok successful, -1 error: not allocated resources
                    161:  */
1.3       misho     162: int ipc_createSession(int nSeed, long nSize, sess_t * __restrict Sess, ...);
1.2       misho     163: /*
                    164:  * ipc_destroySession() - IPC free shared resources
                    165:  *
                    166:  * @Sess = Session item
                    167:  * return: none
                    168:  */
1.3       misho     169: void ipc_destroySession(sess_t * __restrict Sess);
1.2       misho     170: 
                    171: /*
                    172:  * map_attachSession() - MMAP Attach to shared memory & return begin address
                    173:  *
                    174:  * @s = Session item
                    175:  * @procMem = Custom start address (optionl) *default must be 0*
                    176:  * return: NULL failed attach, !=NULL begin address of memory
                    177:  */
1.3       misho     178: void *map_attachSession(sess_t * __restrict s, void *procMem);
1.2       misho     179: /*
                    180:  * map_detachSession() - MMAP Detach from shared memory
                    181:  *
                    182:  * @s = Session item
                    183:  * return: none
                    184:  */
1.3       misho     185: void map_detachSession(sess_t * __restrict s);
1.2       misho     186: 
                    187: /*
                    188:  * ipc_attachSession() - IPC Attach to shared memory & return begin address
                    189:  *
                    190:  * @s = Session item
                    191:  * @procMem = Custom start address (optionl) *default must be 0*
                    192:  * return: NULL failed attach, !=NULL begin address of memory
                    193:  */
1.3       misho     194: void *ipc_attachSession(sess_t * __restrict s, void *procMem);
1.2       misho     195: /*
                    196:  * ipc_detachSession() - IPC Detach from shared memory
                    197:  *
                    198:  * @s = Session item
                    199:  * return: none
                    200:  */
1.3       misho     201: void ipc_detachSession(sess_t * __restrict s);
1.2       misho     202: 
                    203: /*
                    204:  * sess_isAttached() - Check for attached shared memory
                    205:  *
                    206:  * @s = Session item
                    207:  * return: -1 null session item, 0 not attached, 1 attached memory
                    208:  */
1.3.2.1 ! misho     209: int sess_isAttached(sess_t * __restrict s);
1.2       misho     210: 
                    211: 
                    212: /*
                    213:  * map_notSemaphore() - MMAP negative block if semaphore isn`t signaled
                    214:  *
                    215:  * @s = Session item
                    216:  * return: none
                    217:  */
1.3       misho     218: void map_notSemaphore(sess_t * __restrict s);
1.2       misho     219: /*
                    220:  * map_isSemaphoreOK() - MMAP Check semaphore
                    221:  *
                    222:  * @s = Session item
                    223:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
                    224:  */
1.3       misho     225: int map_isSemaphoreOK(sess_t * __restrict s);
1.2       misho     226: /*
                    227:  * map_incSemaphore() - MMAP unblock semaphore, increment semaphore
                    228:  *
                    229:  * @s = Session item
                    230:  * return: 0 Ok, -1 error: can`t increment 
                    231:  */
1.3       misho     232: int map_incSemaphore(sess_t * __restrict s);
1.2       misho     233: /*
                    234:  * map_decSemaphore() - MMAP block semaphore, decrement semaphore
                    235:  *
                    236:  * @s = Session item
                    237:  * return: 0 Ok, -1 error: can`t decrement 
                    238:  */
1.3       misho     239: int map_decSemaphore(sess_t * __restrict s);
1.2       misho     240: 
                    241: /*
                    242:  * ipc_notSemaphore() - IPC negative block if semaphore isn`t signaled
                    243:  *
                    244:  * @s = Session item
                    245:  * return: none
                    246:  */
1.3       misho     247: void ipc_notSemaphore(sess_t * __restrict s);
1.2       misho     248: /*
                    249:  * ipc_isSemaphoreOK() - IPC Check semaphore
                    250:  *
                    251:  * @s = Session item
                    252:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
                    253:  */
1.3       misho     254: int ipc_isSemaphoreOK(sess_t * __restrict s);
1.2       misho     255: /*
                    256:  * ipc_incSemaphore() - IPC unblock semaphore, increment semaphore
                    257:  *
                    258:  * @s = Session item
                    259:  * return: 0 Ok, -1 error: can`t increment 
                    260:  */
1.3       misho     261: int ipc_incSemaphore(sess_t * __restrict s);
1.2       misho     262: /*
                    263:  * ipc_decSemaphore() - IPC block semaphore, decrement semaphore
                    264:  *
                    265:  * @s = Session item
                    266:  * return: 0 Ok, -1 error: can`t decrement 
                    267:  */
1.3       misho     268: int ipc_decSemaphore(sess_t * __restrict s);
1.2       misho     269: 
                    270: 
                    271: /*
                    272:  * sess_GetValues() - Get all values from session shared memory
                    273:  *
                    274:  * @s = Session item
1.3       misho     275:  * @Vals = Return array strings
                    276:  * return: -1 error: in parameter, !=-1 count of returned strings in Vals 
                    277:  *             (must call ait_freeVars() after use!)
1.2       misho     278:  */
1.3       misho     279: int sess_GetValues(sess_t * __restrict s, array_t ** __restrict Vals);
1.2       misho     280: /*
                    281:  * sess_GetValue() - Get value from session shared memory from attribute
                    282:  *
                    283:  * @s = Session item
                    284:  * @csAttr = Attribute for search
1.3       misho     285:  * @v = Return string value and after use must call ait_freeVar(), also may be =NULL
1.2       misho     286:  * return: 0 not found, -1 error: in parameter, >0 get position, 
                    287:  *     if define item merged with IS_DEF
                    288:  */
1.3       misho     289: int sess_GetValue(sess_t * __restrict s, const char *csAttr, ait_val_t ** __restrict v);
1.2       misho     290: /*
                    291:  * sess_DelValue() - Delete item from session shared memory
                    292:  *
                    293:  * @s = Session item
                    294:  * @csAttr = Attribute for erasing
1.3       misho     295:  * return: -1 error: in parameter or !=-1 deleted items
1.2       misho     296:  */
1.3       misho     297: int sess_DelValue(sess_t * __restrict s, const char *csAttr);
1.2       misho     298: /*
                    299:  * sess_SetValue() - Set item into session shared memory or update if exists
                    300:  *
                    301:  * @s = Session item
                    302:  * @csAttr = Attribute
                    303:  * @psVal = Value
                    304:  * return: 0 nothing, -1 error: in parameter, 
1.3       misho     305:        >0 set position, if added new item merged with SESS_F_ADD and 
                    306:        if just define item merged with SESS_F_DEF
1.2       misho     307:  */
1.3       misho     308: int sess_SetValue(sess_t * __restrict s, const char *csAttr, const char *psVal);
1.2       misho     309: 
                    310: /*
                    311:  * sess_prepareSession() - Attach to shared memory and de-marshaling data
                    312:  *
                    313:  * @s = Session
                    314:  * @useDirect = Use direct shared memory if !=0 or snapshot of data to array
                    315:  * return: NULL error or no data, !=NULL array with variables, 
                    316:  *             after use must free resources with sess_doneSession()
                    317:  */
1.3       misho     318: array_t *sess_prepareSession(sess_t * __restrict s, char useDirect);
1.2       misho     319: /*
                    320:  * sess_doneSession() - Free resources allocated with sess_prepareSession()
                    321:  *
                    322:  * @s = Session
                    323:  * @arr = Array with variables for free
                    324:  * return: none
                    325:  */
1.3       misho     326: void sess_doneSession(sess_t * __restrict s, array_t ** __restrict arr);
1.2       misho     327: /*
                    328:  * sess_commitSession() - Commit data to shared memory
                    329:  *
                    330:  * @s = Session
                    331:  * @arr = Array with variables for save
                    332:  * return -1 error or !=-1 size of stored variables into shared memory
                    333:  */
1.3       misho     334: int sess_commitSession(sess_t * __restrict s, array_t * __restrict arr);
1.2       misho     335: 
                    336: 
                    337: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>