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