main.c 4.53 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * Server main function
 *
 * Copyright (C) 1998 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21 22
#include "config.h"

23
#include <assert.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
24 25
#include <ctype.h>
#include <fcntl.h>
26
#include <signal.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
27 28
#include <stdio.h>
#include <stdlib.h>
29
#include <sys/time.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
30
#include <unistd.h>
31 32 33
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
34

35
#include "object.h"
36
#include "file.h"
37
#include "thread.h"
38
#include "request.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
39

40 41
/* command-line options */
int debug_level = 0;
42
int foreground = 0;
43
timeout_t master_socket_timeout = 3 * -TICKS_PER_SEC;  /* master socket timeout, default is 3 seconds */
44
const char *server_argv0;
45 46

/* parse-line args */
47

48
static void usage(void)
49
{
50 51 52 53 54 55 56 57 58
    fprintf(stderr, "Usage: %s [options]\n\n", server_argv0);
    fprintf(stderr, "Options:\n");
    fprintf(stderr, "   -d[n], --debug[=n]       set debug level to n or +1 if n not specified\n");
    fprintf(stderr, "   -f,    --foreground      remain in the foreground for debugging\n");
    fprintf(stderr, "   -h,    --help            display this help message\n");
    fprintf(stderr, "   -k[n], --kill[=n]        kill the current wineserver, optionally with signal n\n");
    fprintf(stderr, "   -p[n], --persistent[=n]  make server persistent, optionally for n seconds\n");
    fprintf(stderr, "   -v,    --version         display version information and exit\n");
    fprintf(stderr, "   -w,    --wait            wait until the current wineserver terminates\n");
59 60 61
    fprintf(stderr, "\n");
}

62
static void parse_args( int argc, char *argv[] )
Alexandre Julliard's avatar
Alexandre Julliard committed
63
{
64 65 66 67 68 69 70 71 72 73 74 75 76
    int ret, optc;

    static struct option long_options[] =
    {
        {"debug",       2, 0, 'd'},
        {"foreground",  0, 0, 'f'},
        {"help",        0, 0, 'h'},
        {"kill",        2, 0, 'k'},
        {"persistent",  2, 0, 'p'},
        {"version",     0, 0, 'v'},
        {"wait",        0, 0, 'w'},
        { NULL,         0, 0, 0}
    };
77 78

    server_argv0 = argv[0];
79 80

    while ((optc = getopt_long( argc, argv, "d::fhk::p::vw", long_options, NULL )) != -1)
81
    {
82
        switch(optc)
83 84
        {
            case 'd':
85 86 87 88
                if (optarg && isdigit(*optarg))
                    debug_level = atoi( optarg );
                else
                    debug_level++;
89
                break;
90 91 92
            case 'f':
                foreground = 1;
                break;
93
            case 'h':
94
                usage();
95 96
                exit(0);
                break;
97
            case 'k':
98 99 100 101
                if (optarg && isdigit(*optarg))
                    ret = kill_lock_owner( atoi( optarg ) );
                else
                    ret = kill_lock_owner(-1);
102
                exit( !ret );
103
            case 'p':
104 105
                if (optarg && isdigit(*optarg))
                    master_socket_timeout = (timeout_t)atoi( optarg ) * -TICKS_PER_SEC;
106 107
                else
                    master_socket_timeout = TIMEOUT_INFINITE;
108
                break;
109 110 111
            case 'v':
                fprintf( stderr, "%s\n", PACKAGE_STRING );
                exit(0);
112 113 114
            case 'w':
                wait_for_lock();
                exit(0);
115
            default:
116
                usage();
117 118 119 120
                exit(1);
        }
    }
}
Alexandre Julliard's avatar
Alexandre Julliard committed
121

122
static void sigterm_handler( int signum )
123 124 125
{
    exit(1);  /* make sure atexit functions get called */
}
Alexandre Julliard's avatar
Alexandre Julliard committed
126

127
int main( int argc, char *argv[] )
128
{
129 130 131
    parse_args( argc, argv );

    /* setup temporary handlers before the real signal initialization is done */
132 133 134 135 136 137 138
    signal( SIGPIPE, SIG_IGN );
    signal( SIGHUP, sigterm_handler );
    signal( SIGINT, sigterm_handler );
    signal( SIGQUIT, sigterm_handler );
    signal( SIGTERM, sigterm_handler );
    signal( SIGABRT, sigterm_handler );

139
    sock_init();
140
    open_master_socket();
141
    setvbuf( stderr, NULL, _IOLBF, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
142

143
    if (debug_level) fprintf( stderr, "wineserver: starting (pid=%ld)\n", (long) getpid() );
144
    init_signals();
145
    init_directories();
146
    init_registry();
147
    main_loop();
Jeff Garzik's avatar
Jeff Garzik committed
148
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
149
}