- Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathiscguard.cpp
1191 lines (1055 loc) · 32.1 KB
/
iscguard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* The contents of this file are subject to the Interbase Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy
* of the License at http://www.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#include"firebird.h"
#include<stdio.h>
#include"../yvalve/gds_proto.h"
#include<stdlib.h>
#include<windows.h>
#include<shellapi.h>
#include<prsht.h>
#include<commctrl.h>
#ifdef TIME_WITH_SYS_TIME
# include<sys/time.h>
# include<time.h>
#else
# ifdef HAVE_SYS_TIME_H
# include<sys/time.h>
# else
# include<time.h>
# endif
#endif
#include"../iscguard/iscguard.rh"
#include"../iscguard/iscguard.h"
#include"../iscguard/cntlg_proto.h"
#include"../utilities/install/install_nt.h"
#include"../remote/server/os/win32/window.h"
#include"../remote/server/os/win32/chop_proto.h"
#include"../common/config/config.h"
#include"../common/classes/init.h"
#include"../common/os/path_utils.h"
#ifdef WIN_NT
#include<process.h>// _beginthread
#endif
// Startup Configuration Entry point for regcfg.exe.
//#define SVC_CONFIG 4
//#define REGCFG_ENTRYPOINT "LaunchInstReg"
//#define REGCFG_DLL "REGCFG.DLL"
typedefvoid (__cdecl * LPFNREGCFG) (char *, short);
// Define an array of dword pairs,
// where the first of each pair is the control ID,
// and the second is the context ID for a help topic,
// which is used in the help file.
static DWORD aMenuHelpIDs[] = {
IDC_VERSION, ibs_guard_version,
IDC_LOG, ibs_guard_log,
IDC_LOCATION, ibs_server_directory,
0, 0
};
// Function prototypes
static LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
static THREAD_ENTRY_DECLARE WINDOW_main(THREAD_ENTRY_PARAM);
#ifdef NOT_USED_OR_REPLACED
staticvoidStartGuardian(HWND);
#endif
staticboolparse_args(LPCSTR);
THREAD_ENTRY_DECLARE start_and_watch_server(THREAD_ENTRY_PARAM);
THREAD_ENTRY_DECLARE swap_icons(THREAD_ENTRY_PARAM);
staticvoidaddTaskBarIcons(HINSTANCE hInstance, HWND hWnd, BOOL& bInTaskBar);
staticvoidwrite_log(int, constchar*);
HWND DisplayPropSheet(HWND, HINSTANCE);
LRESULT CALLBACK GeneralPage(HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInstance_gbl;
HWND hPSDlg, hWndGbl;
staticint nRestarts = 0; // the number of times the server was restarted
staticbool service_flag = true;
static TEXT instance[MAXPATHLEN];
static Firebird::GlobalPtr<Firebird::string> service_name;
static Firebird::GlobalPtr<Firebird::string> remote_name;
static Firebird::GlobalPtr<Firebird::string> mutex_name;
// unsigned short shutdown_flag = FALSE;
static log_info* log_entry;
static Thread::Handle watcher_thd = 0;
static Thread::Handle swap_icons_thd = 0;
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE /*hPrevInstance*/, LPSTR lpszCmdLine, int/*nCmdShow*/)
{
/**************************************
*
* m a i n
*
**************************************
*
* Functional description
* The main routine for Windows based server guardian.
*
**************************************/
strcpy(instance, FB_DEFAULT_INSTANCE);
service_flag = parse_args(lpszCmdLine);
service_name->printf(ISCGUARD_SERVICE, instance);
remote_name->printf(REMOTE_SERVICE, instance);
mutex_name->printf(GUARDIAN_MUTEX, instance);
// set the global HINSTANCE as we need it in WINDOW_main
hInstance_gbl = hInstance;
// allocate space for the event list
log_entry = static_cast<log_info*>(malloc(sizeof(log_info)));
log_entry->next = NULL;
// since the flag is set we run as a service
if (service_flag)
{
CNTL_init(WINDOW_main, instance);
const SERVICE_TABLE_ENTRY service_table[] =
{
{const_cast<char*>(service_name->c_str()), CNTL_main_thread},
{NULL, NULL}
};
// BRS There is a error in MinGW (3.1.0) headers
// the parameter of StartServiceCtrlDispatcher is declared const in msvc headers
#if defined(MINGW)
if (!StartServiceCtrlDispatcher(const_cast<SERVICE_TABLE_ENTRY*>(service_table)))
#else
if (!StartServiceCtrlDispatcher(service_table))
#endif
{
if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
CNTL_shutdown_service("StartServiceCtrlDispatcher failed");
}
if (watcher_thd)
{
WaitForSingleObject(watcher_thd, 5000);
CloseHandle(watcher_thd);
}
}
else {
returnWINDOW_main(0);
}
returnTRUE;
}
staticboolparse_args(LPCSTR lpszArgs)
{
/**************************************
*
* p a r s e _ a r g s
*
**************************************
*
* Functional description
* WinMain gives us a stupid command string, not
* a cool argv. Parse through the string and
* set the options.
* Returns
* A value of true or false depending on if -s is specified.
* CVC: Service is the default for NT, use -a for application.
*
*
**************************************/
bool is_service = true;
bool delimited = false;
for (constchar* p = lpszArgs; *p; p++)
{
if (*p++ == '-')
{
char c;
while (c = *p++)
{
switch (UPPER(c))
{
case'A':
is_service = false;
break;
case'S':
delimited = false;
while (*p && *p == '')
p++;
if (*p && *p == '"')
{
p++;
delimited = true;
}
if (delimited)
{
char* pi = instance;
constchar* pend = instance + sizeof(instance) - 1;
while (*p && *p != '"' && pi < pend) {
*pi++ = *p++;
}
*pi++ = '\0';
if (*p && *p == '"')
p++;
}
else
{
if (*p && *p != '-')
{
char* pi = instance;
constchar* pend = instance + sizeof(instance) - 1;
while (*p && *p != '' && pi < pend) {
*pi++ = *p++;
}
*pi++ = '\0';
}
}
break;
default:
is_service = true;
break;
}
}
}
}
return is_service;
}
static THREAD_ENTRY_DECLARE WINDOW_main(THREAD_ENTRY_PARAM)
{
/**************************************
*
* W I N D O W _ m a i n
*
**************************************
*
* Functional description
*
* This function is where the actual service code starts.
* Do all the window init stuff, then fork off a thread for starting
* the server.
*
**************************************/
// If we're a service, don't create a window
if (service_flag)
{
try
{
Thread::start(start_and_watch_server, 0, THREAD_medium, &watcher_thd);
}
catch (const Firebird::Exception&)
{
// error starting server thread
char szMsgString[256];
LoadString(hInstance_gbl, IDS_CANT_START_THREAD, szMsgString, 256);
gds__log(szMsgString);
}
return0;
}
// Make sure that there is only 1 instance of the guardian running
HWND hWnd = FindWindow(GUARDIAN_CLASS_NAME, GUARDIAN_APP_NAME);
if (hWnd)
{
char szMsgString[256];
LoadString(hInstance_gbl, IDS_ALREADYSTARTED, szMsgString, 256);
MessageBox(NULL, szMsgString, GUARDIAN_APP_LABEL, MB_OK | MB_ICONSTOP);
gds__log(szMsgString);
return0;
}
// initialize main window
WNDCLASS wcl;
wcl.hInstance = hInstance_gbl;
wcl.lpszClassName = GUARDIAN_CLASS_NAME;
wcl.lpfnWndProc = WindowFunc;
wcl.style = 0;
wcl.hIcon = LoadIcon(hInstance_gbl, MAKEINTRESOURCE(IDI_IBGUARD));
wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
wcl.lpszMenuName = NULL;
wcl.cbClsExtra = 0;
wcl.cbWndExtra = 0;
wcl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
if (!RegisterClass(&wcl))
{
char szMsgString[256];
LoadString(hInstance_gbl, IDS_REGERROR, szMsgString, 256);
MessageBox(NULL, szMsgString, GUARDIAN_APP_LABEL, MB_OK | MB_ICONSTOP);
return0;
}
hWnd = CreateWindowEx(0,
GUARDIAN_CLASS_NAME,
GUARDIAN_APP_NAME,
WS_DLGFRAME | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
HWND_DESKTOP, NULL, hInstance_gbl, NULL);
// Save the window handle for the thread
hWndGbl = hWnd;
// begin a new thread for calling the start_and_watch_server
try
{
Thread::start(start_and_watch_server, 0, THREAD_medium, NULL);
}
catch (const Firebird::Exception&)
{
// error starting server thread
char szMsgString[256];
LoadString(hInstance_gbl, IDS_CANT_START_THREAD, szMsgString, 256);
MessageBox(NULL, szMsgString, GUARDIAN_APP_LABEL, MB_OK | MB_ICONSTOP);
gds__log(szMsgString);
DestroyWindow(hWnd);
return0;
}
SendMessage(hWnd, WM_COMMAND, IDM_CANCEL, 0);
UpdateWindow(hWnd);
MSG message;
while (GetMessage(&message, NULL, 0, 0))
{
if (hPSDlg)
{
// If property sheet dialog is open
// Check if the message is property sheet dialog specific
BOOL bPSMsg = PropSheet_IsDialogMessage(hPSDlg, &message);
// Check if the property sheet dialog is still valid, if not destroy it
if (!PropSheet_GetCurrentPageHwnd(hPSDlg))
{
DestroyWindow(hPSDlg);
hPSDlg = NULL;
if (swap_icons_thd)
{
CloseHandle(swap_icons_thd);
swap_icons_thd = 0;
};
}
if (bPSMsg)
continue;
}
TranslateMessage(&message);
DispatchMessage(&message);
}
return message.wParam;
}
static LRESULT CALLBACK WindowFunc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
/**************************************
*
* W i n d o w _ F u n c
*
**************************************
*
* Functional description
*
* This function is where the windowing action takes place.
* Handle the various messages which come here from GetMessage.
*
**************************************/
static BOOL bInTaskBar = FALSE;
staticbool bStartup = false;
static HINSTANCE hInstance = NULL;
static UINT s_uTaskbarRestart;
hInstance = (HINSTANCE) GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
switch (message)
{
case WM_CLOSE:
// Clean up memory for log_entry
while (log_entry->next)
{
log_info* tmp = log_entry->next;
free(log_entry);
log_entry = tmp;
}
free(log_entry);
DestroyWindow(hWnd);
break;
case WM_COMMAND:
switch (wParam)
{
case IDM_CANCEL:
ShowWindow(hWnd, bInTaskBar ? SW_HIDE : SW_MINIMIZE);
returnTRUE;
case IDM_OPENPOPUP:
{
// The SetForegroundWindow() has to be called because our window
// does not become the Foreground one (inspite of clicking on
// the icon). This is so because the icon is painted on the task
// bar and is not the same as a minimized window.
SetForegroundWindow(hWnd);
HMENU hPopup = CreatePopupMenu();
char szMsgString[256];
LoadString(hInstance, IDS_SVRPROPERTIES, szMsgString, 256);
AppendMenu(hPopup, MF_STRING, IDM_SVRPROPERTIES, szMsgString);
LoadString(hInstance, IDS_SHUTDOWN, szMsgString, 256);
AppendMenu(hPopup, MF_STRING, IDM_SHUTDOWN, szMsgString);
LoadString(hInstance, IDS_PROPERTIES, szMsgString, 256);
AppendMenu(hPopup, MF_STRING, IDM_PROPERTIES, szMsgString);
SetMenuDefaultItem(hPopup, IDM_PROPERTIES, FALSE);
POINT curPos;
GetCursorPos(&curPos);
TrackPopupMenu(hPopup, TPM_LEFTALIGN | TPM_RIGHTBUTTON,
curPos.x, curPos.y, 0, hWnd, NULL);
DestroyMenu(hPopup);
returnTRUE;
}
case IDM_SHUTDOWN:
{
HWND hTmpWnd = FindWindow(szClassName, szWindowName);
PostMessage(hTmpWnd, WM_COMMAND, (WPARAM) IDM_SHUTDOWN, 0);
}
returnTRUE;
case IDM_PROPERTIES:
if (!hPSDlg)
hPSDlg = DisplayPropSheet(hWnd, hInstance);
else
SetForegroundWindow(hPSDlg);
returnTRUE;
case IDM_INTRSVRPROPERTIES:
returnTRUE;
case IDM_SVRPROPERTIES:
{
HWND hTmpWnd = FindWindow(szClassName, szWindowName);
PostMessage(hTmpWnd, WM_COMMAND, (WPARAM) IDM_PROPERTIES, 0);
}
returnTRUE;
}
break;
case WM_SWITCHICONS:
nRestarts++;
{ // scope
DWORD thr_exit = 0;
if (swap_icons_thd == 0 ||
!GetExitCodeThread(swap_icons_thd, &thr_exit) ||
thr_exit != STILL_ACTIVE)
{
Thread::start(swap_icons, hWnd, THREAD_medium, &swap_icons_thd);
}
} // scope
break;
case ON_NOTIFYICON:
if (bStartup)
{
SendMessage(hWnd, WM_COMMAND, 0, 0);
returnTRUE;
}
switch (lParam)
{
case WM_LBUTTONDOWN:
break;
case WM_LBUTTONDBLCLK:
PostMessage(hWnd, WM_COMMAND, (WPARAM) IDM_PROPERTIES, 0);
break;
case WM_RBUTTONUP:
PostMessage(hWnd, WM_COMMAND, (WPARAM) IDM_OPENPOPUP, 0);
break;
}
break;
case WM_CREATE:
s_uTaskbarRestart = RegisterWindowMessage("TaskbarCreated");
addTaskBarIcons(hInstance, hWnd, bInTaskBar);
break;
case WM_QUERYOPEN:
if (!bInTaskBar)
returnFALSE;
returnDefWindowProc(hWnd, message, wParam, lParam);
case WM_SYSCOMMAND:
if (!bInTaskBar)
{
switch (wParam)
{
case SC_RESTORE:
returnTRUE;
case IDM_SHUTDOWN:
{
HWND hTmpWnd = FindWindow(szClassName, szWindowName);
PostMessage(hTmpWnd, WM_COMMAND, (WPARAM) IDM_SHUTDOWN, 0);
}
returnTRUE;
case IDM_PROPERTIES:
if (!hPSDlg)
hPSDlg = DisplayPropSheet(hWnd, hInstance);
else
SetFocus(hPSDlg);
returnTRUE;
case IDM_SVRPROPERTIES:
{
HWND hTmpWnd = FindWindow(szClassName, szWindowName);
PostMessage(hTmpWnd, WM_COMMAND, (WPARAM) IDM_PROPERTIES, 0);
}
returnTRUE;
}
}
returnDefWindowProc(hWnd, message, wParam, lParam);
case WM_DESTROY:
if (bInTaskBar)
{
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uID = IDI_IBGUARD;
nid.uFlags = 0;
Shell_NotifyIcon(NIM_DELETE, &nid);
}
PostQuitMessage(0);
break;
default:
if (message == s_uTaskbarRestart)
addTaskBarIcons(hInstance, hWnd, bInTaskBar);
returnDefWindowProc(hWnd, message, wParam, lParam);
}
returnFALSE;
}
THREAD_ENTRY_DECLARE start_and_watch_server(THREAD_ENTRY_PARAM)
{
/**************************************
*
* s t a r t _ a n d _ w a t c h _ s e r v e r
*
**************************************
*
* Functional description
*
* This function is where the server process is created and
* the thread waits for this process to exit.
*
**************************************/
Firebird::ContextPoolHolder threadContext(getDefaultMemoryPool());
HANDLE procHandle = NULL;
bool done = true;
const UINT error_mode = SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX |
SEM_NOOPENFILEERRORBOX | SEM_NOALIGNMENTFAULTEXCEPT;
SC_HANDLE hScManager = 0, hService = 0;
// get the guardian startup information
constshort option = Firebird::Config::getGuardianOption();
char prefix_buffer[MAXPATHLEN];
GetModuleFileName(NULL, prefix_buffer, sizeof(prefix_buffer));
Firebird::PathName path = prefix_buffer;
path = path.substr(0, path.rfind(PathUtils::dir_sep) + 1) + FBSERVER;
path = "\"" + path + "\"";
Firebird::PathName prog_name = path + " -a -n";
// if the guardian is set to FOREVER then set the error mode
UINT old_error_mode = 0;
if (option == START_FOREVER)
old_error_mode = SetErrorMode(error_mode);
// Spawn the new process
do {
SERVICE_STATUS ServiceStatus;
char out_buf[1024];
BOOL success;
int error = 0;
if (service_flag)
{
if (hService)
{
while ((QueryServiceStatus(hService, &ServiceStatus) == TRUE) &&
(ServiceStatus.dwCurrentState != SERVICE_STOPPED))
{
Sleep(500);
}
}
procHandle = CreateMutex(NULL, FALSE, mutex_name->c_str());
// start as a service. If the service can not be found or
// fails to start, close the handle to the mutex and set
// success = FALSE
if (!hScManager)
hScManager = OpenSCManager(NULL, NULL, GENERIC_READ);
if (!hService)
{
hService = OpenService(hScManager, remote_name->c_str(),
GENERIC_READ | GENERIC_EXECUTE);
}
success = StartService(hService, 0, NULL);
if (success != TRUE)
error = GetLastError();
// if the server is already running, then inform it that it should
// open the guardian mutex so that it may be governed.
if (!error || error == ERROR_SERVICE_ALREADY_RUNNING)
{
// Make sure that it is actually ready to receive commands.
// If we were the one who started it, then it will need a few
// seconds to get ready.
while ((QueryServiceStatus(hService, &ServiceStatus) == TRUE) &&
(ServiceStatus.dwCurrentState != SERVICE_RUNNING))
{
Sleep(500);
}
ControlService(hService, SERVICE_CREATE_GUARDIAN_MUTEX, &ServiceStatus);
success = TRUE;
}
}
else
{
HWND hTmpWnd = FindWindow(szClassName, szWindowName);
if (hTmpWnd == NULL)
{
STARTUPINFO si;
SECURITY_ATTRIBUTES sa;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
success = CreateProcess(NULL, const_cast<char*>(prog_name.c_str()),
&sa, NULL, FALSE, 0, NULL, NULL, &si, &pi);
if (success != TRUE)
error = GetLastError();
procHandle = pi.hProcess;
// TMN: 04 Aug 2000 - closed the handle that previously leaked.
CloseHandle(pi.hThread);
}
else
{
SendMessage(hTmpWnd, WM_COMMAND, (WPARAM) IDM_GUARDED, 0);
DWORD server_pid;
GetWindowThreadProcessId(hTmpWnd, &server_pid);
procHandle = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, FALSE, server_pid);
if (procHandle == NULL)
{
error = GetLastError();
success = FALSE;
}
else {
success = TRUE;
}
}
}
if (success != TRUE)
{
// error creating new process
char szMsgString[256];
LoadString(hInstance_gbl, IDS_CANT_START_THREAD, szMsgString, 256);
sprintf(out_buf, "%s : %s errno : %d", path.c_str(), szMsgString, error);
write_log(IDS_CANT_START_THREAD, out_buf);
if (service_flag)
{
SERVICE_STATUS status_info;
// wait a second to get the mutex handle (just in case) and
// then close it
WaitForSingleObject(procHandle, 1000);
CloseHandle(procHandle);
hService = OpenService(hScManager, remote_name->c_str(),
GENERIC_READ | GENERIC_EXECUTE);
ControlService(hService, SERVICE_CONTROL_STOP, &status_info);
CloseServiceHandle(hScManager);
CloseServiceHandle(hService);
CNTL_stop_service(); //service_name->c_str());
}
else
{
MessageBox(NULL, out_buf, NULL, MB_OK | MB_ICONSTOP);
PostMessage(hWndGbl, WM_CLOSE, 0, 0);
}
return0;
}
else
{
char szMsgString[256];
LoadString(hInstance_gbl, IDS_STARTING_GUARD, szMsgString, 256);
sprintf(out_buf, "%s: %s\n", szMsgString, path.c_str());
write_log(IDS_LOG_START, out_buf);
}
// wait for process to terminate
DWORD exit_status;
if (service_flag)
{
while (WaitForSingleObject(procHandle, 500) == WAIT_OBJECT_0)
{
ReleaseMutex(procHandle);
Sleep(100);
}
constint ret_val = WaitForSingleObject(procHandle, INFINITE);
if (ret_val == WAIT_ABANDONED)
exit_status = CRASHED;
elseif (ret_val == WAIT_OBJECT_0)
exit_status = NORMAL_EXIT;
CloseHandle(procHandle);
}
else
{
while (WaitForSingleObject(procHandle, INFINITE) == WAIT_FAILED)
;
GetExitCodeProcess(procHandle, &exit_status);
CloseHandle(procHandle);
}
if (exit_status != NORMAL_EXIT)
{
// check for startup error
if (exit_status == STARTUP_ERROR)
{
char szMsgString[256];
LoadString(hInstance_gbl, IDS_STARTUP_ERROR, szMsgString, 256);
sprintf(out_buf, "%s: %s (%lu)\n", path.c_str(), szMsgString, exit_status);
write_log(IDS_STARTUP_ERROR, out_buf);
done = true;
}
else
{
char szMsgString[256];
LoadString(hInstance_gbl, IDS_ABNORMAL_TERM, szMsgString, 256);
sprintf(out_buf, "%s: %s (%lu)\n", path.c_str(), szMsgString, exit_status);
write_log(IDS_LOG_TERM, out_buf);
// switch the icons if the server restarted
if (!service_flag)
PostMessage(hWndGbl, WM_SWITCHICONS, 0, 0);
if (option == START_FOREVER)
done = false;
}
}
else
{
// Normal shutdown - ie: via ibmgr - don't restart the server
char szMsgString[256];
LoadString(hInstance_gbl, IDS_NORMAL_TERM, szMsgString, 256);
sprintf(out_buf, "%s: %s\n", path.c_str(), szMsgString);
write_log(IDS_LOG_STOP, out_buf);
done = true;
}
if (option == START_ONCE)
done = true;
} while (!done);
// If on WINNT
if (service_flag)
{
CloseServiceHandle(hScManager);
CloseServiceHandle(hService);
CNTL_stop_service(); //(service_name->c_str());
}
else
PostMessage(hWndGbl, WM_CLOSE, 0, 0);
return0;
}
HWND DisplayPropSheet(HWND hParentWnd, HINSTANCE hInst)
{
/******************************************************************************
*
* D i s p l a y P r o p S h e e t
*
******************************************************************************
*
* Input: hParentWnd - Handle to the main window of this application
*
* Return: Handle to the Property sheet dialog if successful
* NULL if error in displaying property sheet
*
* Description: This function initializes the page(s) of the property sheet,
* and then calls the PropertySheet() function to display it.
*****************************************************************************/
PROPSHEETPAGE PSPages[1];
HINSTANCE hInstance = hInst;
PSPages[0].dwSize = sizeof(PROPSHEETPAGE);
PSPages[0].dwFlags = PSP_USETITLE;
PSPages[0].hInstance = hInstance;
PSPages[0].pszTemplate = MAKEINTRESOURCE(IDD_PROPSHEET);
PSPages[0].pszTitle = MAKEINTRESOURCE(IDS_PROP_TITLE);
PSPages[0].pfnDlgProc = (DLGPROC) GeneralPage;
PSPages[0].pfnCallback = NULL;
PROPSHEETHEADER PSHdr;
PSHdr.dwSize = sizeof(PROPSHEETHEADER);
PSHdr.dwFlags = PSH_PROPTITLE | PSH_PROPSHEETPAGE | PSH_USEICONID | PSH_MODELESS | PSH_NOAPPLYNOW | PSH_NOCONTEXTHELP;
PSHdr.hwndParent = hParentWnd;
PSHdr.hInstance = hInstance;
PSHdr.pszIcon = MAKEINTRESOURCE(IDI_IBGUARD);
PSHdr.pszCaption = (LPSTR) GUARDIAN_APP_LABEL;
PSHdr.nPages = FB_NELEM(PSPages);
PSHdr.nStartPage = 0;
PSHdr.ppsp = (LPCPROPSHEETPAGE) & PSPages;
PSHdr.pfnCallback = NULL;
hPSDlg = (HWND) PropertySheet(&PSHdr);
if (hPSDlg == 0 || hPSDlg == (HWND) -1)
{
gds__log("Create property sheet window failed. Error code %d", GetLastError());
hPSDlg = NULL;
}
return hPSDlg;
}
LRESULT CALLBACK GeneralPage(HWND hDlg, UINT unMsg, WPARAM /*wParam*/, LPARAM lParam)
{
/******************************************************************************
*
* G e n e r a l P a g e
*
******************************************************************************
*
* Input: hDlg - Handle to the page dialog
* unMsg - Message ID
* wParam - WPARAM message parameter
* lParam - LPARAM message parameter
*
* Return: FALSE if message is not processed
* TRUE if message is processed here
*
* Description: This is the window procedure for the "General" page dialog
* of the property sheet dialog box. All the Property Sheet
* related events are passed as WM_NOTIFY messages and they
* are identified within the LPARAM which will be pointer to
* the NMDR structure
*****************************************************************************/
HINSTANCE hInstance = (HINSTANCE) GetWindowLongPtr(hDlg, GWLP_HINSTANCE);
switch (unMsg)
{
case WM_INITDIALOG:
{
char szText[256];
char szWindowText[MAXPATHLEN];
char szFullPath[MAXPATHLEN];
intindex = 0;
constint NCOLS = 3;
// Display the number of times the server has been started by
// this session of the guardian
SetDlgItemInt(hDlg, IDC_RESTARTS, nRestarts, FALSE);
// get the path to the exe.
// Make sure that it is null terminated
GetModuleFileName(hInstance, szWindowText, sizeof(szWindowText));
char* pszPtr = strrchr(szWindowText, '\\');
*(pszPtr + 1) = 0x00;
ChopFileName(szWindowText, szWindowText, 38);
SetDlgItemText(hDlg, IDC_LOCATION, szWindowText);
// Get version information from the application
GetModuleFileName(hInstance, szFullPath, sizeof(szFullPath));
DWORD dwVerHnd;
const DWORD dwVerInfoSize = GetFileVersionInfoSize(szFullPath, &dwVerHnd);
if (dwVerInfoSize)
{
// If we were able to get the information, process it:
UINT cchVer = 25;
LPSTR lszVer = NULL;
HANDLE hMem = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
LPVOID lpvMem = GlobalLock(hMem);
GetFileVersionInfo(szFullPath, dwVerHnd, dwVerInfoSize, lpvMem);
if (VerQueryValue(lpvMem, "\\StringFileInfo\\040904E4\\FileVersion",
reinterpret_cast<void**>(&lszVer), &cchVer))
{
SetDlgItemText(hDlg, IDC_VERSION, lszVer);
}
else
SetDlgItemText(hDlg, IDC_VERSION, "N/A");
GlobalUnlock(hMem);
GlobalFree(hMem);
}
// Create the columns Action, Date, Time for the listbox
HWND hWndLog = GetDlgItem(hDlg, IDC_LOG);
LV_COLUMN lvC;
lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvC.fmt = LVCFMT_LEFT; // left-align column
lvC.pszText = szText;
for (index = 0; index < NCOLS; index++)
{
// NOTE: IDS_ACTION = 220
// IDS_DATE = 230
// IDS_TIME = 240
lvC.iSubItem = index;
lvC.cx = 85;
LoadString(hInstance, IDS_ACTION + (index * 10), szText, sizeof(szText));
ListView_InsertColumn(hWndLog, index, &lvC);
}
log_info* liTemp = log_entry->next;
LV_ITEM lvI;
lvI.cchTextMax = sizeof(liTemp->log_action);
lvI.mask = LVIF_TEXT;
for (index = 0; liTemp->log_action; index++, liTemp = liTemp->next)
{
lvI.iItem = index;
lvI.iSubItem = 0;
lvI.pszText = liTemp->log_action;
ListView_InsertItem(hWndLog, &lvI);
ListView_SetItemText(hWndLog, index, 0, lvI.pszText);
lvI.iSubItem = 1;
lvI.pszText = liTemp->log_date;
ListView_InsertItem(hWndLog, &lvI);
ListView_SetItemText(hWndLog, index, 1, lvI.pszText);
lvI.iSubItem = 2;
lvI.pszText = liTemp->log_time;
ListView_InsertItem(hWndLog, &lvI);
ListView_SetItemText(hWndLog, index, 2, lvI.pszText);
}
}
break;
case WM_NOTIFY:
switch (((LPNMHDR) lParam)->code)
{
case PSN_KILLACTIVE:
SetWindowLongPtr(hDlg, DWLP_MSGRESULT, FALSE);
break;
}
break;
}
returnFALSE;
}
THREAD_ENTRY_DECLARE swap_icons(THREAD_ENTRY_PARAM param)
{
/******************************************************************************
*
* S w a p I c o n s
*
******************************************************************************
*
* Description: Animates the icon if the server restarted
*****************************************************************************/
Firebird::ContextPoolHolder threadContext(getDefaultMemoryPool());
HWND hWnd = static_cast<HWND>(param);
HINSTANCE hInstance = (HINSTANCE) GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
HICON hIconNormal = (HICON)
LoadImage(hInstance, MAKEINTRESOURCE(IDI_IBGUARD), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);