f296c240ebee161195da326d455c163486d0259e
[openafs.git] / src / WINNT / afsd / afsd.c
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 <afs/param.h>
11 #include <afs/stds.h>
12
13 #include <windows.h>
14 #include <string.h>
15 #include <nb30.h>
16
17 #include <osi.h>
18 #include "afsd.h"
19 #include "afsd_init.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <winsock2.h>
23
24 #ifdef _DEBUG
25 #include <crtdbg.h>
26 #endif
27
28 HANDLE main_inst;
29 HWND main_wnd;
30 char main_statusText[100];
31 RECT main_rect;
32 osi_log_t *afsd_logp;
33
34 HANDLE hAFSDWorkerThread[WORKER_THREADS], DoTerminate;
35
36 extern int traceOnPanic;
37
38 extern void afsd_DbgBreakAllocInit();
39 extern void afsd_DbgBreakAdd(DWORD requestNumber);
40
41 HANDLE WaitToTerminate = NULL;
42
43 /*
44  * Notifier function for use by osi_panic
45  */
46 void afsd_notifier(char *msgp, char *filep, long line)
47 {
48         char tbuffer[100];
49         if (filep)
50                 sprintf(tbuffer, "Error at file %s, line %d", filep, line);
51         else
52                 strcpy(tbuffer, "Error at unknown location");
53
54         if (!msgp)
55                 msgp = "Assertion failure";
56
57         MessageBox(NULL, tbuffer, msgp, MB_OK|MB_ICONSTOP|MB_SETFOREGROUND);
58
59         afsd_ForceTrace(TRUE);
60         buf_ForceTrace(TRUE);
61
62         if (traceOnPanic) {
63                 _asm int 3h;
64         }
65
66         exit(1);
67 }
68
69 /* Init function called when window application starts.  Inits instance and
70  * application together, since in Win32 they're essentially the same.
71  *
72  * Function then goes into a loop handling user interface messages.  Most are
73  * used to handle redrawing the icon.
74  */
75 int WINAPI WinMain(
76         HINSTANCE hInstance,
77         HINSTANCE hPrevInstance,
78         char *lpCmdLine,
79         int nCmdShow)
80 {
81         MSG msg;
82         int i;
83         
84     afsd_SetUnhandledExceptionFilter();
85        
86 #ifdef _DEBUG
87     afsd_DbgBreakAllocInit();
88     _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF /* | _CRTDBG_CHECK_ALWAYS_DF */ | _CRTDBG_CHECK_CRT_DF | _CRTDBG_DELAY_FREE_MEM_DF );
89     if (lpCmdLine)
90     {
91         char *allocRequest = strtok(lpCmdLine, " \t");
92         while (allocRequest)
93         {
94             afsd_DbgBreakAdd(atoi(allocRequest));
95             allocRequest = strtok(NULL, " \t");
96         }
97     }
98 #endif 
99
100     if (!InitClass(hInstance))
101         return (FALSE);
102
103     if (!InitInstance(hInstance, nCmdShow))
104         return (FALSE);
105
106     while (GetMessage(&msg, NULL, 0, 0)) {
107         TranslateMessage(&msg);
108         DispatchMessage(&msg);
109     }
110
111     return (msg.wParam);
112 }
113
114
115 /* create the window type for our main window */
116 BOOL InitClass(HANDLE hInstance)
117 {
118         WNDCLASS  wc;
119
120         wc.style = CS_DBLCLKS;          /* double-click messages */
121         wc.lpfnWndProc = (WNDPROC) MainWndProc;
122         wc.cbClsExtra = 0;
123         wc.cbWndExtra = 0;
124         wc.hInstance = hInstance;
125         wc.hIcon = LoadIcon(hInstance, "AFSDIcon");
126         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
127         wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
128         wc.lpszMenuName =  "AFSDMenu";
129         wc.lpszClassName = "AFSDWinClass";
130
131         return (RegisterClass(&wc));
132 }
133
134 /* initialize the process.  Reads the init files to get the appropriate
135  * information. */
136 BOOL InitInstance(
137         HANDLE hInstance,
138         int nCmdShow)
139 {
140         HWND hWnd;
141         HDC hDC;
142         TEXTMETRIC textmetric;
143         INT nLineHeight;
144     long code, cnt;
145         char *reason;
146  
147         /* remember this, since it is a useful thing for some of the Windows
148          * calls */
149         main_inst = hInstance;
150
151         /* create our window */
152         hWnd = CreateWindow(
153                 "AFSDWinClass",
154                 "AFSD",
155                 WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,
156                 CW_USEDEFAULT,
157                 CW_USEDEFAULT,
158                 CW_USEDEFAULT,
159                 CW_USEDEFAULT,
160                 NULL,
161                 NULL,
162                 hInstance,
163                 NULL
164         );
165
166         if (!hWnd)
167                 return (FALSE);
168
169         /* lookup text dimensions */
170         hDC = GetDC(hWnd);
171         GetTextMetrics(hDC, &textmetric);
172         nLineHeight = textmetric.tmExternalLeading + textmetric.tmHeight;
173         
174         main_rect.left   = GetDeviceCaps(hDC, LOGPIXELSX) / 4;   /* 1/4 inch */
175         main_rect.right  = GetDeviceCaps(hDC, HORZRES);
176         main_rect.top    = GetDeviceCaps(hDC, LOGPIXELSY) / 4;   /* 1/4 inch */
177         ReleaseDC(hWnd, hDC);
178         main_rect.bottom = main_rect.top + nLineHeight;
179
180         osi_InitPanic(afsd_notifier);
181
182         afsi_start();
183
184         code = afsd_InitCM(&reason);
185         if (code != 0)
186                 osi_panic(reason, __FILE__, __LINE__);
187
188         code = afsd_InitDaemons(&reason);
189         if (code != 0)
190                 osi_panic(reason, __FILE__, __LINE__);
191
192         code = afsd_InitSMB(&reason, MessageBox);
193
194         if (code != 0)
195             osi_panic(reason, __FILE__, __LINE__);
196
197         ShowWindow(hWnd, SW_SHOWMINNOACTIVE);
198         UpdateWindow(hWnd);
199         return (TRUE);
200 }
201
202 /* called with no locks with translated messages */
203 LONG APIENTRY MainWndProc(
204         HWND hWnd,
205         unsigned int message,
206         unsigned int wParam,
207         long lParam)
208 {
209         HDC hDC;                         /* display-context variable     */
210         PAINTSTRUCT ps;                  /* paint structure              */
211
212         main_wnd = hWnd;
213
214         switch (message) {
215             case WM_QUERYOPEN:
216                 /* block attempts to open the window */
217                 return 0;
218
219             case WM_COMMAND:
220                 /* LOWORD(wParam) is command */
221                 return (DefWindowProc(hWnd, message, wParam, lParam));
222
223             case WM_CREATE:
224                 break;
225
226             case WM_PAINT:
227                 hDC = BeginPaint (hWnd, &ps);
228                 /* nothing to print, but this clears invalidated rectangle flag */
229                 EndPaint(hWnd, &ps);
230                 break;
231
232             case WM_DESTROY:
233                 RpcMgmtStopServerListening(NULL);
234                 PostQuitMessage(0);
235                 break;
236
237             default:
238                 return (DefWindowProc(hWnd, message, wParam, lParam));
239         }
240         return (0);
241 }