Annotation of libaitio/src/sess.c, revision 1.1.2.3
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.2.3 ! misho 6: * $Id: sess.c,v 1.1.2.2 2013/03/13 14:25:32 misho Exp $
1.1.2.1 misho 7: *
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, 2013
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: */
46: #include "global.h"
47:
48:
49: /*
1.1.2.2 misho 50: * sess_initSession() - Initializing session structure,
51: * if session file not exists creating with specified tech
52: *
53: * @id = Technology using in session. SHARED_IPC IPC tech or SHARED_MAP BSD MemoryMap tech
54: * @csFName = Session filename for build key and identified
55: * @Sess = Session item, if =NULL allocate memory for session after use must be free!
56: * return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded
57: */
58: int
59: sess_initSession(int id, const char *csFName, ait_sess_t ** __restrict Sess)
60: {
61: int h, ret = 0;
62: char szStr[STRSIZ];
63:
64: if (!csFName) {
65: sess_SetErr(EINVAL, "Filename is NULL");
66: return -1;
67: }
68: if (id < SHARED_UNKNOWN || id > SHARED_MAP) {
69: sess_SetErr(EPROTONOSUPPORT, "Session type not supported");
70: return -1;
71: }
72:
73: if (!*Sess) {
74: *Sess = e_malloc(sizeof(ait_sess_t));
75: if (!*Sess) {
76: LOGERR;
77: return -1;
78: }
79: }
80: memset(*Sess, 0, sizeof(ait_sess_t));
81: strlcpy((*Sess)->name, csFName, sizeof (*Sess)->name);
82:
1.1.2.3 ! misho 83: h = open((*Sess)->name, O_WRONLY | O_CREAT | O_EXCL, SESS_MEM_MODE);
1.1.2.2 misho 84: if (h == -1) {
85: if (errno != EEXIST) {
86: LOGERR;
87: e_free(*Sess);
88: return -1;
89: }
90: /* If key file exist, session already connected */
91: h = open((*Sess)->name, O_RDONLY);
92: if (h == -1) {
93: LOGERR;
94: e_free(*Sess);
95: return -1;
96: }
97: ret = read(h, szStr, sizeof szStr);
98: if (ret == -1) {
99: LOGERR;
100: close(h);
101: e_free(*Sess);
102: return -1;
103: }
104: if (!strncmp(szStr, "IPC@", 4) && id == SHARED_IPC) {
105: ret = 1;
106:
107: (*Sess)->sess.create = (int (*)(int, long, void*, ...)) ipc_createSession;
108: (*Sess)->sess.destroy = (void (*)(void*)) ipc_destroySession;
109: (*Sess)->sess.attach = (void* (*)(void*, void*)) ipc_attachSession;
110: (*Sess)->sess.detach = (void (*)(void*)) ipc_detachSession;
111: (*Sess)->sess.notSem = (void (*)(void*)) ipc_notSemaphore;
112: (*Sess)->sess.isSemOK = (int (*)(void*)) ipc_isSemaphoreOK;
113: (*Sess)->sess.incSem = (int (*)(void*)) ipc_incSemaphore;
114: (*Sess)->sess.decSem = (int (*)(void*)) ipc_decSemaphore;
115: } else if (!strncmp(szStr, "MAP@", 4) && id == SHARED_MAP) {
116: ret = 1;
117:
118: (*Sess)->sess.create = (int (*)(int, long, void*, ...)) map_createSession;
119: (*Sess)->sess.destroy = (void (*)(void*)) map_destroySession;
120: (*Sess)->sess.attach = (void* (*)(void*, void*)) map_attachSession;
121: (*Sess)->sess.detach = (void (*)(void*)) map_detachSession;
122: (*Sess)->sess.notSem = (void (*)(void*)) map_notSemaphore;
123: (*Sess)->sess.isSemOK = (int (*)(void*)) map_isSemaphoreOK;
124: (*Sess)->sess.incSem = (int (*)(void*)) map_incSemaphore;
125: (*Sess)->sess.decSem = (int (*)(void*)) map_decSemaphore;
126: } else {
127: sess_SetErr(EPROTONOSUPPORT,
128: "Session type not supported or wrong session type");
129: close(h);
130: e_free(*Sess);
131: return -1;
132: }
133: /* key found */
134: } else {
135: /* Build new key & new session */
136: if (id == SHARED_IPC) {
137: strlcpy(szStr, "IPC@", sizeof szStr);
138:
139: (*Sess)->sess.create = (int (*)(int, long, void*, ...)) ipc_createSession;
140: (*Sess)->sess.destroy = (void (*)(void*)) ipc_destroySession;
141: (*Sess)->sess.attach = (void* (*)(void*, void*)) ipc_attachSession;
142: (*Sess)->sess.detach = (void (*)(void*)) ipc_detachSession;
143: (*Sess)->sess.notSem = (void (*)(void*)) ipc_notSemaphore;
144: (*Sess)->sess.isSemOK = (int (*)(void*)) ipc_isSemaphoreOK;
145: (*Sess)->sess.incSem = (int (*)(void*)) ipc_incSemaphore;
146: (*Sess)->sess.decSem = (int (*)(void*)) ipc_decSemaphore;
147: } else if (id == SHARED_MAP) {
148: strlcpy(szStr, "MAP@", sizeof szStr);
149:
150: (*Sess)->sess.create = (int (*)(int, long, void*, ...)) map_createSession;
151: (*Sess)->sess.destroy = (void (*)(void*)) map_destroySession;
152: (*Sess)->sess.attach = (void* (*)(void*, void*)) map_attachSession;
153: (*Sess)->sess.detach = (void (*)(void*)) map_detachSession;
154: (*Sess)->sess.notSem = (void (*)(void*)) map_notSemaphore;
155: (*Sess)->sess.isSemOK = (int (*)(void*)) map_isSemaphoreOK;
156: (*Sess)->sess.incSem = (int (*)(void*)) map_incSemaphore;
157: (*Sess)->sess.decSem = (int (*)(void*)) map_decSemaphore;
158: } else {
159: sess_SetErr(EINVAL, "Session type must be specified");
160: close(h);
161: unlink(csFName);
162: e_free(*Sess);
163: return -1;
164: }
165: strlcat(szStr, "ELWIX_Session ("PACKAGE_STRING")\n", sizeof szStr);
166: write(h, szStr, strlen(szStr));
167:
168: ret = 0;
169: /* new key created */
170: }
171: close(h);
172:
173: (*Sess)->type = id;
174: (*Sess)->zcpy = (char) ret;
175: return ret;
176: }
177:
178: /*
179: * sess_freeSession() - Free allocated memory for session item and delete session file if present name
180: *
181: * @Sess = Session item
182: * return: none
183: */
184: void
185: sess_freeSession(ait_sess_t ** __restrict Sess)
186: {
187: if (!Sess || !(*Sess))
188: return;
189:
190: if ((*Sess)->addr)
191: DETACH_MEMORY(*Sess);
192:
193: /*
194: memset(&(*Sess)->sess, 0, sizeof (*Sess)->sess);
195:
196: (*Sess)->type = SHARED_UNKNOWN;
197: */
198:
199: e_free(*Sess);
200: *Sess = NULL;
201: }
202:
203:
204: /*
205: * map_createSession() - MMAP Created session and allocated resources
206: *
207: * @nSeed = Seed for securing key, if =-1 must add ready for use key
208: * @nSize = Allocated shared memory size in bytes
209: * @Sess = Session item
210: * @... = If nSeed == -1 add ready for use key value
211: * return: 0 Ok successful, -1 error: not allocated resources
212: */
213: int
214: map_createSession(int nSeed, long nSize, ait_sess_t * __restrict Sess, ...)
215: {
216: char szSName[2][FILENAME_MAX];
217: va_list lst;
218:
219: if (!Sess || !*Sess->name)
220: return -1;
221:
222: if (nSeed != -1) {
223: /* genkey */
224: Sess->key = ftok(Sess->name, nSeed);
225: if (Sess->key == -1) {
226: LOGERR;
227: return -1;
228: }
229: } else {
230: /* get key from va_args */
231: va_start(lst, Sess);
232: Sess->key = va_arg(lst, key_t);
233: va_end(lst);
234: }
235:
236: /* build semaphore & shared memory name */
237: memset(szSName, 0, sizeof szSName);
238: snprintf(szSName[0], MAX_SEMNAME, "/%X.ANS", (u_int) Sess->key);
239: #ifdef HAVE_SHM_OPEN
240: snprintf(szSName[1], FILENAME_MAX, "/%s-%x.ANM", Sess->name, (u_int) Sess->key);
241: #else
242: snprintf(szSName[1], FILENAME_MAX, "%s-%x.ANM", Sess->name, (u_int) Sess->key);
243: #endif
244:
245: /* create semaphore & add 1 */
1.1.2.3 ! misho 246: Sess->id.sid = sem_open(szSName[0], O_CREAT, SESS_MEM_MODE);
1.1.2.2 misho 247: if (Sess->id.sid == SEM_FAILED) {
248: LOGERR;
249: map_destroySession(Sess);
250: return -1;
251: }
252: /* if is new shared memory session, init sempahore with 1 */
253: if (!Sess->zcpy)
254: sem_post(Sess->id.sid);
255:
256: /* create file for shared memory storage */
257: #ifdef HAVE_SHM_OPEN
1.1.2.3 ! misho 258: Sess->mem.fd = shm_open(szSName[1], O_RDWR | O_CREAT, SESS_MEM_MODE);
1.1.2.2 misho 259: #else
1.1.2.3 ! misho 260: Sess->mem.fd = open(szSName[1], O_RDWR | O_CREAT, SESS_MEM_MODE);
1.1.2.2 misho 261: #endif
262: if (Sess->mem.fd == -1) {
263: LOGERR;
264: map_destroySession(Sess);
265: return -1;
266: }
267: if (!Sess->zcpy) {
268: #ifdef HAVE_SHM_OPEN
269: if (ftruncate(Sess->mem.fd, nSize) == -1) {
270: LOGERR;
271: map_destroySession(Sess);
272: return -1;
273: }
274: #else
275: /* if is new shared memory session, fill file with zeros */
276: if (lseek(Sess->mem.fd, nSize - 1, SEEK_SET) == -1) {
277: LOGERR;
278: map_destroySession(Sess);
279: return -1;
280: } else
281: write(Sess->mem.fd, "", 1);
282: lseek(Sess->mem.fd, 0, SEEK_SET);
283: #endif
284: }
285: Sess->eom = nSize;
286:
287: return (int) Sess->zcpy;
288: }
289:
290: /*
291: * map_destroySession() - MMAP free shared resources
292: *
293: * @Sess = Session item
294: * return: none
295: */
296: void
297: map_destroySession(ait_sess_t * __restrict Sess)
298: {
299: char szSName[2][FILENAME_MAX];
300:
301: if (!Sess || sess_isAttached(Sess) || !*Sess->name)
302: return;
303:
304: memset(szSName, 0, sizeof szSName);
305: snprintf(szSName[0], MAX_SEMNAME, "/%X.ANS", (u_int) Sess->key);
306: #ifdef HAVE_SHM_UNLINK
307: snprintf(szSName[1], FILENAME_MAX, "/%s-%x.ANM", Sess->name, (u_int) Sess->key);
308: #else
309: snprintf(szSName[1], FILENAME_MAX, "%s-%x.ANM", Sess->name, (u_int) Sess->key);
310: #endif
311:
312: if (Sess->id.sid != SEM_FAILED) {
313: sem_close(Sess->id.sid);
314: sem_unlink(szSName[0]);
315: }
316: if (Sess->mem.fd > 2) {
317: close(Sess->mem.fd);
318: #ifdef HAVE_SHM_UNLINK
319: shm_unlink(szSName[1]);
320: #else
321: unlink(szSName[1]);
322: #endif
323: }
324: unlink(Sess->name);
325: memset(Sess->name, 0, sizeof Sess->name);
326: Sess->eom ^= Sess->eom;
327: Sess->key ^= Sess->key;
328: }
329:
330: /*
331: * ipc_createSession() - IPC Created session and allocated resources
332: *
333: * @nSeed = Seed for securing key, if =-1 must add ready for use key
334: * @nSize = Allocated shared memory size in bytes
335: * @Sess = Session item
336: * @... = If nSeed == -1 add ready for use key value
337: * return: 0 Ok successful, -1 error: not allocated resources
338: */
339: int
340: ipc_createSession(int nSeed, long nSize, ait_sess_t * __restrict Sess, ...)
341: {
342: union semun sems;
343: va_list lst;
344:
345: if (!Sess || !*Sess->name)
346: return -1;
347:
348: if (nSeed != -1) {
349: /* genkey */
350: Sess->key = ftok(Sess->name, nSeed);
351: if (Sess->key == -1) {
352: LOGERR;
353: return -1;
354: }
355: } else {
356: /* get key from va_args */
357: va_start(lst, Sess);
358: Sess->key = va_arg(lst, key_t);
359: va_end(lst);
360: }
361:
362: /* create semaphore */
1.1.2.3 ! misho 363: Sess->id.semid = semget(Sess->key, 1, SESS_MEM_MODE | IPC_CREAT);
1.1.2.2 misho 364: if (Sess->id.semid == -1) {
365: LOGERR;
366: ipc_destroySession(Sess);
367: return -1;
368: }
369: /* if is new shared memory session, init sempahore with 1 */
370: if (!Sess->zcpy) {
371: sems.val = 1;
372: if (semctl(Sess->id.semid, 0, SETVAL, sems) == -1) {
373: LOGERR;
374: ipc_destroySession(Sess);
375: return -1;
376: }
377: }
378:
379: /* create shared memory object */
1.1.2.3 ! misho 380: Sess->mem.shmid = shmget(Sess->key, nSize, SESS_MEM_MODE | IPC_CREAT);
1.1.2.2 misho 381: if (Sess->mem.shmid == -1) {
382: LOGERR;
383: ipc_destroySession(Sess);
384: return -1;
385: } else
386: Sess->eom = nSize;
387:
388: return (int) Sess->zcpy;
389: }
390:
391: /*
392: * ipc_destroySession() - IPC free shared resources
393: *
394: * @Sess = Session item
395: * return: none
396: */
397: void
398: ipc_destroySession(ait_sess_t * __restrict Sess)
399: {
400: union semun sems;
401: struct shmid_ds ds;
402:
403: if (!Sess || sess_isAttached(Sess))
404: return;
405:
406: if (Sess->id.semid != -1)
407: semctl(Sess->id.semid, 0, IPC_RMID, &sems);
408: if (Sess->mem.shmid != -1)
409: shmctl(Sess->mem.shmid, IPC_RMID, &ds);
410: unlink(Sess->name);
411: memset(Sess->name, 0, sizeof Sess->name);
412: Sess->eom ^= Sess->eom;
413: Sess->key ^= Sess->key;
414: }
415:
416:
417: /*
418: * map_attachSession() - MMAP Attach to shared memory & return begin address
419: *
420: * @s = Session item
421: * @procMem = Custom start address (optionl) *default must be 0*
422: * return: NULL failed attach, !=NULL begin address of memory
423: */
424: void *
425: map_attachSession(ait_sess_t * __restrict s, void *procMem)
426: {
427: if (!s)
428: return NULL;
429:
430: sync();
431:
432: /* attach to memory */
433: s->addr = mmap(procMem, s->eom, PROT_READ | PROT_WRITE, MAP_SHARED, s->mem.fd, 0);
434: if (s->addr == MAP_FAILED) {
435: LOGERR;
436: s->addr = NULL;
437: }
438:
439: return s->addr;
440: }
441:
442: /*
443: * map_detachSession() - MMAP Detach from shared memory
444: *
445: * @s = Session item
446: * return: none
447: */
448: void
449: map_detachSession(ait_sess_t * __restrict s)
450: {
451: if (!s)
452: return;
453:
454: msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
455:
456: if (s->addr && s->eom) {
457: munmap(s->addr, s->eom);
458: s->addr = NULL;
459: }
460: }
461:
462: /*
463: * ipc_attachSession() - IPC Attach to shared memory & return begin address
464: *
465: * @s = Session item
466: * @procMem = Custom start address (optionl) *default must be 0*
467: * return: NULL failed attach, !=NULL begin address of memory
468: */
469: void *
470: ipc_attachSession(ait_sess_t * __restrict s, void *procMem)
471: {
472: if (!s)
473: return NULL;
474:
475: s->addr = shmat(s->mem.shmid, procMem, 0);
476: if (s->addr == (void*) -1) {
477: LOGERR;
478: s->addr = NULL;
479: }
480:
481: return s->addr;
482: }
483:
484: /*
485: * ipc_detachSession() - IPC Detach from shared memory
486: *
487: * @s = Session item
488: * return: none
489: */
490: void
491: ipc_detachSession(ait_sess_t * __restrict s)
492: {
493: if (!s)
494: return;
495:
496: if (s->addr) {
497: shmdt(s->addr);
498: s->addr = NULL;
499: }
500: }
501:
502: /*
503: * sess_isAttached() - Check for attached shared memory
504: *
505: * @s = Session item
506: * return: -1 null session item, 0 not attached, 1 attached memory
507: */
508: inline int
509: sess_isAttached(ait_sess_t * __restrict s)
510: {
511: if (!s)
512: return -1;
513:
514: return (s->addr ? 1 : 0);
515: }
516:
517:
518: /*
519: * map_notSemaphore() - MMAP negative block if semaphore isn`t signaled
520: *
521: * @s = Session item
522: * return: none
523: */
524: void
525: map_notSemaphore(ait_sess_t * __restrict s)
526: {
527: int i = -1;
528:
529: if (!s)
530: return;
531:
532: sem_getvalue(s->id.sid, &i);
533: while (i > 0) {
534: sem_wait(s->id.sid);
535: i--;
536: }
537: }
538:
539: /*
540: * map_isSemaphoreOK() - MMAP Check semaphore
541: *
542: * @s = Session item
543: * return: -1 error: can`t return semaphore, 0 = false, 1 = true
544: */
545: int
546: map_isSemaphoreOK(ait_sess_t * __restrict s)
547: {
548: int val = -1;
549:
550: if (!s)
551: return -1;
552:
553: sem_getvalue(s->id.sid, &val);
554: return (val ? 0 : 1);
555: }
556:
557: /*
558: * map_incSemaphore() - MMAP unblock semaphore, increment semaphore
559: *
560: * @s = Session item
561: * return: 0 Ok, -1 error: can`t increment
562: */
563: int
564: map_incSemaphore(ait_sess_t * __restrict s)
565: {
566: if (!s)
567: return -1;
568:
569: return sem_post(s->id.sid);
570: }
571:
572: /*
573: * map_decSemaphore() - MMAP block semaphore, decrement semaphore
574: *
575: * @s = Session item
576: * return: 0 Ok, -1 error: can`t decrement
577: */
578: int
579: map_decSemaphore(ait_sess_t * __restrict s)
580: {
581: if (!s)
582: return -1;
583:
584: return sem_wait(s->id.sid);
585: }
586:
587: /*
588: * ipc_notSemaphore() - IPC negative block if semaphore isn`t signaled
589: *
590: * @s = Session item
591: * return: none
592: */
593: void
594: ipc_notSemaphore(ait_sess_t * __restrict s)
595: {
596: struct sembuf sb = { 0, 0, 0 };
597:
598: if (s)
599: semop(s->id.semid, &sb, 1);
600: }
601:
602: /*
603: * ipc_isSemaphoreOK() - IPC Check semaphore
604: *
605: * @s = Session item
606: * return: -1 error: can`t return semaphore, 0 = false, 1 = true
607: */
608: int
609: ipc_isSemaphoreOK(ait_sess_t * __restrict s)
610: {
611: struct sembuf sb = { 0, 0, IPC_NOWAIT };
612:
613: if (!s)
614: return -1;
615:
616: return semop(s->id.semid, &sb, 1) + 1;
617: }
618:
619: /*
620: * ipc_incSemaphore() - IPC unblock semaphore, increment semaphore
621: *
622: * @s = Session item
623: * return: 0 Ok, -1 error: can`t increment
624: */
625: int
626: ipc_incSemaphore(ait_sess_t * __restrict s)
627: {
628: struct sembuf sb = { 0, 1, 0 };
629:
630: if (!s)
631: return -1;
632:
633: return semop(s->id.semid, &sb, 1);
634: }
635:
636: /*
637: * ipc_decSemaphore() - IPC block semaphore, decrement semaphore
638: *
639: * @s = Session item
640: * return: 0 Ok, -1 error: can`t decrement
641: */
642: int
643: ipc_decSemaphore(ait_sess_t * __restrict s)
644: {
645: struct sembuf sb = { 0, -1, 0 };
646:
647: if (!s)
648: return -1;
649:
650: return semop(s->id.semid, &sb, 1);
651: }
652:
653: /* --- A/V management --- */
654:
655: /*
1.1.2.1 misho 656: * sess_FreeValues() - Free all values from value array allocated from sess_GetValues()
657: *
658: * @ppsVals = Array strings
659: * return: none
660: */
661: inline void
662: sess_FreeValues(char *** __restrict ppsVals)
663: {
1.1.2.2 misho 664: str_FreeNullTerm(ppsVals);
1.1.2.1 misho 665: }
666:
667: /*
668: * sess_GetValues() - Get all values from session shared memory
669: *
670: * @s = Session item
671: * @ppsVals = Return array strings
672: * return: -1 error: in parameter, !=-1 count of returned strings in ppsVals
673: * (must be sess_FreeValues after use!)
674: */
675: int
676: sess_GetValues(ait_sess_t * __restrict s, char ***ppsVals)
677: {
678: register int i;
679: char **valz, *Shared = NULL;
680: char *peer, *p_brk;
681:
682: if (!s || !ppsVals)
683: return -1;
684: valz = e_malloc(sizeof(caddr_t));
685: if (!valz) {
686: LOGERR;
687: return -1;
688: } else
689: *valz = NULL;
690:
691: /* allocated memory & mirrored shared memory into this */
692: Shared = e_malloc(s->eom);
693: if (!Shared) {
694: LOGERR;
695: e_free(valz);
696: return -1;
697: } else
698: memcpy(Shared, s->addr, s->eom);
699:
1.1.2.2 misho 700: for (i = 0, peer = strtok_r(Shared, SESS_MEM_DELIM"\r\n", &p_brk); peer;
701: peer = strtok_r(NULL, SESS_MEM_DELIM"\r\n", &p_brk)) {
1.1.2.1 misho 702: if (!strchr(peer, '='))
703: continue;
704: else
705: i++;
706:
707: valz = e_realloc(valz, (i + 1) * sizeof(caddr_t));
708: if (!valz) {
709: LOGERR;
710: e_free(Shared);
711: return -1;
712: } else
713: valz[i] = NULL;
714:
715: valz[i - 1] = e_strdup(peer);
716: }
717:
718: e_free(Shared);
719: *ppsVals = valz;
720: return i;
721: }
722:
723: /*
724: * sess_GetValue() - Get value from session shared memory from attribute
725: *
726: * @s = Session item
727: * @csAttr = Attribute for search
728: * @psVal = Return string buffer
729: * @pnLen = Length of return string buffer,
730: // *{pnLen} input is max_size of buffer & output is really taken bytes
731: * return: 0 not found, -1 error: in parameter, >0 get position,
732: * if define item merged with IS_DEF
733: */
734: int
735: sess_GetValue(ait_sess_t * __restrict s, const char *csAttr, char *psVal, int *pnLen)
736: {
737: register int i;
738: int def = IS_VAL;
739: char *Shared = NULL;
740: char *peer, *p_brk, *a_brk, *attr, *val;
741:
742: if (!s || !csAttr || !*csAttr)
743: return -1;
744: if (psVal) {
745: if (pnLen && *pnLen > 0)
746: memset(psVal, 0, *pnLen);
747: else
748: return -1;
749: }
750:
751: /* allocated memory & mirrored shared memory into this */
752: Shared = e_malloc(s->eom);
753: if (!Shared) {
754: LOGERR;
755: return -1;
756: } else
757: memcpy(Shared, s->addr, s->eom);
758:
1.1.2.2 misho 759: for (i = 1, peer = strtok_r(Shared, SESS_MEM_DELIM"\r\n", &p_brk); peer;
760: i++, peer = strtok_r(NULL, SESS_MEM_DELIM"\r\n", &p_brk)) {
1.1.2.1 misho 761: attr = strtok_r(peer, "=\r\n", &a_brk);
762: if (attr && !strncmp(attr, csAttr, MAX_ATTRIBUTE - 1)) {
763: val = strtok_r(NULL, "=\r\n", &a_brk);
764: if (val && strlen(val)) {
765: if (psVal)
766: strlcpy(psVal, val, *pnLen);
767: if (pnLen)
768: *pnLen = strlen(val);
769: } else
770: def = IS_DEF;
771:
772: e_free(Shared);
773: return i | def;
774: }
775: }
776:
777: e_free(Shared);
778: return 0;
779: }
780:
781: /*
782: * sess_DelValue() - Delete item from session shared memory
783: *
784: * @s = Session item
785: * @csAttr = Attribute for erasing
786: * return: 0 Ok, -1 error: in parameter
787: */
788: int
789: sess_DelValue(ait_sess_t * __restrict s, const char *csAttr)
790: {
791: register int i;
792: int attrlen;
793: char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE];
794: char *peer, *p_brk;
795:
796: if (!s || !csAttr || !*csAttr)
797: return -1;
798: else
799: attrlen = strlen(csAttr);
800: Buffer = Shared = NULL;
801: strlcpy(szAttr, csAttr, sizeof szAttr);
802: strlcat(szAttr, "=", sizeof szAttr);
803:
804: Buffer = e_malloc(s->eom);
805: if (!Buffer) {
806: LOGERR;
807: return -1;
808: } else
809: memset(Buffer, 0, s->eom);
810: Shared = e_malloc(s->eom);
811: if (!Shared) {
812: LOGERR;
813: e_free(Buffer);
814: return -1;
815: } else {
816: DEC_SEM(s);
817: memcpy(Shared, s->addr, s->eom);
818: }
819:
1.1.2.2 misho 820: for (i = 1, peer = strtok_r(Shared, SESS_MEM_DELIM"\r\n", &p_brk); peer;
821: i++, peer = strtok_r(NULL, SESS_MEM_DELIM"\r\n", &p_brk)) {
1.1.2.1 misho 822: if (!strncmp(peer, csAttr, attrlen))
1.1.2.2 misho 823: if (peer[attrlen] == '=' || peer[attrlen] == *SESS_MEM_DELIM || !peer[attrlen] ||
1.1.2.1 misho 824: peer[attrlen] == '\r' || peer[attrlen] == '\n')
825: continue;
826:
827: strlcat(Buffer, peer, s->eom);
1.1.2.2 misho 828: strlcat(Buffer, SESS_MEM_DELIM, s->eom);
1.1.2.1 misho 829: }
830:
831: memcpy(s->addr, Buffer, s->eom);
832:
833: if (s->type == SHARED_MAP)
834: msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
835:
836: INC_SEM(s);
837: e_free(Shared);
838: e_free(Buffer);
839: return 0;
840: }
841:
842: /*
843: * sess_SetValue() - Set item into session shared memory or update if exists
844: *
845: * @s = Session item
846: * @csAttr = Attribute
847: * @psVal = Value
848: * return: 0 nothing, -1 error: in parameter,
849: >0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF
850: */
851: int
852: sess_SetValue(ait_sess_t * __restrict s, const char *csAttr, const char *psVal)
853: {
854: register int i;
855: int upd, def = IS_VAL;
856: char *Buffer, *Shared, szAttr[MAX_ATTRIBUTE];
857: char *peer, *p_brk;
858:
859: if (!s || !csAttr || !*csAttr)
860: return -1;
861: else
862: Buffer = Shared = NULL;
863: strlcpy(szAttr, csAttr, sizeof szAttr);
864: strlcat(szAttr, "=", sizeof szAttr);
865:
866: Buffer = e_malloc(s->eom);
867: if (!Buffer) {
868: LOGERR;
869: return -1;
870: } else
871: memset(Buffer, 0, s->eom);
872: Shared = e_malloc(s->eom);
873: if (!Shared) {
874: LOGERR;
875: e_free(Buffer);
876: return -1;
877: } else {
878: DEC_SEM(s);
879: memcpy(Shared, s->addr, s->eom);
880: }
881:
1.1.2.2 misho 882: for (i = 1, upd = 0, peer = strtok_r(Shared, SESS_MEM_DELIM"\r\n", &p_brk); peer;
883: i++, peer = strtok_r(NULL, SESS_MEM_DELIM"\r\n", &p_brk)) {
1.1.2.1 misho 884: if (!strncmp(peer, szAttr, strlen(szAttr))) {
885: upd++;
886: if (psVal) {
887: strlcat(Buffer, szAttr, s->eom);
888: strlcat(Buffer, psVal, s->eom);
1.1.2.2 misho 889: strlcat(Buffer, SESS_MEM_DELIM, s->eom);
1.1.2.1 misho 890: } else {
891: strlcat(Buffer, csAttr, s->eom);
1.1.2.2 misho 892: strlcat(Buffer, SESS_MEM_DELIM, s->eom);
1.1.2.1 misho 893: def = IS_DEF;
894: }
895: continue;
896: }
897:
898: strlcat(Buffer, peer, s->eom);
1.1.2.2 misho 899: strlcat(Buffer, SESS_MEM_DELIM, s->eom);
1.1.2.1 misho 900: }
901:
902: if (!upd) {
903: if (psVal) {
904: strlcat(Buffer, szAttr, s->eom);
905: strlcat(Buffer, psVal, s->eom);
1.1.2.2 misho 906: strlcat(Buffer, SESS_MEM_DELIM, s->eom);
1.1.2.1 misho 907: } else {
908: strlcat(Buffer, csAttr, s->eom);
1.1.2.2 misho 909: strlcat(Buffer, SESS_MEM_DELIM, s->eom);
1.1.2.1 misho 910: def = IS_DEF;
911: }
912: def |= IS_ADD;
913: }
914:
915: memcpy(s->addr, Buffer, s->eom);
916:
917: if (s->type == SHARED_MAP)
918: msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
919:
920: INC_SEM(s);
921: e_free(Shared);
922: e_free(Buffer);
923: return upd | def;
924: }
925:
926:
927: /*
928: * sess_prepareSession() - Attach to shared memory and de-marshaling data
929: *
930: * @s = Session
931: * @useDirect = Use direct shared memory if !=0 or snapshot of data to array
932: * return: NULL error or no data, !=NULL array with variables,
933: * after use must free resources with sess_doneSession()
934: */
935: array_t *
936: sess_prepareSession(ait_sess_t * __restrict s, char useDirect)
937: {
938: array_t *arr = NULL;
939: sess_hdr_t *hdr;
940:
941: assert(s);
942: if (!s) {
943: sess_SetErr(EINVAL, "Invalid argument\n");
944: return NULL;
945: }
946: if (s->addr) {
947: sess_SetErr(EINVAL, "Already attached memory\n");
948: return NULL;
949: }
950:
951: ATTACH_MEMORY(s);
952: if (!s->addr)
953: return NULL;
954: else
955: hdr = (sess_hdr_t*) s->addr;
956: if (hdr->hdr_magic != SESS_AIT_MAGIC) {
957: DETACH_MEMORY(s);
958:
959: sess_SetErr(EINVAL, "Shared memory not contains values with proper format\n");
960: return NULL;
961: }
962:
963: DEC_SEM(s);
964: s->zcpy = useDirect;
965: arr = ait_map2vars(s->addr + sizeof(sess_hdr_t), s->eom - sizeof(sess_hdr_t),
966: hdr->hdr_argc, useDirect);
967: INC_SEM(s);
968:
969: if (!s->zcpy)
970: DETACH_MEMORY(s);
971: return arr;
972: }
973:
974: /*
975: * sess_doneSession() - Free resources allocated with sess_prepareSession()
976: *
977: * @s = Session
978: * @arr = Array with variables for free
979: * return: none
980: */
981: void
982: sess_doneSession(ait_sess_t * __restrict s, array_t ** __restrict arr)
983: {
984: assert(s);
985: if (!s) {
986: sess_SetErr(EINVAL, "Invalid argument\n");
987: return;
988: }
989:
990: if (!s->zcpy)
991: array_Free(*arr);
992: else
993: DETACH_MEMORY(s);
994: array_Destroy(arr);
995: }
996:
997: /*
998: * sess_commitSession() - Commit data to shared memory
999: *
1000: * @s = Session
1001: * @arr = Array with variables for save
1002: * return -1 error or !=-1 size of stored variables into shared memory
1003: */
1004: int
1005: sess_commitSession(ait_sess_t * __restrict s, array_t * __restrict arr)
1006: {
1007: sess_hdr_t *hdr;
1008: int ret = 0;
1009:
1010: assert(s && arr);
1011: if (!s || !arr) {
1012: sess_SetErr(EINVAL, "Invalid argument\n");
1013: return -1;
1014: }
1015:
1016: ATTACH_MEMORY(s);
1017: if (!s->addr) {
1018: DETACH_MEMORY(s);
1019: return -1;
1020: } else
1021: hdr = (sess_hdr_t*) s->addr;
1022:
1023: DEC_SEM(s);
1024: if ((ret = ait_vars2map(s->addr + sizeof(sess_hdr_t),
1025: s->eom - sizeof(sess_hdr_t), arr)) != -1) {
1026: hdr->hdr_magic = SESS_AIT_MAGIC;
1027: hdr->hdr_argc = array_Size(arr);
1028: ret += sizeof(sess_hdr_t);
1029: }
1030: INC_SEM(s);
1031:
1032: DETACH_MEMORY(s);
1033: return ret;
1034: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>