Annotation of libaitsess/src/sess.c, revision 1.3.2.1
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.3.2.1 ! misho 6: * $Id: sess.c,v 1.3 2011/09/07 13:11:56 misho Exp $
1.2 misho 7: *
1.3 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.3 misho 55: inline void
56: sess_FreeValues(char *** __restrict ppsVals)
1.2 misho 57: {
58: char **ptr;
59:
1.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.3 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.3 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.3 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.3 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: }
1.3.2.1 ! misho 326:
! 327:
! 328: /*
! 329: * sess_prepareSession() Attach to shared memory and de-marshaling data
! 330: * @s = Session
! 331: * @useDirect = Use direct shared memory if !=0 or snapshot of data to array
! 332: * return: NULL error or no data, !=NULL array with variables,
! 333: * after use must free resources with sess_doneSession()
! 334: */
! 335: array_t *
! 336: sess_prepareSession(tagSess * __restrict s, u_char useDirect)
! 337: {
! 338: array_t *arr = NULL;
! 339: sess_hdr_t *hdr;
! 340: int ret;
! 341:
! 342: assert(s);
! 343: if (!s) {
! 344: sess_SetErr(EINVAL, "Error:: invalid argument\n");
! 345: return NULL;
! 346: }
! 347:
! 348: ATTACH_MEMORY(s);
! 349: if (!s->addr)
! 350: return NULL;
! 351: else
! 352: hdr = (sess_hdr_t*) s->addr;
! 353: if (hdr->hdr_magic != SESS_AIT_MAGIC) {
! 354: DETACH_MEMORY(s);
! 355:
! 356: sess_SetErr(EINVAL, "Error:: shared memory not contains values with proper format\n");
! 357: return NULL;
! 358: }
! 359:
! 360: DEC_SEMAPHORE(s, ret);
! 361: s->zcopy = useDirect;
! 362: arr = io_map2vals(s->addr + sizeof(sess_hdr_t), s->eom - sizeof(sess_hdr_t),
! 363: hdr->hdr_argc, useDirect);
! 364: ADD_SEMAPHORE(s, ret);
! 365:
! 366: if (!s->zcopy)
! 367: DETACH_MEMORY(s);
! 368: return arr;
! 369: }
! 370:
! 371: /*
! 372: * sess_doneSession() Free resources allocated with sess_prepareSession()
! 373: * @s = Session
! 374: * @arr = Array with variables for free
! 375: * return: none
! 376: */
! 377: void
! 378: sess_doneSession(tagSess * __restrict s, array_t ** __restrict arr)
! 379: {
! 380: assert(s);
! 381: if (!s) {
! 382: sess_SetErr(EINVAL, "Error:: invalid argument\n");
! 383: return;
! 384: }
! 385:
! 386: if (!s->zcopy)
! 387: io_arrayFree(*arr);
! 388: else
! 389: DETACH_MEMORY(s);
! 390: io_arrayDestroy(arr);
! 391: }
! 392:
! 393: /*
! 394: * sess_commitSession() Commit data to shared memory
! 395: * @s = Session
! 396: * @arr = Array with variables for save
! 397: * return -1 error or !=-1 size of stored variables into shared memory
! 398: */
! 399: int
! 400: sess_commitSession(tagSess * __restrict s, array_t * __restrict arr)
! 401: {
! 402: sess_hdr_t *hdr;
! 403: int rs, ret = 0;
! 404:
! 405: assert(s && arr);
! 406: if (!s || !arr) {
! 407: sess_SetErr(EINVAL, "Error:: invalid argument\n");
! 408: return -1;
! 409: }
! 410:
! 411: if (!s->zcopy)
! 412: ATTACH_MEMORY(s);
! 413: if (!s->addr) {
! 414: if (!s->zcopy)
! 415: DETACH_MEMORY(s);
! 416:
! 417: return -1;
! 418: } else
! 419: hdr = (sess_hdr_t*) s->addr;
! 420: if (hdr->hdr_magic != SESS_AIT_MAGIC) {
! 421: if (!s->zcopy)
! 422: DETACH_MEMORY(s);
! 423:
! 424: sess_SetErr(EINVAL, "Error:: shared memory not contains values with proper format\n");
! 425: return -1;
! 426: }
! 427:
! 428: DEC_SEMAPHORE(s, rs);
! 429: if ((ret = io_vals2map(s->addr + sizeof(sess_hdr_t), s->eom - sizeof(sess_hdr_t), arr)) != -1) {
! 430: hdr->hdr_argc = io_arraySize(arr);
! 431: ret += sizeof(sess_hdr_t);
! 432: }
! 433: ADD_SEMAPHORE(s, rs);
! 434:
! 435: if (!s->zcopy)
! 436: DETACH_MEMORY(s);
! 437: return ret;
! 438: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>