Release 1.5.29.
[wine] / server / main.c
1 /*
2  * Server main function
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/time.h>
30 #include <unistd.h>
31 #ifdef HAVE_GETOPT_H
32 # include <getopt.h>
33 #endif
34
35 #include "object.h"
36 #include "file.h"
37 #include "thread.h"
38 #include "request.h"
39 #include "wine/library.h"
40
41 /* command-line options */
42 int debug_level = 0;
43 int foreground = 0;
44 timeout_t master_socket_timeout = 3 * -TICKS_PER_SEC;  /* master socket timeout, default is 3 seconds */
45 const char *server_argv0;
46
47 /* parse-line args */
48
49 static void usage( FILE *fh )
50 {
51     fprintf(fh, "Usage: %s [options]\n\n", server_argv0);
52     fprintf(fh, "Options:\n");
53     fprintf(fh, "   -d[n], --debug[=n]       set debug level to n or +1 if n not specified\n");
54     fprintf(fh, "   -f,    --foreground      remain in the foreground for debugging\n");
55     fprintf(fh, "   -h,    --help            display this help message\n");
56     fprintf(fh, "   -k[n], --kill[=n]        kill the current wineserver, optionally with signal n\n");
57     fprintf(fh, "   -p[n], --persistent[=n]  make server persistent, optionally for n seconds\n");
58     fprintf(fh, "   -v,    --version         display version information and exit\n");
59     fprintf(fh, "   -w,    --wait            wait until the current wineserver terminates\n");
60     fprintf(fh, "\n");
61 }
62
63 static void parse_args( int argc, char *argv[] )
64 {
65     int ret, optc;
66
67     static struct option long_options[] =
68     {
69         {"debug",       2, NULL, 'd'},
70         {"foreground",  0, NULL, 'f'},
71         {"help",        0, NULL, 'h'},
72         {"kill",        2, NULL, 'k'},
73         {"persistent",  2, NULL, 'p'},
74         {"version",     0, NULL, 'v'},
75         {"wait",        0, NULL, 'w'},
76         { NULL,         0, NULL, 0}
77     };
78
79     server_argv0 = argv[0];
80
81     while ((optc = getopt_long( argc, argv, "d::fhk::p::vw", long_options, NULL )) != -1)
82     {
83         switch(optc)
84         {
85             case 'd':
86                 if (optarg && isdigit(*optarg))
87                     debug_level = atoi( optarg );
88                 else
89                     debug_level++;
90                 break;
91             case 'f':
92                 foreground = 1;
93                 break;
94             case 'h':
95                 usage(stdout);
96                 exit(0);
97                 break;
98             case 'k':
99                 if (optarg && isdigit(*optarg))
100                     ret = kill_lock_owner( atoi( optarg ) );
101                 else
102                     ret = kill_lock_owner(-1);
103                 exit( !ret );
104             case 'p':
105                 if (optarg && isdigit(*optarg))
106                     master_socket_timeout = (timeout_t)atoi( optarg ) * -TICKS_PER_SEC;
107                 else
108                     master_socket_timeout = TIMEOUT_INFINITE;
109                 break;
110             case 'v':
111                 fprintf( stderr, "%s\n", wine_get_build_id());
112                 exit(0);
113             case 'w':
114                 wait_for_lock();
115                 exit(0);
116             default:
117                 usage(stderr);
118                 exit(1);
119         }
120     }
121 }
122
123 static void sigterm_handler( int signum )
124 {
125     exit(1);  /* make sure atexit functions get called */
126 }
127
128 int main( int argc, char *argv[] )
129 {
130     setvbuf( stderr, NULL, _IOLBF, 0 );
131     parse_args( argc, argv );
132
133     /* setup temporary handlers before the real signal initialization is done */
134     signal( SIGPIPE, SIG_IGN );
135     signal( SIGHUP, sigterm_handler );
136     signal( SIGINT, sigterm_handler );
137     signal( SIGQUIT, sigterm_handler );
138     signal( SIGTERM, sigterm_handler );
139     signal( SIGABRT, sigterm_handler );
140
141     sock_init();
142     open_master_socket();
143
144     if (debug_level) fprintf( stderr, "wineserver: starting (pid=%ld)\n", (long) getpid() );
145     init_signals();
146     init_directories();
147     init_registry();
148     main_loop();
149     return 0;
150 }