--- libaitsess/src/aitsess.c 2012/02/10 23:38:30 1.4 +++ 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.4 2012/02/10 23:38:30 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 @@ -274,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); @@ -288,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); @@ -303,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; @@ -325,7 +341,11 @@ map_destroySession(ait_sess_t * __restrict Sess) 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); @@ -333,7 +353,11 @@ 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);