Fixed a crash in IDirectDrawSurface:BltFast.
[wine.git] / server / socket.c
blobc540aac042c08301f8c348d16728eaa01dc85582
1 /*
2 * Server-side socket communication functions
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <string.h>
16 #include <sys/time.h>
17 #include <sys/types.h>
18 #ifdef HAVE_SYS_SOCKET_H
19 # include <sys/socket.h>
20 #endif
21 #include <sys/uio.h>
22 #include <unistd.h>
24 #include "config.h"
25 #include "object.h"
26 #include "request.h"
28 /* Some versions of glibc don't define this */
29 #ifndef SCM_RIGHTS
30 #define SCM_RIGHTS 1
31 #endif
33 /* client structure */
34 struct client
36 struct select_user select; /* select user */
37 unsigned int res; /* current result to send */
38 int pass_fd; /* fd to pass to and from the client */
39 struct thread *self; /* client thread (opaque pointer) */
40 struct timeout_user *timeout; /* current timeout (opaque pointer) */
44 /* socket communication static structures */
45 static struct iovec myiovec;
46 static struct msghdr msghdr = { NULL, 0, &myiovec, 1, };
47 #ifndef HAVE_MSGHDR_ACCRIGHTS
48 struct cmsg_fd
50 int len; /* sizeof structure */
51 int level; /* SOL_SOCKET */
52 int type; /* SCM_RIGHTS */
53 int fd; /* fd to pass */
55 static struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
56 #endif /* HAVE_MSGHDR_ACCRIGHTS */
59 /* send a message to a client that is ready to receive something */
60 static int do_write( struct client *client )
62 int ret;
64 if (client->pass_fd == -1)
66 ret = write( client->select.fd, &client->res, sizeof(client->res) );
67 if (ret == sizeof(client->res)) goto ok;
69 else /* we have an fd to send */
71 #ifdef HAVE_MSGHDR_ACCRIGHTS
72 msghdr.msg_accrightslen = sizeof(int);
73 msghdr.msg_accrights = (void *)&client->pass_fd;
74 #else /* HAVE_MSGHDR_ACCRIGHTS */
75 msghdr.msg_control = &cmsg;
76 msghdr.msg_controllen = sizeof(cmsg);
77 cmsg.fd = client->pass_fd;
78 #endif /* HAVE_MSGHDR_ACCRIGHTS */
80 myiovec.iov_base = (void *)&client->res;
81 myiovec.iov_len = sizeof(client->res);
83 ret = sendmsg( client->select.fd, &msghdr, 0 );
84 close( client->pass_fd );
85 client->pass_fd = -1;
86 if (ret == sizeof(client->res)) goto ok;
88 if (ret == -1)
90 if (errno == EWOULDBLOCK) return 0; /* not a fatal error */
91 if (errno != EPIPE) perror("sendmsg");
93 else fprintf( stderr, "Partial message sent %d/%d\n", ret, sizeof(client->res) );
94 remove_client( client, BROKEN_PIPE );
95 return 0;
97 ok:
98 set_select_events( &client->select, READ_EVENT );
99 return 1;
103 /* read a message from a client that has something to say */
104 static void do_read( struct client *client )
106 int ret;
107 enum request req;
109 #ifdef HAVE_MSGHDR_ACCRIGHTS
110 msghdr.msg_accrightslen = sizeof(int);
111 msghdr.msg_accrights = (void *)&client->pass_fd;
112 #else /* HAVE_MSGHDR_ACCRIGHTS */
113 msghdr.msg_control = &cmsg;
114 msghdr.msg_controllen = sizeof(cmsg);
115 cmsg.fd = -1;
116 #endif /* HAVE_MSGHDR_ACCRIGHTS */
118 assert( client->pass_fd == -1 );
120 myiovec.iov_base = (void *)&req;
121 myiovec.iov_len = sizeof(req);
123 ret = recvmsg( client->select.fd, &msghdr, 0 );
124 #ifndef HAVE_MSGHDR_ACCRIGHTS
125 client->pass_fd = cmsg.fd;
126 #endif
128 if (ret == sizeof(req))
130 int pass_fd = client->pass_fd;
131 client->pass_fd = -1;
132 call_req_handler( client->self, req, pass_fd );
133 if (pass_fd != -1) close( pass_fd );
134 return;
136 if (ret == -1)
138 perror("recvmsg");
139 remove_client( client, BROKEN_PIPE );
140 return;
142 if (!ret) /* closed pipe */
144 remove_client( client, BROKEN_PIPE );
145 return;
147 fatal_protocol_error( client->self, "partial message received %d/%d\n", ret, sizeof(req) );
150 /* handle a client event */
151 static void client_event( int event, void *private )
153 struct client *client = (struct client *)private;
154 if (event & WRITE_EVENT) do_write( client );
155 if (event & READ_EVENT) do_read( client );
158 /*******************************************************************/
159 /* server-side exported functions */
161 /* add a client */
162 struct client *add_client( int fd, struct thread *self )
164 int flags;
165 struct client *client = mem_alloc( sizeof(*client) );
166 if (!client) return NULL;
168 flags = fcntl( fd, F_GETFL, 0 );
169 fcntl( fd, F_SETFL, flags | O_NONBLOCK );
171 client->select.fd = fd;
172 client->select.func = client_event;
173 client->select.private = client;
174 client->self = self;
175 client->timeout = NULL;
176 client->pass_fd = -1;
177 register_select_user( &client->select );
178 set_select_events( &client->select, READ_EVENT );
179 return client;
182 /* remove a client */
183 void remove_client( struct client *client, int exit_code )
185 assert( client );
187 call_kill_handler( client->self, exit_code );
189 if (client->timeout) remove_timeout_user( client->timeout );
190 unregister_select_user( &client->select );
191 close( client->select.fd );
193 /* Purge messages */
194 if (client->pass_fd != -1) close( client->pass_fd );
195 free( client );
198 /* set the fd to pass to the client */
199 void client_pass_fd( struct client *client, int pass_fd )
201 assert( client->pass_fd == -1 );
202 client->pass_fd = pass_fd;
205 /* send a reply to a client */
206 void client_reply( struct client *client, unsigned int res )
208 if (debug_level) trace_reply( client->self, res, client->pass_fd );
209 client->res = res;
210 if (!do_write( client )) set_select_events( &client->select, WRITE_EVENT );