wine/server/socket.c
Alexandre Julliard 85ed45e377 Release 980822
Sat Aug 22 17:46:19 1998  Ulrich Weigand <weigand@informatik.uni-erlangen.de>

	* [include/dosexe.h] [include/module.h] [include/pe_image.h] 
	  [include/process.h] [include/windows.h] [loader/dos/module.c]
	  [loader/module.c] [loader/ne/module.c] [loader/pe_image.c]
	  [scheduler/process.c] [win32/process.c]
	Partially implemented CreateProcess32.

	* [win32/device.c] [relay32/kernel32.spec] [scheduler/k32obj.c]
	  [misc/registry.c] [win32/file.c]
	Partially implemented VxDCall (VMM registry services).

	* [files/dos_fs.c]
	DOSFS_FindNext made thread-safe.

	* [include/sig_context.h] [include/syslevel.h] [loader/signal.c]
	  [scheduler/syslevel.c] [tools/build.c]
	Replaced CALLTO16_Current_fs by SYSLEVEL_Win16CurrentTeb.

	* [win32/kernel32.c]
	Bugfix: QT_Thunk/FT_Thunk should return 'long' in DX:AX, not EAX.

	* [if1632/relay.c] [relay32/builtin32.c] [windows/msgbox.c]
	  [msdos/int20.c] [msdos/int21.c]
	Use ExitProcess instead of TASK_KillCurrentTask.

	* [include/task.h] [include/thread.h] [loader/task.c] 
	  [scheduler/thread.c] [include/queue.h] [windows/message.c] 
	  [windows/queue.c] [windows/win.c]
	Prevent Win32 threads from entering the TASK_Reschedule loop.
	(Note: Win32 messaging still doesn't work correctly; this patch
	is just preventing the system from crashing when Win32 threads
	call messaging functions. Messages will probably still get lost.)

	* [scheduler/critsection.c]
	Deactivated the use of SEM_UNDO for the SYSTEM_LOCK semaphore;
	for some reason, this leads to problems after threads terminate... 

Sat Aug 22 15:00:00 1998  Jürgen Schmied <juergen.schmied@metronet.de>

	* [include/authors.h]
	New file, includes all names of the developer (former shell.c)

	* [Makefile.in][configure][configure.in][dlls/Makefile.in]
	  [dlls/shell32/Makefile.in][shres.rc]
	Created dlls/shell32 and moved the shell32 stuff in it.
	Started to create internal resources.

	* [dlls/shell32/*]
	Split the shell32 implementation into smaller files.
	New classes: IContextMenu, IExtractIcon, IShellView.
	Implemented Shell_GetImageList().
	shell32 links to comctl32 now dynamically so it can use
	internal/external implementations.

	* [documentation/internal-dll] [documentation/shell32]
	New, could anybody do a spellcheck?

	* [include/commctrl.h]
	Many new LV constants, structures, functions.

	* [controls/comctl32undoc.c]
	Rewrote the DSA* functions.

	* [windows/winpos.c]
	SetShellWindow32, GetShellWindow32.

Sat Aug 22 14:02:15 1998  Alexander Lukyanov <lav@long.yar.ru>

	* [loader/resource.c]
	Mark last accelerator as such in LoadAccelerators32W.

	* [relay32/shell32.spec] [misc/shell.c]
	Add stubs for SHGetSpecialFolderPath[AW].

Sat Aug 22 02:07:42 1998  Adrian Harvey <adrian@select.com.au>

	* [include/file.h] [file/file.c] [msdos/int21.c] [msdos/vxd.c]
	  [misc/lzexpand.c] [win32/kernel32.c] [documentation/filehandles]
	Fixed file handle handling.  Created universal HFILE16 to HFILE32
	translation macro from msdos/int21 code by Ove Kaaven.
	Used macro in all Win16 functions so that win32 handles are translated
	to avoid DOS/Win16 stdxx handles.
	Removed handle translation from int21.c where Win16 functions are
	called.  Changed remaining calls to use new macro names.
	Documented filehandle handling and differences between win 16 & 32.

Fri Aug 21 20:32:49 1998  Alexandre Julliard  <julliard@lrc.epfl.ch>

	* [server/process.c] [server/thread.c]
	Implemented object wait queues and synchronization.

Fri Aug 21 18:40:02 1998  Huw D M Davies <daviesh@abacus.physics.ox.ac.uk>

	* [graphics/psdrv/*]
	DEVMODE dmPaper{Width|Length} fields are in 0.1mm.
	Select a 100 pixel default font in CreateDC.

Thu Aug 20 22:47:39 1998  Uwe Bonnes  <bon@elektron.ikp.physik.tu-darmstadt.de>

	* [objects/bitmap.c]
	Handle bits=32 in SetBitmapBits32 and GetBitmapBits32.

	* [msdos/int21.c]
	Add handling of Int21 0A and 37.

	* [misc/commdlg.c]
	Use MapHModuleLS and MapHModuleSL when translating HINSTANCE16 to
	HINSTANCE32 and vice versa.

	* [win32/file.c]
	CreateFile32A: Abort if filename == NULL.

Thu Aug 20 12:28:31 1998  Marcus Meissner <marcus@jet.franken.de>

	* [*/*]
	Lots of missing prototypes added, some parameter types adapted to match
	SDK.

	* [debugger/stabs.c]
	Don't loop forever if we don't find wine or one of the libxxx.so.

	* [loader/ne/module.c]
	Implemented MapHModuleLS,MapHModuleSL,MapHinstLS,MapHinstSL.

	* [misc/network.c]
	Implemented WNetGetUser32A.

	* [misc/shellord.c]
	Implemented ILRemoveLastID.

	* [multimedia/dsound.c]
	Fixed StarCraft memory leak.

	* [graphics/ddraw.c]
	Removed some unnecessary simple relaying functions, tried polishing
	up the Xlib implementation (still doesn't work), temp. removed Xshm
	code (to be remerged with working Xlib code).

Tue Aug 18 22:29:17 1998  Ove Kaaven <ovek@arcticnet.no>

	* [multimedia/mmio.c] [multimedia/mmsystem.c]
	Fixed most mmio bugs, fully implementing mmioSetBuffer
	buffering, ability to read memory files, and the
	sndPlaySound() SND_MEMORY flag. Most mmio-using programs
	now work fine.

	* [include/dosexe.h] [include/miscemu.h] [include/module.h]
	  [loader/module.c] [loader/task.c] [msdos/dosmem.c]
	Improved DOS VM flexibility and portability somewhat. (Did
	I get the #ifdefs right this time, BSD-ers?)

	* [msdos/int21.c]
	Made "Get Current PSP address" work as expected in a DOS VM.

	* [loader/dos/*]
	Began improving flexibility and portability somewhat. It
	should be easier to add DPMI RMCB callbacks now. The
	DOS VM no longer leaves big files lying around in /tmp
	after a crash.

Tue Aug 18 12:38:31 1998 Turchanov Sergey <turchanov@usa.net>

	* [relay32/winmm.spec]
	This patch allows WinAmp to play WAV files (at least in PCM
	format).

Sun Aug 16 05:34:13 1998  Pablo Saratxaga <srtxg@chanae.alphanet.ch>

	* [windows/keyboard.c]
	Corrected keyboard code to properly handle keys : ? ~ and "
	on non US keyboards.

Sat Aug 15 18:47:14 1998  Brian Craft <bcboy@dorothy.wanglab.brandeis.edu>

	* [windows/win.c]
	Fixed severe bug in EnumChildWindwos().

Thu Aug 13 21:05:35 1998  Eric Kohl <ekohl@abo.rhein-zeitung.de>

	* [controls/tooltips.c]
	Fixed some bugs. Added subclassing support.

	* [controls/toolbar.c]
	Improved tooltip integration. Fixed some bugs.

	* [controls/commctrl.c]
	Changed control registration and added some documentation.
	Fixed ShowHideMenuCtl.

	* [controls/rebar.c][include/rebar.h][include/commctrl.h]
	Improved rebar implementation (still no display).

	* [controls/pager.c][include/pager.h][include/commctrl.h]
	Improved pager implementation (still no display).

	* [misc/imagelist.c]
	Fixed a bug.

	* [documentation/common_controls]
	Updated.

Sun Aug  9 19:50:20 1998  James Juran <jrj120@psu.edu>

	* [Makefile.in] [documentation/Makefile.in] 
	  [programs/Makefile.in] [programs/*/Makefile.in]
	Added uninstall rules, cleaned up install rules a little bit.

Sun Aug  9 13:21:35 1998  Andreas Mohr <100.30936@germany.net>

	* [loader/ne/module.c] [if1632/kernel.spec]
	Added the undocumented HIWORD of GetModuleHandle (hFirstModule).

	* [loader/ne/segment.c]
	Wine forgot to set some NE_SEGFLAGS_*.
	Combined with another loader change, this fixed the
	"BLINKER -- error in loading module" or ghost MessageBox problem
	that about 1% of all Windows programs have.
	Some BLINKER programs still don't work, though.
	But I'm working on it, with great help from Blinkinc.

	* [loader/task.c]
	InitTask needs to decrement the SP register by two as Win95 does.

Sun Aug  9 02:41:28 1998  Ulrich Weigand <weigand@informatik.uni-erlangen.de>

	* [if1632/kernel.spec] [relay32/kernel32.spec] [scheduler/syslevel.c]
	  [loader/main.c] [win32/ordinals.c] [include/syslevel.h] 
	  [scheduler/Makefile.in]
	Implemented Win95 'syslevel' routines (including Win16Lock).

	* [if1632/relay.c] [if1632/thunk.c] [tools/build.c] [loader/task.c]
	  [loader/ne/segment.c] [win32/kernel32.c] [memory/selector.c]
	  [include/stackframe.h] [include/thread.h]
	16-bit %fs handling revised.  Use Win16Lock where appropriate.

	* [include/thread.h] [scheduler/synchro.c] [windows/message.c] 
	  [windows/queue.c] [win32/process.c]
	Implemented MsgWaitForMultipleObjects.

	* [files/change.c] [files/Makefile.in] [scheduler/k32obj.c] 
	  [win32/newfns.c]
	Implemented (dummy) file change notification objects.

	* [debugger/dbg.y] [scheduler/process.c] [scheduler/thread.c]
	  [include/process.h] [include/thread.h]
	Suspend all threads except current when hitting debugger break point.

	* [objects/dib.c]
	Bugfix for CreateDIBSection.
1998-08-22 19:03:56 +00:00

471 lines
13 KiB
C

/*
* Server-side socket communication functions
*
* Copyright (C) 1998 Alexandre Julliard
*/
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <unistd.h>
#include "config.h"
#include "server.h"
#include "server/object.h"
/* Some versions of glibc don't define this */
#ifndef SCM_RIGHTS
#define SCM_RIGHTS 1
#endif
/* client state */
enum state
{
RUNNING, /* running normally */
SENDING, /* sending us a request */
WAITING, /* waiting for us to reply */
READING /* reading our reply */
};
/* client timeout */
struct timeout
{
struct timeval when; /* timeout expiry (absolute time) */
struct timeout *next; /* next in sorted list */
struct timeout *prev; /* prev in sorted list */
int client; /* client id */
};
/* client structure */
struct client
{
enum state state; /* client state */
unsigned int seq; /* current sequence number */
struct header head; /* current msg header */
char *data; /* current msg data */
int count; /* bytes sent/received so far */
int pass_fd; /* fd to pass to and from the client */
struct thread *self; /* client thread (opaque pointer) */
struct timeout timeout; /* client timeout */
};
static struct client *clients[FD_SETSIZE]; /* clients array */
static fd_set read_set, write_set; /* current select sets */
static int nb_clients; /* current number of clients */
static int max_fd; /* max fd in use */
static int initial_client_fd; /* fd of the first client */
static struct timeout *timeout_head; /* sorted timeouts list head */
static struct timeout *timeout_tail; /* sorted timeouts list tail */
/* exit code passed to remove_client */
#define OUT_OF_MEMORY -1
#define BROKEN_PIPE -2
#define PROTOCOL_ERROR -3
/* signal a client protocol error */
static void protocol_error( int client_fd, const char *err, ... )
{
va_list args;
va_start( args, err );
fprintf( stderr, "Protocol error:%d: ", client_fd );
vfprintf( stderr, err, args );
va_end( args );
}
/* send a message to a client that is ready to receive something */
static void do_write( int client_fd )
{
struct client *client = clients[client_fd];
struct iovec vec[2];
#ifndef HAVE_MSGHDR_ACCRIGHTS
struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS,
client->pass_fd };
#endif
struct msghdr msghdr = { NULL, 0, vec, 2, };
int ret;
/* make sure we have something to send */
assert( client->count < client->head.len );
/* make sure the client is listening */
assert( client->state == READING );
if (client->count < sizeof(client->head))
{
vec[0].iov_base = (char *)&client->head + client->count;
vec[0].iov_len = sizeof(client->head) - client->count;
vec[1].iov_base = client->data;
vec[1].iov_len = client->head.len - sizeof(client->head);
}
else
{
vec[0].iov_base = client->data + client->count - sizeof(client->head);
vec[0].iov_len = client->head.len - client->count;
msghdr.msg_iovlen = 1;
}
if (client->pass_fd != -1) /* we have an fd to send */
{
#ifdef HAVE_MSGHDR_ACCRIGHTS
msghdr.msg_accrights = (void *)&client->pass_fd;
msghdr.msg_accrightslen = sizeof(client->pass_fd);
#else
msghdr.msg_control = &cmsg;
msghdr.msg_controllen = sizeof(cmsg);
#endif
}
ret = sendmsg( client_fd, &msghdr, 0 );
if (ret == -1)
{
if (errno != EPIPE) perror("sendmsg");
remove_client( client_fd, BROKEN_PIPE );
return;
}
if (client->pass_fd != -1) /* We sent the fd, now we can close it */
{
close( client->pass_fd );
client->pass_fd = -1;
}
if ((client->count += ret) < client->head.len) return;
/* we have finished with this message */
if (client->data) free( client->data );
client->data = NULL;
client->count = 0;
client->state = RUNNING;
client->seq++;
FD_CLR( client_fd, &write_set );
FD_SET( client_fd, &read_set );
}
/* read a message from a client that has something to say */
static void do_read( int client_fd )
{
struct client *client = clients[client_fd];
struct iovec vec;
int pass_fd = -1;
#ifdef HAVE_MSGHDR_ACCRIGHTS
struct msghdr msghdr = { NULL, 0, &vec, 1, (void*)&pass_fd, sizeof(int) };
#else
struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
struct msghdr msghdr = { NULL, 0, &vec, 1, &cmsg, sizeof(cmsg), 0 };
#endif
int ret;
if (client->count < sizeof(client->head))
{
vec.iov_base = (char *)&client->head + client->count;
vec.iov_len = sizeof(client->head) - client->count;
}
else
{
if (!client->data &&
!(client->data = malloc(client->head.len-sizeof(client->head))))
{
remove_client( client_fd, OUT_OF_MEMORY );
return;
}
vec.iov_base = client->data + client->count - sizeof(client->head);
vec.iov_len = client->head.len - client->count;
}
ret = recvmsg( client_fd, &msghdr, 0 );
if (ret == -1)
{
perror("recvmsg");
remove_client( client_fd, BROKEN_PIPE );
return;
}
#ifndef HAVE_MSGHDR_ACCRIGHTS
pass_fd = cmsg.fd;
#endif
if (pass_fd != -1)
{
/* can only receive one fd per message */
if (client->pass_fd != -1) close( client->pass_fd );
client->pass_fd = pass_fd;
}
else if (!ret) /* closed pipe */
{
remove_client( client_fd, BROKEN_PIPE );
return;
}
if (client->state == RUNNING) client->state = SENDING;
assert( client->state == SENDING );
client->count += ret;
/* received the complete header yet? */
if (client->count < sizeof(client->head)) return;
/* sanity checks */
if (client->head.seq != client->seq)
{
protocol_error( client_fd, "bad sequence %08x instead of %08x\n",
client->head.seq, client->seq );
remove_client( client_fd, PROTOCOL_ERROR );
return;
}
if ((client->head.len < sizeof(client->head)) ||
(client->head.len > MAX_MSG_LENGTH + sizeof(client->head)))
{
protocol_error( client_fd, "bad header length %08x\n",
client->head.len );
remove_client( client_fd, PROTOCOL_ERROR );
return;
}
/* received the whole message? */
if (client->count == client->head.len)
{
/* done reading the data, call the callback function */
int len = client->head.len - sizeof(client->head);
char *data = client->data;
int passed_fd = client->pass_fd;
enum request type = client->head.type;
/* clear the info now, as the client may be deleted by the callback */
client->head.len = 0;
client->head.type = 0;
client->count = 0;
client->data = NULL;
client->pass_fd = -1;
client->state = WAITING;
client->seq++;
call_req_handler( client->self, type, data, len, passed_fd );
if (passed_fd != -1) close( passed_fd );
if (data) free( data );
}
}
/* handle a client timeout */
static void do_timeout( int client_fd )
{
struct client *client = clients[client_fd];
set_timeout( client_fd, 0 ); /* Remove the timeout */
call_timeout_handler( client->self );
}
/* server main loop */
void server_main_loop( int fd )
{
int i, ret;
setsid();
signal( SIGPIPE, SIG_IGN );
/* special magic to create the initial thread */
initial_client_fd = fd;
add_client( initial_client_fd, NULL );
while (nb_clients)
{
fd_set read = read_set, write = write_set;
if (timeout_head)
{
struct timeval tv, now;
gettimeofday( &now, NULL );
if ((timeout_head->when.tv_sec < now.tv_sec) ||
((timeout_head->when.tv_sec == now.tv_sec) &&
(timeout_head->when.tv_usec < now.tv_usec)))
{
do_timeout( timeout_head->client );
continue;
}
tv.tv_sec = timeout_head->when.tv_sec - now.tv_sec;
if ((tv.tv_usec = timeout_head->when.tv_usec - now.tv_usec) < 0)
{
tv.tv_usec += 1000000;
tv.tv_sec--;
}
ret = select( max_fd + 1, &read, &write, NULL, &tv );
}
else /* no timeout */
{
ret = select( max_fd + 1, &read, &write, NULL, NULL );
}
if (!ret) continue;
if (ret == -1) perror("select");
for (i = 0; i <= max_fd; i++)
{
if (FD_ISSET( i, &write ))
{
if (clients[i]) do_write( i );
}
else if (FD_ISSET( i, &read ))
{
if (clients[i]) do_read( i );
}
}
}
}
/*******************************************************************/
/* server-side exported functions */
/* add a client */
int add_client( int client_fd, struct thread *self )
{
int flags;
struct client *client = malloc( sizeof(*client) );
if (!client) return -1;
assert( !clients[client_fd] );
client->state = RUNNING;
client->seq = 0;
client->head.len = 0;
client->head.type = 0;
client->count = 0;
client->data = NULL;
client->self = self;
client->pass_fd = -1;
client->timeout.when.tv_sec = 0;
client->timeout.when.tv_usec = 0;
client->timeout.client = client_fd;
flags = fcntl( client_fd, F_GETFL, 0 );
fcntl( client_fd, F_SETFL, flags | O_NONBLOCK );
clients[client_fd] = client;
FD_SET( client_fd, &read_set );
if (client_fd > max_fd) max_fd = client_fd;
nb_clients++;
return client_fd;
}
/* remove a client */
void remove_client( int client_fd, int exit_code )
{
struct client *client = clients[client_fd];
assert( client );
call_kill_handler( client->self, exit_code );
set_timeout( client_fd, 0 );
clients[client_fd] = NULL;
FD_CLR( client_fd, &read_set );
FD_CLR( client_fd, &write_set );
if (max_fd == client_fd) while (max_fd && !clients[max_fd]) max_fd--;
if (initial_client_fd == client_fd) initial_client_fd = -1;
close( client_fd );
nb_clients--;
/* Purge messages */
if (client->data) free( client->data );
if (client->pass_fd != -1) close( client->pass_fd );
free( client );
}
/* return the fd of the initial client */
int get_initial_client_fd(void)
{
assert( initial_client_fd != -1 );
return initial_client_fd;
}
/* set a client timeout */
void set_timeout( int client_fd, struct timeval *when )
{
struct timeout *tm, *pos;
struct client *client = clients[client_fd];
assert( client );
tm = &client->timeout;
if (tm->when.tv_sec || tm->when.tv_usec)
{
/* there is already a timeout */
if (tm->next) tm->next->prev = tm->prev;
else timeout_tail = tm->prev;
if (tm->prev) tm->prev->next = tm->next;
else timeout_head = tm->next;
tm->when.tv_sec = tm->when.tv_usec = 0;
}
if (!when) return; /* no timeout */
tm->when = *when;
/* Now insert it in the linked list */
for (pos = timeout_head; pos; pos = pos->next)
{
if (pos->when.tv_sec > tm->when.tv_sec) break;
if ((pos->when.tv_sec == tm->when.tv_sec) &&
(pos->when.tv_usec > tm->when.tv_usec)) break;
}
if (pos) /* insert it before 'pos' */
{
if ((tm->prev = pos->prev)) tm->prev->next = tm;
else timeout_head = tm;
tm->next = pos;
pos->prev = tm;
}
else /* insert it at the tail */
{
tm->next = NULL;
if (timeout_tail) timeout_tail->next = tm;
else timeout_head = tm;
tm->prev = timeout_tail;
timeout_tail = tm;
}
}
/* send a reply to a client */
int send_reply_v( int client_fd, int type, int pass_fd,
struct iovec *vec, int veclen )
{
int i;
unsigned int len;
char *p;
struct client *client = clients[client_fd];
assert( client );
assert( client->state == WAITING );
assert( !client->data );
if (debug_level) trace_reply( client->self, type, pass_fd, vec, veclen );
for (i = len = 0; i < veclen; i++) len += vec[i].iov_len;
assert( len < MAX_MSG_LENGTH );
if (len && !(client->data = malloc( len ))) return -1;
client->count = 0;
client->head.len = len + sizeof(client->head);
client->head.type = type;
client->head.seq = client->seq;
client->pass_fd = pass_fd;
for (i = 0, p = client->data; i < veclen; i++)
{
memcpy( p, vec[i].iov_base, vec[i].iov_len );
p += vec[i].iov_len;
}
client->state = READING;
FD_CLR( client_fd, &read_set );
FD_SET( client_fd, &write_set );
return 0;
}