Annotation of libaitsess/src/sess.c, revision 1.2.2.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 $
1.2.2.2 ! misho 6: * $Id: sess.c,v 1.2.2.1 2011/04/30 22:02:59 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:
60: for (ptr = *ppsVals; *ptr; ptr++)
61: free(*ptr);
62: free(*ppsVals);
63: *ppsVals = NULL;
64: }
65:
66: /*
67: * sess_GetValues() Get all values from session shared memory
68: * @s = Session item
69: * @ppsVals = Return array strings
70: * return: -1 error: in parameter, !=-1 count of returned strings in ppsVals (must be free after use!)
71: */
1.2.2.2 ! misho 72: int
! 73: sess_GetValues(tagSess * __restrict s, char ***ppsVals)
1.2 misho 74: {
75: register int i;
76: char **valz, *Shared = NULL;
77: char *peer, *p_brk;
78:
79: if (!s || !ppsVals)
80: return -1;
81: valz = malloc(sizeof(caddr_t));
82: if (!valz) {
83: LOGERR;
84: return -1;
85: } else
86: *valz = NULL;
87:
88: // allocated memory & mirrored shared memory into this
89: Shared = malloc(s->eom);
90: if (!Shared) {
91: LOGERR;
92: free(valz);
93: return -1;
94: } else
95: memcpy(Shared, s->addr, s->eom);
96:
97: for (i = 0, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer;
98: peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
99: if (!strchr(peer, '='))
100: continue;
101: else
102: i++;
103:
104: valz = realloc(valz, (i + 1) * sizeof(caddr_t));
105: if (!valz) {
106: LOGERR;
107: free(Shared);
108: return -1;
109: } else
110: valz[i] = NULL;
111:
112: valz[i - 1] = strdup(peer);
113: }
114:
115: free(Shared);
116: *ppsVals = valz;
117: return i;
118: }
119:
120: /*
1.1 misho 121: * sess_GetValue() Get value from session shared memory from attribute
122: * @s = Session item
123: * @csAttr = Attribute for search
124: * @psVal = Return string buffer
125: * @pnLen = Length of return string buffer,
126: // *{pnLen} input is max_size of buffer & output is really taken bytes
127: * return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF
128: */
1.2.2.2 ! misho 129: int
! 130: sess_GetValue(tagSess * __restrict s, const char *csAttr, char *psVal, int *pnLen)
1.1 misho 131: {
132: register int i;
133: int def = IS_VAL;
134: char *Shared = NULL;
135: char *peer, *p_brk, *a_brk, *attr, *val;
136:
137: if (!s || !csAttr || !*csAttr)
138: return -1;
139: if (psVal) {
140: if (pnLen && *pnLen > 0)
141: memset(psVal, 0, *pnLen);
142: else
143: return -1;
144: }
145:
146: // allocated memory & mirrored shared memory into this
147: Shared = malloc(s->eom);
148: if (!Shared) {
149: LOGERR;
150: return -1;
151: } else
152: memcpy(Shared, s->addr, s->eom);
153:
1.2 misho 154: for (i = 1, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer;
155: i++, peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
1.1 misho 156: attr = strtok_r(peer, "=\r\n", &a_brk);
157: if (attr && !strncmp(attr, csAttr, MAX_ATTRIBUTE)) {
158: val = strtok_r(NULL, "=\r\n", &a_brk);
159: if (val && strlen(val)) {
160: if (psVal)
1.2 misho 161: strlcpy(psVal, val, *pnLen);
1.1 misho 162: if (pnLen)
163: *pnLen = strlen(val);
164: } else
165: def = IS_DEF;
166:
167: free(Shared);
168: return i | def;
169: }
170: }
171:
172: free(Shared);
173: return 0;
174: }
175:
176: /*
177: * sess_DelValue() Delete item from session shared memory
178: * @s = Session item
179: * @csAttr = Attribute for erasing
180: * return: 0 Ok, -1 error: in parameter
181: */
1.2.2.2 ! misho 182: int
! 183: sess_DelValue(tagSess * __restrict s, const char *csAttr)
1.1 misho 184: {
185: register int i;
186: int ret, attrlen;
187: char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE + 1];
188: char *peer, *p_brk;
189:
190: if (!s || !csAttr || !*csAttr)
191: return -1;
192: else
193: attrlen = strlen(csAttr);
194: Buffer = Shared = NULL;
195: memset(szAttr, 0, MAX_ATTRIBUTE + 1);
196: strncpy(szAttr, csAttr, MAX_ATTRIBUTE - 1);
197: strcat(szAttr, "=");
198:
199: Buffer = malloc(s->eom);
200: if (!Buffer) {
201: LOGERR;
202: return -1;
203: } else
204: memset(Buffer, 0, s->eom);
205: Shared = malloc(s->eom);
206: if (!Shared) {
207: LOGERR;
208: free(Buffer);
209: return -1;
210: } else {
211: DEC_SEMAPHORE(s, ret);
212: memcpy(Shared, s->addr, s->eom);
213: }
214:
1.2 misho 215: for (i = 1, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer;
216: i++, peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
1.1 misho 217: if (!strncmp(peer, csAttr, attrlen))
1.2 misho 218: if (peer[attrlen] == '=' || peer[attrlen] == *MEM_DELIM || !peer[attrlen] ||
1.1 misho 219: peer[attrlen] == '\r' || peer[attrlen] == '\n')
220: continue;
221:
222: strcat(Buffer, peer);
1.2 misho 223: strcat(Buffer, MEM_DELIM);
1.1 misho 224: }
225:
226: memset(s->addr, 0, s->eom);
227: memcpy(s->addr, Buffer, s->eom);
228:
229: if (s->type == SHARED_MAP)
230: msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
231:
232: ADD_SEMAPHORE(s, ret);
233: free(Shared);
234: free(Buffer);
235: return 0;
236: }
237:
238: /*
239: * sess_SetValue() Set item into session shared memory or update if find it
240: * @s = Session item
241: * @csAttr = Attribute
242: * @psVal = Value
243: * return: 0 nothing, -1 error: in parameter,
244: >0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF
245: */
1.2.2.2 ! misho 246: int
! 247: sess_SetValue(tagSess * __restrict s, const char *csAttr, const char *psVal)
1.1 misho 248: {
249: register int i;
250: int upd, ret, def = IS_VAL;
251: char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE + 1];
252: char *peer, *p_brk;
253:
254: if (!s || !csAttr || !*csAttr)
255: return -1;
256: else
257: Buffer = Shared = NULL;
258: memset(szAttr, 0, MAX_ATTRIBUTE + 1);
259: strncpy(szAttr, csAttr, MAX_ATTRIBUTE - 1);
260: strcat(szAttr, "=");
261:
262: Buffer = malloc(s->eom);
263: if (!Buffer) {
264: LOGERR;
265: return -1;
266: } else
267: memset(Buffer, 0, s->eom);
268: Shared = malloc(s->eom);
269: if (!Shared) {
270: LOGERR;
271: free(Buffer);
272: return -1;
273: } else {
274: DEC_SEMAPHORE(s, ret);
275: memcpy(Shared, s->addr, s->eom);
276: }
277:
1.2 misho 278: for (i = 1, upd = 0, peer = strtok_r(Shared, MEM_DELIM"\r\n", &p_brk); peer;
279: i++, peer = strtok_r(NULL, MEM_DELIM"\r\n", &p_brk)) {
1.1 misho 280: if (!strncmp(peer, szAttr, strlen(szAttr))) {
281: upd++;
282: if (psVal) {
283: strcat(Buffer, szAttr);
284: strcat(Buffer, psVal);
1.2 misho 285: strcat(Buffer, MEM_DELIM);
1.1 misho 286: } else {
287: strcat(Buffer, csAttr);
1.2 misho 288: strcat(Buffer, MEM_DELIM);
1.1 misho 289: def = IS_DEF;
290: }
291: continue;
292: }
293:
294: strcat(Buffer, peer);
1.2 misho 295: strcat(Buffer, MEM_DELIM);
1.1 misho 296: }
297:
298: if (!upd) {
299: if (psVal) {
300: strcat(Buffer, szAttr);
301: strcat(Buffer, psVal);
1.2 misho 302: strcat(Buffer, MEM_DELIM);
1.1 misho 303: } else {
304: strcat(Buffer, csAttr);
1.2 misho 305: strcat(Buffer, MEM_DELIM);
1.1 misho 306: def = IS_DEF;
307: }
308: def |= IS_ADD;
309: }
310:
311: memset(s->addr, 0, s->eom);
312: memcpy(s->addr, Buffer, s->eom);
313:
314: if (s->type == SHARED_MAP)
315: msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
316:
317: ADD_SEMAPHORE(s, ret);
318: free(Shared);
319: free(Buffer);
320: return upd | def;
321: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>