Annotation of libaitsess/src/sess.c, revision 1.2.2.3
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 $
1.2.2.3 ! misho 6: * $Id: sess.c,v 1.2.2.2 2011/05/10 21:06:13 misho Exp $
1.2 misho 7: *
1.2.2.1 misho 8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
1.1 misho 46: #include "global.h"
47: #include "aitsess.h"
48:
49:
50: /*
1.2 misho 51: * sess_FreeValues() Free all values from value array allocated from sess_GetValues()
52: * @ppsVals = Array strings
53: * return: none
54: */
1.2.2.2 misho 55: inline void
56: sess_FreeValues(char *** __restrict ppsVals)
1.2 misho 57: {
58: char **ptr;
59:
1.2.2.3 ! misho 60: assert(ppsVals);
! 61: if (!ppsVals)
! 62: return;
! 63:
1.2 misho 64: for (ptr = *ppsVals; *ptr; ptr++)
65: free(*ptr);
66: free(*ppsVals);
67: *ppsVals = NULL;
68: }
69:
70: /*
71: * sess_GetValues() Get all values from session shared memory
72: * @s = Session item
73: * @ppsVals = Return array strings
74: * return: -1 error: in parameter, !=-1 count of returned strings in ppsVals (must be free after use!)
75: */
1.2.2.2 misho 76: int
77: sess_GetValues(tagSess * __restrict s, char ***ppsVals)
1.2 misho 78: {
79: register int i;
80: char **valz, *Shared = NULL;
81: char *peer, *p_brk;
82:
83: if (!s || !ppsVals)
84: return -1;
85: valz = malloc(sizeof(caddr_t));
86: if (!valz) {
87: LOGERR;
88: return -1;
89: } else
90: *valz = NULL;
91:
92: // allocated memory & mirrored shared memory into this
93: Shared = malloc(s->eom);
94: if (!Shared) {
95: LOGERR;
96: free(valz);
97: return -1;
98: } else
99: memcpy(Shared, s->addr, s->eom);
100:
101: for (i = 0, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer;
102: peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
103: if (!strchr(peer, '='))
104: continue;
105: else
106: i++;
107:
108: valz = realloc(valz, (i + 1) * sizeof(caddr_t));
109: if (!valz) {
110: LOGERR;
111: free(Shared);
112: return -1;
113: } else
114: valz[i] = NULL;
115:
116: valz[i - 1] = strdup(peer);
117: }
118:
119: free(Shared);
120: *ppsVals = valz;
121: return i;
122: }
123:
124: /*
1.1 misho 125: * sess_GetValue() Get value from session shared memory from attribute
126: * @s = Session item
127: * @csAttr = Attribute for search
128: * @psVal = Return string buffer
129: * @pnLen = Length of return string buffer,
130: // *{pnLen} input is max_size of buffer & output is really taken bytes
131: * return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF
132: */
1.2.2.2 misho 133: int
134: sess_GetValue(tagSess * __restrict s, const char *csAttr, char *psVal, int *pnLen)
1.1 misho 135: {
136: register int i;
137: int def = IS_VAL;
138: char *Shared = NULL;
139: char *peer, *p_brk, *a_brk, *attr, *val;
140:
141: if (!s || !csAttr || !*csAttr)
142: return -1;
143: if (psVal) {
144: if (pnLen && *pnLen > 0)
145: memset(psVal, 0, *pnLen);
146: else
147: return -1;
148: }
149:
150: // allocated memory & mirrored shared memory into this
151: Shared = malloc(s->eom);
152: if (!Shared) {
153: LOGERR;
154: return -1;
155: } else
156: memcpy(Shared, s->addr, s->eom);
157:
1.2 misho 158: for (i = 1, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer;
159: i++, peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
1.1 misho 160: attr = strtok_r(peer, "=\r\n", &a_brk);
161: if (attr && !strncmp(attr, csAttr, MAX_ATTRIBUTE)) {
162: val = strtok_r(NULL, "=\r\n", &a_brk);
163: if (val && strlen(val)) {
164: if (psVal)
1.2 misho 165: strlcpy(psVal, val, *pnLen);
1.1 misho 166: if (pnLen)
167: *pnLen = strlen(val);
168: } else
169: def = IS_DEF;
170:
171: free(Shared);
172: return i | def;
173: }
174: }
175:
176: free(Shared);
177: return 0;
178: }
179:
180: /*
181: * sess_DelValue() Delete item from session shared memory
182: * @s = Session item
183: * @csAttr = Attribute for erasing
184: * return: 0 Ok, -1 error: in parameter
185: */
1.2.2.2 misho 186: int
187: sess_DelValue(tagSess * __restrict s, const char *csAttr)
1.1 misho 188: {
189: register int i;
190: int ret, attrlen;
191: char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE + 1];
192: char *peer, *p_brk;
193:
194: if (!s || !csAttr || !*csAttr)
195: return -1;
196: else
197: attrlen = strlen(csAttr);
198: Buffer = Shared = NULL;
199: memset(szAttr, 0, MAX_ATTRIBUTE + 1);
200: strncpy(szAttr, csAttr, MAX_ATTRIBUTE - 1);
201: strcat(szAttr, "=");
202:
203: Buffer = malloc(s->eom);
204: if (!Buffer) {
205: LOGERR;
206: return -1;
207: } else
208: memset(Buffer, 0, s->eom);
209: Shared = malloc(s->eom);
210: if (!Shared) {
211: LOGERR;
212: free(Buffer);
213: return -1;
214: } else {
215: DEC_SEMAPHORE(s, ret);
216: memcpy(Shared, s->addr, s->eom);
217: }
218:
1.2 misho 219: for (i = 1, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer;
220: i++, peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
1.1 misho 221: if (!strncmp(peer, csAttr, attrlen))
1.2 misho 222: if (peer[attrlen] == '=' || peer[attrlen] == *MEM_DELIM || !peer[attrlen] ||
1.1 misho 223: peer[attrlen] == '\r' || peer[attrlen] == '\n')
224: continue;
225:
226: strcat(Buffer, peer);
1.2 misho 227: strcat(Buffer, MEM_DELIM);
1.1 misho 228: }
229:
230: memset(s->addr, 0, s->eom);
231: memcpy(s->addr, Buffer, s->eom);
232:
233: if (s->type == SHARED_MAP)
234: msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
235:
236: ADD_SEMAPHORE(s, ret);
237: free(Shared);
238: free(Buffer);
239: return 0;
240: }
241:
242: /*
243: * sess_SetValue() Set item into session shared memory or update if find it
244: * @s = Session item
245: * @csAttr = Attribute
246: * @psVal = Value
247: * return: 0 nothing, -1 error: in parameter,
248: >0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF
249: */
1.2.2.2 misho 250: int
251: sess_SetValue(tagSess * __restrict s, const char *csAttr, const char *psVal)
1.1 misho 252: {
253: register int i;
254: int upd, ret, def = IS_VAL;
255: char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE + 1];
256: char *peer, *p_brk;
257:
258: if (!s || !csAttr || !*csAttr)
259: return -1;
260: else
261: Buffer = Shared = NULL;
262: memset(szAttr, 0, MAX_ATTRIBUTE + 1);
263: strncpy(szAttr, csAttr, MAX_ATTRIBUTE - 1);
264: strcat(szAttr, "=");
265:
266: Buffer = malloc(s->eom);
267: if (!Buffer) {
268: LOGERR;
269: return -1;
270: } else
271: memset(Buffer, 0, s->eom);
272: Shared = malloc(s->eom);
273: if (!Shared) {
274: LOGERR;
275: free(Buffer);
276: return -1;
277: } else {
278: DEC_SEMAPHORE(s, ret);
279: memcpy(Shared, s->addr, s->eom);
280: }
281:
1.2 misho 282: for (i = 1, upd = 0, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer;
283: i++, peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
1.1 misho 284: if (!strncmp(peer, szAttr, strlen(szAttr))) {
285: upd++;
286: if (psVal) {
287: strcat(Buffer, szAttr);
288: strcat(Buffer, psVal);
1.2 misho 289: strcat(Buffer, MEM_DELIM);
1.1 misho 290: } else {
291: strcat(Buffer, csAttr);
1.2 misho 292: strcat(Buffer, MEM_DELIM);
1.1 misho 293: def = IS_DEF;
294: }
295: continue;
296: }
297:
298: strcat(Buffer, peer);
1.2 misho 299: strcat(Buffer, MEM_DELIM);
1.1 misho 300: }
301:
302: if (!upd) {
303: if (psVal) {
304: strcat(Buffer, szAttr);
305: strcat(Buffer, psVal);
1.2 misho 306: strcat(Buffer, MEM_DELIM);
1.1 misho 307: } else {
308: strcat(Buffer, csAttr);
1.2 misho 309: strcat(Buffer, MEM_DELIM);
1.1 misho 310: def = IS_DEF;
311: }
312: def |= IS_ADD;
313: }
314:
315: memset(s->addr, 0, s->eom);
316: memcpy(s->addr, Buffer, s->eom);
317:
318: if (s->type == SHARED_MAP)
319: msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
320:
321: ADD_SEMAPHORE(s, ret);
322: free(Shared);
323: free(Buffer);
324: return upd | def;
325: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>