Windows: remove trailing whitespace
[openafs.git] / src / WINNT / afsadmsvr / TaAfsAdmSvrMain.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <winsock2.h>
11 #include <ws2tcpip.h>
12
13 extern "C" {
14 #include <afsconfig.h>
15 #include <afs/param.h>
16 #include <afs/stds.h>
17 #include <roken.h>
18 }
19
20 #include "TaAfsAdmSvrInternal.h"
21
22 /*
23  * ROUTINES ___________________________________________________________________
24  *
25  */
26
27 #ifdef DEBUG
28 LPARAM CALLBACK AfsAdmSvr_Debug_ThreadProc (PVOID lp)
29 {
30    ShowMemoryManager();
31
32    MSG msg;
33    while (GetMessage (&msg, 0, 0, 0))
34       {
35       if (!IsMemoryManagerMessage (&msg))
36          {
37          TranslateMessage (&msg);
38          DispatchMessage (&msg);
39          }
40       }
41
42    return 0;
43 }
44 #endif
45
46
47 int cdecl main (int argc, char **argv)
48 {
49    BOOL fSuccess = FALSE;
50
51    Print (TEXT("Initializing..."));
52
53    WSADATA Data;
54    WSAStartup (0x0101, &Data);
55
56    // Parse the command-line
57    //
58    UINT_PTR dwAutoScope = AFSADMSVR_SCOPE_VOLUMES | AFSADMSVR_SCOPE_USERS;
59
60    for (--argc,++argv; argc; --argc,++argv)
61       {
62       if (!lstrcmpi (*argv, AFSADMSVR_KEYWORD_TIMED))
63          AfsAdmSvr_EnableAutoShutdown (TRUE);
64       else if (!lstrcmpi (*argv, AFSADMSVR_KEYWORD_MANUAL))
65          dwAutoScope = 0;
66       else if (!lstrcmpi (*argv, AFSADMSVR_KEYWORD_SCOPE_USERS))
67          dwAutoScope &= ~AFSADMSVR_SCOPE_VOLUMES;
68       else if (!lstrcmpi (*argv, AFSADMSVR_KEYWORD_SCOPE_VOLUMES))
69          dwAutoScope &= ~AFSADMSVR_SCOPE_USERS;
70 #ifdef DEBUG
71       else if (!lstrcmpi (*argv, AFSADMSVR_KEYWORD_DEBUG))
72          CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)AfsAdmSvr_Debug_ThreadProc, 0, 0, 0);
73 #endif
74       }
75
76    // Prepare to listen for RPCs
77    //
78    unsigned char *pszPROTOCOL = (unsigned char *)"ncacn_ip_tcp";
79    unsigned char *pszENTRYNAME = (unsigned char *)AFSADMSVR_ENTRYNAME_DEFAULT;
80    unsigned char *pszANNOTATION = (unsigned char *)"Transarc AFS Administrative Server";
81    unsigned char szEndpoint[ 32 ];
82    wsprintf ((LPTSTR)szEndpoint, "%lu", AFSADMSVR_ENDPOINT_DEFAULT);
83    int cMAX_CALLS = 50;
84
85    // Clean up any broken interface registration
86    //
87    RpcServerUnregisterIf (ITaAfsAdminSvr_v1_0_s_ifspec, 0, FALSE);
88 #ifdef notdef
89    RpcNsBindingUnexport (RPC_C_NS_SYNTAX_DEFAULT, pszENTRYNAME, ITaAfsAdminSvr_v1_0_s_ifspec, NULL);
90 #endif
91
92    // Register our interface
93    //
94    RPC_STATUS status;
95    if ((status = RpcServerUseProtseq (pszPROTOCOL, cMAX_CALLS, NULL)) != 0)
96       {
97       Print (dlERROR, TEXT("RpcServerUseProtseq failed; error 0x%08lX"), status);
98       }
99    else if ((status = RpcServerRegisterIf (ITaAfsAdminSvr_v1_0_s_ifspec, 0, 0)) != 0)
100       {
101       Print (dlERROR, TEXT("RpcServerRegisterIf failed; error 0x%08lX"), status);
102       }
103    else
104       {
105       // Always try to register on port 1025; that's the easiest thing for
106       // some clients to find. We'll only fail if we (a) can't use 1025, and
107       // (b) can't export our bindings.
108       //
109       BOOL fGotPort = FALSE;
110       if (RpcServerUseProtseqEp (pszPROTOCOL, cMAX_CALLS, szEndpoint, NULL) == 0)
111          fGotPort = TRUE;
112       else
113          Print (dlWARNING, TEXT("RpcServerUseProtseqEp failed (benign); error 0x%08lX"), status);
114
115       RPC_BINDING_VECTOR *pBindingVector;
116       if ((status = RpcServerInqBindings (&pBindingVector)) != 0)
117          {
118          Print (dlERROR, TEXT("RpcServerRegisterIf failed; error 0x%08lX"), status);
119          }
120       else if ((status = RpcEpRegister (ITaAfsAdminSvr_v1_0_s_ifspec, pBindingVector, NULL, pszANNOTATION)) != 0)
121          {
122          Print (dlERROR, TEXT("RpcEpRegister failed; error 0x%08lX"), status);
123          }
124       else
125          {
126          BOOL fExportedBinding = FALSE;
127
128 #ifdef notdef
129          if ((status = RpcNsBindingExport (RPC_C_NS_SYNTAX_DEFAULT, pszENTRYNAME, ITaAfsAdminSvr_v1_0_s_ifspec, pBindingVector, NULL)) == 0)
130             fExportedBinding = TRUE;
131          else
132             Print (dlWARNING, TEXT("RpcNsBindingExport failed (benign); error 0x%08lX"), status);
133 #endif
134
135          if (!fExportedBinding && !fGotPort)
136             {
137             Print (dlERROR, TEXT("RpcNsBindingExport failed; error 0x%08lX"), status);
138             Print (dlERROR, TEXT("Could not bind to port %s or export bindings; terminating"), szEndpoint);
139             }
140          else
141             {
142             AfsAdmSvr_Startup();
143
144             Print (TEXT("Ready.\n"));
145
146             // If not asked to open cells manually, fork a thread to start opening
147             // the default local cell
148             //
149             if (dwAutoScope)
150                {
151                DWORD dwThreadID;
152                CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)AfsAdmSvr_AutoOpen_ThreadProc, (PVOID)dwAutoScope, 0, &dwThreadID);
153                }
154
155             // Listen for requests until someone calls StopListen
156             //
157             if ((status = RpcServerListen (1, cMAX_CALLS, FALSE)) != 0)
158                {
159                Print (dlERROR, TEXT("RpcServerListen failed; error 0x%08lX"), status);
160                }
161             else
162                {
163                fSuccess = TRUE;
164                }
165
166             AfsAdmSvr_Shutdown();
167             }
168
169 #ifdef notdef
170          if (fExportedBinding)
171             {
172             if ((status = RpcNsBindingUnexport (RPC_C_NS_SYNTAX_DEFAULT, pszENTRYNAME, ITaAfsAdminSvr_v1_0_s_ifspec, NULL)) != 0)
173                {
174                Print (dlWARNING, TEXT("RpcNsBindingExport failed; error 0x%08lX"), status);
175                }
176             }
177 #endif
178          if ((status = RpcEpUnregister (ITaAfsAdminSvr_v1_0_s_ifspec, pBindingVector, NULL)) != 0)
179             {
180             Print (dlWARNING, TEXT("RpcEpUnregister failed; error 0x%08lX"), status);
181             }
182          }
183       }
184
185    Print (TEXT("Shutting down...\n"));
186
187    if ((status = RpcServerUnregisterIf (0, 0, FALSE)) != 0)
188       {
189       Print (dlWARNING, TEXT("RpcServerUnregisterIf failed; error 0x%08lX"), status);
190       exit (-1);
191       }
192
193    return (fSuccess) ? (0) : (-1);
194 }
195
196
197 extern "C" void __RPC_FAR * __RPC_USER MIDL_user_allocate (size_t cbAllocate)
198 {
199    return (void __RPC_FAR *)Allocate (cbAllocate);
200 }
201
202
203 extern "C" void __RPC_USER MIDL_user_free (void __RPC_FAR *pData)
204 {
205    Free (pData);
206 }
207