-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP2PeerService.cpp
More file actions
1294 lines (1189 loc) · 40.7 KB
/
Copy pathP2PeerService.cpp
File metadata and controls
1294 lines (1189 loc) · 40.7 KB
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
// Copyright © 2009-2013, 2026 Ivyware Pty Ltd, Khrustal & Mann
// MELBOURNE, VICTORIA, AUSTRALIA, 3000
//
// Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// P2PeerService definitions and prototypes
// NOTES: Windows service container for the deployment of P2PeerHub's
// P2PeerMsg's between objects derived from this base class
// : Only P2PeerHub derived network objects may be assigned
// network P2Paddress's
#include "stdafx.h"
#include <windows.h>
#include "P2Pwin32.h"
#include "P2PeerService.h"
#include "Msgexception.h"
#if defined(_WIN32)
#include "TargetcoreEvt.h" // generated from TargetcoreEvt.mc, committed.
// Guarded so the Linux build needs no
// mc.exe output: everything it declares
// is used only from Windows-only code.
#endif
using namespace std;
//
// ServiceStart() definitions
/*VOID WINAPI
P2PeerService_ServiceStart1 ( DWORD argc, LPTSTR *argv )
{
// Firstly globals
CexGlobalsInit ( );
// Delegate
MscsServiceStart ( argc, argv );
}*/
//extern
//P2PeerService *g_pP2PeerService1;
//VOID WINAPI
//P2PeerService_ServiceStart2 ( DWORD argc, LPTSTR *argv );
//extern
//P2PeerService *g_pP2PeerService2;
//VOID WINAPI
//P2PeerService_ServiceStart3 ( DWORD argc, LPTSTR *argv );
//extern
//P2PeerService *g_pP2PeerService3;
//
// Win32 Service Control handler
// NOTES: Default implementation template
//
// Parameters: DWORD dwCtrlCode
// Control code as passed to the registered handler
// Refer RegisterServiceCtrlHandler() for further
// details
P2PeerService *g_pP2PeerService = 0;
VOID WINAPI
Win32ServiceServiceMain ( DWORD argc, LPTSTR *argv )
{
// Delegate to global P2PeerService instance
g_pP2PeerService -> Service ( argc, argv );
}
VOID WINAPI
Win32ServiceControlHandler ( DWORD dwCtrlCode )
{
// Delegate to global P2PeerService instance
g_pP2PeerService -> ServiceCtrlHandler ( dwCtrlCode );
}
///////////////////////////////////////////////////////////////////////////////
// Contructors and destructor
P2PeerService::P2PeerService ( LPCTSTR lpszServiceName
, LPSERVICE_MAIN_FUNCTION fptrServiceMain
, LPHANDLER_FUNCTION fptrServiceCtrlHandler )
{
// Firstly
RenderThisSafe ( );
m_fptrServiceMain = fptrServiceMain;
m_fptrServiceCtrlHandler = fptrServiceCtrlHandler;
_tcscpy_s ( m_szServiceName, ARRAYSIZE(m_szServiceName), lpszServiceName );
// Customised function pointers
//m_pServiceRun = 0;
}
P2PeerService::~P2PeerService( void )
{
// Garbage collection
// NOTES: CloseHub() first, and the delete second. Run() already closes the
// hub on the way out, so on the ordinary path this finds nothing to
// do - but a service destroyed without Run() having returned (a
// SpawnHub from SERVICE_CONTROL_CONTINUE, a constructor that throws
// after the spawn) would otherwise delete a hub whose pump thread
// is still dispatching virtuals through it. ~P2PeerHub cannot close
// that window from where it sits; the owner has to, and here the
// owner is us. Refer the note on ~P2PeerHub.
if ( m_pP2PeerHub )
{
m_pP2PeerHub -> CloseHub ( );
delete m_pP2PeerHub;
}
}
void
P2PeerService::RenderThisSafe ( )
{
m_pP2PeerHub = 0;
m_nMaxHubs = 16; // Within service address space
m_nMaxPumps = 8; // Default hub pump count; subclasses override
m_bDebug = false; // Was left uninitialised: IsDebugMode() and the
// hub-less service Run() path both read it
memset( m_szServiceName, 0, ARRAYSIZE(m_szServiceName) );
memset( &m_oServiceStatus, 0, sizeof(m_oServiceStatus) );
memset( &m_oDispatchTable[0], 0, sizeof(m_oDispatchTable) );
m_hServiceStatus = 0;
// Interbal status and mode flags
m_bStarted = false;
m_bPaused = false;
m_bConsole = false;
m_bSystem = false;
m_bService = false;
m_bExitMode = false;
// Default service control status
m_oServiceStatus.dwServiceType = SERVICE_WIN32;
m_oServiceStatus.dwCurrentState = SERVICE_START_PENDING;
m_oServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP
| SERVICE_ACCEPT_PAUSE_CONTINUE
| SERVICE_ACCEPT_SHUTDOWN;
}
//
// Connects the main thread of a service process to the service
// control manager, which causes the thread to be the service control
// dispatcher thread for the calling process.
// NOTES: Delegates to StartServiceCtrlDispatcher()
//
//
// Parameters: const SERVICE_TABLE_ENTRY *pServiceTable
// Refer StartServiceCtrlDispatcher() for further details
//
DWORD
P2PeerService::StartDispatcher ( const SERVICE_TABLE_ENTRY *pServiceTable )
{
// Direct delegation
ASSERT ( IsServiceMode() );
BOOL bResult = StartServiceCtrlDispatcher ( pServiceTable );
if ( !bResult )
EVERR->MODULE
->Message("StartServiceCtrlDispatcher() failed" )
->HResult( GetLastError() )
->SetLast( );
return bResult;
}
DWORD P2PeerService::Startup( void )
{
//== initialize the dispatch table
m_oDispatchTable[0].lpServiceName = m_szServiceName;
m_oDispatchTable[0].lpServiceProc = m_fptrServiceMain;
//== starts the service
if( !StartServiceCtrlDispatcher( m_oDispatchTable ) )
{
DWORD dwError = GetLastError();
EVERR->MODULE
->Message("StartServiceCtrlDispatcher() failed" )
->HResult( dwError )
->SetLast( );
return dwError;
}
return NO_ERROR;
}
int P2PeerService::Service ( DWORD argc, LPTSTR* argv )
{
m_hServiceStatus
= RegisterServiceCtrlHandler ( m_szServiceName
, m_fptrServiceCtrlHandler );
if( (SERVICE_STATUS_HANDLE)0 == m_hServiceStatus )
{
DWORD dwError = GetLastError();
EVERR->MODULE
->Message("RegisterServiceCtrlHandler() failed" )
->HResult( dwError )
->SetLast( );
return dwError;
}
if( Init( argc, argv ) != NO_ERROR )
{
ChangeStatus( SERVICE_STOPPED );
return GetLastError();
}
ChangeStatus( SERVICE_RUNNING );
return Run ( );
}
//
// Service control handler
// NOTES: Default handler for all P2PeerService derived objects
//
//
// Parameters: DWORD dwOpcode
// Operations code passed from the service control
// manager.
//
void
P2PeerService::ServiceCtrlHandler ( DWORD dwOpcode )
{
switch ( dwOpcode )
{
case SERVICE_CONTROL_PAUSE:
ChangeStatus( SERVICE_PAUSE_PENDING );
if ( OnPause() == NO_ERROR )
{
m_bPaused = true;
ChangeStatus( SERVICE_PAUSED );
}
break;
case SERVICE_CONTROL_CONTINUE:
ChangeStatus( SERVICE_CONTINUE_PENDING );
if ( OnContinue() == NO_ERROR )
{
m_bPaused = false;
ChangeStatus( SERVICE_RUNNING );
}
break;
case SERVICE_CONTROL_STOP:
ChangeStatus( SERVICE_STOP_PENDING );
OnStop();
ChangeStatus( SERVICE_STOPPED );
break;
case SERVICE_CONTROL_SHUTDOWN:
ChangeStatus( SERVICE_STOP_PENDING );
OnShutdown();
ChangeStatus( SERVICE_STOPPED );
break;
case SERVICE_CONTROL_INTERROGATE:
OnInterrogate();
SetServiceStatus( m_hServiceStatus, &m_oServiceStatus );
break;
default:
OnUserControl( dwOpcode );
SetServiceStatus( m_hServiceStatus, &m_oServiceStatus );
break;
};
}
//
// Checks for and processes default service arguments
// NOTES: This default argument set defines the default P2PeerService
// environment
//
// Parameters: LPTSTR *argv
// Argument to be processed
//
// bool bImplement
// true... Implement decoded option
// false.. Validation and object synchronisation
bool
P2PeerService::DefaultMainArgs ( LPTSTR argv[], DWORD& argn, bool bImplement )
{
// Locals
bool bResult = false;
int a = argn, b = argn;
size_t iSize = _tcslen ( argv[a] );
// Because users are involved
try
{
// Debug mode
if ( _tcsnicmp ( argv[a], _T("-Debug"), max(2,iSize) ) == 0 ||
_tcsnicmp ( argv[a], _T("/Debug"), max(2,iSize) ) == 0 )
{
m_bDebug = true;
if ( bImplement )
P2Pevent::Configure ( P2Pevent::ADDMASK
, P2Pevotn_ERROR | P2Pevotn_DEBUG );
bResult = true;
}
// Console mode
// NOTES: Flags execution from Visual Studio or from a DOS Window,
// negates StartServiceCtrlDispatcher() call
// : Implementation -C[onsole] or /C[onsole]
else if ( _tcsnicmp ( argv[a], _T("-Console"), max(2,iSize) ) == 0 ||
_tcsnicmp ( argv[a], _T("/Console"), max(2,iSize) ) == 0 )
{
m_bConsole = true;
bResult = true;
}
// Service mode
// NOTES: Flags execution as a formal service for which a
// StartServiceCtrlDispatcher() call is ultimately performed
// : Implementation -S[ervice] or /S[ervice]
else if ( _tcsnicmp ( argv[a], _T("-Service"), max(2,iSize) ) == 0 ||
_tcsnicmp ( argv[a], _T("/Service"), max(2,iSize) ) == 0 )
{
m_bService = true;
bResult = true;
}
// Register as service
// NOTES: Activates system mode and negates operation as a service
else if ( _tcsnicmp ( argv[a], _T("-Install"), max(2,iSize) ) == 0 ||
_tcsnicmp ( argv[a], _T("/Install"), max(2,iSize) ) == 0 ||
_tcsnicmp ( argv[a], _T("/REGSERVER"), max(4,iSize) ) == 0 )
{
if ( bImplement )
Install ( );
m_bSystem = true;
bResult = true;
}
// Unregister as service
// NOTES: /REGSERVER is a special case in which the
// service is automatically removed
// : Activates system mode and negates operation as a service
else if ( _tcsnicmp ( argv[a], _T("-Remove"), max(2,iSize) ) == 0 ||
_tcsnicmp ( argv[a], _T("/Remove"), max(2,iSize) ) == 0 ||
_tcsnicmp ( argv[a], _T("/UNREGSERVER"), max(2,iSize) ) == 0 )
{
if ( bImplement )
UnInstall ( );
m_bSystem = true;
bResult = true;
}
// Exit mode
// NOTES: Used for setting registry parameters and negates operation
// as either a console application or service
else if ( _tcsnicmp ( argv[a], _T("-Xit"), max(2,iSize) ) == 0 ||
_tcsnicmp ( argv[a], _T("/Xit"), max(2,iSize) ) == 0 ||
_tcsnicmp ( argv[a], _T("-eXit"), max(3,iSize) ) == 0 ||
_tcsnicmp ( argv[a], _T("/eXit"), max(3,iSize) ) == 0 )
{
m_bExitMode = true;
bResult = true;
}
}
// Exceptions
catch ( P2Pevent *pEVT )
{
pEVT -> SetLast();
}
catch ( ... )
{
EVERR->Module ( __FUNCTION__ )
->Message("Unknown exception")
->SetLast();
}
// Tidy up and
if ( bResult )
argn = b; // Comsumed arguments
return bResult; // Consumed arguments
}
//
// Checks for and processes -Help service arguments
// NOTES: The contained argument set defines the default P2PeerService
// -Help environment
//
// Parameters: LPTSTR argv[]
// Argument to be processed
//
// DWORD& argn
// Primary argument to be checked
//
// bool bImplement
// true... Implement decoded option
// false.. Validation and object synchronisation
bool
P2PeerService::HelpMainArgs ( LPTSTR argv[], DWORD& argn, bool bImplement )
{
// Locals
bool bResult = false;
int a = argn, b = argn;
size_t iSize = _tcslen ( argv[a] );
// Because users are involved
try
{
// Help
// NOTES: Simple TTY expression of default set of execution arguments
if ( _tcsnicmp ( argv[a], _T("-Help"), max(2,iSize) ) == 0 ||
_tcsnicmp ( argv[a], _T("/Help"), max(2,iSize) ) == 0 ||
_tcsnicmp ( argv[a], _T("-?"), 2 ) == 0 ||
_tcsnicmp ( argv[a], _T("/?"), 2 ) == 0 )
{
m_bConsole = true;
if ( bImplement )
{
_tprintf ( _T("\n") );
_tprintf ( _T(" -C[onsole]\n") );
_tprintf ( _T(" Run from a DOS window or Visual Studio\n") );
_tprintf ( _T(" -S[ervice]\n") );
_tprintf ( _T(" Run as a Windows service\n") );
_tprintf ( _T(" -I[nstall]\n") );
_tprintf ( _T(" Windows service installation (also REGSERVER)\n") );
_tprintf ( _T(" -R[emove]\n") );
_tprintf ( _T(" Windows service removal (also UNREGISTER)\n") );
_tprintf ( _T(" -[e]X[it]\n") );
_tprintf ( _T(" -Exit application immediately after processing command arguments\n") );
}
bResult = true;
}
}
// Exceptions
catch ( ... )
{
EVERR->Module ( __FUNCTION__ )
->AFP(*argv)->AFP(argn)->AFP(bImplement)
->Message(_T("Unknown exception"))
->SetLast();
}
// Tidy up and
if ( bResult )
argn = b; // Comsumed arguments
return bResult; // Consumed arguments
}
//
// Checks for and processes default service arguments
// NOTES: This default argument set defines the default P2PeerService
// environment
//
// Parameters: LPTSTR argv
// Argument to be processed
//
// DWORD argn
// Processed argument list position
//
// bool bImplement
// true... Implement decoded option
// false.. Validation and object synchronisation
//
// Returns: bool
// Processing summary
// true... Argument identified and processed
// GetP2Pevent() summarises processing result
// false.. Argument NOT identified
bool
P2PeerService::DefaultDebugArgs ( LPTSTR argv[], DWORD& argn, bool bImplement )
{
// Locals
bool bResult = false;
int a = argn, b = argn;
size_t iSize = _tcslen ( argv[a] );
// Debug mode
if ( wmemicmp ( argv[a], L"-Debug", max(2,iSize) ) == 0 ||
wmemicmp ( argv[a], L"/Debug", max(2,iSize) ) == 0 )
{
m_bDebug = true;
if ( bImplement )
P2Pevent::Configure ( P2Pevent::ADDMASK
, P2Pevotn_ERROR | P2Pevotn_DEBUG );
bResult = true;
}
// Tidy up, and
if ( bResult )
argn = b + 1;
return bResult;
}
///////////////////////////////////////////////////////////////////////
// Diagnostic destination for a hosted hub
// NOTES: A service under the SCM has no console, no standard error and no
// viewable desktop. Before this, a P2Pevent in that shape became a
// modal MessageBox on whichever thread raised it - the hub's own PUMP
// for anything reached through P2PeerCon::OnClose - and CloseHub()
// then waited on that pump without a bound, deliberately, because
// bounding it would trade a hang for a use-after-free. A service
// could therefore deadlock its own teardown on any diagnostic.
// : Msgcore now refuses the dialog by itself wherever nobody could
// dismiss one (refer P2PeventDialogViewable in Msgexception.cpp), so
// the hang is closed without configuration. Refusing it is only half
// the problem: the text then goes to a standard error that is not
// there, and a defect that used to hang loudly would vanish quietly.
// This is the other half - the destination.
// : WINDOWS ONLY, and not an oversight. This translation unit is common
// to both platforms - the SCM is shimmed to no-op on Linux - but the
// event log is not a portable concept, and the Platform shim carries
// neither RegisterEventSource nor a registry. Neither is there a
// defect to answer off Windows: MessageBoxEx is a stub there, text is
// unconditional, and a daemon keeps a writable stderr. Routing to
// syslog or the journal is its own decision, not this one's.
//
#if defined(_WIN32)
static HANDLE s_hP2PeventLog = 0;
static P2PeventTextFnc s_pfnP2PeventWas = nullptr;
//
// Writes one event to the Windows application log
// NOTES: Installed as the P2Pevent text sink for the duration of a service
// mode Run(). Refer P2PeventTextFnc for the thread contract: this is
// called on the thread that RAISED the event, so it must not block.
// : IT MUST NOT RAISE A P2Pevent, ever, however it fails. Display()
// reaches this function, so an event raised from here would re-enter
// Display() and recurse until the stack is gone. Every failure below
// is therefore silent by construction, not by oversight.
//
// Parameters: P2Pevent_e eClass
// Class of the event being reported.
//
// LPCWSTR lpszOrigin
// "[service]module(parameters)" - insertion %1.
//
// LPCWSTR lpszText
// Assembled message body - insertion %2.
//
static void WINAPI
P2PeerServiceEventLogSink ( P2Pevent_e eClass
, LPCWSTR lpszOrigin
, LPCWSTR lpszText )
{
// Read the handle ONCE. The teardown in Run() unhooks this sink before it
// deregisters the source, but a thread already inside here holds whatever
// it read on entry rather than racing the store.
HANDLE hLog = s_hP2PeventLog;
if ( !hLog )
return;
// Class to catalogue entry. The catalogue's message ids ARE the
// P2Pevent_e values - P2Pevent_ERROR is 1 and P2PMSG_EVT_ERROR is 0x...0001
// - which is why this is written out rather than computed: an enum that
// gains a class gets a compiler-visible gap here instead of silently
// reporting under a neighbour's id. Anything unlisted, including
// P2Pevent_UNDEF and the P2Pevent_USERn range, still renders its text
// under P2PMSG_EVT_UNCLASSED.
DWORD dwEventID = P2PMSG_EVT_UNCLASSED;
WORD wType = EVENTLOG_WARNING_TYPE;
switch ( eClass )
{
case P2Pevent_ERROR:
dwEventID = P2PMSG_EVT_ERROR; wType = EVENTLOG_ERROR_TYPE; break;
case P2Pevent_WARNING:
dwEventID = P2PMSG_EVT_WARNING; wType = EVENTLOG_WARNING_TYPE; break;
case P2Pevent_INFO:
dwEventID = P2PMSG_EVT_INFO; wType = EVENTLOG_INFORMATION_TYPE; break;
case P2Pevent_DEBUG:
dwEventID = P2PMSG_EVT_DEBUG; wType = EVENTLOG_INFORMATION_TYPE; break;
case P2Pevent_TRACE:
dwEventID = P2PMSG_EVT_TRACE; wType = EVENTLOG_INFORMATION_TYPE; break;
case P2Pevent_LOG:
dwEventID = P2PMSG_EVT_LOG; wType = EVENTLOG_INFORMATION_TYPE; break;
case P2Pevent_REPORT:
dwEventID = P2PMSG_EVT_REPORT; wType = EVENTLOG_INFORMATION_TYPE; break;
default:
break;
}
// Both insertions are always supplied, empty rather than absent, because
// the catalogue references %1 and %2 unconditionally and a missing
// insertion renders as the literal placeholder.
LPCWSTR apszInsert[2] = { lpszOrigin ? lpszOrigin : L""
, lpszText ? lpszText : L"" };
::ReportEventW ( hLog, wType, 0, dwEventID, 0, 2, 0, apszInsert, 0 );
}
//
// Names the module that carries the event message table
// NOTES: NOT the host executable. GetModuleFileName(0) would name the .exe,
// which carries no message table, and the Event Viewer would then
// report every entry as unformattable. The table is linked into THIS
// module (Targetcore, from TargetcoreEvt.mc), so the module is found
// from the address of code that lives in it.
//
// Parameters: CStringW& strModule
// Receives the full path on success, untouched on failure.
//
// Returns: bool
// true ... path resolved
static bool
P2PeerServiceEventModule ( CStringW& strModule )
{
HMODULE hThis = 0;
if ( !::GetModuleHandleExW ( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
, (LPCWSTR)&P2PeerServiceEventLogSink
, &hThis ) )
return false;
WCHAR szModule[MAX_PATH] = { 0 };
DWORD dwLength = ::GetModuleFileNameW ( hThis, szModule, ARRAYSIZE(szModule) );
if ( dwLength == 0 || dwLength >= ARRAYSIZE(szModule) )
return false; // failed, or truncated - do not register a partial path
strModule = szModule;
return true;
}
#endif // _WIN32 - event log destination
//
// Service installation
// NOTES: Skips operation if service already installed
// : Usually activated by one of following execution arguments
// -I[nstall]
// /I[nstall]
// /REG[SERVER]
// : Refer complimentary UnInstall() for removal of installed
// services
//
// Returns: BOOL
// Operation summary
// TRUE... Successful
// FALSE.. Failed, refer GetP2Pevent() for details
BOOL
P2PeerService::Install ( )
{
// Check existing state
if ( IsInstalled ( m_szServiceName ) )
return TRUE;
// Open the Service Control Manager
SafeSCHandle shSCMgr
= OpenSCManager ( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if ( shSCMgr == NULL )
{
EVERR->MODULE
->Message("OpenSCManager() failed" )
->HResult( GetLastError() )
->SetLast( );
return FALSE;
}
// Fetch full path of this executable
TCHAR szFilePath[_MAX_PATH];
::GetModuleFileName ( NULL, szFilePath, ARRAYSIZE(szFilePath) );
// Win32 implementation
SafeSCHandle shSrv
= CreateService ( shSCMgr
, m_szServiceName
, m_szServiceName
, SERVICE_ALL_ACCESS
, SERVICE_WIN32_OWN_PROCESS
, SERVICE_DEMAND_START
, SERVICE_ERROR_NORMAL
, szFilePath
, NULL
, NULL
, NULL
, NULL
, NULL );
if ( shSrv == NULL )
{
EVERR->MODULE
->Message(L"CreateService(%s) failed", m_szServiceName )
->HResult( GetLastError() )
->SetLast( );
return FALSE;
}
// Tidy up, and
// NOTES: Observe post Install() management of registry entries etc
PostInstall ( );
shSrv = 0;
shSCMgr = 0;
return TRUE;
}
//
// Registers this service as an event log source
// NOTES: Reached from Install(), which the SCM already requires to be
// elevated - so this is the one point in the life cycle where HKLM is
// writable and the registration can be made.
// : Without it ReportEvent still succeeds and the text is still present,
// but the Event Viewer has no message table to format it with and
// shows "The description for Event ID (1) ... cannot be found" with
// the text demoted to raw insertion data. So a failure here degrades
// the presentation and loses nothing.
// : EventMessageFile names THIS module rather than the host .exe; refer
// P2PeerServiceEventModule().
//
void
P2PeerService::PostInstall ( )
{
#if !defined(_WIN32)
return; // no event log to register against
#else
CStringW strModule;
if ( !P2PeerServiceEventModule ( strModule ) )
{
EVERR->MODULE
->Message("Could not resolve the module carrying the event message table" )
->Advice_(_T("Service diagnostics will be written to the application log "
"but the Event Viewer will not be able to format them.") )
->Cancel();
return;
}
CStringW strKey;
strKey.Format ( L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s"
, (LPCWSTR)CStringW(m_szServiceName) );
HKEY hKey = 0;
LONG lResult = ::RegCreateKeyExW ( HKEY_LOCAL_MACHINE, strKey, 0, 0
, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE
, 0, &hKey, 0 );
if ( lResult != ERROR_SUCCESS )
{
EVERR->MODULE
->Message(L"RegCreateKeyEx(HKLM\\%s) failed", (LPCWSTR)strKey )
->HResult( lResult )
->Advice_(L"Run the install elevated to register the event log source." )
->Cancel();
return;
}
// REG_EXPAND_SZ is what the log reader expects here, and costs nothing for
// a path that happens to contain no variables to expand.
::RegSetValueExW ( hKey, L"EventMessageFile", 0, REG_EXPAND_SZ
, (const BYTE*)(LPCWSTR)strModule
, (DWORD)((strModule.GetLength() + 1) * sizeof(WCHAR)) );
DWORD dwTypes = EVENTLOG_ERROR_TYPE
| EVENTLOG_WARNING_TYPE
| EVENTLOG_INFORMATION_TYPE;
::RegSetValueExW ( hKey, L"TypesSupported", 0, REG_DWORD
, (const BYTE*)&dwTypes, sizeof(dwTypes) );
::RegCloseKey ( hKey );
#endif // _WIN32
}
//
// Service removal
// NOTES: Skips operation if service already installed
// : Usually activated by one of following execution arguments
// -R[nstall]
// /R[nstall]
// /UNREG[SERVER]
// : Refer complimentary UnInstall() for removal of installed
// services
//
// Returns: BOOL
// Operation summary
// TRUE... Successful
// FALSE.. Failed, refer GetP2Pevent() for details
BOOL
P2PeerService::UnInstall ( )
{
// Redundancy check
if ( !IsInstalled(m_szServiceName) )
return TRUE;
// Open the Service Control Manager
SetLastError ( 0 ); // Clear failure flag
SafeSCHandle shSCMgr
= OpenSCManager ( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if ( shSCMgr == NULL )
{
EVERR->MODULE
->Message("OpenSCManager() failed" )
->HResult( GetLastError() )
->SetLast( );
return FALSE;
}
// Isloate service
SafeSCHandle shSrv = OpenService ( shSCMgr, m_szServiceName, DELETE );
if ( shSrv == NULL )
{
EVERR->MODULE
->Message("OpenService() failed" )
->HResult( GetLastError() )
->SetLast( );
return FALSE;
}
// Delete service
if ( !DeleteService(shSrv) )
{
EVERR->MODULE
->Message("DeleteService() failed" )
->HResult( GetLastError() )
->SetLast( );
return FALSE;
}
// Tidy up, and
// NOTES: Observe post UnInstall() management of registry entries etc
PostUnInstall ( );
shSrv = 0;
shSCMgr = 0;
return TRUE;
}
//
// Removes the event log source registration
// NOTES: Complements PostInstall(). Leaving the key behind would name a
// module that uninstall may have removed, and the Event Viewer would
// then fail to format entries already in the log.
// : The key carries values only, never subkeys, so RegDeleteKey is
// sufficient and its failure is not worth reporting: an uninstall that
// has already removed the service should not fail over a stale
// presentation hint, and the common failure here is simply that the
// key was never registered because the install was not elevated.
//
void
P2PeerService::PostUnInstall ( )
{
#if defined(_WIN32)
CStringW strKey;
strKey.Format ( L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s"
, (LPCWSTR)CStringW(m_szServiceName) );
::RegDeleteKeyW ( HKEY_LOCAL_MACHINE, strKey );
#endif
}
//
// Summarise installed status of passed service name
// NOTES: Passed service name resolves to the "Name" column in
// the Windows Service Control Manager
//
// Parameters: LPCTSTR lpszServiceName
// Name of the service to be checked for installation
//
// Returns: bool
// Installed service summary
// true... Installed
// false.. Not installed or error
bool
P2PeerService::IsInstalled ( LPCTSTR lpszServiceName )
{
// Locals
bool bResult = false;
// Open the Service Control Manager
SetLastError ( 0 ); // Clear failure flag
SafeSCHandle shSCMgr = ::OpenSCManager ( NULL, NULL
, SC_MANAGER_ALL_ACCESS );
if ( shSCMgr == NULL )
{
EVERR->MODULE->AFP(lpszServiceName)
->Message ("OpenSCManager() failed" )
->HResult( GetLastError() )
->SetLast ( );
return false;
}
// Now open the service
SafeSCHandle shSrv = OpenService ( shSCMgr, lpszServiceName
, SERVICE_QUERY_CONFIG );
if ( shSrv != NULL )
bResult = true;
// Tidy up, and
shSrv = 0;
shSCMgr = 0;
return bResult;
}
DWORD
P2PeerService::Init ( DWORD argc, LPTSTR* argv )
{
argc;
argv;
return NO_ERROR;
}
///////////////////////////////////////////////////////////////////////
// Service operation
//
// Service execution entry point
// NOTES: For consistency -C[onsole] mode also utilises this entry point
// but spawns the hub off into its own context and runs a console
// from this context
int
P2PeerService::Run ( )
{
// Environmental
// NOTES: Demand initialisation of P2Pmsg'ing environment
int iResult = 0;
//ASSERT(m_nMaxHubs>0);
//StartupP2Pmsg ( m_nMaxHubs );
//P3PmsgBSTR::SetDefaultP2Pmsgnn ( VBLock_Addr32 );
WSADATA oWsaData;
WORD wVersionRequested = MAKEWORD( 2, 0 );
if ( WSAStartup ( wVersionRequested, &oWsaData ) )
{
EVERR->MODULE
->Message("WSAStartup() failed" )
->HResult( WSAGetLastError() )
->Cancel();
return 1;
}
// Initiate and run contained hub as service
// NOTES: In Service mode the hub locks up this processing context
// Console mode spawns hub off into another context
// : Hence the Service and Console threading models are subtly
// different
if ( IsServiceMode() )
{
// Diagnostics FIRST, before anything that can raise one. CreateHub()
// below is perfectly capable of failing, and the point of this is that
// its failure is reported rather than raised at a desktop nobody is
// looking at.
// ForceTextOutput() is belt and braces: Msgcore already refuses a dialog
// where none could be dismissed, and this states the same intent at the
// deployment boundary, where a reader of THIS file can see it. It also
// covers a host that has somehow arranged a viewable station for itself.
P2Pevent::ForceTextOutput ( true );
#if defined(_WIN32)
s_hP2PeventLog = ::RegisterEventSourceW ( 0, CStringW(m_szServiceName) );
if ( s_hP2PeventLog )
s_pfnP2PeventWas = P2Pevent::SetTextSink ( &P2PeerServiceEventLogSink );
#endif
ASSERT(m_pP2PeerHub);
ASSERT(m_pP2PeerHub->GetHubID()<=0);
ASSERT(m_nMaxPumps>0);
m_pP2PeerHub -> CreateHub ( m_strP2PaddrHub, m_nMaxPumps );
if ( m_pP2PeerHub->GetHubID() > 0 )
m_pP2PeerHub -> RunHub ( );
}
// Initiate and run contained hub as console
// NOTES: In Console mode the hub is spawned off into its own
// processing context and this context by default becomes
// the operator console
// : Hence the Service and Console threading models are subtly
// different
else if ( IsConsoleMode() )
{
ASSERT(m_pP2PeerHub);
ASSERT(m_pP2PeerHub->GetHubID()<=0);
m_pP2PeerHub -> SpawnHub ( );
iResult = RunConsoleModeAttachment ( );
}
// Tidy up
// NOTES: Common to both Service and Console modes
m_pP2PeerHub -> CloseHub ( );
// Retire the event log sink, in this order and only AFTER CloseHub().
// Unhooking before deregistering means no thread can pick the sink up once
// the handle is closed; and CloseHub() has already retired every pump, so
// there is no thread left inside it holding the handle it read on entry.
// Doing this before CloseHub() would drop exactly the diagnostics teardown
// is most likely to raise.
#if defined(_WIN32)
if ( s_hP2PeventLog )
{
P2Pevent::SetTextSink ( s_pfnP2PeventWas );
s_pfnP2PeventWas = nullptr;
::DeregisterEventSource ( s_hP2PeventLog );
s_hP2PeventLog = 0;
}
#endif
CleanupP2Pmsg ( );
return 0;
}
int
P2PeerService::RunConsoleModeAttachment ( )
{
// Locals
int nRetCode = 0;
TCHAR cOption = 0;
// Assume problematic environment
TOP:try
{
// Until terminated
while ( cOption != 'Q' &&
cOption != 'q' )