File:  [ELWIX - Embedded LightWeight unIX -] / libaitsess / src / sess.c
Revision 1.1.1.1.2.3: download - view: text, annotated - select for diffs - revision graph
Thu Aug 26 07:15:57 2010 UTC (14 years, 2 months ago) by misho
Branches: sess2_0
Diff to: branchpoint 1.1.1.1: preferred, unified
add delimiter macros

    1: /*************************************************************************
    2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: sess.c,v 1.1.1.1.2.3 2010/08/26 07:15:57 misho Exp $
    7: *
    8: *************************************************************************/
    9: #include "global.h"
   10: #include "aitsess.h"
   11: 
   12: 
   13: /*
   14:  * sess_GetValue() Get value from session shared memory from attribute
   15:  * @s = Session item
   16:  * @csAttr = Attribute for search
   17:  * @psVal = Return string buffer
   18:  * @pnLen = Length of return string buffer, 
   19: 	// *{pnLen} input is max_size of buffer & output is really taken bytes
   20:  * return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF
   21: */
   22: int sess_GetValue(tagSess * __restrict s, const char *csAttr, char *psVal, int *pnLen)
   23: {
   24: 	register int i;
   25: 	int def = IS_VAL;
   26: 	char *Shared = NULL;
   27: 	char *peer, *p_brk, *a_brk, *attr, *val;
   28: 
   29: 	if (!s || !csAttr || !*csAttr)
   30: 		return -1;
   31: 	if (psVal) {
   32: 		if (pnLen && *pnLen > 0)
   33: 			memset(psVal, 0, *pnLen);
   34: 		else
   35: 			return -1;
   36: 	}
   37: 
   38: 	// allocated memory & mirrored shared memory into this
   39: 	Shared = malloc(s->eom);
   40: 	if (!Shared) {
   41: 		LOGERR;
   42: 		return -1;
   43: 	} else
   44: 		memcpy(Shared, s->addr, s->eom);
   45: 
   46: 	for (i = 1, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer; 
   47: 			i++, peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
   48: 		attr = strtok_r(peer, "=\r\n", &a_brk);
   49: 		if (attr && !strncmp(attr, csAttr, MAX_ATTRIBUTE)) {
   50: 			val = strtok_r(NULL, "=\r\n", &a_brk);
   51: 			if (val && strlen(val)) {
   52: 				if (psVal)
   53: 					strlcpy(psVal, val, *pnLen);
   54: 				if (pnLen)
   55: 					*pnLen = strlen(val);
   56: 			} else
   57: 				def = IS_DEF;
   58: 
   59: 			free(Shared);
   60: 			return i | def;
   61: 		}
   62: 	}
   63: 
   64: 	free(Shared);
   65: 	return 0;
   66: }
   67: 
   68: /*
   69:  * sess_DelValue() Delete item from session shared memory
   70:  * @s = Session item
   71:  * @csAttr = Attribute for erasing
   72:  * return: 0 Ok, -1 error: in parameter
   73: */
   74: int sess_DelValue(tagSess * __restrict s, const char *csAttr)
   75: {
   76: 	register int i;
   77: 	int ret, attrlen;
   78: 	char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE + 1];
   79: 	char *peer, *p_brk;
   80: 
   81: 	if (!s || !csAttr || !*csAttr)
   82: 		return -1;
   83: 	else
   84: 		attrlen = strlen(csAttr);
   85: 	Buffer = Shared = NULL;
   86: 	memset(szAttr, 0, MAX_ATTRIBUTE + 1);
   87: 	strncpy(szAttr, csAttr, MAX_ATTRIBUTE - 1);
   88: 	strcat(szAttr, "=");
   89: 
   90: 	Buffer = malloc(s->eom);
   91: 	if (!Buffer) {
   92: 		LOGERR;
   93: 		return -1;
   94: 	} else
   95: 		memset(Buffer, 0, s->eom);
   96: 	Shared = malloc(s->eom);
   97: 	if (!Shared) {
   98: 		LOGERR;
   99: 		free(Buffer);
  100: 		return -1;
  101: 	} else {
  102: 		DEC_SEMAPHORE(s, ret);
  103: 		memcpy(Shared, s->addr, s->eom);
  104: 	}
  105: 
  106: 	for (i = 1, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer; 
  107: 			i++, peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
  108: 		if (!strncmp(peer, csAttr, attrlen))
  109: 			if (peer[attrlen] == '=' || peer[attrlen] == *MEM_DELIM || !peer[attrlen] ||
  110: 					peer[attrlen] == '\r' || peer[attrlen] == '\n')
  111: 				continue;
  112: 
  113: 		strcat(Buffer, peer);
  114: 		strcat(Buffer, MEM_DELIM);
  115: 	}
  116: 
  117: 	memset(s->addr, 0, s->eom);
  118: 	memcpy(s->addr, Buffer, s->eom);
  119: 
  120: 	if (s->type == SHARED_MAP)
  121: 		msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
  122: 
  123: 	ADD_SEMAPHORE(s, ret);
  124: 	free(Shared);
  125: 	free(Buffer);
  126: 	return 0;
  127: }
  128: 
  129: /*
  130:  * sess_SetValue() Set item into session shared memory or update if find it
  131:  * @s = Session item
  132:  * @csAttr = Attribute
  133:  * @psVal = Value
  134:  * return: 0 nothing, -1 error: in parameter, 
  135:  	>0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF
  136: */
  137: int sess_SetValue(tagSess * __restrict s, const char *csAttr, const char *psVal)
  138: {
  139: 	register int i;
  140: 	int upd, ret, def = IS_VAL;
  141: 	char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE + 1];
  142: 	char *peer, *p_brk;
  143: 
  144: 	if (!s || !csAttr || !*csAttr)
  145: 		return -1;
  146: 	else
  147: 		Buffer = Shared = NULL;
  148: 	memset(szAttr, 0, MAX_ATTRIBUTE + 1);
  149: 	strncpy(szAttr, csAttr, MAX_ATTRIBUTE - 1);
  150: 	strcat(szAttr, "=");
  151: 
  152: 	Buffer = malloc(s->eom);
  153: 	if (!Buffer) {
  154: 		LOGERR;
  155: 		return -1;
  156: 	} else
  157: 		memset(Buffer, 0, s->eom);
  158: 	Shared = malloc(s->eom);
  159: 	if (!Shared) {
  160: 		LOGERR;
  161: 		free(Buffer);
  162: 		return -1;
  163: 	} else {
  164: 		DEC_SEMAPHORE(s, ret);
  165: 		memcpy(Shared, s->addr, s->eom);
  166: 	}
  167: 
  168: 	for (i = 1, upd = 0, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer; 
  169: 			i++, peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
  170: 		if (!strncmp(peer, szAttr, strlen(szAttr))) {
  171: 			upd++;
  172: 			if (psVal) {
  173: 				strcat(Buffer, szAttr);
  174: 				strcat(Buffer, psVal);
  175: 				strcat(Buffer, MEM_DELIM);
  176: 			} else {
  177: 				strcat(Buffer, csAttr);
  178: 				strcat(Buffer, MEM_DELIM);
  179: 				def = IS_DEF;
  180: 			}
  181: 			continue;
  182: 		}
  183: 
  184: 		strcat(Buffer, peer);
  185: 		strcat(Buffer, MEM_DELIM);
  186: 	}
  187: 
  188: 	if (!upd) {
  189: 		if (psVal) {
  190: 			strcat(Buffer, szAttr);
  191: 			strcat(Buffer, psVal);
  192: 			strcat(Buffer, MEM_DELIM);
  193: 		} else {
  194: 			strcat(Buffer, csAttr);
  195: 			strcat(Buffer, MEM_DELIM);
  196: 			def = IS_DEF;
  197: 		}
  198: 		def |= IS_ADD;
  199: 	}
  200: 
  201: 	memset(s->addr, 0, s->eom);
  202: 	memcpy(s->addr, Buffer, s->eom);
  203: 
  204: 	if (s->type == SHARED_MAP)
  205: 		msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
  206: 
  207: 	ADD_SEMAPHORE(s, ret);
  208: 	free(Shared);
  209: 	free(Buffer);
  210: 	return upd | def;
  211: }

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