Added configure check for the unix domain sockaddr length.
[wine] / loader / main.c
1 /*
2  * Main initialization code
3  */
4
5 #include <assert.h>
6 #include <stdlib.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <errno.h>
13 #include "windef.h"
14 #include "wingdi.h"
15 #include "wine/winbase16.h"
16 #include "wine/winuser16.h"
17 #include "bitmap.h"
18 #include "comm.h"
19 #include "neexe.h"
20 #include "main.h"
21 #include "menu.h"
22 #include "message.h"
23 #include "dialog.h"
24 #include "drive.h"
25 #include "queue.h"
26 #include "sysmetrics.h"
27 #include "file.h"
28 #include "heap.h"
29 #include "keyboard.h"
30 #include "mouse.h"
31 #include "input.h"
32 #include "display.h"
33 #include "miscemu.h"
34 #include "options.h"
35 #include "process.h"
36 #include "spy.h"
37 #include "tweak.h"
38 #include "user.h"
39 #include "cursoricon.h"
40 #include "global.h"
41 #include "dce.h"
42 #include "shell.h"
43 #include "win.h"
44 #include "winproc.h"
45 #include "syslevel.h"
46 #include "services.h"
47 #include "winsock.h"
48 #include "thread.h"
49 #include "task.h"
50 #include "debugtools.h"
51 #include "psdrv.h"
52 #include "win16drv.h"
53 #include "callback.h"
54 #include "server.h"
55 #include "loadorder.h"
56
57 DEFAULT_DEBUG_CHANNEL(server)
58
59 /***********************************************************************
60  *           Main initialisation routine
61  */
62 BOOL MAIN_MainInit( int *argc, char *argv[], BOOL win32 )
63 {
64     /* store the program name */
65     argv0 = argv[0];
66
67     /* Create the initial process */
68     if (!PROCESS_Init( win32 )) return 0;
69
70     /* Initialize syslevel handling */
71     SYSLEVEL_Init();
72
73     /* Parse command line arguments */
74     MAIN_WineInit( argc, argv );
75
76     /* Load the configuration file */
77     if (!PROFILE_LoadWineIni()) return FALSE;
78
79     /* Initialise DOS drives */
80     if (!DRIVE_Init()) return FALSE;
81
82     /* Initialise DOS directories */
83     if (!DIR_Init()) return FALSE;
84
85     /* Registry initialisation */
86     SHELL_LoadRegistry();
87     
88     /* Global boot finished, the rest is process-local */
89     CLIENT_BootDone( TRACE_ON(server) );
90
91     /* Initialize module loadorder */
92     if (!MODULE_InitLoadOrder()) return FALSE;
93
94     /* Initialize DOS memory */
95     if (!DOSMEM_Init(0)) return FALSE;
96
97     /* Initialize event handling */
98     if (!EVENT_Init()) return FALSE;
99
100     /* Initialize communications */
101     COMM_Init();
102
103     /* Initialize IO-port permissions */
104     IO_port_init();
105
106     /* Read DOS config.sys */
107     if (!DOSCONF_ReadConfig()) return FALSE;
108
109     /* Initialize KERNEL */
110     if (!LoadLibrary16( "KRNL386.EXE" )) return FALSE;
111     if (!LoadLibraryA( "KERNEL32" )) return FALSE;
112
113     return TRUE;
114 }
115
116 /***********************************************************************
117  *           KERNEL initialisation routine
118  */
119 BOOL WINAPI MAIN_KernelInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
120 {
121     static BOOL initDone = FALSE;
122
123     HMODULE16 hModule;
124
125     if ( initDone ) return TRUE;
126     initDone = TRUE;
127
128     /* Initialize special KERNEL entry points */
129     hModule = GetModuleHandle16( "KERNEL" );
130     if ( hModule )
131     {
132         WORD cs, ds;
133
134         /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
135         NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
136
137         /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
138         GET_CS(cs); GET_DS(ds);
139         NE_SetEntryPoint( hModule, 454, cs );
140         NE_SetEntryPoint( hModule, 455, ds );
141
142         /* Initialize KERNEL.THHOOK */
143         TASK_InstallTHHook((THHOOK *)PTR_SEG_TO_LIN(
144                                   (SEGPTR)NE_GetEntryPoint( hModule, 332 )));
145
146         /* Initialize the real-mode selector entry points */
147 #define SET_ENTRY_POINT( num, addr ) \
148         NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
149                           DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
150                           FALSE, FALSE, FALSE, NULL ))
151
152         SET_ENTRY_POINT( 183, 0x00000 );  /* KERNEL.183: __0000H */
153         SET_ENTRY_POINT( 174, 0xa0000 );  /* KERNEL.174: __A000H */
154         SET_ENTRY_POINT( 181, 0xb0000 );  /* KERNEL.181: __B000H */
155         SET_ENTRY_POINT( 182, 0xb8000 );  /* KERNEL.182: __B800H */
156         SET_ENTRY_POINT( 195, 0xc0000 );  /* KERNEL.195: __C000H */
157         SET_ENTRY_POINT( 179, 0xd0000 );  /* KERNEL.179: __D000H */
158         SET_ENTRY_POINT( 190, 0xe0000 );  /* KERNEL.190: __E000H */
159         NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg );  /* KERNEL.173: __ROMBIOS */
160         NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
161         NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg );  /* KERNEL.194: __F000H */
162 #undef SET_ENTRY_POINT
163     }
164
165     /* Initialize relay code */
166     if (!RELAY_Init()) return FALSE;
167
168     return TRUE;
169 }
170
171 /***********************************************************************
172  *           GDI initialisation routine
173  */
174 BOOL WINAPI MAIN_GdiInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
175 {
176     NE_MODULE *pModule;
177
178     if ( GDI_HeapSel ) return TRUE;
179
180     /* Create GDI heap */
181     pModule = NE_GetPtr( GetModuleHandle16( "GDI" ) );
182     if ( pModule )
183     {
184         GDI_HeapSel = GlobalHandleToSel16( (NE_SEG_TABLE( pModule ) + 
185                                           pModule->dgroup - 1)->hSeg );
186     }
187     else
188     {
189         GDI_HeapSel = GlobalAlloc16( GMEM_FIXED, GDI_HEAP_SIZE );
190         LocalInit16( GDI_HeapSel, 0, GDI_HEAP_SIZE-1 );
191     }
192
193     if (!TWEAK_Init()) return FALSE;
194
195     /* GDI initialisation */
196     if(!GDI_Init()) return FALSE;
197
198     /* Create the Win16 printer driver */
199     if (!WIN16DRV_Init()) return FALSE;
200
201     /* PSDRV initialization */
202     if(!PSDRV_Init()) return FALSE;
203
204     return TRUE;
205 }
206
207 /***********************************************************************
208  *           USER initialisation routine
209  */
210 BOOL WINAPI MAIN_UserInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
211 {
212     NE_MODULE *pModule;
213     int queueSize;
214
215     if ( USER_HeapSel ) return TRUE;
216
217     /* Create USER heap */
218     pModule = NE_GetPtr( GetModuleHandle16( "USER" ) );
219     if ( pModule )
220     {
221         USER_HeapSel = GlobalHandleToSel16( (NE_SEG_TABLE( pModule ) + 
222                                            pModule->dgroup - 1)->hSeg );
223     }
224     else
225     {
226         USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 0x10000 );
227         LocalInit16( USER_HeapSel, 0, 0xffff );
228     }
229
230      /* Global atom table initialisation */
231     if (!ATOM_Init( USER_HeapSel )) return FALSE;
232
233     /* Initialize window handling (critical section) */
234     WIN_Init();
235
236     /* Initialize system colors and metrics*/
237     SYSMETRICS_Init();
238     SYSCOLOR_Init();
239
240     /* Create the DCEs */
241     DCE_Init();
242
243     /* Initialize timers */
244     if (!TIMER_Init()) return FALSE;
245     
246     /* Initialize window procedures */
247     if (!WINPROC_Init()) return FALSE;
248
249     /* Initialize cursor/icons */
250     CURSORICON_Init();
251
252     /* Initialize built-in window classes */
253     if (!WIDGETS_Init()) return FALSE;
254
255     /* Initialize dialog manager */
256     if (!DIALOG_Init()) return FALSE;
257
258     /* Initialize menus */
259     if (!MENU_Init()) return FALSE;
260
261     /* Initialize message spying */
262     if (!SPY_Init()) return FALSE;
263
264     /* Check wine.conf for old/bad entries */
265     if (!TWEAK_CheckConfiguration()) return FALSE;
266
267     /* Create system message queue */
268     queueSize = GetProfileIntA( "windows", "TypeAhead", 120 );
269     if (!QUEUE_CreateSysMsgQueue( queueSize )) return FALSE;
270
271     /* Set double click time */
272     SetDoubleClickTime( GetProfileIntA("windows","DoubleClickSpeed",452) );
273
274     /* Create message queue of initial thread */
275     InitThreadInput16( 0, 0 );
276
277     /* Create desktop window */
278     if (!WIN_CreateDesktopWindow()) return FALSE;
279
280     /* Initialize keyboard driver */
281     KEYBOARD_Enable( keybd_event, InputKeyStateTable );
282
283     /* Initialize mouse driver */
284     MOUSE_Enable( mouse_event );
285
286     /* Start processing X events */
287     UserRepaintDisable16( FALSE );
288
289     return TRUE;
290 }
291
292
293 /***********************************************************************
294  *           Winelib initialisation routine
295  */
296 HINSTANCE MAIN_WinelibInit( int *argc, char *argv[] )
297 {
298     NE_MODULE *pModule;
299     HMODULE16 hModule;
300
301     /* Main initialization */
302     if (!MAIN_MainInit( argc, argv, TRUE )) return 0;
303
304     /* Load WineLib EXE module */
305     if ( (hModule = BUILTIN32_LoadExeModule()) < 32 ) return 0;
306     pModule = (NE_MODULE *)GlobalLock16( hModule );
307
308     /* Create initial task */
309     if (!TASK_Create( pModule, FALSE )) return 0;
310
311     /* Create 32-bit MODREF */
312     if ( !PE_CreateModule( pModule->module32, NE_MODULE_NAME(pModule), 0, FALSE ) )
313         return 0;
314
315     /* Increment EXE refcount */
316     assert( PROCESS_Current()->exe_modref );
317     PROCESS_Current()->exe_modref->refCount++;
318
319     /* Load system DLLs into the initial process (and initialize them) */
320     if (   !LoadLibrary16("GDI.EXE" ) || !LoadLibraryA("GDI32.DLL" )
321         || !LoadLibrary16("USER.EXE") || !LoadLibraryA("USER32.DLL"))
322         ExitProcess( 1 );
323
324     /* Get pointers to USER routines called by KERNEL */
325     THUNK_InitCallout();
326
327     return pModule->module32;
328 }
329
330 /***********************************************************************
331  *           ExitKernel16 (KERNEL.2)
332  *
333  * Clean-up everything and exit the Wine process.
334  *
335  */
336 void WINAPI ExitKernel16( void )
337 {
338     /* Do the clean-up stuff */
339
340     WriteOutProfiles16();
341     SHELL_SaveRegistry();
342
343     TerminateProcess( GetCurrentProcess(), 0 );
344 }
345