Renamed static global variable 'iovec' to 'myiovec'.
[wine] / server / socket.c
1 /*
2  * Server-side socket communication functions
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  */
6
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>
19
20 #include "config.h"
21 #include "object.h"
22 #include "request.h"
23
24 /* Some versions of glibc don't define this */
25 #ifndef SCM_RIGHTS
26 #define SCM_RIGHTS 1
27 #endif
28
29 /* client structure */
30 struct client
31 {
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) */
37 };
38
39
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
45 {
46     int len;   /* sizeof structure */
47     int level; /* SOL_SOCKET */
48     int type;  /* SCM_RIGHTS */
49     int fd;    /* fd to pass */
50 };
51 static struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
52 #endif  /* HAVE_MSGHDR_ACCRIGHTS */
53
54
55 /* send a message to a client that is ready to receive something */
56 static int do_write( struct client *client )
57 {
58     int ret;
59
60     if (client->pass_fd == -1)
61     {
62         ret = write( client->select.fd, &client->res, sizeof(client->res) );
63         if (ret == sizeof(client->res)) goto ok;
64     }
65     else  /* we have an fd to send */
66     {
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 */
75
76         myiovec.iov_base = (void *)&client->res;
77         myiovec.iov_len  = sizeof(client->res);
78
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;
83     }
84     if (ret == -1)
85     {
86         if (errno == EWOULDBLOCK) return 0;  /* not a fatal error */
87         if (errno != EPIPE) perror("sendmsg");
88     }
89     else fprintf( stderr, "Partial message sent %d/%d\n", ret, sizeof(client->res) );
90     remove_client( client, BROKEN_PIPE );
91     return 0;
92
93  ok:
94     set_select_events( &client->select, READ_EVENT );
95     return 1;
96 }
97
98
99 /* read a message from a client that has something to say */
100 static void do_read( struct client *client )
101 {
102     int ret;
103     enum request req;
104
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 */
113
114     assert( client->pass_fd == -1 );
115
116     myiovec.iov_base = (void *)&req;
117     myiovec.iov_len  = sizeof(req);
118
119     ret = recvmsg( client->select.fd, &msghdr, 0 );
120 #ifndef HAVE_MSGHDR_ACCRIGHTS
121     client->pass_fd = cmsg.fd;
122 #endif
123
124     if (ret == sizeof(req))
125     {
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;
131     }
132     if (ret == -1)
133     {
134         perror("recvmsg");
135         remove_client( client, BROKEN_PIPE );
136         return;
137     }
138     if (!ret)  /* closed pipe */
139     {
140         remove_client( client, BROKEN_PIPE );
141         return;
142     }
143     fatal_protocol_error( client->self, "partial message received %d/%d\n", ret, sizeof(req) );
144 }
145
146 /* handle a client event */
147 static void client_event( int event, void *private )
148 {
149     struct client *client = (struct client *)private;
150     if (event & WRITE_EVENT) do_write( client );
151     if (event & READ_EVENT) do_read( client );
152 }
153
154 /*******************************************************************/
155 /* server-side exported functions                                  */
156
157 /* add a client */
158 struct client *add_client( int fd, struct thread *self )
159 {
160     int flags;
161     struct client *client = mem_alloc( sizeof(*client) );
162     if (!client) return NULL;
163
164     flags = fcntl( fd, F_GETFL, 0 );
165     fcntl( fd, F_SETFL, flags | O_NONBLOCK );
166
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;
176 }
177
178 /* remove a client */
179 void remove_client( struct client *client, int exit_code )
180 {
181     assert( client );
182
183     call_kill_handler( client->self, exit_code );
184
185     if (client->timeout) remove_timeout_user( client->timeout );
186     unregister_select_user( &client->select );
187     close( client->select.fd );
188
189     /* Purge messages */
190     if (client->pass_fd != -1) close( client->pass_fd );
191     free( client );
192 }
193
194 /* set the fd to pass to the client */
195 void client_pass_fd( struct client *client, int pass_fd )
196 {
197     assert( client->pass_fd == -1 );
198     client->pass_fd = pass_fd;
199 }
200
201 /* send a reply to a client */
202 void client_reply( struct client *client, unsigned int res )
203 {
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 );
207 }