Added a simple usage() function.
[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 (more complete?) help option */
28
29 static void usage(const char *exeName)
30 {
31     fprintf(stderr, "\nusage: %s [options]\n\n", exeName);
32     fprintf(stderr, "options:\n");
33     fprintf(stderr, "   -d<n>  set debug level to <n>\n");
34     fprintf(stderr, "   -p     make server persistent\n");
35     fprintf(stderr, "   -h     display this help message\n");
36     fprintf(stderr, "\n");
37 }
38
39 static void parse_args( int argc, char *argv[] )
40 {
41     int i;
42     for (i = 1; i < argc; i++)
43     {
44         if (argv[i][0] == '-')
45         {
46             switch(argv[i][1])
47             {
48             case 'd':
49                 if (isdigit(argv[i][2])) debug_level = atoi( argv[i] + 2 );
50                 else debug_level++;
51                 break;
52             case 'h':
53                 usage( argv[0] );
54                 exit(0);
55                 break;
56             case 'p':
57                 persistent_server = 1;
58                 break;
59             default:
60                 fprintf( stderr, "Unknown option '%s'\n", argv[i] );
61                 usage( argv[0] );
62                 exit(1);
63             }
64         }
65         else
66         {
67             fprintf( stderr, "Unknown argument '%s'. Your version of wine may be too old.\n", argv[i] );
68             usage(argv[0]);
69             exit(1);
70         }
71     }
72 }
73
74 static void sigterm_handler()
75 {
76     exit(1);  /* make sure atexit functions get called */
77 }
78
79 /* initialize signal handling */
80 static void signal_init(void)
81 {
82     signal( SIGPIPE, SIG_IGN );
83     signal( SIGHUP, sigterm_handler );
84     signal( SIGINT, sigterm_handler );
85     signal( SIGQUIT, sigterm_handler );
86     signal( SIGTERM, sigterm_handler );
87     signal( SIGABRT, sigterm_handler );
88 }
89
90 /* get server start ticks used to calculate GetTickCount() in Wine clients */
91 static void get_start_ticks(void)
92 {
93     struct timeval t;
94     gettimeofday( &t, NULL );
95     server_start_ticks = (t.tv_sec * 1000) + (t.tv_usec / 1000);
96 }
97
98 int main( int argc, char *argv[] )
99 {
100     parse_args( argc, argv );
101     signal_init();
102     open_master_socket();
103     setvbuf( stderr, NULL, _IOLBF, 0 );
104
105     if (debug_level) fprintf( stderr, "Server: starting (pid=%ld)\n", (long) getpid() );
106     get_start_ticks();
107     init_registry();
108     select_loop();
109     close_registry();
110     if (debug_level) fprintf( stderr, "Server: exiting (pid=%ld)\n", (long) getpid() );
111
112 #ifdef DEBUG_OBJECTS
113     close_atom_table();
114     dump_objects();  /* dump any remaining objects */
115 #endif
116     exit(0);
117 }