Annotation of libaitsess/src/aitsess.c, revision 1.3.2.5
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.5 ! misho 6: * $Id: aitsess.c,v 1.3.2.4 2012/02/10 16:54:37 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: * @csFName = Session filename for delete, if NULL nothing delete
220: * @Sess = Session item
1.3.2.3 misho 221: * return: none
1.1 misho 222: */
1.3.2.3 misho 223: void
224: sess_freeSession(const char *csFName, ait_sess_t ** __restrict Sess)
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.5 ! misho 232: if (csFName && (*Sess)->name)
! 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: *
1.1 misho 243: * @csFName = Session name for identified
1.3.2.3 misho 244: * @nSeed = Seed for securing key, if =-1 must add ready for use key
245: * @nSize = Allocated shared memory size in bytes
1.1 misho 246: * @Sess = Session item
1.3.2.3 misho 247: * @... = If nSeed == -1 add ready for use key value
1.1 misho 248: * return: 0 Ok successful, -1 error: not allocated resources
249: */
1.3 misho 250: int
1.3.2.3 misho 251: map_createSession(const char *csFName, int nSeed, long nSize, ait_sess_t * __restrict Sess, ...)
1.1 misho 252: {
1.3 misho 253: char szSName[2][FILENAME_MAX];
1.3.2.3 misho 254: va_list lst;
1.1 misho 255:
1.3.2.3 misho 256: if (!Sess)
1.1 misho 257: return -1;
258:
1.3.2.3 misho 259: if (nSeed != -1) {
260: /* genkey */
261: Sess->key = ftok(csFName, nSeed);
262: if (Sess->key == -1) {
263: LOGERR;
264: return -1;
265: }
266: } else {
267: /* get key from va_args */
268: va_start(lst, Sess);
269: Sess->key = va_arg(lst, key_t);
270: va_end(lst);
1.1 misho 271: }
272:
1.3 misho 273: /* build semaphore & shared memory name */
274: memset(szSName, 0, sizeof szSName);
1.3.2.3 misho 275: snprintf(szSName[0], MAX_SEMNAME, "/%X.ANS", (u_int) Sess->key);
276: snprintf(szSName[1], FILENAME_MAX, "%s-%x.ANM", csFName, (u_int) Sess->key);
1.1 misho 277:
1.3 misho 278: /* create semaphore & add 1 */
1.3.2.3 misho 279: Sess->id.sid = sem_open(szSName[0], O_CREAT, MEM_MODE);
280: if (Sess->id.sid == SEM_FAILED) {
1.1 misho 281: LOGERR;
282: map_destroySession(csFName, Sess);
283: return -1;
1.3.2.2 misho 284: }
1.3.2.3 misho 285: /* if is new shared memory session, init sempahore with 1 */
286: if (!Sess->zcpy)
287: sem_post(Sess->id.sid);
1.1 misho 288:
1.3 misho 289: /* create file for shared memory storage */
1.3.2.3 misho 290: Sess->mem.fd = open(szSName[1], O_RDWR | O_CREAT, MEM_MODE);
291: if (Sess->mem.fd == -1) {
1.1 misho 292: LOGERR;
293: map_destroySession(csFName, Sess);
294: return -1;
295: }
1.3 misho 296: /* if is new shared memory session, fill file with zeros */
1.3.2.3 misho 297: if (!Sess->zcpy) {
298: if (lseek(Sess->mem.fd, nSize - 1, SEEK_SET) == -1) {
1.1 misho 299: LOGERR;
300: map_destroySession(csFName, Sess);
301: return -1;
1.3 misho 302: } else
1.3.2.3 misho 303: write(Sess->mem.fd, "", 1);
304: lseek(Sess->mem.fd, 0, SEEK_SET);
1.1 misho 305: }
1.3.2.3 misho 306: Sess->eom = nSize;
1.1 misho 307:
1.3.2.3 misho 308: return (int) Sess->zcpy;
1.1 misho 309: }
310:
311: /*
312: * map_destroySession() MMAP free shared resources
1.3.2.3 misho 313: *
1.1 misho 314: * @csFName = Session name for delete
315: * @Sess = Session item
1.3.2.3 misho 316: * return: none
1.1 misho 317: */
1.3 misho 318: void
1.3.2.3 misho 319: map_destroySession(const char *csFName, ait_sess_t * __restrict Sess)
1.1 misho 320: {
1.3 misho 321: char szSName[2][FILENAME_MAX];
1.1 misho 322:
1.3.2.3 misho 323: if (!Sess || sess_isAttached(Sess))
1.1 misho 324: return;
325:
1.3 misho 326: memset(szSName, 0, sizeof szSName);
1.3.2.3 misho 327: snprintf(szSName[0], MAX_SEMNAME, "/%X.ANS", (u_int) Sess->key);
328: snprintf(szSName[1], FILENAME_MAX, "%s-%x.ANM", csFName, (u_int) Sess->key);
1.1 misho 329:
1.3.2.3 misho 330: if (Sess->id.sid != SEM_FAILED) {
331: sem_close(Sess->id.sid);
332: sem_unlink(szSName[0]);
333: }
334: if (Sess->mem.fd > 2) {
335: close(Sess->mem.fd);
336: unlink(szSName[1]);
1.1 misho 337: }
1.3.2.3 misho 338: Sess->eom ^= Sess->eom;
339: Sess->key ^= Sess->key;
1.1 misho 340: }
341:
342: /*
343: * ipc_createSession() IPC Created session and allocated resources
1.3.2.3 misho 344: *
1.1 misho 345: * @csFName = Session name for identified
1.3.2.3 misho 346: * @nSeed = Seed for securing key, if =-1 must add ready for use key
347: * @nSize = Allocated shared memory size in bytes
1.1 misho 348: * @Sess = Session item
1.3.2.3 misho 349: * @... = If nSeed == -1 add ready for use key value
1.1 misho 350: * return: 0 Ok successful, -1 error: not allocated resources
1.3.2.3 misho 351: */
1.3 misho 352: int
1.3.2.3 misho 353: ipc_createSession(const char *csFName, int nSeed, long nSize, ait_sess_t * __restrict Sess, ...)
1.1 misho 354: {
355: union semun sems;
1.3.2.3 misho 356: va_list lst;
1.1 misho 357:
1.3.2.3 misho 358: if (!Sess)
1.1 misho 359: return -1;
360:
1.3.2.3 misho 361: if (nSeed != -1) {
362: /* genkey */
363: Sess->key = ftok(csFName, nSeed);
364: if (Sess->key == -1) {
365: LOGERR;
366: return -1;
367: }
368: } else {
369: /* get key from va_args */
370: va_start(lst, Sess);
371: Sess->key = va_arg(lst, key_t);
372: va_end(lst);
1.1 misho 373: }
374:
1.3 misho 375: /* create semaphore */
1.3.2.3 misho 376: Sess->id.semid = semget(Sess->key, 1, MEM_MODE | IPC_CREAT);
377: if (Sess->id.semid == -1) {
1.1 misho 378: LOGERR;
379: ipc_destroySession(csFName, Sess);
380: return -1;
381: }
1.3 misho 382: /* if is new shared memory session, init sempahore with 1 */
1.3.2.3 misho 383: if (!Sess->zcpy) {
1.1 misho 384: sems.val = 1;
1.3.2.3 misho 385: if (semctl(Sess->id.semid, 0, SETVAL, sems) == -1) {
1.1 misho 386: LOGERR;
387: ipc_destroySession(csFName, Sess);
388: return -1;
389: }
390: }
391:
1.3 misho 392: /* create shared memory object */
1.3.2.3 misho 393: Sess->mem.shmid = shmget(Sess->key, nSize, MEM_MODE | IPC_CREAT);
394: if (Sess->mem.shmid == -1) {
1.1 misho 395: LOGERR;
396: ipc_destroySession(csFName, Sess);
397: return -1;
1.3.2.3 misho 398: } else
399: Sess->eom = nSize;
1.1 misho 400:
1.3.2.3 misho 401: return (int) Sess->zcpy;
1.1 misho 402: }
403:
404: /*
405: * ipc_destroySession() IPC free shared resources
1.3.2.3 misho 406: *
1.1 misho 407: * @csFName = Session name for delete
408: * @Sess = Session item
1.3.2.3 misho 409: * return: none
410: */
1.3 misho 411: void
1.3.2.3 misho 412: ipc_destroySession(const char *csFName, ait_sess_t * __restrict Sess)
1.1 misho 413: {
414: union semun sems;
415: struct shmid_ds ds;
416:
1.3.2.3 misho 417: if (!Sess || sess_isAttached(Sess))
1.1 misho 418: return;
419:
1.3.2.3 misho 420: if (Sess->id.semid != -1)
421: semctl(Sess->id.semid, 0, IPC_RMID, &sems);
422: if (Sess->mem.shmid != -1)
423: shmctl(Sess->mem.shmid, IPC_RMID, &ds);
424: Sess->eom ^= Sess->eom;
425: Sess->key ^= Sess->key;
1.1 misho 426: }
427:
428:
429: /*
430: * map_attachSession() MMAP Attach to shared memory & return begin address
1.3.2.3 misho 431: *
1.1 misho 432: * @s = Session item
433: * @procMem = Custom start address (optionl) *default must be 0*
434: * return: NULL failed attach, !=NULL begin address of memory
1.3.2.3 misho 435: */
436: void *
437: map_attachSession(ait_sess_t * __restrict s, void *procMem)
1.1 misho 438: {
439: if (!s)
440: return NULL;
441:
442: sync();
443:
1.3 misho 444: /* attach to memory */
1.1 misho 445: s->addr = mmap(procMem, s->eom, PROT_READ | PROT_WRITE, MAP_SHARED, s->mem.fd, 0);
446: if (s->addr == MAP_FAILED) {
447: LOGERR;
448: s->addr = NULL;
449: }
450:
451: return s->addr;
452: }
453:
454: /*
455: * map_detachSession() MMAP Detach from shared memory
1.3.2.3 misho 456: *
1.1 misho 457: * @s = Session item
1.3.2.3 misho 458: * return: none
459: */
460: void
461: map_detachSession(ait_sess_t * __restrict s)
1.1 misho 462: {
463: if (!s)
464: return;
465:
466: msync(s->addr, 0, MS_SYNC | MS_INVALIDATE);
467:
468: if (s->addr && s->eom) {
469: munmap(s->addr, s->eom);
470: s->addr = NULL;
471: }
472: }
473:
474: /*
475: * ipc_attachSession() IPC Attach to shared memory & return begin address
1.3.2.3 misho 476: *
1.1 misho 477: * @s = Session item
478: * @procMem = Custom start address (optionl) *default must be 0*
479: * return: NULL failed attach, !=NULL begin address of memory
1.3.2.3 misho 480: */
481: void *
482: ipc_attachSession(ait_sess_t * __restrict s, void *procMem)
1.1 misho 483: {
484: if (!s)
485: return NULL;
486:
487: s->addr = shmat(s->mem.shmid, procMem, 0);
488: if (s->addr == (void*) -1) {
489: LOGERR;
490: s->addr = NULL;
491: }
492:
493: return s->addr;
494: }
495:
496: /*
497: * ipc_detachSession() IPC Detach from shared memory
1.3.2.3 misho 498: *
1.1 misho 499: * @s = Session item
1.3.2.3 misho 500: * return: none
501: */
502: void
503: ipc_detachSession(ait_sess_t * __restrict s)
1.1 misho 504: {
505: if (!s)
506: return;
507:
508: if (s->addr) {
509: shmdt(s->addr);
510: s->addr = NULL;
511: }
512: }
513:
1.2 misho 514: /*
1.3.2.3 misho 515: * sess_isAttached() Check for attached shared memory
516: *
1.2 misho 517: * @s = Session item
518: * return: -1 null session item, 0 not attached, 1 attached memory
1.3.2.3 misho 519: */
1.3 misho 520: inline int
1.3.2.3 misho 521: sess_isAttached(ait_sess_t * __restrict s)
1.2 misho 522: {
523: if (!s)
524: return -1;
525:
526: return (s->addr ? 1 : 0);
527: }
528:
1.1 misho 529:
530: /*
531: * map_notSemaphore() MMAP negative block if semaphore isn`t signaled
1.3.2.3 misho 532: *
1.1 misho 533: * @s = Session item
1.3.2.3 misho 534: * return: none
535: */
536: void
537: map_notSemaphore(ait_sess_t * __restrict s)
1.1 misho 538: {
539: int i = -1;
540:
541: if (!s)
542: return;
543:
544: sem_getvalue(s->id.sid, &i);
1.3.2.3 misho 545: while (i > 0) {
1.1 misho 546: sem_wait(s->id.sid);
1.3.2.3 misho 547: i--;
548: }
1.1 misho 549: }
550:
551: /*
1.3.2.3 misho 552: * map_isSemaphoreOK() MMAP Check semaphore
553: *
1.1 misho 554: * @s = Session item
555: * return: -1 error: can`t return semaphore, 0 = false, 1 = true
1.3.2.3 misho 556: */
557: int
558: map_isSemaphoreOK(ait_sess_t * __restrict s)
1.1 misho 559: {
560: int val = -1;
561:
562: if (!s)
563: return -1;
564:
565: sem_getvalue(s->id.sid, &val);
1.3.2.3 misho 566: return (val ? 0 : 1);
1.1 misho 567: }
568:
569: /*
1.3.2.4 misho 570: * map_incSemaphore() MMAP unblock semaphore, increment semaphore
1.3.2.3 misho 571: *
1.1 misho 572: * @s = Session item
573: * return: 0 Ok, -1 error: can`t increment
1.3.2.3 misho 574: */
575: int
1.3.2.4 misho 576: map_incSemaphore(ait_sess_t * __restrict s)
1.1 misho 577: {
578: if (!s)
579: return -1;
580:
581: return sem_post(s->id.sid);
582: }
583:
584: /*
585: * map_decSemaphore() MMAP block semaphore, decrement semaphore
1.3.2.3 misho 586: *
1.1 misho 587: * @s = Session item
588: * return: 0 Ok, -1 error: can`t decrement
1.3.2.3 misho 589: */
590: int
591: map_decSemaphore(ait_sess_t * __restrict s)
1.1 misho 592: {
593: if (!s)
594: return -1;
595:
596: return sem_wait(s->id.sid);
597: }
598:
599: /*
600: * ipc_notSemaphore() IPC negative block if semaphore isn`t signaled
1.3.2.3 misho 601: *
1.1 misho 602: * @s = Session item
1.3.2.3 misho 603: * return: none
604: */
605: void
606: ipc_notSemaphore(ait_sess_t * __restrict s)
1.1 misho 607: {
608: struct sembuf sb = { 0, 0, 0 };
609:
610: if (s)
611: semop(s->id.semid, &sb, 1);
612: }
613:
614: /*
1.3.2.3 misho 615: * ipc_isSemaphoreOK() IPC Check semaphore
616: *
1.1 misho 617: * @s = Session item
618: * return: -1 error: can`t return semaphore, 0 = false, 1 = true
1.3.2.3 misho 619: */
620: int
621: ipc_isSemaphoreOK(ait_sess_t * __restrict s)
1.1 misho 622: {
623: struct sembuf sb = { 0, 0, IPC_NOWAIT };
624:
625: if (!s)
626: return -1;
627:
628: return semop(s->id.semid, &sb, 1) + 1;
629: }
630:
631: /*
1.3.2.4 misho 632: * ipc_incSemaphore() IPC unblock semaphore, increment semaphore
1.3.2.3 misho 633: *
1.1 misho 634: * @s = Session item
635: * return: 0 Ok, -1 error: can`t increment
1.3.2.3 misho 636: */
637: int
1.3.2.4 misho 638: ipc_incSemaphore(ait_sess_t * __restrict s)
1.1 misho 639: {
640: struct sembuf sb = { 0, 1, 0 };
641:
642: if (!s)
643: return -1;
644:
645: return semop(s->id.semid, &sb, 1);
646: }
647:
648: /*
649: * ipc_decSemaphore() IPC block semaphore, decrement semaphore
1.3.2.3 misho 650: *
1.1 misho 651: * @s = Session item
652: * return: 0 Ok, -1 error: can`t decrement
1.3.2.3 misho 653: */
654: int
655: ipc_decSemaphore(ait_sess_t * __restrict s)
1.1 misho 656: {
657: struct sembuf sb = { 0, -1, 0 };
658:
659: if (!s)
660: return -1;
661:
662: return semop(s->id.semid, &sb, 1);
663: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>