Changed the server to return STATUS_* error codes.
[wine] / server / request.c
1 /*
2  * Server-side request handling
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 "winnt.h"
25 #include "winbase.h"
26 #include "wincon.h"
27 #include "thread.h"
28 #include "server.h"
29 #define WANT_REQUEST_HANDLERS
30 #include "request.h"
31
32 /* Some versions of glibc don't define this */
33 #ifndef SCM_RIGHTS
34 #define SCM_RIGHTS 1
35 #endif
36
37  
38 struct thread *current = NULL;  /* thread handling the current request */
39
40
41 /* socket communication static structures */
42 static struct iovec myiovec;
43 static struct msghdr msghdr = { NULL, 0, &myiovec, 1, };
44 #ifndef HAVE_MSGHDR_ACCRIGHTS
45 struct cmsg_fd
46 {
47     int len;   /* sizeof structure */
48     int level; /* SOL_SOCKET */
49     int type;  /* SCM_RIGHTS */
50     int fd;    /* fd to pass */
51 };
52 static struct cmsg_fd cmsg = { sizeof(cmsg), SOL_SOCKET, SCM_RIGHTS, -1 };
53 #endif  /* HAVE_MSGHDR_ACCRIGHTS */
54
55 /* complain about a protocol error and terminate the client connection */
56 void fatal_protocol_error( struct thread *thread, const char *err, ... )
57 {
58     va_list args;
59
60     va_start( args, err );
61     fprintf( stderr, "Protocol error:%p: ", thread );
62     vfprintf( stderr, err, args );
63     va_end( args );
64     kill_thread( thread, PROTOCOL_ERROR );
65 }
66
67 /* call a request handler */
68 static void call_req_handler( struct thread *thread, enum request req, int fd )
69 {
70     current = thread;
71     clear_error();
72
73     if (debug_level) trace_request( req, fd );
74
75     if (req < REQ_NB_REQUESTS)
76     {
77         req_handlers[req].handler( current->buffer, fd );
78         if (current && !current->wait) send_reply( current );
79         current = NULL;
80         return;
81     }
82     fatal_protocol_error( current, "bad request %d\n", req );
83 }
84
85 /* set the fd to pass to the thread */
86 void set_reply_fd( struct thread *thread, int pass_fd )
87 {
88     assert( thread->pass_fd == -1 );
89     thread->pass_fd = pass_fd;
90 }
91
92 /* send a reply to a thread */
93 void send_reply( struct thread *thread )
94 {
95     assert( !thread->wait );
96     if (debug_level) trace_reply( thread );
97     if (!write_request( thread )) set_select_events( &thread->obj, POLLOUT );
98 }
99
100 /* read a message from a client that has something to say */
101 void read_request( struct thread *thread )
102 {
103     int ret;
104     enum request req;
105
106 #ifdef HAVE_MSGHDR_ACCRIGHTS
107     msghdr.msg_accrightslen = sizeof(int);
108     msghdr.msg_accrights = (void *)&thread->pass_fd;
109 #else  /* HAVE_MSGHDR_ACCRIGHTS */
110     msghdr.msg_control    = &cmsg;
111     msghdr.msg_controllen = sizeof(cmsg);
112     cmsg.fd = -1;
113 #endif  /* HAVE_MSGHDR_ACCRIGHTS */
114
115     assert( thread->pass_fd == -1 );
116
117     myiovec.iov_base = (void *)&req;
118     myiovec.iov_len  = sizeof(req);
119
120     ret = recvmsg( thread->obj.fd, &msghdr, 0 );
121 #ifndef HAVE_MSGHDR_ACCRIGHTS
122     thread->pass_fd = cmsg.fd;
123 #endif
124
125     if (ret == sizeof(req))
126     {
127         int pass_fd = thread->pass_fd;
128         thread->pass_fd = -1;
129         call_req_handler( thread, req, pass_fd );
130         if (pass_fd != -1) close( pass_fd );
131         return;
132     }
133     if (ret == -1)
134     {
135         perror("recvmsg");
136         kill_thread( thread, BROKEN_PIPE );
137         return;
138     }
139     if (!ret)  /* closed pipe */
140     {
141         kill_thread( thread, BROKEN_PIPE );
142         return;
143     }
144     fatal_protocol_error( thread, "partial message received %d/%d\n", ret, sizeof(req) );
145 }
146
147 /* send a message to a client that is ready to receive something */
148 int write_request( struct thread *thread )
149 {
150     int ret;
151
152     if (thread->pass_fd == -1)
153     {
154         ret = write( thread->obj.fd, &thread->error, sizeof(thread->error) );
155         if (ret == sizeof(thread->error)) goto ok;
156     }
157     else  /* we have an fd to send */
158     {
159 #ifdef HAVE_MSGHDR_ACCRIGHTS
160         msghdr.msg_accrightslen = sizeof(int);
161         msghdr.msg_accrights = (void *)&thread->pass_fd;
162 #else  /* HAVE_MSGHDR_ACCRIGHTS */
163         msghdr.msg_control    = &cmsg;
164         msghdr.msg_controllen = sizeof(cmsg);
165         cmsg.fd = thread->pass_fd;
166 #endif  /* HAVE_MSGHDR_ACCRIGHTS */
167
168         myiovec.iov_base = (void *)&thread->error;
169         myiovec.iov_len  = sizeof(thread->error);
170
171         ret = sendmsg( thread->obj.fd, &msghdr, 0 );
172         close( thread->pass_fd );
173         thread->pass_fd = -1;
174         if (ret == sizeof(thread->error)) goto ok;
175     }
176     if (ret == -1)
177     {
178         if (errno == EWOULDBLOCK) return 0;  /* not a fatal error */
179         if (errno != EPIPE) perror("sendmsg");
180     }
181     else fprintf( stderr, "Partial message sent %d/%d\n", ret, sizeof(thread->error) );
182     kill_thread( thread, BROKEN_PIPE );
183     return -1;
184
185  ok:
186     set_select_events( &thread->obj, POLLIN );
187     return 1;
188 }
189
190 /* set the debug level */
191 DECL_HANDLER(set_debug)
192 {
193     debug_level = req->level;
194     /* Make sure last_req is initialized */
195     current->last_req = REQ_SET_DEBUG;
196 }
197
198 /* debugger support operations */
199 DECL_HANDLER(debugger)
200 {
201     switch ( req->op )
202     {
203     case DEBUGGER_FREEZE_ALL:
204         suspend_all_threads();
205         break;
206
207     case DEBUGGER_UNFREEZE_ALL:
208         resume_all_threads();
209         break;
210     }
211 }