Annotation of libaitsess/src/sess.c, revision 1.2
1.2 ! misho 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.5 2010/09/01 21:07:10 misho Exp $
! 7: *
! 8: *************************************************************************/
1.1 misho 9: #include "global.h"
10: #include "aitsess.h"
11:
12:
13: /*
1.2 ! misho 14: * sess_FreeValues() Free all values from value array allocated from sess_GetValues()
! 15: * @ppsVals = Array strings
! 16: * return: none
! 17: */
! 18: inline void sess_FreeValues(char *** __restrict ppsVals)
! 19: {
! 20: char **ptr;
! 21:
! 22: for (ptr = *ppsVals; *ptr; ptr++)
! 23: free(*ptr);
! 24: free(*ppsVals);
! 25: *ppsVals = NULL;
! 26: }
! 27:
! 28: /*
! 29: * sess_GetValues() Get all values from session shared memory
! 30: * @s = Session item
! 31: * @ppsVals = Return array strings
! 32: * return: -1 error: in parameter, !=-1 count of returned strings in ppsVals (must be free after use!)
! 33: */
! 34: int sess_GetValues(tagSess * __restrict s, char ***ppsVals)
! 35: {
! 36: register int i;
! 37: char **valz, *Shared = NULL;
! 38: char *peer, *p_brk;
! 39:
! 40: if (!s || !ppsVals)
! 41: return -1;
! 42: valz = malloc(sizeof(caddr_t));
! 43: if (!valz) {
! 44: LOGERR;
! 45: return -1;
! 46: } else
! 47: *valz = NULL;
! 48:
! 49: // allocated memory & mirrored shared memory into this
! 50: Shared = malloc(s->eom);
! 51: if (!Shared) {
! 52: LOGERR;
! 53: free(valz);
! 54: return -1;
! 55: } else
! 56: memcpy(Shared, s->addr, s->eom);
! 57:
! 58: for (i = 0, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer;
! 59: peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
! 60: if (!strchr(peer, '='))
! 61: continue;
! 62: else
! 63: i++;
! 64:
! 65: valz = realloc(valz, (i + 1) * sizeof(caddr_t));
! 66: if (!valz) {
! 67: LOGERR;
! 68: free(Shared);
! 69: return -1;
! 70: } else
! 71: valz[i] = NULL;
! 72:
! 73: valz[i - 1] = strdup(peer);
! 74: }
! 75:
! 76: free(Shared);
! 77: *ppsVals = valz;
! 78: return i;
! 79: }
! 80:
! 81: /*
1.1 misho 82: * sess_GetValue() Get value from session shared memory from attribute
83: * @s = Session item
84: * @csAttr = Attribute for search
85: * @psVal = Return string buffer
86: * @pnLen = Length of return string buffer,
87: // *{pnLen} input is max_size of buffer & output is really taken bytes
88: * return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF
89: */
90: int sess_GetValue(tagSess * __restrict s, const char *csAttr, char *psVal, int *pnLen)
91: {
92: register int i;
93: int def = IS_VAL;
94: char *Shared = NULL;
95: char *peer, *p_brk, *a_brk, *attr, *val;
96:
97: if (!s || !csAttr || !*csAttr)
98: return -1;
99: if (psVal) {
100: if (pnLen && *pnLen > 0)
101: memset(psVal, 0, *pnLen);
102: else
103: return -1;
104: }
105:
106: // allocated memory & mirrored shared memory into this
107: Shared = malloc(s->eom);
108: if (!Shared) {
109: LOGERR;
110: return -1;
111: } else
112: memcpy(Shared, s->addr, s->eom);
113:
1.2 ! misho 114: for (i = 1, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer;
! 115: i++, peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
1.1 misho 116: attr = strtok_r(peer, "=\r\n", &a_brk);
117: if (attr && !strncmp(attr, csAttr, MAX_ATTRIBUTE)) {
118: val = strtok_r(NULL, "=\r\n", &a_brk);
119: if (val && strlen(val)) {
120: if (psVal)
1.2 ! misho 121: strlcpy(psVal, val, *pnLen);
1.1 misho 122: if (pnLen)
123: *pnLen = strlen(val);
124: } else
125: def = IS_DEF;
126:
127: free(Shared);
128: return i | def;
129: }
130: }
131:
132: free(Shared);
133: return 0;
134: }
135:
136: /*
137: * sess_DelValue() Delete item from session shared memory
138: * @s = Session item
139: * @csAttr = Attribute for erasing
140: * return: 0 Ok, -1 error: in parameter
141: */
142: int sess_DelValue(tagSess * __restrict s, const char *csAttr)
143: {
144: register int i;
145: int ret, attrlen;
146: char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE + 1];
147: char *peer, *p_brk;
148:
149: if (!s || !csAttr || !*csAttr)
150: return -1;
151: else
152: attrlen = strlen(csAttr);
153: Buffer = Shared = NULL;
154: memset(szAttr, 0, MAX_ATTRIBUTE + 1);
155: strncpy(szAttr, csAttr, MAX_ATTRIBUTE - 1);
156: strcat(szAttr, "=");
157:
158: Buffer = malloc(s->eom);
159: if (!Buffer) {
160: LOGERR;
161: return -1;
162: } else
163: memset(Buffer, 0, s->eom);
164: Shared = malloc(s->eom);
165: if (!Shared) {
166: LOGERR;
167: free(Buffer);
168: return -1;
169: } else {
170: DEC_SEMAPHORE(s, ret);
171: memcpy(Shared, s->addr, s->eom);
172: }
173:
1.2 ! misho 174: for (i = 1, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer;
! 175: i++, peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
1.1 misho 176: if (!strncmp(peer, csAttr, attrlen))
1.2 ! misho 177: if (peer[attrlen] == '=' || peer[attrlen] == *MEM_DELIM || !peer[attrlen] ||
1.1 misho 178: peer[attrlen] == '\r' || peer[attrlen] == '\n')
179: continue;
180:
181: strcat(Buffer, peer);
1.2 ! misho 182: strcat(Buffer, MEM_DELIM);
1.1 misho 183: }
184:
185: memset(s->addr, 0, s->eom);
186: memcpy(s->addr, Buffer, s->eom);
187:
188: if (s->type == SHARED_MAP)
189: msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
190:
191: ADD_SEMAPHORE(s, ret);
192: free(Shared);
193: free(Buffer);
194: return 0;
195: }
196:
197: /*
198: * sess_SetValue() Set item into session shared memory or update if find it
199: * @s = Session item
200: * @csAttr = Attribute
201: * @psVal = Value
202: * return: 0 nothing, -1 error: in parameter,
203: >0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF
204: */
205: int sess_SetValue(tagSess * __restrict s, const char *csAttr, const char *psVal)
206: {
207: register int i;
208: int upd, ret, def = IS_VAL;
209: char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE + 1];
210: char *peer, *p_brk;
211:
212: if (!s || !csAttr || !*csAttr)
213: return -1;
214: else
215: Buffer = Shared = NULL;
216: memset(szAttr, 0, MAX_ATTRIBUTE + 1);
217: strncpy(szAttr, csAttr, MAX_ATTRIBUTE - 1);
218: strcat(szAttr, "=");
219:
220: Buffer = malloc(s->eom);
221: if (!Buffer) {
222: LOGERR;
223: return -1;
224: } else
225: memset(Buffer, 0, s->eom);
226: Shared = malloc(s->eom);
227: if (!Shared) {
228: LOGERR;
229: free(Buffer);
230: return -1;
231: } else {
232: DEC_SEMAPHORE(s, ret);
233: memcpy(Shared, s->addr, s->eom);
234: }
235:
1.2 ! misho 236: for (i = 1, upd = 0, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer;
! 237: i++, peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
1.1 misho 238: if (!strncmp(peer, szAttr, strlen(szAttr))) {
239: upd++;
240: if (psVal) {
241: strcat(Buffer, szAttr);
242: strcat(Buffer, psVal);
1.2 ! misho 243: strcat(Buffer, MEM_DELIM);
1.1 misho 244: } else {
245: strcat(Buffer, csAttr);
1.2 ! misho 246: strcat(Buffer, MEM_DELIM);
1.1 misho 247: def = IS_DEF;
248: }
249: continue;
250: }
251:
252: strcat(Buffer, peer);
1.2 ! misho 253: strcat(Buffer, MEM_DELIM);
1.1 misho 254: }
255:
256: if (!upd) {
257: if (psVal) {
258: strcat(Buffer, szAttr);
259: strcat(Buffer, psVal);
1.2 ! misho 260: strcat(Buffer, MEM_DELIM);
1.1 misho 261: } else {
262: strcat(Buffer, csAttr);
1.2 ! misho 263: strcat(Buffer, MEM_DELIM);
1.1 misho 264: def = IS_DEF;
265: }
266: def |= IS_ADD;
267: }
268:
269: memset(s->addr, 0, s->eom);
270: memcpy(s->addr, Buffer, s->eom);
271:
272: if (s->type == SHARED_MAP)
273: msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
274:
275: ADD_SEMAPHORE(s, ret);
276: free(Shared);
277: free(Buffer);
278: return upd | def;
279: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>