2 * Server-side socket communication functions
4 * Copyright (C) 1998 Alexandre Julliard
14 #include <sys/types.h>
15 #include <sys/socket.h>
23 /* Some versions of glibc don't define this */
31 RUNNING
, /* running normally */
32 SENDING
, /* sending us a request */
33 WAITING
, /* waiting for us to reply */
34 READING
/* reading our reply */
37 /* client structure */
40 enum state state
; /* client state */
41 struct select_user select
; /* select user */
42 unsigned int seq
; /* current sequence number */
43 struct header head
; /* current msg header */
44 char *data
; /* current msg data */
45 int count
; /* bytes sent/received so far */
46 int pass_fd
; /* fd to pass to and from the client */
47 struct thread
*self
; /* client thread (opaque pointer) */
48 struct timeout_user
*timeout
; /* current timeout (opaque pointer) */
52 /* exit code passed to remove_client */
53 #define OUT_OF_MEMORY -1
54 #define BROKEN_PIPE -2
55 #define PROTOCOL_ERROR -3
58 /* signal a client protocol error */
59 static void protocol_error( struct client
*client
, const char *err
, ... )
63 va_start( args
, err
);
64 fprintf( stderr
, "Protocol error:%d: ", client
->select
.fd
);
65 vfprintf( stderr
, err
, args
);
69 /* send a message to a client that is ready to receive something */
70 static void do_write( struct client
*client
, int client_fd
)
73 #ifndef HAVE_MSGHDR_ACCRIGHTS
79 /* make sure we have something to send */
80 assert( client
->count
< client
->head
.len
);
81 /* make sure the client is listening */
82 assert( client
->state
== READING
);
84 msghdr
.msg_name
= NULL
;
85 msghdr
.msg_namelen
= 0;
88 if (client
->count
< sizeof(client
->head
))
90 vec
[0].iov_base
= (char *)&client
->head
+ client
->count
;
91 vec
[0].iov_len
= sizeof(client
->head
) - client
->count
;
92 vec
[1].iov_base
= client
->data
;
93 vec
[1].iov_len
= client
->head
.len
- sizeof(client
->head
);
94 msghdr
.msg_iovlen
= 2;
98 vec
[0].iov_base
= client
->data
+ client
->count
- sizeof(client
->head
);
99 vec
[0].iov_len
= client
->head
.len
- client
->count
;
100 msghdr
.msg_iovlen
= 1;
103 #ifdef HAVE_MSGHDR_ACCRIGHTS
104 if (client
->pass_fd
!= -1) /* we have an fd to send */
106 msghdr
.msg_accrights
= (void *)&client
->pass_fd
;
107 msghdr
.msg_accrightslen
= sizeof(client
->pass_fd
);
111 msghdr
.msg_accrights
= NULL
;
112 msghdr
.msg_accrightslen
= 0;
114 #else /* HAVE_MSGHDR_ACCRIGHTS */
115 if (client
->pass_fd
!= -1) /* we have an fd to send */
117 cmsg
.len
= sizeof(cmsg
);
118 cmsg
.level
= SOL_SOCKET
;
119 cmsg
.type
= SCM_RIGHTS
;
120 cmsg
.fd
= client
->pass_fd
;
121 msghdr
.msg_control
= &cmsg
;
122 msghdr
.msg_controllen
= sizeof(cmsg
);
126 msghdr
.msg_control
= NULL
;
127 msghdr
.msg_controllen
= 0;
129 msghdr
.msg_flags
= 0;
130 #endif /* HAVE_MSGHDR_ACCRIGHTS */
132 ret
= sendmsg( client_fd
, &msghdr
, 0 );
135 if (errno
!= EPIPE
) perror("sendmsg");
136 remove_client( client
, BROKEN_PIPE
);
139 if (client
->pass_fd
!= -1) /* We sent the fd, now we can close it */
141 close( client
->pass_fd
);
142 client
->pass_fd
= -1;
144 if ((client
->count
+= ret
) < client
->head
.len
) return;
146 /* we have finished with this message */
147 if (client
->data
) free( client
->data
);
150 client
->state
= RUNNING
;
152 set_select_events( &client
->select
, READ_EVENT
);
156 /* read a message from a client that has something to say */
157 static void do_read( struct client
*client
, int client_fd
)
163 #ifdef HAVE_MSGHDR_ACCRIGHTS
164 struct msghdr msghdr
;
166 msghdr
.msg_accrights
= (void *)&pass_fd
;
167 msghdr
.msg_accrightslen
= sizeof(int);
168 #else /* HAVE_MSGHDR_ACCRIGHTS */
169 struct msghdr msghdr
;
172 cmsg
.len
= sizeof(cmsg
);
173 cmsg
.level
= SOL_SOCKET
;
174 cmsg
.type
= SCM_RIGHTS
;
176 msghdr
.msg_control
= &cmsg
;
177 msghdr
.msg_controllen
= sizeof(cmsg
);
178 msghdr
.msg_flags
= 0;
179 #endif /* HAVE_MSGHDR_ACCRIGHTS */
181 msghdr
.msg_name
= NULL
;
182 msghdr
.msg_namelen
= 0;
183 msghdr
.msg_iov
= &vec
;
184 msghdr
.msg_iovlen
= 1;
186 if (client
->count
< sizeof(client
->head
))
188 vec
.iov_base
= (char *)&client
->head
+ client
->count
;
189 vec
.iov_len
= sizeof(client
->head
) - client
->count
;
194 !(client
->data
= malloc(client
->head
.len
-sizeof(client
->head
))))
196 remove_client( client
, OUT_OF_MEMORY
);
199 vec
.iov_base
= client
->data
+ client
->count
- sizeof(client
->head
);
200 vec
.iov_len
= client
->head
.len
- client
->count
;
203 ret
= recvmsg( client_fd
, &msghdr
, 0 );
207 remove_client( client
, BROKEN_PIPE
);
210 #ifndef HAVE_MSGHDR_ACCRIGHTS
215 /* can only receive one fd per message */
216 if (client
->pass_fd
!= -1) close( client
->pass_fd
);
217 client
->pass_fd
= pass_fd
;
219 else if (!ret
) /* closed pipe */
221 remove_client( client
, BROKEN_PIPE
);
225 if (client
->state
== RUNNING
) client
->state
= SENDING
;
226 assert( client
->state
== SENDING
);
228 client
->count
+= ret
;
230 /* received the complete header yet? */
231 if (client
->count
< sizeof(client
->head
)) return;
234 if (client
->head
.seq
!= client
->seq
)
236 protocol_error( client
, "bad sequence %08x instead of %08x\n",
237 client
->head
.seq
, client
->seq
);
238 remove_client( client
, PROTOCOL_ERROR
);
241 if ((client
->head
.len
< sizeof(client
->head
)) ||
242 (client
->head
.len
> MAX_MSG_LENGTH
+ sizeof(client
->head
)))
244 protocol_error( client
, "bad header length %08x\n",
246 remove_client( client
, PROTOCOL_ERROR
);
250 /* received the whole message? */
251 if (client
->count
== client
->head
.len
)
253 /* done reading the data, call the callback function */
255 int len
= client
->head
.len
- sizeof(client
->head
);
256 char *data
= client
->data
;
257 int passed_fd
= client
->pass_fd
;
258 enum request type
= client
->head
.type
;
260 /* clear the info now, as the client may be deleted by the callback */
261 client
->head
.len
= 0;
262 client
->head
.type
= 0;
265 client
->pass_fd
= -1;
266 client
->state
= WAITING
;
269 call_req_handler( client
->self
, type
, data
, len
, passed_fd
);
270 if (passed_fd
!= -1) close( passed_fd
);
271 if (data
) free( data
);
275 /* handle a client event */
276 static void client_event( int event
, void *private )
278 struct client
*client
= (struct client
*)private;
279 if (event
& WRITE_EVENT
) do_write( client
, client
->select
.fd
);
280 if (event
& READ_EVENT
) do_read( client
, client
->select
.fd
);
284 /*******************************************************************/
285 /* server-side exported functions */
288 struct client
*add_client( int fd
, struct thread
*self
)
290 struct client
*client
= mem_alloc( sizeof(*client
) );
291 if (!client
) return NULL
;
293 client
->state
= RUNNING
;
294 client
->select
.fd
= fd
;
295 client
->select
.func
= client_event
;
296 client
->select
.private = client
;
298 client
->head
.len
= 0;
299 client
->head
.type
= 0;
303 client
->timeout
= NULL
;
304 client
->pass_fd
= -1;
305 register_select_user( &client
->select
);
306 set_select_events( &client
->select
, READ_EVENT
);
310 /* remove a client */
311 void remove_client( struct client
*client
, int exit_code
)
315 call_kill_handler( client
->self
, exit_code
);
317 if (client
->timeout
) remove_timeout_user( client
->timeout
);
318 unregister_select_user( &client
->select
);
319 close( client
->select
.fd
);
322 if (client
->data
) free( client
->data
);
323 if (client
->pass_fd
!= -1) close( client
->pass_fd
);
327 /* send a reply to a client */
328 int send_reply_v( struct client
*client
, int type
, int pass_fd
,
329 struct iovec
*vec
, int veclen
)
336 assert( client
->state
== WAITING
);
337 assert( !client
->data
);
339 if (debug_level
) trace_reply( client
->self
, type
, pass_fd
, vec
, veclen
);
341 for (i
= len
= 0; i
< veclen
; i
++) len
+= vec
[i
].iov_len
;
342 assert( len
< MAX_MSG_LENGTH
);
344 if (len
&& !(client
->data
= malloc( len
))) return -1;
346 client
->head
.len
= len
+ sizeof(client
->head
);
347 client
->head
.type
= type
;
348 client
->head
.seq
= client
->seq
;
349 client
->pass_fd
= pass_fd
;
351 for (i
= 0, p
= client
->data
; i
< veclen
; i
++)
353 memcpy( p
, vec
[i
].iov_base
, vec
[i
].iov_len
);
357 client
->state
= READING
;
358 set_select_events( &client
->select
, WRITE_EVENT
);