Annotation of libaitsess/src/sess.c, revision 1.1.1.1
1.1 misho 1: #include "global.h"
2: #include "aitsess.h"
3:
4:
5: /*
6: * sess_GetValue() Get value from session shared memory from attribute
7: * @s = Session item
8: * @csAttr = Attribute for search
9: * @psVal = Return string buffer
10: * @pnLen = Length of return string buffer,
11: // *{pnLen} input is max_size of buffer & output is really taken bytes
12: * return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF
13: */
14: int sess_GetValue(tagSess * __restrict s, const char *csAttr, char *psVal, int *pnLen)
15: {
16: register int i;
17: int def = IS_VAL;
18: char *Shared = NULL;
19: char *peer, *p_brk, *a_brk, *attr, *val;
20:
21: if (!s || !csAttr || !*csAttr)
22: return -1;
23: if (psVal) {
24: if (pnLen && *pnLen > 0)
25: memset(psVal, 0, *pnLen);
26: else
27: return -1;
28: }
29:
30: // allocated memory & mirrored shared memory into this
31: Shared = malloc(s->eom);
32: if (!Shared) {
33: LOGERR;
34: return -1;
35: } else
36: memcpy(Shared, s->addr, s->eom);
37:
38: for (i = 1, peer = strtok_r(Shared, ":\r\n", &p_brk); peer;
39: i++, peer = strtok_r(NULL, ":\r\n", &p_brk)) {
40: attr = strtok_r(peer, "=\r\n", &a_brk);
41: if (attr && !strncmp(attr, csAttr, MAX_ATTRIBUTE)) {
42: val = strtok_r(NULL, "=\r\n", &a_brk);
43: if (val && strlen(val)) {
44: if (psVal)
45: strncpy(psVal, val, *pnLen);
46: if (pnLen)
47: *pnLen = strlen(val);
48: } else
49: def = IS_DEF;
50:
51: free(Shared);
52: return i | def;
53: }
54: }
55:
56: free(Shared);
57: return 0;
58: }
59:
60: /*
61: * sess_DelValue() Delete item from session shared memory
62: * @s = Session item
63: * @csAttr = Attribute for erasing
64: * return: 0 Ok, -1 error: in parameter
65: */
66: int sess_DelValue(tagSess * __restrict s, const char *csAttr)
67: {
68: register int i;
69: int ret, attrlen;
70: char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE + 1];
71: char *peer, *p_brk;
72:
73: if (!s || !csAttr || !*csAttr)
74: return -1;
75: else
76: attrlen = strlen(csAttr);
77: Buffer = Shared = NULL;
78: memset(szAttr, 0, MAX_ATTRIBUTE + 1);
79: strncpy(szAttr, csAttr, MAX_ATTRIBUTE - 1);
80: strcat(szAttr, "=");
81:
82: Buffer = malloc(s->eom);
83: if (!Buffer) {
84: LOGERR;
85: return -1;
86: } else
87: memset(Buffer, 0, s->eom);
88: Shared = malloc(s->eom);
89: if (!Shared) {
90: LOGERR;
91: free(Buffer);
92: return -1;
93: } else {
94: DEC_SEMAPHORE(s, ret);
95: memcpy(Shared, s->addr, s->eom);
96: }
97:
98: for (i = 1, peer = strtok_r(Shared, ":\r\n", &p_brk); peer;
99: i++, peer = strtok_r(NULL, ":\r\n", &p_brk)) {
100: if (!strncmp(peer, csAttr, attrlen))
101: if (peer[attrlen] == '=' || peer[attrlen] == ':' || !peer[attrlen] ||
102: peer[attrlen] == '\r' || peer[attrlen] == '\n')
103: continue;
104:
105: strcat(Buffer, peer);
106: strcat(Buffer, "\n");
107: }
108:
109: memset(s->addr, 0, s->eom);
110: memcpy(s->addr, Buffer, s->eom);
111:
112: if (s->type == SHARED_MAP)
113: msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
114:
115: ADD_SEMAPHORE(s, ret);
116: free(Shared);
117: free(Buffer);
118: return 0;
119: }
120:
121: /*
122: * sess_SetValue() Set item into session shared memory or update if find it
123: * @s = Session item
124: * @csAttr = Attribute
125: * @psVal = Value
126: * return: 0 nothing, -1 error: in parameter,
127: >0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF
128: */
129: int sess_SetValue(tagSess * __restrict s, const char *csAttr, const char *psVal)
130: {
131: register int i;
132: int upd, ret, def = IS_VAL;
133: char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE + 1];
134: char *peer, *p_brk;
135:
136: if (!s || !csAttr || !*csAttr)
137: return -1;
138: else
139: Buffer = Shared = NULL;
140: memset(szAttr, 0, MAX_ATTRIBUTE + 1);
141: strncpy(szAttr, csAttr, MAX_ATTRIBUTE - 1);
142: strcat(szAttr, "=");
143:
144: Buffer = malloc(s->eom);
145: if (!Buffer) {
146: LOGERR;
147: return -1;
148: } else
149: memset(Buffer, 0, s->eom);
150: Shared = malloc(s->eom);
151: if (!Shared) {
152: LOGERR;
153: free(Buffer);
154: return -1;
155: } else {
156: DEC_SEMAPHORE(s, ret);
157: memcpy(Shared, s->addr, s->eom);
158: }
159:
160: for (i = 1, upd = 0, peer = strtok_r(Shared, ":\r\n", &p_brk); peer;
161: i++, peer = strtok_r(NULL, ":\r\n", &p_brk)) {
162: if (!strncmp(peer, szAttr, strlen(szAttr))) {
163: upd++;
164: if (psVal) {
165: strcat(Buffer, szAttr);
166: strcat(Buffer, psVal);
167: strcat(Buffer, "\n");
168: } else {
169: strcat(Buffer, csAttr);
170: strcat(Buffer, "\n");
171: def = IS_DEF;
172: }
173: continue;
174: }
175:
176: strcat(Buffer, peer);
177: strcat(Buffer, "\n");
178: }
179:
180: if (!upd) {
181: if (psVal) {
182: strcat(Buffer, szAttr);
183: strcat(Buffer, psVal);
184: strcat(Buffer, "\n");
185: } else {
186: strcat(Buffer, csAttr);
187: strcat(Buffer, "\n");
188: def = IS_DEF;
189: }
190: def |= IS_ADD;
191: }
192:
193: memset(s->addr, 0, s->eom);
194: memcpy(s->addr, Buffer, s->eom);
195:
196: if (s->type == SHARED_MAP)
197: msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
198:
199: ADD_SEMAPHORE(s, ret);
200: free(Shared);
201: free(Buffer);
202: return upd | def;
203: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>