1: /*************************************************************************
2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
6: * $Id: aitsess.h,v 1.1.1.1.2.1 2008/09/30 20:19:48 misho Exp $
7: *
8: *************************************************************************/
9: #ifndef __AITSESS_H
10: #define __AITSESS_H
11:
12:
13: #include <semaphore.h>
14: #include <sys/types.h>
15:
16: #define SHARED_IPC 1
17: #define SHARED_MAP 2
18:
19: #define IS_VAL 0x0
20: #define IS_ADD 0x40000000
21: #define IS_DEF 0x80000000
22:
23: #define ISNEW(x) (((x) & IS_ADD) == IS_ADD)
24: #define ISDEF(x) (((x) & IS_DEF) == IS_DEF)
25:
26: #define MAX_ATTRIBUTE 63
27: #define MAX_SEMNAME 14
28:
29:
30: typedef struct _tagSess {
31: key_t key;
32: u_char type;
33: off_t eom;
34: void *addr;
35: off_t offset;
36: union {
37: int shmid;
38: int fd;
39: } mem;
40: union {
41: int semid;
42: sem_t *sid;
43: } id;
44: } tagSess;
45:
46:
47: // -------------------------------------------------------
48: // sess_GetErrno() Get error code of last operation
49: inline int sess_GetErrno();
50: // sess_GetError() Get error text of last operation
51: inline const char *sess_GetError();
52: // -------------------------------------------------------
53:
54:
55: /*
56: * initSession() Initializing session structure, if session file not exists creating with specified tech
57: * @cnID = Technology using in session. SHARED_IPC IPC tech; SHARED_MAP BSD MemoryMap tech
58: * @csFName = Session filename for build key and identified
59: * @Sess = Session item
60: * return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded
61: */
62: inline int initSession(const int cnID, const char *csFName, tagSess ** __restrict Sess);
63: /*
64: * freeSession() Free allocated memory for session item and delete session file if present name
65: * @csFName = Session filename for delete, if NULL nothing delete
66: * @Sess = Session item
67: */
68: inline void freeSession(const char *csFName, tagSess ** __restrict Sess);
69:
70: /*
71: * map_createSession() MMAP Created session and allocated resources
72: * @csFName = Session name for identified
73: * @cnSeed = Seed for securing key
74: * @cnSize = Allocated shared memory size in bytes
75: * @Sess = Session item
76: * return: 0 Ok successful, -1 error: not allocated resources
77: */
78: int map_createSession(const char *csFName, const int cnSeed, const u_int cnSize, tagSess ** __restrict Sess);
79: /*
80: * map_destroySession() MMAP free shared resources
81: * @csFName = Session name for delete
82: * @Sess = Session item
83: */
84: void map_destroySession(const char *csFName, tagSess ** __restrict Sess);
85:
86: /*
87: * ipc_createSession() IPC Created session and allocated resources
88: * @csFName = Session name for identified
89: * @cnSeed = Seed for securing key
90: * @cnSize = Allocated shared memory size in bytes
91: * @Sess = Session item
92: * return: 0 Ok successful, -1 error: not allocated resources
93: */
94: int ipc_createSession(const char *csFName, const int cnSeed, const u_int cnSize, tagSess ** __restrict Sess);
95: /*
96: * ipc_destroySession() IPC free shared resources
97: * @csFName = Session name for delete
98: * @Sess = Session item
99: */
100: void ipc_destroySession(const char *csFName, tagSess ** __restrict Sess);
101:
102: /*
103: * map_attachSession() MMAP Attach to shared memory & return begin address
104: * @s = Session item
105: * @procMem = Custom start address (optionl) *default must be 0*
106: * return: NULL failed attach, !=NULL begin address of memory
107: */
108: inline void *map_attachSession(tagSess * __restrict s, void *procMem);
109: /*
110: * map_detachSession() MMAP Detach from shared memory
111: * @s = Session item
112: */
113: inline void map_detachSession(tagSess * __restrict s);
114:
115: /*
116: * ipc_attachSession() IPC Attach to shared memory & return begin address
117: * @s = Session item
118: * @procMem = Custom start address (optionl) *default must be 0*
119: * return: NULL failed attach, !=NULL begin address of memory
120: */
121: inline void *ipc_attachSession(tagSess * __restrict s, void *procMem);
122: /*
123: * ipc_detachSession() IPC Detach from shared memory
124: * @s = Session item
125: */
126: inline void ipc_detachSession(tagSess * __restrict s);
127:
128: /*
129: * map_notSemaphore() MMAP negative block if semaphore isn`t signaled
130: * @s = Session item
131: */
132: inline void map_notSemaphore(tagSess * __restrict s);
133: /*
134: * map_isSemaphored() MMAP Check semaphore
135: * @s = Session item
136: * return: -1 error: can`t return semaphore, 0 = false, 1 = true
137: */
138: inline int map_isSemaphored(tagSess * __restrict s);
139: /*
140: * map_addSemaphore() MMAP unblock semaphore, increment semaphore
141: * @s = Session item
142: * return: 0 Ok, -1 error: can`t increment
143: */
144: inline int map_addSemaphore(tagSess * __restrict s);
145: /*
146: * map_decSemaphore() MMAP block semaphore, decrement semaphore
147: * @s = Session item
148: * return: 0 Ok, -1 error: can`t decrement
149: */
150: inline int map_decSemaphore(tagSess * __restrict s);
151:
152: /*
153: * ipc_notSemaphore() IPC negative block if semaphore isn`t signaled
154: * @s = Session item
155: */
156: inline void ipc_notSemaphore(tagSess * __restrict s);
157: /*
158: * ipc_isSemaphored() IPC Check semaphore
159: * @s = Session item
160: * return: -1 error: can`t return semaphore, 0 = false, 1 = true
161: */
162: inline int ipc_isSemaphored(tagSess * __restrict s);
163: /*
164: * ipc_addSemaphore() IPC unblock semaphore, increment semaphore
165: * @s = Session item
166: * return: 0 Ok, -1 error: can`t increment
167: */
168: inline int ipc_addSemaphore(tagSess * __restrict s);
169: /*
170: * ipc_decSemaphore() IPC block semaphore, decrement semaphore
171: * @s = Session item
172: * return: 0 Ok, -1 error: can`t decrement
173: */
174: inline int ipc_decSemaphore(tagSess * __restrict s);
175:
176: // ---------------------------------------------------------
177: //
178: // Lazy macros for lazy programmers :-) by Michael Pounov; Optimizing work with sessions!
179: //
180:
181: #define DESTROY_SESSION(fname, s) do { \
182: switch ((s)->type) { \
183: case SHARED_IPC: \
184: ipc_destroySession((fname), (s)); \
185: break; \
186: case SHARED_MAP: \
187: map_destroySession((fname), (s)); \
188: break; \
189: } \
190: } while(0)
191:
192: #define ATTACH_MEMORY(s, shared) do { \
193: switch ((s)->type) { \
194: case SHARED_IPC: \
195: (shared) = ipc_attachSession((s), 0); \
196: break; \
197: case SHARED_MAP: \
198: (shared) = map_attachSession((s), 0); \
199: break; \
200: } \
201: } while(0)
202:
203: #define DETACH_MEMORY(s) do { \
204: switch ((s)->type) { \
205: case SHARED_IPC: \
206: ipc_detachSession((s)); \
207: break; \
208: case SHARED_MAP: \
209: map_detachSession((s)); \
210: break; \
211: } \
212: } while(0)
213:
214: #define NOT_SEMAPHORE(s) do { \
215: switch ((s)->type) { \
216: case SHARED_IPC: \
217: ipc_notSemaphore((s)); \
218: break; \
219: case SHARED_MAP: \
220: map_notSemaphore((s)); \
221: break; \
222: } \
223: } while(0)
224:
225: #define IS_SEMAPHORED(s, ret) do { \
226: switch ((s)->type) { \
227: case SHARED_IPC: \
228: (ret) = ipc_isSemaphored((s)); \
229: break; \
230: case SHARED_MAP: \
231: (ret) = map_isSemaphored((s)); \
232: break; \
233: default: \
234: (ret) = -1; \
235: } \
236: } while(0)
237:
238: #define ADD_SEMAPHORE(s, ret) do { \
239: switch ((s)->type) { \
240: case SHARED_IPC: \
241: (ret) = ipc_addSemaphore((s)); \
242: break; \
243: case SHARED_MAP: \
244: (ret) = map_addSemaphore((s)); \
245: break; \
246: default: \
247: (ret) = -1; \
248: } \
249: } while(0)
250:
251: #define DEC_SEMAPHORE(s, ret) do { \
252: switch ((s)->type) { \
253: case SHARED_IPC: \
254: (ret) = ipc_decSemaphore((s)); \
255: break; \
256: case SHARED_MAP: \
257: (ret) = map_decSemaphore((s)); \
258: break; \
259: default: \
260: (ret) = -1; \
261: } \
262: } while(0)
263:
264: // ---------------------------------------------------------
265:
266: /*
267: * sess_GetValue() Get value from session shared memory from attribute
268: * @s = Session item
269: * @csAttr = Attribute for search
270: * @psVal = Return string buffer
271: * @pnLen = Length of return string buffer,
272: // *{pnLen} input is max_size of buffer & output is really taken bytes
273: * return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF
274: */
275: int sess_GetValue(tagSess * __restrict s, const char *csAttr, char *psVal, int *pnLen);
276: /*
277: * sess_DelValue() Delete item from session shared memory
278: * @s = Session item
279: * @csAttr = Attribute for erasing
280: * return: 0 Ok, -1 error: in parameter
281: */
282: int sess_DelValue(tagSess * __restrict s, const char *csAttr);
283: /*
284: * sess_SetValue() Set item into session shared memory or update if find it
285: * @s = Session item
286: * @csAttr = Attribute
287: * @psVal = Value
288: * return: 0 nothing, -1 error: in parameter,
289: >0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF
290: */
291: int sess_SetValue(tagSess * __restrict s, const char *csAttr, const char *psVal);
292:
293:
294: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>