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