Temporary code for debugging purposes
[reactos.git] / posix / lib / psxdll / misc / main.c
1 /* $Id: main.c,v 1.4 2002/05/17 02:04:39 hyperion Exp $
2 */
3 /*
4 * COPYRIGHT: See COPYING in the top level directory
5 * PROJECT: ReactOS POSIX+ Subsystem
6 * FILE: subsys/psx/lib/psxdll/misc/main.c
7 * PURPOSE: psxdll.dll entry point
8 * PROGRAMMER: KJK::Hyperion <noog@libero.it>
9 * UPDATE HISTORY:
10 * 27/12/2001: Created
11 */
12
13 #include <ddk/ntddk.h>
14 #include <string.h>
15 #include <psx/debug.h>
16 #include <psx/fdtable.h>
17 #include <psx/pdata.h>
18 #include <psx/stdlib.h>
19 #include <psx/tls.h>
20
21 #define DLL_PROCESS_ATTACH 1
22 #define DLL_THREAD_ATTACH 2
23 #define DLL_THREAD_DETACH 3
24 #define DLL_PROCESS_DETACH 0
25
26 /* WARNING: PRELIMINARY CODE FOR DEBUGGING PURPOSES ONLY - DO NOT CHANGE */
27 static __PDX_PDATA __PdxPdata;
28 static WCHAR __tempPathBuf[32768];
29 static char * __tempSelf = "-sh";
30 static char * __tempArgv[2] = {0, 0};
31
32 VOID __PdxSetProcessData(__PPDX_PDATA NewPdata)
33 {
34 memcpy(&__PdxPdata, NewPdata, sizeof(__PdxPdata));
35 }
36
37 __PPDX_PDATA __PdxGetProcessData(VOID)
38 {
39 return &__PdxPdata;
40 }
41
42 BOOL STDCALL DllMain(PVOID pDllInstance, DWORD nReason, PVOID pUnknown)
43 {
44 ULONG nJunk;
45
46 switch(nReason)
47 {
48 /* process created, first thread created */
49 case DLL_PROCESS_ATTACH:
50 {
51 __PPDX_TDATA ThreadData;
52 int i;
53
54 INFO("new process and new thread created");
55
56 __PdxPdata.Spawned = 1;
57
58 __PdxPdata.ArgCount = 1;
59 __tempArgv[0] = __tempSelf;
60 __PdxPdata.ArgVect = __tempArgv;
61
62 __PdxPdata.NativePathBuffer.Length = 0;
63 __PdxPdata.NativePathBuffer.MaximumLength = sizeof(__tempPathBuf);
64 __PdxPdata.NativePathBuffer.Buffer = __tempPathBuf;
65
66 INFO("about to initialize process data lock");
67 RtlInitializeCriticalSection(&__PdxPdata.Lock);
68
69 INFO("about to allocate TLS slot");
70 __PdxPdata.TlsIndex = RtlFindClearBitsAndSet(NtCurrentPeb()->TlsBitmap, 1, 0);
71
72 if(__PdxPdata.TlsIndex == -1)
73 {
74 DbgBreakPoint();
75 NtRaiseHardError(STATUS_NO_MEMORY, 0, 0, 0, 1, (ULONG)&nJunk);
76 return (FALSE);
77 }
78
79 INFO("allocated TLS slot %d", __PdxPdata.TlsIndex);
80
81 INFO("about to allocate thread data");
82 ThreadData = __malloc(sizeof(*ThreadData));
83
84 if(ThreadData == 0)
85 {
86 DbgBreakPoint();
87 NtRaiseHardError(STATUS_NO_MEMORY, 0, 0, 0, 1, (ULONG)&nJunk);
88 return (FALSE);
89 }
90
91 NtCurrentTeb()->TlsSlots[__PdxPdata.TlsIndex] = ThreadData;
92
93 INFO("about to initialize file descriptors table");
94 __fdtable_init(&__PdxPdata.FdTable);
95
96 INFO("end of initialization");
97 return (TRUE);
98 }
99
100 /* process about to exit */
101 case DLL_PROCESS_DETACH:
102 {
103 INFO("process about to exit");
104
105 INFO("about to deallocate thread data");
106 __free(NtCurrentTeb()->TlsSlots[__PdxPdata.TlsIndex]);
107
108 return (TRUE);
109 }
110
111 /* thread created */
112 case DLL_THREAD_ATTACH:
113 {
114 __PPDX_TDATA ThreadData;
115
116 INFO("new thread created");
117
118 INFO("about to allocate thread data");
119 ThreadData = __malloc(sizeof(*ThreadData));
120
121 if(ThreadData == 0)
122 {
123 DbgBreakPoint();
124 NtRaiseHardError(STATUS_NO_MEMORY, 0, 0, 0, 1, (ULONG)&nJunk);
125 return (FALSE);
126 }
127
128 NtCurrentTeb()->TlsSlots[__PdxPdata.TlsIndex] = ThreadData;
129
130 return (TRUE);
131 }
132
133 /* thread exited */
134 case DLL_THREAD_DETACH:
135 {
136 INFO("thread about to exit");
137
138 INFO("about to deallocate thread data");
139 __free(NtCurrentTeb()->TlsSlots[__PdxPdata.TlsIndex]);
140
141 return (TRUE);
142 }
143 }
144
145 return (FALSE);
146 }
147
148 /* EOF */
149