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