2 * Server-side socket communication functions
4 * Copyright (C) 1998 Alexandre Julliard
15 #include <sys/types.h>
16 #include <sys/socket.h>
24 /* Some versions of glibc don't define this */
29 /* client structure */
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
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 )
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 );
82 if (ret == sizeof(client->res)) goto ok;
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 );
94 set_select_events( &client->select, READ_EVENT );
99 /* read a message from a client that has something to say */
100 static void do_read( struct client *client )
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);
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;
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 );
135 remove_client( client, BROKEN_PIPE );
138 if (!ret) /* closed pipe */
140 remove_client( client, BROKEN_PIPE );
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 */
158 struct client *add_client( int fd, struct thread *self )
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;
171 client->timeout = NULL;
172 client->pass_fd = -1;
173 register_select_user( &client->select );
174 set_select_events( &client->select, READ_EVENT );
178 /* remove a client */
179 void remove_client( struct client *client, int exit_code )
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 );
190 if (client->pass_fd != -1) close( client->pass_fd );
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 );
206 if (!do_write( client )) set_select_events( &client->select, WRITE_EVENT );