Annotation of libaitsess/inc/aitsess.h, revision 1.2.2.1
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.2.2.1 ! misho 6: * $Id: aitsess.h,v 1.2 2011/04/30 21:35:21 misho Exp $
1.2 misho 7: *
1.2.2.1 ! 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: #ifndef __AITSESS_H
47: #define __AITSESS_H
48:
49:
50: #include <semaphore.h>
51: #include <sys/types.h>
52:
53: #define SHARED_IPC 1
54: #define SHARED_MAP 2
55:
56: #define IS_VAL 0x0
57: #define IS_ADD 0x40000000
58: #define IS_DEF 0x80000000
59:
60: #define ISNEW(x) (((x) & IS_ADD) == IS_ADD)
61: #define ISDEF(x) (((x) & IS_DEF) == IS_DEF)
62:
63: #define MAX_ATTRIBUTE 63
64: #define MAX_SEMNAME 14
65:
66:
67: typedef struct _tagSess {
68: key_t key;
69: u_char type;
70: off_t eom;
71: void *addr;
72: off_t offset;
73: union {
74: int shmid;
75: int fd;
76: } mem;
77: union {
78: int semid;
79: sem_t *sid;
80: } id;
81: } tagSess;
82:
83:
84: // -------------------------------------------------------
85: // sess_GetErrno() Get error code of last operation
86: inline int sess_GetErrno();
87: // sess_GetError() Get error text of last operation
88: inline const char *sess_GetError();
89: // -------------------------------------------------------
90:
91:
92: /*
93: * initSession() Initializing session structure, if session file not exists creating with specified tech
94: * @cnID = Technology using in session. SHARED_IPC IPC tech; SHARED_MAP BSD MemoryMap tech
95: * @csFName = Session filename for build key and identified
96: * @Sess = Session item
97: * return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded
98: */
99: inline int initSession(const int cnID, const char *csFName, tagSess ** __restrict Sess);
100: /*
101: * freeSession() Free allocated memory for session item and delete session file if present name
102: * @csFName = Session filename for delete, if NULL nothing delete
103: * @Sess = Session item
104: */
105: inline void freeSession(const char *csFName, tagSess ** __restrict Sess);
106:
107: /*
108: * map_createSession() MMAP Created session and allocated resources
109: * @csFName = Session name for identified
110: * @cnSeed = Seed for securing key
111: * @cnSize = Allocated shared memory size in bytes
112: * @Sess = Session item
113: * return: 0 Ok successful, -1 error: not allocated resources
114: */
115: int map_createSession(const char *csFName, const int cnSeed, const u_int cnSize, tagSess ** __restrict Sess);
116: /*
117: * map_destroySession() MMAP free shared resources
118: * @csFName = Session name for delete
119: * @Sess = Session item
120: */
121: void map_destroySession(const char *csFName, tagSess ** __restrict Sess);
122:
123: /*
124: * ipc_createSession() IPC Created session and allocated resources
125: * @csFName = Session name for identified
126: * @cnSeed = Seed for securing key
127: * @cnSize = Allocated shared memory size in bytes
128: * @Sess = Session item
129: * return: 0 Ok successful, -1 error: not allocated resources
130: */
131: int ipc_createSession(const char *csFName, const int cnSeed, const u_int cnSize, tagSess ** __restrict Sess);
132: /*
133: * ipc_destroySession() IPC free shared resources
134: * @csFName = Session name for delete
135: * @Sess = Session item
136: */
137: void ipc_destroySession(const char *csFName, tagSess ** __restrict Sess);
138:
139: /*
140: * map_attachSession() MMAP Attach to shared memory & return begin address
141: * @s = Session item
142: * @procMem = Custom start address (optionl) *default must be 0*
143: * return: NULL failed attach, !=NULL begin address of memory
144: */
145: inline void *map_attachSession(tagSess * __restrict s, void *procMem);
146: /*
147: * map_detachSession() MMAP Detach from shared memory
148: * @s = Session item
149: */
150: inline void map_detachSession(tagSess * __restrict s);
151:
152: /*
153: * ipc_attachSession() IPC Attach to shared memory & return begin address
154: * @s = Session item
155: * @procMem = Custom start address (optionl) *default must be 0*
156: * return: NULL failed attach, !=NULL begin address of memory
157: */
158: inline void *ipc_attachSession(tagSess * __restrict s, void *procMem);
159: /*
160: * ipc_detachSession() IPC Detach from shared memory
161: * @s = Session item
162: */
163: inline void ipc_detachSession(tagSess * __restrict s);
164:
165: /*
1.2 misho 166: * isAttached() Check for mapped/(attached) shared memory
167: * @s = Session item
168: * return: -1 null session item, 0 not attached, 1 attached memory
169: */
170: inline int isAttached(tagSess * __restrict s);
171:
172:
173: /*
1.1 misho 174: * map_notSemaphore() MMAP negative block if semaphore isn`t signaled
175: * @s = Session item
176: */
177: inline void map_notSemaphore(tagSess * __restrict s);
178: /*
179: * map_isSemaphored() MMAP Check semaphore
180: * @s = Session item
181: * return: -1 error: can`t return semaphore, 0 = false, 1 = true
182: */
183: inline int map_isSemaphored(tagSess * __restrict s);
184: /*
185: * map_addSemaphore() MMAP unblock semaphore, increment semaphore
186: * @s = Session item
187: * return: 0 Ok, -1 error: can`t increment
188: */
189: inline int map_addSemaphore(tagSess * __restrict s);
190: /*
191: * map_decSemaphore() MMAP block semaphore, decrement semaphore
192: * @s = Session item
193: * return: 0 Ok, -1 error: can`t decrement
194: */
195: inline int map_decSemaphore(tagSess * __restrict s);
196:
197: /*
198: * ipc_notSemaphore() IPC negative block if semaphore isn`t signaled
199: * @s = Session item
200: */
201: inline void ipc_notSemaphore(tagSess * __restrict s);
202: /*
203: * ipc_isSemaphored() IPC Check semaphore
204: * @s = Session item
205: * return: -1 error: can`t return semaphore, 0 = false, 1 = true
206: */
207: inline int ipc_isSemaphored(tagSess * __restrict s);
208: /*
209: * ipc_addSemaphore() IPC unblock semaphore, increment semaphore
210: * @s = Session item
211: * return: 0 Ok, -1 error: can`t increment
212: */
213: inline int ipc_addSemaphore(tagSess * __restrict s);
214: /*
215: * ipc_decSemaphore() IPC block semaphore, decrement semaphore
216: * @s = Session item
217: * return: 0 Ok, -1 error: can`t decrement
218: */
219: inline int ipc_decSemaphore(tagSess * __restrict s);
220:
1.2 misho 221:
1.1 misho 222: // ---------------------------------------------------------
223: //
224: // Lazy macros for lazy programmers :-) by Michael Pounov; Optimizing work with sessions!
225: //
226:
227: #define DESTROY_SESSION(fname, s) do { \
1.2 misho 228: switch (((*s))->type) { \
1.1 misho 229: case SHARED_IPC: \
230: ipc_destroySession((fname), (s)); \
231: break; \
232: case SHARED_MAP: \
233: map_destroySession((fname), (s)); \
234: break; \
235: } \
236: } while(0)
237:
1.2 misho 238: #define ATTACH_MEMORY(s) do { \
1.1 misho 239: switch ((s)->type) { \
240: case SHARED_IPC: \
1.2 misho 241: ipc_attachSession((s), 0); \
1.1 misho 242: break; \
243: case SHARED_MAP: \
1.2 misho 244: map_attachSession((s), 0); \
1.1 misho 245: break; \
246: } \
247: } while(0)
248:
249: #define DETACH_MEMORY(s) do { \
250: switch ((s)->type) { \
251: case SHARED_IPC: \
252: ipc_detachSession((s)); \
253: break; \
254: case SHARED_MAP: \
255: map_detachSession((s)); \
256: break; \
257: } \
258: } while(0)
259:
260: #define NOT_SEMAPHORE(s) do { \
261: switch ((s)->type) { \
262: case SHARED_IPC: \
263: ipc_notSemaphore((s)); \
264: break; \
265: case SHARED_MAP: \
266: map_notSemaphore((s)); \
267: break; \
268: } \
269: } while(0)
270:
271: #define IS_SEMAPHORED(s, ret) do { \
272: switch ((s)->type) { \
273: case SHARED_IPC: \
274: (ret) = ipc_isSemaphored((s)); \
275: break; \
276: case SHARED_MAP: \
277: (ret) = map_isSemaphored((s)); \
278: break; \
279: default: \
280: (ret) = -1; \
281: } \
282: } while(0)
283:
284: #define ADD_SEMAPHORE(s, ret) do { \
285: switch ((s)->type) { \
286: case SHARED_IPC: \
287: (ret) = ipc_addSemaphore((s)); \
288: break; \
289: case SHARED_MAP: \
290: (ret) = map_addSemaphore((s)); \
291: break; \
292: default: \
293: (ret) = -1; \
294: } \
295: } while(0)
296:
297: #define DEC_SEMAPHORE(s, ret) do { \
298: switch ((s)->type) { \
299: case SHARED_IPC: \
300: (ret) = ipc_decSemaphore((s)); \
301: break; \
302: case SHARED_MAP: \
303: (ret) = map_decSemaphore((s)); \
304: break; \
305: default: \
306: (ret) = -1; \
307: } \
308: } while(0)
309:
310: // ---------------------------------------------------------
311:
312: /*
1.2 misho 313: * sess_FreeValues() Free all values from value array allocated from sess_GetValues()
314: * @ppsVals = Array strings
315: * return: none
316: */
317: inline void sess_FreeValues(char *** __restrict ppsVals);
318: /*
319: * sess_GetValues() Get all values from session shared memory
320: * @s = Session item
321: * @ppsVals = Return array strings
322: * return: -1 error: in parameter, !=-1 count of returned strings in ppsVals (must be free after use!)
323: */
324: int sess_GetValues(tagSess * __restrict s, char ***ppsVals);
325: /*
1.1 misho 326: * sess_GetValue() Get value from session shared memory from attribute
327: * @s = Session item
328: * @csAttr = Attribute for search
329: * @psVal = Return string buffer
330: * @pnLen = Length of return string buffer,
331: // *{pnLen} input is max_size of buffer & output is really taken bytes
332: * return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF
333: */
334: int sess_GetValue(tagSess * __restrict s, const char *csAttr, char *psVal, int *pnLen);
335: /*
336: * sess_DelValue() Delete item from session shared memory
337: * @s = Session item
338: * @csAttr = Attribute for erasing
339: * return: 0 Ok, -1 error: in parameter
340: */
341: int sess_DelValue(tagSess * __restrict s, const char *csAttr);
342: /*
343: * sess_SetValue() Set item into session shared memory or update if find it
344: * @s = Session item
345: * @csAttr = Attribute
346: * @psVal = Value
347: * return: 0 nothing, -1 error: in parameter,
348: >0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF
349: */
350: int sess_SetValue(tagSess * __restrict s, const char *csAttr, const char *psVal);
351:
352:
353: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>