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>
27 /* Some versions of glibc don't define this */
32 /* client structure */
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
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
)
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
);
85 if (ret
== sizeof(client
->res
)) goto ok
;
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
);
97 set_select_events( &client
->select
, READ_EVENT
);
102 /* read a message from a client that has something to say */
103 static void do_read( struct client
*client
)
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
);
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
;
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
);
138 remove_client( client
, BROKEN_PIPE
);
141 if (!ret
) /* closed pipe */
143 remove_client( client
, BROKEN_PIPE
);
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 */
161 struct client
*add_client( int fd
, struct thread
*self
)
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
;
174 client
->timeout
= NULL
;
175 client
->pass_fd
= -1;
176 register_select_user( &client
->select
);
177 set_select_events( &client
->select
, READ_EVENT
);
181 /* remove a client */
182 void remove_client( struct client
*client
, int exit_code
)
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
);
193 if (client
->pass_fd
!= -1) close( client
->pass_fd
);
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
);
209 if (!do_write( client
)) set_select_events( &client
->select
, WRITE_EVENT
);