--- libaitsess/src/aitsess.c 2012/02/10 22:46:41 1.3.2.6 +++ libaitsess/src/aitsess.c 2012/02/28 13:00:24 1.5 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitsess.c,v 1.3.2.6 2012/02/10 22:46:41 misho Exp $ +* $Id: aitsess.c,v 1.5 2012/02/28 13:00:24 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -142,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"); @@ -174,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); @@ -217,22 +217,24 @@ 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 = Session item - * @delFile = !=0 delete session file or =0 keep session * return: none */ void -sess_freeSession(ait_sess_t ** __restrict Sess, int delFile) +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 (delFile && *(*Sess)->name) - unlink((*Sess)->name); - if (*Sess) - free(*Sess); + */ + + free(*Sess); *Sess = NULL; } @@ -272,7 +274,11 @@ map_createSession(int nSeed, long nSize, ait_sess_t * /* build semaphore & shared memory name */ memset(szSName, 0, sizeof szSName); snprintf(szSName[0], MAX_SEMNAME, "/%X.ANS", (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); @@ -286,14 +292,25 @@ map_createSession(int nSeed, long nSize, ait_sess_t * 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(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(Sess); @@ -301,6 +318,7 @@ map_createSession(int nSeed, long nSize, ait_sess_t * } else write(Sess->mem.fd, "", 1); lseek(Sess->mem.fd, 0, SEEK_SET); +#endif } Sess->eom = nSize; @@ -318,12 +336,16 @@ 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); +#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); @@ -331,8 +353,14 @@ map_destroySession(ait_sess_t * __restrict Sess) } 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; } @@ -417,6 +445,8 @@ ipc_destroySession(ait_sess_t * __restrict Sess) 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; }