Set close-on-exec flag on server socket.
[wine] / server / socket.c
1 /*
2  * Server-side socket communication functions
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  */
6
7 #include "config.h"
8
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>
23
24 #include "config.h"
25 #include "object.h"
26 #include "request.h"
27
28 /* Some versions of glibc don't define this */
29 #ifndef SCM_RIGHTS
30 #define SCM_RIGHTS 1
31 #endif
32
33 /* client structure */
34 struct client
35 {
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) */
41 };
42
43
44 /* socket communication static structures */
45 static struct iovec myiovec;
46 static struct msghdr msghdr = { NULL, 0, &myiovec, 1, };
47 #ifndef HAVE_MSGHDR_ACCRIGHTS
48 struct cmsg_fd
49 {
50     int len;   /* sizeof structure */
51     int level; /* SOL_SOCKET */
52     int type;  /* SCM_RIGHTS */
53     int fd;    /* fd to pass */
54 };
55 static struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
56 #endif  /* HAVE_MSGHDR_ACCRIGHTS */
57
58
59 /* send a message to a client that is ready to receive something */
60 static int do_write( struct client *client )
61 {
62     int ret;
63
64     if (client->pass_fd == -1)
65     {
66         ret = write( client->select.fd, &client->res, sizeof(client->res) );
67         if (ret == sizeof(client->res)) goto ok;
68     }
69     else  /* we have an fd to send */
70     {
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 */
79
80         myiovec.iov_base = (void *)&client->res;
81         myiovec.iov_len  = sizeof(client->res);
82
83         ret = sendmsg( client->select.fd, &msghdr, 0 );
84         close( client->pass_fd );
85         client->pass_fd = -1;
86         if (ret == sizeof(client->res)) goto ok;
87     }
88     if (ret == -1)
89     {
90         if (errno == EWOULDBLOCK) return 0;  /* not a fatal error */
91         if (errno != EPIPE) perror("sendmsg");
92     }
93     else fprintf( stderr, "Partial message sent %d/%d\n", ret, sizeof(client->res) );
94     remove_client( client, BROKEN_PIPE );
95     return 0;
96
97  ok:
98     set_select_events( &client->select, READ_EVENT );
99     return 1;
100 }
101
102
103 /* read a message from a client that has something to say */
104 static void do_read( struct client *client )
105 {
106     int ret;
107     enum request req;
108
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);
115     cmsg.fd = -1;
116 #endif  /* HAVE_MSGHDR_ACCRIGHTS */
117
118     assert( client->pass_fd == -1 );
119
120     myiovec.iov_base = (void *)&req;
121     myiovec.iov_len  = sizeof(req);
122
123     ret = recvmsg( client->select.fd, &msghdr, 0 );
124 #ifndef HAVE_MSGHDR_ACCRIGHTS
125     client->pass_fd = cmsg.fd;
126 #endif
127
128     if (ret == sizeof(req))
129     {
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 );
134         return;
135     }
136     if (ret == -1)
137     {
138         perror("recvmsg");
139         remove_client( client, BROKEN_PIPE );
140         return;
141     }
142     if (!ret)  /* closed pipe */
143     {
144         remove_client( client, BROKEN_PIPE );
145         return;
146     }
147     fatal_protocol_error( client->self, "partial message received %d/%d\n", ret, sizeof(req) );
148 }
149
150 /* handle a client event */
151 static void client_event( int event, void *private )
152 {
153     struct client *client = (struct client *)private;
154     if (event & WRITE_EVENT) do_write( client );
155     if (event & READ_EVENT) do_read( client );
156 }
157
158 /*******************************************************************/
159 /* server-side exported functions                                  */
160
161 /* add a client */
162 struct client *add_client( int fd, struct thread *self )
163 {
164     int flags;
165     struct client *client = mem_alloc( sizeof(*client) );
166     if (!client) return NULL;
167
168     flags = fcntl( fd, F_GETFL, 0 );
169     fcntl( fd, F_SETFL, flags | O_NONBLOCK );
170
171     client->select.fd            = fd;
172     client->select.func          = client_event;
173     client->select.private       = client;
174     client->self                 = self;
175     client->timeout              = NULL;
176     client->pass_fd              = -1;
177     register_select_user( &client->select );
178     set_select_events( &client->select, READ_EVENT );
179     return client;
180 }
181
182 /* remove a client */
183 void remove_client( struct client *client, int exit_code )
184 {
185     assert( client );
186
187     call_kill_handler( client->self, exit_code );
188
189     if (client->timeout) remove_timeout_user( client->timeout );
190     unregister_select_user( &client->select );
191     close( client->select.fd );
192
193     /* Purge messages */
194     if (client->pass_fd != -1) close( client->pass_fd );
195     free( client );
196 }
197
198 /* set the fd to pass to the client */
199 void client_pass_fd( struct client *client, int pass_fd )
200 {
201     assert( client->pass_fd == -1 );
202     client->pass_fd = pass_fd;
203 }
204
205 /* send a reply to a client */
206 void client_reply( struct client *client, unsigned int res )
207 {
208     if (debug_level) trace_reply( client->self, res, client->pass_fd );
209     client->res = res;
210     if (!do_write( client )) set_select_events( &client->select, WRITE_EVENT );
211 }