2001-07-10 21:16:49 +02:00
|
|
|
/*
|
|
|
|
* Server-side pipe management
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Alexandre Julliard
|
2001-08-24 01:29:20 +02:00
|
|
|
* Copyright (C) 2001 Mike McCormack
|
|
|
|
*
|
2002-03-10 00:29:33 +01:00
|
|
|
* 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
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
2001-08-24 01:29:20 +02:00
|
|
|
* TODO:
|
2003-05-15 06:22:45 +02:00
|
|
|
* message mode
|
2001-07-10 21:16:49 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2002-04-26 21:05:15 +02:00
|
|
|
#include "wine/port.h"
|
2001-07-10 21:16:49 +02:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2001-07-10 21:16:49 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2003-02-11 23:27:13 +01:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
2001-07-10 21:16:49 +02:00
|
|
|
#include <sys/socket.h>
|
2003-02-11 23:27:13 +01:00
|
|
|
#endif
|
2001-07-10 21:16:49 +02:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2005-03-04 13:38:36 +01:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
2001-07-10 21:16:49 +02:00
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2005-06-08 21:11:46 +02:00
|
|
|
#include "winternl.h"
|
2001-07-10 21:16:49 +02:00
|
|
|
|
2003-01-30 01:26:44 +01:00
|
|
|
#include "file.h"
|
2001-07-10 21:16:49 +02:00
|
|
|
#include "handle.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "request.h"
|
|
|
|
|
2001-08-24 01:29:20 +02:00
|
|
|
enum pipe_state
|
|
|
|
{
|
|
|
|
ps_idle_server,
|
|
|
|
ps_wait_open,
|
|
|
|
ps_connected_server,
|
2003-05-15 06:22:45 +02:00
|
|
|
ps_wait_disconnect,
|
|
|
|
ps_disconnected_server,
|
|
|
|
ps_wait_connect
|
|
|
|
};
|
|
|
|
|
2001-07-10 21:16:49 +02:00
|
|
|
struct named_pipe;
|
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
struct pipe_server
|
|
|
|
{
|
2005-02-03 11:48:23 +01:00
|
|
|
struct object obj; /* object header */
|
|
|
|
struct fd *fd; /* pipe file descriptor */
|
|
|
|
struct list entry; /* entry in named pipe servers list */
|
|
|
|
enum pipe_state state; /* server state */
|
|
|
|
struct pipe_client *client; /* client that this server is connected to */
|
2003-05-15 06:22:45 +02:00
|
|
|
struct named_pipe *pipe;
|
|
|
|
struct timeout_user *flush_poll;
|
|
|
|
struct event *event;
|
2005-04-18 16:57:04 +02:00
|
|
|
struct list wait_q; /* only a single one can be queued */
|
2005-06-08 21:11:46 +02:00
|
|
|
unsigned int options; /* pipe options */
|
2003-05-15 06:22:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct pipe_client
|
|
|
|
{
|
2005-02-03 11:48:23 +01:00
|
|
|
struct object obj; /* object header */
|
|
|
|
struct fd *fd; /* pipe file descriptor */
|
|
|
|
struct pipe_server *server; /* server that this client is connected to */
|
2005-06-08 21:11:46 +02:00
|
|
|
unsigned int flags; /* file flags */
|
2001-07-10 21:16:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct named_pipe
|
|
|
|
{
|
|
|
|
struct object obj; /* object header */
|
2005-04-18 16:57:04 +02:00
|
|
|
unsigned int flags;
|
2001-07-10 21:16:49 +02:00
|
|
|
unsigned int maxinstances;
|
|
|
|
unsigned int outsize;
|
|
|
|
unsigned int insize;
|
|
|
|
unsigned int timeout;
|
2003-05-15 06:22:45 +02:00
|
|
|
unsigned int instances;
|
2005-02-03 11:48:23 +01:00
|
|
|
struct list servers; /* list of servers using this pipe */
|
|
|
|
struct list waiters; /* list of clients waiting to connect */
|
2001-07-10 21:16:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void named_pipe_dump( struct object *obj, int verbose );
|
2003-05-15 06:22:45 +02:00
|
|
|
static void named_pipe_destroy( struct object *obj );
|
2001-07-10 21:16:49 +02:00
|
|
|
|
|
|
|
static const struct object_ops named_pipe_ops =
|
|
|
|
{
|
|
|
|
sizeof(struct named_pipe), /* size */
|
|
|
|
named_pipe_dump, /* dump */
|
|
|
|
no_add_queue, /* add_queue */
|
|
|
|
NULL, /* remove_queue */
|
|
|
|
NULL, /* signaled */
|
|
|
|
NULL, /* satisfied */
|
2005-04-24 19:35:52 +02:00
|
|
|
no_signal, /* signal */
|
2001-07-10 21:16:49 +02:00
|
|
|
no_get_fd, /* get_fd */
|
2005-06-09 17:39:52 +02:00
|
|
|
no_close_handle, /* close_handle */
|
2001-07-10 21:16:49 +02:00
|
|
|
named_pipe_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
/* server end functions */
|
|
|
|
static void pipe_server_dump( struct object *obj, int verbose );
|
|
|
|
static struct fd *pipe_server_get_fd( struct object *obj );
|
|
|
|
static void pipe_server_destroy( struct object *obj);
|
|
|
|
static int pipe_server_flush( struct fd *fd, struct event **event );
|
2005-06-08 21:11:46 +02:00
|
|
|
static int pipe_server_get_info( struct fd *fd );
|
2003-05-15 06:22:45 +02:00
|
|
|
|
|
|
|
static const struct object_ops pipe_server_ops =
|
|
|
|
{
|
|
|
|
sizeof(struct pipe_server), /* size */
|
|
|
|
pipe_server_dump, /* dump */
|
|
|
|
default_fd_add_queue, /* add_queue */
|
|
|
|
default_fd_remove_queue, /* remove_queue */
|
|
|
|
default_fd_signaled, /* signaled */
|
|
|
|
no_satisfied, /* satisfied */
|
2005-04-24 19:35:52 +02:00
|
|
|
no_signal, /* signal */
|
2003-05-15 06:22:45 +02:00
|
|
|
pipe_server_get_fd, /* get_fd */
|
2005-06-09 17:39:52 +02:00
|
|
|
no_close_handle, /* close_handle */
|
2003-05-15 06:22:45 +02:00
|
|
|
pipe_server_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct fd_ops pipe_server_fd_ops =
|
|
|
|
{
|
2005-06-08 21:11:46 +02:00
|
|
|
default_fd_get_poll_events, /* get_poll_events */
|
2003-05-15 06:22:45 +02:00
|
|
|
default_poll_event, /* poll_event */
|
|
|
|
pipe_server_flush, /* flush */
|
2005-06-08 21:11:46 +02:00
|
|
|
pipe_server_get_info, /* get_file_info */
|
|
|
|
default_fd_queue_async, /* queue_async */
|
|
|
|
default_fd_cancel_async, /* cancel_async */
|
2003-05-15 06:22:45 +02:00
|
|
|
};
|
2003-02-19 01:33:32 +01:00
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
/* client end functions */
|
|
|
|
static void pipe_client_dump( struct object *obj, int verbose );
|
|
|
|
static struct fd *pipe_client_get_fd( struct object *obj );
|
|
|
|
static void pipe_client_destroy( struct object *obj );
|
|
|
|
static int pipe_client_flush( struct fd *fd, struct event **event );
|
2005-06-08 21:11:46 +02:00
|
|
|
static int pipe_client_get_info( struct fd *fd );
|
2001-07-10 21:16:49 +02:00
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
static const struct object_ops pipe_client_ops =
|
2001-07-10 21:16:49 +02:00
|
|
|
{
|
2003-05-15 06:22:45 +02:00
|
|
|
sizeof(struct pipe_client), /* size */
|
|
|
|
pipe_client_dump, /* dump */
|
2003-01-30 01:26:44 +01:00
|
|
|
default_fd_add_queue, /* add_queue */
|
|
|
|
default_fd_remove_queue, /* remove_queue */
|
|
|
|
default_fd_signaled, /* signaled */
|
2001-07-10 21:16:49 +02:00
|
|
|
no_satisfied, /* satisfied */
|
2005-04-24 19:35:52 +02:00
|
|
|
no_signal, /* signal */
|
2003-05-15 06:22:45 +02:00
|
|
|
pipe_client_get_fd, /* get_fd */
|
2005-06-09 17:39:52 +02:00
|
|
|
no_close_handle, /* close_handle */
|
2003-05-15 06:22:45 +02:00
|
|
|
pipe_client_destroy /* destroy */
|
2003-01-30 01:26:44 +01:00
|
|
|
};
|
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
static const struct fd_ops pipe_client_fd_ops =
|
2003-01-30 01:26:44 +01:00
|
|
|
{
|
2005-06-08 21:11:46 +02:00
|
|
|
default_fd_get_poll_events, /* get_poll_events */
|
2001-07-10 21:16:49 +02:00
|
|
|
default_poll_event, /* poll_event */
|
2003-05-15 06:22:45 +02:00
|
|
|
pipe_client_flush, /* flush */
|
2005-06-08 21:11:46 +02:00
|
|
|
pipe_client_get_info, /* get_file_info */
|
|
|
|
default_fd_queue_async, /* queue_async */
|
|
|
|
default_fd_cancel_async /* cancel_async */
|
2001-07-10 21:16:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void named_pipe_dump( struct object *obj, int verbose )
|
|
|
|
{
|
2003-05-15 06:22:45 +02:00
|
|
|
struct named_pipe *pipe = (struct named_pipe *) obj;
|
2001-07-10 21:16:49 +02:00
|
|
|
assert( obj->ops == &named_pipe_ops );
|
2005-02-03 11:48:23 +01:00
|
|
|
fprintf( stderr, "Named pipe " );
|
|
|
|
dump_object_name( &pipe->obj );
|
|
|
|
fprintf( stderr, "\n" );
|
2001-07-10 21:16:49 +02:00
|
|
|
}
|
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
static void pipe_server_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct pipe_server *server = (struct pipe_server *) obj;
|
|
|
|
assert( obj->ops == &pipe_server_ops );
|
2005-02-03 11:48:23 +01:00
|
|
|
fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
|
2003-05-15 06:22:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pipe_client_dump( struct object *obj, int verbose )
|
2001-07-10 21:16:49 +02:00
|
|
|
{
|
2003-05-15 06:22:45 +02:00
|
|
|
struct pipe_client *client = (struct pipe_client *) obj;
|
2005-02-03 11:48:23 +01:00
|
|
|
assert( obj->ops == &pipe_client_ops );
|
|
|
|
fprintf( stderr, "Named pipe client server=%p\n", client->server );
|
2001-07-10 21:16:49 +02:00
|
|
|
}
|
|
|
|
|
2005-02-03 11:48:23 +01:00
|
|
|
static void named_pipe_destroy( struct object *obj)
|
|
|
|
{
|
|
|
|
struct named_pipe *pipe = (struct named_pipe *) obj;
|
|
|
|
|
|
|
|
assert( list_empty( &pipe->servers ) );
|
|
|
|
assert( !pipe->instances );
|
2005-04-18 16:57:04 +02:00
|
|
|
async_terminate_queue( &pipe->waiters, STATUS_HANDLES_CLOSED );
|
2003-05-15 06:22:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct fd *pipe_client_get_fd( struct object *obj )
|
|
|
|
{
|
|
|
|
struct pipe_client *client = (struct pipe_client *) obj;
|
2005-06-10 21:54:46 +02:00
|
|
|
if (client->fd)
|
2003-05-15 06:22:45 +02:00
|
|
|
return (struct fd *) grab_object( client->fd );
|
2003-04-17 04:14:04 +02:00
|
|
|
set_error( STATUS_PIPE_DISCONNECTED );
|
|
|
|
return NULL;
|
2003-02-19 01:33:32 +01:00
|
|
|
}
|
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
static struct fd *pipe_server_get_fd( struct object *obj )
|
|
|
|
{
|
|
|
|
struct pipe_server *server = (struct pipe_server *) obj;
|
|
|
|
|
|
|
|
switch(server->state)
|
|
|
|
{
|
|
|
|
case ps_connected_server:
|
|
|
|
case ps_wait_disconnect:
|
|
|
|
assert( server->fd );
|
|
|
|
return (struct fd *) grab_object( server->fd );
|
|
|
|
|
|
|
|
case ps_wait_open:
|
|
|
|
case ps_idle_server:
|
|
|
|
set_error( STATUS_PIPE_LISTENING );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ps_disconnected_server:
|
|
|
|
case ps_wait_connect:
|
|
|
|
set_error( STATUS_PIPE_DISCONNECTED );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void notify_empty( struct pipe_server *server )
|
|
|
|
{
|
2005-06-10 21:54:46 +02:00
|
|
|
if (!server->flush_poll)
|
2003-05-15 06:22:45 +02:00
|
|
|
return;
|
|
|
|
assert( server->state == ps_connected_server );
|
|
|
|
assert( server->event );
|
|
|
|
remove_timeout_user( server->flush_poll );
|
|
|
|
server->flush_poll = NULL;
|
|
|
|
set_event( server->event );
|
|
|
|
release_object( server->event );
|
|
|
|
server->event = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_disconnect( struct pipe_server *server )
|
|
|
|
{
|
|
|
|
/* we may only have a server fd, if the client disconnected */
|
2005-06-10 21:54:46 +02:00
|
|
|
if (server->client)
|
2003-05-15 06:22:45 +02:00
|
|
|
{
|
|
|
|
assert( server->client->server == server );
|
|
|
|
assert( server->client->fd );
|
|
|
|
release_object( server->client->fd );
|
|
|
|
server->client->fd = NULL;
|
|
|
|
}
|
|
|
|
assert( server->fd );
|
|
|
|
release_object( server->fd );
|
|
|
|
server->fd = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pipe_server_destroy( struct object *obj)
|
|
|
|
{
|
|
|
|
struct pipe_server *server = (struct pipe_server *)obj;
|
|
|
|
|
|
|
|
assert( obj->ops == &pipe_server_ops );
|
|
|
|
|
2005-06-10 21:54:46 +02:00
|
|
|
if (server->fd)
|
2003-05-15 06:22:45 +02:00
|
|
|
{
|
|
|
|
notify_empty( server );
|
|
|
|
do_disconnect( server );
|
|
|
|
}
|
|
|
|
|
2005-06-10 21:54:46 +02:00
|
|
|
if (server->client)
|
2003-05-15 06:22:45 +02:00
|
|
|
{
|
|
|
|
server->client->server = NULL;
|
|
|
|
server->client = NULL;
|
|
|
|
}
|
|
|
|
|
2005-04-18 16:57:04 +02:00
|
|
|
async_terminate_head( &server->wait_q, STATUS_HANDLES_CLOSED );
|
2003-05-15 06:22:45 +02:00
|
|
|
|
|
|
|
assert( server->pipe->instances );
|
|
|
|
server->pipe->instances--;
|
|
|
|
|
2005-02-03 11:48:23 +01:00
|
|
|
list_remove( &server->entry );
|
2003-05-15 06:22:45 +02:00
|
|
|
release_object( server->pipe );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pipe_client_destroy( struct object *obj)
|
2001-07-10 21:16:49 +02:00
|
|
|
{
|
2003-05-15 06:22:45 +02:00
|
|
|
struct pipe_client *client = (struct pipe_client *)obj;
|
|
|
|
struct pipe_server *server = client->server;
|
2001-07-10 21:16:49 +02:00
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
assert( obj->ops == &pipe_client_ops );
|
2001-07-10 21:16:49 +02:00
|
|
|
|
2005-06-10 21:54:46 +02:00
|
|
|
if (server)
|
2001-08-24 01:29:20 +02:00
|
|
|
{
|
2003-05-15 06:22:45 +02:00
|
|
|
notify_empty( server );
|
|
|
|
|
2005-06-10 21:54:46 +02:00
|
|
|
switch(server->state)
|
2001-08-24 01:29:20 +02:00
|
|
|
{
|
|
|
|
case ps_connected_server:
|
2003-05-15 06:22:45 +02:00
|
|
|
/* Don't destroy the server's fd here as we can't
|
|
|
|
do a successful flush without it. */
|
|
|
|
server->state = ps_wait_disconnect;
|
|
|
|
release_object( client->fd );
|
|
|
|
client->fd = NULL;
|
2001-08-24 01:29:20 +02:00
|
|
|
break;
|
2003-05-15 06:22:45 +02:00
|
|
|
case ps_disconnected_server:
|
|
|
|
server->state = ps_wait_connect;
|
2001-08-24 01:29:20 +02:00
|
|
|
break;
|
2005-02-03 11:48:23 +01:00
|
|
|
case ps_idle_server:
|
|
|
|
case ps_wait_open:
|
|
|
|
case ps_wait_disconnect:
|
|
|
|
case ps_wait_connect:
|
2003-05-15 06:22:45 +02:00
|
|
|
assert( 0 );
|
2001-08-24 01:29:20 +02:00
|
|
|
}
|
2003-05-15 06:22:45 +02:00
|
|
|
assert( server->client );
|
|
|
|
server->client = NULL;
|
|
|
|
client->server = NULL;
|
2001-08-24 01:29:20 +02:00
|
|
|
}
|
2003-05-15 06:22:45 +02:00
|
|
|
assert( !client->fd );
|
2003-02-19 01:33:32 +01:00
|
|
|
}
|
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
static int pipe_data_remaining( struct pipe_server *server )
|
|
|
|
{
|
|
|
|
struct pollfd pfd;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
assert( server->client );
|
|
|
|
|
|
|
|
fd = get_unix_fd( server->client->fd );
|
2005-06-10 21:54:46 +02:00
|
|
|
if (fd < 0)
|
2003-05-15 06:22:45 +02:00
|
|
|
return 0;
|
|
|
|
pfd.fd = fd;
|
|
|
|
pfd.events = POLLIN;
|
|
|
|
pfd.revents = 0;
|
|
|
|
|
2005-06-10 21:54:46 +02:00
|
|
|
if (0 > poll( &pfd, 1, 0 ))
|
2003-05-15 06:22:45 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return pfd.revents&POLLIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check_flushed( void *arg )
|
|
|
|
{
|
|
|
|
struct pipe_server *server = (struct pipe_server*) arg;
|
|
|
|
|
|
|
|
assert( server->event );
|
2005-06-10 21:54:46 +02:00
|
|
|
if (pipe_data_remaining( server ))
|
2003-05-15 06:22:45 +02:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
2005-05-25 20:41:09 +02:00
|
|
|
gettimeofday( &tv, NULL );
|
2003-05-15 06:22:45 +02:00
|
|
|
add_timeout( &tv, 100 );
|
|
|
|
server->flush_poll = add_timeout_user( &tv, check_flushed, server );
|
|
|
|
}
|
|
|
|
else
|
2004-07-15 20:59:58 +02:00
|
|
|
{
|
|
|
|
/* notify_empty( server ); */
|
|
|
|
server->flush_poll = NULL;
|
|
|
|
set_event( server->event );
|
|
|
|
release_object( server->event );
|
|
|
|
server->event = NULL;
|
|
|
|
}
|
2003-05-15 06:22:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pipe_server_flush( struct fd *fd, struct event **event )
|
|
|
|
{
|
|
|
|
struct pipe_server *server = get_fd_user( fd );
|
|
|
|
|
2005-06-10 21:54:46 +02:00
|
|
|
if (!server)
|
2003-05-15 06:22:45 +02:00
|
|
|
return 0;
|
|
|
|
|
2005-06-10 21:54:46 +02:00
|
|
|
if (server->state != ps_connected_server)
|
2003-05-15 06:22:45 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* FIXME: if multiple threads flush the same pipe,
|
|
|
|
maybe should create a list of processes to notify */
|
2005-06-10 21:54:46 +02:00
|
|
|
if (server->flush_poll)
|
2003-05-15 06:22:45 +02:00
|
|
|
return 0;
|
|
|
|
|
2005-06-10 21:54:46 +02:00
|
|
|
if (pipe_data_remaining( server ))
|
2003-05-15 06:22:45 +02:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
/* this kind of sux -
|
|
|
|
there's no unix way to be alerted when a pipe becomes empty */
|
|
|
|
server->event = create_event( NULL, 0, 0, 0 );
|
2005-06-10 21:54:46 +02:00
|
|
|
if (!server->event)
|
2003-05-15 06:22:45 +02:00
|
|
|
return 0;
|
2005-05-25 20:41:09 +02:00
|
|
|
gettimeofday( &tv, NULL );
|
2003-05-15 06:22:45 +02:00
|
|
|
add_timeout( &tv, 100 );
|
|
|
|
server->flush_poll = add_timeout_user( &tv, check_flushed, server );
|
|
|
|
*event = server->event;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pipe_client_flush( struct fd *fd, struct event **event )
|
|
|
|
{
|
|
|
|
/* FIXME: what do we have to do for this? */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-08 21:11:46 +02:00
|
|
|
static inline int is_overlapped( unsigned int options )
|
2001-10-05 21:45:45 +02:00
|
|
|
{
|
2005-06-08 21:11:46 +02:00
|
|
|
return !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pipe_server_get_info( struct fd *fd )
|
|
|
|
{
|
|
|
|
struct pipe_server *server = get_fd_user( fd );
|
|
|
|
int flags = FD_FLAG_AVAILABLE;
|
|
|
|
|
|
|
|
if (is_overlapped( server->options )) flags |= FD_FLAG_OVERLAPPED;
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pipe_client_get_info( struct fd *fd )
|
|
|
|
{
|
|
|
|
struct pipe_client *client = get_fd_user( fd );
|
|
|
|
int flags = FD_FLAG_AVAILABLE;
|
|
|
|
|
|
|
|
if (is_overlapped( client->flags )) flags |= FD_FLAG_OVERLAPPED;
|
|
|
|
|
|
|
|
return flags;
|
2001-10-05 21:45:45 +02:00
|
|
|
}
|
|
|
|
|
2001-07-10 21:16:49 +02:00
|
|
|
static struct named_pipe *create_named_pipe( const WCHAR *name, size_t len )
|
|
|
|
{
|
|
|
|
struct named_pipe *pipe;
|
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
pipe = create_named_object( sync_namespace, &named_pipe_ops, name, len );
|
2005-06-10 21:54:46 +02:00
|
|
|
if (pipe)
|
2001-07-10 21:16:49 +02:00
|
|
|
{
|
2005-06-10 21:54:46 +02:00
|
|
|
if (get_error() != STATUS_OBJECT_NAME_COLLISION)
|
2001-07-10 21:16:49 +02:00
|
|
|
{
|
|
|
|
/* initialize it if it didn't already exist */
|
2003-05-15 06:22:45 +02:00
|
|
|
pipe->instances = 0;
|
2005-02-03 11:48:23 +01:00
|
|
|
list_init( &pipe->servers );
|
|
|
|
list_init( &pipe->waiters );
|
2001-07-10 21:16:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return pipe;
|
|
|
|
}
|
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
static struct named_pipe *open_named_pipe( const WCHAR *name, size_t len )
|
|
|
|
{
|
|
|
|
struct object *obj;
|
|
|
|
|
|
|
|
if ((obj = find_object( sync_namespace, name, len )))
|
|
|
|
{
|
|
|
|
if (obj->ops == &named_pipe_ops) return (struct named_pipe *)obj;
|
|
|
|
release_object( obj );
|
|
|
|
set_error( STATUS_OBJECT_TYPE_MISMATCH );
|
|
|
|
}
|
|
|
|
else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
static struct pipe_server *get_pipe_server_obj( struct process *process,
|
|
|
|
obj_handle_t handle, unsigned int access )
|
2001-07-10 21:16:49 +02:00
|
|
|
{
|
2003-05-15 06:22:45 +02:00
|
|
|
struct object *obj;
|
|
|
|
obj = get_handle_obj( process, handle, access, &pipe_server_ops );
|
|
|
|
return (struct pipe_server *) obj;
|
2001-07-10 21:16:49 +02:00
|
|
|
}
|
|
|
|
|
2005-06-08 21:11:46 +02:00
|
|
|
static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options )
|
2001-07-10 21:16:49 +02:00
|
|
|
{
|
2003-05-15 06:22:45 +02:00
|
|
|
struct pipe_server *server;
|
2001-07-10 21:16:49 +02:00
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
server = alloc_object( &pipe_server_ops );
|
2005-06-10 21:54:46 +02:00
|
|
|
if (!server)
|
2001-07-10 21:16:49 +02:00
|
|
|
return NULL;
|
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
server->fd = NULL;
|
|
|
|
server->pipe = pipe;
|
2005-02-03 11:48:23 +01:00
|
|
|
server->state = ps_idle_server;
|
2003-05-15 06:22:45 +02:00
|
|
|
server->client = NULL;
|
|
|
|
server->flush_poll = NULL;
|
2005-06-08 21:11:46 +02:00
|
|
|
server->options = options;
|
2005-04-18 16:57:04 +02:00
|
|
|
list_init( &server->wait_q );
|
2001-07-10 21:16:49 +02:00
|
|
|
|
2005-02-03 11:48:23 +01:00
|
|
|
list_add_head( &pipe->servers, &server->entry );
|
2003-05-15 06:22:45 +02:00
|
|
|
grab_object( pipe );
|
2001-07-10 21:16:49 +02:00
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
return server;
|
2001-07-10 21:16:49 +02:00
|
|
|
}
|
|
|
|
|
2005-06-08 21:11:46 +02:00
|
|
|
static struct pipe_client *create_pipe_client( struct pipe_server *server, unsigned int flags )
|
2001-07-10 21:16:49 +02:00
|
|
|
{
|
2003-05-15 06:22:45 +02:00
|
|
|
struct pipe_client *client;
|
2001-07-10 21:16:49 +02:00
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
client = alloc_object( &pipe_client_ops );
|
2005-06-10 21:54:46 +02:00
|
|
|
if (!client)
|
2003-05-15 06:22:45 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
client->fd = NULL;
|
|
|
|
client->server = server;
|
2005-06-08 21:11:46 +02:00
|
|
|
client->flags = flags;
|
2003-05-15 06:22:45 +02:00
|
|
|
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
2005-02-03 11:48:23 +01:00
|
|
|
static inline struct pipe_server *find_server( struct named_pipe *pipe, enum pipe_state state )
|
2003-05-15 06:22:45 +02:00
|
|
|
{
|
2005-02-03 11:48:23 +01:00
|
|
|
struct pipe_server *server;
|
2003-05-15 06:22:45 +02:00
|
|
|
|
2005-02-03 11:48:23 +01:00
|
|
|
LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
|
|
|
|
{
|
|
|
|
if (server->state == state) return (struct pipe_server *)grab_object( server );
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-07-10 21:16:49 +02:00
|
|
|
|
2005-02-03 11:48:23 +01:00
|
|
|
static inline struct pipe_server *find_server2( struct named_pipe *pipe,
|
|
|
|
enum pipe_state state1, enum pipe_state state2 )
|
|
|
|
{
|
|
|
|
struct pipe_server *server;
|
2001-08-24 01:29:20 +02:00
|
|
|
|
2005-02-03 11:48:23 +01:00
|
|
|
LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
|
|
|
|
{
|
|
|
|
if (server->state == state1 || server->state == state2)
|
|
|
|
return (struct pipe_server *)grab_object( server );
|
|
|
|
}
|
|
|
|
return NULL;
|
2001-07-10 21:16:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DECL_HANDLER(create_named_pipe)
|
|
|
|
{
|
|
|
|
struct named_pipe *pipe;
|
2003-05-15 06:22:45 +02:00
|
|
|
struct pipe_server *server;
|
2001-07-10 21:16:49 +02:00
|
|
|
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->handle = 0;
|
|
|
|
pipe = create_named_pipe( get_req_data(), get_req_data_size() );
|
2005-06-10 21:54:46 +02:00
|
|
|
if (!pipe)
|
2001-07-10 21:16:49 +02:00
|
|
|
return;
|
|
|
|
|
2005-06-10 21:54:46 +02:00
|
|
|
if (get_error() != STATUS_OBJECT_NAME_COLLISION)
|
2001-08-27 21:03:42 +02:00
|
|
|
{
|
|
|
|
pipe->insize = req->insize;
|
|
|
|
pipe->outsize = req->outsize;
|
|
|
|
pipe->maxinstances = req->maxinstances;
|
|
|
|
pipe->timeout = req->timeout;
|
2005-04-18 16:57:04 +02:00
|
|
|
pipe->flags = req->flags;
|
2001-08-27 21:03:42 +02:00
|
|
|
}
|
2003-05-15 06:22:45 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
set_error( 0 ); /* clear the name collision */
|
2005-06-10 21:54:46 +02:00
|
|
|
if (pipe->maxinstances <= pipe->instances)
|
2003-05-15 06:22:45 +02:00
|
|
|
{
|
|
|
|
set_error( STATUS_PIPE_BUSY );
|
|
|
|
release_object( pipe );
|
|
|
|
return;
|
|
|
|
}
|
2005-06-10 21:54:46 +02:00
|
|
|
if ((pipe->maxinstances != req->maxinstances) ||
|
|
|
|
(pipe->timeout != req->timeout) ||
|
|
|
|
(pipe->flags != req->flags))
|
2003-05-15 06:22:45 +02:00
|
|
|
{
|
|
|
|
set_error( STATUS_ACCESS_DENIED );
|
|
|
|
release_object( pipe );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2001-08-27 21:03:42 +02:00
|
|
|
|
2005-06-08 21:11:46 +02:00
|
|
|
server = create_pipe_server( pipe, req->options );
|
2005-06-10 21:54:46 +02:00
|
|
|
if (server)
|
2001-07-10 21:16:49 +02:00
|
|
|
{
|
2003-05-15 06:22:45 +02:00
|
|
|
reply->handle = alloc_handle( current->process, server,
|
2004-02-24 02:06:30 +01:00
|
|
|
GENERIC_READ|GENERIC_WRITE, req->inherit );
|
2003-05-15 06:22:45 +02:00
|
|
|
server->pipe->instances++;
|
|
|
|
release_object( server );
|
2001-07-10 21:16:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
release_object( pipe );
|
|
|
|
}
|
|
|
|
|
|
|
|
DECL_HANDLER(open_named_pipe)
|
|
|
|
{
|
2003-05-15 06:22:45 +02:00
|
|
|
struct pipe_server *server;
|
|
|
|
struct pipe_client *client;
|
2001-07-10 21:16:49 +02:00
|
|
|
struct named_pipe *pipe;
|
2003-05-15 06:22:45 +02:00
|
|
|
int fds[2];
|
2001-07-10 21:16:49 +02:00
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
pipe = open_named_pipe( get_req_data(), get_req_data_size() );
|
2005-06-10 21:54:46 +02:00
|
|
|
if (!pipe)
|
2001-07-10 21:16:49 +02:00
|
|
|
{
|
2003-02-19 01:33:32 +01:00
|
|
|
set_error( STATUS_NO_SUCH_FILE );
|
|
|
|
return;
|
|
|
|
}
|
2003-05-15 06:22:45 +02:00
|
|
|
|
2005-02-03 11:48:23 +01:00
|
|
|
server = find_server2( pipe, ps_idle_server, ps_wait_open );
|
2003-05-15 06:22:45 +02:00
|
|
|
release_object( pipe );
|
|
|
|
|
2005-06-10 21:54:46 +02:00
|
|
|
if (!server)
|
2003-02-19 01:33:32 +01:00
|
|
|
{
|
|
|
|
set_error( STATUS_PIPE_NOT_AVAILABLE );
|
|
|
|
return;
|
|
|
|
}
|
2001-08-24 01:29:20 +02:00
|
|
|
|
2005-06-08 21:11:46 +02:00
|
|
|
client = create_pipe_client( server, req->flags );
|
2005-06-10 21:54:46 +02:00
|
|
|
if (client)
|
2003-05-15 06:22:45 +02:00
|
|
|
{
|
2005-06-09 11:46:55 +02:00
|
|
|
if (!socketpair( PF_UNIX, SOCK_STREAM, 0, fds ))
|
2001-07-10 21:16:49 +02:00
|
|
|
{
|
2005-06-09 11:46:55 +02:00
|
|
|
int res = 0;
|
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
assert( !client->fd );
|
|
|
|
assert( !server->fd );
|
2005-06-09 11:46:55 +02:00
|
|
|
|
|
|
|
/* for performance reasons, only set nonblocking mode when using
|
|
|
|
* overlapped I/O. Otherwise, we will be doing too much busy
|
|
|
|
* looping */
|
|
|
|
if (is_overlapped( req->flags ))
|
|
|
|
res = fcntl( fds[1], F_SETFL, O_NONBLOCK );
|
|
|
|
if ((res != -1) && is_overlapped( server->options ))
|
|
|
|
res = fcntl( fds[0], F_SETFL, O_NONBLOCK );
|
|
|
|
|
2005-05-09 11:25:45 +02:00
|
|
|
client->fd = create_anonymous_fd( &pipe_client_fd_ops,
|
2003-05-15 06:22:45 +02:00
|
|
|
fds[1], &client->obj );
|
|
|
|
server->fd = create_anonymous_fd( &pipe_server_fd_ops,
|
|
|
|
fds[0], &server->obj );
|
2005-06-09 11:46:55 +02:00
|
|
|
if (client->fd && server->fd && res != 1)
|
2001-07-10 21:16:49 +02:00
|
|
|
{
|
2005-06-10 21:54:46 +02:00
|
|
|
if (server->state == ps_wait_open)
|
2005-04-18 16:57:04 +02:00
|
|
|
async_terminate_head( &server->wait_q, STATUS_SUCCESS );
|
|
|
|
assert( list_empty( &server->wait_q ) );
|
2003-05-15 06:22:45 +02:00
|
|
|
server->state = ps_connected_server;
|
|
|
|
server->client = client;
|
|
|
|
client->server = server;
|
|
|
|
reply->handle = alloc_handle( current->process, client,
|
2003-06-14 03:31:56 +02:00
|
|
|
req->access, req->inherit );
|
2001-07-10 21:16:49 +02:00
|
|
|
}
|
|
|
|
}
|
2003-05-15 06:22:45 +02:00
|
|
|
else
|
|
|
|
file_set_error();
|
2001-07-10 21:16:49 +02:00
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
release_object( client );
|
2003-02-19 01:33:32 +01:00
|
|
|
}
|
2005-02-03 11:48:23 +01:00
|
|
|
release_object( server );
|
2001-07-10 21:16:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DECL_HANDLER(connect_named_pipe)
|
|
|
|
{
|
2003-05-15 06:22:45 +02:00
|
|
|
struct pipe_server *server;
|
2001-07-10 21:16:49 +02:00
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
server = get_pipe_server_obj(current->process, req->handle, 0);
|
2005-06-10 21:54:46 +02:00
|
|
|
if (!server)
|
2001-07-10 21:16:49 +02:00
|
|
|
return;
|
|
|
|
|
2005-06-10 21:54:46 +02:00
|
|
|
switch(server->state)
|
2001-07-10 21:16:49 +02:00
|
|
|
{
|
2003-05-15 06:22:45 +02:00
|
|
|
case ps_idle_server:
|
|
|
|
case ps_wait_connect:
|
|
|
|
assert( !server->fd );
|
|
|
|
server->state = ps_wait_open;
|
2005-04-18 16:57:04 +02:00
|
|
|
create_async( current, NULL, &server->wait_q,
|
|
|
|
req->func, req->overlapped, NULL );
|
|
|
|
async_terminate_queue( &server->pipe->waiters, STATUS_SUCCESS );
|
2003-05-15 06:22:45 +02:00
|
|
|
break;
|
|
|
|
case ps_connected_server:
|
|
|
|
assert( server->fd );
|
|
|
|
set_error( STATUS_PIPE_CONNECTED );
|
|
|
|
break;
|
|
|
|
case ps_disconnected_server:
|
|
|
|
set_error( STATUS_PIPE_BUSY );
|
|
|
|
break;
|
|
|
|
case ps_wait_disconnect:
|
|
|
|
set_error( STATUS_NO_DATA_DETECTED );
|
|
|
|
break;
|
2005-02-03 11:48:23 +01:00
|
|
|
case ps_wait_open:
|
2003-05-15 06:22:45 +02:00
|
|
|
set_error( STATUS_INVALID_HANDLE );
|
|
|
|
break;
|
2001-08-24 01:29:20 +02:00
|
|
|
}
|
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
release_object(server);
|
2001-08-24 01:29:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DECL_HANDLER(wait_named_pipe)
|
|
|
|
{
|
|
|
|
struct named_pipe *pipe;
|
2003-05-15 06:22:45 +02:00
|
|
|
struct pipe_server *server;
|
2001-08-24 01:29:20 +02:00
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
if (!(pipe = open_named_pipe( get_req_data(), get_req_data_size() )))
|
2001-08-24 01:29:20 +02:00
|
|
|
{
|
2003-02-19 01:33:32 +01:00
|
|
|
set_error( STATUS_PIPE_NOT_AVAILABLE );
|
|
|
|
return;
|
|
|
|
}
|
2003-05-15 06:22:45 +02:00
|
|
|
server = find_server( pipe, ps_wait_open );
|
2005-06-10 21:54:46 +02:00
|
|
|
if (server)
|
2003-02-19 01:33:32 +01:00
|
|
|
{
|
2003-05-15 06:22:45 +02:00
|
|
|
/* there's already a server waiting for a client to connect */
|
2005-04-18 16:57:04 +02:00
|
|
|
thread_queue_apc( current, NULL, req->func, APC_ASYNC_IO,
|
|
|
|
1, req->overlapped, NULL, (void *)STATUS_SUCCESS );
|
2003-05-15 06:22:45 +02:00
|
|
|
release_object( server );
|
2003-02-19 01:33:32 +01:00
|
|
|
}
|
|
|
|
else
|
2005-02-08 13:54:23 +01:00
|
|
|
{
|
|
|
|
unsigned int timeout;
|
|
|
|
if (req->timeout == NMPWAIT_USE_DEFAULT_WAIT)
|
|
|
|
timeout = pipe->timeout;
|
|
|
|
else
|
|
|
|
timeout = req->timeout;
|
|
|
|
|
|
|
|
if (req->timeout == NMPWAIT_WAIT_FOREVER)
|
2005-04-18 16:57:04 +02:00
|
|
|
create_async( current, NULL, &pipe->waiters,
|
|
|
|
req->func, req->overlapped, NULL );
|
2005-02-08 13:54:23 +01:00
|
|
|
else
|
2005-04-18 16:57:04 +02:00
|
|
|
create_async( current, &timeout, &pipe->waiters,
|
|
|
|
req->func, req->overlapped, NULL );
|
2005-02-08 13:54:23 +01:00
|
|
|
}
|
2001-08-24 01:29:20 +02:00
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
release_object( pipe );
|
2001-08-24 01:29:20 +02:00
|
|
|
}
|
2001-07-10 21:16:49 +02:00
|
|
|
|
2001-08-24 01:29:20 +02:00
|
|
|
DECL_HANDLER(disconnect_named_pipe)
|
|
|
|
{
|
2003-05-15 06:22:45 +02:00
|
|
|
struct pipe_server *server;
|
2001-08-24 01:29:20 +02:00
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
reply->fd = -1;
|
|
|
|
server = get_pipe_server_obj( current->process, req->handle, 0 );
|
2005-06-10 21:54:46 +02:00
|
|
|
if (!server)
|
2001-08-24 01:29:20 +02:00
|
|
|
return;
|
2005-06-10 21:54:46 +02:00
|
|
|
switch(server->state)
|
2001-08-24 01:29:20 +02:00
|
|
|
{
|
2003-05-15 06:22:45 +02:00
|
|
|
case ps_connected_server:
|
|
|
|
assert( server->fd );
|
|
|
|
assert( server->client );
|
|
|
|
assert( server->client->fd );
|
|
|
|
|
|
|
|
notify_empty( server );
|
|
|
|
|
|
|
|
/* Dump the client and server fds, but keep the pointers
|
|
|
|
around - client loses all waiting data */
|
|
|
|
server->state = ps_disconnected_server;
|
|
|
|
do_disconnect( server );
|
|
|
|
reply->fd = flush_cached_fd( current->process, req->handle );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ps_wait_disconnect:
|
|
|
|
assert( !server->client );
|
|
|
|
assert( server->fd );
|
|
|
|
do_disconnect( server );
|
|
|
|
server->state = ps_wait_connect;
|
|
|
|
reply->fd = flush_cached_fd( current->process, req->handle );
|
|
|
|
break;
|
|
|
|
|
2005-02-03 11:48:23 +01:00
|
|
|
case ps_idle_server:
|
|
|
|
case ps_wait_open:
|
|
|
|
case ps_disconnected_server:
|
|
|
|
case ps_wait_connect:
|
2003-05-15 06:22:45 +02:00
|
|
|
set_error( STATUS_PIPE_DISCONNECTED );
|
2005-02-03 11:48:23 +01:00
|
|
|
break;
|
2001-08-24 01:29:20 +02:00
|
|
|
}
|
2003-05-15 06:22:45 +02:00
|
|
|
release_object( server );
|
2001-07-10 21:16:49 +02:00
|
|
|
}
|
2001-08-27 21:03:42 +02:00
|
|
|
|
|
|
|
DECL_HANDLER(get_named_pipe_info)
|
|
|
|
{
|
2003-05-15 06:22:45 +02:00
|
|
|
struct pipe_server *server;
|
2001-08-27 21:03:42 +02:00
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
server = get_pipe_server_obj( current->process, req->handle, 0 );
|
2005-06-10 21:54:46 +02:00
|
|
|
if (!server)
|
2001-08-27 21:03:42 +02:00
|
|
|
return;
|
|
|
|
|
2005-04-18 16:57:04 +02:00
|
|
|
reply->flags = server->pipe->flags;
|
2003-05-15 06:22:45 +02:00
|
|
|
reply->maxinstances = server->pipe->maxinstances;
|
|
|
|
reply->insize = server->pipe->insize;
|
|
|
|
reply->outsize = server->pipe->outsize;
|
2001-08-27 21:03:42 +02:00
|
|
|
|
2003-05-15 06:22:45 +02:00
|
|
|
release_object(server);
|
2001-08-27 21:03:42 +02:00
|
|
|
}
|