Reorganized DOS memory remapping slightly, so that the real-mode
[wine] / server / main.c
1 /*
2  * Server main function
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  */
6
7 #include <assert.h>
8 #include <ctype.h>
9 #include <fcntl.h>
10 #include <signal.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <sys/time.h>
14 #include <unistd.h>
15
16 #include "object.h"
17 #include "thread.h"
18 #include "request.h"
19
20 /* command-line options */
21 int debug_level = 0;
22 int persistent_server = 0;
23
24 unsigned int server_start_ticks = 0;
25
26 /* parse-line args */
27 /* FIXME: should probably use getopt, and add a help option */
28 static void parse_args( int argc, char *argv[] )
29 {
30     int i;
31     for (i = 1; i < argc; i++)
32     {
33         if (argv[i][0] == '-')
34         {
35             switch(argv[i][1])
36             {
37             case 'd':
38                 if (isdigit(argv[i][2])) debug_level = atoi( argv[i] + 2 );
39                 else debug_level++;
40                 break;
41             case 'p':
42                 persistent_server = 1;
43                 break;
44             default:
45                 fprintf( stderr, "Unknown option '%s'\n", argv[i] );
46                 exit(1);
47             }
48         }
49         else
50         {
51             fprintf( stderr, "Unknown argument '%s'. Your version of wine may be too old.\n", argv[i] );
52             exit(1);
53         }
54     }
55 }
56
57 static void sigterm_handler()
58 {
59     exit(1);  /* make sure atexit functions get called */
60 }
61
62 /* initialize signal handling */
63 static void signal_init(void)
64 {
65     signal( SIGPIPE, SIG_IGN );
66     signal( SIGHUP, sigterm_handler );
67     signal( SIGINT, sigterm_handler );
68     signal( SIGQUIT, sigterm_handler );
69     signal( SIGTERM, sigterm_handler );
70     signal( SIGABRT, sigterm_handler );
71 }
72
73 /* get server start ticks used to calculate GetTickCount() in Wine clients */
74 static void get_start_ticks(void)
75 {
76     struct timeval t;
77     gettimeofday( &t, NULL );
78     server_start_ticks = (t.tv_sec * 1000) + (t.tv_usec / 1000);
79 }
80
81 int main( int argc, char *argv[] )
82 {
83     parse_args( argc, argv );
84     signal_init();
85     open_master_socket();
86     setvbuf( stderr, NULL, _IOLBF, 0 );
87
88     if (debug_level) fprintf( stderr, "Server: starting (pid=%ld)\n", (long) getpid() );
89     get_start_ticks();
90     init_registry();
91     select_loop();
92     close_registry();
93     if (debug_level) fprintf( stderr, "Server: exiting (pid=%ld)\n", (long) getpid() );
94
95 #ifdef DEBUG_OBJECTS
96     close_atom_table();
97     dump_objects();  /* dump any remaining objects */
98 #endif
99     exit(0);
100 }