Bugfix: Declare CALL32_CBClient[Ex] without WINAPI.
[wine.git] / server / socket.c
blobabf1a1021dba2ef3a52edc00802067a9a072657d
1 /*
2 * Server-side socket communication functions
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <errno.h>
9 #include <fcntl.h>
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>
20 #include "config.h"
21 #include "object.h"
22 #include "request.h"
24 /* Some versions of glibc don't define this */
25 #ifndef SCM_RIGHTS
26 #define SCM_RIGHTS 1
27 #endif
29 /* client structure */
30 struct client
32 struct select_user select; /* select user */
33 unsigned int res; /* current result to send */
34 int pass_fd; /* fd to pass to and from the client */
35 struct thread *self; /* client thread (opaque pointer) */
36 struct timeout_user *timeout; /* current timeout (opaque pointer) */
40 /* socket communication static structures */
41 static struct iovec myiovec;
42 static struct msghdr msghdr = { NULL, 0, &myiovec, 1, };
43 #ifndef HAVE_MSGHDR_ACCRIGHTS
44 struct cmsg_fd
46 int len; /* sizeof structure */
47 int level; /* SOL_SOCKET */
48 int type; /* SCM_RIGHTS */
49 int fd; /* fd to pass */
51 static struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
52 #endif /* HAVE_MSGHDR_ACCRIGHTS */
55 /* send a message to a client that is ready to receive something */
56 static int do_write( struct client *client )
58 int ret;
60 if (client->pass_fd == -1)
62 ret = write( client->select.fd, &client->res, sizeof(client->res) );
63 if (ret == sizeof(client->res)) goto ok;
65 else /* we have an fd to send */
67 #ifdef HAVE_MSGHDR_ACCRIGHTS
68 msghdr.msg_accrightslen = sizeof(int);
69 msghdr.msg_accrights = (void *)&client->pass_fd;
70 #else /* HAVE_MSGHDR_ACCRIGHTS */
71 msghdr.msg_control = &cmsg;
72 msghdr.msg_controllen = sizeof(cmsg);
73 cmsg.fd = client->pass_fd;
74 #endif /* HAVE_MSGHDR_ACCRIGHTS */
76 myiovec.iov_base = (void *)&client->res;
77 myiovec.iov_len = sizeof(client->res);
79 ret = sendmsg( client->select.fd, &msghdr, 0 );
80 close( client->pass_fd );
81 client->pass_fd = -1;
82 if (ret == sizeof(client->res)) goto ok;
84 if (ret == -1)
86 if (errno == EWOULDBLOCK) return 0; /* not a fatal error */
87 if (errno != EPIPE) perror("sendmsg");
89 else fprintf( stderr, "Partial message sent %d/%d\n", ret, sizeof(client->res) );
90 remove_client( client, BROKEN_PIPE );
91 return 0;
93 ok:
94 set_select_events( &client->select, READ_EVENT );
95 return 1;
99 /* read a message from a client that has something to say */
100 static void do_read( struct client *client )
102 int ret;
103 enum request req;
105 #ifdef HAVE_MSGHDR_ACCRIGHTS
106 msghdr.msg_accrightslen = sizeof(int);
107 msghdr.msg_accrights = (void *)&client->pass_fd;
108 #else /* HAVE_MSGHDR_ACCRIGHTS */
109 msghdr.msg_control = &cmsg;
110 msghdr.msg_controllen = sizeof(cmsg);
111 cmsg.fd = -1;
112 #endif /* HAVE_MSGHDR_ACCRIGHTS */
114 assert( client->pass_fd == -1 );
116 myiovec.iov_base = (void *)&req;
117 myiovec.iov_len = sizeof(req);
119 ret = recvmsg( client->select.fd, &msghdr, 0 );
120 #ifndef HAVE_MSGHDR_ACCRIGHTS
121 client->pass_fd = cmsg.fd;
122 #endif
124 if (ret == sizeof(req))
126 int pass_fd = client->pass_fd;
127 client->pass_fd = -1;
128 call_req_handler( client->self, req, pass_fd );
129 if (pass_fd != -1) close( pass_fd );
130 return;
132 if (ret == -1)
134 perror("recvmsg");
135 remove_client( client, BROKEN_PIPE );
136 return;
138 if (!ret) /* closed pipe */
140 remove_client( client, BROKEN_PIPE );
141 return;
143 fatal_protocol_error( client->self, "partial message received %d/%d\n", ret, sizeof(req) );
146 /* handle a client event */
147 static void client_event( int event, void *private )
149 struct client *client = (struct client *)private;
150 if (event & WRITE_EVENT) do_write( client );
151 if (event & READ_EVENT) do_read( client );
154 /*******************************************************************/
155 /* server-side exported functions */
157 /* add a client */
158 struct client *add_client( int fd, struct thread *self )
160 int flags;
161 struct client *client = mem_alloc( sizeof(*client) );
162 if (!client) return NULL;
164 flags = fcntl( fd, F_GETFL, 0 );
165 fcntl( fd, F_SETFL, flags | O_NONBLOCK );
167 client->select.fd = fd;
168 client->select.func = client_event;
169 client->select.private = client;
170 client->self = self;
171 client->timeout = NULL;
172 client->pass_fd = -1;
173 register_select_user( &client->select );
174 set_select_events( &client->select, READ_EVENT );
175 return client;
178 /* remove a client */
179 void remove_client( struct client *client, int exit_code )
181 assert( client );
183 call_kill_handler( client->self, exit_code );
185 if (client->timeout) remove_timeout_user( client->timeout );
186 unregister_select_user( &client->select );
187 close( client->select.fd );
189 /* Purge messages */
190 if (client->pass_fd != -1) close( client->pass_fd );
191 free( client );
194 /* set the fd to pass to the client */
195 void client_pass_fd( struct client *client, int pass_fd )
197 assert( client->pass_fd == -1 );
198 client->pass_fd = pass_fd;
201 /* send a reply to a client */
202 void client_reply( struct client *client, unsigned int res )
204 if (debug_level) trace_reply( client->self, res, client->pass_fd );
205 client->res = res;
206 if (!do_write( client )) set_select_events( &client->select, WRITE_EVENT );