--- libaitsess/src/aitsess.c 2012/02/10 23:38:30 1.4 +++ libaitsess/src/aitsess.c 2013/01/17 13:10:49 1.6.4.1 @@ -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.6.4.1 2013/01/17 13:10:49 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,7 +107,7 @@ 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; @@ -122,21 +120,21 @@ sess_initSession(int id, const char *csFName, ait_sess if (h == -1) { if (errno != EEXIST) { LOGERR; - free(*Sess); + e_free(*Sess); return -1; } /* If key file exist, session already connected */ 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) { @@ -165,7 +163,7 @@ sess_initSession(int id, const char *csFName, ait_sess sess_SetErr(EPROTONOSUPPORT, "Session type not supported or wrong session type"); close(h); - free(*Sess); + e_free(*Sess); return -1; } /* key found */ @@ -197,7 +195,7 @@ sess_initSession(int id, const char *csFName, ait_sess 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); @@ -234,7 +232,7 @@ sess_freeSession(ait_sess_t ** __restrict Sess) (*Sess)->type = SHARED_UNKNOWN; */ - free(*Sess); + e_free(*Sess); *Sess = NULL; } @@ -274,7 +272,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 +290,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 +316,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 +339,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 +351,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);