1998-12-30 13:06:45 +01:00
|
|
|
/*
|
|
|
|
* Server-side pipe management
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Alexandre Julliard
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
1999-01-03 12:55:56 +01:00
|
|
|
#include <string.h>
|
1998-12-30 13:06:45 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "winerror.h"
|
1999-02-09 16:49:39 +01:00
|
|
|
#include "winbase.h"
|
1999-05-15 12:48:19 +02:00
|
|
|
|
|
|
|
#include "handle.h"
|
|
|
|
#include "thread.h"
|
1999-06-22 19:26:53 +02:00
|
|
|
#include "request.h"
|
1998-12-30 13:06:45 +01:00
|
|
|
|
|
|
|
enum side { READ_SIDE, WRITE_SIDE };
|
|
|
|
|
|
|
|
struct pipe
|
|
|
|
{
|
1999-05-16 18:59:38 +02:00
|
|
|
struct object obj; /* object header */
|
|
|
|
struct pipe *other; /* the pipe other end */
|
|
|
|
struct select_user select; /* select user */
|
|
|
|
enum side side; /* which side of the pipe is this */
|
1998-12-30 13:06:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void pipe_dump( struct object *obj, int verbose );
|
1999-01-01 17:59:27 +01:00
|
|
|
static int pipe_add_queue( struct object *obj, struct wait_queue_entry *entry );
|
1998-12-30 13:06:45 +01:00
|
|
|
static void pipe_remove_queue( struct object *obj, struct wait_queue_entry *entry );
|
|
|
|
static int pipe_signaled( struct object *obj, struct thread *thread );
|
|
|
|
static int pipe_get_read_fd( struct object *obj );
|
|
|
|
static int pipe_get_write_fd( struct object *obj );
|
1999-06-26 10:43:26 +02:00
|
|
|
static int pipe_get_info( struct object *obj, struct get_file_info_request *req );
|
1998-12-30 13:06:45 +01:00
|
|
|
static void pipe_destroy( struct object *obj );
|
|
|
|
|
|
|
|
static const struct object_ops pipe_ops =
|
|
|
|
{
|
1999-06-22 19:26:53 +02:00
|
|
|
sizeof(struct pipe),
|
1998-12-30 13:06:45 +01:00
|
|
|
pipe_dump,
|
|
|
|
pipe_add_queue,
|
|
|
|
pipe_remove_queue,
|
|
|
|
pipe_signaled,
|
|
|
|
no_satisfied,
|
|
|
|
pipe_get_read_fd,
|
|
|
|
pipe_get_write_fd,
|
|
|
|
no_flush,
|
1999-01-03 12:55:56 +01:00
|
|
|
pipe_get_info,
|
1998-12-30 13:06:45 +01:00
|
|
|
pipe_destroy
|
|
|
|
};
|
|
|
|
|
|
|
|
|
1999-06-22 19:26:53 +02:00
|
|
|
static struct pipe *create_pipe_side( int fd, int side )
|
|
|
|
{
|
|
|
|
struct pipe *pipe;
|
|
|
|
|
|
|
|
if ((pipe = alloc_object( &pipe_ops )))
|
|
|
|
{
|
|
|
|
pipe->select.fd = fd;
|
|
|
|
pipe->select.func = default_select_event;
|
|
|
|
pipe->select.private = pipe;
|
|
|
|
pipe->other = NULL;
|
|
|
|
pipe->side = side;
|
|
|
|
register_select_user( &pipe->select );
|
|
|
|
}
|
|
|
|
return pipe;
|
|
|
|
}
|
|
|
|
|
1999-05-15 12:48:19 +02:00
|
|
|
static int create_pipe( struct object *obj[2] )
|
1998-12-30 13:06:45 +01:00
|
|
|
{
|
1999-06-22 19:26:53 +02:00
|
|
|
struct pipe *read_pipe;
|
|
|
|
struct pipe *write_pipe;
|
1998-12-30 13:06:45 +01:00
|
|
|
int fd[2];
|
|
|
|
|
|
|
|
if (pipe( fd ) == -1)
|
|
|
|
{
|
|
|
|
file_set_error();
|
|
|
|
return 0;
|
|
|
|
}
|
1999-06-22 19:26:53 +02:00
|
|
|
if ((read_pipe = create_pipe_side( fd[0], READ_SIDE )))
|
1998-12-30 13:06:45 +01:00
|
|
|
{
|
1999-06-22 19:26:53 +02:00
|
|
|
if ((write_pipe = create_pipe_side( fd[1], WRITE_SIDE )))
|
|
|
|
{
|
|
|
|
write_pipe->other = read_pipe;
|
|
|
|
read_pipe->other = write_pipe;
|
|
|
|
obj[0] = &read_pipe->obj;
|
|
|
|
obj[1] = &write_pipe->obj;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
release_object( read_pipe );
|
1998-12-30 13:06:45 +01:00
|
|
|
}
|
1999-06-22 19:26:53 +02:00
|
|
|
close( fd[0] );
|
|
|
|
close( fd[1] );
|
|
|
|
return 0;
|
1998-12-30 13:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pipe_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct pipe *pipe = (struct pipe *)obj;
|
|
|
|
assert( obj->ops == &pipe_ops );
|
1999-01-03 12:55:56 +01:00
|
|
|
fprintf( stderr, "Pipe %s-side fd=%d\n",
|
1999-05-16 18:59:38 +02:00
|
|
|
(pipe->side == READ_SIDE) ? "read" : "write", pipe->select.fd );
|
1998-12-30 13:06:45 +01:00
|
|
|
}
|
|
|
|
|
1999-01-01 17:59:27 +01:00
|
|
|
static int pipe_add_queue( struct object *obj, struct wait_queue_entry *entry )
|
1998-12-30 13:06:45 +01:00
|
|
|
{
|
|
|
|
struct pipe *pipe = (struct pipe *)obj;
|
|
|
|
assert( obj->ops == &pipe_ops );
|
|
|
|
if (!obj->head) /* first on the queue */
|
1999-05-16 18:59:38 +02:00
|
|
|
set_select_events( &pipe->select,
|
|
|
|
(pipe->side == READ_SIDE) ? READ_EVENT : WRITE_EVENT );
|
1998-12-30 13:06:45 +01:00
|
|
|
add_queue( obj, entry );
|
1999-01-01 17:59:27 +01:00
|
|
|
return 1;
|
1998-12-30 13:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pipe_remove_queue( struct object *obj, struct wait_queue_entry *entry )
|
|
|
|
{
|
|
|
|
struct pipe *pipe = (struct pipe *)grab_object(obj);
|
|
|
|
assert( obj->ops == &pipe_ops );
|
|
|
|
|
|
|
|
remove_queue( obj, entry );
|
|
|
|
if (!obj->head) /* last on the queue is gone */
|
1999-05-16 18:59:38 +02:00
|
|
|
set_select_events( &pipe->select, 0 );
|
1998-12-30 13:06:45 +01:00
|
|
|
release_object( obj );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pipe_signaled( struct object *obj, struct thread *thread )
|
|
|
|
{
|
1999-05-16 18:59:38 +02:00
|
|
|
int event;
|
1998-12-30 13:06:45 +01:00
|
|
|
struct pipe *pipe = (struct pipe *)obj;
|
|
|
|
assert( obj->ops == &pipe_ops );
|
1999-05-16 18:59:38 +02:00
|
|
|
|
|
|
|
event = (pipe->side == READ_SIDE) ? READ_EVENT : WRITE_EVENT;
|
|
|
|
if (check_select_events( &pipe->select, event ))
|
|
|
|
{
|
|
|
|
/* stop waiting on select() if we are signaled */
|
|
|
|
set_select_events( &pipe->select, 0 );
|
|
|
|
return 1;
|
|
|
|
}
|
1998-12-30 13:06:45 +01:00
|
|
|
else
|
1999-05-16 18:59:38 +02:00
|
|
|
{
|
|
|
|
/* restart waiting on select() if we are no longer signaled */
|
|
|
|
if (obj->head) set_select_events( &pipe->select, event );
|
|
|
|
return 0;
|
|
|
|
}
|
1998-12-30 13:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pipe_get_read_fd( struct object *obj )
|
|
|
|
{
|
|
|
|
struct pipe *pipe = (struct pipe *)obj;
|
|
|
|
assert( obj->ops == &pipe_ops );
|
|
|
|
|
|
|
|
if (!pipe->other)
|
|
|
|
{
|
1999-06-22 19:26:53 +02:00
|
|
|
set_error( ERROR_BROKEN_PIPE );
|
1998-12-30 13:06:45 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (pipe->side != READ_SIDE) /* FIXME: should not be necessary */
|
|
|
|
{
|
1999-06-22 19:26:53 +02:00
|
|
|
set_error( ERROR_ACCESS_DENIED );
|
1998-12-30 13:06:45 +01:00
|
|
|
return -1;
|
|
|
|
}
|
1999-05-16 18:59:38 +02:00
|
|
|
return dup( pipe->select.fd );
|
1998-12-30 13:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pipe_get_write_fd( struct object *obj )
|
|
|
|
{
|
|
|
|
struct pipe *pipe = (struct pipe *)obj;
|
|
|
|
assert( obj->ops == &pipe_ops );
|
|
|
|
|
|
|
|
if (!pipe->other)
|
|
|
|
{
|
1999-06-22 19:26:53 +02:00
|
|
|
set_error( ERROR_BROKEN_PIPE );
|
1998-12-30 13:06:45 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (pipe->side != WRITE_SIDE) /* FIXME: should not be necessary */
|
|
|
|
{
|
1999-06-22 19:26:53 +02:00
|
|
|
set_error( ERROR_ACCESS_DENIED );
|
1998-12-30 13:06:45 +01:00
|
|
|
return -1;
|
|
|
|
}
|
1999-05-16 18:59:38 +02:00
|
|
|
return dup( pipe->select.fd );
|
1998-12-30 13:06:45 +01:00
|
|
|
}
|
|
|
|
|
1999-06-26 10:43:26 +02:00
|
|
|
static int pipe_get_info( struct object *obj, struct get_file_info_request *req )
|
1999-01-03 12:55:56 +01:00
|
|
|
{
|
1999-06-26 10:43:26 +02:00
|
|
|
req->type = FILE_TYPE_PIPE;
|
|
|
|
req->attr = 0;
|
|
|
|
req->access_time = 0;
|
|
|
|
req->write_time = 0;
|
|
|
|
req->size_high = 0;
|
|
|
|
req->size_low = 0;
|
|
|
|
req->links = 0;
|
|
|
|
req->index_high = 0;
|
|
|
|
req->index_low = 0;
|
|
|
|
req->serial = 0;
|
1999-01-03 12:55:56 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1998-12-30 13:06:45 +01:00
|
|
|
static void pipe_destroy( struct object *obj )
|
|
|
|
{
|
|
|
|
struct pipe *pipe = (struct pipe *)obj;
|
|
|
|
assert( obj->ops == &pipe_ops );
|
|
|
|
|
|
|
|
if (pipe->other) pipe->other->other = NULL;
|
1999-05-16 18:59:38 +02:00
|
|
|
unregister_select_user( &pipe->select );
|
|
|
|
close( pipe->select.fd );
|
1998-12-30 13:06:45 +01:00
|
|
|
}
|
1999-05-15 12:48:19 +02:00
|
|
|
|
|
|
|
/* create an anonymous pipe */
|
|
|
|
DECL_HANDLER(create_pipe)
|
|
|
|
{
|
|
|
|
struct object *obj[2];
|
1999-06-26 10:43:26 +02:00
|
|
|
int hread = -1, hwrite = -1;
|
|
|
|
|
1999-05-15 12:48:19 +02:00
|
|
|
if (create_pipe( obj ))
|
|
|
|
{
|
1999-06-26 10:43:26 +02:00
|
|
|
hread = alloc_handle( current->process, obj[0],
|
|
|
|
STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|GENERIC_READ,
|
|
|
|
req->inherit );
|
|
|
|
if (hread != -1)
|
1999-05-15 12:48:19 +02:00
|
|
|
{
|
1999-06-26 10:43:26 +02:00
|
|
|
hwrite = alloc_handle( current->process, obj[1],
|
|
|
|
STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|GENERIC_WRITE,
|
|
|
|
req->inherit );
|
|
|
|
if (hwrite == -1)
|
|
|
|
close_handle( current->process, hread );
|
1999-05-15 12:48:19 +02:00
|
|
|
}
|
|
|
|
release_object( obj[0] );
|
|
|
|
release_object( obj[1] );
|
|
|
|
}
|
1999-06-26 10:43:26 +02:00
|
|
|
req->handle_read = hread;
|
|
|
|
req->handle_write = hwrite;
|
1999-05-15 12:48:19 +02:00
|
|
|
}
|