2 * Server-side pipe management
4 * Copyright (C) 1998 Alexandre Julliard
12 #ifdef HAVE_SYS_ERRNO_H
13 #include <sys/errno.h>
17 #include <sys/types.h>
28 enum side
{ READ_SIDE
, WRITE_SIDE
};
32 struct object obj
; /* object header */
33 struct pipe
*other
; /* the pipe other end */
34 struct select_user select
; /* select user */
35 enum side side
; /* which side of the pipe is this */
38 static void pipe_dump( struct object
*obj
, int verbose
);
39 static int pipe_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
40 static void pipe_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
41 static int pipe_signaled( struct object
*obj
, struct thread
*thread
);
42 static int pipe_get_read_fd( struct object
*obj
);
43 static int pipe_get_write_fd( struct object
*obj
);
44 static int pipe_get_info( struct object
*obj
, struct get_file_info_request
*req
);
45 static void pipe_destroy( struct object
*obj
);
47 static const struct object_ops pipe_ops
=
63 static struct pipe
*create_pipe_side( int fd
, int side
)
67 if ((pipe
= alloc_object( &pipe_ops
)))
70 pipe
->select
.func
= default_select_event
;
71 pipe
->select
.private = pipe
;
74 register_select_user( &pipe
->select
);
79 static int create_pipe( struct object
*obj
[2] )
81 struct pipe
*read_pipe
;
82 struct pipe
*write_pipe
;
90 if ((read_pipe
= create_pipe_side( fd
[0], READ_SIDE
)))
92 if ((write_pipe
= create_pipe_side( fd
[1], WRITE_SIDE
)))
94 write_pipe
->other
= read_pipe
;
95 read_pipe
->other
= write_pipe
;
96 obj
[0] = &read_pipe
->obj
;
97 obj
[1] = &write_pipe
->obj
;
100 release_object( read_pipe
);
107 static void pipe_dump( struct object
*obj
, int verbose
)
109 struct pipe
*pipe
= (struct pipe
*)obj
;
110 assert( obj
->ops
== &pipe_ops
);
111 fprintf( stderr
, "Pipe %s-side fd=%d\n",
112 (pipe
->side
== READ_SIDE
) ? "read" : "write", pipe
->select
.fd
);
115 static int pipe_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
117 struct pipe
*pipe
= (struct pipe
*)obj
;
118 assert( obj
->ops
== &pipe_ops
);
119 if (!obj
->head
) /* first on the queue */
120 set_select_events( &pipe
->select
,
121 (pipe
->side
== READ_SIDE
) ? READ_EVENT
: WRITE_EVENT
);
122 add_queue( obj
, entry
);
126 static void pipe_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
128 struct pipe
*pipe
= (struct pipe
*)grab_object(obj
);
129 assert( obj
->ops
== &pipe_ops
);
131 remove_queue( obj
, entry
);
132 if (!obj
->head
) /* last on the queue is gone */
133 set_select_events( &pipe
->select
, 0 );
134 release_object( obj
);
137 static int pipe_signaled( struct object
*obj
, struct thread
*thread
)
140 struct pipe
*pipe
= (struct pipe
*)obj
;
141 assert( obj
->ops
== &pipe_ops
);
143 event
= (pipe
->side
== READ_SIDE
) ? READ_EVENT
: WRITE_EVENT
;
144 if (check_select_events( &pipe
->select
, event
))
146 /* stop waiting on select() if we are signaled */
147 set_select_events( &pipe
->select
, 0 );
152 /* restart waiting on select() if we are no longer signaled */
153 if (obj
->head
) set_select_events( &pipe
->select
, event
);
158 static int pipe_get_read_fd( struct object
*obj
)
160 struct pipe
*pipe
= (struct pipe
*)obj
;
161 assert( obj
->ops
== &pipe_ops
);
165 set_error( ERROR_BROKEN_PIPE
);
168 if (pipe
->side
!= READ_SIDE
) /* FIXME: should not be necessary */
170 set_error( ERROR_ACCESS_DENIED
);
173 return dup( pipe
->select
.fd
);
176 static int pipe_get_write_fd( struct object
*obj
)
178 struct pipe
*pipe
= (struct pipe
*)obj
;
179 assert( obj
->ops
== &pipe_ops
);
183 set_error( ERROR_BROKEN_PIPE
);
186 if (pipe
->side
!= WRITE_SIDE
) /* FIXME: should not be necessary */
188 set_error( ERROR_ACCESS_DENIED
);
191 return dup( pipe
->select
.fd
);
194 static int pipe_get_info( struct object
*obj
, struct get_file_info_request
*req
)
196 req
->type
= FILE_TYPE_PIPE
;
198 req
->access_time
= 0;
209 static void pipe_destroy( struct object
*obj
)
211 struct pipe
*pipe
= (struct pipe
*)obj
;
212 assert( obj
->ops
== &pipe_ops
);
214 if (pipe
->other
) pipe
->other
->other
= NULL
;
215 unregister_select_user( &pipe
->select
);
216 close( pipe
->select
.fd
);
219 /* create an anonymous pipe */
220 DECL_HANDLER(create_pipe
)
222 struct object
*obj
[2];
223 int hread
= -1, hwrite
= -1;
225 if (create_pipe( obj
))
227 hread
= alloc_handle( current
->process
, obj
[0],
228 STANDARD_RIGHTS_REQUIRED
|SYNCHRONIZE
|GENERIC_READ
,
232 hwrite
= alloc_handle( current
->process
, obj
[1],
233 STANDARD_RIGHTS_REQUIRED
|SYNCHRONIZE
|GENERIC_WRITE
,
236 close_handle( current
->process
, hread
);
238 release_object( obj
[0] );
239 release_object( obj
[1] );
241 req
->handle_read
= hread
;
242 req
->handle_write
= hwrite
;