msiexec: Invert the return values of the boolean functions so that
[wine/multimedia.git] / programs / rpcss / np_server.c
blob47fa348aaf0ce3b21c1bbb60302a4570bb81798e
1 /*
2 * RPCSS named pipe server
4 * Copyright (C) 2002 Greg Turner
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <assert.h>
23 #include "rpcss.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(ole);
28 static HANDLE np_server_end;
29 static HANDLE np_server_work_event;
30 static CRITICAL_SECTION np_server_cs;
31 static LONG srv_thread_count;
32 static BOOL server_live;
34 LONG RPCSS_SrvThreadCount(void)
36 return srv_thread_count;
39 BOOL RPCSS_UnBecomePipeServer(void)
41 BOOL rslt = TRUE;
42 DWORD wait_result;
43 HANDLE master_mutex = RPCSS_GetMasterMutex();
45 WINE_TRACE("\n");
47 wait_result = WaitForSingleObject(master_mutex, MASTER_MUTEX_TIMEOUT);
49 switch (wait_result) {
50 case WAIT_ABANDONED: /* ? */
51 case WAIT_OBJECT_0:
52 /* we have ownership */
53 break;
54 case WAIT_FAILED:
55 case WAIT_TIMEOUT:
56 default:
57 WINE_ERR("This should never happen: couldn't enter mutex.\n");
58 /* this is totally unacceptable. no graceful out exists */
59 assert(FALSE);
62 /* now that we have the master mutex, we can safely stop
63 listening on the pipe. Before we proceed, we do a final
64 check that it's OK to shut down to ensure atomicity */
66 if (!RPCSS_ReadyToDie())
67 rslt = FALSE;
68 else {
69 WINE_TRACE("shutting down pipe.\n");
70 server_live = FALSE;
71 if (!CloseHandle(np_server_end))
72 WINE_WARN("Failed to close named pipe.\n");
73 if (!CloseHandle(np_server_work_event))
74 WINE_WARN("Failed to close the event handle.\n");
75 DeleteCriticalSection(&np_server_cs);
78 if (!ReleaseMutex(master_mutex))
79 WINE_ERR("Unable to leave master mutex!??\n");
81 return rslt;
84 static void RPCSS_ServerProcessRANMessage(PRPCSS_NP_MESSAGE pMsg, PRPCSS_NP_REPLY pReply)
86 WINE_TRACE("\n");
87 /* we do absolutely nothing, but on the server end,
88 the lazy timeout is reset as a result of our connection. */
89 RPCSS_SetMaxLazyTimeout(pMsg->message.ranmsg.timeout);
90 RPCSS_SetLazyTimeRemaining(RPCSS_GetMaxLazyTimeout());
91 pReply->as_uint = 0;
94 static void RPCSS_ServerProcessREGISTEREPMessage(PRPCSS_NP_MESSAGE pMsg, PRPCSS_NP_REPLY pReply,
95 char *vardata)
97 WINE_TRACE("\n");
99 RPCSS_RegisterRpcEndpoints(
100 pMsg->message.registerepmsg.iface,
101 pMsg->message.registerepmsg.object_count,
102 pMsg->message.registerepmsg.binding_count,
103 pMsg->message.registerepmsg.no_replace,
104 vardata,
105 pMsg->vardata_payload_size
108 /* no reply */
109 pReply->as_uint = 0;
112 static void RPCSS_ServerProcessUNREGISTEREPMessage(PRPCSS_NP_MESSAGE pMsg,
113 PRPCSS_NP_REPLY pReply, char *vardata)
115 WINE_TRACE("\n");
117 RPCSS_UnregisterRpcEndpoints(
118 pMsg->message.unregisterepmsg.iface,
119 pMsg->message.unregisterepmsg.object_count,
120 pMsg->message.unregisterepmsg.binding_count,
121 vardata,
122 pMsg->vardata_payload_size
125 /* no reply */
126 pReply->as_uint = 0;
129 static void RPCSS_ServerProcessRESOLVEEPMessage(PRPCSS_NP_MESSAGE pMsg,
130 PRPCSS_NP_REPLY pReply, char *vardata)
132 WINE_TRACE("\n");
134 /* for now, reply is placed into *pReply.as_string, on success, by RPCSS_ResolveRpcEndpoints */
135 ZeroMemory(pReply->as_string, MAX_RPCSS_NP_REPLY_STRING_LEN);
136 RPCSS_ResolveRpcEndpoints(
137 pMsg->message.resolveepmsg.iface,
138 pMsg->message.resolveepmsg.object,
139 vardata,
140 pReply->as_string
144 static void RPCSS_ServerProcessMessage(PRPCSS_NP_MESSAGE pMsg, PRPCSS_NP_REPLY pReply, char *vardata)
146 WINE_TRACE("\n");
147 switch (pMsg->message_type) {
148 case RPCSS_NP_MESSAGE_TYPEID_RANMSG:
149 RPCSS_ServerProcessRANMessage(pMsg, pReply);
150 break;
151 case RPCSS_NP_MESSAGE_TYPEID_REGISTEREPMSG:
152 RPCSS_ServerProcessREGISTEREPMessage(pMsg, pReply, vardata);
153 break;
154 case RPCSS_NP_MESSAGE_TYPEID_UNREGISTEREPMSG:
155 RPCSS_ServerProcessUNREGISTEREPMessage(pMsg, pReply, vardata);
156 break;
157 case RPCSS_NP_MESSAGE_TYPEID_RESOLVEEPMSG:
158 RPCSS_ServerProcessRESOLVEEPMessage(pMsg, pReply, vardata);
159 break;
160 default:
161 WINE_ERR("Message type unknown!! No action taken.\n");
165 /* each message gets its own thread. this is it. */
166 static VOID HandlerThread(LPVOID lpvPipeHandle)
168 RPCSS_NP_MESSAGE msg, vardata_payload_msg;
169 char *c, *vardata = NULL;
170 RPCSS_NP_REPLY reply;
171 DWORD bytesread, written;
172 BOOL success, had_payload = FALSE;
173 HANDLE mypipe;
175 mypipe = (HANDLE) lpvPipeHandle;
177 WINE_TRACE("mypipe: %p\n", mypipe);
179 success = ReadFile(
180 mypipe, /* pipe handle */
181 (char *) &msg, /* message buffer */
182 sizeof(RPCSS_NP_MESSAGE), /* message buffer size */
183 &bytesread, /* receives number of bytes read */
184 NULL /* not overlapped */
187 if (msg.vardata_payload_size) {
188 had_payload = TRUE;
189 /* this fudge space allows us not to worry about exceeding the buffer space
190 on the last read */
191 vardata = LocalAlloc(LPTR, (msg.vardata_payload_size) + VARDATA_PAYLOAD_BYTES);
192 if (!vardata) {
193 WINE_ERR("vardata memory allocation failure.\n");
194 success = FALSE;
195 } else {
196 for ( c = vardata; (c - vardata) < msg.vardata_payload_size;
197 c += VARDATA_PAYLOAD_BYTES) {
198 success = ReadFile(
199 mypipe,
200 (char *) &vardata_payload_msg,
201 sizeof(RPCSS_NP_MESSAGE),
202 &bytesread,
203 NULL
205 if ( (!success) || (bytesread != sizeof(RPCSS_NP_MESSAGE)) ||
206 (vardata_payload_msg.message_type != RPCSS_NP_MESSAGE_TYPEID_VARDATAPAYLOADMSG) ) {
207 WINE_ERR("vardata payload read failure! (s=%s,br=%ld,mt=%u,mt_exp=%u\n",
208 success ? "TRUE" : "FALSE", bytesread,
209 vardata_payload_msg.message_type, RPCSS_NP_MESSAGE_TYPEID_VARDATAPAYLOADMSG);
210 success = FALSE;
211 break;
213 CopyMemory(c, vardata_payload_msg.message.vardatapayloadmsg.payload, VARDATA_PAYLOAD_BYTES);
214 WINE_TRACE("payload read.\n");
219 if (success && (bytesread == sizeof(RPCSS_NP_MESSAGE))) {
220 WINE_TRACE("read success.\n");
221 /* process the message and send a reply, serializing requests. */
222 EnterCriticalSection(&np_server_cs);
223 WINE_TRACE("processing message.\n");
224 RPCSS_ServerProcessMessage(&msg, &reply, vardata);
225 LeaveCriticalSection(&np_server_cs);
227 if (had_payload) LocalFree(vardata);
229 WINE_TRACE("message processed, sending reply....\n");
231 success = WriteFile(
232 mypipe, /* pipe handle */
233 (char *) &reply, /* reply buffer */
234 sizeof(RPCSS_NP_REPLY), /* reply buffer size */
235 &written, /* receives number of bytes written */
236 NULL /* not overlapped */
239 if ( (!success) || (written != sizeof(RPCSS_NP_REPLY)) )
240 WINE_WARN("Message reply failed. (success=%d, br=%ld)\n", success, written);
241 else
242 WINE_TRACE("Reply sent successfully.\n");
243 } else
244 WINE_WARN("Message receipt failed.\n");
246 FlushFileBuffers(mypipe);
247 DisconnectNamedPipe(mypipe);
248 CloseHandle(mypipe);
249 InterlockedDecrement(&srv_thread_count);
252 static VOID NPMainWorkThread(LPVOID ignored)
254 BOOL connected;
255 HANDLE hthread, master_mutex = RPCSS_GetMasterMutex();
256 DWORD threadid, wait_result;
258 WINE_TRACE("\n");
260 while (server_live) {
261 connected = ConnectNamedPipe(np_server_end, NULL) ?
262 TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
264 if (connected) {
265 /* is "work" the act of connecting pipes, or the act of serving
266 requests successfully? for now I will make it the former. */
267 if (!SetEvent(np_server_work_event))
268 WINE_WARN("failed to signal np_server_work_event.\n");
270 /* Create a thread for this client. */
271 InterlockedIncrement(&srv_thread_count);
272 hthread = CreateThread(
273 NULL, /* no security attribute */
274 0, /* default stack size */
275 (LPTHREAD_START_ROUTINE) HandlerThread,
276 (LPVOID) np_server_end, /* thread parameter */
277 0, /* not suspended */
278 &threadid /* returns thread ID (not used) */
281 if (hthread) {
282 WINE_TRACE("Spawned handler thread: %p\n", hthread);
283 CloseHandle(hthread);
285 /* for safety's sake, hold the mutex while we switch the pipe */
287 wait_result = WaitForSingleObject(master_mutex, MASTER_MUTEX_TIMEOUT);
289 switch (wait_result) {
290 case WAIT_ABANDONED: /* ? */
291 case WAIT_OBJECT_0:
292 /* we have ownership */
293 break;
294 case WAIT_FAILED:
295 case WAIT_TIMEOUT:
296 default:
297 /* huh? */
298 wait_result = WAIT_FAILED;
301 if (wait_result == WAIT_FAILED) {
302 WINE_ERR("Couldn't enter master mutex. Expect prolems.\n");
303 } else {
304 /* now create a new named pipe instance to listen on */
305 np_server_end = CreateNamedPipe(
306 NAME_RPCSS_NAMED_PIPE, /* pipe name */
307 PIPE_ACCESS_DUPLEX, /* pipe open mode */
308 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, /* pipe-specific modes */
309 PIPE_UNLIMITED_INSTANCES, /* maximum instances */
310 sizeof(RPCSS_NP_REPLY), /* output buffer size */
311 sizeof(RPCSS_NP_MESSAGE), /* input buffer size */
312 2000, /* time-out interval */
313 NULL /* SD */
316 if (np_server_end == INVALID_HANDLE_VALUE) {
317 WINE_ERR("Failed to recreate named pipe!\n");
318 /* not sure what to do? */
319 assert(FALSE);
322 if (!ReleaseMutex(master_mutex))
323 WINE_ERR("Uh oh. Couldn't leave master mutex. Expect deadlock.\n");
325 } else {
326 WINE_ERR("Failed to spawn handler thread!\n");
327 DisconnectNamedPipe(np_server_end);
328 InterlockedDecrement(&srv_thread_count);
332 WINE_TRACE("Server thread shutdown.\n");
335 static HANDLE RPCSS_NPConnect(void)
337 HANDLE the_pipe = NULL;
338 DWORD dwmode, wait_result;
339 HANDLE master_mutex = RPCSS_GetMasterMutex();
341 WINE_TRACE("\n");
343 while (TRUE) {
345 wait_result = WaitForSingleObject(master_mutex, MASTER_MUTEX_TIMEOUT);
346 switch (wait_result) {
347 case WAIT_ABANDONED:
348 case WAIT_OBJECT_0:
349 break;
350 case WAIT_FAILED:
351 case WAIT_TIMEOUT:
352 default:
353 WINE_ERR("This should never happen: couldn't enter mutex.\n");
354 return NULL;
357 /* try to open the client side of the named pipe. */
358 the_pipe = CreateFileA(
359 NAME_RPCSS_NAMED_PIPE, /* pipe name */
360 GENERIC_READ | GENERIC_WRITE, /* r/w access */
361 0, /* no sharing */
362 NULL, /* no security attributes */
363 OPEN_EXISTING, /* open an existing pipe */
364 0, /* default attributes */
365 NULL /* no template file */
368 if (the_pipe != INVALID_HANDLE_VALUE)
369 break;
371 if (GetLastError() != ERROR_PIPE_BUSY) {
372 WINE_WARN("Unable to open named pipe %s (assuming unavailable).\n",
373 wine_dbgstr_a(NAME_RPCSS_NAMED_PIPE));
374 the_pipe = NULL;
375 break;
378 WINE_WARN("Named pipe busy (will wait)\n");
380 if (!ReleaseMutex(master_mutex))
381 WINE_ERR("Failed to release master mutex. Expect deadlock.\n");
383 /* wait for the named pipe. We are only
384 willing to wait only 5 seconds. It should be available /very/ soon. */
385 if (! WaitNamedPipeA(NAME_RPCSS_NAMED_PIPE, MASTER_MUTEX_WAITNAMEDPIPE_TIMEOUT))
387 WINE_ERR("Named pipe unavailable after waiting. Something is probably wrong.\n");
388 return NULL;
393 if (the_pipe) {
394 dwmode = PIPE_READMODE_MESSAGE;
395 /* SetNamedPipeHandleState not implemented ATM, but still seems to work somehow. */
396 if (! SetNamedPipeHandleState(the_pipe, &dwmode, NULL, NULL))
397 WINE_WARN("Failed to set pipe handle state\n");
400 if (!ReleaseMutex(master_mutex))
401 WINE_ERR("Uh oh, failed to leave the RPC Master Mutex!\n");
403 return the_pipe;
406 static BOOL RPCSS_SendReceiveNPMsg(HANDLE np, PRPCSS_NP_MESSAGE msg, PRPCSS_NP_REPLY reply)
408 DWORD count;
410 WINE_TRACE("(np == %p, msg == %p, reply == %p)\n", np, msg, reply);
412 if (! WriteFile(np, msg, sizeof(RPCSS_NP_MESSAGE), &count, NULL)) {
413 WINE_ERR("write failed.\n");
414 return FALSE;
417 if (count != sizeof(RPCSS_NP_MESSAGE)) {
418 WINE_ERR("write count mismatch.\n");
419 return FALSE;
422 if (! ReadFile(np, reply, sizeof(RPCSS_NP_REPLY), &count, NULL)) {
423 WINE_ERR("read failed.\n");
424 return FALSE;
427 if (count != sizeof(RPCSS_NP_REPLY)) {
428 WINE_ERR("read count mismatch, got %ld.\n", count);
429 return FALSE;
432 /* message execution was successful */
433 return TRUE;
436 BOOL RPCSS_BecomePipeServer(void)
438 RPCSS_NP_MESSAGE msg;
439 RPCSS_NP_REPLY reply;
440 BOOL rslt = TRUE;
441 HANDLE client_handle, hthread, master_mutex = RPCSS_GetMasterMutex();
442 DWORD threadid, wait_result;
444 WINE_TRACE("\n");
446 wait_result = WaitForSingleObject(master_mutex, MASTER_MUTEX_TIMEOUT);
448 switch (wait_result) {
449 case WAIT_ABANDONED: /* ? */
450 case WAIT_OBJECT_0:
451 /* we have ownership */
452 break;
453 case WAIT_FAILED:
454 case WAIT_TIMEOUT:
455 default:
456 WINE_ERR("Couldn't enter master mutex.\n");
457 return FALSE;
460 /* now we have the master mutex. during this time we will
462 * o check if an rpcss already listens on the pipe. If so,
463 * we will tell it we were invoked, which will cause the
464 * other end to update its timeouts. After, we just return
465 * false.
467 * o otherwise, we establish the pipe for ourselves and get
468 * ready to listen on it
471 if ((client_handle = RPCSS_NPConnect()) != NULL) {
472 msg.message_type = RPCSS_NP_MESSAGE_TYPEID_RANMSG;
473 msg.message.ranmsg.timeout = RPCSS_GetMaxLazyTimeout();
474 msg.vardata_payload_size = 0;
475 if (!RPCSS_SendReceiveNPMsg(client_handle, &msg, &reply))
476 WINE_ERR("Something is amiss: RPC_SendReceive failed.\n");
477 rslt = FALSE;
479 if (rslt) {
480 np_server_work_event = CreateEventA(NULL, FALSE, FALSE, "RpcNpServerWorkEvent");
481 if (np_server_work_event == NULL) {
482 /* dunno what we can do then */
483 WINE_ERR("Unable to create the np_server_work_event\n");
484 assert(FALSE);
486 InitializeCriticalSection(&np_server_cs);
488 np_server_end = CreateNamedPipe(
489 NAME_RPCSS_NAMED_PIPE, /* pipe name */
490 PIPE_ACCESS_DUPLEX, /* pipe open mode */
491 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, /* pipe-specific modes */
492 PIPE_UNLIMITED_INSTANCES, /* maximum number of instances */
493 sizeof(RPCSS_NP_REPLY), /* output buffer size */
494 sizeof(RPCSS_NP_MESSAGE), /* input buffer size */
495 2000, /* time-out interval */
496 NULL /* SD */
499 if (np_server_end == INVALID_HANDLE_VALUE) {
500 WINE_ERR("Failed to create named pipe!\n");
501 DeleteCriticalSection(&np_server_cs);
502 if (!CloseHandle(np_server_work_event)) /* we will leak the handle... */
503 WINE_WARN("Failed to close np_server_work_event handle!\n");
504 np_server_work_event = NULL;
505 np_server_end = NULL;
506 rslt = FALSE;
510 server_live = rslt;
512 if (rslt) {
513 /* OK, now spawn the (single) server thread */
514 hthread = CreateThread(
515 NULL, /* no security attribute */
516 0, /* default stack size */
517 (LPTHREAD_START_ROUTINE) NPMainWorkThread,
518 NULL, /* thread parameter */
519 0, /* not suspended */
520 &threadid /* returns thread ID (not used) */
522 if (hthread) {
523 WINE_TRACE("Created server thread.\n");
524 CloseHandle(hthread);
525 } else {
526 WINE_ERR("Serious error: unable to create server thread!\n");
527 if (!CloseHandle(np_server_work_event)) /* we will leak the handle... */
528 WINE_WARN("Failed to close np_server_work_event handle!\n");
529 if (!CloseHandle(np_server_end)) /* we will leak the handle... */
530 WINE_WARN("Unable to close named pipe handle!\n");
531 DeleteCriticalSection(&np_server_cs);
532 np_server_end = NULL;
533 np_server_work_event = NULL;
534 rslt = FALSE;
535 server_live = FALSE;
538 if (!ReleaseMutex(master_mutex))
539 WINE_ERR("Unable to leave master mutex!??\n");
541 return rslt;
544 BOOL RPCSS_NPDoWork(void)
546 DWORD waitresult = WaitForSingleObject(np_server_work_event, 1000);
548 if (waitresult == WAIT_TIMEOUT)
549 return FALSE;
550 if (waitresult == WAIT_OBJECT_0)
551 return TRUE;
553 return FALSE;