Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side pipe management |
| 3 | * |
| 4 | * Copyright (C) 1998 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Patrik Stridvall | 9633632 | 1999-10-24 22:13:47 +0000 | [diff] [blame] | 21 | #include "config.h" |
| 22 | |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 23 | #include <assert.h> |
| 24 | #include <fcntl.h> |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 25 | #include <string.h> |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 28 | #ifdef HAVE_SYS_ERRNO_H |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 29 | #include <sys/errno.h> |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 30 | #endif |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 31 | #include <sys/time.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <time.h> |
| 34 | #include <unistd.h> |
| 35 | |
Michael Veksler | f935c59 | 1999-02-09 15:49:39 +0000 | [diff] [blame] | 36 | #include "winbase.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 37 | |
| 38 | #include "handle.h" |
| 39 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 40 | #include "request.h" |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 41 | |
| 42 | enum side { READ_SIDE, WRITE_SIDE }; |
| 43 | |
| 44 | struct pipe |
| 45 | { |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 46 | struct object obj; /* object header */ |
| 47 | struct pipe *other; /* the pipe other end */ |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 48 | enum side side; /* which side of the pipe is this */ |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | static void pipe_dump( struct object *obj, int verbose ); |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 52 | static int pipe_get_poll_events( struct object *obj ); |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 53 | static int pipe_get_fd( struct object *obj ); |
Martin Wilck | 88cd32b | 2002-01-09 20:30:51 +0000 | [diff] [blame] | 54 | static int pipe_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags ); |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 55 | static void pipe_destroy( struct object *obj ); |
| 56 | |
| 57 | static const struct object_ops pipe_ops = |
| 58 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 59 | sizeof(struct pipe), /* size */ |
| 60 | pipe_dump, /* dump */ |
| 61 | default_poll_add_queue, /* add_queue */ |
| 62 | default_poll_remove_queue, /* remove_queue */ |
| 63 | default_poll_signaled, /* signaled */ |
| 64 | no_satisfied, /* satisfied */ |
| 65 | pipe_get_poll_events, /* get_poll_events */ |
| 66 | default_poll_event, /* poll_event */ |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 67 | pipe_get_fd, /* get_fd */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 68 | no_flush, /* flush */ |
| 69 | pipe_get_info, /* get_file_info */ |
Mike McCormack | 6f011c0 | 2001-12-20 00:07:05 +0000 | [diff] [blame] | 70 | NULL, /* queue_async */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 71 | pipe_destroy /* destroy */ |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 74 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 75 | static struct pipe *create_pipe_side( int fd, int side ) |
| 76 | { |
| 77 | struct pipe *pipe; |
| 78 | |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 79 | if ((pipe = alloc_object( &pipe_ops, fd ))) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 80 | { |
Alexandre Julliard | 247b8ae | 1999-12-13 00:16:44 +0000 | [diff] [blame] | 81 | pipe->other = NULL; |
| 82 | pipe->side = side; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 83 | } |
| 84 | return pipe; |
| 85 | } |
| 86 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 87 | static int create_pipe( struct object *obj[2] ) |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 88 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 89 | struct pipe *read_pipe; |
| 90 | struct pipe *write_pipe; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 91 | int fd[2]; |
| 92 | |
| 93 | if (pipe( fd ) == -1) |
| 94 | { |
| 95 | file_set_error(); |
| 96 | return 0; |
| 97 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 98 | if ((read_pipe = create_pipe_side( fd[0], READ_SIDE ))) |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 99 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 100 | if ((write_pipe = create_pipe_side( fd[1], WRITE_SIDE ))) |
| 101 | { |
| 102 | write_pipe->other = read_pipe; |
| 103 | read_pipe->other = write_pipe; |
| 104 | obj[0] = &read_pipe->obj; |
| 105 | obj[1] = &write_pipe->obj; |
| 106 | return 1; |
| 107 | } |
| 108 | release_object( read_pipe ); |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 109 | } |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 110 | else close( fd[1] ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 111 | return 0; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static void pipe_dump( struct object *obj, int verbose ) |
| 115 | { |
| 116 | struct pipe *pipe = (struct pipe *)obj; |
| 117 | assert( obj->ops == &pipe_ops ); |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 118 | fprintf( stderr, "Pipe %s-side fd=%d\n", |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 119 | (pipe->side == READ_SIDE) ? "read" : "write", pipe->obj.fd ); |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 122 | static int pipe_get_poll_events( struct object *obj ) |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 123 | { |
| 124 | struct pipe *pipe = (struct pipe *)obj; |
| 125 | assert( obj->ops == &pipe_ops ); |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 126 | return (pipe->side == READ_SIDE) ? POLLIN : POLLOUT; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 129 | static int pipe_get_fd( struct object *obj ) |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 130 | { |
| 131 | struct pipe *pipe = (struct pipe *)obj; |
| 132 | assert( obj->ops == &pipe_ops ); |
| 133 | |
| 134 | if (!pipe->other) |
| 135 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 136 | set_error( STATUS_PIPE_BROKEN ); |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 137 | return -1; |
| 138 | } |
Alexandre Julliard | 63411db | 2000-12-22 21:12:36 +0000 | [diff] [blame] | 139 | return pipe->obj.fd; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Martin Wilck | 88cd32b | 2002-01-09 20:30:51 +0000 | [diff] [blame] | 142 | static int pipe_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags ) |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 143 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 144 | if (reply) |
Mike McCormack | ff58be5 | 2001-10-04 16:18:15 +0000 | [diff] [blame] | 145 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 146 | reply->type = FILE_TYPE_PIPE; |
| 147 | reply->attr = 0; |
| 148 | reply->access_time = 0; |
| 149 | reply->write_time = 0; |
| 150 | reply->size_high = 0; |
| 151 | reply->size_low = 0; |
| 152 | reply->links = 0; |
| 153 | reply->index_high = 0; |
| 154 | reply->index_low = 0; |
| 155 | reply->serial = 0; |
Mike McCormack | ff58be5 | 2001-10-04 16:18:15 +0000 | [diff] [blame] | 156 | } |
Martin Wilck | 88cd32b | 2002-01-09 20:30:51 +0000 | [diff] [blame] | 157 | *flags = 0; |
Mike McCormack | ff58be5 | 2001-10-04 16:18:15 +0000 | [diff] [blame] | 158 | return FD_TYPE_DEFAULT; |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 161 | static void pipe_destroy( struct object *obj ) |
| 162 | { |
| 163 | struct pipe *pipe = (struct pipe *)obj; |
| 164 | assert( obj->ops == &pipe_ops ); |
| 165 | |
| 166 | if (pipe->other) pipe->other->other = NULL; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 167 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 168 | |
| 169 | /* create an anonymous pipe */ |
| 170 | DECL_HANDLER(create_pipe) |
| 171 | { |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 172 | struct object *obj[2]; |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 173 | obj_handle_t hread = 0, hwrite = 0; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 174 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 175 | if (create_pipe( obj )) |
| 176 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 177 | hread = alloc_handle( current->process, obj[0], |
| 178 | STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|GENERIC_READ, |
| 179 | req->inherit ); |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 180 | if (hread) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 181 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 182 | hwrite = alloc_handle( current->process, obj[1], |
| 183 | STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|GENERIC_WRITE, |
| 184 | req->inherit ); |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 185 | if (!hwrite) close_handle( current->process, hread, NULL ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 186 | } |
| 187 | release_object( obj[0] ); |
| 188 | release_object( obj[1] ); |
| 189 | } |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 190 | reply->handle_read = hread; |
| 191 | reply->handle_write = hwrite; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 192 | } |