2 * Server-side pipe management
4 * Copyright (C) 1998 Alexandre Julliard
14 #ifdef HAVE_SYS_ERRNO_H
15 #include <sys/errno.h>
19 #include <sys/types.h>
29 enum side
{ READ_SIDE
, WRITE_SIDE
};
33 struct object obj
; /* object header */
34 struct pipe
*other
; /* the pipe other end */
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_get_poll_events( struct object
*obj
);
40 static int pipe_get_read_fd( struct object
*obj
);
41 static int pipe_get_write_fd( struct object
*obj
);
42 static int pipe_get_info( struct object
*obj
, struct get_file_info_request
*req
);
43 static void pipe_destroy( struct object
*obj
);
45 static const struct object_ops pipe_ops
=
47 sizeof(struct pipe
), /* size */
49 default_poll_add_queue
, /* add_queue */
50 default_poll_remove_queue
, /* remove_queue */
51 default_poll_signaled
, /* signaled */
52 no_satisfied
, /* satisfied */
53 pipe_get_poll_events
, /* get_poll_events */
54 default_poll_event
, /* poll_event */
55 pipe_get_read_fd
, /* get_read_fd */
56 pipe_get_write_fd
, /* get_write_fd */
58 pipe_get_info
, /* get_file_info */
59 pipe_destroy
/* destroy */
63 static struct pipe
*create_pipe_side( int fd
, int side
)
67 if ((pipe
= alloc_object( &pipe_ops
, fd
)))
75 static int create_pipe( struct object
*obj
[2] )
77 struct pipe
*read_pipe
;
78 struct pipe
*write_pipe
;
86 if ((read_pipe
= create_pipe_side( fd
[0], READ_SIDE
)))
88 if ((write_pipe
= create_pipe_side( fd
[1], WRITE_SIDE
)))
90 write_pipe
->other
= read_pipe
;
91 read_pipe
->other
= write_pipe
;
92 obj
[0] = &read_pipe
->obj
;
93 obj
[1] = &write_pipe
->obj
;
96 release_object( read_pipe
);
102 static void pipe_dump( struct object
*obj
, int verbose
)
104 struct pipe
*pipe
= (struct pipe
*)obj
;
105 assert( obj
->ops
== &pipe_ops
);
106 fprintf( stderr
, "Pipe %s-side fd=%d\n",
107 (pipe
->side
== READ_SIDE
) ? "read" : "write", pipe
->obj
.fd
);
110 static int pipe_get_poll_events( struct object
*obj
)
112 struct pipe
*pipe
= (struct pipe
*)obj
;
113 assert( obj
->ops
== &pipe_ops
);
114 return (pipe
->side
== READ_SIDE
) ? POLLIN
: POLLOUT
;
117 static int pipe_get_read_fd( struct object
*obj
)
119 struct pipe
*pipe
= (struct pipe
*)obj
;
120 assert( obj
->ops
== &pipe_ops
);
124 set_error( STATUS_PIPE_BROKEN
);
127 if (pipe
->side
!= READ_SIDE
) /* FIXME: should not be necessary */
129 set_error( STATUS_ACCESS_DENIED
);
132 return dup( pipe
->obj
.fd
);
135 static int pipe_get_write_fd( struct object
*obj
)
137 struct pipe
*pipe
= (struct pipe
*)obj
;
138 assert( obj
->ops
== &pipe_ops
);
142 set_error( STATUS_PIPE_BROKEN
);
145 if (pipe
->side
!= WRITE_SIDE
) /* FIXME: should not be necessary */
147 set_error( STATUS_ACCESS_DENIED
);
150 return dup( pipe
->obj
.fd
);
153 static int pipe_get_info( struct object
*obj
, struct get_file_info_request
*req
)
155 req
->type
= FILE_TYPE_PIPE
;
157 req
->access_time
= 0;
168 static void pipe_destroy( struct object
*obj
)
170 struct pipe
*pipe
= (struct pipe
*)obj
;
171 assert( obj
->ops
== &pipe_ops
);
173 if (pipe
->other
) pipe
->other
->other
= NULL
;
176 /* create an anonymous pipe */
177 DECL_HANDLER(create_pipe
)
179 struct object
*obj
[2];
180 int hread
= -1, hwrite
= -1;
182 if (create_pipe( obj
))
184 hread
= alloc_handle( current
->process
, obj
[0],
185 STANDARD_RIGHTS_REQUIRED
|SYNCHRONIZE
|GENERIC_READ
,
189 hwrite
= alloc_handle( current
->process
, obj
[1],
190 STANDARD_RIGHTS_REQUIRED
|SYNCHRONIZE
|GENERIC_WRITE
,
193 close_handle( current
->process
, hread
);
195 release_object( obj
[0] );
196 release_object( obj
[1] );
198 req
->handle_read
= hread
;
199 req
->handle_write
= hwrite
;