--- libaitsess/src/aitsess.c 2008/08/28 13:18:30 1.1.1.1 +++ libaitsess/src/aitsess.c 2011/04/30 22:02:59 1.2.2.1 @@ -1,3 +1,48 @@ +/************************************************************************* +* (C) 2008 AITNET ltd - Sofia/Bulgaria - +* by Michael Pounov +* +* $Author: misho $ +* $Id: aitsess.c,v 1.2.2.1 2011/04/30 22:02:59 misho Exp $ +* +************************************************************************** +The ELWIX and AITNET software is distributed under the following +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 + by Michael Pounov . All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: +This product includes software developed by Michael Pounov +ELWIX - Embedded LightWeight unIX and its contributors. +4. Neither the name of AITNET nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. +*/ #include "global.h" #include "aitsess.h" @@ -63,7 +108,7 @@ inline int initSession(const int cnID, const char *csF if (!access(csFName, F_OK)) ret = 1; // Build new key & new session - h = open(csFName, O_WRONLY | O_CREAT, 0640); + h = open(csFName, O_WRONLY | O_CREAT, MEM_MODE); if (h == -1) { LOGERR; free(*Sess); @@ -152,7 +197,7 @@ int map_createSession(const char *csFName, const int c memset(mem, 0, cnSize); // create semaphore & add 1 - (*Sess)->id.sid = sem_open(szSName[0], O_CREAT, 0644); + (*Sess)->id.sid = sem_open(szSName[0], O_CREAT, MEM_MODE); if ((*Sess)->id.sid == SEM_FAILED) { LOGERR; map_destroySession(csFName, Sess); @@ -162,7 +207,7 @@ int map_createSession(const char *csFName, const int c sem_post((*Sess)->id.sid); // create file for shared memory storage - (*Sess)->mem.fd = open(szSName[1], O_RDWR | O_CREAT, 0644); + (*Sess)->mem.fd = open(szSName[1], O_RDWR | O_CREAT, MEM_MODE); if ((*Sess)->mem.fd == -1) { LOGERR; map_destroySession(csFName, Sess); @@ -252,7 +297,7 @@ int ipc_createSession(const char *csFName, const int c } // create semaphore - (*Sess)->id.semid = semget((*Sess)->key, 1, 0644 | IPC_CREAT); + (*Sess)->id.semid = semget((*Sess)->key, 1, MEM_MODE | IPC_CREAT); if ((*Sess)->id.semid == -1) { LOGERR; ipc_destroySession(csFName, Sess); @@ -269,7 +314,7 @@ int ipc_createSession(const char *csFName, const int c } // create shared memory object - (*Sess)->mem.shmid = shmget((*Sess)->key, cnSize, 0644 | IPC_CREAT); + (*Sess)->mem.shmid = shmget((*Sess)->key, cnSize, MEM_MODE | IPC_CREAT); if ((*Sess)->mem.shmid == -1) { LOGERR; ipc_destroySession(csFName, Sess); @@ -387,6 +432,19 @@ inline void ipc_detachSession(tagSess * __restrict s) shmdt(s->addr); s->addr = NULL; } +} + +/* + * isAttached() Check for mapped/(attached) shared memory + * @s = Session item + * return: -1 null session item, 0 not attached, 1 attached memory +*/ +inline int isAttached(tagSess * __restrict s) +{ + if (!s) + return -1; + + return (s->addr ? 1 : 0); }