Annotation of libaitsess/src/aitsess.c, revision 1.6
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.6 ! misho 6: * $Id: aitsess.c,v 1.5.2.1 2012/05/23 15:14:41 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: #pragma GCC visibility push(hidden)
51:
1.3 misho 52: int sess_Errno;
53: char sess_Error[STRSIZ];
1.1 misho 54:
55: #pragma GCC visibility pop
56:
57: // -----------------------------------------------------------
58:
59: // Error maintenance functions ...
60:
61: // sess_GetErrno() Get error code of last operation
1.3 misho 62: inline int
63: sess_GetErrno()
1.1 misho 64: {
1.3 misho 65: return sess_Errno;
1.1 misho 66: }
67: // sess_GetError() Get error text of last operation
1.3 misho 68: inline const char *
69: sess_GetError()
1.1 misho 70: {
1.3 misho 71: return sess_Error;
1.1 misho 72: }
1.3 misho 73: // sess_SetErr() Set error to variables for internal use!!!
74: inline void
75: sess_SetErr(int eno, char *estr, ...)
1.1 misho 76: {
77: va_list lst;
78:
1.3 misho 79: sess_Errno = eno;
80: memset(sess_Error, 0, sizeof sess_Error);
81: va_start(lst, estr);
82: vsnprintf(sess_Error, sizeof sess_Error, estr, lst);
1.1 misho 83: va_end(lst);
84: }
85:
86: // -----------------------------------------------------------
87:
88: /*
1.4 misho 89: * sess_initSession() Initializing session structure, if session file not exists creating with specified tech
90: *
91: * @id = Technology using in session. SHARED_IPC IPC tech orSHARED_MAP BSD MemoryMap tech
1.1 misho 92: * @csFName = Session filename for build key and identified
1.3 misho 93: * @Sess = Session item, if =NULL allocate memory for session after use must be free!
1.1 misho 94: * return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded
95: */
1.4 misho 96: int
97: sess_initSession(int id, const char *csFName, ait_sess_t ** __restrict Sess)
1.1 misho 98: {
1.4 misho 99: int h, ret = 0;
1.3 misho 100: char szStr[STRSIZ];
1.1 misho 101:
1.4 misho 102: if (!csFName) {
103: sess_SetErr(EINVAL, "Filename is NULL");
104: return -1;
105: }
106: if (id < SHARED_UNKNOWN || id > SHARED_MAP) {
107: sess_SetErr(EPROTONOSUPPORT, "Session type not supported");
108: return -1;
109: }
110:
1.1 misho 111: if (!*Sess) {
1.6 ! misho 112: *Sess = io_malloc(sizeof(ait_sess_t));
1.1 misho 113: if (!*Sess) {
114: LOGERR;
115: return -1;
116: }
1.4 misho 117: }
118: memset(*Sess, 0, sizeof(ait_sess_t));
119: strlcpy((*Sess)->name, csFName, sizeof (*Sess)->name);
1.1 misho 120:
1.4 misho 121: h = open((*Sess)->name, O_WRONLY | O_CREAT | O_EXCL, MEM_MODE);
1.1 misho 122: if (h == -1) {
1.3 misho 123: if (errno != EEXIST) {
124: LOGERR;
1.6 ! misho 125: io_free(*Sess);
1.3 misho 126: return -1;
127: }
128: /* If key file exist, session already connected */
1.4 misho 129: h = open((*Sess)->name, O_RDONLY);
1.3 misho 130: if (h == -1) {
131: LOGERR;
1.6 ! misho 132: io_free(*Sess);
1.3 misho 133: return -1;
134: }
135: ret = read(h, szStr, sizeof szStr);
136: if (ret == -1) {
1.1 misho 137: LOGERR;
138: close(h);
1.6 ! misho 139: io_free(*Sess);
1.1 misho 140: return -1;
1.3 misho 141: }
1.4 misho 142: if (!strncmp(szStr, "IPC@", 4) && id == SHARED_IPC) {
143: ret = 1;
144:
145: (*Sess)->sess.create = (int (*)(int, long, void*, ...)) ipc_createSession;
146: (*Sess)->sess.destroy = (void (*)(void*)) ipc_destroySession;
147: (*Sess)->sess.attach = (void* (*)(void*, void*)) ipc_attachSession;
148: (*Sess)->sess.detach = (void (*)(void*)) ipc_detachSession;
149: (*Sess)->sess.notSem = (void (*)(void*)) ipc_notSemaphore;
150: (*Sess)->sess.isSemOK = (int (*)(void*)) ipc_isSemaphoreOK;
151: (*Sess)->sess.incSem = (int (*)(void*)) ipc_incSemaphore;
152: (*Sess)->sess.decSem = (int (*)(void*)) ipc_decSemaphore;
153: } else if (!strncmp(szStr, "MAP@", 4) && id == SHARED_MAP) {
154: ret = 1;
155:
156: (*Sess)->sess.create = (int (*)(int, long, void*, ...)) map_createSession;
157: (*Sess)->sess.destroy = (void (*)(void*)) map_destroySession;
158: (*Sess)->sess.attach = (void* (*)(void*, void*)) map_attachSession;
159: (*Sess)->sess.detach = (void (*)(void*)) map_detachSession;
160: (*Sess)->sess.notSem = (void (*)(void*)) map_notSemaphore;
161: (*Sess)->sess.isSemOK = (int (*)(void*)) map_isSemaphoreOK;
162: (*Sess)->sess.incSem = (int (*)(void*)) map_incSemaphore;
163: (*Sess)->sess.decSem = (int (*)(void*)) map_decSemaphore;
164: } else {
165: sess_SetErr(EPROTONOSUPPORT,
166: "Session type not supported or wrong session type");
1.3 misho 167: close(h);
1.6 ! misho 168: io_free(*Sess);
1.3 misho 169: return -1;
170: }
1.4 misho 171: /* key found */
1.3 misho 172: } else {
173: /* Build new key & new session */
1.4 misho 174: if (id == SHARED_IPC) {
175: strlcpy(szStr, "IPC@", sizeof szStr);
176:
177: (*Sess)->sess.create = (int (*)(int, long, void*, ...)) ipc_createSession;
178: (*Sess)->sess.destroy = (void (*)(void*)) ipc_destroySession;
179: (*Sess)->sess.attach = (void* (*)(void*, void*)) ipc_attachSession;
180: (*Sess)->sess.detach = (void (*)(void*)) ipc_detachSession;
181: (*Sess)->sess.notSem = (void (*)(void*)) ipc_notSemaphore;
182: (*Sess)->sess.isSemOK = (int (*)(void*)) ipc_isSemaphoreOK;
183: (*Sess)->sess.incSem = (int (*)(void*)) ipc_incSemaphore;
184: (*Sess)->sess.decSem = (int (*)(void*)) ipc_decSemaphore;
185: } else if (id == SHARED_MAP) {
186: strlcpy(szStr, "MAP@", sizeof szStr);
187:
188: (*Sess)->sess.create = (int (*)(int, long, void*, ...)) map_createSession;
189: (*Sess)->sess.destroy = (void (*)(void*)) map_destroySession;
190: (*Sess)->sess.attach = (void* (*)(void*, void*)) map_attachSession;
191: (*Sess)->sess.detach = (void (*)(void*)) map_detachSession;
192: (*Sess)->sess.notSem = (void (*)(void*)) map_notSemaphore;
193: (*Sess)->sess.isSemOK = (int (*)(void*)) map_isSemaphoreOK;
194: (*Sess)->sess.incSem = (int (*)(void*)) map_incSemaphore;
195: (*Sess)->sess.decSem = (int (*)(void*)) map_decSemaphore;
196: } else {
197: sess_SetErr(EINVAL, "Session type must be specified");
198: close(h);
199: unlink(csFName);
1.6 ! misho 200: io_free(*Sess);
1.4 misho 201: return -1;
1.3 misho 202: }
203: strlcat(szStr, "ELWIX_Session ("PACKAGE_STRING")\n", sizeof szStr);
204: write(h, szStr, strlen(szStr));
205:
1.4 misho 206: ret = 0;
207: /* new key created */
1.1 misho 208: }
209: close(h);
210:
1.3 misho 211: (*Sess)->type = id;
1.4 misho 212: (*Sess)->zcpy = (char) ret;
1.1 misho 213: return ret;
214: }
215:
216: /*
1.4 misho 217: * sess_freeSession() Free allocated memory for session item and delete session file if present name
218: *
1.1 misho 219: * @Sess = Session item
1.4 misho 220: * return: none
1.1 misho 221: */
1.4 misho 222: void
223: sess_freeSession(ait_sess_t ** __restrict Sess)
1.1 misho 224: {
1.4 misho 225: if (!Sess || !(*Sess))
1.3 misho 226: return;
227:
1.4 misho 228: if ((*Sess)->addr)
229: DETACH_MEMORY(*Sess);
230:
231: /*
232: memset(&(*Sess)->sess, 0, sizeof (*Sess)->sess);
233:
1.3 misho 234: (*Sess)->type = SHARED_UNKNOWN;
1.4 misho 235: */
236:
1.6 ! misho 237: io_free(*Sess);
1.1 misho 238: *Sess = NULL;
239: }
240:
241:
242: /*
243: * map_createSession() MMAP Created session and allocated resources
1.4 misho 244: *
245: * @nSeed = Seed for securing key, if =-1 must add ready for use key
246: * @nSize = Allocated shared memory size in bytes
1.1 misho 247: * @Sess = Session item
1.4 misho 248: * @... = If nSeed == -1 add ready for use key value
1.1 misho 249: * return: 0 Ok successful, -1 error: not allocated resources
250: */
1.3 misho 251: int
1.4 misho 252: map_createSession(int nSeed, long nSize, ait_sess_t * __restrict Sess, ...)
1.1 misho 253: {
1.3 misho 254: char szSName[2][FILENAME_MAX];
1.4 misho 255: va_list lst;
1.1 misho 256:
1.4 misho 257: if (!Sess || !*Sess->name)
1.1 misho 258: return -1;
259:
1.4 misho 260: if (nSeed != -1) {
261: /* genkey */
262: Sess->key = ftok(Sess->name, nSeed);
263: if (Sess->key == -1) {
264: LOGERR;
265: return -1;
266: }
267: } else {
268: /* get key from va_args */
269: va_start(lst, Sess);
270: Sess->key = va_arg(lst, key_t);
271: va_end(lst);
1.1 misho 272: }
273:
1.3 misho 274: /* build semaphore & shared memory name */
275: memset(szSName, 0, sizeof szSName);
1.4 misho 276: snprintf(szSName[0], MAX_SEMNAME, "/%X.ANS", (u_int) Sess->key);
1.5 misho 277: #ifdef HAVE_SHM_OPEN
278: snprintf(szSName[1], FILENAME_MAX, "/%s-%x.ANM", Sess->name, (u_int) Sess->key);
279: #else
1.4 misho 280: snprintf(szSName[1], FILENAME_MAX, "%s-%x.ANM", Sess->name, (u_int) Sess->key);
1.5 misho 281: #endif
1.1 misho 282:
1.3 misho 283: /* create semaphore & add 1 */
1.4 misho 284: Sess->id.sid = sem_open(szSName[0], O_CREAT, MEM_MODE);
285: if (Sess->id.sid == SEM_FAILED) {
1.1 misho 286: LOGERR;
1.4 misho 287: map_destroySession(Sess);
1.1 misho 288: return -1;
1.4 misho 289: }
290: /* if is new shared memory session, init sempahore with 1 */
291: if (!Sess->zcpy)
292: sem_post(Sess->id.sid);
1.1 misho 293:
1.3 misho 294: /* create file for shared memory storage */
1.5 misho 295: #ifdef HAVE_SHM_OPEN
296: Sess->mem.fd = shm_open(szSName[1], O_RDWR | O_CREAT, MEM_MODE);
297: #else
1.4 misho 298: Sess->mem.fd = open(szSName[1], O_RDWR | O_CREAT, MEM_MODE);
1.5 misho 299: #endif
1.4 misho 300: if (Sess->mem.fd == -1) {
1.1 misho 301: LOGERR;
1.4 misho 302: map_destroySession(Sess);
1.1 misho 303: return -1;
304: }
1.5 misho 305: if (!Sess->zcpy) {
306: #ifdef HAVE_SHM_OPEN
307: if (ftruncate(Sess->mem.fd, nSize) == -1) {
308: LOGERR;
309: map_destroySession(Sess);
310: return -1;
311: }
312: #else
1.3 misho 313: /* if is new shared memory session, fill file with zeros */
1.4 misho 314: if (lseek(Sess->mem.fd, nSize - 1, SEEK_SET) == -1) {
1.1 misho 315: LOGERR;
1.4 misho 316: map_destroySession(Sess);
1.1 misho 317: return -1;
1.3 misho 318: } else
1.4 misho 319: write(Sess->mem.fd, "", 1);
320: lseek(Sess->mem.fd, 0, SEEK_SET);
1.5 misho 321: #endif
1.1 misho 322: }
1.4 misho 323: Sess->eom = nSize;
1.1 misho 324:
1.4 misho 325: return (int) Sess->zcpy;
1.1 misho 326: }
327:
328: /*
329: * map_destroySession() MMAP free shared resources
1.4 misho 330: *
1.1 misho 331: * @Sess = Session item
1.4 misho 332: * return: none
1.1 misho 333: */
1.3 misho 334: void
1.4 misho 335: map_destroySession(ait_sess_t * __restrict Sess)
1.1 misho 336: {
1.3 misho 337: char szSName[2][FILENAME_MAX];
1.1 misho 338:
1.4 misho 339: if (!Sess || sess_isAttached(Sess) || !*Sess->name)
1.1 misho 340: return;
341:
1.3 misho 342: memset(szSName, 0, sizeof szSName);
1.4 misho 343: snprintf(szSName[0], MAX_SEMNAME, "/%X.ANS", (u_int) Sess->key);
1.5 misho 344: #ifdef HAVE_SHM_UNLINK
345: snprintf(szSName[1], FILENAME_MAX, "/%s-%x.ANM", Sess->name, (u_int) Sess->key);
346: #else
1.4 misho 347: snprintf(szSName[1], FILENAME_MAX, "%s-%x.ANM", Sess->name, (u_int) Sess->key);
1.5 misho 348: #endif
1.1 misho 349:
1.4 misho 350: if (Sess->id.sid != SEM_FAILED) {
351: sem_close(Sess->id.sid);
352: sem_unlink(szSName[0]);
353: }
354: if (Sess->mem.fd > 2) {
355: close(Sess->mem.fd);
1.5 misho 356: #ifdef HAVE_SHM_UNLINK
357: shm_unlink(szSName[1]);
358: #else
1.4 misho 359: unlink(szSName[1]);
1.5 misho 360: #endif
1.4 misho 361: }
362: unlink(Sess->name);
363: memset(Sess->name, 0, sizeof Sess->name);
364: Sess->eom ^= Sess->eom;
365: Sess->key ^= Sess->key;
1.1 misho 366: }
367:
368: /*
369: * ipc_createSession() IPC Created session and allocated resources
1.4 misho 370: *
371: * @nSeed = Seed for securing key, if =-1 must add ready for use key
372: * @nSize = Allocated shared memory size in bytes
1.1 misho 373: * @Sess = Session item
1.4 misho 374: * @... = If nSeed == -1 add ready for use key value
1.1 misho 375: * return: 0 Ok successful, -1 error: not allocated resources
1.4 misho 376: */
1.3 misho 377: int
1.4 misho 378: ipc_createSession(int nSeed, long nSize, ait_sess_t * __restrict Sess, ...)
1.1 misho 379: {
380: union semun sems;
1.4 misho 381: va_list lst;
1.1 misho 382:
1.4 misho 383: if (!Sess || !*Sess->name)
1.1 misho 384: return -1;
385:
1.4 misho 386: if (nSeed != -1) {
387: /* genkey */
388: Sess->key = ftok(Sess->name, nSeed);
389: if (Sess->key == -1) {
390: LOGERR;
391: return -1;
392: }
393: } else {
394: /* get key from va_args */
395: va_start(lst, Sess);
396: Sess->key = va_arg(lst, key_t);
397: va_end(lst);
1.1 misho 398: }
399:
1.3 misho 400: /* create semaphore */
1.4 misho 401: Sess->id.semid = semget(Sess->key, 1, MEM_MODE | IPC_CREAT);
402: if (Sess->id.semid == -1) {
1.1 misho 403: LOGERR;
1.4 misho 404: ipc_destroySession(Sess);
1.1 misho 405: return -1;
406: }
1.3 misho 407: /* if is new shared memory session, init sempahore with 1 */
1.4 misho 408: if (!Sess->zcpy) {
1.1 misho 409: sems.val = 1;
1.4 misho 410: if (semctl(Sess->id.semid, 0, SETVAL, sems) == -1) {
1.1 misho 411: LOGERR;
1.4 misho 412: ipc_destroySession(Sess);
1.1 misho 413: return -1;
414: }
415: }
416:
1.3 misho 417: /* create shared memory object */
1.4 misho 418: Sess->mem.shmid = shmget(Sess->key, nSize, MEM_MODE | IPC_CREAT);
419: if (Sess->mem.shmid == -1) {
1.1 misho 420: LOGERR;
1.4 misho 421: ipc_destroySession(Sess);
1.1 misho 422: return -1;
1.4 misho 423: } else
424: Sess->eom = nSize;
1.1 misho 425:
1.4 misho 426: return (int) Sess->zcpy;
1.1 misho 427: }
428:
429: /*
430: * ipc_destroySession() IPC free shared resources
1.4 misho 431: *
1.1 misho 432: * @Sess = Session item
1.4 misho 433: * return: none
434: */
1.3 misho 435: void
1.4 misho 436: ipc_destroySession(ait_sess_t * __restrict Sess)
1.1 misho 437: {
438: union semun sems;
439: struct shmid_ds ds;
440:
1.4 misho 441: if (!Sess || sess_isAttached(Sess))
1.1 misho 442: return;
443:
1.4 misho 444: if (Sess->id.semid != -1)
445: semctl(Sess->id.semid, 0, IPC_RMID, &sems);
446: if (Sess->mem.shmid != -1)
447: shmctl(Sess->mem.shmid, IPC_RMID, &ds);
448: unlink(Sess->name);
449: memset(Sess->name, 0, sizeof Sess->name);
450: Sess->eom ^= Sess->eom;
451: Sess->key ^= Sess->key;
1.1 misho 452: }
453:
454:
455: /*
456: * map_attachSession() MMAP Attach to shared memory & return begin address
1.4 misho 457: *
1.1 misho 458: * @s = Session item
459: * @procMem = Custom start address (optionl) *default must be 0*
460: * return: NULL failed attach, !=NULL begin address of memory
1.4 misho 461: */
462: void *
463: map_attachSession(ait_sess_t * __restrict s, void *procMem)
1.1 misho 464: {
465: if (!s)
466: return NULL;
467:
468: sync();
469:
1.3 misho 470: /* attach to memory */
1.1 misho 471: s->addr = mmap(procMem, s->eom, PROT_READ | PROT_WRITE, MAP_SHARED, s->mem.fd, 0);
472: if (s->addr == MAP_FAILED) {
473: LOGERR;
474: s->addr = NULL;
475: }
476:
477: return s->addr;
478: }
479:
480: /*
481: * map_detachSession() MMAP Detach from shared memory
1.4 misho 482: *
1.1 misho 483: * @s = Session item
1.4 misho 484: * return: none
485: */
486: void
487: map_detachSession(ait_sess_t * __restrict s)
1.1 misho 488: {
489: if (!s)
490: return;
491:
492: msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
493:
494: if (s->addr && s->eom) {
495: munmap(s->addr, s->eom);
496: s->addr = NULL;
497: }
498: }
499:
500: /*
501: * ipc_attachSession() IPC Attach to shared memory & return begin address
1.4 misho 502: *
1.1 misho 503: * @s = Session item
504: * @procMem = Custom start address (optionl) *default must be 0*
505: * return: NULL failed attach, !=NULL begin address of memory
1.4 misho 506: */
507: void *
508: ipc_attachSession(ait_sess_t * __restrict s, void *procMem)
1.1 misho 509: {
510: if (!s)
511: return NULL;
512:
513: s->addr = shmat(s->mem.shmid, procMem, 0);
514: if (s->addr == (void*) -1) {
515: LOGERR;
516: s->addr = NULL;
517: }
518:
519: return s->addr;
520: }
521:
522: /*
523: * ipc_detachSession() IPC Detach from shared memory
1.4 misho 524: *
1.1 misho 525: * @s = Session item
1.4 misho 526: * return: none
527: */
528: void
529: ipc_detachSession(ait_sess_t * __restrict s)
1.1 misho 530: {
531: if (!s)
532: return;
533:
534: if (s->addr) {
535: shmdt(s->addr);
536: s->addr = NULL;
537: }
538: }
539:
1.2 misho 540: /*
1.4 misho 541: * sess_isAttached() Check for attached shared memory
542: *
1.2 misho 543: * @s = Session item
544: * return: -1 null session item, 0 not attached, 1 attached memory
1.4 misho 545: */
1.3 misho 546: inline int
1.4 misho 547: sess_isAttached(ait_sess_t * __restrict s)
1.2 misho 548: {
549: if (!s)
550: return -1;
551:
552: return (s->addr ? 1 : 0);
553: }
554:
1.1 misho 555:
556: /*
557: * map_notSemaphore() MMAP negative block if semaphore isn`t signaled
1.4 misho 558: *
1.1 misho 559: * @s = Session item
1.4 misho 560: * return: none
561: */
562: void
563: map_notSemaphore(ait_sess_t * __restrict s)
1.1 misho 564: {
565: int i = -1;
566:
567: if (!s)
568: return;
569:
570: sem_getvalue(s->id.sid, &i);
1.4 misho 571: while (i > 0) {
1.1 misho 572: sem_wait(s->id.sid);
1.4 misho 573: i--;
574: }
1.1 misho 575: }
576:
577: /*
1.4 misho 578: * map_isSemaphoreOK() MMAP Check semaphore
579: *
1.1 misho 580: * @s = Session item
581: * return: -1 error: can`t return semaphore, 0 = false, 1 = true
1.4 misho 582: */
583: int
584: map_isSemaphoreOK(ait_sess_t * __restrict s)
1.1 misho 585: {
586: int val = -1;
587:
588: if (!s)
589: return -1;
590:
591: sem_getvalue(s->id.sid, &val);
1.4 misho 592: return (val ? 0 : 1);
1.1 misho 593: }
594:
595: /*
1.4 misho 596: * map_incSemaphore() MMAP unblock semaphore, increment semaphore
597: *
1.1 misho 598: * @s = Session item
599: * return: 0 Ok, -1 error: can`t increment
1.4 misho 600: */
601: int
602: map_incSemaphore(ait_sess_t * __restrict s)
1.1 misho 603: {
604: if (!s)
605: return -1;
606:
607: return sem_post(s->id.sid);
608: }
609:
610: /*
611: * map_decSemaphore() MMAP block semaphore, decrement semaphore
1.4 misho 612: *
1.1 misho 613: * @s = Session item
614: * return: 0 Ok, -1 error: can`t decrement
1.4 misho 615: */
616: int
617: map_decSemaphore(ait_sess_t * __restrict s)
1.1 misho 618: {
619: if (!s)
620: return -1;
621:
622: return sem_wait(s->id.sid);
623: }
624:
625: /*
626: * ipc_notSemaphore() IPC negative block if semaphore isn`t signaled
1.4 misho 627: *
1.1 misho 628: * @s = Session item
1.4 misho 629: * return: none
630: */
631: void
632: ipc_notSemaphore(ait_sess_t * __restrict s)
1.1 misho 633: {
634: struct sembuf sb = { 0, 0, 0 };
635:
636: if (s)
637: semop(s->id.semid, &sb, 1);
638: }
639:
640: /*
1.4 misho 641: * ipc_isSemaphoreOK() IPC Check semaphore
642: *
1.1 misho 643: * @s = Session item
644: * return: -1 error: can`t return semaphore, 0 = false, 1 = true
1.4 misho 645: */
646: int
647: ipc_isSemaphoreOK(ait_sess_t * __restrict s)
1.1 misho 648: {
649: struct sembuf sb = { 0, 0, IPC_NOWAIT };
650:
651: if (!s)
652: return -1;
653:
654: return semop(s->id.semid, &sb, 1) + 1;
655: }
656:
657: /*
1.4 misho 658: * ipc_incSemaphore() IPC unblock semaphore, increment semaphore
659: *
1.1 misho 660: * @s = Session item
661: * return: 0 Ok, -1 error: can`t increment
1.4 misho 662: */
663: int
664: ipc_incSemaphore(ait_sess_t * __restrict s)
1.1 misho 665: {
666: struct sembuf sb = { 0, 1, 0 };
667:
668: if (!s)
669: return -1;
670:
671: return semop(s->id.semid, &sb, 1);
672: }
673:
674: /*
675: * ipc_decSemaphore() IPC block semaphore, decrement semaphore
1.4 misho 676: *
1.1 misho 677: * @s = Session item
678: * return: 0 Ok, -1 error: can`t decrement
1.4 misho 679: */
680: int
681: ipc_decSemaphore(ait_sess_t * __restrict s)
1.1 misho 682: {
683: struct sembuf sb = { 0, -1, 0 };
684:
685: if (!s)
686: return -1;
687:
688: return semop(s->id.semid, &sb, 1);
689: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>