Made the server listen for new clients on a Unix socket in
$HOME/.wine. Newly started wine processes now attach to an existing
server if one is running.

diff --git a/server/Makefile.in b/server/Makefile.in
index e2bad9f..e849135 100644
--- a/server/Makefile.in
+++ b/server/Makefile.in
@@ -1,9 +1,9 @@
-DEFS      = @DLLFLAGS@ -D__WINE__ -D__WINE_SERVER__
+DEFS      = -D__WINE__ -D__WINE_SERVER__
 TOPSRCDIR = @top_srcdir@
 TOPOBJDIR = ..
 SRCDIR    = @srcdir@
 VPATH     = @srcdir@
-MODULE    = server
+MODULE    = none
 
 C_SRCS = \
 	change.c \
@@ -14,6 +14,7 @@
 	event.c \
 	file.c \
 	handle.c \
+	main.c \
 	mapping.c \
 	mutex.c \
 	object.c \
@@ -31,16 +32,13 @@
 	trace.c \
 	unicode.c
 
-EXTRA_SRCS = main.c
-MAIN_OBJS = main.o
-
 PROGRAMS = wineserver
 
-all: $(MODULE).o $(PROGRAMS)
+all: $(PROGRAMS)
 
 @MAKE_RULES@
 
-wineserver: $(OBJS) $(MAIN_OBJS)
-	$(CC) -o $(PROGRAMS) $(OBJS) $(MAIN_OBJS) $(LIBS)
+wineserver: $(OBJS)
+	$(CC) -o $(PROGRAMS) $(OBJS) $(LIBS)
 
 ### Dependencies:
diff --git a/server/main.c b/server/main.c
index 51490ce..7ec67d7 100644
--- a/server/main.c
+++ b/server/main.c
@@ -4,39 +4,98 @@
  * Copyright (C) 1998 Alexandre Julliard
  */
 
+#include <assert.h>
 #include <ctype.h>
 #include <fcntl.h>
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/time.h>
 #include <unistd.h>
 
 #include "object.h"
 #include "thread.h"
+#include "request.h"
+
+/* command-line options */
+int debug_level = 0;
+int persistent_server = 0;
+
+/* parse-line args */
+/* FIXME: should probably use getopt, and add a help option */
+static void parse_args( int argc, char *argv[] )
+{
+    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
+        {
+            fprintf( stderr, "Unknown argument '%s'\n", argv[i] );
+            exit(1);
+        }
+    }
+}
+
+static void sigterm_handler()
+{
+    exit(1);  /* make sure atexit functions get called */
+}
+
+/* initialize signal handling */
+static void signal_init(void)
+{
+#if 0
+    if (!debug_level)
+    {
+        switch(fork())
+        {
+        case -1:
+            break;
+        case 0:
+            setsid();
+            break;
+        default:
+            exit(0);
+        }
+    }
+#endif
+    signal( SIGPIPE, SIG_IGN );
+    signal( SIGHUP, sigterm_handler );
+    signal( SIGINT, sigterm_handler );
+    signal( SIGQUIT, sigterm_handler );
+    signal( SIGTERM, sigterm_handler );
+    signal( SIGABRT, sigterm_handler );
+}
 
 int main( int argc, char *argv[] )
 {
-    int fd;
-
-    if (argc != 2) goto error;
-    if (!isdigit( *argv[1] )) goto error;
-    fd = atoi( argv[1] );
-    /* make sure the fd is valid */
-    if (fcntl( fd, F_GETFL, 0 ) == -1) goto error;
-
-/*    debug_level = 1; */
+    parse_args( argc, argv );
+    signal_init();
+    open_master_socket();
 
     if (debug_level) fprintf( stderr, "Server: starting (pid=%ld)\n", (long) getpid() );
-    create_initial_thread( fd );
+    select_loop();
     if (debug_level) fprintf( stderr, "Server: exiting (pid=%ld)\n", (long) getpid() );
 
-    close_registry();
 #ifdef DEBUG_OBJECTS
+    close_registry();
     dump_objects();  /* dump any remaining objects */
 #endif
-
     exit(0);
-
- error:    
-    fprintf( stderr, "%s: must be run from Wine.\n", argv[0] );
-    exit(1);
 }
diff --git a/server/object.c b/server/object.c
index 71e9451..4ade006 100644
--- a/server/object.c
+++ b/server/object.c
@@ -15,7 +15,6 @@
 #include "thread.h"
 #include "unicode.h"
 
-int debug_level = 0;
 
 struct object_name
 {
diff --git a/server/object.h b/server/object.h
index fb874f7..5ab85a1 100644
--- a/server/object.h
+++ b/server/object.h
@@ -165,6 +165,9 @@
 
 extern void close_registry(void);
 
+/* global variables (command-line options) */
+
 extern int debug_level;
+extern int persistent_server;
 
 #endif  /* __WINE_SERVER_OBJECT_H */
diff --git a/server/process.c b/server/process.c
index b2a53e4..b2743e1 100644
--- a/server/process.c
+++ b/server/process.c
@@ -12,6 +12,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/time.h>
+#include <sys/socket.h>
 #include <unistd.h>
 
 #include "winbase.h"
@@ -50,14 +51,89 @@
     process_destroy              /* destroy */
 };
 
+/* set the process creation info */
+static int set_creation_info( struct process *process, struct new_process_request *req,
+                              const char *cmd_line, size_t len )
+{
+    if (!(process->info = mem_alloc( sizeof(*process->info) + len ))) return 0;
+    if (req)
+    {
+        /* copy the request structure */
+        memcpy( process->info, req, sizeof(*req) );
+    }
+    else  /* no request, use defaults */
+    {
+        req = process->info;
+        req->inherit      = 0;
+        req->inherit_all  = 0;
+        req->create_flags = CREATE_NEW_CONSOLE;
+        req->start_flags  = STARTF_USESTDHANDLES;
+        req->hstdin       = -1;
+        req->hstdout      = -1;
+        req->hstderr      = -1;
+        req->event        = -1;
+        req->cmd_show     = 0;
+        req->env_ptr      = NULL;
+    }
+    memcpy( process->info->cmdline, cmd_line, len );
+    process->info->cmdline[len] = 0;
+    process->create_flags = process->info->create_flags;
+    return 1;
+}
 
-/* create a new process */
-static struct process *create_process( struct process *parent, struct new_process_request *req,
-                                       const char *cmd_line, size_t len )
+/* set the console and stdio handles for a newly created process */
+static int set_process_console( struct process *process, struct process *parent )
+{
+    struct new_process_request *info = process->info;
+
+    if (process->create_flags & CREATE_NEW_CONSOLE)
+    {
+        if (!alloc_console( process )) return 0;
+    }
+    else if (!(process->create_flags & DETACHED_PROCESS))
+    {
+        if (parent->console_in) process->console_in = grab_object( parent->console_in );
+        if (parent->console_out) process->console_out = grab_object( parent->console_out );
+    }
+    if (parent)
+    {
+        if (!info->inherit_all && !(info->start_flags & STARTF_USESTDHANDLES))
+        {
+            /* duplicate the handle from the parent into this process */
+            info->hstdin  = duplicate_handle( parent, info->hstdin, process,
+                                              0, TRUE, DUPLICATE_SAME_ACCESS );
+            info->hstdout = duplicate_handle( parent, info->hstdout, process,
+                                              0, TRUE, DUPLICATE_SAME_ACCESS );
+            info->hstderr = duplicate_handle( parent, info->hstderr, process,
+                                              0, TRUE, DUPLICATE_SAME_ACCESS );
+        }
+    }
+    else
+    {
+        /* no parent, use handles to the console for stdio */
+        info->hstdin  = alloc_handle( process, process->console_in,
+                                      GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
+        info->hstdout = alloc_handle( process, process->console_out,
+                                      GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
+        info->hstderr = alloc_handle( process, process->console_out,
+                                      GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
+    }
+    return 1;
+}
+
+/* create a new process and its main thread */
+struct thread *create_process( int fd, struct process *parent,
+                               struct new_process_request *req,
+                               const char *cmd_line, size_t len )
 {
     struct process *process;
+    struct thread *thread = NULL;
 
-    if (!(process = alloc_object( &process_ops, -1 ))) return NULL;
+    if (!(process = alloc_object( &process_ops, -1 )))
+    {
+        close( fd );
+        return NULL;
+    }
     process->next            = NULL;
     process->prev            = NULL;
     process->thread_list     = NULL;
@@ -74,16 +150,13 @@
     process->init_event      = NULL;
     process->info            = NULL;
     gettimeofday( &process->start_time, NULL );
+    if ((process->next = first_process) != NULL) process->next->prev = process;
+    first_process = process;
 
     /* copy the request structure */
-    if (!(process->info = mem_alloc( sizeof(*process->info) + len ))) goto error;
-    memcpy( process->info, req, sizeof(*req) );
-    memcpy( process->info->cmdline, cmd_line, len );
-    process->info->cmdline[len] = 0;
-    req = process->info;  /* use the copy now */
-    process->create_flags = req->create_flags;
+    if (!set_creation_info( process, req, cmd_line, len )) goto error;
 
-    if (req->inherit_all)
+    if (process->info->inherit_all)
         process->handles = copy_handle_table( process, parent );
     else
         process->handles = alloc_handle_table( process, 0 );
@@ -93,32 +166,18 @@
     alloc_handle( process, process, PROCESS_ALL_ACCESS, 0 );
 
     /* get the init done event */
-    if (req->event != -1)
+    if (process->info->event != -1)
     {
-        if (!(process->init_event = get_event_obj( parent, req->event, EVENT_MODIFY_STATE )))
-            goto error;
+        if (!(process->init_event = get_event_obj( parent, process->info->event,
+                                                   EVENT_MODIFY_STATE ))) goto error;
     }
 
     /* set the process console */
-    if (process->create_flags & CREATE_NEW_CONSOLE)
-    {
-        if (!alloc_console( process )) goto error;
-    }
-    else if (!(process->create_flags & DETACHED_PROCESS))
-    {
-        if (parent->console_in) process->console_in = grab_object( parent->console_in );
-        if (parent->console_out) process->console_out = grab_object( parent->console_out );
-    }
+    if (!set_process_console( process, parent )) goto error;
 
-    if (!req->inherit_all && !(req->start_flags & STARTF_USESTDHANDLES))
-    {
-        process->info->hstdin  = duplicate_handle( parent, req->hstdin, process,
-                                                   0, TRUE, DUPLICATE_SAME_ACCESS );
-        process->info->hstdout = duplicate_handle( parent, req->hstdout, process,
-                                                   0, TRUE, DUPLICATE_SAME_ACCESS );
-        process->info->hstderr = duplicate_handle( parent, req->hstderr, process,
-                                                   0, TRUE, DUPLICATE_SAME_ACCESS );
-    }
+    /* create the main thread */
+    if (!(thread = create_thread( fd, process, (process->create_flags & CREATE_SUSPENDED) != 0)))
+        goto error;
 
     /* attach to the debugger if requested */
     if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
@@ -126,45 +185,17 @@
     else if (parent && parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
         debugger_attach( process, parent->debugger );
 
-    if ((process->next = first_process) != NULL) process->next->prev = process;
-    first_process = process;
-    return process;
+    release_object( process );
+    return thread;
 
  error:
+    close( fd );
     free_console( process );
     if (process->handles) release_object( process->handles );
     release_object( process );
     return NULL;
 }
 
-/* create the initial process */
-struct process *create_initial_process(void)
-{
-    struct process *process;
-    struct new_process_request req;
-
-    req.inherit      = 0;
-    req.inherit_all  = 0;
-    req.create_flags = CREATE_NEW_CONSOLE;
-    req.start_flags  = STARTF_USESTDHANDLES;
-    req.hstdin       = -1;
-    req.hstdout      = -1;
-    req.hstderr      = -1;
-    req.event        = -1;
-    req.cmd_show     = 0;
-    req.env_ptr      = NULL;
-    if ((process = create_process( NULL, &req, "", 1 )))
-    {
-        process->info->hstdin  = alloc_handle( process, process->console_in,
-                                               GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
-        process->info->hstdout = alloc_handle( process, process->console_out,
-                                               GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
-        process->info->hstderr = alloc_handle( process, process->console_out,
-                                               GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
-    }
-    return process;
-}
-
 /* destroy a process when its refcount is 0 */
 static void process_destroy( struct object *obj )
 {
@@ -229,6 +260,8 @@
     {
         /* last process died, close global handles */
         close_global_handles();
+        /* this will cause the select loop to terminate */
+        if (!persistent_server) close_master_socket();
     }
 }
 
@@ -455,16 +488,40 @@
 DECL_HANDLER(new_process)
 {
     size_t len = get_req_strlen( req->cmdline );
-    struct process *process;
+    struct thread *thread;
+    int sock[2];
 
-    req->handle = -1;
-    req->pid    = NULL;
-    if ((process = create_process( current->process, req, req->cmdline, len )))
+    req->phandle = -1;
+    req->thandle = -1;
+    req->pid     = NULL;
+    req->tid     = NULL;
+
+    if (socketpair( AF_UNIX, SOCK_STREAM, 0, sock ) == -1)
     {
-        req->handle = alloc_handle( current->process, process, PROCESS_ALL_ACCESS, req->inherit );
-        req->pid    = process;
-        release_object( process );
+        file_set_error();
+        return;
     }
+
+    if ((thread = create_process( sock[0], current->process, req, req->cmdline, len )))
+    {
+        int phandle = alloc_handle( current->process, thread->process,
+                                    PROCESS_ALL_ACCESS, req->inherit );
+        if ((req->phandle = phandle) != -1)
+        {
+            if ((req->thandle = alloc_handle( current->process, thread,
+                                              THREAD_ALL_ACCESS, req->inherit )) != -1)
+            {
+                /* thread object will be released when the thread gets killed */
+                set_reply_fd( current, sock[1] );
+                req->pid = thread->process;
+                req->tid = thread;
+                return;
+            }
+            close_handle( current->process, phandle );
+        }
+        release_object( thread );
+    }
+    close( sock[1] );
 }
 
 /* initialize a new process */
diff --git a/server/process.h b/server/process.h
index 305d1c5..737de56 100644
--- a/server/process.h
+++ b/server/process.h
@@ -47,7 +47,9 @@
 
 /* process functions */
 
-extern struct process *create_initial_process(void);
+extern struct thread *create_process( int fd, struct process *parent,
+                                      struct new_process_request *req,
+                                      const char *cmd_line, size_t len );
 extern struct process *get_process_from_id( void *id );
 extern struct process *get_process_from_handle( int handle, unsigned int access );
 extern int process_set_debugger( struct process *process, struct thread *thread );
diff --git a/server/request.c b/server/request.c
index 945126b..b7a5161 100644
--- a/server/request.c
+++ b/server/request.c
@@ -9,22 +9,27 @@
 #include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <pwd.h>
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
 #include <string.h>
+#include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/types.h>
 #ifdef HAVE_SYS_SOCKET_H
 # include <sys/socket.h>
 #endif
 #include <sys/uio.h>
+#include <sys/un.h>
 #include <unistd.h>
 
 #include "winnt.h"
 #include "winbase.h"
 #include "wincon.h"
 #include "thread.h"
+#include "process.h"
 #include "server.h"
 #define WANT_REQUEST_HANDLERS
 #include "request.h"
@@ -34,9 +39,41 @@
 #define SCM_RIGHTS 1
 #endif
 
- 
+ /* path names for server master Unix socket */
+#define CONFDIR    "/.wine"        /* directory for Wine config relative to $HOME */
+#define SERVERDIR  "/wineserver-"  /* server socket directory (hostname appended) */
+#define SOCKETNAME "socket"        /* name of the socket file */
+
+struct master_socket
+{
+    struct object       obj;         /* object header */
+};
+
+static void master_socket_dump( struct object *obj, int verbose );
+static void master_socket_poll_event( struct object *obj, int event );
+static void master_socket_destroy( struct object *obj );
+
+static const struct object_ops master_socket_ops =
+{
+    sizeof(struct master_socket),  /* size */
+    master_socket_dump,            /* dump */
+    no_add_queue,                  /* add_queue */
+    NULL,                          /* remove_queue */
+    NULL,                          /* signaled */
+    NULL,                          /* satisfied */
+    NULL,                          /* get_poll_events */
+    master_socket_poll_event,      /* poll_event */
+    no_read_fd,                    /* get_read_fd */
+    no_write_fd,                   /* get_write_fd */
+    no_flush,                      /* flush */
+    no_get_file_info,              /* get_file_info */
+    master_socket_destroy          /* destroy */
+};
+
+
 struct thread *current = NULL;  /* thread handling the current request */
 
+static struct master_socket *master_socket;  /* the master socket object */
 
 /* socket communication static structures */
 static struct iovec myiovec;
@@ -64,6 +101,31 @@
     kill_thread( thread, PROTOCOL_ERROR );
 }
 
+/* die on a fatal error */
+static void fatal_error( const char *err, ... )
+{
+    va_list args;
+
+    va_start( args, err );
+    fprintf( stderr, "wineserver: " );
+    vfprintf( stderr, err, args );
+    va_end( args );
+    exit(1);
+}
+
+/* die on a fatal error */
+static void fatal_perror( const char *err, ... )
+{
+    va_list args;
+
+    va_start( args, err );
+    fprintf( stderr, "wineserver: " );
+    vfprintf( stderr, err, args );
+    perror( " " );
+    va_end( args );
+    exit(1);
+}
+
 /* call a request handler */
 static void call_req_handler( struct thread *thread, enum request req, int fd )
 {
@@ -187,12 +249,135 @@
     return 1;
 }
 
-/* set the debug level */
-DECL_HANDLER(set_debug)
+static void master_socket_dump( struct object *obj, int verbose )
 {
-    debug_level = req->level;
-    /* Make sure last_req is initialized */
-    current->last_req = REQ_SET_DEBUG;
+    struct master_socket *sock = (struct master_socket *)obj;
+    assert( obj->ops == &master_socket_ops );
+    fprintf( stderr, "Master socket fd=%d\n", sock->obj.fd );
+}
+
+/* handle a socket event */
+static void master_socket_poll_event( struct object *obj, int event )
+{
+    struct master_socket *sock = (struct master_socket *)obj;
+    assert( obj->ops == &master_socket_ops );
+
+    assert( sock == master_socket );  /* there is only one master socket */
+
+    if (event & (POLLERR | POLLHUP))
+    {
+        /* this is not supposed to happen */
+        fprintf( stderr, "wineserver: Error on master socket\n" );
+        release_object( obj );
+    }
+    else if (event & POLLIN)
+    {
+        struct sockaddr_un dummy;
+        int len = sizeof(dummy);
+        int client = accept( master_socket->obj.fd, &dummy, &len );
+        if (client != -1) create_process( client, NULL, NULL, "", 1 );
+    }
+}
+
+/* remove the socket upon exit */
+static void socket_cleanup(void)
+{
+    unlink( SOCKETNAME );
+}
+
+static void master_socket_destroy( struct object *obj )
+{
+    socket_cleanup();
+}
+
+/* return the configuration directory ($HOME/.wine) */
+static const char *get_config_dir(void)
+{
+    static char *confdir;
+    if (!confdir)
+    {
+        const char *home = getenv( "HOME" );
+        if (!home)
+        {
+            struct passwd *pwd = getpwuid( getuid() );
+            if (!pwd) fatal_error( "could not find your home directory\n" );
+            home = pwd->pw_dir;
+        }
+        if (!(confdir = malloc( strlen(home) + strlen(CONFDIR) + 1 )))
+            fatal_error( "out of memory\n" );
+        strcpy( confdir, home );
+        strcat( confdir, CONFDIR );
+        mkdir( confdir, 0755 );  /* just in case */
+    }
+    return confdir;
+}
+
+/* create the server directory and chdir to it */
+static void create_server_dir(void)
+{
+    char hostname[64];
+    char *serverdir;
+    const char *confdir = get_config_dir();
+    struct stat st;
+
+    if (gethostname( hostname, sizeof(hostname) ) == -1) fatal_perror( "gethostname" );
+
+    if (!(serverdir = malloc( strlen(confdir) + strlen(SERVERDIR) + strlen(hostname) + 1 )))
+        fatal_error( "out of memory\n" );
+
+    strcpy( serverdir, confdir );
+    strcat( serverdir, SERVERDIR );
+    strcat( serverdir, hostname );
+
+    if (chdir( serverdir ) == -1)
+    {
+        if (errno != ENOENT) fatal_perror( "chdir %s", serverdir );
+        if (mkdir( serverdir, 0700 ) == -1) fatal_perror( "mkdir %s", serverdir );
+        if (chdir( serverdir ) == -1) fatal_perror( "chdir %s", serverdir );
+    }
+    if (stat( ".", &st ) == -1) fatal_perror( "stat %s", serverdir );
+    if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", serverdir );
+    if (st.st_uid != getuid()) fatal_error( "%s is not owned by you\n", serverdir );
+    if (st.st_mode & 077) fatal_error( "%s must not be accessible by other users\n", serverdir );
+}
+
+/* open the master server socket and start waiting for new clients */
+void open_master_socket(void)
+{
+    struct sockaddr_un addr;
+    int fd;
+
+    create_server_dir();
+    if ((fd = socket( AF_UNIX, SOCK_STREAM, 0 )) == -1) fatal_perror( "socket" );
+    addr.sun_family = AF_UNIX;
+    strcpy( addr.sun_path, "socket" );
+    if (bind( fd, &addr, sizeof(addr.sun_family) + strlen(addr.sun_path) ) == -1)
+    {
+        if ((errno == EEXIST) || (errno == EADDRINUSE))
+            fatal_error( "another server is already running\n" );
+        else
+            fatal_perror( "bind" );
+    }
+    atexit( socket_cleanup );
+
+    chmod( "socket", 0600 );  /* make sure no other user can connect */
+    if (listen( fd, 5 ) == -1) fatal_perror( "listen" );
+
+    if (!(master_socket = alloc_object( &master_socket_ops, fd )))
+        fatal_error( "out of memory\n" );
+    set_select_events( &master_socket->obj, POLLIN );
+}
+
+/* close the master socket and stop waiting for new clients */
+void close_master_socket(void)
+{
+    release_object( master_socket );
+}
+
+/* lock/unlock the master socket to stop accepting new clients */
+void lock_master_socket( int locked )
+{
+    set_select_events( &master_socket->obj, locked ? 0 : POLLIN );
 }
 
 /* debugger support operations */
diff --git a/server/request.h b/server/request.h
index 80ad9aa..c9fcd10 100644
--- a/server/request.h
+++ b/server/request.h
@@ -31,6 +31,9 @@
 extern void fatal_protocol_error( struct thread *thread, const char *err, ... );
 extern void set_reply_fd( struct thread *thread, int pass_fd );
 extern void send_reply( struct thread *thread );
+extern void open_master_socket(void);
+extern void close_master_socket(void);
+extern void lock_master_socket( int locked );
 
 extern void trace_request( enum request req, int fd );
 extern void trace_kill( struct thread *thread );
@@ -69,7 +72,7 @@
 
 DECL_HANDLER(new_process);
 DECL_HANDLER(new_thread);
-DECL_HANDLER(set_debug);
+DECL_HANDLER(boot_done);
 DECL_HANDLER(init_process);
 DECL_HANDLER(init_process_done);
 DECL_HANDLER(init_thread);
@@ -168,7 +171,7 @@
 } req_handlers[REQ_NB_REQUESTS] = {
     { (void(*)())req_new_process, sizeof(struct new_process_request) },
     { (void(*)())req_new_thread, sizeof(struct new_thread_request) },
-    { (void(*)())req_set_debug, sizeof(struct set_debug_request) },
+    { (void(*)())req_boot_done, sizeof(struct boot_done_request) },
     { (void(*)())req_init_process, sizeof(struct init_process_request) },
     { (void(*)())req_init_process_done, sizeof(struct init_process_done_request) },
     { (void(*)())req_init_thread, sizeof(struct init_thread_request) },
diff --git a/server/select.c b/server/select.c
index 98b4817..c46ccd6 100644
--- a/server/select.c
+++ b/server/select.c
@@ -210,9 +210,6 @@
     sigset_t sigset;
     struct sigaction action;
 
-    setsid();
-    signal( SIGPIPE, SIG_IGN );
-
     /* block the signals we use */
     sigemptyset( &sigset );
     sigaddset( &sigset, SIGCHLD );
diff --git a/server/thread.c b/server/thread.c
index f162918..d2c5a56 100644
--- a/server/thread.c
+++ b/server/thread.c
@@ -87,6 +87,7 @@
 };
 
 static struct thread *first_thread;
+static struct thread *booting_thread;
 
 /* allocate the buffer for the communication with the client */
 static int alloc_client_buffer( struct thread *thread )
@@ -106,7 +107,7 @@
 }
 
 /* create a new thread */
-static struct thread *create_thread( int fd, struct process *process, int suspend )
+struct thread *create_thread( int fd, struct process *process, int suspend )
 {
     struct thread *thread;
     int buf_fd;
@@ -135,14 +136,15 @@
     thread->suspend     = (suspend != 0);
     thread->buffer      = (void *)-1;
     thread->last_req    = REQ_GET_THREAD_BUFFER;
+    thread->process     = (struct process *)grab_object( process );
 
-    if (!first_thread)  /* creating the first thread */
+    if (!current) current = thread;
+
+    if (!booting_thread)  /* first thread ever */
     {
-        current = thread;
-        thread->process = process = create_initial_process(); 
-        assert( process );
+        booting_thread = thread;
+        lock_master_socket(1);
     }
-    else thread->process = (struct process *)grab_object( process );
 
     if ((thread->next = first_thread) != NULL) thread->next->prev = thread;
     first_thread = thread;
@@ -161,13 +163,6 @@
     return NULL;
 }
 
-/* create the initial thread and start the main server loop */
-void create_initial_thread( int fd )
-{
-    create_thread( fd, NULL, 0 );
-    select_loop();
-}
-
 /* handle a client event */
 void thread_poll_event( struct object *obj, int event )
 {
@@ -543,25 +538,34 @@
     release_object( thread );
 }
 
+/* signal that we are finished booting on the client side */
+DECL_HANDLER(boot_done)
+{
+    debug_level = req->debug_level;
+    /* Make sure last_req is initialized */
+    current->last_req = REQ_BOOT_DONE;
+    if (current == booting_thread)
+    {
+        booting_thread = (struct thread *)~0UL;  /* make sure it doesn't match other threads */
+        lock_master_socket(0);  /* allow other clients now */
+    }
+}
+
 /* create a new thread */
 DECL_HANDLER(new_thread)
 {
     struct thread *thread;
-    struct process *process;
     int sock[2];
 
-    if (!(process = get_process_from_id( req->pid ))) return;
-
     if (socketpair( AF_UNIX, SOCK_STREAM, 0, sock ) != -1)
     {
-        if ((thread = create_thread( sock[0], process, req->suspend )))
+        if ((thread = create_thread( sock[0], current->process, req->suspend )))
         {
             req->tid = thread;
             if ((req->handle = alloc_handle( current->process, thread,
                                              THREAD_ALL_ACCESS, req->inherit )) != -1)
             {
                 set_reply_fd( current, sock[1] );
-                release_object( process );
                 /* thread object will be released when the thread gets killed */
                 return;
             }
@@ -570,7 +574,6 @@
         close( sock[1] );
     }
     else file_set_error();
-    release_object( process );
 }
 
 /* retrieve the thread buffer file descriptor */
@@ -590,8 +593,9 @@
     current->unix_pid = req->unix_pid;
     current->teb      = req->teb;
     if (current->suspend + current->process->suspend > 0) stop_thread( current );
-    req->pid = current->process;
-    req->tid = current;
+    req->pid  = current->process;
+    req->tid  = current;
+    req->boot = (current == booting_thread);
 }
 
 /* terminate a thread */
diff --git a/server/thread.h b/server/thread.h
index c19b9d4..5e46493 100644
--- a/server/thread.h
+++ b/server/thread.h
@@ -62,7 +62,7 @@
 
 /* thread functions */
 
-extern void create_initial_thread( int fd );
+extern struct thread *create_thread( int fd, struct process *process, int suspend );
 extern struct thread *get_thread_from_id( void *id );
 extern struct thread *get_thread_from_handle( int handle, unsigned int access );
 extern struct thread *get_thread_from_pid( int pid );
diff --git a/server/trace.c b/server/trace.c
index f140afb..89547b5 100644
--- a/server/trace.c
+++ b/server/trace.c
@@ -215,12 +215,13 @@
 static void dump_new_process_reply( const struct new_process_request *req )
 {
     fprintf( stderr, " pid=%p,", req->pid );
-    fprintf( stderr, " handle=%d", req->handle );
+    fprintf( stderr, " phandle=%d,", req->phandle );
+    fprintf( stderr, " tid=%p,", req->tid );
+    fprintf( stderr, " thandle=%d", req->thandle );
 }
 
 static void dump_new_thread_request( const struct new_thread_request *req )
 {
-    fprintf( stderr, " pid=%p,", req->pid );
     fprintf( stderr, " suspend=%d,", req->suspend );
     fprintf( stderr, " inherit=%d", req->inherit );
 }
@@ -231,9 +232,9 @@
     fprintf( stderr, " handle=%d", req->handle );
 }
 
-static void dump_set_debug_request( const struct set_debug_request *req )
+static void dump_boot_done_request( const struct boot_done_request *req )
 {
-    fprintf( stderr, " level=%d", req->level );
+    fprintf( stderr, " debug_level=%d", req->debug_level );
 }
 
 static void dump_init_process_request( const struct init_process_request *req )
@@ -265,7 +266,8 @@
 static void dump_init_thread_reply( const struct init_thread_request *req )
 {
     fprintf( stderr, " pid=%p,", req->pid );
-    fprintf( stderr, " tid=%p", req->tid );
+    fprintf( stderr, " tid=%p,", req->tid );
+    fprintf( stderr, " boot=%d", req->boot );
 }
 
 static void dump_get_thread_buffer_request( const struct get_thread_buffer_request *req )
@@ -1168,7 +1170,7 @@
 static const dump_func req_dumpers[REQ_NB_REQUESTS] = {
     (dump_func)dump_new_process_request,
     (dump_func)dump_new_thread_request,
-    (dump_func)dump_set_debug_request,
+    (dump_func)dump_boot_done_request,
     (dump_func)dump_init_process_request,
     (dump_func)dump_init_process_done_request,
     (dump_func)dump_init_thread_request,
@@ -1358,7 +1360,7 @@
 static const char * const req_names[REQ_NB_REQUESTS] = {
     "new_process",
     "new_thread",
-    "set_debug",
+    "boot_done",
     "init_process",
     "init_process_done",
     "init_thread",