dinput: Remove redundant declaration.
[wine] / programs / rpcss / rpcss_main.c
1 /*
2  * Copyright 2001, Ove Kåven, TransGaming Technologies Inc.
3  * Copyright 2002 Greg Turner
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  *
19  * ---- rpcss_main.c:
20  *   Initialize and start serving requests.  Bail if rpcss already is
21  *   running.
22  *
23  * ---- RPCSS.EXE:
24  *   
25  *   Wine needs a server whose role is somewhat like that
26  *   of rpcss.exe in windows.  This is not a clone of
27  *   windows rpcss at all.  It has been given the same name, however,
28  *   to provide for the possibility that at some point in the future, 
29  *   it may become interface compatible with the "real" rpcss.exe on
30  *   Windows.
31  *
32  * ---- KNOWN BUGS / TODO:
33  *
34  *   o Service hooks are unimplemented (if you bother to implement
35  *     these, also implement net.exe, at least for "net start" and
36  *     "net stop" (should be pretty easy I guess, assuming the rest
37  *     of the services API infrastructure works.
38  * 
39  *   o Is supposed to use RPC, not random kludges, to map endpoints.
40  *
41  *   o Probably name services should be implemented here as well.
42  *
43  *   o Wine's named pipes (in general) may not interoperate with those of 
44  *     Windows yet (?)
45  *
46  *   o There is a looming problem regarding listening on privileged
47  *     ports.  We will need to be able to coexist with SAMBA, and be able
48  *     to function without running winelib code as root.  This may
49  *     take some doing, including significant reconceptualization of the
50  *     role of rpcss.exe in wine.
51  *
52  *   o Who knows?  Whatever rpcss does, we ought to at
53  *     least think about doing... but what /does/ it do?
54  */
55
56 #include <stdio.h>
57 #include <limits.h>
58 #include <assert.h>
59
60 #define NONAMELESSUNION
61 #define NONAMELESSSTRUCT
62 #include "rpcss.h"
63 #include "winnt.h"
64
65 #include "wine/debug.h"
66
67 WINE_DEFAULT_DEBUG_CHANNEL(ole);
68
69 static HANDLE master_mutex;
70 static HANDLE exit_event;
71
72 extern HANDLE __wine_make_process_system(void);
73
74 HANDLE RPCSS_GetMasterMutex(void)
75 {
76   return master_mutex;
77 }
78
79 static BOOL RPCSS_work(HANDLE exit_event)
80 {
81   return RPCSS_NPDoWork(exit_event);
82 }
83
84 static BOOL RPCSS_Initialize(void)
85 {
86   WINE_TRACE("\n");
87
88   exit_event = __wine_make_process_system();
89
90   master_mutex = CreateMutexA( NULL, FALSE, RPCSS_MASTER_MUTEX_NAME);
91   if (!master_mutex) {
92     WINE_ERR("Failed to create master mutex\n");
93     return FALSE;
94   }
95
96   if (!RPCSS_BecomePipeServer()) {
97     WINE_WARN("Server already running: exiting.\n");
98
99     CloseHandle(master_mutex);
100     master_mutex = NULL;
101
102     return FALSE;
103   }
104
105   return TRUE;
106 }
107
108 /* returns false if we discover at the last moment that we
109    aren't ready to terminate */
110 static BOOL RPCSS_Shutdown(void)
111 {
112   if (!RPCSS_UnBecomePipeServer())
113     return FALSE;
114    
115   if (!CloseHandle(master_mutex))
116     WINE_WARN("Failed to release master mutex\n");
117
118   master_mutex = NULL;
119
120   CloseHandle(exit_event);
121
122   return TRUE;
123 }
124
125 static void RPCSS_MainLoop(void)
126 {
127   WINE_TRACE("\n");
128
129   while ( RPCSS_work(exit_event) )
130       ;
131 }
132
133 int main( int argc, char **argv )
134 {
135   /* 
136    * We are invoked as a standard executable; we act in a
137    * "lazy" manner.  We open up our pipe, and hang around until we all
138    * user processes exit, and then silently terminate.
139    */
140
141   if (RPCSS_Initialize()) {
142     RPCSS_MainLoop();
143     RPCSS_Shutdown();
144   }
145
146   return 0;
147 }