main.c 2.85 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4 5 6
/*
 * Server main function
 *
 * Copyright (C) 1998 Alexandre Julliard
 */

7
#include <assert.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
8 9
#include <ctype.h>
#include <fcntl.h>
10
#include <signal.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
11 12
#include <stdio.h>
#include <stdlib.h>
13
#include <sys/time.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
14 15
#include <unistd.h>

16 17
#include "object.h"
#include "thread.h"
18
#include "request.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
19

20 21 22 23
/* command-line options */
int debug_level = 0;
int persistent_server = 0;

24 25
unsigned int server_start_ticks = 0;

26
/* parse-line args */
27 28 29 30 31 32 33 34 35 36 37 38
/* FIXME: should probably use getopt, and add a (more complete?) help option */

static void usage(const char *exeName)
{
    fprintf(stderr, "\nusage: %s [options]\n\n", exeName);
    fprintf(stderr, "options:\n");
    fprintf(stderr, "   -d<n>  set debug level to <n>\n");
    fprintf(stderr, "   -p     make server persistent\n");
    fprintf(stderr, "   -h     display this help message\n");
    fprintf(stderr, "\n");
}

39
static void parse_args( int argc, char *argv[] )
Alexandre Julliard's avatar
Alexandre Julliard committed
40
{
41 42 43 44 45 46 47 48 49 50 51
    int i;
    for (i = 1; i < argc; i++)
    {
        if (argv[i][0] == '-')
        {
            switch(argv[i][1])
            {
            case 'd':
                if (isdigit(argv[i][2])) debug_level = atoi( argv[i] + 2 );
                else debug_level++;
                break;
52 53 54 55
            case 'h':
                usage( argv[0] );
                exit(0);
                break;
56 57 58 59 60
            case 'p':
                persistent_server = 1;
                break;
            default:
                fprintf( stderr, "Unknown option '%s'\n", argv[i] );
61
                usage( argv[0] );
62 63 64 65 66
                exit(1);
            }
        }
        else
        {
67
            fprintf( stderr, "Unknown argument '%s'. Your version of wine may be too old.\n", argv[i] );
68
            usage(argv[0]);
69 70 71 72
            exit(1);
        }
    }
}
Alexandre Julliard's avatar
Alexandre Julliard committed
73

74 75 76 77
static void sigterm_handler()
{
    exit(1);  /* make sure atexit functions get called */
}
Alexandre Julliard's avatar
Alexandre Julliard committed
78

79 80 81 82 83 84 85 86 87 88 89
/* initialize signal handling */
static void signal_init(void)
{
    signal( SIGPIPE, SIG_IGN );
    signal( SIGHUP, sigterm_handler );
    signal( SIGINT, sigterm_handler );
    signal( SIGQUIT, sigterm_handler );
    signal( SIGTERM, sigterm_handler );
    signal( SIGABRT, sigterm_handler );
}

90 91 92 93 94 95 96 97
/* get server start ticks used to calculate GetTickCount() in Wine clients */
static void get_start_ticks(void)
{
    struct timeval t;
    gettimeofday( &t, NULL );
    server_start_ticks = (t.tv_sec * 1000) + (t.tv_usec / 1000);
}

98 99 100 101 102
int main( int argc, char *argv[] )
{
    parse_args( argc, argv );
    signal_init();
    open_master_socket();
103
    setvbuf( stderr, NULL, _IOLBF, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
104

Patrik Stridvall's avatar
Patrik Stridvall committed
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();
Patrik Stridvall's avatar
Patrik Stridvall committed
110
    if (debug_level) fprintf( stderr, "Server: exiting (pid=%ld)\n", (long) getpid() );
111 112

#ifdef DEBUG_OBJECTS
113
    close_atom_table();
114 115
    dump_objects();  /* dump any remaining objects */
#endif
Jeff Garzik's avatar
Jeff Garzik committed
116 117

    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
118
}