main.c 2.34 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 27 28
/* parse-line args */
/* FIXME: should probably use getopt, and add a help option */
static void parse_args( int argc, char *argv[] )
Alexandre Julliard's avatar
Alexandre Julliard committed
29
{
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    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;
            case 'p':
                persistent_server = 1;
                break;
            default:
                fprintf( stderr, "Unknown option '%s'\n", argv[i] );
                exit(1);
            }
        }
        else
        {
51
            fprintf( stderr, "Unknown argument '%s'. Your version of wine may be too old.\n", argv[i] );
52 53 54 55
            exit(1);
        }
    }
}
Alexandre Julliard's avatar
Alexandre Julliard committed
56

57 58 59 60
static void sigterm_handler()
{
    exit(1);  /* make sure atexit functions get called */
}
Alexandre Julliard's avatar
Alexandre Julliard committed
61

62 63 64 65 66 67 68 69 70 71 72
/* 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 );
}

73 74 75 76 77 78 79 80
/* 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);
}

81 82 83 84 85
int main( int argc, char *argv[] )
{
    parse_args( argc, argv );
    signal_init();
    open_master_socket();
86
    setvbuf( stderr, NULL, _IOLBF, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
87

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

#ifdef DEBUG_OBJECTS
96
    close_atom_table();
97 98
    dump_objects();  /* dump any remaining objects */
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
99 100
    exit(0);
}