Annotation of libaitsess/inc/aitsess.h, revision 1.2.2.2

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.2.2.2 ! misho       6: * $Id: aitsess.h,v 1.2.2.1 2011/04/30 22:02:59 misho Exp $
1.2       misho       7: *
1.2.2.1   misho       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
                     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: */
1.1       misho      46: #ifndef __AITSESS_H
                     47: #define __AITSESS_H
                     48: 
                     49: 
                     50: #include <semaphore.h>
                     51: #include <sys/types.h>
                     52: 
1.2.2.2 ! misho      53: #define SHARED_UNKNOWN 0
1.1       misho      54: #define SHARED_IPC     1
                     55: #define SHARED_MAP     2
                     56: 
                     57: #define IS_VAL         0x0
                     58: #define IS_ADD         0x40000000
                     59: #define IS_DEF         0x80000000
                     60: 
                     61: #define ISNEW(x)       (((x) & IS_ADD) == IS_ADD)
                     62: #define ISDEF(x)       (((x) & IS_DEF) == IS_DEF)
                     63: 
                     64: #define MAX_ATTRIBUTE  63
                     65: #define MAX_SEMNAME    14
                     66: 
                     67: 
                     68: typedef struct _tagSess {
                     69:        key_t   key;
                     70:        u_char  type;
                     71:        off_t   eom;
                     72:        void    *addr;
                     73:        off_t   offset;
                     74:        union {
                     75:                int     shmid;
                     76:                int     fd;
                     77:        } mem;
                     78:        union {
                     79:                int     semid;
                     80:                sem_t   *sid;
                     81:        } id;
                     82: } tagSess;
                     83: 
                     84: 
                     85: // -------------------------------------------------------
                     86: // sess_GetErrno() Get error code of last operation
                     87: inline int sess_GetErrno();
                     88: // sess_GetError() Get error text of last operation
                     89: inline const char *sess_GetError();
                     90: // -------------------------------------------------------
                     91: 
                     92: 
                     93: /*
                     94:  * initSession() Initializing session structure, if session file not exists creating with specified tech
1.2.2.2 ! misho      95:  * @pnID = Technology using in session. 
        !            96:        SHARED_IPC IPC tech; SHARED_MAP BSD MemoryMap tech or if =NULL SHARED_IPC
1.1       misho      97:  * @csFName = Session filename for build key and identified
1.2.2.2 ! misho      98:  * @Sess = Session item, if =NULL allocate memory for session after use must be free!
1.1       misho      99:  * return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded
                    100: */
1.2.2.2 ! misho     101: inline int initSession(int *pnID, const char *csFName, tagSess ** __restrict Sess);
1.1       misho     102: /*
                    103:  * freeSession() Free allocated memory for session item and delete session file if present name
                    104:  * @csFName = Session filename for delete, if NULL nothing delete
                    105:  * @Sess = Session item
                    106: */
                    107: inline void freeSession(const char *csFName, tagSess ** __restrict Sess);
                    108: 
                    109: /*
                    110:  * map_createSession() MMAP Created session and allocated resources
                    111:  * @csFName = Session name for identified
                    112:  * @cnSeed = Seed for securing key
                    113:  * @cnSize = Allocated shared memory size in bytes
                    114:  * @Sess = Session item
                    115:  * return: 0 Ok successful, -1 error: not allocated resources
                    116: */
                    117: int map_createSession(const char *csFName, const int cnSeed, const u_int cnSize, tagSess ** __restrict Sess);
                    118: /*
                    119:  * map_destroySession() MMAP free shared resources
                    120:  * @csFName = Session name for delete
                    121:  * @Sess = Session item
                    122: */
                    123: void map_destroySession(const char *csFName, tagSess ** __restrict Sess);
                    124: 
                    125: /*
                    126:  * ipc_createSession() IPC Created session and allocated resources
                    127:  * @csFName = Session name for identified
                    128:  * @cnSeed = Seed for securing key
                    129:  * @cnSize = Allocated shared memory size in bytes
                    130:  * @Sess = Session item
                    131:  * return: 0 Ok successful, -1 error: not allocated resources
                    132: */
                    133: int ipc_createSession(const char *csFName, const int cnSeed, const u_int cnSize, tagSess ** __restrict Sess);
                    134: /*
                    135:  * ipc_destroySession() IPC free shared resources
                    136:  * @csFName = Session name for delete
                    137:  * @Sess = Session item
                    138: */
                    139: void ipc_destroySession(const char *csFName, tagSess ** __restrict Sess);
                    140: 
                    141: /*
                    142:  * map_attachSession() MMAP Attach to shared memory & return begin address
                    143:  * @s = Session item
                    144:  * @procMem = Custom start address (optionl) *default must be 0*
                    145:  * return: NULL failed attach, !=NULL begin address of memory
                    146: */
                    147: inline void *map_attachSession(tagSess * __restrict s, void *procMem);
                    148: /*
                    149:  * map_detachSession() MMAP Detach from shared memory
                    150:  * @s = Session item
                    151: */
                    152: inline void map_detachSession(tagSess * __restrict s);
                    153: 
                    154: /*
                    155:  * ipc_attachSession() IPC Attach to shared memory & return begin address
                    156:  * @s = Session item
                    157:  * @procMem = Custom start address (optionl) *default must be 0*
                    158:  * return: NULL failed attach, !=NULL begin address of memory
                    159: */
                    160: inline void *ipc_attachSession(tagSess * __restrict s, void *procMem);
                    161: /*
                    162:  * ipc_detachSession() IPC Detach from shared memory
                    163:  * @s = Session item
                    164: */
                    165: inline void ipc_detachSession(tagSess * __restrict s);
                    166: 
                    167: /*
1.2       misho     168:  * isAttached() Check for mapped/(attached) shared memory
                    169:  * @s = Session item
                    170:  * return: -1 null session item, 0 not attached, 1 attached memory
                    171: */
                    172: inline int isAttached(tagSess * __restrict s);
                    173: 
                    174: 
                    175: /*
1.1       misho     176:  * map_notSemaphore() MMAP negative block if semaphore isn`t signaled
                    177:  * @s = Session item
                    178: */
                    179: inline void map_notSemaphore(tagSess * __restrict s);
                    180: /*
                    181:  * map_isSemaphored() MMAP Check semaphore
                    182:  * @s = Session item
                    183:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
                    184: */
                    185: inline int map_isSemaphored(tagSess * __restrict s);
                    186: /*
                    187:  * map_addSemaphore() MMAP unblock semaphore, increment semaphore
                    188:  * @s = Session item
                    189:  * return: 0 Ok, -1 error: can`t increment 
                    190: */
                    191: inline int map_addSemaphore(tagSess * __restrict s);
                    192: /*
                    193:  * map_decSemaphore() MMAP block semaphore, decrement semaphore
                    194:  * @s = Session item
                    195:  * return: 0 Ok, -1 error: can`t decrement 
                    196: */
                    197: inline int map_decSemaphore(tagSess * __restrict s);
                    198: 
                    199: /*
                    200:  * ipc_notSemaphore() IPC negative block if semaphore isn`t signaled
                    201:  * @s = Session item
                    202: */
                    203: inline void ipc_notSemaphore(tagSess * __restrict s);
                    204: /*
                    205:  * ipc_isSemaphored() IPC Check semaphore
                    206:  * @s = Session item
                    207:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
                    208: */
                    209: inline int ipc_isSemaphored(tagSess * __restrict s);
                    210: /*
                    211:  * ipc_addSemaphore() IPC unblock semaphore, increment semaphore
                    212:  * @s = Session item
                    213:  * return: 0 Ok, -1 error: can`t increment 
                    214: */
                    215: inline int ipc_addSemaphore(tagSess * __restrict s);
                    216: /*
                    217:  * ipc_decSemaphore() IPC block semaphore, decrement semaphore
                    218:  * @s = Session item
                    219:  * return: 0 Ok, -1 error: can`t decrement 
                    220: */
                    221: inline int ipc_decSemaphore(tagSess * __restrict s);
                    222: 
1.2       misho     223: 
1.1       misho     224: // ---------------------------------------------------------
                    225: //
                    226: //  Lazy macros for lazy programmers :-) by Michael Pounov; Optimizing work with sessions!
                    227: //
                    228: 
                    229: #define DESTROY_SESSION(fname, s)      do { \
1.2       misho     230:                                                switch (((*s))->type) { \
1.1       misho     231:                                                        case SHARED_IPC: \
                    232:                                                                         ipc_destroySession((fname), (s)); \
                    233:                                                                         break; \
                    234:                                                        case SHARED_MAP: \
                    235:                                                                         map_destroySession((fname), (s)); \
                    236:                                                                         break; \
                    237:                                                } \
                    238:                                        } while(0)
                    239: 
1.2       misho     240: #define ATTACH_MEMORY(s)               do { \
1.1       misho     241:                                                switch ((s)->type) { \
                    242:                                                        case SHARED_IPC: \
1.2       misho     243:                                                                         ipc_attachSession((s), 0); \
1.1       misho     244:                                                                         break; \
                    245:                                                        case SHARED_MAP: \
1.2       misho     246:                                                                         map_attachSession((s), 0); \
1.1       misho     247:                                                                         break; \
                    248:                                                } \
                    249:                                        } while(0)
                    250: 
                    251: #define DETACH_MEMORY(s)               do { \
                    252:                                                switch ((s)->type) { \
                    253:                                                        case SHARED_IPC: \
                    254:                                                                         ipc_detachSession((s)); \
                    255:                                                                         break; \
                    256:                                                        case SHARED_MAP: \
                    257:                                                                         map_detachSession((s)); \
                    258:                                                                         break; \
                    259:                                                } \
                    260:                                        } while(0)
                    261: 
                    262: #define NOT_SEMAPHORE(s)       do { \
                    263:                                        switch ((s)->type) { \
                    264:                                                case SHARED_IPC: \
                    265:                                                                 ipc_notSemaphore((s)); \
                    266:                                                                 break; \
                    267:                                                case SHARED_MAP: \
                    268:                                                                 map_notSemaphore((s)); \
                    269:                                                                 break; \
                    270:                                        } \
                    271:                                } while(0)
                    272: 
                    273: #define IS_SEMAPHORED(s, ret)  do { \
                    274:                                        switch ((s)->type) { \
                    275:                                                case SHARED_IPC: \
                    276:                                                                 (ret) = ipc_isSemaphored((s)); \
                    277:                                                                 break; \
                    278:                                                case SHARED_MAP: \
                    279:                                                                 (ret) = map_isSemaphored((s)); \
                    280:                                                                 break; \
                    281:                                                default: \
                    282:                                                         (ret) = -1; \
                    283:                                        } \
                    284:                                } while(0)
                    285: 
                    286: #define ADD_SEMAPHORE(s, ret)  do { \
                    287:                                        switch ((s)->type) { \
                    288:                                                case SHARED_IPC: \
                    289:                                                                 (ret) = ipc_addSemaphore((s)); \
                    290:                                                                 break; \
                    291:                                                case SHARED_MAP: \
                    292:                                                                 (ret) = map_addSemaphore((s)); \
                    293:                                                                 break; \
                    294:                                                default: \
                    295:                                                         (ret) = -1; \
                    296:                                        } \
                    297:                                } while(0)
                    298: 
                    299: #define DEC_SEMAPHORE(s, ret)  do { \
                    300:                                        switch ((s)->type) { \
                    301:                                                case SHARED_IPC: \
                    302:                                                                 (ret) = ipc_decSemaphore((s)); \
                    303:                                                                 break; \
                    304:                                                case SHARED_MAP: \
                    305:                                                                 (ret) = map_decSemaphore((s)); \
                    306:                                                                 break; \
                    307:                                                default: \
                    308:                                                         (ret) = -1; \
                    309:                                        } \
                    310:                                } while(0)
                    311: 
                    312: // ---------------------------------------------------------
                    313: 
                    314: /*
1.2       misho     315:  * sess_FreeValues() Free all values from value array allocated from sess_GetValues()
                    316:  * @ppsVals = Array strings
                    317:  * return: none
                    318: */
                    319: inline void sess_FreeValues(char *** __restrict ppsVals);
                    320: /*
                    321:  * sess_GetValues() Get all values from session shared memory
                    322:  * @s = Session item
                    323:  * @ppsVals = Return array strings
                    324:  * return: -1 error: in parameter, !=-1 count of returned strings in ppsVals (must be free after use!)
                    325: */
                    326: int sess_GetValues(tagSess * __restrict s, char ***ppsVals);
                    327: /*
1.1       misho     328:  * sess_GetValue() Get value from session shared memory from attribute
                    329:  * @s = Session item
                    330:  * @csAttr = Attribute for search
                    331:  * @psVal = Return string buffer
                    332:  * @pnLen = Length of return string buffer, 
                    333:        // *{pnLen} input is max_size of buffer & output is really taken bytes
                    334:  * return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF
                    335: */
                    336: int sess_GetValue(tagSess * __restrict s, const char *csAttr, char *psVal, int *pnLen);
                    337: /*
                    338:  * sess_DelValue() Delete item from session shared memory
                    339:  * @s = Session item
                    340:  * @csAttr = Attribute for erasing
                    341:  * return: 0 Ok, -1 error: in parameter
                    342: */
                    343: int sess_DelValue(tagSess * __restrict s, const char *csAttr);
                    344: /*
                    345:  * sess_SetValue() Set item into session shared memory or update if find it
                    346:  * @s = Session item
                    347:  * @csAttr = Attribute
                    348:  * @psVal = Value
                    349:  * return: 0 nothing, -1 error: in parameter, 
                    350:        >0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF
                    351: */
                    352: int sess_SetValue(tagSess * __restrict s, const char *csAttr, const char *psVal);
                    353: 
                    354: 
                    355: #endif

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