--- libaitsess/src/aitsess.c 2012/02/10 16:54:37 1.3.2.4 +++ libaitsess/src/aitsess.c 2012/02/10 23:38:30 1.4 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitsess.c,v 1.3.2.4 2012/02/10 16:54:37 misho Exp $ +* $Id: aitsess.c,v 1.4 2012/02/10 23:38:30 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -116,8 +116,9 @@ sess_initSession(int id, const char *csFName, ait_sess } } 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; @@ -125,7 +126,7 @@ sess_initSession(int id, const char *csFName, ait_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); @@ -141,25 +142,25 @@ sess_initSession(int id, const char *csFName, ait_sess 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"); @@ -173,25 +174,25 @@ 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); @@ -215,23 +216,25 @@ sess_initSession(int id, const char *csFName, ait_sess /* * 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); + */ + + free(*Sess); *Sess = NULL; } @@ -239,7 +242,6 @@ sess_freeSession(const char *csFName, ait_sess_t ** __ /* * 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 +249,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 +274,13 @@ 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); + snprintf(szSName[1], FILENAME_MAX, "%s-%x.ANM", Sess->name, (u_int) Sess->key); /* 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 */ @@ -289,14 +291,14 @@ map_createSession(const char *csFName, int nSeed, long Sess->mem.fd = open(szSName[1], O_RDWR | O_CREAT, MEM_MODE); 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) { 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); @@ -310,21 +312,20 @@ map_createSession(const char *csFName, int nSeed, long /* * 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); + snprintf(szSName[1], FILENAME_MAX, "%s-%x.ANM", Sess->name, (u_int) Sess->key); if (Sess->id.sid != SEM_FAILED) { sem_close(Sess->id.sid); @@ -334,6 +335,8 @@ map_destroySession(const char *csFName, ait_sess_t * _ close(Sess->mem.fd); unlink(szSName[1]); } + unlink(Sess->name); + memset(Sess->name, 0, sizeof Sess->name); Sess->eom ^= Sess->eom; Sess->key ^= Sess->key; } @@ -341,7 +344,6 @@ map_destroySession(const char *csFName, ait_sess_t * _ /* * 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 +351,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 +377,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 +385,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 +394,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; @@ -403,12 +405,11 @@ ipc_createSession(const char *csFName, int nSeed, long /* * 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,6 +421,8 @@ 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; }