Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side socket communication functions |
| 3 | * |
| 4 | * Copyright (C) 1998 Alexandre Julliard |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <errno.h> |
Alexandre Julliard | f2616a2 | 1999-05-20 16:40:23 +0000 | [diff] [blame] | 9 | #include <fcntl.h> |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <stdarg.h> |
| 13 | #include <string.h> |
| 14 | #include <sys/time.h> |
| 15 | #include <sys/types.h> |
| 16 | #include <sys/socket.h> |
| 17 | #include <sys/uio.h> |
| 18 | #include <unistd.h> |
| 19 | |
Alexandre Julliard | 829fe32 | 1998-07-26 14:27:39 +0000 | [diff] [blame] | 20 | #include "config.h" |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 21 | #include "server.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 22 | #include "object.h" |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 23 | |
Alexandre Julliard | 829fe32 | 1998-07-26 14:27:39 +0000 | [diff] [blame] | 24 | /* Some versions of glibc don't define this */ |
| 25 | #ifndef SCM_RIGHTS |
| 26 | #define SCM_RIGHTS 1 |
| 27 | #endif |
| 28 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 29 | /* client state */ |
| 30 | enum state |
| 31 | { |
| 32 | RUNNING, /* running normally */ |
| 33 | SENDING, /* sending us a request */ |
| 34 | WAITING, /* waiting for us to reply */ |
| 35 | READING /* reading our reply */ |
| 36 | }; |
| 37 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 38 | /* client structure */ |
| 39 | struct client |
| 40 | { |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 41 | enum state state; /* client state */ |
| 42 | struct select_user select; /* select user */ |
| 43 | unsigned int seq; /* current sequence number */ |
| 44 | struct header head; /* current msg header */ |
| 45 | char *data; /* current msg data */ |
| 46 | int count; /* bytes sent/received so far */ |
| 47 | int pass_fd; /* fd to pass to and from the client */ |
| 48 | struct thread *self; /* client thread (opaque pointer) */ |
| 49 | struct timeout_user *timeout; /* current timeout (opaque pointer) */ |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 52 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 53 | /* exit code passed to remove_client */ |
| 54 | #define OUT_OF_MEMORY -1 |
| 55 | #define BROKEN_PIPE -2 |
| 56 | #define PROTOCOL_ERROR -3 |
| 57 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 58 | |
| 59 | /* signal a client protocol error */ |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 60 | static void protocol_error( struct client *client, const char *err, ... ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 61 | { |
| 62 | va_list args; |
| 63 | |
| 64 | va_start( args, err ); |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 65 | fprintf( stderr, "Protocol error:%d: ", client->select.fd ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 66 | vfprintf( stderr, err, args ); |
| 67 | va_end( args ); |
| 68 | } |
| 69 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 70 | /* send a message to a client that is ready to receive something */ |
Alexandre Julliard | c6e45ed | 1998-12-27 08:35:39 +0000 | [diff] [blame] | 71 | static void do_write( struct client *client, int client_fd ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 72 | { |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 73 | struct iovec vec[2]; |
Alexandre Julliard | 829fe32 | 1998-07-26 14:27:39 +0000 | [diff] [blame] | 74 | #ifndef HAVE_MSGHDR_ACCRIGHTS |
Patrik Stridvall | 1bb9403 | 1999-05-08 15:47:44 +0000 | [diff] [blame] | 75 | struct cmsg_fd cmsg; |
Alexandre Julliard | 829fe32 | 1998-07-26 14:27:39 +0000 | [diff] [blame] | 76 | #endif |
Patrik Stridvall | 1bb9403 | 1999-05-08 15:47:44 +0000 | [diff] [blame] | 77 | struct msghdr msghdr; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 78 | int ret; |
| 79 | |
| 80 | /* make sure we have something to send */ |
| 81 | assert( client->count < client->head.len ); |
| 82 | /* make sure the client is listening */ |
| 83 | assert( client->state == READING ); |
| 84 | |
Patrik Stridvall | 1bb9403 | 1999-05-08 15:47:44 +0000 | [diff] [blame] | 85 | msghdr.msg_name = NULL; |
| 86 | msghdr.msg_namelen = 0; |
| 87 | msghdr.msg_iov = vec; |
| 88 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 89 | if (client->count < sizeof(client->head)) |
| 90 | { |
| 91 | vec[0].iov_base = (char *)&client->head + client->count; |
| 92 | vec[0].iov_len = sizeof(client->head) - client->count; |
| 93 | vec[1].iov_base = client->data; |
| 94 | vec[1].iov_len = client->head.len - sizeof(client->head); |
Patrik Stridvall | 1bb9403 | 1999-05-08 15:47:44 +0000 | [diff] [blame] | 95 | msghdr.msg_iovlen = 2; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 96 | } |
| 97 | else |
| 98 | { |
| 99 | vec[0].iov_base = client->data + client->count - sizeof(client->head); |
| 100 | vec[0].iov_len = client->head.len - client->count; |
| 101 | msghdr.msg_iovlen = 1; |
| 102 | } |
Patrik Stridvall | 1bb9403 | 1999-05-08 15:47:44 +0000 | [diff] [blame] | 103 | |
| 104 | #ifdef HAVE_MSGHDR_ACCRIGHTS |
Alexandre Julliard | 829fe32 | 1998-07-26 14:27:39 +0000 | [diff] [blame] | 105 | if (client->pass_fd != -1) /* we have an fd to send */ |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 106 | { |
Alexandre Julliard | 829fe32 | 1998-07-26 14:27:39 +0000 | [diff] [blame] | 107 | msghdr.msg_accrights = (void *)&client->pass_fd; |
| 108 | msghdr.msg_accrightslen = sizeof(client->pass_fd); |
Patrik Stridvall | 1bb9403 | 1999-05-08 15:47:44 +0000 | [diff] [blame] | 109 | } |
| 110 | else |
| 111 | { |
| 112 | msghdr.msg_accrights = NULL; |
| 113 | msghdr.msg_accrightslen = 0; |
| 114 | } |
| 115 | #else /* HAVE_MSGHDR_ACCRIGHTS */ |
| 116 | if (client->pass_fd != -1) /* we have an fd to send */ |
| 117 | { |
| 118 | cmsg.len = sizeof(cmsg); |
| 119 | cmsg.level = SOL_SOCKET; |
| 120 | cmsg.type = SCM_RIGHTS; |
| 121 | cmsg.fd = client->pass_fd; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 122 | msghdr.msg_control = &cmsg; |
| 123 | msghdr.msg_controllen = sizeof(cmsg); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 124 | } |
Patrik Stridvall | 1bb9403 | 1999-05-08 15:47:44 +0000 | [diff] [blame] | 125 | else |
| 126 | { |
| 127 | msghdr.msg_control = NULL; |
| 128 | msghdr.msg_controllen = 0; |
| 129 | } |
| 130 | msghdr.msg_flags = 0; |
| 131 | #endif /* HAVE_MSGHDR_ACCRIGHTS */ |
| 132 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 133 | ret = sendmsg( client_fd, &msghdr, 0 ); |
| 134 | if (ret == -1) |
| 135 | { |
| 136 | if (errno != EPIPE) perror("sendmsg"); |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 137 | remove_client( client, BROKEN_PIPE ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 138 | return; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 139 | } |
| 140 | if (client->pass_fd != -1) /* We sent the fd, now we can close it */ |
| 141 | { |
| 142 | close( client->pass_fd ); |
| 143 | client->pass_fd = -1; |
| 144 | } |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 145 | if ((client->count += ret) < client->head.len) return; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 146 | |
| 147 | /* we have finished with this message */ |
| 148 | if (client->data) free( client->data ); |
| 149 | client->data = NULL; |
| 150 | client->count = 0; |
| 151 | client->state = RUNNING; |
| 152 | client->seq++; |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 153 | set_select_events( &client->select, READ_EVENT ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
| 157 | /* read a message from a client that has something to say */ |
Alexandre Julliard | c6e45ed | 1998-12-27 08:35:39 +0000 | [diff] [blame] | 158 | static void do_read( struct client *client, int client_fd ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 159 | { |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 160 | struct iovec vec; |
Alexandre Julliard | 829fe32 | 1998-07-26 14:27:39 +0000 | [diff] [blame] | 161 | int pass_fd = -1; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 162 | int ret; |
| 163 | |
Patrik Stridvall | 1bb9403 | 1999-05-08 15:47:44 +0000 | [diff] [blame] | 164 | #ifdef HAVE_MSGHDR_ACCRIGHTS |
| 165 | struct msghdr msghdr; |
| 166 | |
| 167 | msghdr.msg_accrights = (void *)&pass_fd; |
| 168 | msghdr.msg_accrightslen = sizeof(int); |
| 169 | #else /* HAVE_MSGHDR_ACCRIGHTS */ |
| 170 | struct msghdr msghdr; |
| 171 | struct cmsg_fd cmsg; |
| 172 | |
| 173 | cmsg.len = sizeof(cmsg); |
| 174 | cmsg.level = SOL_SOCKET; |
| 175 | cmsg.type = SCM_RIGHTS; |
| 176 | cmsg.fd = -1; |
| 177 | msghdr.msg_control = &cmsg; |
| 178 | msghdr.msg_controllen = sizeof(cmsg); |
| 179 | msghdr.msg_flags = 0; |
| 180 | #endif /* HAVE_MSGHDR_ACCRIGHTS */ |
| 181 | |
| 182 | msghdr.msg_name = NULL; |
| 183 | msghdr.msg_namelen = 0; |
| 184 | msghdr.msg_iov = &vec; |
| 185 | msghdr.msg_iovlen = 1; |
| 186 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 187 | if (client->count < sizeof(client->head)) |
| 188 | { |
| 189 | vec.iov_base = (char *)&client->head + client->count; |
| 190 | vec.iov_len = sizeof(client->head) - client->count; |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | if (!client->data && |
| 195 | !(client->data = malloc(client->head.len-sizeof(client->head)))) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 196 | { |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 197 | remove_client( client, OUT_OF_MEMORY ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 198 | return; |
| 199 | } |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 200 | vec.iov_base = client->data + client->count - sizeof(client->head); |
| 201 | vec.iov_len = client->head.len - client->count; |
| 202 | } |
| 203 | |
| 204 | ret = recvmsg( client_fd, &msghdr, 0 ); |
| 205 | if (ret == -1) |
| 206 | { |
| 207 | perror("recvmsg"); |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 208 | remove_client( client, BROKEN_PIPE ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 209 | return; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 210 | } |
Alexandre Julliard | 829fe32 | 1998-07-26 14:27:39 +0000 | [diff] [blame] | 211 | #ifndef HAVE_MSGHDR_ACCRIGHTS |
| 212 | pass_fd = cmsg.fd; |
| 213 | #endif |
| 214 | if (pass_fd != -1) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 215 | { |
| 216 | /* can only receive one fd per message */ |
| 217 | if (client->pass_fd != -1) close( client->pass_fd ); |
Alexandre Julliard | 829fe32 | 1998-07-26 14:27:39 +0000 | [diff] [blame] | 218 | client->pass_fd = pass_fd; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 219 | } |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 220 | else if (!ret) /* closed pipe */ |
| 221 | { |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 222 | remove_client( client, BROKEN_PIPE ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 223 | return; |
| 224 | } |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 225 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 226 | if (client->state == RUNNING) client->state = SENDING; |
| 227 | assert( client->state == SENDING ); |
| 228 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 229 | client->count += ret; |
| 230 | |
| 231 | /* received the complete header yet? */ |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 232 | if (client->count < sizeof(client->head)) return; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 233 | |
| 234 | /* sanity checks */ |
| 235 | if (client->head.seq != client->seq) |
| 236 | { |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 237 | protocol_error( client, "bad sequence %08x instead of %08x\n", |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 238 | client->head.seq, client->seq ); |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 239 | remove_client( client, PROTOCOL_ERROR ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 240 | return; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 241 | } |
| 242 | if ((client->head.len < sizeof(client->head)) || |
| 243 | (client->head.len > MAX_MSG_LENGTH + sizeof(client->head))) |
| 244 | { |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 245 | protocol_error( client, "bad header length %08x\n", |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 246 | client->head.len ); |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 247 | remove_client( client, PROTOCOL_ERROR ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 248 | return; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /* received the whole message? */ |
| 252 | if (client->count == client->head.len) |
| 253 | { |
| 254 | /* done reading the data, call the callback function */ |
| 255 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 256 | int len = client->head.len - sizeof(client->head); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 257 | char *data = client->data; |
| 258 | int passed_fd = client->pass_fd; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 259 | enum request type = client->head.type; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 260 | |
| 261 | /* clear the info now, as the client may be deleted by the callback */ |
| 262 | client->head.len = 0; |
| 263 | client->head.type = 0; |
| 264 | client->count = 0; |
| 265 | client->data = NULL; |
| 266 | client->pass_fd = -1; |
| 267 | client->state = WAITING; |
| 268 | client->seq++; |
| 269 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 270 | call_req_handler( client->self, type, data, len, passed_fd ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 271 | if (passed_fd != -1) close( passed_fd ); |
| 272 | if (data) free( data ); |
| 273 | } |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Alexandre Julliard | c6e45ed | 1998-12-27 08:35:39 +0000 | [diff] [blame] | 276 | /* handle a client event */ |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 277 | static void client_event( int event, void *private ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 278 | { |
Alexandre Julliard | c6e45ed | 1998-12-27 08:35:39 +0000 | [diff] [blame] | 279 | struct client *client = (struct client *)private; |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 280 | if (event & WRITE_EVENT) do_write( client, client->select.fd ); |
| 281 | if (event & READ_EVENT) do_read( client, client->select.fd ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 284 | |
| 285 | /*******************************************************************/ |
| 286 | /* server-side exported functions */ |
| 287 | |
| 288 | /* add a client */ |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 289 | struct client *add_client( int fd, struct thread *self ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 290 | { |
Alexandre Julliard | f2616a2 | 1999-05-20 16:40:23 +0000 | [diff] [blame] | 291 | int flags; |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 292 | struct client *client = mem_alloc( sizeof(*client) ); |
| 293 | if (!client) return NULL; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 294 | |
Alexandre Julliard | f2616a2 | 1999-05-20 16:40:23 +0000 | [diff] [blame] | 295 | flags = fcntl( fd, F_GETFL, 0 ); |
| 296 | fcntl( fd, F_SETFL, flags | O_NONBLOCK ); |
| 297 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 298 | client->state = RUNNING; |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 299 | client->select.fd = fd; |
| 300 | client->select.func = client_event; |
| 301 | client->select.private = client; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 302 | client->seq = 0; |
| 303 | client->head.len = 0; |
| 304 | client->head.type = 0; |
| 305 | client->count = 0; |
| 306 | client->data = NULL; |
| 307 | client->self = self; |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 308 | client->timeout = NULL; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 309 | client->pass_fd = -1; |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 310 | register_select_user( &client->select ); |
| 311 | set_select_events( &client->select, READ_EVENT ); |
| 312 | return client; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /* remove a client */ |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 316 | void remove_client( struct client *client, int exit_code ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 317 | { |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 318 | assert( client ); |
| 319 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 320 | call_kill_handler( client->self, exit_code ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 321 | |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 322 | if (client->timeout) remove_timeout_user( client->timeout ); |
| 323 | unregister_select_user( &client->select ); |
| 324 | close( client->select.fd ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 325 | |
| 326 | /* Purge messages */ |
| 327 | if (client->data) free( client->data ); |
| 328 | if (client->pass_fd != -1) close( client->pass_fd ); |
| 329 | free( client ); |
| 330 | } |
| 331 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 332 | /* send a reply to a client */ |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 333 | int send_reply_v( struct client *client, int type, int pass_fd, |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 334 | struct iovec *vec, int veclen ) |
| 335 | { |
| 336 | int i; |
| 337 | unsigned int len; |
| 338 | char *p; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 339 | |
| 340 | assert( client ); |
| 341 | assert( client->state == WAITING ); |
| 342 | assert( !client->data ); |
| 343 | |
| 344 | if (debug_level) trace_reply( client->self, type, pass_fd, vec, veclen ); |
| 345 | |
| 346 | for (i = len = 0; i < veclen; i++) len += vec[i].iov_len; |
| 347 | assert( len < MAX_MSG_LENGTH ); |
| 348 | |
| 349 | if (len && !(client->data = malloc( len ))) return -1; |
| 350 | client->count = 0; |
| 351 | client->head.len = len + sizeof(client->head); |
| 352 | client->head.type = type; |
| 353 | client->head.seq = client->seq; |
| 354 | client->pass_fd = pass_fd; |
| 355 | |
| 356 | for (i = 0, p = client->data; i < veclen; i++) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 357 | { |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 358 | memcpy( p, vec[i].iov_base, vec[i].iov_len ); |
| 359 | p += vec[i].iov_len; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 360 | } |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 361 | |
| 362 | client->state = READING; |
Alexandre Julliard | 88de35c | 1999-05-16 16:57:49 +0000 | [diff] [blame] | 363 | set_select_events( &client->select, WRITE_EVENT ); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 364 | return 0; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 365 | } |