Implemented waitable timers.
[wine/multimedia.git] / server / socket.c
blob57411105a4768ff060762787e685dc0df1588df3
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 "object.h"
25 #include "request.h"
27 /* Some versions of glibc don't define this */
28 #ifndef SCM_RIGHTS
29 #define SCM_RIGHTS 1
30 #endif
32 /* client structure */
33 struct client
35 struct select_user select; /* select user */
36 unsigned int res; /* current result to send */
37 int pass_fd; /* fd to pass to and from the client */
38 struct thread *self; /* client thread (opaque pointer) */
39 struct timeout_user *timeout; /* current timeout (opaque pointer) */
43 /* socket communication static structures */
44 static struct iovec myiovec;
45 static struct msghdr msghdr = { NULL, 0, &myiovec, 1, };
46 #ifndef HAVE_MSGHDR_ACCRIGHTS
47 struct cmsg_fd
49 int len; /* sizeof structure */
50 int level; /* SOL_SOCKET */
51 int type; /* SCM_RIGHTS */
52 int fd; /* fd to pass */
54 static struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
55 #endif /* HAVE_MSGHDR_ACCRIGHTS */
58 /* send a message to a client that is ready to receive something */
59 static int do_write( struct client *client )
61 int ret;
63 if (client->pass_fd == -1)
65 ret = write( client->select.fd, &client->res, sizeof(client->res) );
66 if (ret == sizeof(client->res)) goto ok;
68 else /* we have an fd to send */
70 #ifdef HAVE_MSGHDR_ACCRIGHTS
71 msghdr.msg_accrightslen = sizeof(int);
72 msghdr.msg_accrights = (void *)&client->pass_fd;
73 #else /* HAVE_MSGHDR_ACCRIGHTS */
74 msghdr.msg_control = &cmsg;
75 msghdr.msg_controllen = sizeof(cmsg);
76 cmsg.fd = client->pass_fd;
77 #endif /* HAVE_MSGHDR_ACCRIGHTS */
79 myiovec.iov_base = (void *)&client->res;
80 myiovec.iov_len = sizeof(client->res);
82 ret = sendmsg( client->select.fd, &msghdr, 0 );
83 close( client->pass_fd );
84 client->pass_fd = -1;
85 if (ret == sizeof(client->res)) goto ok;
87 if (ret == -1)
89 if (errno == EWOULDBLOCK) return 0; /* not a fatal error */
90 if (errno != EPIPE) perror("sendmsg");
92 else fprintf( stderr, "Partial message sent %d/%d\n", ret, sizeof(client->res) );
93 remove_client( client, BROKEN_PIPE );
94 return 0;
96 ok:
97 set_select_events( &client->select, READ_EVENT );
98 return 1;
102 /* read a message from a client that has something to say */
103 static void do_read( struct client *client )
105 int ret;
106 enum request req;
108 #ifdef HAVE_MSGHDR_ACCRIGHTS
109 msghdr.msg_accrightslen = sizeof(int);
110 msghdr.msg_accrights = (void *)&client->pass_fd;
111 #else /* HAVE_MSGHDR_ACCRIGHTS */
112 msghdr.msg_control = &cmsg;
113 msghdr.msg_controllen = sizeof(cmsg);
114 cmsg.fd = -1;
115 #endif /* HAVE_MSGHDR_ACCRIGHTS */
117 assert( client->pass_fd == -1 );
119 myiovec.iov_base = (void *)&req;
120 myiovec.iov_len = sizeof(req);
122 ret = recvmsg( client->select.fd, &msghdr, 0 );
123 #ifndef HAVE_MSGHDR_ACCRIGHTS
124 client->pass_fd = cmsg.fd;
125 #endif
127 if (ret == sizeof(req))
129 int pass_fd = client->pass_fd;
130 client->pass_fd = -1;
131 call_req_handler( client->self, req, pass_fd );
132 if (pass_fd != -1) close( pass_fd );
133 return;
135 if (ret == -1)
137 perror("recvmsg");
138 remove_client( client, BROKEN_PIPE );
139 return;
141 if (!ret) /* closed pipe */
143 remove_client( client, BROKEN_PIPE );
144 return;
146 fatal_protocol_error( client->self, "partial message received %d/%d\n", ret, sizeof(req) );
149 /* handle a client event */
150 static void client_event( int event, void *private )
152 struct client *client = (struct client *)private;
153 if (event & WRITE_EVENT) do_write( client );
154 if (event & READ_EVENT) do_read( client );
157 /*******************************************************************/
158 /* server-side exported functions */
160 /* add a client */
161 struct client *add_client( int fd, struct thread *self )
163 int flags;
164 struct client *client = mem_alloc( sizeof(*client) );
165 if (!client) return NULL;
167 flags = fcntl( fd, F_GETFL, 0 );
168 fcntl( fd, F_SETFL, flags | O_NONBLOCK );
170 client->select.fd = fd;
171 client->select.func = client_event;
172 client->select.private = client;
173 client->self = self;
174 client->timeout = NULL;
175 client->pass_fd = -1;
176 register_select_user( &client->select );
177 set_select_events( &client->select, READ_EVENT );
178 return client;
181 /* remove a client */
182 void remove_client( struct client *client, int exit_code )
184 assert( client );
186 call_kill_handler( client->self, exit_code );
188 if (client->timeout) remove_timeout_user( client->timeout );
189 unregister_select_user( &client->select );
190 close( client->select.fd );
192 /* Purge messages */
193 if (client->pass_fd != -1) close( client->pass_fd );
194 free( client );
197 /* set the fd to pass to the client */
198 void client_pass_fd( struct client *client, int pass_fd )
200 assert( client->pass_fd == -1 );
201 client->pass_fd = pass_fd;
204 /* send a reply to a client */
205 void client_reply( struct client *client, unsigned int res )
207 if (debug_level) trace_reply( client->self, res, client->pass_fd );
208 client->res = res;
209 if (!do_write( client )) set_select_events( &client->select, WRITE_EVENT );