Annotation of libaitio/src/sess.c, revision 1.5.8.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.5.8.1 ! misho       6: * $Id: sess.c,v 1.5 2014/02/08 22:06:17 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: 
1.5       misho      15: Copyright 2004 - 2014
1.2       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: */
                     46: #include "global.h"
                     47: 
                     48: 
                     49: /*
                     50:  * sess_initSession() - Initializing session structure, 
                     51:  *                     if session file not exists creating with specified tech
                     52:  *
                     53:  * @id = Technology using in session. SHARED_IPC IPC tech or SHARED_MAP BSD MemoryMap tech
                     54:  * @csFName = Session filename for build key and identified
                     55:  * @Sess = Session item, if =NULL allocate memory for session after use must be free!
                     56:  * return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded
                     57: */
                     58: int
1.3       misho      59: sess_initSession(int id, const char *csFName, sess_t ** __restrict Sess)
1.2       misho      60: {
                     61:        int h, ret = 0;
                     62:        char szStr[STRSIZ];
                     63: 
                     64:        if (!csFName) {
                     65:                io_SetErr(EINVAL, "Filename is NULL");
                     66:                return -1;
                     67:        }
                     68:        if (id < SHARED_UNKNOWN || id > SHARED_MAP) {
                     69:                io_SetErr(EPROTONOSUPPORT, "Session type not supported");
                     70:                return -1;
                     71:        }
                     72: 
                     73:        if (!*Sess) {
1.3       misho      74:                *Sess = e_malloc(sizeof(sess_t));
1.2       misho      75:                if (!*Sess) {
                     76:                        LOGERR;
                     77:                        return -1;
                     78:                }
                     79:        }
1.3       misho      80:        memset(*Sess, 0, sizeof(sess_t));
                     81:        strlcpy((*Sess)->sess_name, csFName, sizeof (*Sess)->sess_name);
1.2       misho      82: 
1.3       misho      83:        h = open((*Sess)->sess_name, O_WRONLY | O_CREAT | O_EXCL, SESS_MEM_MODE);
1.2       misho      84:        if (h == -1) {
                     85:                if (errno != EEXIST) {
                     86:                        LOGERR;
                     87:                        e_free(*Sess);
                     88:                        return -1;
                     89:                }
                     90:                /* If key file exist, session already connected */
1.3       misho      91:                h = open((*Sess)->sess_name, O_RDONLY);
1.2       misho      92:                if (h == -1) {
                     93:                        LOGERR;
                     94:                        e_free(*Sess);
                     95:                        return -1;
                     96:                }
                     97:                ret = read(h, szStr, sizeof szStr);
                     98:                if (ret == -1) {
                     99:                        LOGERR;
                    100:                        close(h);
                    101:                        e_free(*Sess);
                    102:                        return -1;
                    103:                }
                    104:                if (!strncmp(szStr, "IPC@", 4) && id == SHARED_IPC) {
                    105:                        ret = 1;
                    106: 
                    107:                        (*Sess)->sess.create = (int (*)(int, long, void*, ...)) ipc_createSession;
                    108:                        (*Sess)->sess.destroy = (void (*)(void*)) ipc_destroySession;
                    109:                        (*Sess)->sess.attach = (void* (*)(void*, void*)) ipc_attachSession;
                    110:                        (*Sess)->sess.detach = (void (*)(void*)) ipc_detachSession;
                    111:                        (*Sess)->sess.notSem = (void (*)(void*)) ipc_notSemaphore;
                    112:                        (*Sess)->sess.isSemOK = (int (*)(void*)) ipc_isSemaphoreOK;
                    113:                        (*Sess)->sess.incSem = (int (*)(void*)) ipc_incSemaphore;
                    114:                        (*Sess)->sess.decSem = (int (*)(void*)) ipc_decSemaphore;
                    115:                } else if (!strncmp(szStr, "MAP@", 4) && id == SHARED_MAP) {
                    116:                        ret = 1;
                    117: 
                    118:                        (*Sess)->sess.create = (int (*)(int, long, void*, ...)) map_createSession;
                    119:                        (*Sess)->sess.destroy = (void (*)(void*)) map_destroySession;
                    120:                        (*Sess)->sess.attach = (void* (*)(void*, void*)) map_attachSession;
                    121:                        (*Sess)->sess.detach = (void (*)(void*)) map_detachSession;
                    122:                        (*Sess)->sess.notSem = (void (*)(void*)) map_notSemaphore;
                    123:                        (*Sess)->sess.isSemOK = (int (*)(void*)) map_isSemaphoreOK;
                    124:                        (*Sess)->sess.incSem = (int (*)(void*)) map_incSemaphore;
                    125:                        (*Sess)->sess.decSem = (int (*)(void*)) map_decSemaphore;
                    126:                } else {
                    127:                        io_SetErr(EPROTONOSUPPORT, 
                    128:                                        "Session type not supported or wrong session type");
                    129:                        close(h);
                    130:                        e_free(*Sess);
                    131:                        return -1;
                    132:                }
                    133:                /* key found */
                    134:        } else {
                    135:                /* Build new key & new session */
                    136:                if (id == SHARED_IPC) {
                    137:                        strlcpy(szStr, "IPC@", sizeof szStr);
                    138: 
                    139:                        (*Sess)->sess.create = (int (*)(int, long, void*, ...)) ipc_createSession;
                    140:                        (*Sess)->sess.destroy = (void (*)(void*)) ipc_destroySession;
                    141:                        (*Sess)->sess.attach = (void* (*)(void*, void*)) ipc_attachSession;
                    142:                        (*Sess)->sess.detach = (void (*)(void*)) ipc_detachSession;
                    143:                        (*Sess)->sess.notSem = (void (*)(void*)) ipc_notSemaphore;
                    144:                        (*Sess)->sess.isSemOK = (int (*)(void*)) ipc_isSemaphoreOK;
                    145:                        (*Sess)->sess.incSem = (int (*)(void*)) ipc_incSemaphore;
                    146:                        (*Sess)->sess.decSem = (int (*)(void*)) ipc_decSemaphore;
                    147:                } else if (id == SHARED_MAP) {
                    148:                        strlcpy(szStr, "MAP@", sizeof szStr);
                    149: 
                    150:                        (*Sess)->sess.create = (int (*)(int, long, void*, ...)) map_createSession;
                    151:                        (*Sess)->sess.destroy = (void (*)(void*)) map_destroySession;
                    152:                        (*Sess)->sess.attach = (void* (*)(void*, void*)) map_attachSession;
                    153:                        (*Sess)->sess.detach = (void (*)(void*)) map_detachSession;
                    154:                        (*Sess)->sess.notSem = (void (*)(void*)) map_notSemaphore;
                    155:                        (*Sess)->sess.isSemOK = (int (*)(void*)) map_isSemaphoreOK;
                    156:                        (*Sess)->sess.incSem = (int (*)(void*)) map_incSemaphore;
                    157:                        (*Sess)->sess.decSem = (int (*)(void*)) map_decSemaphore;
                    158:                } else {
                    159:                        io_SetErr(EINVAL, "Session type must be specified");
                    160:                        close(h);
                    161:                        unlink(csFName);
                    162:                        e_free(*Sess);
                    163:                        return -1;
                    164:                }
                    165:                strlcat(szStr, "ELWIX_Session ("PACKAGE_STRING")\n", sizeof szStr);
                    166:                write(h, szStr, strlen(szStr));
                    167: 
                    168:                ret = 0;
                    169:                /* new key created */
                    170:        }
                    171:        close(h);
                    172: 
1.3       misho     173:        (*Sess)->sess_type = id;
                    174:        (*Sess)->sess_zcpy = (char) ret;
1.2       misho     175:        return ret;
                    176: }
                    177: 
                    178: /*
                    179:  * sess_freeSession() - Free allocated memory for session item and delete session file if present name
                    180:  *
                    181:  * @Sess = Session item
                    182:  * return: none
                    183: */
                    184: void
1.3       misho     185: sess_freeSession(sess_t ** __restrict Sess)
1.2       misho     186: {
                    187:        if (!Sess || !(*Sess))
                    188:                return;
                    189: 
1.3       misho     190:        if ((*Sess)->sess_addr)
1.2       misho     191:                DETACH_MEMORY(*Sess);
                    192: 
                    193:        /*
                    194:        memset(&(*Sess)->sess, 0, sizeof (*Sess)->sess);
                    195: 
                    196:        (*Sess)->type = SHARED_UNKNOWN;
                    197:        */
                    198: 
                    199:        e_free(*Sess);
                    200:        *Sess = NULL;
                    201: }
                    202: 
                    203: 
                    204: /*
                    205:  * map_createSession() - MMAP Created session and allocated resources
                    206:  *
1.3       misho     207:  * @nSeed = Seed for securing key, if =SESS_OPT_SEED must add ready for use key
1.2       misho     208:  * @nSize = Allocated shared memory size in bytes
                    209:  * @Sess = Session item
                    210:  * @... = If nSeed == -1 add ready for use key value
                    211:  * return: 0 Ok successful, -1 error: not allocated resources
                    212: */
                    213: int
1.3       misho     214: map_createSession(int nSeed, long nSize, sess_t * __restrict Sess, ...)
1.2       misho     215: {
                    216:        char szSName[2][FILENAME_MAX];
                    217:        va_list lst;
1.5.8.1 ! misho     218:        struct stat st;
1.2       misho     219: 
1.3       misho     220:        if (!Sess || !*Sess->sess_name)
1.2       misho     221:                return -1;
                    222: 
1.3       misho     223:        if (nSeed != SESS_OPT_SEED) {
1.2       misho     224:                /* genkey */
1.3       misho     225:                Sess->sess_key = ftok(Sess->sess_name, nSeed);
                    226:                if (Sess->sess_key == -1) {
1.2       misho     227:                        LOGERR;
                    228:                        return -1;
                    229:                }
                    230:        } else {
                    231:                /* get key from va_args */
                    232:                va_start(lst, Sess);
1.3       misho     233:                Sess->sess_key = va_arg(lst, key_t);
1.2       misho     234:                va_end(lst);
                    235:        }
                    236: 
                    237:        /* build semaphore & shared memory name */
                    238:        memset(szSName, 0, sizeof szSName);
1.3       misho     239:        snprintf(szSName[0], SEMNAME_MAX, "/%X.ANS", (u_int) Sess->sess_key);
1.2       misho     240: #ifdef HAVE_SHM_OPEN
1.5.8.1 ! misho     241:        snprintf(szSName[1], FILENAME_MAX, "/%X.ANM", (u_int) Sess->sess_key);
1.2       misho     242: #else
1.3       misho     243:        snprintf(szSName[1], FILENAME_MAX, "%s-%x.ANM", Sess->sess_name, (u_int) Sess->sess_key);
1.2       misho     244: #endif
                    245: 
                    246:        /* create semaphore & add 1 */
1.5.8.1 ! misho     247:        Sess->sess_id.sid = sem_open(szSName[0], O_CREAT, SESS_MEM_MODE, 0);
1.3       misho     248:        if (Sess->sess_id.sid == SEM_FAILED) {
1.2       misho     249:                LOGERR;
                    250:                map_destroySession(Sess);
                    251:                return -1;
                    252:        }
                    253:        /* if is new shared memory session, init sempahore with 1 */
1.3       misho     254:        if (!Sess->sess_zcpy)
                    255:                sem_post(Sess->sess_id.sid);
1.2       misho     256: 
                    257:        /* create file for shared memory storage */
                    258: #ifdef HAVE_SHM_OPEN
1.3       misho     259:        Sess->sess_mem.fd = shm_open(szSName[1], O_RDWR | O_CREAT, SESS_MEM_MODE);
1.2       misho     260: #else
1.3       misho     261:        Sess->sess_mem.fd = open(szSName[1], O_RDWR | O_CREAT, SESS_MEM_MODE);
1.2       misho     262: #endif
1.3       misho     263:        if (Sess->sess_mem.fd == -1) {
1.2       misho     264:                LOGERR;
                    265:                map_destroySession(Sess);
                    266:                return -1;
                    267:        }
1.5.8.1 ! misho     268:        if (fstat(Sess->sess_mem.fd, &st) == -1) {
        !           269:                LOGERR;
        !           270:                map_destroySession(Sess);
        !           271:                return -1;
        !           272:        }
        !           273:        if (st.st_size < nSize) {
1.3       misho     274: /* #ifdef HAVE_SHM_OPEN */
                    275:                if (ftruncate(Sess->sess_mem.fd, nSize) == -1) {
1.2       misho     276:                        LOGERR;
                    277:                        map_destroySession(Sess);
                    278:                        return -1;
                    279:                }
1.3       misho     280: /* #else */
1.2       misho     281:                /* if is new shared memory session, fill file with zeros */
1.3       misho     282:                /*
                    283:                if (lseek(Sess->sess_mem.fd, nSize - 1, SEEK_SET) == -1) {
1.2       misho     284:                        LOGERR;
                    285:                        map_destroySession(Sess);
                    286:                        return -1;
                    287:                } else
1.3       misho     288:                        write(Sess->sess_mem.fd, "", 1);
                    289:                lseek(Sess->sess_mem.fd, 0, SEEK_SET);
1.2       misho     290: #endif
1.3       misho     291: */
1.2       misho     292:        }
1.3       misho     293:        Sess->sess_size = nSize;
1.2       misho     294: 
1.3       misho     295:        return (int) Sess->sess_zcpy;
1.2       misho     296: }
                    297: 
                    298: /*
                    299:  * map_destroySession() - MMAP free shared resources
                    300:  *
                    301:  * @Sess = Session item
                    302:  * return: none
                    303: */
                    304: void
1.3       misho     305: map_destroySession(sess_t * __restrict Sess)
1.2       misho     306: {
                    307:        char szSName[2][FILENAME_MAX];
                    308: 
1.3       misho     309:        if (!Sess || sess_isAttached(Sess) || !*Sess->sess_name)
1.2       misho     310:                return;
                    311: 
                    312:        memset(szSName, 0, sizeof szSName);
1.3       misho     313:        snprintf(szSName[0], SEMNAME_MAX, "/%X.ANS", (u_int) Sess->sess_key);
1.2       misho     314: #ifdef HAVE_SHM_UNLINK
1.3       misho     315:        snprintf(szSName[1], FILENAME_MAX, "/%s-%x.ANM", Sess->sess_name, (u_int) Sess->sess_key);
1.2       misho     316: #else
1.3       misho     317:        snprintf(szSName[1], FILENAME_MAX, "%s-%x.ANM", Sess->sess_name, (u_int) Sess->sess_key);
1.2       misho     318: #endif
                    319: 
1.3       misho     320:        if (Sess->sess_id.sid != SEM_FAILED) {
                    321:                sem_close(Sess->sess_id.sid);
1.2       misho     322:                sem_unlink(szSName[0]);
                    323:        }
1.3       misho     324:        if (Sess->sess_mem.fd > 2) {
                    325:                close(Sess->sess_mem.fd);
1.2       misho     326: #ifdef HAVE_SHM_UNLINK
                    327:                shm_unlink(szSName[1]);
                    328: #else
                    329:                unlink(szSName[1]);
                    330: #endif
                    331:        }
1.3       misho     332:        unlink(Sess->sess_name);
                    333:        memset(Sess->sess_name, 0, sizeof Sess->sess_name);
                    334:        Sess->sess_size ^= Sess->sess_size;
                    335:        Sess->sess_key ^= Sess->sess_key;
1.2       misho     336: }
                    337: 
                    338: /*
                    339:  * ipc_createSession() - IPC Created session and allocated resources
                    340:  *
1.3       misho     341:  * @nSeed = Seed for securing key, if =SESS_OPT_SEED must add ready for use key
1.2       misho     342:  * @nSize = Allocated shared memory size in bytes
                    343:  * @Sess = Session item
                    344:  * @... = If nSeed == -1 add ready for use key value
                    345:  * return: 0 Ok successful, -1 error: not allocated resources
                    346:  */
                    347: int
1.3       misho     348: ipc_createSession(int nSeed, long nSize, sess_t * __restrict Sess, ...)
1.2       misho     349: {
                    350:        union semun sems;
                    351:        va_list lst;
                    352: 
1.3       misho     353:        if (!Sess || !*Sess->sess_name)
1.2       misho     354:                return -1;
                    355: 
                    356:        if (nSeed != -1) {
                    357:                /* genkey */
1.3       misho     358:                Sess->sess_key = ftok(Sess->sess_name, nSeed);
                    359:                if (Sess->sess_key == -1) {
1.2       misho     360:                        LOGERR;
                    361:                        return -1;
                    362:                }
                    363:        } else {
                    364:                /* get key from va_args */
                    365:                va_start(lst, Sess);
1.3       misho     366:                Sess->sess_key = va_arg(lst, key_t);
1.2       misho     367:                va_end(lst);
                    368:        }
                    369: 
                    370:        /* create semaphore */
1.3       misho     371:        Sess->sess_id.semid = semget(Sess->sess_key, 1, SESS_MEM_MODE | IPC_CREAT);
                    372:        if (Sess->sess_id.semid == -1) {
1.2       misho     373:                LOGERR;
                    374:                ipc_destroySession(Sess);
                    375:                return -1;
                    376:        }
                    377:        /* if is new shared memory session, init sempahore with 1 */
1.3       misho     378:        if (!Sess->sess_zcpy) {
1.2       misho     379:                sems.val = 1;
1.3       misho     380:                if (semctl(Sess->sess_id.semid, 0, SETVAL, sems) == -1) {
1.2       misho     381:                        LOGERR;
                    382:                        ipc_destroySession(Sess);
                    383:                        return -1;
                    384:                }
                    385:        }
                    386: 
                    387:        /* create shared memory object */
1.3       misho     388:        Sess->sess_mem.shmid = shmget(Sess->sess_key, nSize, SESS_MEM_MODE | IPC_CREAT);
                    389:        if (Sess->sess_mem.shmid == -1) {
1.2       misho     390:                LOGERR;
                    391:                ipc_destroySession(Sess);
                    392:                return -1;
                    393:        } else
1.3       misho     394:                Sess->sess_size = nSize;
1.2       misho     395: 
1.3       misho     396:        return (int) Sess->sess_zcpy;
1.2       misho     397: }
                    398: 
                    399: /*
                    400:  * ipc_destroySession() - IPC free shared resources
                    401:  *
                    402:  * @Sess = Session item
                    403:  * return: none
                    404:  */
                    405: void
1.3       misho     406: ipc_destroySession(sess_t * __restrict Sess)
1.2       misho     407: {
                    408:        union semun sems;
                    409:        struct shmid_ds ds;
                    410: 
                    411:        if (!Sess || sess_isAttached(Sess))
                    412:                return;
                    413: 
1.3       misho     414:        if (Sess->sess_id.semid != -1)
                    415:                semctl(Sess->sess_id.semid, 0, IPC_RMID, &sems);
                    416:        if (Sess->sess_mem.shmid != -1)
                    417:                shmctl(Sess->sess_mem.shmid, IPC_RMID, &ds);
                    418:        unlink(Sess->sess_name);
                    419:        memset(Sess->sess_name, 0, sizeof Sess->sess_name);
                    420:        Sess->sess_size ^= Sess->sess_size;
                    421:        Sess->sess_key ^= Sess->sess_key;
1.2       misho     422: }
                    423: 
                    424: 
                    425: /*
                    426:  * map_attachSession() - MMAP Attach to shared memory & return begin address
                    427:  *
                    428:  * @s = Session item
                    429:  * @procMem = Custom start address (optionl) *default must be 0*
                    430:  * return: NULL failed attach, !=NULL begin address of memory
                    431:  */
                    432: void *
1.3       misho     433: map_attachSession(sess_t * __restrict s, void *procMem)
1.2       misho     434: {
                    435:        if (!s)
                    436:                return NULL;
                    437: 
                    438:        sync();
                    439: 
                    440:        /* attach to memory */
1.3       misho     441:        s->sess_addr = mmap(procMem, s->sess_size, PROT_READ | PROT_WRITE, MAP_SHARED, s->sess_mem.fd, 0);
                    442:        if (s->sess_addr == MAP_FAILED) {
1.2       misho     443:                LOGERR;
1.3       misho     444:                s->sess_addr = NULL;
1.2       misho     445:        }
                    446: 
1.3       misho     447:        return s->sess_addr;
1.2       misho     448: }
                    449: 
                    450: /*
                    451:  * map_detachSession() - MMAP Detach from shared memory
                    452:  *
                    453:  * @s = Session item
                    454:  * return: none
                    455:  */
                    456: void
1.3       misho     457: map_detachSession(sess_t * __restrict s)
1.2       misho     458: {
                    459:        if (!s)
                    460:                return;
                    461: 
1.3       misho     462:        msync(s->sess_addr, 0, MS_SYNC | MS_INVALIDATE);
1.2       misho     463: 
1.3       misho     464:        if (s->sess_size) {
                    465:                munmap(s->sess_addr, s->sess_size);
                    466:                s->sess_addr = NULL;
1.2       misho     467:        }
                    468: }
                    469: 
                    470: /*
                    471:  * ipc_attachSession() - IPC Attach to shared memory & return begin address
                    472:  *
                    473:  * @s = Session item
                    474:  * @procMem = Custom start address (optionl) *default must be 0*
                    475:  * return: NULL failed attach, !=NULL begin address of memory
                    476:  */
                    477: void *
1.3       misho     478: ipc_attachSession(sess_t * __restrict s, void *procMem)
1.2       misho     479: {
                    480:        if (!s)
                    481:                return NULL;
                    482: 
1.3       misho     483:        s->sess_addr = shmat(s->sess_mem.shmid, procMem, 0);
                    484:        if (s->sess_addr == (void*) -1) {
1.2       misho     485:                LOGERR;
1.3       misho     486:                s->sess_addr = NULL;
1.2       misho     487:        }
                    488: 
1.3       misho     489:        return s->sess_addr;
1.2       misho     490: }
                    491: 
                    492: /*
                    493:  * ipc_detachSession() - IPC Detach from shared memory
                    494:  *
                    495:  * @s = Session item
                    496:  * return: none
                    497:  */
                    498: void
1.3       misho     499: ipc_detachSession(sess_t * __restrict s)
1.2       misho     500: {
                    501:        if (!s)
                    502:                return;
                    503: 
1.3       misho     504:        if (s->sess_size) {
                    505:                shmdt(s->sess_addr);
                    506:                s->sess_addr = NULL;
1.2       misho     507:        }
                    508: }
                    509: 
                    510: /*
                    511:  * sess_isAttached() - Check for attached shared memory
                    512:  *
                    513:  * @s = Session item
                    514:  * return: -1 null session item, 0 not attached, 1 attached memory
                    515:  */
1.4       misho     516: int
1.3       misho     517: sess_isAttached(sess_t * __restrict s)
1.2       misho     518: {
                    519:        if (!s)
                    520:                return -1;
                    521: 
1.3       misho     522:        return !!s->sess_addr;
1.2       misho     523: }
                    524: 
                    525: 
                    526: /*
                    527:  * map_notSemaphore() - MMAP negative block if semaphore isn`t signaled
                    528:  *
                    529:  * @s = Session item
                    530:  * return: none
                    531:  */
                    532: void
1.3       misho     533: map_notSemaphore(sess_t * __restrict s)
1.2       misho     534: {
                    535:        int i = -1;
                    536: 
                    537:        if (!s)
                    538:                return;
                    539: 
1.3       misho     540:        sem_getvalue(s->sess_id.sid, &i);
                    541:        for (; i; i--)
                    542:                sem_wait(s->sess_id.sid);
1.2       misho     543: }
                    544: 
                    545: /*
                    546:  * map_isSemaphoreOK() - MMAP Check semaphore
                    547:  *
                    548:  * @s = Session item
                    549:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
                    550:  */
                    551: int
1.3       misho     552: map_isSemaphoreOK(sess_t * __restrict s)
1.2       misho     553: {
                    554:        int val = -1;
                    555: 
                    556:        if (!s)
                    557:                return -1;
                    558: 
1.3       misho     559:        return sem_getvalue(s->sess_id.sid, &val) + 1;
1.2       misho     560: }
                    561: 
                    562: /*
                    563:  * map_incSemaphore() - MMAP unblock semaphore, increment semaphore
                    564:  *
                    565:  * @s = Session item
                    566:  * return: 0 Ok, -1 error: can`t increment 
                    567:  */
                    568: int
1.3       misho     569: map_incSemaphore(sess_t * __restrict s)
1.2       misho     570: {
                    571:        if (!s)
                    572:                return -1;
                    573: 
1.3       misho     574:        return sem_post(s->sess_id.sid);
1.2       misho     575: }
                    576: 
                    577: /*
                    578:  * map_decSemaphore() - MMAP block semaphore, decrement semaphore
                    579:  *
                    580:  * @s = Session item
                    581:  * return: 0 Ok, -1 error: can`t decrement 
                    582:  */
                    583: int
1.3       misho     584: map_decSemaphore(sess_t * __restrict s)
1.2       misho     585: {
                    586:        if (!s)
                    587:                return -1;
                    588: 
1.3       misho     589:        return sem_wait(s->sess_id.sid);
1.2       misho     590: }
                    591: 
                    592: /*
                    593:  * ipc_notSemaphore() - IPC negative block if semaphore isn`t signaled
                    594:  *
                    595:  * @s = Session item
                    596:  * return: none
                    597:  */
                    598: void
1.3       misho     599: ipc_notSemaphore(sess_t * __restrict s)
1.2       misho     600: {
                    601:        struct sembuf sb = { 0, 0, 0 };
                    602: 
                    603:        if (s)
1.3       misho     604:                semop(s->sess_id.semid, &sb, 1);
1.2       misho     605: }
                    606: 
                    607: /*
                    608:  * ipc_isSemaphoreOK() - IPC Check semaphore
                    609:  *
                    610:  * @s = Session item
                    611:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
                    612:  */
                    613: int
1.3       misho     614: ipc_isSemaphoreOK(sess_t * __restrict s)
1.2       misho     615: {
                    616:        struct sembuf sb = { 0, 0, IPC_NOWAIT };
                    617: 
                    618:        if (!s)
                    619:                return -1;
                    620: 
1.3       misho     621:        return semop(s->sess_id.semid, &sb, 1) + 1;
1.2       misho     622: }
                    623: 
                    624: /*
                    625:  * ipc_incSemaphore() - IPC unblock semaphore, increment semaphore
                    626:  *
                    627:  * @s = Session item
                    628:  * return: 0 Ok, -1 error: can`t increment 
                    629:  */
                    630: int
1.3       misho     631: ipc_incSemaphore(sess_t * __restrict s)
1.2       misho     632: {
                    633:        struct sembuf sb = { 0, 1, 0 };
                    634: 
                    635:        if (!s)
                    636:                return -1;
                    637: 
1.3       misho     638:        return semop(s->sess_id.semid, &sb, 1);
1.2       misho     639: }
                    640: 
                    641: /*
                    642:  * ipc_decSemaphore() - IPC block semaphore, decrement semaphore
                    643:  *
                    644:  * @s = Session item
                    645:  * return: 0 Ok, -1 error: can`t decrement 
                    646:  */
                    647: int
1.3       misho     648: ipc_decSemaphore(sess_t * __restrict s)
1.2       misho     649: {
                    650:        struct sembuf sb = { 0, -1, 0 };
                    651: 
                    652:        if (!s)
                    653:                return -1;
                    654: 
1.3       misho     655:        return semop(s->sess_id.semid, &sb, 1);
1.2       misho     656: }
                    657: 
                    658: /* --- A/V management --- */
                    659: 
                    660: /*
                    661:  * sess_GetValues() - Get all values from session shared memory
                    662:  *
                    663:  * @s = Session item
1.3       misho     664:  * @Vals = Return array strings
                    665:  * return: -1 error: in parameter, !=-1 count of returned strings in Vals 
                    666:  *             (must call ait_freeVars() after use!)
1.2       misho     667:  */
                    668: int
1.3       misho     669: sess_GetValues(sess_t * __restrict s, array_t ** __restrict Vals)
1.2       misho     670: {
                    671:        register int i;
1.3       misho     672:        char *Shared = NULL;
1.2       misho     673:        char *peer, *p_brk;
1.3       misho     674:        ait_val_t *v;
1.2       misho     675: 
1.3       misho     676:        if (!s || !Vals)
1.2       misho     677:                return -1;
1.3       misho     678:        *Vals = ait_allocVars(0);
                    679:        if (!*Vals) {
                    680:                io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.2       misho     681:                return -1;
1.3       misho     682:        }
1.2       misho     683: 
                    684:        /* allocated memory & mirrored shared memory into this */
1.3       misho     685:        Shared = e_malloc(s->sess_size);
1.2       misho     686:        if (!Shared) {
                    687:                LOGERR;
1.3       misho     688:                ait_freeVars(Vals);
1.2       misho     689:                return -1;
1.3       misho     690:        }
                    691: 
                    692:        DEC_SEM(s);
                    693:        memcpy(Shared, s->sess_addr, s->sess_size);
                    694:        INC_SEM(s);
1.2       misho     695: 
                    696:        for (i = 0, peer = strtok_r(Shared, SESS_MEM_DELIM"\r\n", &p_brk); peer; 
                    697:                        peer = strtok_r(NULL, SESS_MEM_DELIM"\r\n", &p_brk)) {
                    698:                if (!strchr(peer, '='))
                    699:                        continue;
                    700:                else
                    701:                        i++;
                    702: 
1.3       misho     703:                if (!(v = ait_allocVar())) {
                    704:                        io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.2       misho     705:                        e_free(Shared);
1.3       misho     706:                        ait_freeVars(Vals);
1.2       misho     707:                        return -1;
                    708:                } else
1.3       misho     709:                        AIT_SET_STR(v, peer);
1.2       misho     710: 
1.3       misho     711:                array_Push(*Vals, v, 0);
1.2       misho     712:        }
                    713: 
                    714:        e_free(Shared);
1.3       misho     715:        ait_hashKeyVars(*Vals);
1.2       misho     716:        return i;
                    717: }
                    718: 
                    719: /*
                    720:  * sess_GetValue() - Get value from session shared memory from attribute
                    721:  *
                    722:  * @s = Session item
                    723:  * @csAttr = Attribute for search
1.3       misho     724:  * @v = Return string value and after use must call ait_freeVar(), also may be =NULL
1.2       misho     725:  * return: 0 not found, -1 error: in parameter, >0 get position, 
                    726:  *     if define item merged with IS_DEF
                    727:  */
                    728: int
1.3       misho     729: sess_GetValue(sess_t * __restrict s, const char *csAttr, ait_val_t ** __restrict v)
1.2       misho     730: {
                    731:        register int i;
1.3       misho     732:        int def = 0;
1.2       misho     733:        char *Shared = NULL;
                    734:        char *peer, *p_brk, *a_brk, *attr, *val;
                    735: 
                    736:        if (!s || !csAttr || !*csAttr)
                    737:                return -1;
1.3       misho     738:        if (v) {
                    739:                if (!(*v = ait_allocVar())) {
                    740:                        io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.2       misho     741:                        return -1;
1.3       misho     742:                } else
                    743:                        AIT_INIT_VAL2(*v, string);
1.2       misho     744:        }
                    745: 
                    746:        /* allocated memory & mirrored shared memory into this */
1.3       misho     747:        Shared = e_malloc(s->sess_size);
1.2       misho     748:        if (!Shared) {
                    749:                LOGERR;
1.3       misho     750:                ait_freeVar(v);
1.2       misho     751:                return -1;
1.3       misho     752:        }
                    753: 
                    754:        DEC_SEM(s);
                    755:        memcpy(Shared, s->sess_addr, s->sess_size);
                    756:        INC_SEM(s);
1.2       misho     757: 
                    758:        for (i = 1, peer = strtok_r(Shared, SESS_MEM_DELIM"\r\n", &p_brk); peer; 
                    759:                        i++, peer = strtok_r(NULL, SESS_MEM_DELIM"\r\n", &p_brk)) {
                    760:                attr = strtok_r(peer, "=\r\n", &a_brk);
1.3       misho     761:                if (attr && !strncmp(attr, csAttr, SESS_MAX_ATTRIBUTE - 1)) {
1.2       misho     762:                        val = strtok_r(NULL, "=\r\n", &a_brk);
1.3       misho     763:                        if (val && strlen(val))
                    764:                                AIT_SET_STR(*v, val);
                    765:                        else
                    766:                                def = SESS_F_DEF;
1.2       misho     767: 
                    768:                        e_free(Shared);
                    769:                        return i | def;
                    770:                }
                    771:        }
                    772: 
                    773:        e_free(Shared);
                    774:        return 0;
                    775: }
                    776: 
                    777: /*
                    778:  * sess_DelValue() - Delete item from session shared memory
                    779:  *
                    780:  * @s = Session item
                    781:  * @csAttr = Attribute for erasing
1.3       misho     782:  * return: -1 error: in parameter or !=-1 deleted items
1.2       misho     783:  */
                    784: int
1.3       misho     785: sess_DelValue(sess_t * __restrict s, const char *csAttr)
1.2       misho     786: {
                    787:        register int i;
1.3       misho     788:        int attrlen, d = 0;
                    789:        char *Buffer, *Shared, szAttr[SESS_MAX_ATTRIBUTE];
1.2       misho     790:        char *peer, *p_brk;
                    791: 
                    792:        if (!s || !csAttr || !*csAttr)
                    793:                return -1;
                    794:        else
                    795:                attrlen = strlen(csAttr);
                    796:        Buffer = Shared = NULL;
                    797:        strlcpy(szAttr, csAttr, sizeof szAttr);
                    798:        strlcat(szAttr, "=", sizeof szAttr);
                    799: 
1.3       misho     800:        Buffer = e_malloc(s->sess_size);
1.2       misho     801:        if (!Buffer) {
                    802:                LOGERR;
                    803:                return -1;
                    804:        } else
1.3       misho     805:                memset(Buffer, 0, s->sess_size);
                    806:        Shared = e_malloc(s->sess_size);
1.2       misho     807:        if (!Shared) {
                    808:                LOGERR;
                    809:                e_free(Buffer);
                    810:                return -1;
                    811:        } else {
                    812:                DEC_SEM(s);
1.3       misho     813:                memcpy(Shared, s->sess_addr, s->sess_size);
1.2       misho     814:        }
                    815: 
                    816:        for (i = 1, peer = strtok_r(Shared, SESS_MEM_DELIM"\r\n", &p_brk); peer; 
                    817:                        i++, peer = strtok_r(NULL, SESS_MEM_DELIM"\r\n", &p_brk)) {
                    818:                if (!strncmp(peer, csAttr, attrlen))
1.3       misho     819:                        if (peer[attrlen] == '=' || !peer[attrlen]) {
                    820:                                d++;
1.2       misho     821:                                continue;
1.3       misho     822:                        }
1.2       misho     823: 
1.3       misho     824:                strlcat(Buffer, peer, s->sess_size);
                    825:                strlcat(Buffer, SESS_MEM_DELIM, s->sess_size);
1.2       misho     826:        }
                    827: 
1.3       misho     828:        memcpy(s->sess_addr, Buffer, s->sess_size);
1.2       misho     829: 
1.3       misho     830:        if (s->sess_type == SHARED_MAP)
                    831:                msync(s->sess_addr, 0, MS_SYNC | MS_INVALIDATE);
1.2       misho     832: 
                    833:        INC_SEM(s);
                    834:        e_free(Shared);
                    835:        e_free(Buffer);
1.3       misho     836:        return d;
1.2       misho     837: }
                    838: 
                    839: /*
                    840:  * sess_SetValue() - Set item into session shared memory or update if exists
                    841:  *
                    842:  * @s = Session item
                    843:  * @csAttr = Attribute
                    844:  * @psVal = Value
                    845:  * return: 0 nothing, -1 error: in parameter, 
1.3       misho     846:        >0 set position, if added new item merged with SESS_F_ADD and 
                    847:        if just define item merged with SESS_F_DEF
1.2       misho     848:  */
                    849: int
1.3       misho     850: sess_SetValue(sess_t * __restrict s, const char *csAttr, const char *psVal)
1.2       misho     851: {
                    852:        register int i;
1.3       misho     853:        int upd = 0, def = 0;
                    854:        char *Buffer, *Shared, szAttr[SESS_MAX_ATTRIBUTE];
1.2       misho     855:        char *peer, *p_brk;
                    856: 
                    857:        if (!s || !csAttr || !*csAttr)
                    858:                return -1;
                    859:        else
                    860:                Buffer = Shared = NULL;
                    861:        strlcpy(szAttr, csAttr, sizeof szAttr);
                    862:        strlcat(szAttr, "=", sizeof szAttr);
                    863: 
1.3       misho     864:        Buffer = e_malloc(s->sess_size);
1.2       misho     865:        if (!Buffer) {
                    866:                LOGERR;
                    867:                return -1;
                    868:        } else
1.3       misho     869:                memset(Buffer, 0, s->sess_size);
                    870:        Shared = e_malloc(s->sess_size);
1.2       misho     871:        if (!Shared) {
                    872:                LOGERR;
                    873:                e_free(Buffer);
                    874:                return -1;
                    875:        } else {
                    876:                DEC_SEM(s);
1.3       misho     877:                memcpy(Shared, s->sess_addr, s->sess_size);
1.2       misho     878:        }
                    879: 
1.3       misho     880:        for (i = 1, peer = strtok_r(Shared, SESS_MEM_DELIM"\r\n", &p_brk); peer; 
                    881:                        i++, peer = strtok_r(NULL, SESS_MEM_DELIM"\r\n", &p_brk))
1.2       misho     882:                if (!strncmp(peer, szAttr, strlen(szAttr))) {
                    883:                        upd++;
                    884:                        if (psVal) {
1.3       misho     885:                                strlcat(Buffer, szAttr, s->sess_size);
                    886:                                strlcat(Buffer, psVal, s->sess_size);
                    887:                                strlcat(Buffer, SESS_MEM_DELIM, s->sess_size);
1.2       misho     888:                        } else {
1.3       misho     889:                                strlcat(Buffer, csAttr, s->sess_size);
                    890:                                strlcat(Buffer, SESS_MEM_DELIM, s->sess_size);
                    891:                                def = SESS_F_DEF;
1.2       misho     892:                        }
1.3       misho     893:                } else {
                    894:                        strlcat(Buffer, peer, s->sess_size);
                    895:                        strlcat(Buffer, SESS_MEM_DELIM, s->sess_size);
1.2       misho     896:                }
                    897: 
                    898:        if (!upd) {
                    899:                if (psVal) {
1.3       misho     900:                        strlcat(Buffer, szAttr, s->sess_size);
                    901:                        strlcat(Buffer, psVal, s->sess_size);
                    902:                        strlcat(Buffer, SESS_MEM_DELIM, s->sess_size);
1.2       misho     903:                } else {
1.3       misho     904:                        strlcat(Buffer, csAttr, s->sess_size);
                    905:                        strlcat(Buffer, SESS_MEM_DELIM, s->sess_size);
                    906:                        def = SESS_F_DEF;
1.2       misho     907:                }
1.3       misho     908:                def |= SESS_F_ADD;
1.2       misho     909:        }
                    910: 
1.3       misho     911:        memcpy(s->sess_addr, Buffer, s->sess_size);
1.2       misho     912: 
1.3       misho     913:        if (s->sess_type == SHARED_MAP)
                    914:                msync(s->sess_addr, 0, MS_SYNC | MS_INVALIDATE);
1.2       misho     915: 
                    916:        INC_SEM(s);
                    917:        e_free(Shared);
                    918:        e_free(Buffer);
                    919:        return upd | def;
                    920: }
                    921: 
                    922: 
                    923: /*
                    924:  * sess_prepareSession() - Attach to shared memory and de-marshaling data
                    925:  *
                    926:  * @s = Session
                    927:  * @useDirect = Use direct shared memory if !=0 or snapshot of data to array
                    928:  * return: NULL error or no data, !=NULL array with variables, 
                    929:  *             after use must free resources with sess_doneSession()
                    930:  */
                    931: array_t *
1.3       misho     932: sess_prepareSession(sess_t * __restrict s, char useDirect)
1.2       misho     933: {
                    934:        array_t *arr = NULL;
                    935:        sess_hdr_t *hdr;
                    936: 
                    937:        if (!s) {
                    938:                io_SetErr(EINVAL, "Invalid argument\n");
                    939:                return NULL;
                    940:        }
1.3       misho     941:        if (s->sess_addr) {
1.2       misho     942:                io_SetErr(EINVAL, "Already attached memory\n");
                    943:                return NULL;
                    944:        }
                    945: 
1.3       misho     946:        if (!ATTACH_MEMORY(s))
1.2       misho     947:                return NULL;
                    948:        else
1.3       misho     949:                hdr = (sess_hdr_t*) s->sess_addr;
1.2       misho     950:        if (hdr->hdr_magic != SESS_AIT_MAGIC) {
                    951:                DETACH_MEMORY(s);
                    952: 
                    953:                io_SetErr(EINVAL, "Shared memory not contains values with proper format\n");
                    954:                return NULL;
                    955:        }
                    956: 
                    957:        DEC_SEM(s);
1.3       misho     958:        s->sess_zcpy = useDirect;
                    959:        arr = ait_map2vars(s->sess_addr + sizeof(sess_hdr_t), s->sess_size - sizeof(sess_hdr_t), 
                    960:                        hdr->hdr_argc, s->sess_zcpy);
1.2       misho     961:        INC_SEM(s);
                    962: 
1.3       misho     963:        if (!s->sess_zcpy)
1.2       misho     964:                DETACH_MEMORY(s);
                    965:        return arr;
                    966: }
                    967: 
                    968: /*
                    969:  * sess_doneSession() - Free resources allocated with sess_prepareSession()
                    970:  *
                    971:  * @s = Session
                    972:  * @arr = Array with variables for free
                    973:  * return: none
                    974:  */
                    975: void
1.3       misho     976: sess_doneSession(sess_t * __restrict s, array_t ** __restrict arr)
1.2       misho     977: {
                    978:        if (!s) {
                    979:                io_SetErr(EINVAL, "Invalid argument\n");
                    980:                return;
                    981:        }
                    982: 
1.3       misho     983:        if (!s->sess_zcpy)
1.2       misho     984:                array_Free(*arr);
                    985:        else
                    986:                DETACH_MEMORY(s);
                    987:        array_Destroy(arr);
                    988: }
                    989: 
                    990: /*
                    991:  * sess_commitSession() - Commit data to shared memory
                    992:  *
                    993:  * @s = Session
                    994:  * @arr = Array with variables for save
                    995:  * return -1 error or !=-1 size of stored variables into shared memory
                    996:  */
                    997: int
1.3       misho     998: sess_commitSession(sess_t * __restrict s, array_t * __restrict arr)
1.2       misho     999: {
                   1000:        sess_hdr_t *hdr;
                   1001:        int ret = 0;
                   1002: 
                   1003:        if (!s || !arr) {
                   1004:                io_SetErr(EINVAL, "Invalid argument\n");
                   1005:                return -1;
                   1006:        }
                   1007: 
1.3       misho    1008:        if (!ATTACH_MEMORY(s)) {
1.2       misho    1009:                DETACH_MEMORY(s);
                   1010:                return -1;
                   1011:        } else
1.3       misho    1012:                hdr = (sess_hdr_t*) s->sess_addr;
1.2       misho    1013: 
                   1014:        DEC_SEM(s);
1.3       misho    1015:        if ((ret = ait_vars2map(s->sess_addr + sizeof(sess_hdr_t), 
                   1016:                                        s->sess_size - sizeof(sess_hdr_t), arr)) != -1) {
1.2       misho    1017:                hdr->hdr_magic = SESS_AIT_MAGIC;
                   1018:                hdr->hdr_argc = array_Size(arr);
                   1019:                ret += sizeof(sess_hdr_t);
                   1020:        }
                   1021:        INC_SEM(s);
                   1022: 
                   1023:        DETACH_MEMORY(s);
                   1024:        return ret;
                   1025: }

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