Server reorganization:
[wine/multimedia.git] / server / pipe.c
blob575f6f2541392a45f9de95deca9c7dbdd1ffa7af
1 /*
2 * Server-side pipe management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <fcntl.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <sys/errno.h>
13 #include <sys/stat.h>
14 #include <sys/time.h>
15 #include <sys/types.h>
16 #include <time.h>
17 #include <unistd.h>
19 #include "winerror.h"
20 #include "winbase.h"
22 #include "handle.h"
23 #include "thread.h"
25 enum side { READ_SIDE, WRITE_SIDE };
27 struct pipe
29 struct object obj; /* object header */
30 struct pipe *other; /* the pipe other end */
31 int fd; /* Unix file descriptor */
32 enum side side; /* which side of the pipe is this */
35 static void pipe_dump( struct object *obj, int verbose );
36 static int pipe_add_queue( struct object *obj, struct wait_queue_entry *entry );
37 static void pipe_remove_queue( struct object *obj, struct wait_queue_entry *entry );
38 static int pipe_signaled( struct object *obj, struct thread *thread );
39 static int pipe_get_read_fd( struct object *obj );
40 static int pipe_get_write_fd( struct object *obj );
41 static int pipe_get_info( struct object *obj, struct get_file_info_reply *reply );
42 static void pipe_destroy( struct object *obj );
44 static const struct object_ops pipe_ops =
46 pipe_dump,
47 pipe_add_queue,
48 pipe_remove_queue,
49 pipe_signaled,
50 no_satisfied,
51 pipe_get_read_fd,
52 pipe_get_write_fd,
53 no_flush,
54 pipe_get_info,
55 pipe_destroy
58 static const struct select_ops select_ops =
60 default_select_event,
61 NULL /* we never set a timeout on a pipe */
64 static int create_pipe( struct object *obj[2] )
66 struct pipe *newpipe[2];
67 int fd[2];
69 if (pipe( fd ) == -1)
71 file_set_error();
72 return 0;
74 if (!(newpipe[0] = mem_alloc( sizeof(struct pipe) )))
76 close( fd[0] );
77 close( fd[1] );
78 return 0;
80 if (!(newpipe[1] = mem_alloc( sizeof(struct pipe) )))
82 close( fd[0] );
83 close( fd[1] );
84 free( newpipe[0] );
85 return 0;
87 init_object( &newpipe[0]->obj, &pipe_ops, NULL );
88 init_object( &newpipe[1]->obj, &pipe_ops, NULL );
89 newpipe[0]->fd = fd[0];
90 newpipe[0]->other = newpipe[1];
91 newpipe[0]->side = READ_SIDE;
92 newpipe[1]->fd = fd[1];
93 newpipe[1]->other = newpipe[0];
94 newpipe[1]->side = WRITE_SIDE;
95 obj[0] = &newpipe[0]->obj;
96 obj[1] = &newpipe[1]->obj;
97 CLEAR_ERROR();
98 return 1;
101 static void pipe_dump( struct object *obj, int verbose )
103 struct pipe *pipe = (struct pipe *)obj;
104 assert( obj->ops == &pipe_ops );
105 fprintf( stderr, "Pipe %s-side fd=%d\n",
106 (pipe->side == READ_SIDE) ? "read" : "write", pipe->fd );
109 static int pipe_add_queue( struct object *obj, struct wait_queue_entry *entry )
111 struct pipe *pipe = (struct pipe *)obj;
112 assert( obj->ops == &pipe_ops );
113 if (!obj->head) /* first on the queue */
115 if (!add_select_user( pipe->fd,
116 (pipe->side == READ_SIDE) ? READ_EVENT : WRITE_EVENT,
117 &select_ops, pipe ))
119 SET_ERROR( ERROR_OUTOFMEMORY );
120 return 0;
123 add_queue( obj, entry );
124 return 1;
127 static void pipe_remove_queue( struct object *obj, struct wait_queue_entry *entry )
129 struct pipe *pipe = (struct pipe *)grab_object(obj);
130 assert( obj->ops == &pipe_ops );
132 remove_queue( obj, entry );
133 if (!obj->head) /* last on the queue is gone */
134 remove_select_user( pipe->fd );
135 release_object( obj );
138 static int pipe_signaled( struct object *obj, struct thread *thread )
140 struct pipe *pipe = (struct pipe *)obj;
141 struct timeval tv = { 0, 0 };
142 fd_set fds;
144 assert( obj->ops == &pipe_ops );
145 FD_ZERO( &fds );
146 FD_SET( pipe->fd, &fds );
147 if (pipe->side == READ_SIDE)
148 return select( pipe->fd + 1, &fds, NULL, NULL, &tv ) > 0;
149 else
150 return select( pipe->fd + 1, NULL, &fds, NULL, &tv ) > 0;
153 static int pipe_get_read_fd( struct object *obj )
155 struct pipe *pipe = (struct pipe *)obj;
156 assert( obj->ops == &pipe_ops );
158 if (!pipe->other)
160 SET_ERROR( ERROR_BROKEN_PIPE );
161 return -1;
163 if (pipe->side != READ_SIDE) /* FIXME: should not be necessary */
165 SET_ERROR( ERROR_ACCESS_DENIED );
166 return -1;
168 return dup( pipe->fd );
171 static int pipe_get_write_fd( struct object *obj )
173 struct pipe *pipe = (struct pipe *)obj;
174 assert( obj->ops == &pipe_ops );
176 if (!pipe->other)
178 SET_ERROR( ERROR_BROKEN_PIPE );
179 return -1;
181 if (pipe->side != WRITE_SIDE) /* FIXME: should not be necessary */
183 SET_ERROR( ERROR_ACCESS_DENIED );
184 return -1;
186 return dup( pipe->fd );
189 static int pipe_get_info( struct object *obj, struct get_file_info_reply *reply )
191 memset( reply, 0, sizeof(*reply) );
192 reply->type = FILE_TYPE_PIPE;
193 return 1;
196 static void pipe_destroy( struct object *obj )
198 struct pipe *pipe = (struct pipe *)obj;
199 assert( obj->ops == &pipe_ops );
201 if (pipe->other) pipe->other->other = NULL;
202 close( pipe->fd );
203 free( pipe );
206 /* create an anonymous pipe */
207 DECL_HANDLER(create_pipe)
209 struct create_pipe_reply reply = { -1, -1 };
210 struct object *obj[2];
211 if (create_pipe( obj ))
213 reply.handle_read = alloc_handle( current->process, obj[0],
214 STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|GENERIC_READ,
215 req->inherit );
216 if (reply.handle_read != -1)
218 reply.handle_write = alloc_handle( current->process, obj[1],
219 STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|GENERIC_WRITE,
220 req->inherit );
221 if (reply.handle_write == -1)
222 close_handle( current->process, reply.handle_read );
224 release_object( obj[0] );
225 release_object( obj[1] );
227 send_reply( current, -1, 1, &reply, sizeof(reply) );