Annotation of libaitsess/src/aitsess.c, revision 1.6.4.3

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.6.4.3 ! misho       6: * $Id: aitsess.c,v 1.6.4.2 2013/01/17 13:12:47 misho Exp $
1.2       misho       7: *
1.3       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: 
1.6.4.1   misho      15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013
1.3       misho      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: #include "global.h"
                     47: 
                     48: 
                     49: #pragma GCC visibility push(hidden)
                     50: 
1.3       misho      51: int sess_Errno;
                     52: char sess_Error[STRSIZ];
1.1       misho      53: 
                     54: #pragma GCC visibility pop
                     55: 
                     56: 
                     57: // Error maintenance functions ...
                     58: 
                     59: // sess_GetErrno() Get error code of last operation
1.3       misho      60: inline int
                     61: sess_GetErrno()
1.1       misho      62: {
1.3       misho      63:        return sess_Errno;
1.1       misho      64: }
                     65: // sess_GetError() Get error text of last operation
1.3       misho      66: inline const char *
                     67: sess_GetError()
1.1       misho      68: {
1.3       misho      69:        return sess_Error;
1.1       misho      70: }
1.3       misho      71: // sess_SetErr() Set error to variables for internal use!!!
                     72: inline void
                     73: sess_SetErr(int eno, char *estr, ...)
1.1       misho      74: {
                     75:        va_list lst;
                     76: 
1.3       misho      77:        sess_Errno = eno;
                     78:        memset(sess_Error, 0, sizeof sess_Error);
                     79:        va_start(lst, estr);
                     80:        vsnprintf(sess_Error, sizeof sess_Error, estr, lst);
1.1       misho      81:        va_end(lst);
                     82: }
                     83: 
                     84: 
                     85: /*
1.6.4.3 ! misho      86:  * sess_initSession() - Initializing session structure, 
1.6.4.1   misho      87:  *                     if session file not exists creating with specified tech
1.4       misho      88:  *
1.6.4.1   misho      89:  * @id = Technology using in session. SHARED_IPC IPC tech or SHARED_MAP BSD MemoryMap tech
1.1       misho      90:  * @csFName = Session filename for build key and identified
1.3       misho      91:  * @Sess = Session item, if =NULL allocate memory for session after use must be free!
1.1       misho      92:  * return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded
                     93: */
1.4       misho      94: int
                     95: sess_initSession(int id, const char *csFName, ait_sess_t ** __restrict Sess)
1.1       misho      96: {
1.4       misho      97:        int h, ret = 0;
1.3       misho      98:        char szStr[STRSIZ];
1.1       misho      99: 
1.4       misho     100:        if (!csFName) {
                    101:                sess_SetErr(EINVAL, "Filename is NULL");
                    102:                return -1;
                    103:        }
                    104:        if (id < SHARED_UNKNOWN || id > SHARED_MAP) {
                    105:                sess_SetErr(EPROTONOSUPPORT, "Session type not supported");
                    106:                return -1;
                    107:        }
                    108: 
1.1       misho     109:        if (!*Sess) {
1.6.4.1   misho     110:                *Sess = e_malloc(sizeof(ait_sess_t));
1.1       misho     111:                if (!*Sess) {
                    112:                        LOGERR;
                    113:                        return -1;
                    114:                }
1.4       misho     115:        }
                    116:        memset(*Sess, 0, sizeof(ait_sess_t));
                    117:        strlcpy((*Sess)->name, csFName, sizeof (*Sess)->name);
1.1       misho     118: 
1.4       misho     119:        h = open((*Sess)->name, O_WRONLY | O_CREAT | O_EXCL, MEM_MODE);
1.1       misho     120:        if (h == -1) {
1.3       misho     121:                if (errno != EEXIST) {
                    122:                        LOGERR;
1.6.4.1   misho     123:                        e_free(*Sess);
1.3       misho     124:                        return -1;
                    125:                }
                    126:                /* If key file exist, session already connected */
1.4       misho     127:                h = open((*Sess)->name, O_RDONLY);
1.3       misho     128:                if (h == -1) {
                    129:                        LOGERR;
1.6.4.1   misho     130:                        e_free(*Sess);
1.3       misho     131:                        return -1;
                    132:                }
                    133:                ret = read(h, szStr, sizeof szStr);
                    134:                if (ret == -1) {
1.1       misho     135:                        LOGERR;
                    136:                        close(h);
1.6.4.1   misho     137:                        e_free(*Sess);
1.1       misho     138:                        return -1;
1.3       misho     139:                }
1.4       misho     140:                if (!strncmp(szStr, "IPC@", 4) && id == SHARED_IPC) {
                    141:                        ret = 1;
                    142: 
                    143:                        (*Sess)->sess.create = (int (*)(int, long, void*, ...)) ipc_createSession;
                    144:                        (*Sess)->sess.destroy = (void (*)(void*)) ipc_destroySession;
                    145:                        (*Sess)->sess.attach = (void* (*)(void*, void*)) ipc_attachSession;
                    146:                        (*Sess)->sess.detach = (void (*)(void*)) ipc_detachSession;
                    147:                        (*Sess)->sess.notSem = (void (*)(void*)) ipc_notSemaphore;
                    148:                        (*Sess)->sess.isSemOK = (int (*)(void*)) ipc_isSemaphoreOK;
                    149:                        (*Sess)->sess.incSem = (int (*)(void*)) ipc_incSemaphore;
                    150:                        (*Sess)->sess.decSem = (int (*)(void*)) ipc_decSemaphore;
                    151:                } else if (!strncmp(szStr, "MAP@", 4) && id == SHARED_MAP) {
                    152:                        ret = 1;
                    153: 
                    154:                        (*Sess)->sess.create = (int (*)(int, long, void*, ...)) map_createSession;
                    155:                        (*Sess)->sess.destroy = (void (*)(void*)) map_destroySession;
                    156:                        (*Sess)->sess.attach = (void* (*)(void*, void*)) map_attachSession;
                    157:                        (*Sess)->sess.detach = (void (*)(void*)) map_detachSession;
                    158:                        (*Sess)->sess.notSem = (void (*)(void*)) map_notSemaphore;
                    159:                        (*Sess)->sess.isSemOK = (int (*)(void*)) map_isSemaphoreOK;
                    160:                        (*Sess)->sess.incSem = (int (*)(void*)) map_incSemaphore;
                    161:                        (*Sess)->sess.decSem = (int (*)(void*)) map_decSemaphore;
                    162:                } else {
                    163:                        sess_SetErr(EPROTONOSUPPORT, 
                    164:                                        "Session type not supported or wrong session type");
1.3       misho     165:                        close(h);
1.6.4.1   misho     166:                        e_free(*Sess);
1.3       misho     167:                        return -1;
                    168:                }
1.4       misho     169:                /* key found */
1.3       misho     170:        } else {
                    171:                /* Build new key & new session */
1.4       misho     172:                if (id == SHARED_IPC) {
                    173:                        strlcpy(szStr, "IPC@", sizeof szStr);
                    174: 
                    175:                        (*Sess)->sess.create = (int (*)(int, long, void*, ...)) ipc_createSession;
                    176:                        (*Sess)->sess.destroy = (void (*)(void*)) ipc_destroySession;
                    177:                        (*Sess)->sess.attach = (void* (*)(void*, void*)) ipc_attachSession;
                    178:                        (*Sess)->sess.detach = (void (*)(void*)) ipc_detachSession;
                    179:                        (*Sess)->sess.notSem = (void (*)(void*)) ipc_notSemaphore;
                    180:                        (*Sess)->sess.isSemOK = (int (*)(void*)) ipc_isSemaphoreOK;
                    181:                        (*Sess)->sess.incSem = (int (*)(void*)) ipc_incSemaphore;
                    182:                        (*Sess)->sess.decSem = (int (*)(void*)) ipc_decSemaphore;
                    183:                } else if (id == SHARED_MAP) {
                    184:                        strlcpy(szStr, "MAP@", sizeof szStr);
                    185: 
                    186:                        (*Sess)->sess.create = (int (*)(int, long, void*, ...)) map_createSession;
                    187:                        (*Sess)->sess.destroy = (void (*)(void*)) map_destroySession;
                    188:                        (*Sess)->sess.attach = (void* (*)(void*, void*)) map_attachSession;
                    189:                        (*Sess)->sess.detach = (void (*)(void*)) map_detachSession;
                    190:                        (*Sess)->sess.notSem = (void (*)(void*)) map_notSemaphore;
                    191:                        (*Sess)->sess.isSemOK = (int (*)(void*)) map_isSemaphoreOK;
                    192:                        (*Sess)->sess.incSem = (int (*)(void*)) map_incSemaphore;
                    193:                        (*Sess)->sess.decSem = (int (*)(void*)) map_decSemaphore;
                    194:                } else {
                    195:                        sess_SetErr(EINVAL, "Session type must be specified");
                    196:                        close(h);
                    197:                        unlink(csFName);
1.6.4.1   misho     198:                        e_free(*Sess);
1.4       misho     199:                        return -1;
1.3       misho     200:                }
                    201:                strlcat(szStr, "ELWIX_Session ("PACKAGE_STRING")\n", sizeof szStr);
                    202:                write(h, szStr, strlen(szStr));
                    203: 
1.4       misho     204:                ret = 0;
                    205:                /* new key created */
1.1       misho     206:        }
                    207:        close(h);
                    208: 
1.3       misho     209:        (*Sess)->type = id;
1.4       misho     210:        (*Sess)->zcpy = (char) ret;
1.1       misho     211:        return ret;
                    212: }
                    213: 
                    214: /*
1.6.4.3 ! misho     215:  * sess_freeSession() - Free allocated memory for session item and delete session file if present name
1.4       misho     216:  *
1.1       misho     217:  * @Sess = Session item
1.4       misho     218:  * return: none
1.1       misho     219: */
1.4       misho     220: void
                    221: sess_freeSession(ait_sess_t ** __restrict Sess)
1.1       misho     222: {
1.4       misho     223:        if (!Sess || !(*Sess))
1.3       misho     224:                return;
                    225: 
1.4       misho     226:        if ((*Sess)->addr)
                    227:                DETACH_MEMORY(*Sess);
                    228: 
                    229:        /*
                    230:        memset(&(*Sess)->sess, 0, sizeof (*Sess)->sess);
                    231: 
1.3       misho     232:        (*Sess)->type = SHARED_UNKNOWN;
1.4       misho     233:        */
                    234: 
1.6.4.1   misho     235:        e_free(*Sess);
1.1       misho     236:        *Sess = NULL;
                    237: }
                    238: 
                    239: 
                    240: /*
1.6.4.3 ! misho     241:  * map_createSession() - MMAP Created session and allocated resources
1.4       misho     242:  *
                    243:  * @nSeed = Seed for securing key, if =-1 must add ready for use key
                    244:  * @nSize = Allocated shared memory size in bytes
1.1       misho     245:  * @Sess = Session item
1.4       misho     246:  * @... = If nSeed == -1 add ready for use key value
1.1       misho     247:  * return: 0 Ok successful, -1 error: not allocated resources
                    248: */
1.3       misho     249: int
1.4       misho     250: map_createSession(int nSeed, long nSize, ait_sess_t * __restrict Sess, ...)
1.1       misho     251: {
1.3       misho     252:        char szSName[2][FILENAME_MAX];
1.4       misho     253:        va_list lst;
1.1       misho     254: 
1.4       misho     255:        if (!Sess || !*Sess->name)
1.1       misho     256:                return -1;
                    257: 
1.4       misho     258:        if (nSeed != -1) {
                    259:                /* genkey */
                    260:                Sess->key = ftok(Sess->name, nSeed);
                    261:                if (Sess->key == -1) {
                    262:                        LOGERR;
                    263:                        return -1;
                    264:                }
                    265:        } else {
                    266:                /* get key from va_args */
                    267:                va_start(lst, Sess);
                    268:                Sess->key = va_arg(lst, key_t);
                    269:                va_end(lst);
1.1       misho     270:        }
                    271: 
1.3       misho     272:        /* build semaphore & shared memory name */
                    273:        memset(szSName, 0, sizeof szSName);
1.4       misho     274:        snprintf(szSName[0], MAX_SEMNAME, "/%X.ANS", (u_int) Sess->key);
1.5       misho     275: #ifdef HAVE_SHM_OPEN
                    276:        snprintf(szSName[1], FILENAME_MAX, "/%s-%x.ANM", Sess->name, (u_int) Sess->key);
                    277: #else
1.4       misho     278:        snprintf(szSName[1], FILENAME_MAX, "%s-%x.ANM", Sess->name, (u_int) Sess->key);
1.5       misho     279: #endif
1.1       misho     280: 
1.3       misho     281:        /* create semaphore & add 1 */
1.4       misho     282:        Sess->id.sid = sem_open(szSName[0], O_CREAT, MEM_MODE);
                    283:        if (Sess->id.sid == SEM_FAILED) {
1.1       misho     284:                LOGERR;
1.4       misho     285:                map_destroySession(Sess);
1.1       misho     286:                return -1;
1.4       misho     287:        }
                    288:        /* if is new shared memory session, init sempahore with 1 */
                    289:        if (!Sess->zcpy)
                    290:                sem_post(Sess->id.sid);
1.1       misho     291: 
1.3       misho     292:        /* create file for shared memory storage */
1.5       misho     293: #ifdef HAVE_SHM_OPEN
                    294:        Sess->mem.fd = shm_open(szSName[1], O_RDWR | O_CREAT, MEM_MODE);
                    295: #else
1.4       misho     296:        Sess->mem.fd = open(szSName[1], O_RDWR | O_CREAT, MEM_MODE);
1.5       misho     297: #endif
1.4       misho     298:        if (Sess->mem.fd == -1) {
1.1       misho     299:                LOGERR;
1.4       misho     300:                map_destroySession(Sess);
1.1       misho     301:                return -1;
                    302:        }
1.5       misho     303:        if (!Sess->zcpy) {
                    304: #ifdef HAVE_SHM_OPEN
                    305:                if (ftruncate(Sess->mem.fd, nSize) == -1) {
                    306:                        LOGERR;
                    307:                        map_destroySession(Sess);
                    308:                        return -1;
                    309:                }
                    310: #else
1.6.4.2   misho     311:                /* if is new shared memory session, fill file with zeros */
1.4       misho     312:                if (lseek(Sess->mem.fd, nSize - 1, SEEK_SET) == -1) {
1.1       misho     313:                        LOGERR;
1.4       misho     314:                        map_destroySession(Sess);
1.1       misho     315:                        return -1;
1.3       misho     316:                } else
1.4       misho     317:                        write(Sess->mem.fd, "", 1);
                    318:                lseek(Sess->mem.fd, 0, SEEK_SET);
1.5       misho     319: #endif
1.1       misho     320:        }
1.4       misho     321:        Sess->eom = nSize;
1.1       misho     322: 
1.4       misho     323:        return (int) Sess->zcpy;
1.1       misho     324: }
                    325: 
                    326: /*
1.6.4.3 ! misho     327:  * map_destroySession() - MMAP free shared resources
1.4       misho     328:  *
1.1       misho     329:  * @Sess = Session item
1.4       misho     330:  * return: none
1.1       misho     331: */
1.3       misho     332: void
1.4       misho     333: map_destroySession(ait_sess_t * __restrict Sess)
1.1       misho     334: {
1.3       misho     335:        char szSName[2][FILENAME_MAX];
1.1       misho     336: 
1.4       misho     337:        if (!Sess || sess_isAttached(Sess) || !*Sess->name)
1.1       misho     338:                return;
                    339: 
1.3       misho     340:        memset(szSName, 0, sizeof szSName);
1.4       misho     341:        snprintf(szSName[0], MAX_SEMNAME, "/%X.ANS", (u_int) Sess->key);
1.5       misho     342: #ifdef HAVE_SHM_UNLINK
                    343:        snprintf(szSName[1], FILENAME_MAX, "/%s-%x.ANM", Sess->name, (u_int) Sess->key);
                    344: #else
1.4       misho     345:        snprintf(szSName[1], FILENAME_MAX, "%s-%x.ANM", Sess->name, (u_int) Sess->key);
1.5       misho     346: #endif
1.1       misho     347: 
1.4       misho     348:        if (Sess->id.sid != SEM_FAILED) {
                    349:                sem_close(Sess->id.sid);
                    350:                sem_unlink(szSName[0]);
                    351:        }
                    352:        if (Sess->mem.fd > 2) {
                    353:                close(Sess->mem.fd);
1.5       misho     354: #ifdef HAVE_SHM_UNLINK
                    355:                shm_unlink(szSName[1]);
                    356: #else
1.4       misho     357:                unlink(szSName[1]);
1.5       misho     358: #endif
1.4       misho     359:        }
                    360:        unlink(Sess->name);
                    361:        memset(Sess->name, 0, sizeof Sess->name);
                    362:        Sess->eom ^= Sess->eom;
                    363:        Sess->key ^= Sess->key;
1.1       misho     364: }
                    365: 
                    366: /*
1.6.4.3 ! misho     367:  * ipc_createSession() - IPC Created session and allocated resources
1.4       misho     368:  *
                    369:  * @nSeed = Seed for securing key, if =-1 must add ready for use key
                    370:  * @nSize = Allocated shared memory size in bytes
1.1       misho     371:  * @Sess = Session item
1.4       misho     372:  * @... = If nSeed == -1 add ready for use key value
1.1       misho     373:  * return: 0 Ok successful, -1 error: not allocated resources
1.4       misho     374:  */
1.3       misho     375: int
1.4       misho     376: ipc_createSession(int nSeed, long nSize, ait_sess_t * __restrict Sess, ...)
1.1       misho     377: {
                    378:        union semun sems;
1.4       misho     379:        va_list lst;
1.1       misho     380: 
1.4       misho     381:        if (!Sess || !*Sess->name)
1.1       misho     382:                return -1;
                    383: 
1.4       misho     384:        if (nSeed != -1) {
                    385:                /* genkey */
                    386:                Sess->key = ftok(Sess->name, nSeed);
                    387:                if (Sess->key == -1) {
                    388:                        LOGERR;
                    389:                        return -1;
                    390:                }
                    391:        } else {
                    392:                /* get key from va_args */
                    393:                va_start(lst, Sess);
                    394:                Sess->key = va_arg(lst, key_t);
                    395:                va_end(lst);
1.1       misho     396:        }
                    397: 
1.3       misho     398:        /* create semaphore */
1.4       misho     399:        Sess->id.semid = semget(Sess->key, 1, MEM_MODE | IPC_CREAT);
                    400:        if (Sess->id.semid == -1) {
1.1       misho     401:                LOGERR;
1.4       misho     402:                ipc_destroySession(Sess);
1.1       misho     403:                return -1;
                    404:        }
1.3       misho     405:        /* if is new shared memory session, init sempahore with 1 */
1.4       misho     406:        if (!Sess->zcpy) {
1.1       misho     407:                sems.val = 1;
1.4       misho     408:                if (semctl(Sess->id.semid, 0, SETVAL, sems) == -1) {
1.1       misho     409:                        LOGERR;
1.4       misho     410:                        ipc_destroySession(Sess);
1.1       misho     411:                        return -1;
                    412:                }
                    413:        }
                    414: 
1.3       misho     415:        /* create shared memory object */
1.4       misho     416:        Sess->mem.shmid = shmget(Sess->key, nSize, MEM_MODE | IPC_CREAT);
                    417:        if (Sess->mem.shmid == -1) {
1.1       misho     418:                LOGERR;
1.4       misho     419:                ipc_destroySession(Sess);
1.1       misho     420:                return -1;
1.4       misho     421:        } else
                    422:                Sess->eom = nSize;
1.1       misho     423: 
1.4       misho     424:        return (int) Sess->zcpy;
1.1       misho     425: }
                    426: 
                    427: /*
1.6.4.3 ! misho     428:  * ipc_destroySession() - IPC free shared resources
1.4       misho     429:  *
1.1       misho     430:  * @Sess = Session item
1.4       misho     431:  * return: none
                    432:  */
1.3       misho     433: void
1.4       misho     434: ipc_destroySession(ait_sess_t * __restrict Sess)
1.1       misho     435: {
                    436:        union semun sems;
                    437:        struct shmid_ds ds;
                    438: 
1.4       misho     439:        if (!Sess || sess_isAttached(Sess))
1.1       misho     440:                return;
                    441: 
1.4       misho     442:        if (Sess->id.semid != -1)
                    443:                semctl(Sess->id.semid, 0, IPC_RMID, &sems);
                    444:        if (Sess->mem.shmid != -1)
                    445:                shmctl(Sess->mem.shmid, IPC_RMID, &ds);
                    446:        unlink(Sess->name);
                    447:        memset(Sess->name, 0, sizeof Sess->name);
                    448:        Sess->eom ^= Sess->eom;
                    449:        Sess->key ^= Sess->key;
1.1       misho     450: }
                    451: 
                    452: 
                    453: /*
1.6.4.3 ! misho     454:  * map_attachSession() - MMAP Attach to shared memory & return begin address
1.4       misho     455:  *
1.1       misho     456:  * @s = Session item
                    457:  * @procMem = Custom start address (optionl) *default must be 0*
                    458:  * return: NULL failed attach, !=NULL begin address of memory
1.4       misho     459:  */
                    460: void *
                    461: map_attachSession(ait_sess_t * __restrict s, void *procMem)
1.1       misho     462: {
                    463:        if (!s)
                    464:                return NULL;
                    465: 
                    466:        sync();
                    467: 
1.3       misho     468:        /* attach to memory */
1.1       misho     469:        s->addr = mmap(procMem, s->eom, PROT_READ | PROT_WRITE, MAP_SHARED, s->mem.fd, 0);
                    470:        if (s->addr == MAP_FAILED) {
                    471:                LOGERR;
                    472:                s->addr = NULL;
                    473:        }
                    474: 
                    475:        return s->addr;
                    476: }
                    477: 
                    478: /*
1.6.4.3 ! misho     479:  * map_detachSession() - MMAP Detach from shared memory
1.4       misho     480:  *
1.1       misho     481:  * @s = Session item
1.4       misho     482:  * return: none
                    483:  */
                    484: void
                    485: map_detachSession(ait_sess_t * __restrict s)
1.1       misho     486: {
                    487:        if (!s)
                    488:                return;
                    489: 
                    490:        msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
                    491: 
                    492:        if (s->addr && s->eom) {
                    493:                munmap(s->addr, s->eom);
                    494:                s->addr = NULL;
                    495:        }
                    496: }
                    497: 
                    498: /*
1.6.4.3 ! misho     499:  * ipc_attachSession() - IPC Attach to shared memory & return begin address
1.4       misho     500:  *
1.1       misho     501:  * @s = Session item
                    502:  * @procMem = Custom start address (optionl) *default must be 0*
                    503:  * return: NULL failed attach, !=NULL begin address of memory
1.4       misho     504:  */
                    505: void *
                    506: ipc_attachSession(ait_sess_t * __restrict s, void *procMem)
1.1       misho     507: {
                    508:        if (!s)
                    509:                return NULL;
                    510: 
                    511:        s->addr = shmat(s->mem.shmid, procMem, 0);
                    512:        if (s->addr == (void*) -1) {
                    513:                LOGERR;
                    514:                s->addr = NULL;
                    515:        }
                    516: 
                    517:        return s->addr;
                    518: }
                    519: 
                    520: /*
1.6.4.3 ! misho     521:  * ipc_detachSession() - IPC Detach from shared memory
1.4       misho     522:  *
1.1       misho     523:  * @s = Session item
1.4       misho     524:  * return: none
                    525:  */
                    526: void
                    527: ipc_detachSession(ait_sess_t * __restrict s)
1.1       misho     528: {
                    529:        if (!s)
                    530:                return;
                    531: 
                    532:        if (s->addr) {
                    533:                shmdt(s->addr);
                    534:                s->addr = NULL;
                    535:        }
                    536: }
                    537: 
1.2       misho     538: /*
1.6.4.3 ! misho     539:  * sess_isAttached() - Check for attached shared memory
1.4       misho     540:  *
1.2       misho     541:  * @s = Session item
                    542:  * return: -1 null session item, 0 not attached, 1 attached memory
1.4       misho     543:  */
1.3       misho     544: inline int
1.4       misho     545: sess_isAttached(ait_sess_t * __restrict s)
1.2       misho     546: {
                    547:        if (!s)
                    548:                return -1;
                    549: 
                    550:        return (s->addr ? 1 : 0);
                    551: }
                    552: 
1.1       misho     553: 
                    554: /*
1.6.4.3 ! misho     555:  * map_notSemaphore() - MMAP negative block if semaphore isn`t signaled
1.4       misho     556:  *
1.1       misho     557:  * @s = Session item
1.4       misho     558:  * return: none
                    559:  */
                    560: void
                    561: map_notSemaphore(ait_sess_t * __restrict s)
1.1       misho     562: {
                    563:        int i = -1;
                    564: 
                    565:        if (!s)
                    566:                return;
                    567: 
                    568:        sem_getvalue(s->id.sid, &i);
1.4       misho     569:        while (i > 0) {
1.1       misho     570:                sem_wait(s->id.sid);
1.4       misho     571:                i--;
                    572:        }
1.1       misho     573: }
                    574: 
                    575: /*
1.6.4.3 ! misho     576:  * map_isSemaphoreOK() - MMAP Check semaphore
1.4       misho     577:  *
1.1       misho     578:  * @s = Session item
                    579:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
1.4       misho     580:  */
                    581: int
                    582: map_isSemaphoreOK(ait_sess_t * __restrict s)
1.1       misho     583: {
                    584:        int val = -1;
                    585: 
                    586:        if (!s)
                    587:                return -1;
                    588: 
                    589:        sem_getvalue(s->id.sid, &val);
1.4       misho     590:        return (val ? 0 : 1);
1.1       misho     591: }
                    592: 
                    593: /*
1.6.4.3 ! misho     594:  * map_incSemaphore() - MMAP unblock semaphore, increment semaphore
1.4       misho     595:  *
1.1       misho     596:  * @s = Session item
                    597:  * return: 0 Ok, -1 error: can`t increment 
1.4       misho     598:  */
                    599: int
                    600: map_incSemaphore(ait_sess_t * __restrict s)
1.1       misho     601: {
                    602:        if (!s)
                    603:                return -1;
                    604: 
                    605:        return sem_post(s->id.sid);
                    606: }
                    607: 
                    608: /*
1.6.4.3 ! misho     609:  * map_decSemaphore() - MMAP block semaphore, decrement semaphore
1.4       misho     610:  *
1.1       misho     611:  * @s = Session item
                    612:  * return: 0 Ok, -1 error: can`t decrement 
1.4       misho     613:  */
                    614: int
                    615: map_decSemaphore(ait_sess_t * __restrict s)
1.1       misho     616: {
                    617:        if (!s)
                    618:                return -1;
                    619: 
                    620:        return sem_wait(s->id.sid);
                    621: }
                    622: 
                    623: /*
1.6.4.3 ! misho     624:  * ipc_notSemaphore() - IPC negative block if semaphore isn`t signaled
1.4       misho     625:  *
1.1       misho     626:  * @s = Session item
1.4       misho     627:  * return: none
                    628:  */
                    629: void
                    630: ipc_notSemaphore(ait_sess_t * __restrict s)
1.1       misho     631: {
                    632:        struct sembuf sb = { 0, 0, 0 };
                    633: 
                    634:        if (s)
                    635:                semop(s->id.semid, &sb, 1);
                    636: }
                    637: 
                    638: /*
1.6.4.3 ! misho     639:  * ipc_isSemaphoreOK() - IPC Check semaphore
1.4       misho     640:  *
1.1       misho     641:  * @s = Session item
                    642:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
1.4       misho     643:  */
                    644: int
                    645: ipc_isSemaphoreOK(ait_sess_t * __restrict s)
1.1       misho     646: {
                    647:        struct sembuf sb = { 0, 0, IPC_NOWAIT };
                    648: 
                    649:        if (!s)
                    650:                return -1;
                    651: 
                    652:        return semop(s->id.semid, &sb, 1) + 1;
                    653: }
                    654: 
                    655: /*
1.6.4.3 ! misho     656:  * ipc_incSemaphore() - IPC unblock semaphore, increment semaphore
1.4       misho     657:  *
1.1       misho     658:  * @s = Session item
                    659:  * return: 0 Ok, -1 error: can`t increment 
1.4       misho     660:  */
                    661: int
                    662: ipc_incSemaphore(ait_sess_t * __restrict s)
1.1       misho     663: {
                    664:        struct sembuf sb = { 0, 1, 0 };
                    665: 
                    666:        if (!s)
                    667:                return -1;
                    668: 
                    669:        return semop(s->id.semid, &sb, 1);
                    670: }
                    671: 
                    672: /*
1.6.4.3 ! misho     673:  * ipc_decSemaphore() - IPC block semaphore, decrement semaphore
1.4       misho     674:  *
1.1       misho     675:  * @s = Session item
                    676:  * return: 0 Ok, -1 error: can`t decrement 
1.4       misho     677:  */
                    678: int
                    679: ipc_decSemaphore(ait_sess_t * __restrict s)
1.1       misho     680: {
                    681:        struct sembuf sb = { 0, -1, 0 };
                    682: 
                    683:        if (!s)
                    684:                return -1;
                    685: 
                    686:        return semop(s->id.semid, &sb, 1);
                    687: }

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