Diff for /libaitsess/src/sess.c between versions 1.3 and 1.4

version 1.3, 2011/09/07 13:11:56 version 1.4, 2012/02/10 23:38:30
Line 49  SUCH DAMAGE. Line 49  SUCH DAMAGE.
   
 /*  /*
  * sess_FreeValues() Free all values from value array allocated from sess_GetValues()   * sess_FreeValues() Free all values from value array allocated from sess_GetValues()
    *
  * @ppsVals = Array strings   * @ppsVals = Array strings
  * return: none   * return: none
*/ */
 inline void  inline void
 sess_FreeValues(char *** __restrict ppsVals)  sess_FreeValues(char *** __restrict ppsVals)
 {  {
Line 69  sess_FreeValues(char *** __restrict ppsVals) Line 70  sess_FreeValues(char *** __restrict ppsVals)
   
 /*  /*
  * sess_GetValues() Get all values from session shared memory   * sess_GetValues() Get all values from session shared memory
    *
  * @s = Session item   * @s = Session item
  * @ppsVals = Return array strings   * @ppsVals = Return array strings
  * return: -1 error: in parameter, !=-1 count of returned strings in ppsVals (must be free after use!)   * return: -1 error: in parameter, !=-1 count of returned strings in ppsVals (must be free after use!)
*/ */
 int  int
sess_GetValues(tagSess * __restrict s, char ***ppsVals)sess_GetValues(ait_sess_t * __restrict s, char ***ppsVals)
 {  {
         register int i;          register int i;
         char **valz, *Shared = NULL;          char **valz, *Shared = NULL;
Line 123  sess_GetValues(tagSess * __restrict s, char ***ppsVals Line 125  sess_GetValues(tagSess * __restrict s, char ***ppsVals
   
 /*  /*
  * sess_GetValue() Get value from session shared memory from attribute   * sess_GetValue() Get value from session shared memory from attribute
    *
  * @s = Session item   * @s = Session item
  * @csAttr = Attribute for search   * @csAttr = Attribute for search
  * @psVal = Return string buffer   * @psVal = Return string buffer
  * @pnLen = Length of return string buffer,    * @pnLen = Length of return string buffer, 
         // *{pnLen} input is max_size of buffer & output is really taken bytes          // *{pnLen} input is max_size of buffer & output is really taken bytes
  * return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF   * return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF
*/ */
 int  int
sess_GetValue(tagSess * __restrict s, const char *csAttr, char *psVal, int *pnLen)sess_GetValue(ait_sess_t * __restrict s, const char *csAttr, char *psVal, int *pnLen)
 {  {
         register int i;          register int i;
         int def = IS_VAL;          int def = IS_VAL;
Line 158  sess_GetValue(tagSess * __restrict s, const char *csAt Line 161  sess_GetValue(tagSess * __restrict s, const char *csAt
         for (i = 1, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer;           for (i = 1, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer; 
                         i++, peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {                          i++, peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
                 attr = strtok_r(peer, "=\r\n", &a_brk);                  attr = strtok_r(peer, "=\r\n", &a_brk);
                if (attr && !strncmp(attr, csAttr, MAX_ATTRIBUTE)) {                if (attr && !strncmp(attr, csAttr, MAX_ATTRIBUTE - 1)) {
                         val = strtok_r(NULL, "=\r\n", &a_brk);                          val = strtok_r(NULL, "=\r\n", &a_brk);
                         if (val && strlen(val)) {                          if (val && strlen(val)) {
                                 if (psVal)                                  if (psVal)
Line 179  sess_GetValue(tagSess * __restrict s, const char *csAt Line 182  sess_GetValue(tagSess * __restrict s, const char *csAt
   
 /*  /*
  * sess_DelValue() Delete item from session shared memory   * sess_DelValue() Delete item from session shared memory
    *
  * @s = Session item   * @s = Session item
  * @csAttr = Attribute for erasing   * @csAttr = Attribute for erasing
  * return: 0 Ok, -1 error: in parameter   * return: 0 Ok, -1 error: in parameter
*/ */
 int  int
sess_DelValue(tagSess * __restrict s, const char *csAttr)sess_DelValue(ait_sess_t * __restrict s, const char *csAttr)
 {  {
         register int i;          register int i;
        int ret, attrlen;        int attrlen;
        char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE + 1];        char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE];
         char *peer, *p_brk;          char *peer, *p_brk;
   
         if (!s || !csAttr || !*csAttr)          if (!s || !csAttr || !*csAttr)
Line 196  sess_DelValue(tagSess * __restrict s, const char *csAt Line 200  sess_DelValue(tagSess * __restrict s, const char *csAt
         else          else
                 attrlen = strlen(csAttr);                  attrlen = strlen(csAttr);
         Buffer = Shared = NULL;          Buffer = Shared = NULL;
        memset(szAttr, 0, MAX_ATTRIBUTE + 1);        strlcpy(szAttr, csAttr, sizeof szAttr);
        strncpy(szAttr, csAttr, MAX_ATTRIBUTE - 1);        strlcat(szAttr, "=", sizeof szAttr);
        strcat(szAttr, "="); 
   
         Buffer = malloc(s->eom);          Buffer = malloc(s->eom);
         if (!Buffer) {          if (!Buffer) {
Line 212  sess_DelValue(tagSess * __restrict s, const char *csAt Line 215  sess_DelValue(tagSess * __restrict s, const char *csAt
                 free(Buffer);                  free(Buffer);
                 return -1;                  return -1;
         } else {          } else {
                DEC_SEMAPHORE(s, ret);                DEC_SEM(s);
                 memcpy(Shared, s->addr, s->eom);                  memcpy(Shared, s->addr, s->eom);
         }          }
   
Line 223  sess_DelValue(tagSess * __restrict s, const char *csAt Line 226  sess_DelValue(tagSess * __restrict s, const char *csAt
                                         peer[attrlen] == '\r' || peer[attrlen] == '\n')                                          peer[attrlen] == '\r' || peer[attrlen] == '\n')
                                 continue;                                  continue;
   
                strcat(Buffer, peer);                strlcat(Buffer, peer, s->eom);
                strcat(Buffer, MEM_DELIM);                strlcat(Buffer, MEM_DELIM, s->eom);
         }          }
   
         memset(s->addr, 0, s->eom);  
         memcpy(s->addr, Buffer, s->eom);          memcpy(s->addr, Buffer, s->eom);
   
         if (s->type == SHARED_MAP)          if (s->type == SHARED_MAP)
                 msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);                  msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
   
        ADD_SEMAPHORE(s, ret);        INC_SEM(s);
         free(Shared);          free(Shared);
         free(Buffer);          free(Buffer);
         return 0;          return 0;
Line 241  sess_DelValue(tagSess * __restrict s, const char *csAt Line 243  sess_DelValue(tagSess * __restrict s, const char *csAt
   
 /*  /*
  * sess_SetValue() Set item into session shared memory or update if find it   * sess_SetValue() Set item into session shared memory or update if find it
    *
  * @s = Session item   * @s = Session item
  * @csAttr = Attribute   * @csAttr = Attribute
  * @psVal = Value   * @psVal = Value
  * return: 0 nothing, -1 error: in parameter,    * return: 0 nothing, -1 error: in parameter, 
         >0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF          >0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF
*/ */
 int  int
sess_SetValue(tagSess * __restrict s, const char *csAttr, const char *psVal)sess_SetValue(ait_sess_t * __restrict s, const char *csAttr, const char *psVal)
 {  {
         register int i;          register int i;
        int upd, ret, def = IS_VAL;        int upd, def = IS_VAL;
        char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE + 1];        char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE];
         char *peer, *p_brk;          char *peer, *p_brk;
   
         if (!s || !csAttr || !*csAttr)          if (!s || !csAttr || !*csAttr)
                 return -1;                  return -1;
         else          else
                 Buffer = Shared = NULL;                  Buffer = Shared = NULL;
        memset(szAttr, 0, MAX_ATTRIBUTE + 1);        strlcpy(szAttr, csAttr, sizeof szAttr);
        strncpy(szAttr, csAttr, MAX_ATTRIBUTE - 1);        strlcat(szAttr, "=", sizeof szAttr);
        strcat(szAttr, "="); 
   
         Buffer = malloc(s->eom);          Buffer = malloc(s->eom);
         if (!Buffer) {          if (!Buffer) {
Line 275  sess_SetValue(tagSess * __restrict s, const char *csAt Line 277  sess_SetValue(tagSess * __restrict s, const char *csAt
                 free(Buffer);                  free(Buffer);
                 return -1;                  return -1;
         } else {          } else {
                DEC_SEMAPHORE(s, ret);                DEC_SEM(s);
                 memcpy(Shared, s->addr, s->eom);                  memcpy(Shared, s->addr, s->eom);
         }          }
   
Line 284  sess_SetValue(tagSess * __restrict s, const char *csAt Line 286  sess_SetValue(tagSess * __restrict s, const char *csAt
                 if (!strncmp(peer, szAttr, strlen(szAttr))) {                  if (!strncmp(peer, szAttr, strlen(szAttr))) {
                         upd++;                          upd++;
                         if (psVal) {                          if (psVal) {
                                strcat(Buffer, szAttr);                                strlcat(Buffer, szAttr, s->eom);
                                strcat(Buffer, psVal);                                strlcat(Buffer, psVal, s->eom);
                                strcat(Buffer, MEM_DELIM);                                strlcat(Buffer, MEM_DELIM, s->eom);
                         } else {                          } else {
                                strcat(Buffer, csAttr);                                strlcat(Buffer, csAttr, s->eom);
                                strcat(Buffer, MEM_DELIM);                                strlcat(Buffer, MEM_DELIM, s->eom);
                                 def = IS_DEF;                                  def = IS_DEF;
                         }                          }
                         continue;                          continue;
                 }                  }
   
                strcat(Buffer, peer);                strlcat(Buffer, peer, s->eom);
                strcat(Buffer, MEM_DELIM);                strlcat(Buffer, MEM_DELIM, s->eom);
         }          }
   
         if (!upd) {          if (!upd) {
                 if (psVal) {                  if (psVal) {
                        strcat(Buffer, szAttr);                        strlcat(Buffer, szAttr, s->eom);
                        strcat(Buffer, psVal);                        strlcat(Buffer, psVal, s->eom);
                        strcat(Buffer, MEM_DELIM);                        strlcat(Buffer, MEM_DELIM, s->eom);
                 } else {                  } else {
                        strcat(Buffer, csAttr);                        strlcat(Buffer, csAttr, s->eom);
                        strcat(Buffer, MEM_DELIM);                        strlcat(Buffer, MEM_DELIM, s->eom);
                         def = IS_DEF;                          def = IS_DEF;
                 }                  }
                 def |= IS_ADD;                  def |= IS_ADD;
         }          }
   
         memset(s->addr, 0, s->eom);  
         memcpy(s->addr, Buffer, s->eom);          memcpy(s->addr, Buffer, s->eom);
   
         if (s->type == SHARED_MAP)          if (s->type == SHARED_MAP)
                 msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);                  msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
   
        ADD_SEMAPHORE(s, ret);        INC_SEM(s);
         free(Shared);          free(Shared);
         free(Buffer);          free(Buffer);
         return upd | def;          return upd | def;
   }
   
   
   /*
    * sess_prepareSession() Attach to shared memory and de-marshaling data
    *
    * @s = Session
    * @useDirect = Use direct shared memory if !=0 or snapshot of data to array
    * return: NULL error or no data, !=NULL array with variables, 
    *              after use must free resources with sess_doneSession()
    */
   array_t *
   sess_prepareSession(ait_sess_t * __restrict s, char useDirect)
   {
           array_t *arr = NULL;
           sess_hdr_t *hdr;
   
           assert(s);
           if (!s) {
                   sess_SetErr(EINVAL, "Error:: invalid argument\n");
                   return NULL;
           }
           if (s->addr) {
                   sess_SetErr(EINVAL, "Error:: already attached memory\n");
                   return NULL;
           }
   
           ATTACH_MEMORY(s);
           if (!s->addr)
                   return NULL;
           else
                   hdr = (sess_hdr_t*) s->addr;
           if (hdr->hdr_magic != SESS_AIT_MAGIC) {
                   DETACH_MEMORY(s);
   
                   sess_SetErr(EINVAL, "Error:: shared memory not contains values with proper format\n");
                   return NULL;
           }
   
           DEC_SEM(s);
           s->zcpy = useDirect;
           arr = io_map2vars(s->addr + sizeof(sess_hdr_t), s->eom - sizeof(sess_hdr_t), 
                           hdr->hdr_argc, useDirect);
           INC_SEM(s);
   
           if (!s->zcpy)
                   DETACH_MEMORY(s);
           return arr;
   }
   
   /*
    * sess_doneSession() Free resources allocated with sess_prepareSession()
    *
    * @s = Session
    * @arr = Array with variables for free
    * return: none
    */
   void
   sess_doneSession(ait_sess_t * __restrict s, array_t ** __restrict arr)
   {
           assert(s);
           if (!s) {
                   sess_SetErr(EINVAL, "Error:: invalid argument\n");
                   return;
           }
   
           if (!s->zcpy)
                   io_arrayFree(*arr);
           else
                   DETACH_MEMORY(s);
           io_arrayDestroy(arr);
   }
   
   /*
    * sess_commitSession() Commit data to shared memory
    *
    * @s = Session
    * @arr = Array with variables for save
    * return -1 error or !=-1 size of stored variables into shared memory
    */
   int
   sess_commitSession(ait_sess_t * __restrict s, array_t * __restrict arr)
   {
           sess_hdr_t *hdr;
           int ret = 0;
   
           assert(s && arr);
           if (!s || !arr) {
                   sess_SetErr(EINVAL, "Error:: invalid argument\n");
                   return -1;
           }
   
           ATTACH_MEMORY(s);
           if (!s->addr) {
                   DETACH_MEMORY(s);
                   return -1;
           } else
                   hdr = (sess_hdr_t*) s->addr;
   
           DEC_SEM(s);
           if ((ret = io_vars2map(s->addr + sizeof(sess_hdr_t), s->eom - sizeof(sess_hdr_t), arr)) != -1) {
                   hdr->hdr_magic = SESS_AIT_MAGIC;
                   hdr->hdr_argc = io_arraySize(arr);
                   ret += sizeof(sess_hdr_t);
           }
           INC_SEM(s);
   
           DETACH_MEMORY(s);
           return ret;
 }  }

Removed from v.1.3  
changed lines
  Added in v.1.4


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>