2 * Server-side socket communication functions
4 * Copyright (C) 1998 Alexandre Julliard
17 #include <sys/types.h>
18 #ifdef HAVE_SYS_SOCKET_H
19 # include <sys/socket.h>
28 /* Some versions of glibc don't define this */
33 /* client structure */
36 struct select_user select; /* select user */
37 unsigned int res; /* current result to send */
38 int pass_fd; /* fd to pass to and from the client */
39 struct thread *self; /* client thread (opaque pointer) */
40 struct timeout_user *timeout; /* current timeout (opaque pointer) */
44 /* socket communication static structures */
45 static struct iovec myiovec;
46 static struct msghdr msghdr = { NULL, 0, &myiovec, 1, };
47 #ifndef HAVE_MSGHDR_ACCRIGHTS
50 int len; /* sizeof structure */
51 int level; /* SOL_SOCKET */
52 int type; /* SCM_RIGHTS */
53 int fd; /* fd to pass */
55 static struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
56 #endif /* HAVE_MSGHDR_ACCRIGHTS */
59 /* send a message to a client that is ready to receive something */
60 static int do_write( struct client *client )
64 if (client->pass_fd == -1)
66 ret = write( client->select.fd, &client->res, sizeof(client->res) );
67 if (ret == sizeof(client->res)) goto ok;
69 else /* we have an fd to send */
71 #ifdef HAVE_MSGHDR_ACCRIGHTS
72 msghdr.msg_accrightslen = sizeof(int);
73 msghdr.msg_accrights = (void *)&client->pass_fd;
74 #else /* HAVE_MSGHDR_ACCRIGHTS */
75 msghdr.msg_control = &cmsg;
76 msghdr.msg_controllen = sizeof(cmsg);
77 cmsg.fd = client->pass_fd;
78 #endif /* HAVE_MSGHDR_ACCRIGHTS */
80 myiovec.iov_base = (void *)&client->res;
81 myiovec.iov_len = sizeof(client->res);
83 ret = sendmsg( client->select.fd, &msghdr, 0 );
84 close( client->pass_fd );
86 if (ret == sizeof(client->res)) goto ok;
90 if (errno == EWOULDBLOCK) return 0; /* not a fatal error */
91 if (errno != EPIPE) perror("sendmsg");
93 else fprintf( stderr, "Partial message sent %d/%d\n", ret, sizeof(client->res) );
94 remove_client( client, BROKEN_PIPE );
98 set_select_events( &client->select, READ_EVENT );
103 /* read a message from a client that has something to say */
104 static void do_read( struct client *client )
109 #ifdef HAVE_MSGHDR_ACCRIGHTS
110 msghdr.msg_accrightslen = sizeof(int);
111 msghdr.msg_accrights = (void *)&client->pass_fd;
112 #else /* HAVE_MSGHDR_ACCRIGHTS */
113 msghdr.msg_control = &cmsg;
114 msghdr.msg_controllen = sizeof(cmsg);
116 #endif /* HAVE_MSGHDR_ACCRIGHTS */
118 assert( client->pass_fd == -1 );
120 myiovec.iov_base = (void *)&req;
121 myiovec.iov_len = sizeof(req);
123 ret = recvmsg( client->select.fd, &msghdr, 0 );
124 #ifndef HAVE_MSGHDR_ACCRIGHTS
125 client->pass_fd = cmsg.fd;
128 if (ret == sizeof(req))
130 int pass_fd = client->pass_fd;
131 client->pass_fd = -1;
132 call_req_handler( client->self, req, pass_fd );
133 if (pass_fd != -1) close( pass_fd );
139 remove_client( client, BROKEN_PIPE );
142 if (!ret) /* closed pipe */
144 remove_client( client, BROKEN_PIPE );
147 fatal_protocol_error( client->self, "partial message received %d/%d\n", ret, sizeof(req) );
150 /* handle a client event */
151 static void client_event( int event, void *private )
153 struct client *client = (struct client *)private;
154 if (event & WRITE_EVENT) do_write( client );
155 if (event & READ_EVENT) do_read( client );
158 /*******************************************************************/
159 /* server-side exported functions */
162 struct client *add_client( int fd, struct thread *self )
165 struct client *client = mem_alloc( sizeof(*client) );
166 if (!client) return NULL;
168 flags = fcntl( fd, F_GETFL, 0 );
169 fcntl( fd, F_SETFL, flags | O_NONBLOCK );
171 client->select.fd = fd;
172 client->select.func = client_event;
173 client->select.private = client;
175 client->timeout = NULL;
176 client->pass_fd = -1;
177 register_select_user( &client->select );
178 set_select_events( &client->select, READ_EVENT );
182 /* remove a client */
183 void remove_client( struct client *client, int exit_code )
187 call_kill_handler( client->self, exit_code );
189 if (client->timeout) remove_timeout_user( client->timeout );
190 unregister_select_user( &client->select );
191 close( client->select.fd );
194 if (client->pass_fd != -1) close( client->pass_fd );
198 /* set the fd to pass to the client */
199 void client_pass_fd( struct client *client, int pass_fd )
201 assert( client->pass_fd == -1 );
202 client->pass_fd = pass_fd;
205 /* send a reply to a client */
206 void client_reply( struct client *client, unsigned int res )
208 if (debug_level) trace_reply( client->self, res, client->pass_fd );
210 if (!do_write( client )) set_select_events( &client->select, WRITE_EVENT );