source: trunk/src/emx/include/InnoTekLIBC/sharedpm.h@ 1461

Last change on this file since 1461 was 1461, checked in by bird, 21 years ago

Fixing, extending, optimizing.

  • Property cvs2svn:cvs-rev set to 1.5
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 16.5 KB
Line 
1/** $Id: sharedpm.h 1461 2004-09-06 04:22:46Z bird $ */
2/** @file
3 *
4 * LIBC Shared Process Management.
5 *
6 *
7 *
8 * This file is part of InnoTek LIBC.
9 *
10 * InnoTek LIBC is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * InnoTek LIBC is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with InnoTek LIBC; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
26#ifndef __InnoTekLIBC_sharedpm_h__
27#define __InnoTekLIBC_sharedpm_h__
28
29#include <sys/cdefs.h>
30#include <sys/types.h>
31
32__BEGIN_DECLS
33
34/** The name of the shared semaphore protecting the memory. */
35#define SPM_MUTEX_NAME "\\SEM32\\INNOTEKLIBC.V01"
36
37/** The name of the shared memory. */
38#define SPM_MEMORY_NAME "\\SHAREMEM\\INNOTEKLIBC.V01"
39
40/** The timeout for accessing the shared mem semaphore. */
41#define SPM_MUTEX_TIMEOUT (30*1000)
42
43/** Allocate 512KB if OBJ_ANY isn't supported.
44 * If it is supported we allocated the double amount. */
45#define SPM_MEMORY_SIZE (512*1024)
46
47/** The defined max size of a process.
48 * A good bit is reserved for the future here.
49 *
50 * Theoretically speaking, OS/2 cannot entertain more than a 1000 processes.
51 * So, even if we reserve a good bit here it's not gonna cause any trouble.
52 */
53#define SPM_PROCESS_SIZE (256)
54
55/**
56 * The SPM version.
57 */
58#define SPM_VERSION 0x00010000
59
60/**
61 * Process state.
62 */
63typedef enum __LIBC_SPMPROCSTAT
64{
65 /** The process is free. */
66 __LIBC_PROCSTATE_FREE = 0,
67 /** The process is waiting for a chile to be created and snatch all the
68 * inherited goodies from it.
69 * If not caught before DosExecPgm/DosStartSession returns
70 * it will be freed by the disapointed parent.
71 */
72 __LIBC_PROCSTATE_EMBRYO = 1,
73 /** The process is running. */
74 __LIBC_PROCSTATE_ALIVE = 2,
75 /** The process is dead but other guys are still using it's data. */
76 __LIBC_PROCSTATE_ZOMBIE = 3,
77 /** The maximum state number (exclusive). */
78 __LIBC_PROCSTATE_MAX = 10,
79 /** Hack to ensure that this will be a 32-bit int. */
80 __LIBC_PROCSTATE_MAKE_INT32 = 0x7fffffff
81} __LIBC_SPMPROCSTAT;
82
83/**
84 * Process termination reason.
85 */
86typedef enum __LIBC_EXIT_REASON
87{
88 /** No reason. */
89 __LIBC_EXIT_REASON_NONE = 0,
90 /** Normal exit. */
91 __LIBC_EXIT_REASON_EXIT = 1,
92 /** OS/2 hard error. */
93 __LIBC_EXIT_REASON_HARDERROR = 2,
94 /** Trap (i.e. 16-bit?). */
95 __LIBC_EXIT_REASON_TRAP = 3,
96 /** Kill process. */
97 __LIBC_EXIT_REASON_KILL = 4,
98 /** Exception. */
99 __LIBC_EXIT_REASON_XCPT = 5,
100 /** Signal base. */
101 __LIBC_EXIT_REASON_SIGNAL_BASE = 32,
102 /** Signal max. */
103 __LIBC_EXIT_REASON_SIGNAL_MAX = 256,
104 /** Hack to ensure that this will be a 32-bit int. */
105 __LIBC_EXIT_REASON_MAKE_INT32 = 0x7fffffff
106} __LIBC_EXIT_REASON;
107
108/** Inheritance FH Bundle Type.
109 * @{ */
110/** Termination bundle. */
111#define __LIBC_SPM_INH_FHB_TYPE_END (0)
112/** Standard bundle. */
113#define __LIBC_SPM_INH_FHB_TYPE_STANDARD (0x10 | sizeof(unsigned))
114/** Socket bundle, using the BSD 4.4 backend. */
115#define __LIBC_SPM_INH_FHB_TYPE_SOCKET_44 (0x20 | (sizeof(unsigned) + sizeof(unsigned short)))
116/** Socket bundle, using the BSD 4.3 backend. */
117#define __LIBC_SPM_INH_FHB_TYPE_SOCKET_43 (0x30 | (sizeof(unsigned) + sizeof(unsigned short)))
118/** Get the per file handle size from the bundle type. */
119#define __LIBC_SPM_INH_FHB_SIZE(type) ((type) & 0x0f)
120/** @} */
121
122/**
123 * SPM Inheritance File Handle Bundle Header.
124 */
125#pragma pack(1)
126typedef struct __libc_SPMInhFHBHdr
127{
128 /** Bundle type. */
129 unsigned char uchType;
130 /** Cound of handles in this bundle. */
131 unsigned char cHandles;
132 /** Start handle number. */
133 unsigned short fhStart;
134} __LIBC_SPMINHFHBHDR;
135#pragma pack()
136/** Pointer to SPM filehandle bundle header. */
137typedef __LIBC_SPMINHFHBHDR *__LIBC_PSPMINHFHBHDR;
138
139/**
140 * SPM standard filehandle inherit bundle
141 * This is used for OS/2 filehandles which only needs flags
142 * transfered.
143 */
144#pragma pack(1)
145typedef struct __libc_SPMInhFH
146{
147 /** The common bundle header. */
148 __LIBC_SPMINHFHBHDR Hdr;
149 /** Array of flags of Hdr.cHandles entries. */
150 unsigned afFlags[1];
151} __LIBC_SPMINHFHBSTD;
152#pragma pack()
153/** Pointer to SPM standard filehandle inherit bundle. */
154typedef __LIBC_SPMINHFHBSTD *__LIBC_PSPMINHFHBSTD;
155
156/**
157 * SPM socket filehandle inherit bundle.
158 *
159 * This is used for both BSD 4.3 and 4.4 sockets. The data needed
160 * here is the flags and the socket number.
161 */
162#pragma pack(1)
163typedef struct __libc_SPMInhFHSocket
164{
165 /** The common bundle header. */
166 __LIBC_SPMINHFHBHDR Hdr;
167 /** Array of flags of Hdr.cHandles entries. */
168 struct
169 {
170 /** The file handle flags. */
171 unsigned fFlags;
172 /** The socket number. */
173 unsigned short usSocket;
174 } aHandles[1];
175} __LIBC_SPMINHFHBSOCK;
176#pragma pack()
177/** Pointer to SPM socket filehandle inherit bundle. */
178typedef __LIBC_SPMINHFHBSOCK *__LIBC_PSPMINHFHBSOCK;
179
180
181/**
182 * Inherit structure.
183 *
184 * All the data it contains is allocated in one block heap block!
185 */
186typedef struct __libc_SPMInherit
187{
188 /** Size of the inherit structure. */
189 size_t cb;
190 /** Pointer to the filehandle part.
191 * This is a succession of bundles terminating with a _END one. */
192 __LIBC_PSPMINHFHBHDR pFHBundles;
193
194 /** @todo Add signals and the other properties which are inherited. */
195} __LIBC_SPMINHERIT;
196/** Pointer to inherit data. */
197typedef __LIBC_SPMINHERIT *__LIBC_PSPMINHERIT;
198
199
200/**
201 * Per Process Data.
202 */
203typedef struct __libc_SPMProcess
204{
205 /** Pointer to the next process in the list.
206 * Every process is in one or another list depending on their state. */
207 struct __libc_SPMProcess *pNext;
208 /** Pointer to the previous process in the list. */
209 struct __libc_SPMProcess *pPrev;
210
211 /** Version of the SPM which created this process. */
212 unsigned uVersion;
213 /** Number of references to this process.
214 * A process is not actually free till the reference count
215 * reaches 0. */
216 unsigned cReferences;
217 /** Process id. */
218 pid_t pid;
219 /** Process id of parent. */
220 pid_t pidParent;
221 /** State of this process. */
222 __LIBC_SPMPROCSTAT enmState;
223 /** The SPM open count of this process. */
224 unsigned cSPMOpens;
225 /** Exit code. */
226 int iExitCode;
227 /** Reason code. */
228 __LIBC_EXIT_REASON enmDeathReason;
229 /** Pointer to fork module list head. */
230 void * volatile pvModuleHead;
231 /** Pointer to fork module list tail pointer. */
232 void * volatile * volatile ppvModuleTail;
233 /** Pointer to fork handle which a child should use when spawned by fork(). */
234 void *pvForkHandle;
235 /** Creation timestamp. */
236 unsigned uTimestamp;
237
238 /** Reserved pool pointer field with default value 0. */
239 unsigned aReserved[40 - 14];
240
241 /** Number of possible pointers to shared memory starting at pvInherit.
242 * This means we can extend the structure with pointers for as long as we
243 * want and still have older LIBC versions cleanup the mess. */
244 unsigned cPoolPointers;
245 /** Pointer to data inherited from the parent process. */
246 __LIBC_PSPMINHERIT volatile pInherit;
247 /** Pointer to data inherited from the parent process when it's locked.
248 * When locked pInherit is NULL and this member points to the data instead.
249 * This prevents spmAlloc() from reclaiming it while it's in use. */
250 __LIBC_PSPMINHERIT volatile pInheritLocked;
251} __LIBC_SPMPROCESS, *__LIBC_PSPMPROCESS;
252
253
254/**
255 * Pool chunk.
256 */
257typedef struct __libc_SPMPoolChunk
258{
259 /** Next block in the list of all blocks.. */
260 struct __libc_SPMPoolChunk *pPrev;
261 /** Previous block in the list of all blocks.. */
262 struct __libc_SPMPoolChunk *pNext;
263} __LIBC_SPMPOOLCHUNK, *__LIBC_PSPMPOOLCHUNK;
264
265/**
266 * Free pool chunk.
267 */
268typedef struct __libc_SPMPoolChunkFree
269{
270 /** Main list. */
271 __LIBC_SPMPOOLCHUNK core;
272 /** Next block in list of free nodes. */
273 struct __libc_SPMPoolChunkFree *pPrev;
274 /** Previous block in list of free nodes. */
275 struct __libc_SPMPoolChunkFree *pNext;
276 /** Size of this block. */
277 size_t cb;
278} __LIBC_SPMPOOLCHUNKFREE, *__LIBC_PSPMPOOLCHUNKFREE;
279
280/**
281 * Pool chunk alignment.
282 */
283#define SPM_POOL_ALIGN(size) (((size) + 7) & ~7)
284
285/**
286 * This structure contains the global TCP/IP socket data.
287 */
288typedef struct __libc_SPMTcpIp
289{
290 /** Size of the structure. */
291 size_t cb;
292 /** The number of elements in the acRefs array. */
293 unsigned cSockets;
294 /** Array containing the references counters for the sockets in the system. */
295 uint16_t acRefs[32768];
296} __LIBC_SPMTCPIP, *__LIBC_PSPMTCPIP;
297
298
299/**
300 * This is the header of the LIBC shared memory.
301 */
302typedef struct __libc_SPMHeader
303{
304 /** SPM version. (the one which initialized the memory) */
305 unsigned uVersion;
306
307 /** Size of the shared memory. */
308 size_t cb;
309 /** Free memory. */
310 size_t cbFree;
311 /** Start chunk of shared memory pool. */
312 __LIBC_PSPMPOOLCHUNK pPoolHead;
313 /** End chunk of shared memory pool. */
314 __LIBC_PSPMPOOLCHUNK pPoolTail;
315 /** Start free chunk in the shared memory pool. */
316 __LIBC_PSPMPOOLCHUNKFREE pPoolFreeHead;
317 /** End free chunk in the shared memory pool. */
318 __LIBC_PSPMPOOLCHUNKFREE pPoolFreeTail;
319
320 /** The size of one process block in this version of the shared memory. */
321 size_t cbProcess;
322 /** Array index by process state giving the head pointers
323 * to the processes in that state. */
324 __LIBC_PSPMPROCESS volatile apHeads[__LIBC_PROCSTATE_MAX];
325
326 /** Pointer to the tcpip globals. */
327 __LIBC_PSPMTCPIP pTcpip;
328 /** Creator pid. */
329 pid_t pidCreate;
330 /** Creation timestamp. */
331 __extension__ union
332 {
333#ifdef INCL_DOSDATETIME
334 DATETIME dtCreate;
335#endif
336 char ach[16];
337 };
338 /* The rest of the block, up to cbProcess, is undefined in this version.
339 * Future versions of LIBC may use this area assuming it's initalized with zeros.
340 */
341} __LIBC_SPMHEADER, *__LIBC_PSPMHEADER;
342
343
344/**
345 * SPM Exception handler registration record.
346 */
347typedef struct __LIBC_SPMXCPTREGREC
348{
349 __extension__ union
350 {
351#ifdef INCL_DOSEXCEPTIONS
352 EXCEPTIONREGISTRATIONRECORD Core;
353#endif
354 void *apv[2];
355 };
356 unsigned uFutureStuff0;
357 unsigned uFutureStuff1;
358} __LIBC_SPMXCPTREGREC;
359/** Pointer to SPM exception handler registration record. */
360typedef __LIBC_SPMXCPTREGREC *__LIBC_PSPMXCPTREGREC;
361
362
363
364
365/**
366 * Gets the current process.
367 *
368 * @returns Pointer to the current process.
369 * @returns NULL and errno on failure.
370 * @remark For this too __libc_spmRelease() must be called when done.
371 */
372__LIBC_PSPMPROCESS __libc_spmSelf(void);
373
374/**
375 * Gets the inherit data associated with the current process.
376 * This call prevents it from being release by underrun handling.
377 *
378 * @returns Pointer to inherit data.
379 * The caller must call __libc_spmInheritRelease() when done.
380 * @returns NULL and errno if no inherit data.
381 */
382__LIBC_PSPMINHERIT __libc_spmInheritRequest(void);
383
384/**
385 * Releases the inherit data locked by the __libc_spmInheritRequest() call.
386 *
387 * @returns 0 on success.
388 * @returns -1 and errno on failure.
389 */
390int __libc_spmInheritRelease(void);
391
392/**
393 * Frees the inherit data of this process.
394 * This is called when the executable is initialized.
395 */
396void __libc_spmInheritFree(void);
397
398/**
399 * Create an embryo related to the current process.
400 *
401 * @returns pointer to the embryo process.
402 * The allocated process must be released by the caller.
403 * @returns NULL and errno on failure.
404 * @param pidParent The parent pid (i.e. this process).
405 */
406__LIBC_PSPMPROCESS __libc_spmCreateEmbryo(pid_t pidParent);
407
408/**
409 * Searches for a process given by pid.
410 *
411 * @returns Pointer to the desired process on success.
412 * @returns NULL and errno on failure.
413 * @param pid Process id to search for.
414 * @param enmState The state of the process.
415 * @remark Call __libc_spmRelease() to release the result.
416 */
417__LIBC_PSPMPROCESS __libc_spmQueryProcess(pid_t pid);
418
419/**
420 * Searches for a process with a given pid and state.
421 *
422 * @returns Pointer to the desired process on success.
423 * @returns NULL and errno on failure.
424 * @param pid Process id to search for.
425 * @param enmState The state of the process.
426 * @remark Call __libc_spmRelease() to release the result.
427 */
428__LIBC_PSPMPROCESS __libc_spmQueryProcessInState(pid_t pid, __LIBC_SPMPROCSTAT enmState);
429
430/**
431 * Release reference to the given process.
432 *
433 * @returns 0 on success.
434 * @returns -1 on failure.
435 * @param pProcess Pointer to process to release.
436 */
437int __libc_spmRelease(__LIBC_PSPMPROCESS pProcess);
438
439/**
440 * Marks the current process (if we have it around) as zombie
441 * or dead freeing all resources associated with it.
442 *
443 * @param uReason The OS/2 exit list type reason code.
444 * This is only used if the current code is NONE.
445 * @param iExitCode The unix exit code for this process.
446 * This is only if the current code is 0.
447 * @remark this might not be a sufficient interface for process termination but we'll see.
448 */
449void __libc_spmTerm(__LIBC_EXIT_REASON enmDeathReason, int iExitCode);
450
451/**
452 * Locks the LIBC shared memory for short exclusive access.
453 * The call must call __libc_spmUnlock() as fast as possible and make
454 * no api calls until that is done.
455 *
456 * @returns 0 on success.
457 * @returns -1 and errno on failure.
458 * @param pRegRec Pointer to the exception handler registration record.
459 * @remark Don't even think of calling this if you're not LIBC!
460 */
461int __libc_spmLock(__LIBC_PSPMXCPTREGREC pRegRec);
462
463/**
464 * Unlock the LIBC shared memory after call to __libc_spmLock().
465 *
466 * @returns 0 on success.
467 * @returns -1 on and errno failure.
468 * @param pRegRec Pointer to the exception handler registration record.
469 * @remark Don't even think of calling this if you're not LIBC!
470 */
471int __libc_spmUnlock(__LIBC_PSPMXCPTREGREC pRegRec);
472
473/**
474 * Allocate memory from the LIBC shared memory.
475 *
476 * @returns Pointer to allocated memory on success.
477 * @returns NULL on failure.
478 * @param cbSize Size of memory to allocate.
479 * @remark Don't think of calling this if you're not LIBC!
480 */
481void * __libc_spmAlloc(size_t cbSize);
482
483/**
484 * Free memory allocated by __libc_SpmAlloc().
485 *
486 * @returns 0 on success.
487 * @returns -1 and errno on failure.
488 * @param pv Pointer to memory block returned by __libc_SpmAlloc().
489 * NULL is allowed.
490 * @remark Don't think of calling this if you're not LIBC!
491 */
492int __libc_spmFree(void *pv);
493
494
495/**
496 * Register TCPIP termination handler.
497 *
498 * This is a manual way of by passing a.out's broken weak symbols.
499 * @param pfnTerm Pointer to the termination function.
500 */
501void __libc_spmSocketRegTerm(void (*pfnTerm)(void));
502
503
504/**
505 * Adds the first reference to a new socket.
506 *
507 * @returns 0 on success.
508 * @returns -1 and errno on failure.
509 * @param iSocket The new socket.
510 */
511int __libc_spmSocketNew(int iSocket);
512
513/**
514 * References a socket.
515 *
516 * @returns The new reference count.
517 * @returns -1 and errno on failure.
518 * @param iSocket socket to reference.
519 */
520int __libc_spmSocketRef(int iSocket);
521
522/**
523 * Dereferences a socket.
524 *
525 * @returns The new reference count.
526 * The caller must close the socket if 0 is returned.
527 * @returns -1 and errno on failure.
528 * @param iSocket Socket to dereference.
529 */
530int __libc_spmSocketDeref(int iSocket);
531
532/**
533 * Checks the SPM memory for trouble.
534 *
535 * @returns 0 on perfect state.
536 * @returns -1 and errno on mutex failure.
537 * @returns Number of failures if SPM is broken.
538 * @param fBreakpoint Raise breakpoint exception if a problem is encountered.
539 * @param fVerbose Log everything.
540 */
541int __libc_SpmCheck(int fBreakpoint, int fVerbose);
542
543__END_DECLS
544
545#endif /* __InnoTekLIBC_sharedpm_h__ */
Note: See TracBrowser for help on using the repository browser.