--- libaitsess/src/aitsess.c 2011/05/10 21:06:13 1.2.2.2 +++ libaitsess/src/aitsess.c 2011/08/21 14:13:41 1.2.2.4 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitsess.c,v 1.2.2.2 2011/05/10 21:06:13 misho Exp $ +* $Id: aitsess.c,v 1.2.2.4 2011/08/21 14:13:41 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -49,8 +49,8 @@ SUCH DAMAGE. #pragma GCC visibility push(hidden) -int sessErrno; -char sessError[MAX_STR + 1]; +int sess_Errno; +char sess_Error[STRSIZ]; #pragma GCC visibility pop @@ -62,42 +62,42 @@ char sessError[MAX_STR + 1]; inline int sess_GetErrno() { - return sessErrno; + return sess_Errno; } // sess_GetError() Get error text of last operation inline const char * sess_GetError() { - return sessError; + return sess_Error; } -// sessDbg() Debug/Logging operations -static inline int -sessDbg(FILE *f, char *fmt, ...) +// sess_SetErr() Set error to variables for internal use!!! +inline void +sess_SetErr(int eno, char *estr, ...) { - int ret = 0; va_list lst; - va_start(lst, fmt); - ret = vfprintf(f, fmt, lst); + sess_Errno = eno; + memset(sess_Error, 0, sizeof sess_Error); + va_start(lst, estr); + vsnprintf(sess_Error, sizeof sess_Error, estr, lst); va_end(lst); - - return ret; } // ----------------------------------------------------------- /* * initSession() Initializing session structure, if session file not exists creating with specified tech - * @cnID = Technology using in session. SHARED_IPC IPC tech; SHARED_MAP BSD MemoryMap tech + * @pnID = Technology using in session. + SHARED_IPC IPC tech; SHARED_MAP BSD MemoryMap tech or if =NULL SHARED_IPC * @csFName = Session filename for build key and identified - * @Sess = Session item + * @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 */ inline int -initSession(const int cnID, const char *csFName, tagSess ** __restrict Sess) +initSession(int *pnID, const char *csFName, tagSess ** __restrict Sess) { - int h, ret = 0; - char szStr[MAX_STR + 1]; + int h, ret = 0, id = SHARED_IPC; + char szStr[STRSIZ]; if (!*Sess) { *Sess = malloc(sizeof(tagSess)); @@ -105,43 +105,68 @@ initSession(const int cnID, const char *csFName, tagSe LOGERR; return -1; } - } - memset(*Sess, 0, sizeof(tagSess)); + } else + memset(*Sess, 0, sizeof(tagSess)); + if (pnID && *pnID) + id = *pnID; - // If key file exist, session already connected - if (!access(csFName, F_OK)) - ret = 1; - // Build new key & new session - h = open(csFName, O_WRONLY | O_CREAT, MEM_MODE); + h = open(csFName, O_WRONLY | O_CREAT | O_EXCL, MEM_MODE); if (h == -1) { - LOGERR; - free(*Sess); - return -1; - } - - bzero(szStr, MAX_STR + 1); - switch (cnID) { - case SHARED_IPC: - strcpy(szStr, "IPC@"); - break; - case SHARED_MAP: - strcpy(szStr, "MAP@"); - break; - default: - errno = EPROTONOSUPPORT; + if (errno != EEXIST) { LOGERR; - + free(*Sess); + return -1; + } + /* If key file exist, session already connected */ + h = open(csFName, O_RDONLY); + if (h == -1) { + LOGERR; + free(*Sess); + return -1; + } + ret = read(h, szStr, sizeof szStr); + if (ret == -1) { + LOGERR; close(h); - unlink(csFName); free(*Sess); return -1; + } + if (!strncmp(szStr, "IPC@", 4)) + id = SHARED_IPC; + else if (!strncmp(szStr, "MAP@", 4)) + id = SHARED_MAP; + else { + sess_SetErr(EPROTONOSUPPORT, "Error:: Session type not supported"); + close(h); + free(*Sess); + return -1; + } + + ret = 1; /* key found */ + } else { + /* Build new key & new session */ + switch (id) { + case SHARED_IPC: + strlcpy(szStr, "IPC@", sizeof szStr); + break; + case SHARED_MAP: + strlcpy(szStr, "MAP@", sizeof szStr); + break; + default: + sess_SetErr(EPROTONOSUPPORT, "Error:: Session type not supported"); + close(h); + unlink(csFName); + free(*Sess); + return -1; + } + strlcat(szStr, "ELWIX_Session ("PACKAGE_STRING")\n", sizeof szStr); + write(h, szStr, strlen(szStr)); + + ret = 0; /* new key created */ } - strcat(szStr, "AN_Session ver"); - strcat(szStr, "\n"); - write(h, szStr, strlen(szStr)); close(h); - (*Sess)->type = cnID; + (*Sess)->type = id; return ret; } @@ -153,7 +178,11 @@ initSession(const int cnID, const char *csFName, tagSe inline void freeSession(const char *csFName, tagSess ** __restrict Sess) { - (*Sess)->type ^= (*Sess)->type; + assert(Sess); + if (!Sess) + return; + + (*Sess)->type = SHARED_UNKNOWN; if (csFName) unlink(csFName); if (*Sess) @@ -173,15 +202,14 @@ freeSession(const char *csFName, tagSess ** __restrict int map_createSession(const char *csFName, const int cnSeed, const u_int cnSize, tagSess ** __restrict Sess) { - int ret = 0; - char szSName[2][FILENAME_MAX + 1]; - void *mem; + int ret = 0, id = SHARED_MAP; + char szSName[2][FILENAME_MAX]; - ret = initSession(SHARED_MAP, csFName, Sess); + ret = initSession(&id, csFName, Sess); if (ret == -1 || !*Sess) return -1; - // genkey + /* genkey */ (*Sess)->key = ftok(csFName, cnSeed); if ((*Sess)->key == -1) { LOGERR; @@ -189,55 +217,39 @@ map_createSession(const char *csFName, const int cnSee return -1; } - // build semaphore & shared memory name - memset(szSName, 0, (FILENAME_MAX + 1) * 2); - snprintf(szSName[0], MAX_SEMNAME + 1, "/%X.ANS", (u_int) (*Sess)->key); - snprintf(szSName[1], FILENAME_MAX + 1, "%s-%x.ANM", csFName, (u_int) (*Sess)->key); + /* 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); - mem = malloc(cnSize); - if (!mem) { - LOGERR; - freeSession(csFName, Sess); - return -1; - } else - memset(mem, 0, cnSize); - - // create semaphore & add 1 + /* 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); - free(mem); return -1; } else sem_post((*Sess)->id.sid); - // create file for shared memory storage + /* create file for shared memory storage */ (*Sess)->mem.fd = open(szSName[1], O_RDWR | O_CREAT, MEM_MODE); if ((*Sess)->mem.fd == -1) { LOGERR; map_destroySession(csFName, Sess); - free(mem); return -1; } - // if is new shared memory session, fill file with zeros + /* if is new shared memory session, fill file with zeros */ if (!ret) { - if (write((*Sess)->mem.fd, mem, cnSize) != cnSize) { + if (lseek((*Sess)->mem.fd, cnSize - 1, SEEK_SET) == -1) { LOGERR; map_destroySession(csFName, Sess); - free(mem); return -1; - } - if (lseek((*Sess)->mem.fd, 0, SEEK_SET)) { - LOGERR; - map_destroySession(csFName, Sess); - free(mem); - return -1; - } + } else + write((*Sess)->mem.fd, "", 1); + lseek((*Sess)->mem.fd, 0, SEEK_SET); } (*Sess)->eom = cnSize; - free(mem); return ret; } @@ -250,26 +262,25 @@ void map_destroySession(const char *csFName, tagSess ** __restrict Sess) { int flg = 1; - char szSName[2][FILENAME_MAX + 1]; + char szSName[2][FILENAME_MAX]; - if (!*Sess) + assert(Sess); + if (!Sess || !*Sess) return; - bzero(szSName, (FILENAME_MAX + 1) * 2); - snprintf(szSName[0], MAX_SEMNAME + 1, "/%X.ANS", (u_int) (*Sess)->key); - snprintf(szSName[1], FILENAME_MAX + 1, "%s-%x.ANM", csFName, (u_int) (*Sess)->key); + 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); if ((*Sess)->id.sid != SEM_FAILED) { if (sem_close((*Sess)->id.sid) == -1) flg = 0; - if (sem_unlink(szSName[0]) == -1) /*flg = 0*/; } if ((*Sess)->mem.fd != -1) { if (close((*Sess)->mem.fd) == -1) flg = 0; - if (unlink(szSName[1]) == -1) /*flg = 0*/; } @@ -289,14 +300,14 @@ map_destroySession(const char *csFName, tagSess ** __r int ipc_createSession(const char *csFName, const int cnSeed, const u_int cnSize, tagSess ** __restrict Sess) { - int ret = 0; + int ret = 0, id = SHARED_IPC; union semun sems; - ret = initSession(SHARED_IPC, csFName, Sess); + ret = initSession(&id, csFName, Sess); if (ret == -1 || !*Sess) return -1; - // genkey + /* genkey */ (*Sess)->key = ftok(csFName, cnSeed); if ((*Sess)->key == -1) { LOGERR; @@ -304,14 +315,14 @@ ipc_createSession(const char *csFName, const int cnSee return -1; } - // create semaphore + /* create semaphore */ (*Sess)->id.semid = semget((*Sess)->key, 1, MEM_MODE | IPC_CREAT); if ((*Sess)->id.semid == -1) { LOGERR; ipc_destroySession(csFName, Sess); return -1; } - // if is new shared memory session, init sempahore with 1 + /* if is new shared memory session, init sempahore with 1 */ if (!ret) { sems.val = 1; if (semctl((*Sess)->id.semid, 0, SETVAL, sems) == -1) { @@ -321,7 +332,7 @@ ipc_createSession(const char *csFName, const int cnSee } } - // create shared memory object + /* create shared memory object */ (*Sess)->mem.shmid = shmget((*Sess)->key, cnSize, MEM_MODE | IPC_CREAT); if ((*Sess)->mem.shmid == -1) { LOGERR; @@ -345,7 +356,8 @@ ipc_destroySession(const char *csFName, tagSess ** __r union semun sems; struct shmid_ds ds; - if (!*Sess) + assert(Sess); + if (!Sess || !*Sess) return; if ((*Sess)->id.semid != -1) @@ -374,7 +386,7 @@ map_attachSession(tagSess * __restrict s, void *procMe if (!s) return NULL; - // Learn size of shared memory block + /* Learn size of shared memory block */ sync(); if (fstat(s->mem.fd, &sb) == -1) { LOGERR; @@ -382,7 +394,7 @@ map_attachSession(tagSess * __restrict s, void *procMe } else s->eom = sb.st_size; - // attach to memory + /* attach to memory */ s->addr = mmap(procMem, s->eom, PROT_READ | PROT_WRITE, MAP_SHARED, s->mem.fd, 0); if (s->addr == MAP_FAILED) { LOGERR;