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