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 |
| 5 | */ |
| 6 | |
Patrik Stridvall | 9633632 | 1999-10-24 22:13:47 +0000 | [diff] [blame] | 7 | #include "config.h" |
| 8 | |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 9 | #include <assert.h> |
| 10 | #include <fcntl.h> |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 11 | #include <string.h> |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 14 | #ifdef HAVE_SYS_ERRNO_H |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 15 | #include <sys/errno.h> |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 16 | #endif |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 17 | #include <sys/time.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <time.h> |
| 20 | #include <unistd.h> |
| 21 | |
Michael Veksler | f935c59 | 1999-02-09 15:49:39 +0000 | [diff] [blame] | 22 | #include "winbase.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 23 | |
| 24 | #include "handle.h" |
| 25 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 26 | #include "request.h" |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 27 | |
| 28 | enum side { READ_SIDE, WRITE_SIDE }; |
| 29 | |
| 30 | struct pipe |
| 31 | { |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 32 | struct object obj; /* object header */ |
| 33 | struct pipe *other; /* the pipe other end */ |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 34 | enum side side; /* which side of the pipe is this */ |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | static void pipe_dump( struct object *obj, int verbose ); |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 38 | static int pipe_get_poll_events( struct object *obj ); |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 39 | static int pipe_get_fd( struct object *obj ); |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 40 | static int pipe_get_info( struct object *obj, struct get_file_info_reply *reply ); |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 41 | static void pipe_destroy( struct object *obj ); |
| 42 | |
| 43 | static const struct object_ops pipe_ops = |
| 44 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 45 | sizeof(struct pipe), /* size */ |
| 46 | pipe_dump, /* dump */ |
| 47 | default_poll_add_queue, /* add_queue */ |
| 48 | default_poll_remove_queue, /* remove_queue */ |
| 49 | default_poll_signaled, /* signaled */ |
| 50 | no_satisfied, /* satisfied */ |
| 51 | pipe_get_poll_events, /* get_poll_events */ |
| 52 | default_poll_event, /* poll_event */ |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 53 | pipe_get_fd, /* get_fd */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 54 | no_flush, /* flush */ |
| 55 | pipe_get_info, /* get_file_info */ |
| 56 | pipe_destroy /* destroy */ |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 59 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 60 | static struct pipe *create_pipe_side( int fd, int side ) |
| 61 | { |
| 62 | struct pipe *pipe; |
| 63 | |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 64 | if ((pipe = alloc_object( &pipe_ops, fd ))) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 65 | { |
Alexandre Julliard | 247b8ae | 1999-12-13 00:16:44 +0000 | [diff] [blame] | 66 | pipe->other = NULL; |
| 67 | pipe->side = side; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 68 | } |
| 69 | return pipe; |
| 70 | } |
| 71 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 72 | static int create_pipe( struct object *obj[2] ) |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 73 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 74 | struct pipe *read_pipe; |
| 75 | struct pipe *write_pipe; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 76 | int fd[2]; |
| 77 | |
| 78 | if (pipe( fd ) == -1) |
| 79 | { |
| 80 | file_set_error(); |
| 81 | return 0; |
| 82 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 83 | if ((read_pipe = create_pipe_side( fd[0], READ_SIDE ))) |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 84 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 85 | if ((write_pipe = create_pipe_side( fd[1], WRITE_SIDE ))) |
| 86 | { |
| 87 | write_pipe->other = read_pipe; |
| 88 | read_pipe->other = write_pipe; |
| 89 | obj[0] = &read_pipe->obj; |
| 90 | obj[1] = &write_pipe->obj; |
| 91 | return 1; |
| 92 | } |
| 93 | release_object( read_pipe ); |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 94 | } |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 95 | else close( fd[1] ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 96 | return 0; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static void pipe_dump( struct object *obj, int verbose ) |
| 100 | { |
| 101 | struct pipe *pipe = (struct pipe *)obj; |
| 102 | assert( obj->ops == &pipe_ops ); |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 103 | fprintf( stderr, "Pipe %s-side fd=%d\n", |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 104 | (pipe->side == READ_SIDE) ? "read" : "write", pipe->obj.fd ); |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 107 | static int pipe_get_poll_events( struct object *obj ) |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 108 | { |
| 109 | struct pipe *pipe = (struct pipe *)obj; |
| 110 | assert( obj->ops == &pipe_ops ); |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 111 | return (pipe->side == READ_SIDE) ? POLLIN : POLLOUT; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 114 | static int pipe_get_fd( struct object *obj ) |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 115 | { |
| 116 | struct pipe *pipe = (struct pipe *)obj; |
| 117 | assert( obj->ops == &pipe_ops ); |
| 118 | |
| 119 | if (!pipe->other) |
| 120 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 121 | set_error( STATUS_PIPE_BROKEN ); |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 122 | return -1; |
| 123 | } |
Alexandre Julliard | 63411db | 2000-12-22 21:12:36 +0000 | [diff] [blame] | 124 | return pipe->obj.fd; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 127 | static int pipe_get_info( struct object *obj, struct get_file_info_reply *reply ) |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 128 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 129 | if (reply) |
Mike McCormack | ff58be5 | 2001-10-04 16:18:15 +0000 | [diff] [blame] | 130 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 131 | reply->type = FILE_TYPE_PIPE; |
| 132 | reply->attr = 0; |
| 133 | reply->access_time = 0; |
| 134 | reply->write_time = 0; |
| 135 | reply->size_high = 0; |
| 136 | reply->size_low = 0; |
| 137 | reply->links = 0; |
| 138 | reply->index_high = 0; |
| 139 | reply->index_low = 0; |
| 140 | reply->serial = 0; |
Mike McCormack | ff58be5 | 2001-10-04 16:18:15 +0000 | [diff] [blame] | 141 | } |
| 142 | return FD_TYPE_DEFAULT; |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 145 | static void pipe_destroy( struct object *obj ) |
| 146 | { |
| 147 | struct pipe *pipe = (struct pipe *)obj; |
| 148 | assert( obj->ops == &pipe_ops ); |
| 149 | |
| 150 | if (pipe->other) pipe->other->other = NULL; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 151 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 152 | |
| 153 | /* create an anonymous pipe */ |
| 154 | DECL_HANDLER(create_pipe) |
| 155 | { |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 156 | struct object *obj[2]; |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 157 | handle_t hread = 0, hwrite = 0; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 158 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 159 | if (create_pipe( obj )) |
| 160 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 161 | hread = alloc_handle( current->process, obj[0], |
| 162 | STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|GENERIC_READ, |
| 163 | req->inherit ); |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 164 | if (hread) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 165 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 166 | hwrite = alloc_handle( current->process, obj[1], |
| 167 | STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|GENERIC_WRITE, |
| 168 | req->inherit ); |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 169 | if (!hwrite) close_handle( current->process, hread, NULL ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 170 | } |
| 171 | release_object( obj[0] ); |
| 172 | release_object( obj[1] ); |
| 173 | } |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 174 | reply->handle_read = hread; |
| 175 | reply->handle_write = hwrite; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 176 | } |