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