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>
30 enum side { READ_SIDE, WRITE_SIDE };
34 struct object obj; /* object header */
35 struct pipe *other; /* the pipe other end */
36 enum side side; /* which side of the pipe is this */
39 static void pipe_dump( struct object *obj, int verbose );
40 static int pipe_get_poll_events( struct object *obj );
41 static int pipe_get_read_fd( struct object *obj );
42 static int pipe_get_write_fd( struct object *obj );
43 static int pipe_get_info( struct object *obj, struct get_file_info_request *req );
44 static void pipe_destroy( struct object *obj );
46 static const struct object_ops pipe_ops =
48 sizeof(struct pipe), /* size */
50 default_poll_add_queue, /* add_queue */
51 default_poll_remove_queue, /* remove_queue */
52 default_poll_signaled, /* signaled */
53 no_satisfied, /* satisfied */
54 pipe_get_poll_events, /* get_poll_events */
55 default_poll_event, /* poll_event */
56 pipe_get_read_fd, /* get_read_fd */
57 pipe_get_write_fd, /* get_write_fd */
59 pipe_get_info, /* get_file_info */
60 pipe_destroy /* destroy */
64 static struct pipe *create_pipe_side( int fd, int side )
68 if ((pipe = alloc_object( &pipe_ops, fd )))
76 static int create_pipe( struct object *obj[2] )
78 struct pipe *read_pipe;
79 struct pipe *write_pipe;
87 if ((read_pipe = create_pipe_side( fd[0], READ_SIDE )))
89 if ((write_pipe = create_pipe_side( fd[1], WRITE_SIDE )))
91 write_pipe->other = read_pipe;
92 read_pipe->other = write_pipe;
93 obj[0] = &read_pipe->obj;
94 obj[1] = &write_pipe->obj;
97 release_object( read_pipe );
103 static void pipe_dump( struct object *obj, int verbose )
105 struct pipe *pipe = (struct pipe *)obj;
106 assert( obj->ops == &pipe_ops );
107 fprintf( stderr, "Pipe %s-side fd=%d\n",
108 (pipe->side == READ_SIDE) ? "read" : "write", pipe->obj.fd );
111 static int pipe_get_poll_events( struct object *obj )
113 struct pipe *pipe = (struct pipe *)obj;
114 assert( obj->ops == &pipe_ops );
115 return (pipe->side == READ_SIDE) ? POLLIN : POLLOUT;
118 static int pipe_get_read_fd( struct object *obj )
120 struct pipe *pipe = (struct pipe *)obj;
121 assert( obj->ops == &pipe_ops );
125 set_error( ERROR_BROKEN_PIPE );
128 if (pipe->side != READ_SIDE) /* FIXME: should not be necessary */
130 set_error( ERROR_ACCESS_DENIED );
133 return dup( pipe->obj.fd );
136 static int pipe_get_write_fd( struct object *obj )
138 struct pipe *pipe = (struct pipe *)obj;
139 assert( obj->ops == &pipe_ops );
143 set_error( ERROR_BROKEN_PIPE );
146 if (pipe->side != WRITE_SIDE) /* FIXME: should not be necessary */
148 set_error( ERROR_ACCESS_DENIED );
151 return dup( pipe->obj.fd );
154 static int pipe_get_info( struct object *obj, struct get_file_info_request *req )
156 req->type = FILE_TYPE_PIPE;
158 req->access_time = 0;
169 static void pipe_destroy( struct object *obj )
171 struct pipe *pipe = (struct pipe *)obj;
172 assert( obj->ops == &pipe_ops );
174 if (pipe->other) pipe->other->other = NULL;
177 /* create an anonymous pipe */
178 DECL_HANDLER(create_pipe)
180 struct object *obj[2];
181 int hread = -1, hwrite = -1;
183 if (create_pipe( obj ))
185 hread = alloc_handle( current->process, obj[0],
186 STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|GENERIC_READ,
190 hwrite = alloc_handle( current->process, obj[1],
191 STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|GENERIC_WRITE,
194 close_handle( current->process, hread );
196 release_object( obj[0] );
197 release_object( obj[1] );
199 req->handle_read = hread;
200 req->handle_write = hwrite;