Added one safety check to AFM parsing.
[wine.git] / server / pipe.c
blob47dfb61a1a2a254c439beb55baa98c53e6d72122
1 /*
2 * Server-side pipe management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <fcntl.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #ifdef HAVE_SYS_ERRNO_H
15 #include <sys/errno.h>
16 #endif
17 #include <sys/stat.h>
18 #include <sys/time.h>
19 #include <sys/types.h>
20 #include <time.h>
21 #include <unistd.h>
23 #include "winbase.h"
25 #include "handle.h"
26 #include "thread.h"
27 #include "request.h"
29 enum side { READ_SIDE, WRITE_SIDE };
31 struct pipe
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_fd( struct object *obj );
41 static int pipe_get_info( struct object *obj, struct get_file_info_request *req );
42 static void pipe_destroy( struct object *obj );
44 static const struct object_ops pipe_ops =
46 sizeof(struct pipe), /* size */
47 pipe_dump, /* dump */
48 default_poll_add_queue, /* add_queue */
49 default_poll_remove_queue, /* remove_queue */
50 default_poll_signaled, /* signaled */
51 no_satisfied, /* satisfied */
52 pipe_get_poll_events, /* get_poll_events */
53 default_poll_event, /* poll_event */
54 pipe_get_fd, /* get_fd */
55 no_flush, /* flush */
56 pipe_get_info, /* get_file_info */
57 pipe_destroy /* destroy */
61 static struct pipe *create_pipe_side( int fd, int side )
63 struct pipe *pipe;
65 if ((pipe = alloc_object( &pipe_ops, fd )))
67 pipe->other = NULL;
68 pipe->side = side;
70 return pipe;
73 static int create_pipe( struct object *obj[2] )
75 struct pipe *read_pipe;
76 struct pipe *write_pipe;
77 int fd[2];
79 if (pipe( fd ) == -1)
81 file_set_error();
82 return 0;
84 if ((read_pipe = create_pipe_side( fd[0], READ_SIDE )))
86 if ((write_pipe = create_pipe_side( fd[1], WRITE_SIDE )))
88 write_pipe->other = read_pipe;
89 read_pipe->other = write_pipe;
90 obj[0] = &read_pipe->obj;
91 obj[1] = &write_pipe->obj;
92 return 1;
94 release_object( read_pipe );
96 else close( fd[1] );
97 return 0;
100 static void pipe_dump( struct object *obj, int verbose )
102 struct pipe *pipe = (struct pipe *)obj;
103 assert( obj->ops == &pipe_ops );
104 fprintf( stderr, "Pipe %s-side fd=%d\n",
105 (pipe->side == READ_SIDE) ? "read" : "write", pipe->obj.fd );
108 static int pipe_get_poll_events( struct object *obj )
110 struct pipe *pipe = (struct pipe *)obj;
111 assert( obj->ops == &pipe_ops );
112 return (pipe->side == READ_SIDE) ? POLLIN : POLLOUT;
115 static int pipe_get_fd( struct object *obj )
117 struct pipe *pipe = (struct pipe *)obj;
118 assert( obj->ops == &pipe_ops );
120 if (!pipe->other)
122 set_error( STATUS_PIPE_BROKEN );
123 return -1;
125 return pipe->obj.fd;
128 static int pipe_get_info( struct object *obj, struct get_file_info_request *req )
130 req->type = FILE_TYPE_PIPE;
131 req->attr = 0;
132 req->access_time = 0;
133 req->write_time = 0;
134 req->size_high = 0;
135 req->size_low = 0;
136 req->links = 0;
137 req->index_high = 0;
138 req->index_low = 0;
139 req->serial = 0;
140 return 1;
143 static void pipe_destroy( struct object *obj )
145 struct pipe *pipe = (struct pipe *)obj;
146 assert( obj->ops == &pipe_ops );
148 if (pipe->other) pipe->other->other = NULL;
151 /* create an anonymous pipe */
152 DECL_HANDLER(create_pipe)
154 struct object *obj[2];
155 int hread = -1, hwrite = -1;
157 if (create_pipe( obj ))
159 hread = alloc_handle( current->process, obj[0],
160 STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|GENERIC_READ,
161 req->inherit );
162 if (hread != -1)
164 hwrite = alloc_handle( current->process, obj[1],
165 STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|GENERIC_WRITE,
166 req->inherit );
167 if (hwrite == -1)
168 close_handle( current->process, hread, NULL );
170 release_object( obj[0] );
171 release_object( obj[1] );
173 req->handle_read = hread;
174 req->handle_write = hwrite;