--- libaitsess/src/aitsess.c 2012/02/10 16:45:36 1.3.2.3 +++ libaitsess/src/aitsess.c 2013/01/17 13:26:37 1.6.4.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitsess.c,v 1.3.2.3 2012/02/10 16:45:36 misho Exp $ +* $Id: aitsess.c,v 1.6.4.3 2013/01/17 13:26:37 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -44,7 +44,6 @@ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF TH SUCH DAMAGE. */ #include "global.h" -#include "aitsess.h" #pragma GCC visibility push(hidden) @@ -54,7 +53,6 @@ char sess_Error[STRSIZ]; #pragma GCC visibility pop -// ----------------------------------------------------------- // Error maintenance functions ... @@ -83,12 +81,12 @@ sess_SetErr(int eno, char *estr, ...) va_end(lst); } -// ----------------------------------------------------------- /* - * sess_initSession() Initializing session structure, if session file not exists creating with specified tech + * sess_initSession() - Initializing session structure, + * if session file not exists creating with specified tech * - * @id = Technology using in session. SHARED_IPC IPC tech orSHARED_MAP BSD MemoryMap tech + * @id = Technology using in session. SHARED_IPC IPC tech or SHARED_MAP BSD MemoryMap tech * @csFName = Session filename for build key and identified * @Sess = Session item, if =NULL allocate memory for session after use must be free! * return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded @@ -109,62 +107,63 @@ sess_initSession(int id, const char *csFName, ait_sess } if (!*Sess) { - *Sess = malloc(sizeof(ait_sess_t)); + *Sess = e_malloc(sizeof(ait_sess_t)); if (!*Sess) { LOGERR; return -1; } } memset(*Sess, 0, sizeof(ait_sess_t)); + strlcpy((*Sess)->name, csFName, sizeof (*Sess)->name); - h = open(csFName, O_WRONLY | O_CREAT | O_EXCL, MEM_MODE); + h = open((*Sess)->name, O_WRONLY | O_CREAT | O_EXCL, MEM_MODE); if (h == -1) { if (errno != EEXIST) { LOGERR; - free(*Sess); + e_free(*Sess); return -1; } /* If key file exist, session already connected */ - h = open(csFName, O_RDONLY); + h = open((*Sess)->name, O_RDONLY); if (h == -1) { LOGERR; - free(*Sess); + e_free(*Sess); return -1; } ret = read(h, szStr, sizeof szStr); if (ret == -1) { LOGERR; close(h); - free(*Sess); + e_free(*Sess); return -1; } if (!strncmp(szStr, "IPC@", 4) && id == SHARED_IPC) { ret = 1; - (*Sess)->sess.create = ipc_createSession; - (*Sess)->sess.destroy = ipc_destroySession; - (*Sess)->sess.attach = ipc_attachSession; - (*Sess)->sess.detach = ipc_detachSession; - (*Sess)->sess.notSem = ipc_notSemaphore; - (*Sess)->sess.isSemOK = ipc_isSemaphoreOK; - (*Sess)->sess.incSem = ipc_incSemaphore; - (*Sess)->sess.decSem = ipc_decSemaphore; + (*Sess)->sess.create = (int (*)(int, long, void*, ...)) ipc_createSession; + (*Sess)->sess.destroy = (void (*)(void*)) ipc_destroySession; + (*Sess)->sess.attach = (void* (*)(void*, void*)) ipc_attachSession; + (*Sess)->sess.detach = (void (*)(void*)) ipc_detachSession; + (*Sess)->sess.notSem = (void (*)(void*)) ipc_notSemaphore; + (*Sess)->sess.isSemOK = (int (*)(void*)) ipc_isSemaphoreOK; + (*Sess)->sess.incSem = (int (*)(void*)) ipc_incSemaphore; + (*Sess)->sess.decSem = (int (*)(void*)) ipc_decSemaphore; } else if (!strncmp(szStr, "MAP@", 4) && id == SHARED_MAP) { ret = 1; - (*Sess)->sess.create = map_createSession; - (*Sess)->sess.destroy = map_destroySession; - (*Sess)->sess.attach = map_attachSession; - (*Sess)->sess.detach = map_detachSession; - (*Sess)->sess.notSem = map_notSemaphore; - (*Sess)->sess.isSemOK = map_isSemaphoreOK; - (*Sess)->sess.incSem = map_incSemaphore; - (*Sess)->sess.decSem = map_decSemaphore; + (*Sess)->sess.create = (int (*)(int, long, void*, ...)) map_createSession; + (*Sess)->sess.destroy = (void (*)(void*)) map_destroySession; + (*Sess)->sess.attach = (void* (*)(void*, void*)) map_attachSession; + (*Sess)->sess.detach = (void (*)(void*)) map_detachSession; + (*Sess)->sess.notSem = (void (*)(void*)) map_notSemaphore; + (*Sess)->sess.isSemOK = (int (*)(void*)) map_isSemaphoreOK; + (*Sess)->sess.incSem = (int (*)(void*)) map_incSemaphore; + (*Sess)->sess.decSem = (int (*)(void*)) map_decSemaphore; } else { sess_SetErr(EPROTONOSUPPORT, "Session type not supported or wrong session type"); close(h); - free(*Sess); + e_free(*Sess); return -1; } /* key found */ @@ -173,30 +172,30 @@ sess_initSession(int id, const char *csFName, ait_sess if (id == SHARED_IPC) { strlcpy(szStr, "IPC@", sizeof szStr); - (*Sess)->sess.create = ipc_createSession; - (*Sess)->sess.destroy = ipc_destroySession; - (*Sess)->sess.attach = ipc_attachSession; - (*Sess)->sess.detach = ipc_detachSession; - (*Sess)->sess.notSem = ipc_notSemaphore; - (*Sess)->sess.isSemOK = ipc_isSemaphoreOK; - (*Sess)->sess.incSem = ipc_incSemaphore; - (*Sess)->sess.decSem = ipc_decSemaphore; + (*Sess)->sess.create = (int (*)(int, long, void*, ...)) ipc_createSession; + (*Sess)->sess.destroy = (void (*)(void*)) ipc_destroySession; + (*Sess)->sess.attach = (void* (*)(void*, void*)) ipc_attachSession; + (*Sess)->sess.detach = (void (*)(void*)) ipc_detachSession; + (*Sess)->sess.notSem = (void (*)(void*)) ipc_notSemaphore; + (*Sess)->sess.isSemOK = (int (*)(void*)) ipc_isSemaphoreOK; + (*Sess)->sess.incSem = (int (*)(void*)) ipc_incSemaphore; + (*Sess)->sess.decSem = (int (*)(void*)) ipc_decSemaphore; } else if (id == SHARED_MAP) { strlcpy(szStr, "MAP@", sizeof szStr); - (*Sess)->sess.create = map_createSession; - (*Sess)->sess.destroy = map_destroySession; - (*Sess)->sess.attach = map_attachSession; - (*Sess)->sess.detach = map_detachSession; - (*Sess)->sess.notSem = map_notSemaphore; - (*Sess)->sess.isSemOK = map_isSemaphoreOK; - (*Sess)->sess.incSem = map_incSemaphore; - (*Sess)->sess.decSem = map_decSemaphore; + (*Sess)->sess.create = (int (*)(int, long, void*, ...)) map_createSession; + (*Sess)->sess.destroy = (void (*)(void*)) map_destroySession; + (*Sess)->sess.attach = (void* (*)(void*, void*)) map_attachSession; + (*Sess)->sess.detach = (void (*)(void*)) map_detachSession; + (*Sess)->sess.notSem = (void (*)(void*)) map_notSemaphore; + (*Sess)->sess.isSemOK = (int (*)(void*)) map_isSemaphoreOK; + (*Sess)->sess.incSem = (int (*)(void*)) map_incSemaphore; + (*Sess)->sess.decSem = (int (*)(void*)) map_decSemaphore; } else { sess_SetErr(EINVAL, "Session type must be specified"); close(h); unlink(csFName); - free(*Sess); + e_free(*Sess); return -1; } strlcat(szStr, "ELWIX_Session ("PACKAGE_STRING")\n", sizeof szStr); @@ -213,33 +212,34 @@ sess_initSession(int id, const char *csFName, ait_sess } /* - * sess_freeSession() Free allocated memory for session item and delete session file if present name + * sess_freeSession() - Free allocated memory for session item and delete session file if present name * - * @csFName = Session filename for delete, if NULL nothing delete * @Sess = Session item * return: none */ void -sess_freeSession(const char *csFName, ait_sess_t ** __restrict Sess) +sess_freeSession(ait_sess_t ** __restrict Sess) { - if (!Sess) + if (!Sess || !(*Sess)) return; + if ((*Sess)->addr) + DETACH_MEMORY(*Sess); + + /* memset(&(*Sess)->sess, 0, sizeof (*Sess)->sess); (*Sess)->type = SHARED_UNKNOWN; - if (csFName) - unlink(csFName); - if (*Sess) - free(*Sess); + */ + + e_free(*Sess); *Sess = NULL; } /* - * map_createSession() MMAP Created session and allocated resources + * map_createSession() - MMAP Created session and allocated resources * - * @csFName = Session name for identified * @nSeed = Seed for securing key, if =-1 must add ready for use key * @nSize = Allocated shared memory size in bytes * @Sess = Session item @@ -247,17 +247,17 @@ sess_freeSession(const char *csFName, ait_sess_t ** __ * return: 0 Ok successful, -1 error: not allocated resources */ int -map_createSession(const char *csFName, int nSeed, long nSize, ait_sess_t * __restrict Sess, ...) +map_createSession(int nSeed, long nSize, ait_sess_t * __restrict Sess, ...) { char szSName[2][FILENAME_MAX]; va_list lst; - if (!Sess) + if (!Sess || !*Sess->name) return -1; if (nSeed != -1) { /* genkey */ - Sess->key = ftok(csFName, nSeed); + Sess->key = ftok(Sess->name, nSeed); if (Sess->key == -1) { LOGERR; return -1; @@ -272,13 +272,17 @@ map_createSession(const char *csFName, int nSeed, long /* build semaphore & shared memory name */ memset(szSName, 0, sizeof szSName); snprintf(szSName[0], MAX_SEMNAME, "/%X.ANS", (u_int) Sess->key); - snprintf(szSName[1], FILENAME_MAX, "%s-%x.ANM", csFName, (u_int) Sess->key); +#ifdef HAVE_SHM_OPEN + snprintf(szSName[1], FILENAME_MAX, "/%s-%x.ANM", Sess->name, (u_int) Sess->key); +#else + snprintf(szSName[1], FILENAME_MAX, "%s-%x.ANM", Sess->name, (u_int) Sess->key); +#endif /* create semaphore & add 1 */ Sess->id.sid = sem_open(szSName[0], O_CREAT, MEM_MODE); if (Sess->id.sid == SEM_FAILED) { LOGERR; - map_destroySession(csFName, Sess); + map_destroySession(Sess); return -1; } /* if is new shared memory session, init sempahore with 1 */ @@ -286,21 +290,33 @@ map_createSession(const char *csFName, int nSeed, long sem_post(Sess->id.sid); /* create file for shared memory storage */ +#ifdef HAVE_SHM_OPEN + Sess->mem.fd = shm_open(szSName[1], O_RDWR | O_CREAT, MEM_MODE); +#else Sess->mem.fd = open(szSName[1], O_RDWR | O_CREAT, MEM_MODE); +#endif if (Sess->mem.fd == -1) { LOGERR; - map_destroySession(csFName, Sess); + map_destroySession(Sess); return -1; } - /* if is new shared memory session, fill file with zeros */ if (!Sess->zcpy) { +#ifdef HAVE_SHM_OPEN + if (ftruncate(Sess->mem.fd, nSize) == -1) { + LOGERR; + map_destroySession(Sess); + return -1; + } +#else + /* if is new shared memory session, fill file with zeros */ if (lseek(Sess->mem.fd, nSize - 1, SEEK_SET) == -1) { LOGERR; - map_destroySession(csFName, Sess); + map_destroySession(Sess); return -1; } else write(Sess->mem.fd, "", 1); lseek(Sess->mem.fd, 0, SEEK_SET); +#endif } Sess->eom = nSize; @@ -308,23 +324,26 @@ map_createSession(const char *csFName, int nSeed, long } /* - * map_destroySession() MMAP free shared resources + * map_destroySession() - MMAP free shared resources * - * @csFName = Session name for delete * @Sess = Session item * return: none */ void -map_destroySession(const char *csFName, ait_sess_t * __restrict Sess) +map_destroySession(ait_sess_t * __restrict Sess) { char szSName[2][FILENAME_MAX]; - if (!Sess || sess_isAttached(Sess)) + if (!Sess || sess_isAttached(Sess) || !*Sess->name) return; memset(szSName, 0, sizeof szSName); snprintf(szSName[0], MAX_SEMNAME, "/%X.ANS", (u_int) Sess->key); - snprintf(szSName[1], FILENAME_MAX, "%s-%x.ANM", csFName, (u_int) Sess->key); +#ifdef HAVE_SHM_UNLINK + snprintf(szSName[1], FILENAME_MAX, "/%s-%x.ANM", Sess->name, (u_int) Sess->key); +#else + snprintf(szSName[1], FILENAME_MAX, "%s-%x.ANM", Sess->name, (u_int) Sess->key); +#endif if (Sess->id.sid != SEM_FAILED) { sem_close(Sess->id.sid); @@ -332,16 +351,21 @@ map_destroySession(const char *csFName, ait_sess_t * _ } if (Sess->mem.fd > 2) { close(Sess->mem.fd); +#ifdef HAVE_SHM_UNLINK + shm_unlink(szSName[1]); +#else unlink(szSName[1]); +#endif } + unlink(Sess->name); + memset(Sess->name, 0, sizeof Sess->name); Sess->eom ^= Sess->eom; Sess->key ^= Sess->key; } /* - * ipc_createSession() IPC Created session and allocated resources + * ipc_createSession() - IPC Created session and allocated resources * - * @csFName = Session name for identified * @nSeed = Seed for securing key, if =-1 must add ready for use key * @nSize = Allocated shared memory size in bytes * @Sess = Session item @@ -349,17 +373,17 @@ map_destroySession(const char *csFName, ait_sess_t * _ * return: 0 Ok successful, -1 error: not allocated resources */ int -ipc_createSession(const char *csFName, int nSeed, long nSize, ait_sess_t * __restrict Sess, ...) +ipc_createSession(int nSeed, long nSize, ait_sess_t * __restrict Sess, ...) { union semun sems; va_list lst; - if (!Sess) + if (!Sess || !*Sess->name) return -1; if (nSeed != -1) { /* genkey */ - Sess->key = ftok(csFName, nSeed); + Sess->key = ftok(Sess->name, nSeed); if (Sess->key == -1) { LOGERR; return -1; @@ -375,7 +399,7 @@ ipc_createSession(const char *csFName, int nSeed, long Sess->id.semid = semget(Sess->key, 1, MEM_MODE | IPC_CREAT); if (Sess->id.semid == -1) { LOGERR; - ipc_destroySession(csFName, Sess); + ipc_destroySession(Sess); return -1; } /* if is new shared memory session, init sempahore with 1 */ @@ -383,7 +407,7 @@ ipc_createSession(const char *csFName, int nSeed, long sems.val = 1; if (semctl(Sess->id.semid, 0, SETVAL, sems) == -1) { LOGERR; - ipc_destroySession(csFName, Sess); + ipc_destroySession(Sess); return -1; } } @@ -392,7 +416,7 @@ ipc_createSession(const char *csFName, int nSeed, long Sess->mem.shmid = shmget(Sess->key, nSize, MEM_MODE | IPC_CREAT); if (Sess->mem.shmid == -1) { LOGERR; - ipc_destroySession(csFName, Sess); + ipc_destroySession(Sess); return -1; } else Sess->eom = nSize; @@ -401,14 +425,13 @@ ipc_createSession(const char *csFName, int nSeed, long } /* - * ipc_destroySession() IPC free shared resources + * ipc_destroySession() - IPC free shared resources * - * @csFName = Session name for delete * @Sess = Session item * return: none */ void -ipc_destroySession(const char *csFName, ait_sess_t * __restrict Sess) +ipc_destroySession(ait_sess_t * __restrict Sess) { union semun sems; struct shmid_ds ds; @@ -420,13 +443,15 @@ ipc_destroySession(const char *csFName, ait_sess_t * _ semctl(Sess->id.semid, 0, IPC_RMID, &sems); if (Sess->mem.shmid != -1) shmctl(Sess->mem.shmid, IPC_RMID, &ds); + unlink(Sess->name); + memset(Sess->name, 0, sizeof Sess->name); Sess->eom ^= Sess->eom; Sess->key ^= Sess->key; } /* - * map_attachSession() MMAP Attach to shared memory & return begin address + * map_attachSession() - MMAP Attach to shared memory & return begin address * * @s = Session item * @procMem = Custom start address (optionl) *default must be 0* @@ -451,7 +476,7 @@ map_attachSession(ait_sess_t * __restrict s, void *pro } /* - * map_detachSession() MMAP Detach from shared memory + * map_detachSession() - MMAP Detach from shared memory * * @s = Session item * return: none @@ -471,7 +496,7 @@ map_detachSession(ait_sess_t * __restrict s) } /* - * ipc_attachSession() IPC Attach to shared memory & return begin address + * ipc_attachSession() - IPC Attach to shared memory & return begin address * * @s = Session item * @procMem = Custom start address (optionl) *default must be 0* @@ -493,7 +518,7 @@ ipc_attachSession(ait_sess_t * __restrict s, void *pro } /* - * ipc_detachSession() IPC Detach from shared memory + * ipc_detachSession() - IPC Detach from shared memory * * @s = Session item * return: none @@ -511,7 +536,7 @@ ipc_detachSession(ait_sess_t * __restrict s) } /* - * sess_isAttached() Check for attached shared memory + * sess_isAttached() - Check for attached shared memory * * @s = Session item * return: -1 null session item, 0 not attached, 1 attached memory @@ -527,7 +552,7 @@ sess_isAttached(ait_sess_t * __restrict s) /* - * map_notSemaphore() MMAP negative block if semaphore isn`t signaled + * map_notSemaphore() - MMAP negative block if semaphore isn`t signaled * * @s = Session item * return: none @@ -548,7 +573,7 @@ map_notSemaphore(ait_sess_t * __restrict s) } /* - * map_isSemaphoreOK() MMAP Check semaphore + * map_isSemaphoreOK() - MMAP Check semaphore * * @s = Session item * return: -1 error: can`t return semaphore, 0 = false, 1 = true @@ -566,13 +591,13 @@ map_isSemaphoreOK(ait_sess_t * __restrict s) } /* - * map_addSemaphore() MMAP unblock semaphore, increment semaphore + * map_incSemaphore() - MMAP unblock semaphore, increment semaphore * * @s = Session item * return: 0 Ok, -1 error: can`t increment */ int -map_addSemaphore(ait_sess_t * __restrict s) +map_incSemaphore(ait_sess_t * __restrict s) { if (!s) return -1; @@ -581,7 +606,7 @@ map_addSemaphore(ait_sess_t * __restrict s) } /* - * map_decSemaphore() MMAP block semaphore, decrement semaphore + * map_decSemaphore() - MMAP block semaphore, decrement semaphore * * @s = Session item * return: 0 Ok, -1 error: can`t decrement @@ -596,7 +621,7 @@ map_decSemaphore(ait_sess_t * __restrict s) } /* - * ipc_notSemaphore() IPC negative block if semaphore isn`t signaled + * ipc_notSemaphore() - IPC negative block if semaphore isn`t signaled * * @s = Session item * return: none @@ -611,7 +636,7 @@ ipc_notSemaphore(ait_sess_t * __restrict s) } /* - * ipc_isSemaphoreOK() IPC Check semaphore + * ipc_isSemaphoreOK() - IPC Check semaphore * * @s = Session item * return: -1 error: can`t return semaphore, 0 = false, 1 = true @@ -628,13 +653,13 @@ ipc_isSemaphoreOK(ait_sess_t * __restrict s) } /* - * ipc_addSemaphore() IPC unblock semaphore, increment semaphore + * ipc_incSemaphore() - IPC unblock semaphore, increment semaphore * * @s = Session item * return: 0 Ok, -1 error: can`t increment */ int -ipc_addSemaphore(ait_sess_t * __restrict s) +ipc_incSemaphore(ait_sess_t * __restrict s) { struct sembuf sb = { 0, 1, 0 }; @@ -645,7 +670,7 @@ ipc_addSemaphore(ait_sess_t * __restrict s) } /* - * ipc_decSemaphore() IPC block semaphore, decrement semaphore + * ipc_decSemaphore() - IPC block semaphore, decrement semaphore * * @s = Session item * return: 0 Ok, -1 error: can`t decrement